From 97c487837622a955df4467489c2878ce6638b0d6 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 8 Nov 2024 01:17:38 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/plugin-sample/issues/24 --- app/src/plugin/index.ts | 4 ++ app/src/protyle/toolbar/index.ts | 6 +- app/src/protyle/toolbar/util.ts | 118 +++++++++++++++++++++++++++++++ app/src/protyle/util/Options.ts | 117 +----------------------------- 4 files changed, 128 insertions(+), 117 deletions(-) diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts index 070567e3edc..42af4c850df 100644 --- a/app/src/plugin/index.ts +++ b/app/src/plugin/index.ts @@ -373,6 +373,10 @@ export class Plugin { })); }; + public updateProtyleToolbar(toolbar: Array) { + return toolbar; + } + set protyleOptions(options: IProtyleOptions) { this.protyleOptionsValue = options; } diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index d54cdc57c7b..1ae12e52eaf 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -38,7 +38,7 @@ import {insertEmptyBlock} from "../../block/util"; import {matchHotKey} from "../util/hotKey"; import {hideElements} from "../ui/hideElements"; import {electronUndo} from "../undo"; -import {previewTemplate} from "./util"; +import {previewTemplate, toolbarKeyToMenu} from "./util"; import {hideMessage, showMessage} from "../../dialog/message"; import {InlineMath} from "./InlineMath"; import {InlineMemo} from "./InlineMemo"; @@ -68,7 +68,9 @@ export class Toolbar { this.subElement.className = "protyle-util fn__none"; /// #endif this.toolbarHeight = 29; - + protyle.app.plugins.forEach(item => { + options.toolbar = toolbarKeyToMenu(item.updateProtyleToolbar(options.toolbar)); + }); options.toolbar.forEach((menuItem: IMenuItem) => { const itemElement = this.genItem(protyle, menuItem); this.element.appendChild(itemElement); diff --git a/app/src/protyle/toolbar/util.ts b/app/src/protyle/toolbar/util.ts index 64d9dd5080e..0df245c89fc 100644 --- a/app/src/protyle/toolbar/util.ts +++ b/app/src/protyle/toolbar/util.ts @@ -95,3 +95,121 @@ export const removeInlineType = (inlineElement: HTMLElement, type: string, range } } }; + +export const toolbarKeyToMenu = (toolbar: Array) => { + const toolbarItem: IMenuItem [] = [{ + name: "block-ref", + hotkey: window.siyuan.config.keymap.editor.insert.ref.custom, + lang: "ref", + icon: "iconRef", + tipPosition: "ne", + }, { + name: "a", + hotkey: window.siyuan.config.keymap.editor.insert.link.custom, + lang: "link", + icon: "iconLink", + tipPosition: "n", + }, { + name: "strong", + lang: "bold", + hotkey: window.siyuan.config.keymap.editor.insert.bold.custom, + icon: "iconBold", + tipPosition: "n", + }, { + name: "em", + lang: "italic", + hotkey: window.siyuan.config.keymap.editor.insert.italic.custom, + icon: "iconItalic", + tipPosition: "n", + }, { + name: "u", + lang: "underline", + hotkey: window.siyuan.config.keymap.editor.insert.underline.custom, + icon: "iconUnderline", + tipPosition: "n", + }, { + name: "s", + lang: "strike", + hotkey: window.siyuan.config.keymap.editor.insert.strike.custom, + icon: "iconStrike", + tipPosition: "n", + }, { + name: "mark", + lang: "mark", + hotkey: window.siyuan.config.keymap.editor.insert.mark.custom, + icon: "iconMark", + tipPosition: "n", + }, { + name: "sup", + lang: "sup", + hotkey: window.siyuan.config.keymap.editor.insert.sup.custom, + icon: "iconSup", + tipPosition: "n", + }, { + name: "sub", + lang: "sub", + hotkey: window.siyuan.config.keymap.editor.insert.sub.custom, + icon: "iconSub", + tipPosition: "n", + }, { + name: "kbd", + lang: "kbd", + hotkey: window.siyuan.config.keymap.editor.insert.kbd.custom, + icon: "iconKeymap", + tipPosition: "n", + }, { + name: "tag", + lang: "tag", + hotkey: window.siyuan.config.keymap.editor.insert.tag.custom, + icon: "iconTags", + tipPosition: "n", + }, { + name: "code", + lang: "inline-code", + hotkey: window.siyuan.config.keymap.editor.insert["inline-code"].custom, + icon: "iconInlineCode", + tipPosition: "n", + }, { + name: "inline-math", + lang: "inline-math", + hotkey: window.siyuan.config.keymap.editor.insert["inline-math"].custom, + icon: "iconMath", + tipPosition: "n", + }, { + name: "inline-memo", + lang: "memo", + hotkey: window.siyuan.config.keymap.editor.insert.memo.custom, + icon: "iconM", + tipPosition: "n", + }, { + name: "text", + lang: "appearance", + hotkey: window.siyuan.config.keymap.editor.insert.appearance.custom, + icon: "iconFont", + tipPosition: "n", + }, { + name: "clear", + lang: "clearInline", + hotkey: window.siyuan.config.keymap.editor.insert.clearInline.custom, + icon: "iconClear", + tipPosition: "n", + }, { + name: "|", + }]; + const toolbarResult: IMenuItem[] = []; + toolbar.forEach((menuItem: IMenuItem) => { + let currentMenuItem = menuItem; + toolbarItem.find((defaultMenuItem: IMenuItem) => { + if (typeof menuItem === "string" && defaultMenuItem.name === menuItem) { + currentMenuItem = defaultMenuItem; + return true; + } + if (typeof menuItem === "object" && defaultMenuItem.name === menuItem.name) { + currentMenuItem = Object.assign({}, defaultMenuItem, menuItem); + return true; + } + }); + toolbarResult.push(currentMenuItem); + }); + return toolbarResult; +} diff --git a/app/src/protyle/util/Options.ts b/app/src/protyle/util/Options.ts index 98d12821755..19b34aad636 100644 --- a/app/src/protyle/util/Options.ts +++ b/app/src/protyle/util/Options.ts @@ -2,6 +2,7 @@ import {Constants} from "../../constants"; import {merge} from "./merge"; import {hintEmbed, hintRef, hintSlash, hintTag} from "../hint/extend"; import {isMobile} from "../../util/functions"; +import {toolbarKeyToMenu} from "../toolbar/util"; export class Options { public options: IProtyleOptions; @@ -152,120 +153,6 @@ export class Options { } private mergeToolbar(toolbar: Array) { - const toolbarItem: IMenuItem [] = [{ - name: "block-ref", - hotkey: window.siyuan.config.keymap.editor.insert.ref.custom, - lang: "ref", - icon: "iconRef", - tipPosition: "ne", - }, { - name: "a", - hotkey: window.siyuan.config.keymap.editor.insert.link.custom, - lang: "link", - icon: "iconLink", - tipPosition: "n", - }, { - name: "strong", - lang: "bold", - hotkey: window.siyuan.config.keymap.editor.insert.bold.custom, - icon: "iconBold", - tipPosition: "n", - }, { - name: "em", - lang: "italic", - hotkey: window.siyuan.config.keymap.editor.insert.italic.custom, - icon: "iconItalic", - tipPosition: "n", - }, { - name: "u", - lang: "underline", - hotkey: window.siyuan.config.keymap.editor.insert.underline.custom, - icon: "iconUnderline", - tipPosition: "n", - }, { - name: "s", - lang: "strike", - hotkey: window.siyuan.config.keymap.editor.insert.strike.custom, - icon: "iconStrike", - tipPosition: "n", - }, { - name: "mark", - lang: "mark", - hotkey: window.siyuan.config.keymap.editor.insert.mark.custom, - icon: "iconMark", - tipPosition: "n", - }, { - name: "sup", - lang: "sup", - hotkey: window.siyuan.config.keymap.editor.insert.sup.custom, - icon: "iconSup", - tipPosition: "n", - }, { - name: "sub", - lang: "sub", - hotkey: window.siyuan.config.keymap.editor.insert.sub.custom, - icon: "iconSub", - tipPosition: "n", - }, { - name: "kbd", - lang: "kbd", - hotkey: window.siyuan.config.keymap.editor.insert.kbd.custom, - icon: "iconKeymap", - tipPosition: "n", - }, { - name: "tag", - lang: "tag", - hotkey: window.siyuan.config.keymap.editor.insert.tag.custom, - icon: "iconTags", - tipPosition: "n", - }, { - name: "code", - lang: "inline-code", - hotkey: window.siyuan.config.keymap.editor.insert["inline-code"].custom, - icon: "iconInlineCode", - tipPosition: "n", - }, { - name: "inline-math", - lang: "inline-math", - hotkey: window.siyuan.config.keymap.editor.insert["inline-math"].custom, - icon: "iconMath", - tipPosition: "n", - }, { - name: "inline-memo", - lang: "memo", - hotkey: window.siyuan.config.keymap.editor.insert.memo.custom, - icon: "iconM", - tipPosition: "n", - }, { - name: "text", - lang: "appearance", - hotkey: window.siyuan.config.keymap.editor.insert.appearance.custom, - icon: "iconFont", - tipPosition: "n", - }, { - name: "clear", - lang: "clearInline", - hotkey: window.siyuan.config.keymap.editor.insert.clearInline.custom, - icon: "iconClear", - tipPosition: "n", - }, { - name: "|", - }]; - const toolbarResult: IMenuItem[] = []; - toolbar.forEach((menuItem: IMenuItem) => { - let currentMenuItem = menuItem; - toolbarItem.find((defaultMenuItem: IMenuItem) => { - if (typeof menuItem === "string" && defaultMenuItem.name === menuItem) { - currentMenuItem = defaultMenuItem; - return true; - } - if (typeof menuItem === "object" && defaultMenuItem.name === menuItem.name) { - currentMenuItem = Object.assign({}, defaultMenuItem, menuItem); - return true; - } - }); - toolbarResult.push(currentMenuItem); - }); - return toolbarResult; + return toolbarKeyToMenu(toolbar); } }