From 8c9c2a982df0950f71e799e59b9c24e79bc84897 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Mon, 14 Nov 2022 22:36:13 +0800 Subject: [PATCH] add: note link actions #172 --- addon/defaults/preferences/defaults.js | 2 + src/editor/editorViews.ts | 72 ++++++++++----------- src/note/noteExportController.ts | 2 - src/note/noteUtils.ts | 9 +-- src/zotero/events.ts | 89 ++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 42 deletions(-) diff --git a/addon/defaults/preferences/defaults.js b/addon/defaults/preferences/defaults.js index 013c7e8..80fab7b 100644 --- a/addon/defaults/preferences/defaults.js +++ b/addon/defaults/preferences/defaults.js @@ -20,3 +20,5 @@ pref("extensions.zotero.Knowledge4Zotero.OCRMathpix.APIKey", ""); pref("extensions.zotero.Knowledge4Zotero.linkAction.click", ""); pref("extensions.zotero.Knowledge4Zotero.linkAction.shiftclick", ""); pref("extensions.zotero.Knowledge4Zotero.linkAction.ctrlclick", ""); +pref("extensions.zotero.Knowledge4Zotero.linkAction.altclick", ""); +pref("extensions.zotero.Knowledge4Zotero.linkAction.preview", true); diff --git a/src/editor/editorViews.ts b/src/editor/editorViews.ts index 276e669..cdd7ac7 100644 --- a/src/editor/editorViews.ts +++ b/src/editor/editorViews.ts @@ -754,44 +754,44 @@ class EditorViews extends AddonBase { ".link-popup" ) as HTMLElement; - let previewContainer = - _window.document.getElementById("note-link-preview"); - if (previewContainer) { - previewContainer.remove(); - } - previewContainer = _window.document.createElementNS( - "http://www.w3.org/1999/xhtml", - "div" - ); - previewContainer.id = "note-link-preview"; - previewContainer.className = "ProseMirror primary-editor"; - previewContainer.innerHTML = - await this._Addon.NoteParse.parseNoteStyleHTML(note); - previewContainer.addEventListener("click", (e) => { - this._Addon.WorkspaceWindow.setWorkspaceNote("preview", note); - }); if (linkPopup) { - linkPopup.append( - insertButton, - updateButton, - openInWindowButton, - previewContainer - ); - previewContainer.setAttribute( - "style", - `width: 98%;height: ${ - linkPopup ? Math.min(linkPopup.offsetTop, 300) : 300 - }px;position: absolute;background: white;bottom: 36px;overflow: hidden;box-shadow: 0 0 5px 5px rgba(0,0,0,0.2);border-radius: 5px;cursor: pointer;opacity: 0.9;` - ); - previewContainer - .querySelector("div[data-schema-version]") - .childNodes.forEach((node) => { - if ((node as Element).setAttribute) { - (node as Element).setAttribute("style", "margin: 0"); - } else { - node.remove(); - } + linkPopup.append(insertButton, updateButton, openInWindowButton); + if ( + Zotero.Prefs.get("Knowledge4Zotero.linkAction.preview") as boolean + ) { + let previewContainer = + _window.document.getElementById("note-link-preview"); + if (previewContainer) { + previewContainer.remove(); + } + previewContainer = _window.document.createElementNS( + "http://www.w3.org/1999/xhtml", + "div" + ); + previewContainer.id = "note-link-preview"; + previewContainer.className = "ProseMirror primary-editor"; + previewContainer.innerHTML = + await this._Addon.NoteParse.parseNoteStyleHTML(note); + previewContainer.addEventListener("click", (e) => { + this._Addon.WorkspaceWindow.setWorkspaceNote("preview", note); }); + linkPopup.append(previewContainer); + previewContainer.setAttribute( + "style", + `width: 98%;height: ${ + linkPopup ? Math.min(linkPopup.offsetTop, 300) : 300 + }px;position: absolute;background: white;bottom: 36px;overflow: hidden;box-shadow: 0 0 5px 5px rgba(0,0,0,0.2);border-radius: 5px;cursor: pointer;opacity: 0.9;` + ); + previewContainer + .querySelector("div[data-schema-version]") + .childNodes.forEach((node) => { + if ((node as Element).setAttribute) { + (node as Element).setAttribute("style", "margin: 0"); + } else { + node.remove(); + } + }); + } } } else { const insertLink = _window.document.querySelector("#insert-note-link"); diff --git a/src/note/noteExportController.ts b/src/note/noteExportController.ts index 4e9f970..ccad5a4 100644 --- a/src/note/noteExportController.ts +++ b/src/note/noteExportController.ts @@ -57,8 +57,6 @@ class NoteExport extends AddonBase { await Zotero.Notes.copyEmbeddedImages(subNote, newNote); } }); - - } else { newNote = note; } diff --git a/src/note/noteUtils.ts b/src/note/noteUtils.ts index 613f934..ac9bff5 100644 --- a/src/note/noteUtils.ts +++ b/src/note/noteUtils.ts @@ -619,18 +619,19 @@ class NoteUtils extends AddonBase { const params = this._Addon.NoteParse.parseParamsFromLink(uri); if (!params.libraryID) { return { - item: false, + item: undefined, + args: {}, infoText: "Library does not exist or access denied.", }; } Zotero.debug(params); - let item = await Zotero.Items.getByLibraryAndKeyAsync( + let item: Zotero.Item = await Zotero.Items.getByLibraryAndKeyAsync( params.libraryID, params.noteKey ); if (!item || !item.isNote()) { return { - item: false, + item: undefined, args: params, infoText: "Note does not exist or is not a note.", }; @@ -721,7 +722,7 @@ class NoteUtils extends AddonBase { !( linkPopup && (linkPopup.querySelector("a") as unknown as HTMLLinkElement) - .href === link + ?.href === link ) && t < 100 ) { diff --git a/src/zotero/events.ts b/src/zotero/events.ts index 6448986..6f64728 100644 --- a/src/zotero/events.ts +++ b/src/zotero/events.ts @@ -324,6 +324,95 @@ class ZoteroEvents extends AddonBase { await this._Addon.NoteUtils.onSelectionChange(instance); } ); + instance._iframeWindow.document.addEventListener( + "click", + async (e) => { + if ((e.target as HTMLElement).tagName === "A") { + const link = (e.target as HTMLLinkElement).href; + const actions = { + // @ts-ignore + openLink: () => window.openURL(link), + openLinkIfNote: () => { + link.includes("zotero://note") + ? actions.openLink() + : null; + }, + openLinkIfSelect: () => { + link.includes("zotero://select") + ? actions.openLink() + : null; + }, + openLinkIfPDF: () => { + link.includes("zotero://open-pdf") + ? actions.openLink() + : null; + }, + openLinkInNewWindow: async () => { + if (link.includes("zotero://note")) { + ZoteroPane.openNoteWindow( + ( + (await this._Addon.NoteUtils.getNoteFromLink(link)) + .item as Zotero.Item + )?.id + ); + } else { + actions.openLink(); + } + }, + copyLink: async () => { + new CopyHelper() + .addText(link, "text/unicode") + .addText( + (e.target as HTMLLinkElement).outerHTML, + "text/html" + ) + .copy(); + }, + setMainNote: async () => { + const noteItem = ( + await this._Addon.NoteUtils.getNoteFromLink(link) + ).item as Zotero.Item; + if (!noteItem) { + return; + } + await this.onEditorEvent( + new EditorMessage("setMainNote", { + params: { + itemID: noteItem.id, + enableConfirm: false, + enableOpen: true, + }, + }) + ); + }, + }; + const shiftAction = Zotero.Prefs.get( + "Knowledge4Zotero.linkAction.shiftclick" + ) as string; + const ctrlAction = Zotero.Prefs.get( + "Knowledge4Zotero.linkAction.ctrlclick" + ) as string; + const altAction = Zotero.Prefs.get( + "Knowledge4Zotero.linkAction.altclick" + ) as string; + const clickAction = Zotero.Prefs.get( + "Knowledge4Zotero.linkAction.click" + ) as string; + if (e.shiftKey && shiftAction) { + actions[shiftAction](); + } else if (e.ctrlKey && ctrlAction) { + actions[ctrlAction](); + } else if (e.altKey && altAction) { + actions[altAction](); + } else if ( + clickAction && + !(e.shiftKey || e.ctrlKey || e.altKey) + ) { + actions[clickAction](); + } + } + } + ); instance._knowledgeSelectionInitialized = true; }