mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
search script UI
This commit is contained in:
parent
ee78413ecb
commit
a1f67e830d
@ -47,8 +47,8 @@ async function createSearchNote(opts = {}) {
|
|||||||
const note = await server.post('search-note');
|
const note = await server.post('search-note');
|
||||||
|
|
||||||
const attrsToUpdate = [
|
const attrsToUpdate = [
|
||||||
opts.ancestor ? { type: 'relation', name: 'ancestor', value: opts.ancestorNoteId } : undefined,
|
opts.ancestorNoteId ? { type: 'relation', name: 'ancestor', value: opts.ancestorNoteId } : undefined,
|
||||||
{ type: 'label', name: 'searchString', value: opts.searchString }
|
{ type: 'label', name: 'searchString', value: opts.searchStringe }
|
||||||
].filter(attr => !!attr);
|
].filter(attr => !!attr);
|
||||||
|
|
||||||
if (attrsToUpdate.length > 0) {
|
if (attrsToUpdate.length > 0) {
|
||||||
|
@ -167,7 +167,7 @@ class TreeCache {
|
|||||||
for (const note of resp.notes) {
|
for (const note of resp.notes) {
|
||||||
if (note.type === 'search') {
|
if (note.type === 'search') {
|
||||||
const searchResultNoteIds = await server.get('search-note/' + note.noteId);
|
const searchResultNoteIds = await server.get('search-note/' + note.noteId);
|
||||||
|
console.log("searchResultNoteIds", searchResultNoteIds);
|
||||||
if (!Array.isArray(searchResultNoteIds)) {
|
if (!Array.isArray(searchResultNoteIds)) {
|
||||||
throw new Error(`Search note ${note.noteId} failed: ${searchResultNoteIds}`);
|
throw new Error(`Search note ${note.noteId} failed: ${searchResultNoteIds}`);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import server from "../../services/server.js";
|
import server from "../../services/server.js";
|
||||||
import ws from "../../services/ws.js";
|
import ws from "../../services/ws.js";
|
||||||
import Component from "../component.js";
|
import Component from "../component.js";
|
||||||
|
import utils from "../../services/utils.js";
|
||||||
|
|
||||||
export default class AbstractSearchAction extends Component {
|
export default class AbstractSearchAction extends Component {
|
||||||
constructor(attribute, actionDef) {
|
constructor(attribute, actionDef) {
|
||||||
@ -18,6 +19,8 @@ export default class AbstractSearchAction extends Component {
|
|||||||
.on('click', () => this.deleteAction())
|
.on('click', () => this.deleteAction())
|
||||||
.attr('title', 'Remove this search action');
|
.attr('title', 'Remove this search action');
|
||||||
|
|
||||||
|
utils.initHelpDropdown($rendered);
|
||||||
|
|
||||||
return $rendered;
|
return $rendered;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -21,6 +21,10 @@ const TPL = `
|
|||||||
For example to append a string to a note's title, use this small script:
|
For example to append a string to a note's title, use this small script:
|
||||||
|
|
||||||
<pre>note.title = note.title + ' - suffix';</pre>
|
<pre>note.title = note.title + ' - suffix';</pre>
|
||||||
|
|
||||||
|
More complex example would be deleting all matched note's attributes:
|
||||||
|
|
||||||
|
<pre>for (const attr of note.getOwnedAttributes) { attr.isDeleted = true; attr.save(); }</pre>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import noteAutocompleteService from "../services/note_autocomplete.js";
|
|
||||||
import server from "../services/server.js";
|
import server from "../services/server.js";
|
||||||
import TabAwareWidget from "./tab_aware_widget.js";
|
import TabAwareWidget from "./tab_aware_widget.js";
|
||||||
import treeCache from "../services/tree_cache.js";
|
import treeCache from "../services/tree_cache.js";
|
||||||
@ -18,12 +17,13 @@ import FastSearch from "./search_options/fast_search.js";
|
|||||||
import Ancestor from "./search_options/ancestor.js";
|
import Ancestor from "./search_options/ancestor.js";
|
||||||
import IncludeArchivedNotes from "./search_options/include_archived_notes.js";
|
import IncludeArchivedNotes from "./search_options/include_archived_notes.js";
|
||||||
import OrderBy from "./search_options/order_by.js";
|
import OrderBy from "./search_options/order_by.js";
|
||||||
|
import SearchScript from "./search_options/search_script.js";
|
||||||
|
|
||||||
const TPL = `
|
const TPL = `
|
||||||
<div class="search-definition-widget">
|
<div class="search-definition-widget">
|
||||||
<style>
|
<style>
|
||||||
.search-setting-table {
|
.search-setting-table {
|
||||||
margin-top: 7px;
|
margin-top: 0;
|
||||||
margin-bottom: 7px;
|
margin-bottom: 7px;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
border-collapse: separate;
|
border-collapse: separate;
|
||||||
@ -63,6 +63,10 @@ const TPL = `
|
|||||||
.search-definition-widget input:invalid {
|
.search-definition-widget input:invalid {
|
||||||
border: 3px solid red;
|
border: 3px solid red;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.add-search-option button {
|
||||||
|
margin-top: 5px; /* to give some spacing when buttons overflow on the next line */
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<div class="search-settings">
|
<div class="search-settings">
|
||||||
@ -75,6 +79,11 @@ const TPL = `
|
|||||||
search string
|
search string
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button type="button" class="btn btn-sm" data-search-option-add="searchScript">
|
||||||
|
<span class="bx bx-code"></span>
|
||||||
|
search script
|
||||||
|
</button>
|
||||||
|
|
||||||
<button type="button" class="btn btn-sm" data-search-option-add="ancestor">
|
<button type="button" class="btn btn-sm" data-search-option-add="ancestor">
|
||||||
<span class="bx bx-filter-alt"></span>
|
<span class="bx bx-filter-alt"></span>
|
||||||
ancestor
|
ancestor
|
||||||
@ -150,6 +159,7 @@ const TPL = `
|
|||||||
|
|
||||||
const OPTION_CLASSES = [
|
const OPTION_CLASSES = [
|
||||||
SearchString,
|
SearchString,
|
||||||
|
SearchScript,
|
||||||
Ancestor,
|
Ancestor,
|
||||||
FastSearch,
|
FastSearch,
|
||||||
IncludeArchivedNotes,
|
IncludeArchivedNotes,
|
||||||
@ -232,7 +242,12 @@ export default class SearchDefinitionWidget extends TabAwareWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async refreshResultsCommand() {
|
async refreshResultsCommand() {
|
||||||
await treeCache.reloadNotes([this.noteId]);
|
try {
|
||||||
|
await treeCache.reloadNotes([this.noteId]);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
toastService.showError(e.message);
|
||||||
|
}
|
||||||
|
|
||||||
this.triggerEvent('searchRefreshed', {tabId: this.tabContext.tabId});
|
this.triggerEvent('searchRefreshed', {tabId: this.tabContext.tabId});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import server from "../../services/server.js";
|
import server from "../../services/server.js";
|
||||||
import ws from "../../services/ws.js";
|
import ws from "../../services/ws.js";
|
||||||
import Component from "../component.js";
|
import Component from "../component.js";
|
||||||
|
import utils from "../../services/utils.js";
|
||||||
|
|
||||||
export default class AbstractSearchOption extends Component {
|
export default class AbstractSearchOption extends Component {
|
||||||
constructor(attribute, note) {
|
constructor(attribute, note) {
|
||||||
@ -28,6 +29,8 @@ export default class AbstractSearchOption extends Component {
|
|||||||
.on('click', () => this.deleteOption())
|
.on('click', () => this.deleteOption())
|
||||||
.attr('title', 'Remove this search option');
|
.attr('title', 'Remove this search option');
|
||||||
|
|
||||||
|
utils.initHelpDropdown($rendered);
|
||||||
|
|
||||||
return $rendered;
|
return $rendered;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -30,14 +30,18 @@ export default class Ancestor extends AbstractSearchOption {
|
|||||||
noteAutocompleteService.initNoteAutocomplete($ancestor);
|
noteAutocompleteService.initNoteAutocomplete($ancestor);
|
||||||
|
|
||||||
$ancestor.on('autocomplete:closed', async () => {
|
$ancestor.on('autocomplete:closed', async () => {
|
||||||
const ancestorOfNoteId = $ancestor.getSelectedNoteId();
|
const ancestorNoteId = $ancestor.getSelectedNoteId();
|
||||||
|
|
||||||
await this.setAttribute('relation', 'ancestor', ancestorOfNoteId);
|
if (ancestorNoteId) {
|
||||||
|
await this.setAttribute('relation', 'ancestor', ancestorNoteId);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const ancestorNoteId = this.note.getRelationValue('ancestor');
|
const ancestorNoteId = this.note.getRelationValue('ancestor');
|
||||||
|
|
||||||
$ancestor.setNote(ancestorNoteId);
|
if (ancestorNoteId !== 'root') {
|
||||||
|
$ancestor.setNote(ancestorNoteId);
|
||||||
|
}
|
||||||
|
|
||||||
return $option;
|
return $option;
|
||||||
}
|
}
|
||||||
|
70
src/public/app/widgets/search_options/search_script.js
Normal file
70
src/public/app/widgets/search_options/search_script.js
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import AbstractSearchOption from "./abstract_search_option.js";
|
||||||
|
import noteAutocompleteService from "../../services/note_autocomplete.js";
|
||||||
|
|
||||||
|
const TPL = `
|
||||||
|
<tr>
|
||||||
|
<td class="title-column">
|
||||||
|
Search script:
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div class="input-group">
|
||||||
|
<input class="search-script form-control" placeholder="search for note by its name">
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="button-column">
|
||||||
|
<div class="dropdown help-dropdown">
|
||||||
|
<span class="bx bx-help-circle icon-action" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
|
||||||
|
<div class="dropdown-menu dropdown-menu-right p-4">
|
||||||
|
<p>Search script allows to define search results by running a script. This provides maximal flexibility when standard search doesn't suffice.</p>
|
||||||
|
|
||||||
|
<p>Search script must be of type "code" and subtype "JavaScript backend". The script receives needs to return an array of noteIds or notes.</p>
|
||||||
|
|
||||||
|
<p>See this example:</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
// 1. prefiltering using standard search
|
||||||
|
const candidateNotes = api.searchForNotes("#journal");
|
||||||
|
|
||||||
|
// 2. applying custom search criteria
|
||||||
|
const matchedNotes = candidateNotes
|
||||||
|
.filter(note => note.title.match(/[0-9]{1,2}\. ?[0-9]{1,2}\. ?[0-9]{4}\/));
|
||||||
|
|
||||||
|
return matchedNotes;</pre>
|
||||||
|
|
||||||
|
<p>Note that search script and search string can't be combined with each other.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<span class="bx bx-x icon-action search-option-del"></span>
|
||||||
|
</td>
|
||||||
|
</tr>`;
|
||||||
|
|
||||||
|
export default class SearchScript extends AbstractSearchOption {
|
||||||
|
static get optionName() { return "searchScript" };
|
||||||
|
static get attributeType() { return "relation" };
|
||||||
|
|
||||||
|
static async create(noteId) {
|
||||||
|
await AbstractSearchOption.setAttribute(noteId, 'relation', 'searchScript', 'root');
|
||||||
|
}
|
||||||
|
|
||||||
|
doRender() {
|
||||||
|
const $option = $(TPL);
|
||||||
|
const $searchScript = $option.find('.search-script');
|
||||||
|
noteAutocompleteService.initNoteAutocomplete($searchScript);
|
||||||
|
|
||||||
|
$searchScript.on('autocomplete:closed', async () => {
|
||||||
|
const searchScriptNoteId = $searchScript.getSelectedNoteId();
|
||||||
|
|
||||||
|
if (searchScriptNoteId) {
|
||||||
|
await this.setAttribute('relation', 'searchScript', searchScriptNoteId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const searchScriptNoteId = this.note.getRelationValue('searchScript');
|
||||||
|
|
||||||
|
if (searchScriptNoteId !== 'root') {
|
||||||
|
$searchScript.setNote(searchScriptNoteId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $option;
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,7 @@ const TPL = `
|
|||||||
<tr>
|
<tr>
|
||||||
<td class="title-column">Search string:</td>
|
<td class="title-column">Search string:</td>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" class="form-control search-string">
|
<input type="text" class="form-control search-string" placeholder="fulltext keywords, #tag = value ...">
|
||||||
</td>
|
</td>
|
||||||
<td class="button-column">
|
<td class="button-column">
|
||||||
<div class="dropdown help-dropdown">
|
<div class="dropdown help-dropdown">
|
||||||
@ -64,8 +64,6 @@ export default class SearchString extends AbstractSearchOption {
|
|||||||
|
|
||||||
this.$searchString.val(this.note.getLabelValue('searchString'));
|
this.$searchString.val(this.note.getLabelValue('searchString'));
|
||||||
|
|
||||||
utils.initHelpDropdown($option);
|
|
||||||
|
|
||||||
return $option;
|
return $option;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -935,7 +935,7 @@ ul.fancytree-container li {
|
|||||||
border-width: 2px;
|
border-width: 2px;
|
||||||
box-shadow: 10px 10px 93px -25px black;
|
box-shadow: 10px 10px 93px -25px black;
|
||||||
padding: 10px 15px 10px 15px !important;
|
padding: 10px 15px 10px 15px !important;
|
||||||
width: 500px;
|
width: 600px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.help-dropdown .dropdown-menu pre {
|
.help-dropdown .dropdown-menu pre {
|
||||||
|
@ -9,34 +9,28 @@ const searchService = require('../../services/search/services/search');
|
|||||||
async function search(note) {
|
async function search(note) {
|
||||||
let searchResultNoteIds;
|
let searchResultNoteIds;
|
||||||
|
|
||||||
try {
|
const searchScript = note.getRelationValue('searchScript');
|
||||||
const searchScript = note.getRelationValue('searchScript');
|
const searchString = note.getLabelValue('searchString');
|
||||||
const searchString = note.getLabelValue('searchString');
|
|
||||||
|
|
||||||
if (searchScript) {
|
if (searchScript) {
|
||||||
searchResultNoteIds = await searchFromRelation(note, 'searchScript');
|
searchResultNoteIds = await searchFromRelation(note, 'searchScript');
|
||||||
} else {
|
} else {
|
||||||
const searchContext = new SearchContext({
|
const searchContext = new SearchContext({
|
||||||
fastSearch: note.hasLabel('fastSearch'),
|
fastSearch: note.hasLabel('fastSearch'),
|
||||||
ancestorNoteId: note.getRelationValue('ancestor'),
|
ancestorNoteId: note.getRelationValue('ancestor'),
|
||||||
includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
|
includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
|
||||||
orderBy: note.getLabelValue('orderBy'),
|
orderBy: note.getLabelValue('orderBy'),
|
||||||
orderDirection: note.getLabelValue('orderDirection'),
|
orderDirection: note.getLabelValue('orderDirection'),
|
||||||
fuzzyAttributeSearch: false
|
fuzzyAttributeSearch: false
|
||||||
});
|
});
|
||||||
|
|
||||||
searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext)
|
searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext)
|
||||||
.map(sr => sr.noteId);
|
.map(sr => sr.noteId);
|
||||||
}
|
|
||||||
|
|
||||||
// we won't return search note's own noteId
|
|
||||||
// also don't allow root since that would force infinite cycle
|
|
||||||
return searchResultNoteIds.filter(resultNoteId => !['root', note.noteId].includes(resultNoteId));
|
|
||||||
} catch (e) {
|
|
||||||
log.error(`Search failed for note ${note.noteId}: ` + e.message + ": " + e.stack);
|
|
||||||
|
|
||||||
throw new Error("Search failed, see logs for details.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we won't return search note's own noteId
|
||||||
|
// also don't allow root since that would force infinite cycle
|
||||||
|
return searchResultNoteIds.filter(resultNoteId => !['root', note.noteId].includes(resultNoteId));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function searchFromNote(req) {
|
async function searchFromNote(req) {
|
||||||
@ -55,13 +49,7 @@ async function searchFromNote(req) {
|
|||||||
return [400, `Note ${req.params.noteId} is not a search note.`]
|
return [400, `Note ${req.params.noteId} is not a search note.`]
|
||||||
}
|
}
|
||||||
|
|
||||||
let searchResultNoteIds = await search(note);
|
return await search(note);
|
||||||
|
|
||||||
if (searchResultNoteIds.length > 200) {
|
|
||||||
searchResultNoteIds = searchResultNoteIds.slice(0, 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
return searchResultNoteIds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ACTION_HANDLERS = {
|
const ACTION_HANDLERS = {
|
||||||
|
@ -101,6 +101,11 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
|
|||||||
if (resultHandler) {
|
if (resultHandler) {
|
||||||
if (result && result.then) {
|
if (result && result.then) {
|
||||||
result.then(actualResult => resultHandler(req, res, actualResult))
|
result.then(actualResult => resultHandler(req, res, actualResult))
|
||||||
|
.catch(e => {
|
||||||
|
log.error(`${method} ${path} threw exception: ` + e.stack);
|
||||||
|
|
||||||
|
res.status(500).send(e.message);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
resultHandler(req, res, result);
|
resultHandler(req, res, result);
|
||||||
@ -110,10 +115,10 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
|
|||||||
catch (e) {
|
catch (e) {
|
||||||
log.error(`${method} ${path} threw exception: ` + e.stack);
|
log.error(`${method} ${path} threw exception: ` + e.stack);
|
||||||
|
|
||||||
res.sendStatus(500);
|
res.status(500).send(e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
log.request(req, Date.now() - start);
|
log.request(req, res, Date.now() - start);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ function error(message) {
|
|||||||
|
|
||||||
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
|
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
|
||||||
|
|
||||||
function request(req, timeMs) {
|
function request(req, res, timeMs) {
|
||||||
for (const bl of requestBlacklist) {
|
for (const bl of requestBlacklist) {
|
||||||
if (req.url.startsWith(bl)) {
|
if (req.url.startsWith(bl)) {
|
||||||
return;
|
return;
|
||||||
@ -76,7 +76,7 @@ function request(req, timeMs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
info((timeMs >= 10 ? "Slow " : "") +
|
info((timeMs >= 10 ? "Slow " : "") +
|
||||||
req.method + " " + req.url + " took " + timeMs + "ms");
|
res.statusCode + " " + req.method + " " + req.url + " took " + timeMs + "ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
function pad(num) {
|
function pad(num) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user