From ed5bfad6d21010604662c41c9666de0574075dc3 Mon Sep 17 00:00:00 2001 From: liangwang Date: Thu, 14 Mar 2024 15:36:26 +0800 Subject: [PATCH] add title, so user can use sqlite gui client get plugin-joplin.plugin.note.tabs.noteTabs, modify by text editor, and write back --- src/index.ts | 26 +++++++++++++++++++++++--- src/noteTabs.ts | 6 ++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index d6a94a2..8d11c61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ joplin.plugins.register({ // settings const settings: Settings = new Settings(); await settings.register(); + // note tabs const tabs = new NoteTabs(settings); // last active note @@ -24,6 +25,25 @@ joplin.plugins.register({ const panel = new Panel(tabs, settings); await panel.register(); + await settings.read(); + let updateTitle = false + for (const noteTab of tabs.tabs) { + let note: any = null; + // get real note from database, if no longer exists remove tab and continue with next one + if (noteTab.title == undefined || noteTab.title.length==0){ + try { + note = await joplin.data.get(['notes', noteTab.id], { fields: ['id', 'title'] }); + } catch (error) { + continue; + } + noteTab.title = note.title; + updateTitle = true + } + } + if (updateTitle) { + await settings.storeTabs(); + } + //#region HELPERS /** @@ -43,7 +63,7 @@ joplin.plugins.register({ tabs.replaceTemp(noteId); } else { // or add as new temporary tab at the end - await tabs.add(noteId, NoteTabType.Temporary); + await tabs.add(noteId, '', NoteTabType.Temporary); } } } @@ -52,7 +72,7 @@ joplin.plugins.register({ * Add new or pin tab for handled note. Optionally at the specified index of targetId. */ async function pinTab(noteId: string, addAsNew: boolean, targetId?: string) { - const note: any = await DATA.get(['notes', noteId], { fields: ['id', 'is_todo', 'todo_completed'] }); + const note: any = await DATA.get(['notes', noteId], { fields: ['id', 'title', 'is_todo', 'todo_completed'] }); if (note) { // do not pin completed todos if auto unpin is enabled @@ -64,7 +84,7 @@ joplin.plugins.register({ } else { // otherwise add as new one if (addAsNew) { - await tabs.add(note.id, NoteTabType.Pinned, targetId); + await tabs.add(note.id, note.title, NoteTabType.Pinned, targetId); } } } diff --git a/src/noteTabs.ts b/src/noteTabs.ts index cd36606..8b5631e 100644 --- a/src/noteTabs.ts +++ b/src/noteTabs.ts @@ -15,6 +15,8 @@ export enum NoteTabType { interface INoteTab { // Joplin note ID id: string, + // Joplin note Title + title: string, // Type of the tab type: NoteTabType } @@ -115,11 +117,11 @@ export class NoteTabs { /** * Adds note as new tab at the end. */ - async add(newId: string, newType: NoteTabType, targetId?: string) { + async add(newId: string, title: string, newType: NoteTabType, targetId?: string) { if (newId === undefined || newType === undefined) return; const index = this.indexOf(targetId); - const newTab: any = { id: newId, type: newType }; + const newTab: any = { id: newId, title:title, type: newType }; if (index >= 0) { this.tabs.splice(index, 0, newTab); } else {