From 4dd666c92d35cbfd400eac33b52ae030b9199693 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Mon, 16 May 2022 17:34:01 +0800 Subject: [PATCH] feat: import notes resolve: #10 --- addon/chrome/content/workspace.xul | 3 ++ addon/chrome/locale/en-US/overlay.dtd | 1 + addon/chrome/locale/zh-CN/overlay.dtd | 1 + src/events.ts | 42 +++++++++++++++++++++++++++ src/knowledge.ts | 29 ++++++++++-------- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/addon/chrome/content/workspace.xul b/addon/chrome/content/workspace.xul index cd7b034..8ce9db1 100644 --- a/addon/chrome/content/workspace.xul +++ b/addon/chrome/content/workspace.xul @@ -29,6 +29,7 @@ + @@ -36,6 +37,7 @@ + @@ -55,6 +57,7 @@ + diff --git a/addon/chrome/locale/en-US/overlay.dtd b/addon/chrome/locale/en-US/overlay.dtd index ab56aa6..951270d 100644 --- a/addon/chrome/locale/en-US/overlay.dtd +++ b/addon/chrome/locale/en-US/overlay.dtd @@ -2,6 +2,7 @@ + diff --git a/addon/chrome/locale/zh-CN/overlay.dtd b/addon/chrome/locale/zh-CN/overlay.dtd index 313f8d3..e041902 100644 --- a/addon/chrome/locale/zh-CN/overlay.dtd +++ b/addon/chrome/locale/zh-CN/overlay.dtd @@ -2,6 +2,7 @@ + diff --git a/src/events.ts b/src/events.ts index c0a8d1b..f93d132 100644 --- a/src/events.ts +++ b/src/events.ts @@ -667,6 +667,48 @@ class AddonEvents extends AddonBase { -1, node.model.lineIndex ); + } else if (message.type === "import") { + /* + message.content = {} + */ + const io = { + // Not working + singleSelection: true, + dataIn: null, + dataOut: null, + deferred: Zotero.Promise.defer(), + }; + + (window as unknown as XULWindow).openDialog( + "chrome://zotero/content/selectItemsDialog.xul", + "", + "chrome,dialog=no,centerscreen,resizable=yes", + io + ); + await io.deferred.promise; + + const ids = io.dataOut; + const notes = Zotero.Items.get(ids).filter((item: ZoteroItem) => + item.isNote() + ); + if (notes.length === 0) { + return; + } + + const newLines = []; + newLines.push("

Imported Notes

"); + newLines.push("

"); + + for (const note of notes) { + const linkURL = this._Addon.knowledge.getNoteLink(note); + const linkText = note.getNoteTitle().trim(); + newLines.push( + `

${linkText ? linkText : linkURL}

` + ); + newLines.push("

"); + } + // End of line + await this._Addon.knowledge.addLinesToNote(undefined, newLines, 65535); } else if (message.type === "export") { /* message.content = { diff --git a/src/knowledge.ts b/src/knowledge.ts index bd117eb..3b4f255 100644 --- a/src/knowledge.ts +++ b/src/knowledge.ts @@ -358,20 +358,12 @@ class Knowledge extends AddonBase { this._Addon.views.showProgressWindow("Better Notes", "Not a note item"); return; } - let libraryID = linkedNote.libraryID; - let library = Zotero.Libraries.get(libraryID); - let groupID: string; - if (library.libraryType === "user") { - groupID = "u"; - } else if (library.libraryType === "group") { - groupID = `${library.id}`; - } - let noteKey = linkedNote.key; - let linkText = linkedNote.getNoteTitle().trim(); + const link = this.getNoteLink(linkedNote); + const linkText = linkedNote.getNoteTitle().trim(); this.addSubLineToNote( targetNote, - `${ - linkText ? linkText : `zotero://note/${groupID}/${noteKey}/` + `${ + linkText ? linkText : `${link}` }`, lineIndex, true @@ -382,6 +374,19 @@ class Knowledge extends AddonBase { ); } + getNoteLink(note: ZoteroItem) { + let libraryID = note.libraryID; + let library = Zotero.Libraries.get(libraryID); + let groupID: string; + if (library.libraryType === "user") { + groupID = "u"; + } else if (library.libraryType === "group") { + groupID = `${library.id}`; + } + let noteKey = note.key; + return `zotero://note/${groupID}/${noteKey}/`; + } + async modifyLineInNote(note: ZoteroItem, text: string, lineIndex: number) { note = note || this.getWorkspaceNote(); if (!note) {