-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
112 lines (97 loc) · 2.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import joplin from 'api';
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', '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', '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',
iconName: 'fas fa-link'
});
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',
'./QuickLinksPlugin.js'
);
await joplin.contentScripts.onMessage('quickLinks', async (message: any) => {
const selectedNoteIds = await joplin.workspace.selectedNoteIds();
const noteId = selectedNoteIds[0];
if (message.command === 'getNotes') {
const prefix = message.prefix;
let notes = await getNotes(prefix);
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 };
}
});
}
});