import server from "./server.js"; import appContext from "../components/app_context.js"; import utils from './utils.js'; import noteCreateService from './note_create.js'; import froca from "./froca.js"; // this key needs to have this value, so it's hit by the tooltip const SELECTED_NOTE_PATH_KEY = "data-note-path"; const SELECTED_EXTERNAL_LINK_KEY = "data-external-link"; async function autocompleteSourceForCKEditor(queryText) { return await new Promise((res, rej) => { autocompleteSource(queryText, rows => { res(rows.map(row => { return { action: row.action, noteTitle: row.noteTitle, id: `@${row.notePathTitle}`, name: row.notePathTitle, link: `#${row.notePath}`, notePath: row.notePath, highlightedNotePathTitle: row.highlightedNotePathTitle } })); }, { allowCreatingNotes: true }); }); } async function autocompleteSource(term, cb, options = {}) { const fastSearch = options.fastSearch === false ? false : true; if (fastSearch === false) { if (term.trim().length === 0){ return; } cb( [{ noteTitle: term, highlightedNotePathTitle: `Searching...` }] ); } const activeNoteId = appContext.tabManager.getActiveContextNoteId(); let results = await server.get(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${fastSearch}`); if (term.trim().length >= 1 && options.allowCreatingNotes) { results = [ { action: 'create-note', noteTitle: term, parentNoteId: activeNoteId || 'root', highlightedNotePathTitle: `Create and link child note "${utils.escapeHtml(term)}"` } ].concat(results); } if (term.trim().length >= 1 && options.allowJumpToSearchNotes) { results = results.concat([ { action: 'search-notes', noteTitle: term, highlightedNotePathTitle: `Search for "${utils.escapeHtml(term)}" Ctrl+Enter` } ]); } if (term.match(/^[a-z]+:\/\/.+/i) && options.allowExternalLinks) { results = [ { action: 'external-link', externalLink: term, highlightedNotePathTitle: `Insert external link to "${utils.escapeHtml(term)}"` } ].concat(results); } cb(results); } function clearText($el) { if (utils.isMobile()) { return; } $el.setSelectedNotePath(""); $el.autocomplete("val", "").trigger('change'); } function setText($el, text) { if (utils.isMobile()) { return; } $el.setSelectedNotePath(""); $el .autocomplete("val", text.trim()) .autocomplete("open"); } function showRecentNotes($el) { if (utils.isMobile()) { return; } $el.setSelectedNotePath(""); $el.autocomplete("val", ""); $el.autocomplete('open'); $el.trigger('focus'); } function fullTextSearch($el, options){ const searchString = $el.autocomplete('val'); if (options.fastSearch === false || searchString.trim().length === 0) { return; } $el.trigger('focus'); options.fastSearch = false; $el.autocomplete('val', ''); $el.setSelectedNotePath(""); $el.autocomplete('val', searchString); // Set a delay to avoid resetting to true before full text search (await server.get) is called. setTimeout(() => { options.fastSearch = true; }, 100); } function initNoteAutocomplete($el, options) { if ($el.hasClass("note-autocomplete-input") || utils.isMobile()) { // clear any event listener added in previous invocation of this function $el.off('autocomplete:noteselected'); return $el; } options = options || {}; $el.addClass("note-autocomplete-input"); const $clearTextButton = $("