mirror of
https://github.com/zadam/trilium.git
synced 2025-11-16 18:25:51 +01:00
search script UI
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import server from "../../services/server.js";
|
||||
import ws from "../../services/ws.js";
|
||||
import Component from "../component.js";
|
||||
import utils from "../../services/utils.js";
|
||||
|
||||
export default class AbstractSearchAction extends Component {
|
||||
constructor(attribute, actionDef) {
|
||||
@@ -18,6 +19,8 @@ export default class AbstractSearchAction extends Component {
|
||||
.on('click', () => this.deleteAction())
|
||||
.attr('title', 'Remove this search action');
|
||||
|
||||
utils.initHelpDropdown($rendered);
|
||||
|
||||
return $rendered;
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
@@ -21,6 +21,10 @@ const TPL = `
|
||||
For example to append a string to a note's title, use this small script:
|
||||
|
||||
<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>
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import noteAutocompleteService from "../services/note_autocomplete.js";
|
||||
import server from "../services/server.js";
|
||||
import TabAwareWidget from "./tab_aware_widget.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 IncludeArchivedNotes from "./search_options/include_archived_notes.js";
|
||||
import OrderBy from "./search_options/order_by.js";
|
||||
import SearchScript from "./search_options/search_script.js";
|
||||
|
||||
const TPL = `
|
||||
<div class="search-definition-widget">
|
||||
<style>
|
||||
.search-setting-table {
|
||||
margin-top: 7px;
|
||||
margin-top: 0;
|
||||
margin-bottom: 7px;
|
||||
width: 100%;
|
||||
border-collapse: separate;
|
||||
@@ -63,6 +63,10 @@ const TPL = `
|
||||
.search-definition-widget input:invalid {
|
||||
border: 3px solid red;
|
||||
}
|
||||
|
||||
.add-search-option button {
|
||||
margin-top: 5px; /* to give some spacing when buttons overflow on the next line */
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="search-settings">
|
||||
@@ -75,6 +79,11 @@ const TPL = `
|
||||
search string
|
||||
</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">
|
||||
<span class="bx bx-filter-alt"></span>
|
||||
ancestor
|
||||
@@ -150,6 +159,7 @@ const TPL = `
|
||||
|
||||
const OPTION_CLASSES = [
|
||||
SearchString,
|
||||
SearchScript,
|
||||
Ancestor,
|
||||
FastSearch,
|
||||
IncludeArchivedNotes,
|
||||
@@ -232,7 +242,12 @@ export default class SearchDefinitionWidget extends TabAwareWidget {
|
||||
}
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import server from "../../services/server.js";
|
||||
import ws from "../../services/ws.js";
|
||||
import Component from "../component.js";
|
||||
import utils from "../../services/utils.js";
|
||||
|
||||
export default class AbstractSearchOption extends Component {
|
||||
constructor(attribute, note) {
|
||||
@@ -28,6 +29,8 @@ export default class AbstractSearchOption extends Component {
|
||||
.on('click', () => this.deleteOption())
|
||||
.attr('title', 'Remove this search option');
|
||||
|
||||
utils.initHelpDropdown($rendered);
|
||||
|
||||
return $rendered;
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
@@ -30,14 +30,18 @@ export default class Ancestor extends AbstractSearchOption {
|
||||
noteAutocompleteService.initNoteAutocomplete($ancestor);
|
||||
|
||||
$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');
|
||||
|
||||
$ancestor.setNote(ancestorNoteId);
|
||||
if (ancestorNoteId !== 'root') {
|
||||
$ancestor.setNote(ancestorNoteId);
|
||||
}
|
||||
|
||||
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>
|
||||
<td class="title-column">Search string:</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 class="button-column">
|
||||
<div class="dropdown help-dropdown">
|
||||
@@ -64,8 +64,6 @@ export default class SearchString extends AbstractSearchOption {
|
||||
|
||||
this.$searchString.val(this.note.getLabelValue('searchString'));
|
||||
|
||||
utils.initHelpDropdown($option);
|
||||
|
||||
return $option;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user