joplin-plugin-explorer 1.1.4 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/publish/index.js CHANGED
@@ -1,708 +1 @@
1
- /* Notes In List Plugin - notebooks and notes in a single tree view */
2
-
3
- /* ======================== i18n ======================== */
4
- var i18nData = {
5
- 'zh_CN': {
6
- newNotebook: '新建笔记本', newNote: '新建笔记', newTodo: '新建待办',
7
- sort: '排序', collapseAll: '全部折叠', expandAll: '全部展开',
8
- search: '搜索笔记内容...', searchResultCount: '找到 {count} 条结果',
9
- searchNoResult: '没有找到匹配的笔记', searching: '搜索中...',
10
- sync: '同步', syncing: '同步中...', syncDone: '\u2714 同步完成', loading: '加载中...',
11
- sortUpdatedDesc: '\u2193 修改时间', sortUpdatedAsc: '\u2191 修改时间',
12
- sortTitleAsc: '\u2191 标题', sortTitleDesc: '\u2193 标题',
13
- // Folder context menu
14
- ctxNewNoteHere: '在此新建笔记', ctxNewTodoHere: '在此新建待办',
15
- ctxNewSubNotebook: '新建子笔记本', ctxRenameFolder: '重命名',
16
- ctxExportFolder: '导出笔记本', ctxDeleteFolder: '删除笔记本',
17
- // Note context menu
18
- ctxOpenNote: '打开笔记', ctxOpenInNewWindow: '在新窗口中打开',
19
- ctxCopyLink: '复制 Markdown 链接', ctxDuplicateNote: '复制副本',
20
- ctxSwitchNoteType: '笔记/待办 切换', ctxToggleTodo: '切换完成状态',
21
- ctxRenameNote: '重命名', ctxMoveNote: '移动到笔记本...',
22
- ctxNoteInfo: '笔记属性', ctxDeleteNote: '删除笔记',
23
- // Confirm dialogs
24
- confirmDeleteFolder: '确定删除此笔记本及其所有内容吗?',
25
- confirmDeleteNote: '确定删除此笔记吗?',
26
- promptRename: '请输入新名称:',
27
- promptMoveNote: '请输入目标笔记本名称:',
28
- cancel: '取消',
29
- },
30
- 'zh_TW': {
31
- newNotebook: '新建筆記本', newNote: '新建筆記', newTodo: '新建待辦',
32
- sort: '排序', collapseAll: '全部摺疊', expandAll: '全部展開',
33
- search: '搜尋筆記內容...', searchResultCount: '找到 {count} 條結果',
34
- searchNoResult: '沒有找到匹配的筆記', searching: '搜尋中...',
35
- sync: '同步', syncing: '同步中...', syncDone: '\u2714 同步完成', loading: '載入中...',
36
- sortUpdatedDesc: '\u2193 修改時間', sortUpdatedAsc: '\u2191 修改時間',
37
- sortTitleAsc: '\u2191 標題', sortTitleDesc: '\u2193 標題',
38
- ctxNewNoteHere: '在此新建筆記', ctxNewTodoHere: '在此新建待辦',
39
- ctxNewSubNotebook: '新建子筆記本', ctxRenameFolder: '重新命名',
40
- ctxExportFolder: '匯出筆記本', ctxDeleteFolder: '刪除筆記本',
41
- ctxOpenNote: '開啟筆記', ctxOpenInNewWindow: '在新視窗中開啟',
42
- ctxCopyLink: '複製 Markdown 連結', ctxDuplicateNote: '複製副本',
43
- ctxSwitchNoteType: '筆記/待辦 切換', ctxToggleTodo: '切換完成狀態',
44
- ctxRenameNote: '重新命名', ctxMoveNote: '移動到筆記本...',
45
- ctxNoteInfo: '筆記屬性', ctxDeleteNote: '刪除筆記',
46
- confirmDeleteFolder: '確定刪除此筆記本及其所有內容嗎?',
47
- confirmDeleteNote: '確定刪除此筆記嗎?',
48
- promptRename: '請輸入新名稱:', promptMoveNote: '請輸入目標筆記本名稱:',
49
- cancel: '取消',
50
- },
51
- 'en_US': {
52
- newNotebook: 'New Notebook', newNote: 'New Note', newTodo: 'New To-do',
53
- sort: 'Sort', collapseAll: 'Collapse All', expandAll: 'Expand All',
54
- search: 'Search note contents...', searchResultCount: '{count} results found',
55
- searchNoResult: 'No matching notes found', searching: 'Searching...',
56
- sync: 'Synchronise', syncing: 'Syncing...', syncDone: '\u2714 Sync Done', loading: 'Loading...',
57
- sortUpdatedDesc: '\u2193 Updated', sortUpdatedAsc: '\u2191 Updated',
58
- sortTitleAsc: '\u2191 Title', sortTitleDesc: '\u2193 Title',
59
- ctxNewNoteHere: 'New Note Here', ctxNewTodoHere: 'New To-do Here',
60
- ctxNewSubNotebook: 'New Sub-notebook', ctxRenameFolder: 'Rename',
61
- ctxExportFolder: 'Export Notebook', ctxDeleteFolder: 'Delete Notebook',
62
- ctxOpenNote: 'Open Note', ctxOpenInNewWindow: 'Open in New Window',
63
- ctxCopyLink: 'Copy Markdown Link', ctxDuplicateNote: 'Duplicate',
64
- ctxSwitchNoteType: 'Switch Note/To-do', ctxToggleTodo: 'Toggle Completed',
65
- ctxRenameNote: 'Rename', ctxMoveNote: 'Move to Notebook...',
66
- ctxNoteInfo: 'Note Properties', ctxDeleteNote: 'Delete Note',
67
- confirmDeleteFolder: 'Delete this notebook and all its contents?',
68
- confirmDeleteNote: 'Delete this note?',
69
- promptRename: 'Enter new name:', promptMoveNote: 'Enter target notebook name:',
70
- cancel: 'Cancel',
71
- },
72
- 'ja_JP': {
73
- newNotebook: '\u65B0\u898F\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF', newNote: '\u65B0\u898F\u30CE\u30FC\u30C8', newTodo: '\u65B0\u898F\u30BF\u30B9\u30AF',
74
- sort: '\u4E26\u3079\u66FF\u3048', collapseAll: '\u3059\u3079\u3066\u6298\u308A\u305F\u305F\u3080', expandAll: '\u3059\u3079\u3066\u5C55\u958B',
75
- search: '\u30CE\u30FC\u30C8\u5185\u5BB9\u3092\u691C\u7D22...', searchResultCount: '{count} \u4EF6\u306E\u7D50\u679C',
76
- searchNoResult: '\u4E00\u81F4\u3059\u308B\u30CE\u30FC\u30C8\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093', searching: '\u691C\u7D22\u4E2D...',
77
- sync: '\u540C\u671F', syncing: '\u540C\u671F\u4E2D...', syncDone: '\u2714 \u540C\u671F\u5B8C\u4E86', loading: '\u8AAD\u307F\u8FBC\u307F\u4E2D...',
78
- sortUpdatedDesc: '\u2193 \u66F4\u65B0\u65E5\u6642', sortUpdatedAsc: '\u2191 \u66F4\u65B0\u65E5\u6642',
79
- sortTitleAsc: '\u2191 \u30BF\u30A4\u30C8\u30EB', sortTitleDesc: '\u2193 \u30BF\u30A4\u30C8\u30EB',
80
- ctxNewNoteHere: '\u3053\u3053\u306B\u65B0\u898F\u30CE\u30FC\u30C8', ctxNewTodoHere: '\u3053\u3053\u306B\u65B0\u898F\u30BF\u30B9\u30AF',
81
- ctxNewSubNotebook: '\u65B0\u898F\u30B5\u30D6\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF', ctxRenameFolder: '\u540D\u524D\u3092\u5909\u66F4',
82
- ctxExportFolder: '\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF\u3092\u30A8\u30AF\u30B9\u30DD\u30FC\u30C8', ctxDeleteFolder: '\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF\u3092\u524A\u9664',
83
- ctxOpenNote: '\u30CE\u30FC\u30C8\u3092\u958B\u304F', ctxOpenInNewWindow: '\u65B0\u3057\u3044\u30A6\u30A3\u30F3\u30C9\u30A6\u3067\u958B\u304F',
84
- ctxCopyLink: 'Markdown\u30EA\u30F3\u30AF\u3092\u30B3\u30D4\u30FC', ctxDuplicateNote: '\u8907\u88FD',
85
- ctxSwitchNoteType: '\u30CE\u30FC\u30C8/\u30BF\u30B9\u30AF\u5207\u308A\u66FF\u3048', ctxToggleTodo: '\u5B8C\u4E86\u72B6\u614B\u3092\u5207\u308A\u66FF\u3048',
86
- ctxRenameNote: '\u540D\u524D\u3092\u5909\u66F4', ctxMoveNote: '\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF\u306B\u79FB\u52D5...',
87
- ctxNoteInfo: '\u30CE\u30FC\u30C8\u30D7\u30ED\u30D1\u30C6\u30A3', ctxDeleteNote: '\u30CE\u30FC\u30C8\u3092\u524A\u9664',
88
- confirmDeleteFolder: '\u3053\u306E\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF\u3068\u305D\u306E\u5185\u5BB9\u3092\u3059\u3079\u3066\u524A\u9664\u3057\u307E\u3059\u304B\uFF1F',
89
- confirmDeleteNote: '\u3053\u306E\u30CE\u30FC\u30C8\u3092\u524A\u9664\u3057\u307E\u3059\u304B\uFF1F',
90
- promptRename: '\u65B0\u3057\u3044\u540D\u524D\u3092\u5165\u529B\uFF1A', promptMoveNote: '\u79FB\u52D5\u5148\u306E\u30CE\u30FC\u30C8\u30D6\u30C3\u30AF\u540D\uFF1A',
91
- cancel: '\u30AD\u30E3\u30F3\u30BB\u30EB',
92
- },
93
- };
94
-
95
- function getI18n(locale) {
96
- return i18nData[locale] || i18nData[locale.split('_')[0]] || i18nData['en_US'];
97
- }
98
-
99
- /* ======================== Data helpers ======================== */
100
- function escapeHtml(str) {
101
- return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
102
- }
103
-
104
- async function getAllFolders() {
105
- var folders = [];
106
- var page = 1;
107
- var hasMore = true;
108
- while (hasMore) {
109
- var result = await joplin.data.get(['folders'], {
110
- fields: ['id', 'title', 'parent_id', 'icon'],
111
- page: page, limit: 100,
112
- });
113
- folders = folders.concat(result.items);
114
- hasMore = result.has_more;
115
- page++;
116
- }
117
- return folders;
118
- }
119
-
120
- async function getNotesInFolder(folderId) {
121
- var notes = [];
122
- var page = 1;
123
- var hasMore = true;
124
- while (hasMore) {
125
- var result = await joplin.data.get(['folders', folderId, 'notes'], {
126
- fields: ['id', 'title', 'parent_id', 'is_todo', 'todo_completed', 'updated_time', 'user_updated_time'],
127
- page: page, limit: 100,
128
- order_by: 'user_updated_time', order_dir: 'DESC',
129
- });
130
- notes = notes.concat(result.items);
131
- hasMore = result.has_more;
132
- page++;
133
- }
134
- return notes;
135
- }
136
-
137
- function buildTree(folders, notesByFolder) {
138
- var folderMap = {};
139
- for (var i = 0; i < folders.length; i++) {
140
- var f = folders[i];
141
- folderMap[f.id] = {
142
- type: 'folder', id: f.id, title: f.title,
143
- parent_id: f.parent_id, icon: f.icon, note_count: 0, children: [],
144
- };
145
- }
146
- var folderIds = Object.keys(notesByFolder);
147
- for (var i = 0; i < folderIds.length; i++) {
148
- var fid = folderIds[i];
149
- var folder = folderMap[fid];
150
- if (folder) {
151
- var notes = notesByFolder[fid];
152
- folder.note_count = notes.length;
153
- for (var j = 0; j < notes.length; j++) {
154
- var n = notes[j];
155
- folder.children.push({
156
- type: 'note', id: n.id, title: n.title || '(untitled)',
157
- is_todo: n.is_todo, todo_completed: n.todo_completed,
158
- });
159
- }
160
- }
161
- }
162
- var roots = [];
163
- for (var i = 0; i < folders.length; i++) {
164
- var f = folders[i];
165
- var node = folderMap[f.id];
166
- if (f.parent_id && folderMap[f.parent_id]) {
167
- folderMap[f.parent_id].children.unshift(node);
168
- } else {
169
- roots.push(node);
170
- }
171
- }
172
-
173
- // Recursively compute total note count including sub-folders
174
- function calcTotalCount(node) {
175
- var total = 0;
176
- if (!node.children) return 0;
177
- for (var i = 0; i < node.children.length; i++) {
178
- var child = node.children[i];
179
- if (child.type === 'note') {
180
- total++;
181
- } else if (child.type === 'folder') {
182
- calcTotalCount(child);
183
- total += child.total_count;
184
- }
185
- }
186
- node.total_count = total + (node.note_count || 0) - (node.note_count || 0);
187
- // note_count is direct notes only (already counted in children), total_count is all
188
- node.total_count = total;
189
- return total;
190
- }
191
- for (var i = 0; i < roots.length; i++) {
192
- calcTotalCount(roots[i]);
193
- }
194
-
195
- return roots;
196
- }
197
-
198
- function getFolderIcon(node) {
199
- var iconData = node.icon;
200
- if (iconData && typeof iconData === 'string') {
201
- try { iconData = JSON.parse(iconData); } catch(e) { iconData = null; }
202
- }
203
- if (iconData && iconData.emoji) return iconData.emoji;
204
- return '\uD83D\uDCC2';
205
- }
206
-
207
- function renderTreeHtml(nodes, selectedNoteId, collapsedSet, level) {
208
- level = level || 0;
209
- var html = '';
210
- for (var i = 0; i < nodes.length; i++) {
211
- var node = nodes[i];
212
- var indent = level * 18;
213
- if (node.type === 'folder') {
214
- var count = node.total_count || node.note_count || 0;
215
- var isCollapsed = collapsedSet[node.id];
216
- var arrowChar = isCollapsed ? '\u25B6' : '\u25BC';
217
- var toggleClass = isCollapsed ? 'toggle' : 'toggle expanded';
218
- html += '<div class="tree-item folder" style="padding-left:' + indent + 'px" data-id="' + node.id + '" data-type="folder">';
219
- html += '<span class="' + toggleClass + '">' + arrowChar + '</span>';
220
- html += '<span class="icon folder-icon">' + getFolderIcon(node) + '</span>';
221
- html += '<span class="label">' + escapeHtml(node.title) + '</span>';
222
- html += '<span class="count">' + count + '</span>';
223
- html += '</div>';
224
- html += '<div class="children' + (isCollapsed ? ' collapsed' : '') + '" data-folder-id="' + node.id + '">';
225
- if (node.children) {
226
- html += renderTreeHtml(node.children, selectedNoteId, collapsedSet, level + 1);
227
- }
228
- html += '</div>';
229
- } else {
230
- var selected = node.id === selectedNoteId ? ' selected' : '';
231
- var icon = '\uD83D\uDCDD';
232
- if (node.is_todo) {
233
- icon = node.todo_completed ? '\u2611' : '\u2610';
234
- }
235
- html += '<div class="tree-item note' + selected + '" style="padding-left:' + indent + 'px" data-id="' + node.id + '" data-type="note">';
236
- html += '<span class="icon note-icon">' + icon + '</span>';
237
- html += '<span class="label">' + escapeHtml(node.title) + '</span>';
238
- html += '</div>';
239
- }
240
- }
241
- return html;
242
- }
243
-
244
- /* ======================== Plugin ======================== */
245
- joplin.plugins.register({
246
- onStart: async function () {
247
- // Get locale
248
- var locale = await joplin.settings.globalValue('locale') || 'en_US';
249
- var t = getI18n(locale);
250
-
251
- var panel = await joplin.views.panels.create('notesInListPanel');
252
- await joplin.views.panels.addScript(panel, 'webview/panel.css');
253
- await joplin.views.panels.addScript(panel, 'webview/panel.js');
254
- await joplin.views.panels.setHtml(panel, '<div id="notes-in-list-root"><p style="padding:12px;">' + t.loading + '</p></div>');
255
- await joplin.views.panels.show(panel, true);
256
-
257
- // Native dialogs
258
- var inputDialog = await joplin.views.dialogs.create('explorerInputDialog');
259
- var confirmDialog = await joplin.views.dialogs.create('explorerConfirmDialog');
260
- var infoDialog = await joplin.views.dialogs.create('explorerInfoDialog');
261
-
262
- async function showNativeConfirm(message) {
263
- await joplin.views.dialogs.setHtml(confirmDialog,
264
- '<div style="padding:10px;min-width:280px;">'
265
- + '<div style="font-size:13px;">' + escapeHtml(message) + '</div>'
266
- + '</div>'
267
- );
268
- await joplin.views.dialogs.setButtons(confirmDialog, [
269
- { id: 'ok', title: 'OK' },
270
- { id: 'cancel', title: t.cancel || 'Cancel' },
271
- ]);
272
- var result = await joplin.views.dialogs.open(confirmDialog);
273
- return result.id === 'ok';
274
- }
275
-
276
- async function showNativeInfo(title, body) {
277
- await joplin.views.dialogs.setHtml(infoDialog,
278
- '<div style="padding:10px;min-width:320px;">'
279
- + '<div style="font-size:14px;font-weight:bold;margin-bottom:10px;">' + escapeHtml(title) + '</div>'
280
- + '<div style="font-size:12px;line-height:1.8;white-space:pre-wrap;">' + escapeHtml(body) + '</div>'
281
- + '</div>'
282
- );
283
- await joplin.views.dialogs.setButtons(infoDialog, [
284
- { id: 'ok', title: 'OK' },
285
- ]);
286
- await joplin.views.dialogs.open(infoDialog);
287
- }
288
-
289
- async function showNativeInput(label, defaultValue) {
290
- await joplin.views.dialogs.setHtml(inputDialog,
291
- '<div style="padding:10px;min-width:300px;">'
292
- + '<div style="margin-bottom:8px;font-size:13px;">' + escapeHtml(label) + '</div>'
293
- + '<form name="inputForm">'
294
- + '<input name="value" type="text" value="' + escapeHtml(defaultValue || '') + '" '
295
- + 'style="width:100%;box-sizing:border-box;padding:6px 8px;font-size:13px;" />'
296
- + '</form>'
297
- + '</div>'
298
- );
299
- await joplin.views.dialogs.setButtons(inputDialog, [
300
- { id: 'ok', title: 'OK' },
301
- { id: 'cancel', title: t.cancel || 'Cancel' },
302
- ]);
303
- var result = await joplin.views.dialogs.open(inputDialog);
304
- if (result.id === 'ok' && result.formData && result.formData.inputForm) {
305
- return result.formData.inputForm.value || null;
306
- }
307
- return null;
308
- }
309
-
310
- var selectedNoteId = '';
311
- var collapsedFolders = {};
312
- var currentSort = 'updated_desc';
313
- var allFoldersCache = [];
314
- var isFirstLoad = true;
315
-
316
- function expandToFolder(folderId) {
317
- var parentId = folderId;
318
- while (parentId) {
319
- delete collapsedFolders[parentId];
320
- var found = null;
321
- for (var i = 0; i < allFoldersCache.length; i++) {
322
- if (allFoldersCache[i].id === parentId) { found = allFoldersCache[i]; break; }
323
- }
324
- parentId = found ? found.parent_id : null;
325
- }
326
- }
327
-
328
- function sortNotes(notes, sortMode) {
329
- var sorted = notes.slice();
330
- switch (sortMode) {
331
- case 'title_asc': sorted.sort(function(a, b) { return (a.title || '').localeCompare(b.title || ''); }); break;
332
- case 'title_desc': sorted.sort(function(a, b) { return (b.title || '').localeCompare(a.title || ''); }); break;
333
- case 'updated_asc': sorted.sort(function(a, b) { return (a.user_updated_time || 0) - (b.user_updated_time || 0); }); break;
334
- default: sorted.sort(function(a, b) { return (b.user_updated_time || 0) - (a.user_updated_time || 0); }); break;
335
- }
336
- return sorted;
337
- }
338
-
339
- async function refreshPanel() {
340
- try {
341
- var folders = await getAllFolders();
342
- allFoldersCache = folders;
343
-
344
- // Default: all folders collapsed, then expand path to current note
345
- if (isFirstLoad) {
346
- for (var fi = 0; fi < folders.length; fi++) {
347
- collapsedFolders[folders[fi].id] = true;
348
- }
349
- // Expand the path to the currently selected note
350
- var currentNote = await joplin.workspace.selectedNote();
351
- if (currentNote) {
352
- selectedNoteId = currentNote.id;
353
- var parentId = currentNote.parent_id;
354
- while (parentId) {
355
- delete collapsedFolders[parentId];
356
- var parentFolder = null;
357
- for (var pi = 0; pi < folders.length; pi++) {
358
- if (folders[pi].id === parentId) { parentFolder = folders[pi]; break; }
359
- }
360
- parentId = parentFolder ? parentFolder.parent_id : null;
361
- }
362
- }
363
- isFirstLoad = false;
364
- }
365
-
366
- var notesByFolder = {};
367
-
368
- var batchSize = 10;
369
- for (var i = 0; i < folders.length; i += batchSize) {
370
- var batch = folders.slice(i, i + batchSize);
371
- var promises = [];
372
- for (var j = 0; j < batch.length; j++) promises.push(getNotesInFolder(batch[j].id));
373
- var results = await Promise.all(promises);
374
- for (var j = 0; j < batch.length; j++) notesByFolder[batch[j].id] = sortNotes(results[j], currentSort);
375
- }
376
-
377
- var tree = buildTree(folders, notesByFolder);
378
- var treeHtml = renderTreeHtml(tree, selectedNoteId, collapsedFolders);
379
-
380
- var sortLabels = {
381
- 'updated_desc': t.sortUpdatedDesc, 'updated_asc': t.sortUpdatedAsc,
382
- 'title_asc': t.sortTitleAsc, 'title_desc': t.sortTitleDesc,
383
- };
384
-
385
- // Pass i18n via data attribute (inline scripts may not execute in webview)
386
- var i18nJson = escapeHtml(JSON.stringify(t));
387
-
388
- var html = '<div id="notes-in-list-root" data-i18n="' + i18nJson + '">'
389
- + ' <div class="toolbar">'
390
- + ' <button id="btn-new-notebook" title="' + t.newNotebook + '">\uD83D\uDCC1+</button>'
391
- + ' <button id="btn-new-note" title="' + t.newNote + '">\uD83D\uDCDD+</button>'
392
- + ' <button id="btn-new-todo" title="' + t.newTodo + '">\u2610+</button>'
393
- + ' <button id="btn-sort" title="' + t.sort + '">' + sortLabels[currentSort] + '</button>'
394
- + ' <button id="btn-collapse-all" title="' + t.collapseAll + '">\u25B2</button>'
395
- + ' </div>'
396
- + ' <div class="search-bar">'
397
- + ' <input id="search-input" type="text" placeholder="\uD83D\uDD0D ' + t.search + '" />'
398
- + ' </div>'
399
- + ' <div id="tree-container">' + treeHtml + '</div>'
400
- + ' <div id="search-results" style="display:none;"></div>'
401
- + ' <div class="bottom-bar">'
402
- + ' <button id="btn-sync" title="' + t.sync + '">\uD83D\uDD04 ' + t.sync + '</button>'
403
- + ' </div>'
404
- + '</div>';
405
-
406
- await joplin.views.panels.setHtml(panel, html);
407
- } catch (err) {
408
- console.error('Notes In List: refresh error', err);
409
- await joplin.views.panels.setHtml(panel, '<div style="padding:12px;color:red;">Error: ' + escapeHtml(String(err)) + '</div>');
410
- }
411
- }
412
-
413
- await joplin.views.panels.onMessage(panel, async function(msg) {
414
- if (msg.name === 'openNote') {
415
- selectedNoteId = msg.id;
416
- await joplin.commands.execute('openNote', msg.id);
417
- } else if (msg.name === 'refresh') {
418
- await refreshPanel();
419
- } else if (msg.name === 'toggleFolder') {
420
- if (collapsedFolders[msg.id]) { delete collapsedFolders[msg.id]; }
421
- else { collapsedFolders[msg.id] = true; }
422
- await refreshPanel();
423
- } else if (msg.name === 'collapseAll') {
424
- var folders = await getAllFolders();
425
- for (var i = 0; i < folders.length; i++) collapsedFolders[folders[i].id] = true;
426
- await refreshPanel();
427
- } else if (msg.name === 'expandAll') {
428
- collapsedFolders = {};
429
- await refreshPanel();
430
- } else if (msg.name === 'newNotebook') {
431
- await joplin.commands.execute('newFolder');
432
- await refreshPanel();
433
- } else if (msg.name === 'newNote') {
434
- await joplin.commands.execute('newNote');
435
- var nn = await joplin.workspace.selectedNote();
436
- if (nn) { selectedNoteId = nn.id; expandToFolder(nn.parent_id); }
437
- await refreshPanel();
438
- } else if (msg.name === 'newTodo') {
439
- await joplin.commands.execute('newTodo');
440
- var nt = await joplin.workspace.selectedNote();
441
- if (nt) { selectedNoteId = nt.id; expandToFolder(nt.parent_id); }
442
- await refreshPanel();
443
- } else if (msg.name === 'search') {
444
- var query = msg.query;
445
- if (!query || !query.trim()) {
446
- // Empty query: tell webview to clear search results
447
- await joplin.views.panels.postMessage(panel, { name: 'searchResults', results: null, query: '', searchId: msg.searchId });
448
- return;
449
- }
450
- try {
451
- var searchResults = [];
452
- var page = 1;
453
- var hasMore = true;
454
- while (hasMore && searchResults.length < 100) {
455
- var result = await joplin.data.get(['search'], {
456
- query: query,
457
- fields: ['id', 'title', 'body', 'parent_id', 'is_todo', 'todo_completed'],
458
- page: page, limit: 20,
459
- });
460
- searchResults = searchResults.concat(result.items);
461
- hasMore = result.has_more;
462
- page++;
463
- }
464
- // Build folder name lookup
465
- var folderNameMap = {};
466
- for (var i = 0; i < allFoldersCache.length; i++) {
467
- folderNameMap[allFoldersCache[i].id] = allFoldersCache[i].title;
468
- }
469
- // Extract snippet around the matched keyword
470
- var items = [];
471
- for (var i = 0; i < searchResults.length; i++) {
472
- var note = searchResults[i];
473
- var snippet = '';
474
- var body = note.body || '';
475
- var lowerBody = body.toLowerCase();
476
- var lowerQuery = query.toLowerCase();
477
- var matchIdx = lowerBody.indexOf(lowerQuery);
478
- if (matchIdx >= 0) {
479
- var start = Math.max(0, matchIdx - 40);
480
- var end = Math.min(body.length, matchIdx + lowerQuery.length + 80);
481
- snippet = (start > 0 ? '...' : '') + body.substring(start, end).replace(/\n/g, ' ') + (end < body.length ? '...' : '');
482
- } else {
483
- // Title match - show beginning of body
484
- snippet = body.substring(0, 120).replace(/\n/g, ' ') + (body.length > 120 ? '...' : '');
485
- }
486
- items.push({
487
- id: note.id,
488
- title: note.title || '(untitled)',
489
- is_todo: note.is_todo,
490
- todo_completed: note.todo_completed,
491
- snippet: snippet,
492
- folderName: folderNameMap[note.parent_id] || '',
493
- });
494
- }
495
- await joplin.views.panels.postMessage(panel, { name: 'searchResults', results: items, query: query, searchId: msg.searchId });
496
- } catch (err) {
497
- console.error('Joplin Explorer: search error', err);
498
- }
499
- } else if (msg.name === 'cycleSort') {
500
- var sortModes = ['updated_desc', 'updated_asc', 'title_asc', 'title_desc'];
501
- var idx = sortModes.indexOf(currentSort);
502
- currentSort = sortModes[(idx + 1) % sortModes.length];
503
- await refreshPanel();
504
- } else if (msg.name === 'sync') {
505
- await joplin.views.panels.postMessage(panel, { name: 'syncState', state: 'syncing' });
506
- try {
507
- await joplin.commands.execute('synchronize');
508
- } catch(e) {
509
- console.error('Joplin Explorer: sync error', e);
510
- }
511
- // Keep "syncing" visible for a few seconds, then show "done" briefly
512
- await new Promise(function(r) { setTimeout(r, 3000); });
513
- await joplin.views.panels.postMessage(panel, { name: 'syncState', state: 'done' });
514
- await refreshPanel();
515
- } else if (msg.name === 'contextMenu') {
516
- var action = msg.action;
517
- var id = msg.id;
518
- var itemType = msg.itemType;
519
- try {
520
- if (itemType === 'folder') {
521
- switch (action) {
522
- case 'newNote':
523
- var newNote = await joplin.data.post(['notes'], null, { title: t.newNote, parent_id: id });
524
- await joplin.commands.execute('openNote', newNote.id);
525
- selectedNoteId = newNote.id;
526
- expandToFolder(id);
527
- break;
528
- case 'newTodo':
529
- var newTodo = await joplin.data.post(['notes'], null, { title: t.newTodo, parent_id: id, is_todo: 1 });
530
- await joplin.commands.execute('openNote', newTodo.id);
531
- selectedNoteId = newTodo.id;
532
- expandToFolder(id);
533
- break;
534
- case 'newSubNotebook':
535
- var subName = await showNativeInput(t.newNotebook, '');
536
- if (subName && subName.trim()) {
537
- await joplin.data.post(['folders'], null, { title: subName.trim(), parent_id: id });
538
- }
539
- break;
540
- case 'deleteFolder':
541
- var folderInfo = await joplin.data.get(['folders', id], { fields: ['title'] });
542
- if (await showNativeConfirm(t.confirmDeleteFolder + '\n\n' + folderInfo.title)) {
543
- await joplin.data.delete(['folders', id]);
544
- }
545
- break;
546
- case 'renameFolder':
547
- var folderData = await joplin.data.get(['folders', id], { fields: ['title'] });
548
- var newFolderName = await showNativeInput(t.promptRename, folderData.title);
549
- if (newFolderName && newFolderName.trim()) {
550
- await joplin.data.put(['folders', id], null, { title: newFolderName.trim() });
551
- }
552
- break;
553
- case 'exportFolder':
554
- try { await joplin.commands.execute('exportFolders', [id]); } catch(e) {}
555
- break;
556
- }
557
- } else if (itemType === 'note') {
558
- switch (action) {
559
- case 'openNote':
560
- await joplin.commands.execute('openNote', id);
561
- selectedNoteId = id;
562
- break;
563
- case 'openInNewWindow':
564
- try { await joplin.commands.execute('openNoteInNewWindow', id); } catch(e) {
565
- // Fallback: just open in main window
566
- await joplin.commands.execute('openNote', id);
567
- }
568
- break;
569
- case 'copyLink':
570
- var linkNote = await joplin.data.get(['notes', id], { fields: ['id', 'title'] });
571
- var mdLink = '[' + linkNote.title + '](:/' + linkNote.id + ')';
572
- try {
573
- await joplin.clipboard.writeText(mdLink);
574
- } catch(e) {
575
- // Fallback: send link text to webview for copying
576
- await joplin.views.panels.postMessage(panel, { name: 'copyText', text: mdLink });
577
- }
578
- break;
579
- case 'duplicateNote':
580
- var srcNote = await joplin.data.get(['notes', id], { fields: ['title', 'body', 'parent_id', 'is_todo'] });
581
- var dupNote = await joplin.data.post(['notes'], null, {
582
- title: srcNote.title + ' (copy)', body: srcNote.body,
583
- parent_id: srcNote.parent_id, is_todo: srcNote.is_todo,
584
- });
585
- await joplin.commands.execute('openNote', dupNote.id);
586
- selectedNoteId = dupNote.id;
587
- break;
588
- case 'switchNoteType':
589
- var sn = await joplin.data.get(['notes', id], { fields: ['is_todo'] });
590
- await joplin.data.put(['notes', id], null, { is_todo: sn.is_todo ? 0 : 1 });
591
- break;
592
- case 'toggleTodo':
593
- var tn = await joplin.data.get(['notes', id], { fields: ['is_todo', 'todo_completed'] });
594
- if (tn.is_todo) {
595
- await joplin.data.put(['notes', id], null, { todo_completed: tn.todo_completed ? 0 : Date.now() });
596
- }
597
- break;
598
- case 'renameNote':
599
- var noteData = await joplin.data.get(['notes', id], { fields: ['title'] });
600
- var newNoteName = await showNativeInput(t.promptRename, noteData.title);
601
- if (newNoteName && newNoteName.trim()) {
602
- await joplin.data.put(['notes', id], null, { title: newNoteName.trim() });
603
- }
604
- break;
605
- case 'moveNote':
606
- if (msg.targetFolderName) {
607
- var targetFolder = null;
608
- for (var i = 0; i < allFoldersCache.length; i++) {
609
- if (allFoldersCache[i].title === msg.targetFolderName) {
610
- targetFolder = allFoldersCache[i]; break;
611
- }
612
- }
613
- if (targetFolder) {
614
- await joplin.data.put(['notes', id], null, { parent_id: targetFolder.id });
615
- }
616
- }
617
- break;
618
- case 'noteInfo':
619
- var info = await joplin.data.get(['notes', id], { fields: ['id', 'title', 'created_time', 'updated_time', 'is_todo', 'parent_id', 'body'] });
620
- var pTitle = '';
621
- for (var i = 0; i < allFoldersCache.length; i++) {
622
- if (allFoldersCache[i].id === info.parent_id) { pTitle = allFoldersCache[i].title; break; }
623
- }
624
- var bodyLen = (info.body || '').length;
625
- var infoBody = 'ID: ' + info.id
626
- + '\n' + (t.ctxRenameNote || 'Title') + ': ' + info.title
627
- + '\n' + (t.newNotebook || 'Notebook') + ': ' + pTitle
628
- + '\n' + (t.sortUpdatedDesc ? t.sortUpdatedDesc.replace(/[↓↑\u2193\u2191]\s*/, '') : 'Created') + ': ' + new Date(info.created_time).toLocaleString()
629
- + '\n' + (t.sortUpdatedAsc ? t.sortUpdatedAsc.replace(/[↓↑\u2193\u2191]\s*/, '') : 'Updated') + ': ' + new Date(info.updated_time).toLocaleString()
630
- + '\nType: ' + (info.is_todo ? 'To-do' : 'Note')
631
- + '\n' + 'Size: ' + bodyLen + ' chars';
632
- await showNativeInfo(info.title, infoBody);
633
- break;
634
- case 'deleteNote':
635
- var noteForDel = await joplin.data.get(['notes', id], { fields: ['title'] });
636
- if (await showNativeConfirm(t.confirmDeleteNote + '\n\n' + noteForDel.title)) {
637
- await joplin.data.delete(['notes', id]);
638
- }
639
- break;
640
- }
641
- }
642
- await refreshPanel();
643
- } catch (err) {
644
- console.error('Notes In List: context menu error', err);
645
- }
646
- } else if (msg.name === 'dragDrop') {
647
- try {
648
- var dragId = msg.dragId;
649
- var dragType = msg.dragType;
650
- var targetId = msg.targetId;
651
- var position = msg.position; // 'into', 'above', 'below'
652
-
653
- if (dragType === 'note') {
654
- // Move note to target folder
655
- if (msg.position === 'into') {
656
- // Find which folder the target belongs to
657
- var targetFolderId = targetId;
658
- // If target is a note, find its parent folder
659
- if (msg.dragType === 'note') {
660
- // Check if targetId is a folder
661
- var isFolder = false;
662
- for (var i = 0; i < allFoldersCache.length; i++) {
663
- if (allFoldersCache[i].id === targetId) { isFolder = true; break; }
664
- }
665
- if (!isFolder) {
666
- // Target is a note, get its parent_id
667
- var targetNote = await joplin.data.get(['notes', targetId], { fields: ['parent_id'] });
668
- targetFolderId = targetNote.parent_id;
669
- }
670
- }
671
- await joplin.data.put(['notes', dragId], null, { parent_id: targetFolderId });
672
- }
673
- } else if (dragType === 'folder') {
674
- if (position === 'into') {
675
- // Move folder as child of target folder
676
- if (dragId !== targetId) {
677
- await joplin.data.put(['folders', dragId], null, { parent_id: targetId });
678
- }
679
- } else {
680
- // Move folder to same parent as target folder
681
- var targetFolder = null;
682
- for (var i = 0; i < allFoldersCache.length; i++) {
683
- if (allFoldersCache[i].id === targetId) { targetFolder = allFoldersCache[i]; break; }
684
- }
685
- if (targetFolder) {
686
- await joplin.data.put(['folders', dragId], null, { parent_id: targetFolder.parent_id || '' });
687
- }
688
- }
689
- }
690
- await refreshPanel();
691
- } catch (err) {
692
- console.error('Notes In List: drag drop error', err);
693
- }
694
- }
695
- });
696
-
697
- await joplin.workspace.onNoteSelectionChange(async function() {
698
- var note = await joplin.workspace.selectedNote();
699
- if (note && note.id !== selectedNoteId) {
700
- selectedNoteId = note.id;
701
- // Only send selection update to webview, don't re-render everything
702
- await joplin.views.panels.postMessage(panel, { name: 'selectNote', id: note.id });
703
- }
704
- });
705
-
706
- await refreshPanel();
707
- },
708
- });
1
+ (()=>{"use strict";const e={zh_CN:{newNotebook:"新建笔记本",newNote:"新建笔记",newTodo:"新建待办",sort:"排序",collapseAll:"全部折叠",expandAll:"全部展开",search:"搜索笔记内容...",searchResultCount:"找到 {count} 条结果",searchNoResult:"没有找到匹配的笔记",searching:"搜索中...",sync:"同步",syncing:"同步中...",syncDone:"✔ 同步完成",loading:"加载中...",sortUpdatedDesc:"↓ 修改时间",sortUpdatedAsc:"↑ 修改时间",sortTitleAsc:"↑ 标题",sortTitleDesc:"↓ 标题",ctxNewNoteHere:"在此新建笔记",ctxNewTodoHere:"在此新建待办",ctxNewSubNotebook:"新建子笔记本",ctxRenameFolder:"重命名",ctxExportFolder:"导出笔记本",ctxDeleteFolder:"删除笔记本",ctxOpenNote:"打开笔记",ctxOpenInNewWindow:"在新窗口中打开",ctxCopyLink:"复制 Markdown 链接",ctxDuplicateNote:"复制副本",ctxSwitchNoteType:"笔记/待办 切换",ctxToggleTodo:"切换完成状态",ctxRenameNote:"重命名",ctxMoveNote:"移动到笔记本...",ctxNoteInfo:"笔记属性",ctxDeleteNote:"删除笔记",confirmDeleteFolder:"确定删除此笔记本及其所有内容吗?",confirmDeleteNote:"确定删除此笔记吗?",promptRename:"请输入新名称:",promptMoveNote:"请输入目标笔记本名称:",promptNewNotebookName:"请输入新笔记本名称:",dropCreateNotebook:"释放以创建新笔记本",searchSectionNotes:"笔记",searchSectionTags:"标签",searchSectionFolders:"笔记本",searchTagNoteCount:"{count} 条笔记",pinned:"收藏夹",ctxPin:"📌 收藏",ctxUnpin:"取消收藏",cancel:"取消"},zh_TW:{newNotebook:"新建筆記本",newNote:"新建筆記",newTodo:"新建待辦",sort:"排序",collapseAll:"全部摺疊",expandAll:"全部展開",search:"搜尋筆記內容...",searchResultCount:"找到 {count} 條結果",searchNoResult:"沒有找到匹配的筆記",searching:"搜尋中...",sync:"同步",syncing:"同步中...",syncDone:"✔ 同步完成",loading:"載入中...",sortUpdatedDesc:"↓ 修改時間",sortUpdatedAsc:"↑ 修改時間",sortTitleAsc:"↑ 標題",sortTitleDesc:"↓ 標題",ctxNewNoteHere:"在此新建筆記",ctxNewTodoHere:"在此新建待辦",ctxNewSubNotebook:"新建子筆記本",ctxRenameFolder:"重新命名",ctxExportFolder:"匯出筆記本",ctxDeleteFolder:"刪除筆記本",ctxOpenNote:"開啟筆記",ctxOpenInNewWindow:"在新視窗中開啟",ctxCopyLink:"複製 Markdown 連結",ctxDuplicateNote:"複製副本",ctxSwitchNoteType:"筆記/待辦 切換",ctxToggleTodo:"切換完成狀態",ctxRenameNote:"重新命名",ctxMoveNote:"移動到筆記本...",ctxNoteInfo:"筆記屬性",ctxDeleteNote:"刪除筆記",confirmDeleteFolder:"確定刪除此筆記本及其所有內容嗎?",confirmDeleteNote:"確定刪除此筆記嗎?",promptRename:"請輸入新名稱:",promptMoveNote:"請輸入目標筆記本名稱:",promptNewNotebookName:"請輸入新筆記本名稱:",dropCreateNotebook:"釋放以建立新筆記本",searchSectionNotes:"筆記",searchSectionTags:"標籤",searchSectionFolders:"筆記本",searchTagNoteCount:"{count} 條筆記",pinned:"收藏夾",ctxPin:"📌 收藏",ctxUnpin:"取消收藏",cancel:"取消"},en_US:{newNotebook:"New Notebook",newNote:"New Note",newTodo:"New To-do",sort:"Sort",collapseAll:"Collapse All",expandAll:"Expand All",search:"Search note contents...",searchResultCount:"{count} results found",searchNoResult:"No matching notes found",searching:"Searching...",sync:"Synchronise",syncing:"Syncing...",syncDone:"✔ Sync Done",loading:"Loading...",sortUpdatedDesc:"↓ Updated",sortUpdatedAsc:"↑ Updated",sortTitleAsc:"↑ Title",sortTitleDesc:"↓ Title",ctxNewNoteHere:"New Note Here",ctxNewTodoHere:"New To-do Here",ctxNewSubNotebook:"New Sub-notebook",ctxRenameFolder:"Rename",ctxExportFolder:"Export Notebook",ctxDeleteFolder:"Delete Notebook",ctxOpenNote:"Open Note",ctxOpenInNewWindow:"Open in New Window",ctxCopyLink:"Copy Markdown Link",ctxDuplicateNote:"Duplicate",ctxSwitchNoteType:"Switch Note/To-do",ctxToggleTodo:"Toggle Completed",ctxRenameNote:"Rename",ctxMoveNote:"Move to Notebook...",ctxNoteInfo:"Note Properties",ctxDeleteNote:"Delete Note",confirmDeleteFolder:"Delete this notebook and all its contents?",confirmDeleteNote:"Delete this note?",promptRename:"Enter new name:",promptMoveNote:"Enter target notebook name:",promptNewNotebookName:"Enter new notebook name:",dropCreateNotebook:"Release to create a new notebook",searchSectionNotes:"Notes",searchSectionTags:"Tags",searchSectionFolders:"Notebooks",searchTagNoteCount:"{count} notes",pinned:"Pinned",ctxPin:"📌 Pin",ctxUnpin:"Unpin",cancel:"Cancel"},ja_JP:{newNotebook:"新規ノートブック",newNote:"新規ノート",newTodo:"新規タスク",sort:"並べ替え",collapseAll:"すべて折りたたむ",expandAll:"すべて展開",search:"ノート内容を検索...",searchResultCount:"{count} 件の結果",searchNoResult:"一致するノートが見つかりません",searching:"検索中...",sync:"同期",syncing:"同期中...",syncDone:"✔ 同期完了",loading:"読み込み中...",sortUpdatedDesc:"↓ 更新日時",sortUpdatedAsc:"↑ 更新日時",sortTitleAsc:"↑ タイトル",sortTitleDesc:"↓ タイトル",ctxNewNoteHere:"ここに新規ノート",ctxNewTodoHere:"ここに新規タスク",ctxNewSubNotebook:"新規サブノートブック",ctxRenameFolder:"名前を変更",ctxExportFolder:"ノートブックをエクスポート",ctxDeleteFolder:"ノートブックを削除",ctxOpenNote:"ノートを開く",ctxOpenInNewWindow:"新しいウィンドウで開く",ctxCopyLink:"Markdownリンクをコピー",ctxDuplicateNote:"複製",ctxSwitchNoteType:"ノート/タスク切り替え",ctxToggleTodo:"完了状態を切り替え",ctxRenameNote:"名前を変更",ctxMoveNote:"ノートブックに移動...",ctxNoteInfo:"ノートプロパティ",ctxDeleteNote:"ノートを削除",confirmDeleteFolder:"このノートブックとその内容をすべて削除しますか?",confirmDeleteNote:"このノートを削除しますか?",promptRename:"新しい名前を入力:",promptMoveNote:"移動先のノートブック名:",promptNewNotebookName:"新しいノートブック名を入力:",dropCreateNotebook:"ドロップして新規ノートブックを作成",searchSectionNotes:"ノート",searchSectionTags:"タグ",searchSectionFolders:"ノートブック",searchTagNoteCount:"{count} 件のノート",pinned:"ピン留め",ctxPin:"📌 ピン留め",ctxUnpin:"ピン解除",cancel:"キャンセル"}};function t(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}async function o(){let e=[],t=1,o=!0;for(;o;){const i=await joplin.data.get(["folders"],{fields:["id","title","parent_id","icon"],page:t,limit:100});e=e.concat(i.items),o=i.has_more,t++}return e}async function i(e){let t=[],o=1,i=!0;for(;i;){const n=await joplin.data.get(["folders",e,"notes"],{fields:["id","title","parent_id","is_todo","todo_completed","updated_time","user_updated_time"],page:o,limit:100,order_by:"user_updated_time",order_dir:"DESC"});t=t.concat(n.items),i=n.has_more,o++}return t}function n(e){let t=e.icon;if(t&&"string"==typeof t)try{t=JSON.parse(t)}catch(e){t=null}return t&&t.emoji?t.emoji:"📂"}function a(e,o,i,s=0){let l="";for(const d of e){const e=18*s;if("folder"===d.type){const r=d.total_count||d.note_count||0,c=i[d.id],p=c?"▶":"▼",w=c?"toggle":"toggle expanded";l+='<div class="tree-item folder" style="padding-left:'+e+'px" data-id="'+d.id+'" data-type="folder">',l+='<span class="'+w+'">'+p+"</span>",l+='<span class="icon folder-icon">'+n(d)+"</span>",l+='<span class="label">'+t(d.title)+"</span>",l+='<span class="count">'+r+"</span>",l+="</div>",l+='<div class="children'+(c?" collapsed":"")+'" data-folder-id="'+d.id+'">',d.children&&(l+=a(d.children,o,i,s+1)),l+="</div>"}else{const i=d.id===o?" selected":"";let n="📝";d.is_todo&&(n=d.todo_completed?"☑":"☐"),l+='<div class="tree-item note'+i+'" style="padding-left:'+e+'px" data-id="'+d.id+'" data-type="note">',l+='<span class="icon note-icon">'+n+"</span>",l+='<span class="label">'+t(d.title)+"</span>",l+="</div>"}}return l}function s(e,t){const o=e.slice();switch(t){case"title_asc":o.sort((e,t)=>(e.title||"").localeCompare(t.title||""));break;case"title_desc":o.sort((e,t)=>(t.title||"").localeCompare(e.title||""));break;case"updated_asc":o.sort((e,t)=>(e.user_updated_time||0)-(t.user_updated_time||0));break;default:o.sort((e,t)=>(t.user_updated_time||0)-(e.user_updated_time||0))}return o}joplin.plugins.register({onStart:async function(){const l=(r=await joplin.settings.globalValue("locale")||"en_US",e[r]||e[r.split("_")[0]]||e.en_US),d=await joplin.views.panels.create("notesInListPanel");var r;await joplin.views.panels.addScript(d,"webview/panel.css"),await joplin.views.panels.addScript(d,"webview/panel.js"),await joplin.views.panels.setHtml(d,'<div id="notes-in-list-root"><p style="padding:12px;">'+l.loading+"</p></div>"),await joplin.views.panels.show(d,!0);try{await joplin.settings.registerSection("joplinExplorer",{label:"Joplin Explorer",iconName:"fas fa-columns"}),await joplin.settings.registerSettings({pinnedItems:{section:"joplinExplorer",type:2,value:'{"notes":[],"folders":[]}',public:!1,label:"Pinned Items (JSON)"}})}catch(e){console.error("Joplin Explorer: failed to register settings",e)}const c=await joplin.views.dialogs.create("explorerInputDialog"),p=await joplin.views.dialogs.create("explorerConfirmDialog"),w=await joplin.views.dialogs.create("explorerInfoDialog");async function f(e){return await joplin.views.dialogs.setHtml(p,'<div style="padding:10px;min-width:280px;"><div style="font-size:13px;">'+t(e)+"</div></div>"),await joplin.views.dialogs.setButtons(p,[{id:"ok",title:"OK"},{id:"cancel",title:l.cancel||"Cancel"}]),"ok"===(await joplin.views.dialogs.open(p)).id}async function m(e,o){await joplin.views.dialogs.setHtml(c,'<div style="padding:10px;min-width:300px;"><div style="margin-bottom:8px;font-size:13px;">'+t(e)+'</div><form name="inputForm"><input name="value" type="text" value="'+t(o||"")+'" style="width:100%;box-sizing:border-box;padding:6px 8px;font-size:13px;" /></form></div>'),await joplin.views.dialogs.setButtons(c,[{id:"ok",title:"OK"},{id:"cancel",title:l.cancel||"Cancel"}]);const i=await joplin.views.dialogs.open(c);return"ok"===i.id&&i.formData&&i.formData.inputForm&&i.formData.inputForm.value||null}let u="",g={},N="updated_desc",h=[],x=[],y=[],_=!1,b=!0;async function j(){try{await joplin.settings.setValue("pinnedItems",JSON.stringify(y))}catch(e){console.error("Joplin Explorer: failed to save pinned items",e)}}function v(e){let t=e;for(;t;){delete g[t];let e=null;for(const o of h)if(o.id===t){e=o;break}t=e?e.parent_id:null}}async function k(){try{const e=await o();if(h=e,b){for(const t of e)g[t.id]=!0;const t=await joplin.workspace.selectedNote();if(t){u=t.id;let o=t.parent_id;for(;o;){delete g[o];let t=null;for(const i of e)if(i.id===o){t=i;break}o=t?t.parent_id:null}}b=!1}const r={},c=10;for(let t=0;t<e.length;t+=c){const o=e.slice(t,t+c),n=await Promise.all(o.map(e=>i(e.id)));for(let e=0;e<o.length;e++)r[o[e].id]=s(n[e],N)}const p=[];for(const e of Object.keys(r))for(const t of r[e])p.push(t);x=p;const w=function(e,t){const o={};for(const t of e)o[t.id]={type:"folder",id:t.id,title:t.title,parent_id:t.parent_id,icon:t.icon,note_count:0,children:[]};for(const e of Object.keys(t)){const i=o[e];if(i){const o=t[e];i.note_count=o.length;for(const e of o)i.children.push({type:"note",id:e.id,title:e.title||"(untitled)",is_todo:e.is_todo,todo_completed:e.todo_completed})}}const i=[];for(const t of e){const e=o[t.id];t.parent_id&&o[t.parent_id]?o[t.parent_id].children.unshift(e):i.push(e)}function n(e){let t=0;if(!e.children)return 0;for(const o of e.children)"note"===o.type?t++:"folder"===o.type&&(n(o),t+=o.total_count||0);return e.total_count=t,t}for(const e of i)n(e);return i}(e,r),f=a(w,u,g);await async function(){try{const e=await joplin.settings.value("pinnedItems");if(!e)return;const t=JSON.parse(e);if(t&&!Array.isArray(t)&&t.notes&&t.folders){y=[];for(const e of t.folders)y.push({id:e,type:"folder"});for(const e of t.notes)y.push({id:e,type:"note"});await j()}else Array.isArray(t)&&(y=t)}catch(e){y=[]}}();const m=new Set(e.map(e=>e.id)),v=new Set(p.map(e=>e.id)),k=y.length;y=y.filter(e=>"folder"===e.type?m.has(e.id):v.has(e.id)),y.length!==k&&await j();let T="";const D=y.length;if(D>0){T+='<div class="pinned-section-header" id="pinned-header"><span class="toggle">'+(_?"▶":"▼")+'</span><span class="icon">📌</span><span class="label">'+l.pinned+" ("+D+")</span></div>",T+='<div class="pinned-section-body'+(_?" collapsed":"")+'" id="pinned-body">';for(const o of y)if("folder"===o.type){let i=null;for(const t of e)if(t.id===o.id){i=t;break}if(i){const e=n(i);T+='<div class="tree-item folder pinned-item" data-id="'+i.id+'" data-type="folder">',T+='<span class="icon folder-icon">'+e+"</span>",T+='<span class="label">'+t(i.title)+"</span>",T+="</div>"}}else{let e=null;for(const t of p)if(t.id===o.id){e=t;break}if(e){const o=e.id===u?" selected":"";let i="📝";e.is_todo&&(i=e.todo_completed?"☑":"☐"),T+='<div class="tree-item note pinned-item'+o+'" data-id="'+e.id+'" data-type="note">',T+='<span class="icon note-icon">'+i+"</span>",T+='<span class="label">'+t(e.title)+"</span>",T+="</div>"}}T+="</div>"}const S=t(JSON.stringify(y)),I={updated_desc:l.sortUpdatedDesc,updated_asc:l.sortUpdatedAsc,title_asc:l.sortTitleAsc,title_desc:l.sortTitleDesc},F='<div id="notes-in-list-root" data-i18n="'+t(JSON.stringify(l))+'" data-pinned="'+S+'"> <div class="toolbar"> <button id="btn-new-notebook" title="'+l.newNotebook+'">📁+</button> <button id="btn-new-note" title="'+l.newNote+'">📝+</button> <button id="btn-new-todo" title="'+l.newTodo+'">☐+</button> <button id="btn-sort" title="'+l.sort+'">'+I[N]+'</button> <button id="btn-collapse-all" title="'+l.collapseAll+'">▲</button> </div> <div class="search-bar"> <input id="search-input" type="text" placeholder="🔍 '+l.search+'" /> </div> <div id="tree-container">'+T+f+' <div id="drop-zone-empty" class="drop-zone-empty">+ '+l.dropCreateNotebook+'</div> </div> <div id="search-results" style="display:none;"></div> <div class="bottom-bar"> <button id="btn-sync" title="'+l.sync+'">🔄 '+l.sync+"</button> </div></div>";await joplin.views.panels.setHtml(d,F)}catch(e){console.error("Notes In List: refresh error",e),await joplin.views.panels.setHtml(d,'<div style="padding:12px;color:red;">Error: '+t(String(e))+"</div>")}}await joplin.views.panels.onMessage(d,async e=>{if("openNote"===e.name)u=e.id,await joplin.commands.execute("openNote",e.id);else if("refresh"===e.name)await k();else if("toggleFolder"===e.name)g[e.id]?delete g[e.id]:g[e.id]=!0,await k();else if("collapseAll"===e.name){const e=await o();for(const t of e)g[t.id]=!0;await k()}else if("expandAll"===e.name)g={},await k();else if("newNotebook"===e.name)await joplin.commands.execute("newFolder"),await k();else if("newNote"===e.name){await joplin.commands.execute("newNote");const e=await joplin.workspace.selectedNote();e&&(u=e.id,v(e.parent_id)),await k()}else if("newTodo"===e.name){await joplin.commands.execute("newTodo");const e=await joplin.workspace.selectedNote();e&&(u=e.id,v(e.parent_id)),await k()}else if("search"===e.name){const t=e.query;if(!t||!t.trim())return void await joplin.views.panels.postMessage(d,{name:"searchResults",results:null,query:"",searchId:e.searchId});try{const o=t.toLowerCase(),i={};for(const e of h)i[e.id]=e.title;let n=[],a=1,s=!0;for(;s&&n.length<100;){const e=await joplin.data.get(["search"],{query:t,fields:["id","title","body","parent_id","is_todo","todo_completed"],page:a,limit:20});n=n.concat(e.items),s=e.has_more,a++}const l=new Set(n.map(e=>e.id)),r=[];for(const e of x)!l.has(e.id)&&(e.title||"").toLowerCase().indexOf(o)>=0&&r.push(e);for(let e=0;e<r.length&&e<50;e++)try{const t=await joplin.data.get(["notes",r[e].id],{fields:["id","title","body","parent_id","is_todo","todo_completed"]});n.push(t)}catch(e){}const c=[];for(const e of n){let t="";const n=e.body||"",a=n.toLowerCase().indexOf(o);if(a>=0){const e=Math.max(0,a-40),i=Math.min(n.length,a+o.length+80);t=(e>0?"...":"")+n.substring(e,i).replace(/\n/g," ")+(i<n.length?"...":"")}else t=n.substring(0,120).replace(/\n/g," ")+(n.length>120?"...":"");c.push({id:e.id,title:e.title||"(untitled)",is_todo:e.is_todo,todo_completed:e.todo_completed,snippet:t,folderName:i[e.parent_id]||""})}let p=[],w=1,f=!0;for(;f;){const e=await joplin.data.get(["tags"],{fields:["id","title"],page:w,limit:100});p=p.concat(e.items),f=e.has_more,w++}const m=[];for(const e of p)if((e.title||"").toLowerCase().indexOf(o)>=0){let t=0;try{const o=await joplin.data.get(["tags",e.id,"notes"],{fields:["id"],limit:1});if(t=o.items.length+(o.has_more?"+":0),o.has_more){let i=o.items.length,n=2,a=!0;for(;a;){const t=await joplin.data.get(["tags",e.id,"notes"],{fields:["id"],page:n,limit:100});i+=t.items.length,a=t.has_more,n++}t=i}}catch(e){}m.push({id:e.id,title:e.title,noteCount:t})}const u=[];for(const e of h)(e.title||"").toLowerCase().indexOf(o)>=0&&u.push({id:e.id,title:e.title,parent_id:e.parent_id,icon:e.icon});await joplin.views.panels.postMessage(d,{name:"searchResults",notes:c,tags:m,folders:u,query:t,searchId:e.searchId})}catch(e){console.error("Joplin Explorer: search error",e)}}else if("loadTagNotes"===e.name)try{const t=e.tagId;let o=[],i=1,n=!0;for(;n&&o.length<100;){const e=await joplin.data.get(["tags",t,"notes"],{fields:["id","title","parent_id","is_todo","todo_completed"],page:i,limit:50});o=o.concat(e.items),n=e.has_more,i++}const a={};for(const e of h)a[e.id]=e.title;const s=o.map(e=>({id:e.id,title:e.title||"(untitled)",is_todo:e.is_todo,todo_completed:e.todo_completed,snippet:"",folderName:a[e.parent_id]||""}));await joplin.views.panels.postMessage(d,{name:"tagNotes",tagId:t,notes:s})}catch(e){console.error("Joplin Explorer: loadTagNotes error",e)}else if("locateFolder"===e.name)try{v(e.folderId),await k(),await joplin.views.panels.postMessage(d,{name:"exitSearchAndLocate",folderId:e.folderId})}catch(e){console.error("Joplin Explorer: locateFolder error",e)}else if("locatePinnedFolder"===e.name)try{v(e.folderId),await k(),await joplin.views.panels.postMessage(d,{name:"scrollToFolder",folderId:e.folderId})}catch(e){console.error("Joplin Explorer: locatePinnedFolder error",e)}else if("reorderPinned"===e.name)try{const t=e.dragId,o=e.targetId,i=e.position,n=y.findIndex(e=>e.id===t);if(n<0)return;const a=y[n];y.splice(n,1);let s=y.findIndex(e=>e.id===o);s<0?y.push(a):("after"===i&&s++,y.splice(s,0,a)),await j(),await k()}catch(e){console.error("Joplin Explorer: reorderPinned error",e)}else if("togglePinnedCollapse"===e.name)_=!_,await k();else if("cycleSort"===e.name){const e=["updated_desc","updated_asc","title_asc","title_desc"],t=e.indexOf(N);N=e[(t+1)%e.length],await k()}else if("sync"===e.name){await joplin.views.panels.postMessage(d,{name:"syncState",state:"syncing"});try{await joplin.commands.execute("synchronize")}catch(e){console.error("Joplin Explorer: sync error",e)}await new Promise(e=>setTimeout(e,3e3)),await joplin.views.panels.postMessage(d,{name:"syncState",state:"done"}),await k()}else if("contextMenu"===e.name){const o=e.action,i=e.id,n=e.itemType;try{if("folder"===n)switch(o){case"newNote":{const e=await joplin.data.post(["notes"],null,{title:l.newNote,parent_id:i});await joplin.commands.execute("openNote",e.id),u=e.id,v(i);break}case"newTodo":{const e=await joplin.data.post(["notes"],null,{title:l.newTodo,parent_id:i,is_todo:1});await joplin.commands.execute("openNote",e.id),u=e.id,v(i);break}case"newSubNotebook":{const e=await m(l.newNotebook,"");e&&e.trim()&&await joplin.data.post(["folders"],null,{title:e.trim(),parent_id:i});break}case"deleteFolder":{const e=await joplin.data.get(["folders",i],{fields:["title"]});await f(l.confirmDeleteFolder+"\n\n"+e.title)&&await joplin.data.delete(["folders",i]);break}case"renameFolder":{const e=await joplin.data.get(["folders",i],{fields:["title"]}),t=await m(l.promptRename,e.title);t&&t.trim()&&await joplin.data.put(["folders",i],null,{title:t.trim()});break}case"exportFolder":try{await joplin.commands.execute("exportFolders",[i])}catch(e){}break;case"pinFolder":y.some(e=>e.id===i)||(y.push({id:i,type:"folder"}),await j());break;case"unpinFolder":y=y.filter(e=>e.id!==i),await j()}else if("note"===n)switch(o){case"openNote":await joplin.commands.execute("openNote",i),u=i;break;case"openInNewWindow":try{await joplin.commands.execute("openNoteInNewWindow",i)}catch(e){await joplin.commands.execute("openNote",i)}break;case"copyLink":{const e=await joplin.data.get(["notes",i],{fields:["id","title"]}),t="["+e.title+"](:/"+e.id+")";try{await joplin.clipboard.writeText(t)}catch(e){await joplin.views.panels.postMessage(d,{name:"copyText",text:t})}break}case"duplicateNote":{const e=await joplin.data.get(["notes",i],{fields:["title","body","parent_id","is_todo"]}),t=await joplin.data.post(["notes"],null,{title:e.title+" (copy)",body:e.body,parent_id:e.parent_id,is_todo:e.is_todo});await joplin.commands.execute("openNote",t.id),u=t.id;break}case"switchNoteType":{const e=await joplin.data.get(["notes",i],{fields:["is_todo"]});await joplin.data.put(["notes",i],null,{is_todo:e.is_todo?0:1});break}case"toggleTodo":{const e=await joplin.data.get(["notes",i],{fields:["is_todo","todo_completed"]});e.is_todo&&await joplin.data.put(["notes",i],null,{todo_completed:e.todo_completed?0:Date.now()});break}case"renameNote":{const e=await joplin.data.get(["notes",i],{fields:["title"]}),t=await m(l.promptRename,e.title);t&&t.trim()&&await joplin.data.put(["notes",i],null,{title:t.trim()});break}case"moveNote":if(e.targetFolderName){let t=null;for(const o of h)if(o.title===e.targetFolderName){t=o;break}t&&await joplin.data.put(["notes",i],null,{parent_id:t.id})}break;case"noteInfo":{const e=await joplin.data.get(["notes",i],{fields:["id","title","created_time","updated_time","is_todo","parent_id","body"]});let o="";for(const t of h)if(t.id===e.parent_id){o=t.title;break}const n=(e.body||"").length,a="ID: "+e.id+"\n"+(l.ctxRenameNote||"Title")+": "+e.title+"\n"+(l.newNotebook||"Notebook")+": "+o+"\n"+(l.sortUpdatedDesc?l.sortUpdatedDesc.replace(/[↓↑\u2193\u2191]\s*/,""):"Created")+": "+new Date(e.created_time).toLocaleString()+"\n"+(l.sortUpdatedAsc?l.sortUpdatedAsc.replace(/[↓↑\u2193\u2191]\s*/,""):"Updated")+": "+new Date(e.updated_time).toLocaleString()+"\nType: "+(e.is_todo?"To-do":"Note")+"\nSize: "+n+" chars";await async function(e,o){await joplin.views.dialogs.setHtml(w,'<div style="padding:10px;min-width:320px;"><div style="font-size:14px;font-weight:bold;margin-bottom:10px;">'+t(e)+'</div><div style="font-size:12px;line-height:1.8;white-space:pre-wrap;">'+t(o)+"</div></div>"),await joplin.views.dialogs.setButtons(w,[{id:"ok",title:"OK"}]),await joplin.views.dialogs.open(w)}(e.title,a);break}case"deleteNote":{const e=await joplin.data.get(["notes",i],{fields:["title"]});await f(l.confirmDeleteNote+"\n\n"+e.title)&&await joplin.data.delete(["notes",i]);break}case"pinNote":y.some(e=>e.id===i)||(y.push({id:i,type:"note"}),await j());break;case"unpinNote":y=y.filter(e=>e.id!==i),await j()}await k()}catch(e){console.error("Notes In List: context menu error",e)}}else if("dragDrop"===e.name)try{const t=e.dragId,o=e.dragType,i=e.targetId,n=e.position;if("note"===o){if("into"===n){let e=i,o=!1;for(const e of h)if(e.id===i){o=!0;break}o||(e=(await joplin.data.get(["notes",i],{fields:["parent_id"]})).parent_id),await joplin.data.put(["notes",t],null,{parent_id:e})}}else if("folder"===o)if("into"===n)t!==i&&await joplin.data.put(["folders",t],null,{parent_id:i});else{let e=null;for(const t of h)if(t.id===i){e=t;break}e&&await joplin.data.put(["folders",t],null,{parent_id:e.parent_id||""})}await k()}catch(e){console.error("Notes In List: drag drop error",e)}else if("dragToEmpty"===e.name)try{const t=e.dragId,o=e.dragType,i=await m(l.promptNewNotebookName,"");if(!i||!i.trim())return;const n=await joplin.data.post(["folders"],null,{title:i.trim()});"note"===o?await joplin.data.put(["notes",t],null,{parent_id:n.id}):"folder"===o&&t!==n.id&&await joplin.data.put(["folders",t],null,{parent_id:n.id}),await k()}catch(e){console.error("Notes In List: drag to empty error",e)}}),await joplin.workspace.onNoteSelectionChange(async()=>{const e=await joplin.workspace.selectedNote();e&&e.id!==u&&(u=e.id,await joplin.views.panels.postMessage(d,{name:"selectNote",id:e.id}))}),await k()}})})();