add: open attachment to magic key command

This commit is contained in:
windingwind 2024-11-12 14:37:42 +01:00
parent 354430200d
commit 1591c0f2a0
3 changed files with 46 additions and 1 deletions

View File

@ -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": "复制章节链接",

View File

@ -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;

View File

@ -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: {