Skip to content

Commit

Permalink
Show notebooks in suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-r-m committed Feb 4, 2021
1 parent 8d559ff commit dad33d8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 14 deletions.
37 changes: 28 additions & 9 deletions src/QuickLinksPlugin.ts
Original file line number Diff line number Diff line change
@@ -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 = `
<div style="display:table-cell; padding-right: 5px">${note.title}</div>
<div style="display:table-cell; text-align: right;"><small><em>In ${note.folder}</em></small></div>
`
};
} else {
hint.displayText = note.title;
}
hints.push(hint);
}
return hints;
}
Expand Down
78 changes: 73 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,90 @@
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<any[]> {
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,
});
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()}*`,
});
return notes.items;
}
}

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',
Expand All @@ -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 };
}
});
},
}
});

0 comments on commit dad33d8

Please sign in to comment.