From 1591c0f2a01f3c6708138e69aad48ff6d6d0f650 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:37:42 +0100 Subject: [PATCH] add: open attachment to magic key command --- src/extras/editor/editorStrings.ts | 4 ++++ src/extras/editor/magicKey.ts | 23 ++++++++++++++++++++++- src/utils/editor.ts | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/extras/editor/editorStrings.ts b/src/extras/editor/editorStrings.ts index 4967726..f2f8111 100644 --- a/src/extras/editor/editorStrings.ts +++ b/src/extras/editor/editorStrings.ts @@ -28,6 +28,10 @@ const editorStrings = { "en-US": "Insert Citation", "zh-CN": "插入引用", }, + openAttachment: { + "en-US": "Open attachment of parent item", + "zh-CN": "打开父条目的附件", + }, copySectionLink: { "en-US": "Copy Section Link", "zh-CN": "复制章节链接", diff --git a/src/extras/editor/magicKey.ts b/src/extras/editor/magicKey.ts index 91b22e0..d962893 100644 --- a/src/extras/editor/magicKey.ts +++ b/src/extras/editor/magicKey.ts @@ -13,6 +13,8 @@ interface MagicKeyOptions { insertTemplate?: () => void; insertLink?: (type: "inbound" | "outbound") => void; copyLink?: (mode: "section" | "line") => void; + openAttachment?: () => void; + canOpenAttachment?: () => boolean; enable?: boolean; } @@ -21,6 +23,7 @@ interface MagicCommand { title?: string; icon?: string; command: (state: EditorState) => void | Transaction; + enabled?: (state: EditorState) => boolean; } class PluginState { @@ -28,7 +31,7 @@ class PluginState { options: MagicKeyOptions; - commands: MagicCommand[] = [ + _commands: MagicCommand[] = [ { messageId: "insertTemplate", command: (state) => { @@ -53,6 +56,15 @@ class PluginState { getPlugin("citation")?.insertCitation(); }, }, + { + messageId: "openAttachment", + command: (state) => { + this.options.openAttachment?.(); + }, + enabled: (state) => { + return this.options.canOpenAttachment?.() || false; + }, + }, { messageId: "copySectionLink", command: (state) => { @@ -166,6 +178,15 @@ class PluginState { }, ]; + get commands() { + return this._commands.filter((command) => { + if (command.enabled) { + return command.enabled(this.state); + } + return true; + }); + } + popup: Popup | null = null; selectedCommandIndex = 0; diff --git a/src/utils/editor.ts b/src/utils/editor.ts index 3647198..f6e80e0 100644 --- a/src/utils/editor.ts +++ b/src/utils/editor.ts @@ -530,6 +530,26 @@ function initEditorPlugins(editor: Zotero.EditorInstance) { copyLink: (mode: "section" | "line") => { copyNoteLink(editor, mode); }, + openAttachment: () => { + editor._item.parentItem + ?.getBestAttachment() + .then((attachment) => { + if (!attachment) { + return; + } + Zotero.getActiveZoteroPane().viewAttachment([attachment.id]); + Zotero.Notifier.trigger("open", "file", attachment.id); + }); + }, + canOpenAttachment: () => { + const parentItem = editor._item.parentItem; + if (!parentItem) { + return false; + } + return ( + (editor._item.parentItem as Zotero.Item).numAttachments() > 0 + ); + }, enable: getPref("editor.useMagicKey") as boolean, }, markdownPaste: {