search script UI

This commit is contained in:
zadam 2021-01-26 22:22:17 +01:00
parent ee78413ecb
commit a1f67e830d
13 changed files with 139 additions and 49 deletions

View File

@ -47,8 +47,8 @@ async function createSearchNote(opts = {}) {
const note = await server.post('search-note');
const attrsToUpdate = [
opts.ancestor ? { type: 'relation', name: 'ancestor', value: opts.ancestorNoteId } : undefined,
{ type: 'label', name: 'searchString', value: opts.searchString }
opts.ancestorNoteId ? { type: 'relation', name: 'ancestor', value: opts.ancestorNoteId } : undefined,
{ type: 'label', name: 'searchString', value: opts.searchStringe }
].filter(attr => !!attr);
if (attrsToUpdate.length > 0) {

View File

@ -167,7 +167,7 @@ class TreeCache {
for (const note of resp.notes) {
if (note.type === 'search') {
const searchResultNoteIds = await server.get('search-note/' + note.noteId);
console.log("searchResultNoteIds", searchResultNoteIds);
if (!Array.isArray(searchResultNoteIds)) {
throw new Error(`Search note ${note.noteId} failed: ${searchResultNoteIds}`);
}

View File

@ -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) {

View File

@ -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>

View File

@ -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});
}

View File

@ -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) {

View File

@ -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;
}

View 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;
}
}

View File

@ -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;
}

View File

@ -935,7 +935,7 @@ ul.fancytree-container li {
border-width: 2px;
box-shadow: 10px 10px 93px -25px black;
padding: 10px 15px 10px 15px !important;
width: 500px;
width: 600px;
}
.help-dropdown .dropdown-menu pre {

View File

@ -9,34 +9,28 @@ const searchService = require('../../services/search/services/search');
async function search(note) {
let searchResultNoteIds;
try {
const searchScript = note.getRelationValue('searchScript');
const searchString = note.getLabelValue('searchString');
const searchScript = note.getRelationValue('searchScript');
const searchString = note.getLabelValue('searchString');
if (searchScript) {
searchResultNoteIds = await searchFromRelation(note, 'searchScript');
} else {
const searchContext = new SearchContext({
fastSearch: note.hasLabel('fastSearch'),
ancestorNoteId: note.getRelationValue('ancestor'),
includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
orderBy: note.getLabelValue('orderBy'),
orderDirection: note.getLabelValue('orderDirection'),
fuzzyAttributeSearch: false
});
if (searchScript) {
searchResultNoteIds = await searchFromRelation(note, 'searchScript');
} else {
const searchContext = new SearchContext({
fastSearch: note.hasLabel('fastSearch'),
ancestorNoteId: note.getRelationValue('ancestor'),
includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
orderBy: note.getLabelValue('orderBy'),
orderDirection: note.getLabelValue('orderDirection'),
fuzzyAttributeSearch: false
});
searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext)
.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.");
searchResultNoteIds = searchService.findNotesWithQuery(searchString, searchContext)
.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));
}
async function searchFromNote(req) {
@ -55,13 +49,7 @@ async function searchFromNote(req) {
return [400, `Note ${req.params.noteId} is not a search note.`]
}
let searchResultNoteIds = await search(note);
if (searchResultNoteIds.length > 200) {
searchResultNoteIds = searchResultNoteIds.slice(0, 200);
}
return searchResultNoteIds;
return await search(note);
}
const ACTION_HANDLERS = {

View File

@ -101,6 +101,11 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
if (resultHandler) {
if (result && result.then) {
result.then(actualResult => resultHandler(req, res, actualResult))
.catch(e => {
log.error(`${method} ${path} threw exception: ` + e.stack);
res.status(500).send(e.message);
});
}
else {
resultHandler(req, res, result);
@ -110,10 +115,10 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
catch (e) {
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);
});
}

View File

@ -64,7 +64,7 @@ function error(message) {
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
function request(req, timeMs) {
function request(req, res, timeMs) {
for (const bl of requestBlacklist) {
if (req.url.startsWith(bl)) {
return;
@ -76,7 +76,7 @@ function request(req, timeMs) {
}
info((timeMs >= 10 ? "Slow " : "") +
req.method + " " + req.url + " took " + timeMs + "ms");
res.statusCode + " " + req.method + " " + req.url + " took " + timeMs + "ms");
}
function pad(num) {