From dad33d88677519e61503d4bffa240e786d75bb90 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 3 Feb 2021 22:29:35 +0000 Subject: [PATCH] Show notebooks in suggestions --- src/QuickLinksPlugin.ts | 37 ++++++++++++++----- src/index.ts | 78 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/QuickLinksPlugin.ts b/src/QuickLinksPlugin.ts index c42c8f8..47837f8 100644 --- a/src/QuickLinksPlugin.ts +++ b/src/QuickLinksPlugin.ts @@ -1,24 +1,43 @@ +interface Hint { + text: string; + hint: Function; + displayText?: string; + render?: Function; +} + module.exports = { default: function(context: any) { const buildHints = async (prefix: string) =>{ - const notes = await context.postMessage({ - command: 'getNotes', - prefix: prefix, - }); + const response = await context.postMessage({ command: 'getNotes', prefix: prefix }); - let hints = []; + let hints: Hint[] = []; + const notes = response.notes; for (let i = 0; i < notes.length; i++) { const note = notes[i]; - hints.push({ + const hint: Hint = { text: note.title, - displayText: note.title, hint: async (cm, data, completion) => { const from = completion.from || data.from; from.ch -= 2; cm.replaceRange(`[${note.title}](:/${note.id})`, from, cm.getCursor(), "complete"); - } - }); + }, + }; + if (response.showFolders) { + const folder = !!note.folder ? note.folder : "unknown"; + hint.render = (elem, _data, _completion) => { + const p = elem.ownerDocument.createElement('div'); + p.setAttribute('style', 'width: 100%; display:table;'); + elem.appendChild(p); + p.innerHTML = ` +
${note.title}
+
In ${note.folder}
+ ` + }; + } else { + hint.displayText = note.title; + } + hints.push(hint); } return hints; } diff --git a/src/index.ts b/src/index.ts index d5a2a6d..31ddd60 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,31 @@ import joplin from 'api'; -import { ContentScriptType, Path } from 'api/types'; +import { ContentScriptType, SettingItem, SettingItemType } from 'api/types'; const NUM_RESULTS = 21; +const FOLDERS_REFRESH_INTERVAL = 6000; +const SETTING_SHOW_FOLDERS = 'showFolders'; + +let showFolders = false; +let folders = {}; + +async function onShowFolderSettingChanged() { + showFolders = await joplin.settings.value(SETTING_SHOW_FOLDERS); + if (showFolders) { + refreshFolderList(); + } +} + +async function refreshFolderList() { + folders = await getFolders(); + setTimeout(() => { + if (showFolders) refreshFolderList(); + }, FOLDERS_REFRESH_INTERVAL); +} async function getNotes(prefix: string): Promise { if (prefix === "") { const notes = await joplin.data.get(['notes'], { - fields: ['id', 'title'], + fields: ['id', 'title', 'parent_id'], order_by: 'updated_time', order_dir: 'DESC', limit: NUM_RESULTS, @@ -14,7 +33,7 @@ async function getNotes(prefix: string): Promise { return notes.items; } else { const notes = await joplin.data.get(['search'], { - fields: ['id', 'title'], + fields: ['id', 'title', 'parent_id'], limit: NUM_RESULTS, query: `title:${prefix.trimRight()}*`, }); @@ -22,8 +41,50 @@ async function getNotes(prefix: string): Promise { } } +async function getFolders() { + let folders = {}; + + const query = { fields: ['id', 'title'], page: 1 }; + let result = await joplin.data.get(['folders'], query); + result.items.forEach(i => folders[i.id] = i.title); + + while (!!result.has_more) { + query.page += 1; + result = await joplin.data.get(['folders'], query); + result.items.forEach(i => folders[i.id] = i.title); + } + return folders; +} + +async function initSettings() { + const SECTION = 'QuickLinks'; + + await joplin.settings.registerSection(SECTION, { + description: 'Quick Links Plugin Settings', + label: 'Quick Links', + }); + await joplin.settings.registerSetting(SETTING_SHOW_FOLDERS, { + public: true, + section: SECTION, + type: SettingItemType.Bool, + value: showFolders, + label: 'Show Notebooks', + } as SettingItem); + + await onShowFolderSettingChanged(); + + await joplin.settings.onChange(change => { + const idx = change.keys.indexOf(SETTING_SHOW_FOLDERS); + if (idx >= 0) { + onShowFolderSettingChanged(); + } + }); +} + joplin.plugins.register({ onStart: async function() { + await initSettings(); + await joplin.contentScripts.register( ContentScriptType.CodeMirrorPlugin, 'quickLinks', @@ -36,8 +97,15 @@ joplin.plugins.register({ if (message.command === 'getNotes') { const prefix = message.prefix; let notes = await getNotes(prefix); - return notes.filter(n => n.id !== noteId); + const res = notes.filter(n => n.id !== noteId).map(n => { + return { + id: n.id, + title: n.title, + folder: folders[n.parent_id], + }; + }); + return { notes: res, showFolders: showFolders }; } }); - }, + } });