Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Refresh mobile plugin toolbar after plugin changes https://github.com/siyuan-note/siyuan/issues/18596

Danielea44206ff5a0
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 53eb474259..921b8efc47 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -17,6 +17,7 @@ import {tabCodeBlock} from "../../protyle/wysiwyg/codeBlock";
import {callMobileAppShowKeyboard, canInput, keyboardLockUntil} from "./mobileAppUtil";
import {isNotEditBlock} from "../../protyle/wysiwyg/getBlock";
import {getMirror, getUndoRootID, hasUndoStateMirror, initMirror} from "../../protyle/undo/globalUndo";
+import {getMobilePluginToolbarItems} from "./pluginToolbar";
let renderKeyboardToolbarTimeout: number;
let scrollSelectionIntoViewTimeout: number;
@@ -33,6 +34,31 @@ let lastAndroidReadonlySelection: {
focusOffset: number,
};
+export const updateMobilePluginToolbar = (protyle: IProtyle) => {
+ const currentProtyle = getCurrentEditor()?.protyle;
+ if (currentProtyle && currentProtyle !== protyle) {
+ return;
+ }
+ const inlineToolbarElement = document.querySelector<HTMLElement>(
+ '#keyboardToolbar .keyboard__action[data-type="inline-memo"]')?.parentElement;
+ if (!inlineToolbarElement) {
+ return;
+ }
+ inlineToolbarElement.querySelectorAll('[data-plugin-toolbar="true"]').forEach(item => item.remove());
+ getMobilePluginToolbarItems(protyle.options.toolbar, Constants.INLINE_TYPE).forEach(toolbarItem => {
+ const itemElement = document.createElement("button");
+ itemElement.className = "keyboard__action";
+ itemElement.dataset.type = toolbarItem.name;
+ itemElement.dataset.pluginToolbar = "true";
+ itemElement.innerHTML = `<svg><use xlink:href="#${toolbarItem.icon}"></use></svg>`;
+ const label = toolbarItem.tip || (toolbarItem.lang ? window.siyuan.languages[toolbarItem.lang] : "");
+ if (label) {
+ itemElement.setAttribute("aria-label", label);
+ }
+ inlineToolbarElement.append(itemElement);
+ });
+};
+
const preserveAndroidReadonlySelection = () => {
if (!isInAndroid() || restoringAndroidReadonlySelection) {
return false;
diff --git a/app/src/mobile/util/pluginToolbar.test.ts b/app/src/mobile/util/pluginToolbar.test.ts
new file mode 100644
index 0000000000..231f0cad7c
--- /dev/null
+++ b/app/src/mobile/util/pluginToolbar.test.ts
@@ -0,0 +1,35 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getMobilePluginToolbarItems} from "./pluginToolbar";
+
+describe("getMobilePluginToolbarItems", () => {
+ const builtinTypes = ["strong", "em", "inline-memo"];
+
+ it("keeps unique plugin items in their final order", () => {
+ const toolbar: Array<string | IMenuItem> = [
+ "strong",
+ {name: "inline-memo", icon: "iconM"},
+ {name: "plugin-a", icon: "iconA"},
+ {name: "|"},
+ {name: "plugin-a", icon: "iconAChanged"},
+ {name: "plugin-b", icon: "iconB"},
+ ];
+
+ assert.deepEqual(getMobilePluginToolbarItems(toolbar, builtinTypes), [
+ {name: "plugin-a", icon: "iconA"},
+ {name: "plugin-b", icon: "iconB"},
+ ]);
+ });
+
+ it("reflects removed and reordered plugin items", () => {
+ const toolbar: Array<string | IMenuItem> = [
+ {name: "plugin-b", icon: "iconB"},
+ "em",
+ {name: "plugin-a", icon: "iconANew"},
+ ];
+
+ const pluginItems = getMobilePluginToolbarItems(toolbar, builtinTypes);
+ assert.deepEqual(pluginItems.map(item => item.name), ["plugin-b", "plugin-a"]);
+ assert.equal(pluginItems[1].icon, "iconANew");
+ });
+});
diff --git a/app/src/mobile/util/pluginToolbar.ts b/app/src/mobile/util/pluginToolbar.ts
new file mode 100644
index 0000000000..cde41f45b1
--- /dev/null
+++ b/app/src/mobile/util/pluginToolbar.ts
@@ -0,0 +1,12 @@
+export const getMobilePluginToolbarItems = (toolbar: Array<string | IMenuItem>, builtinTypes: string[]) => {
+ const builtinToolbarTypes = new Set(builtinTypes.concat("|"));
+ const names = new Set<string>();
+ return toolbar.filter((toolbarItem): toolbarItem is IMenuItem => {
+ if (typeof toolbarItem === "string" || !toolbarItem.name || builtinToolbarTypes.has(toolbarItem.name) ||
+ names.has(toolbarItem.name)) {
+ return false;
+ }
+ names.add(toolbarItem.name);
+ return true;
+ });
+};
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index f6a9c804e9..85d69237d3 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -55,7 +55,7 @@ import {confirmDialog} from "../../dialog/confirmDialog";
import {paste, pasteAsPlainText, pasteEscaped} from "../util/paste";
import {escapeHtml} from "../../util/escape";
import {resizeSide} from "../../history/resizeSide";
-import {activeBlur} from "../../mobile/util/keyboardToolbar";
+import {activeBlur, updateMobilePluginToolbar} from "../../mobile/util/keyboardToolbar";
const filterPluginToolbar = (toolbar: Array<string | IMenuItem>, lite: boolean) => {
if (!lite) {
@@ -104,7 +104,6 @@ export class Toolbar {
this.subElement.className = "protyle-util fn__none";
/// #endif
this.toolbarHeight = 29;
- const inlineToolbarElement = document.querySelector('#keyboardToolbar .keyboard__action[data-type="inline-memo"]')?.parentElement;
protyle.app.plugins.forEach(item => {
const pluginToolbar = filterPluginToolbar(item.updateProtyleToolbar(options.toolbar), protyle.lite);
pluginToolbar.forEach(toolbarItem => {
@@ -117,17 +116,6 @@ export class Toolbar {
if (window.siyuan.config.keymap.plugin && window.siyuan.config.keymap.plugin[item.name] && window.siyuan.config.keymap.plugin[item.name][toolbarItem.name]) {
toolbarItem.hotkey = window.siyuan.config.keymap.plugin[item.name][toolbarItem.name].custom;
}
- /// #if MOBILE
- if (inlineToolbarElement) {
- const itemElement = new ToolbarItem(protyle, toolbarItem).element;
- itemElement.className = "keyboard__action";
- const oldItemElement = inlineToolbarElement.querySelector(`[data-type="${toolbarItem.name}"]`);
- if (!oldItemElement) {
- inlineToolbarElement.insertAdjacentHTML("beforeend",
- `<button class="keyboard__action" data-type="${toolbarItem.name}"><svg><use xlink:href="#${toolbarItem.icon}"></use></svg></button>`);
- }
- }
- /// #endif
});
options.toolbar = toolbarKeyToMenu(pluginToolbar);
});
@@ -135,6 +123,9 @@ export class Toolbar {
const itemElement = this.genItem(protyle, menuItem);
this.element.appendChild(itemElement);
});
+ /// #if MOBILE
+ updateMobilePluginToolbar(protyle);
+ /// #endif
}
public update(protyle: IProtyle) {
@@ -192,6 +183,9 @@ export class Toolbar {
const itemElement = this.genItem(protyle, menuItem);
this.element.appendChild(itemElement);
});
+ /// #if MOBILE
+ updateMobilePluginToolbar(protyle);
+ /// #endif
}
public setSelectionElementPosition(protyle: IProtyle, element: HTMLElement, triggerRect?: DOMRect) {