feat: refresh templates in magic palette

resolve: #1212
This commit is contained in:
windingwind 2025-01-05 18:14:53 +01:00
parent 80fcb350ef
commit a59647688e
2 changed files with 45 additions and 3 deletions

View File

@ -11,6 +11,7 @@ declare const _currentEditorInstance: {
interface MagicKeyOptions { interface MagicKeyOptions {
insertTemplate?: () => void; insertTemplate?: () => void;
refreshTemplates?: () => void;
insertLink?: (type: "inbound" | "outbound") => void; insertLink?: (type: "inbound" | "outbound") => void;
copyLink?: (mode: "section" | "line") => void; copyLink?: (mode: "section" | "line") => void;
openAttachment?: () => void; openAttachment?: () => void;
@ -20,6 +21,7 @@ interface MagicKeyOptions {
interface MagicCommand { interface MagicCommand {
messageId?: string; messageId?: string;
searchParts?: string[];
title?: string; title?: string;
icon?: string; icon?: string;
command: (state: EditorState) => void | Transaction; command: (state: EditorState) => void | Transaction;
@ -34,30 +36,35 @@ class PluginState {
_commands: MagicCommand[] = [ _commands: MagicCommand[] = [
{ {
messageId: "insertTemplate", messageId: "insertTemplate",
searchParts: ["it", "insertTemplate"],
command: (state) => { command: (state) => {
this.options.insertTemplate?.(); this.options.insertTemplate?.();
}, },
}, },
{ {
messageId: "outboundLink", messageId: "outboundLink",
searchParts: ["ol", "obl", "outboundLink"],
command: (state) => { command: (state) => {
this.options.insertLink?.("outbound"); this.options.insertLink?.("outbound");
}, },
}, },
{ {
messageId: "inboundLink", messageId: "inboundLink",
searchParts: ["il", "ibl", "inboundLink"],
command: (state) => { command: (state) => {
this.options.insertLink?.("inbound"); this.options.insertLink?.("inbound");
}, },
}, },
{ {
messageId: "insertCitation", messageId: "insertCitation",
searchParts: ["ic", "insertCitation"],
command: (state) => { command: (state) => {
getPlugin("citation")?.insertCitation(); getPlugin("citation")?.insertCitation();
}, },
}, },
{ {
messageId: "openAttachment", messageId: "openAttachment",
searchParts: ["oa", "openAttachment"],
command: (state) => { command: (state) => {
this.options.openAttachment?.(); this.options.openAttachment?.();
}, },
@ -67,18 +74,28 @@ class PluginState {
}, },
{ {
messageId: "copySectionLink", messageId: "copySectionLink",
searchParts: ["csl", "copySectionLink"],
command: (state) => { command: (state) => {
this.options.copyLink?.("section"); this.options.copyLink?.("section");
}, },
}, },
{ {
messageId: "copyLineLink", messageId: "copyLineLink",
searchParts: ["cll", "copyLineLink"],
command: (state) => { command: (state) => {
this.options.copyLink?.("line"); this.options.copyLink?.("line");
}, },
}, },
{
messageId: "refreshTemplates",
searchParts: ["rt", "refreshTemplates"],
command: (state) => {
this.options.refreshTemplates?.();
},
},
{ {
messageId: "table", messageId: "table",
searchParts: ["t", "tb", "table"],
command: (state) => { command: (state) => {
const input = prompt( const input = prompt(
"Enter the number of rows and columns, separated by a comma (e.g., 3,3)", "Enter the number of rows and columns, separated by a comma (e.g., 3,3)",
@ -118,60 +135,70 @@ class PluginState {
}, },
{ {
messageId: "heading1", messageId: "heading1",
searchParts: ["h1", "heading1"],
command: (state) => { command: (state) => {
getPlugin()?.heading1.run(); getPlugin()?.heading1.run();
}, },
}, },
{ {
messageId: "heading2", messageId: "heading2",
searchParts: ["h2", "heading2"],
command: (state) => { command: (state) => {
getPlugin()?.heading2.run(); getPlugin()?.heading2.run();
}, },
}, },
{ {
messageId: "heading3", messageId: "heading3",
searchParts: ["h3", "heading3"],
command: (state) => { command: (state) => {
getPlugin()?.heading3.run(); getPlugin()?.heading3.run();
}, },
}, },
{ {
messageId: "paragraph", messageId: "paragraph",
searchParts: ["p", "paragraph"],
command: (state) => { command: (state) => {
getPlugin()?.paragraph.run(); getPlugin()?.paragraph.run();
}, },
}, },
{ {
messageId: "monospaced", messageId: "monospaced",
searchParts: ["m", "monospaced"],
command: (state) => { command: (state) => {
getPlugin()?.codeBlock.run(); getPlugin()?.codeBlock.run();
}, },
}, },
{ {
messageId: "bulletList", messageId: "bulletList",
searchParts: ["ul", "bulletList", "unorderedList"],
command: (state) => { command: (state) => {
getPlugin()?.bulletList.run(); getPlugin()?.bulletList.run();
}, },
}, },
{ {
messageId: "orderedList", messageId: "orderedList",
searchParts: ["ol", "orderedList"],
command: (state) => { command: (state) => {
getPlugin()?.orderedList.run(); getPlugin()?.orderedList.run();
}, },
}, },
{ {
messageId: "blockquote", messageId: "blockquote",
searchParts: ["bq", "blockquote"],
command: (state) => { command: (state) => {
getPlugin()?.blockquote.run(); getPlugin()?.blockquote.run();
}, },
}, },
{ {
messageId: "mathBlock", messageId: "mathBlock",
searchParts: ["mb", "mathBlock"],
command: (state) => { command: (state) => {
getPlugin()?.math_display.run(); getPlugin()?.math_display.run();
}, },
}, },
{ {
messageId: "clearFormatting", messageId: "clearFormatting",
searchParts: ["cf", "clearFormatting"],
command: (state) => { command: (state) => {
getPlugin()?.clearFormatting.run(); getPlugin()?.clearFormatting.run();
}, },
@ -354,9 +381,24 @@ class PluginState {
const item = this.popup!.container.querySelector( const item = this.popup!.container.querySelector(
`.popup-item[data-command-id="${id}"]`, `.popup-item[data-command-id="${id}"]`,
) as HTMLElement; ) as HTMLElement;
if (!value) {
item.hidden = false;
continue;
}
const matchedIndex = command const matchedIndex = command
.title!.toLowerCase() .title!.toLowerCase()
.indexOf(value.toLowerCase()); .indexOf(value.toLowerCase());
if (
matchedIndex < 0 &&
// Try to match the search parts
!command.searchParts?.some((part) =>
part.toLowerCase().includes(value.toLowerCase()),
)
) {
item.hidden = true;
} else {
item.hidden = false;
}
if (matchedIndex >= 0) { if (matchedIndex >= 0) {
// Change the matched part to bold // Change the matched part to bold
const title = command.title!; const title = command.title!;
@ -364,9 +406,6 @@ class PluginState {
title.slice(0, matchedIndex) + title.slice(0, matchedIndex) +
`<b>${title.slice(matchedIndex, matchedIndex + value.length)}</b>` + `<b>${title.slice(matchedIndex, matchedIndex + value.length)}</b>` +
title.slice(matchedIndex + value.length); title.slice(matchedIndex + value.length);
item.hidden = false;
} else {
item.hidden = true;
} }
} }
this._selectCommand(); this._selectCommand();

View File

@ -521,6 +521,9 @@ function initEditorPlugins(editor: Zotero.EditorInstance) {
lineIndex: getLineAtCursor(editor), lineIndex: getLineAtCursor(editor),
}); });
}, },
refreshTemplates: () => {
addon.hooks.onRefreshTemplatesInNote(editor);
},
insertLink: (mode: "inbound" | "outbound") => { insertLink: (mode: "inbound" | "outbound") => {
openLinkCreator(editor._item, { openLinkCreator(editor._item, {
lineIndex: getLineAtCursor(editor), lineIndex: getLineAtCursor(editor),