Skip to content

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

:recycle: Reuse the mobile editing toolbar for lite editors https://github.com/siyuan-note/siyuan/issues/19326

Daniela6d0a4aa692f
diff --git a/app/src/assets/scss/protyle/_toolbar.scss b/app/src/assets/scss/protyle/_toolbar.scss
index d1984d2f4c..62d799a49d 100644
--- a/app/src/assets/scss/protyle/_toolbar.scss
+++ b/app/src/assets/scss/protyle/_toolbar.scss
@@ -8,23 +8,6 @@
border-radius: var(--b3-border-radius);
display: flex;
- &--mobile {
- flex-wrap: nowrap;
- overflow-x: auto;
- overflow-y: hidden;
- border-radius: 0;
- box-sizing: border-box;
-
- > * {
- flex-shrink: 0;
- }
-
- .protyle-toolbar__item {
- height: 48px;
- min-width: 40px;
- }
- }
-
&__item {
display: inline-flex;
align-items: center;
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index af49800db3..702ba6db53 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -8,7 +8,9 @@ import {
import {moveToDown, moveToUp} from "../../protyle/wysiwyg/move";
import {Constants} from "../../constants";
import {focusBlock, focusByRange, getSelectionPosition} from "../../protyle/util/selection";
-import {getCurrentEditor} from "../editor";
+import {getCurrentEditor as getDocumentEditor} from "../editor";
+import {getMobileToolbarProtyle, getMobileToolbarUndo} from "../../protyle/lite/mobileToolbar";
+import {LocalUndo} from "../../protyle/undo";
import {convertFontSize, fontEvent, getFontNodeElements, getFontSizeInfo} from "../../protyle/toolbar/Font";
import {hideElements} from "../../protyle/ui/hideElements";
import {softEnter} from "../../protyle/wysiwyg/enter";
@@ -79,6 +81,11 @@ import {getEntryOrder, isEntryVisible} from "../../config/entryVisibility/runtim
import {TOOLBAR_ENTRY_ROOT_PATH} from "../../protyle/toolbar/defaults";
import {getKeyboardPanelHeight} from "./keyboardPanelHeight";
+const getCurrentEditor = () => getMobileToolbarProtyle()?.getInstance() || getDocumentEditor();
+const getKeyboardPaddingElement = (protyle: IProtyle) =>
+ protyle.lite ? protyle.contentElement : protyle.element.parentElement;
+let toolbarProtyle: IProtyle;
+
const applyKeyboardToolbarEntries = (element: HTMLElement, toolbar: Array<string | IMenuItem>) => {
applyMobileToolbarEntries(element, toolbar, {
order: getEntryOrder(TOOLBAR_ENTRY_ROOT_PATH),
@@ -416,7 +423,7 @@ const updateKeyboardPanelHeight = () => {
toolbarElement.style.height = panelHeight;
const editor = getCurrentEditor();
if (editor) {
- editor.protyle.element.parentElement.style.paddingBottom = panelHeight;
+ getKeyboardPaddingElement(editor.protyle).style.paddingBottom = panelHeight;
}
};
@@ -684,6 +691,23 @@ export const renderTextMenu = (protyle: IProtyle, toolbarElement: Element) => {
const renderSlashMenu = (protyle: IProtyle, toolbarElement: Element) => {
protyle.hint.splitChar = "/";
protyle.hint.lastIndex = -1;
+ if (protyle.lite) {
+ // 轻量编辑器沿用自己的插入候选,保留单元格内容限制和智能体技能入口。
+ const items = protyle.options.hint.extend.find(item => item.key === "/")?.hint?.("", protyle, "hint") || [];
+ protyle.hint.genHTML(items, protyle, true, "hint");
+ const utilElement = toolbarElement.querySelector(".keyboard__util");
+ utilElement.replaceChildren();
+ if (items.length > 0) {
+ Array.from(protyle.hint.element.children).forEach((element: HTMLElement, index) => {
+ element.classList.add("keyboard__slash-item");
+ element.setAttribute("data-focus", items[index]?.focus === false ? "false" : "true");
+ utilElement.appendChild(element);
+ });
+ }
+ protyle.hint.element.classList.add("fn__none");
+ protyle.hint.bindUploadEvent(protyle, utilElement as HTMLElement);
+ return;
+ }
let pluginHTML = "";
protyle.app.plugins.forEach((plugin) => {
plugin.protyleSlash.forEach(slash => {
@@ -841,7 +865,7 @@ const hideKeyboardToolbarUtil = (restoreKeyboard = false) => {
updateKeyboardToolbarPosition();
const editor = getCurrentEditor();
if (editor) {
- editor.protyle.element.parentElement.style.paddingBottom = "48px";
+ getKeyboardPaddingElement(editor.protyle).style.paddingBottom = "48px";
}
resetKeyboardToolbarUtilButtons();
};
@@ -954,15 +978,19 @@ const renderKeyboardToolbar = () => {
protyle.toolbar.range = range;
if (!dynamicElements[0].classList.contains("fn__none")) {
// 撤销权威栈在 kernel,本地按 rootID 读镜像设按钮态,首次进入嵌入源文档时按需初始化。
- const undoRootID = getUndoRootID(protyle, range);
+ const undoProtyle = getMobileToolbarUndo(protyle)?.owner || protyle;
+ const undoRootID = undoProtyle.lite ? undefined : getUndoRootID(undoProtyle, range);
if (undoRootID && !hasUndoStateMirror(undoRootID)) {
initMirror(undoRootID).then((initialized) => {
- if (initialized && getUndoRootID(protyle, protyle.toolbar.range) === undoRootID) {
+ if (initialized && getCurrentEditor()?.protyle === protyle) {
renderKeyboardToolbar();
}
});
}
- const undoState = undoRootID ? getMirror(undoRootID) : {
+ const undoState = undoProtyle.undo instanceof LocalUndo ? {
+ canUndo: undoProtyle.undo.undoStack.length > 0,
+ canRedo: undoProtyle.undo.redoStack.length > 0,
+ } : undoRootID ? getMirror(undoRootID) : {
canUndo: false,
canRedo: false
};
@@ -1041,13 +1069,23 @@ const showKeyboardToolbarElement = () => {
const selection = getSelection();
// 空块恢复焦点时 Selection 可能暂时为空,但原生键盘已经显示,仍需隐藏普通底栏。
notifyMobileKeyboardChange(true);
- if (selection.rangeCount > 0 && (
- hasClosestByClassName(selection.getRangeAt(0).startContainer, "protyle-lite-fragment", true) ||
- hasClosestByClassName(selection.getRangeAt(0).startContainer, "agent-chat__composer-host", true))) {
- // Lite 编辑器使用自己的工具栏,不能显示会作用于下层文档的移动端编辑工具栏。
+ const protyle = getCurrentEditor()?.protyle;
+ if (!protyle || (selection.rangeCount > 0 && (
+ hasClosestByClassName(selection.getRangeAt(0).startContainer, "protyle-wysiwyg", true) !== protyle.wysiwyg.element ||
+ hasClosestByClassName(selection.getRangeAt(0).endContainer, "protyle-wysiwyg", true) !== protyle.wysiwyg.element))) {
toolbarElement.classList.add("fn__none");
return;
}
+ if (toolbarProtyle !== protyle) {
+ keyboardPanelClosing = false;
+ hideKeyboardToolbarUtil();
+ if (toolbarProtyle) {
+ getKeyboardPaddingElement(toolbarProtyle).style.paddingBottom = "";
+ }
+ toolbarProtyle = protyle;
+ updateMobilePluginToolbar(protyle);
+ }
+ toolbarElement.querySelector('[data-type="block"]').classList.toggle("fn__none", !protyle.gutter);
if (!toolbarElement.classList.contains("fn__none")) {
return;
}
@@ -1065,7 +1103,7 @@ const showKeyboardToolbarElement = () => {
const editor = getCurrentEditor();
if (editor) {
if (editor.protyle.wysiwyg.element.contains(range.startContainer)) {
- editor.protyle.element.parentElement.style.paddingBottom = "48px";
+ getKeyboardPaddingElement(editor.protyle).style.paddingBottom = "48px";
}
forEachPluginSubscriber("mobile-keyboard-show", eventBus => {
eventBus.emit("mobile-keyboard-show");
@@ -1183,7 +1221,7 @@ export const hideKeyboardToolbar = () => {
toolbarElement.style.height = "";
const editor = getCurrentEditor();
if (editor) {
- editor.protyle.element.parentElement.style.paddingBottom = "";
+ getKeyboardPaddingElement(editor.protyle).style.paddingBottom = "";
if (!toolbarHidden) {
forEachPluginSubscriber("mobile-keyboard-hide", eventBus => {
eventBus.emit("mobile-keyboard-hide");
@@ -1251,6 +1289,18 @@ export const activeBlur = (force = false) => {
};
export const initKeyboardToolbar = () => {
+ window.addEventListener("siyuan-mobile-toolbar-editor", (event: CustomEvent<IProtyle>) => {
+ if (event.detail) {
+ getKeyboardPaddingElement(event.detail).style.paddingBottom = "";
+ if (toolbarProtyle === event.detail) {
+ toolbarProtyle = undefined;
+ }
+ }
+ keyboardPanelClosing = false;
+ hideKeyboardToolbarUtil();
+ hideKeyboardToolbar();
+ renderKeyboardToolbar();
+ });
let composing = false;
window.addEventListener("siyuan-mobile-keyboard-hiding", () => {
// 键盘退场前先隐藏工具栏,焦点和选区由原生端在动画结束后清理。
@@ -1597,10 +1647,22 @@ export const initKeyboardToolbar = () => {
return;
}
if (type === "undo") {
- protyle.undo.undo(protyle);
+ const undoContext = getMobileToolbarUndo(protyle);
+ if (undoContext) {
+ undoContext.run(false);
+ } else {
+ protyle.undo.undo(protyle);
+ }
+ renderKeyboardToolbar();
return;
} else if (type === "redo") {
- protyle.undo.redo(protyle);
+ const undoContext = getMobileToolbarUndo(protyle);
+ if (undoContext) {
+ undoContext.run(true);
+ } else {
+ protyle.undo.redo(protyle);
+ }
+ renderKeyboardToolbar();
return;
}
if (type !== "block" && getSelection().rangeCount === 0) {
@@ -1751,7 +1813,7 @@ export const initKeyboardToolbar = () => {
protyle.toolbar.range = range;
keyboardPanelClosing = false;
hideKeyboardToolbarUtil();
- protyle.gutter.renderMenu(protyle, nodeElement);
+ protyle.gutter?.renderMenu(protyle, nodeElement);
window.siyuan.menus.menu.fullscreen("all", restoreMenuKeyboard);
return;
} else if (type === "outdent") {
diff --git a/app/src/mobile/util/toolbarEntries.test.ts b/app/src/mobile/util/toolbarEntries.test.ts
index 558c6f2cee..dc1363e35e 100644
--- a/app/src/mobile/util/toolbarEntries.test.ts
+++ b/app/src/mobile/util/toolbarEntries.test.ts
@@ -26,6 +26,20 @@ class ToolbarElement {
}
}
+test("shared toolbar restores document entries after editing a restricted fragment", () => {
+ const root = new ToolbarElement();
+ const back = new ToolbarElement("goback");
+ const bold = new ToolbarElement("strong");
+ const tag = new ToolbarElement("tag");
+ const separator = new ToolbarElement(undefined, "separator_1");
+ root.children = [back, bold, separator, tag];
+ const options = {order: ["strong", "separator_1", "tag"], isVisible: () => true};
+ applyMobileToolbarEntries(root as unknown as HTMLElement, ["strong"], options);
+ assert.deepEqual(root.children.filter(item => !item.classList.contains("fn__none")), [back, bold]);
+ applyMobileToolbarEntries(root as unknown as HTMLElement, getDefaultToolbar(true), options);
+ assert.deepEqual(root.children.filter(item => !item.classList.contains("fn__none")), [back, bold, separator, tag]);
+});
+
test("mobile toolbar keeps navigation reachable and removes empty separators", () => {
const root = new ToolbarElement();
const back = new ToolbarElement("goback");
diff --git a/app/src/mobile/util/toolbarEntries.ts b/app/src/mobile/util/toolbarEntries.ts
index 1fcdd01b51..63b7b248d8 100644
--- a/app/src/mobile/util/toolbarEntries.ts
+++ b/app/src/mobile/util/toolbarEntries.ts
@@ -6,6 +6,7 @@ export const applyMobileToolbarEntries = (element: HTMLElement, toolbar: Array<s
}) => {
const items = new Map(toolbar.filter((item): item is IMenuItem => typeof item !== "string")
.map(item => [item.name, item]));
+ const available = new Set(toolbar.map(item => typeof item === "string" ? item : item.name));
const children = Array.from(element.children).filter((child: HTMLElement) => child.dataset.type !== "goback") as HTMLElement[];
children.forEach(child => {
const item = items.get(child.dataset.type);
@@ -17,7 +18,8 @@ export const applyMobileToolbarEntries = (element: HTMLElement, toolbar: Array<s
const result = resolveToolbarItems(children, {
getKey: item => item.dataset.id,
isSeparator: item => item.classList.contains("keyboard__split"),
- isVisible: options.isVisible,
+ isVisible: key => options.isVisible(key) && children.some(item => item.dataset.id === key &&
+ (item.classList.contains("keyboard__split") || available.has(item.dataset.type))),
order: options.order,
});
const visible = new Set(result.visible);
diff --git a/app/src/protyle/lite/mobileToolbar.test.ts b/app/src/protyle/lite/mobileToolbar.test.ts
index b355adc48d..018aeea0df 100644
--- a/app/src/protyle/lite/mobileToolbar.test.ts
+++ b/app/src/protyle/lite/mobileToolbar.test.ts
@@ -1,110 +1,63 @@
import * as assert from "node:assert/strict";
import {test} from "node:test";
-import {bindMobileToolbar} from "./mobileToolbar";
+import {bindMobileToolbar, getMobileToolbarProtyle, getMobileToolbarUndo, setMobileToolbarUndo} from "./mobileToolbar";
-test("mobile fragment toolbar follows its own selection and cleans up pending rendering", () => {
+test("shared mobile toolbar follows fragment focus, retains panel ownership and releases destroyed editors", () => {
const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
const originalDocument = Object.getOwnPropertyDescriptor(globalThis, "document");
const events = new EventTarget();
- const viewport = Object.assign(new EventTarget(), {offsetTop: 0, offsetLeft: 0, width: 390, height: 500});
- const frames = new Map<number, FrameRequestCallback>();
- let frameID = 0;
- const root = {};
- const ownNode = {nodeType: 1, closest: () => root};
- const foreignNode = {nodeType: 1, closest: () => ({})};
- let range = {startContainer: ownNode, endContainer: ownNode};
- let collapsed = false;
- let panelFocused = false;
- let renders = 0;
- const classes = new Set<string>();
- const element = Object.assign(new EventTarget(), {
- classList: classes,
- style: {top: "", left: "", width: ""},
- offsetHeight: 48,
- remove: () => {},
- setAttribute: () => {},
- });
- const protyle = {
- wysiwyg: {element: root},
- toolbar: {
- element,
- subElement: {contains: () => panelFocused},
- render: (owner: unknown, selectedRange: unknown) => {
- assert.equal(owner, protyle);
- assert.equal(selectedRange, range);
- renders++;
- classes.delete("fn__none");
- },
- },
- } as unknown as IProtyle;
- Object.defineProperty(globalThis, "document", {configurable: true, value: Object.assign(events, {
- body: {appendChild: () => {}},
- })});
- Object.defineProperty(globalThis, "window", {configurable: true, value: Object.assign(new EventTarget(), {
- visualViewport: viewport,
- siyuan: {zIndex: 10},
- requestAnimationFrame: (callback: FrameRequestCallback) => {
- frames.set(++frameID, callback);
- return frameID;
- },
- cancelAnimationFrame: (id: number) => frames.delete(id),
- getSelection: () => ({rangeCount: 1, isCollapsed: collapsed, getRangeAt: () => range}),
- })});
- const flush = () => {
- const callbacks = Array.from(frames.values());
- frames.clear();
- callbacks.forEach(callback => callback(0));
+ const state = {activeElement: {closest: (): unknown => null}};
+ const createEditor = () => {
+ const element = Object.assign(new EventTarget(), {
+ isConnected: true,
+ closest: (selector: string): unknown => selector === ".protyle-wysiwyg" ? element : null,
+ });
+ return {element, wysiwyg: {element}} as unknown as IProtyle;
};
- let cleanup: () => void;
+ const composer = createEditor();
+ const cell = createEditor();
+ const changes: IProtyle[] = [];
+ events.addEventListener("siyuan-mobile-toolbar-editor", (event: CustomEvent<IProtyle>) => changes.push(event.detail));
+ Object.defineProperty(globalThis, "window", {configurable: true, value: events});
+ Object.defineProperty(globalThis, "document", {configurable: true, value: state});
+ const cleanups: Array<() => void> = [];
try {
- cleanup = bindMobileToolbar(protyle);
- events.dispatchEvent(new Event("selectionchange"));
- events.dispatchEvent(new Event("selectionchange"));
- assert.equal(frames.size, 1);
- flush();
- assert.equal(renders, 1);
- assert.equal(element.style.top, "452px");
- assert.equal(element.style.width, "390px");
- viewport.height = 320;
- viewport.offsetTop = 20;
- viewport.dispatchEvent(new Event("resize"));
- assert.equal(element.style.top, "292px");
- viewport.offsetLeft = 12;
- viewport.dispatchEvent(new Event("scroll"));
- assert.equal(element.style.left, "12px");
+ cleanups.push(bindMobileToolbar(composer), bindMobileToolbar(cell));
+ const owner = createEditor();
+ const operations: boolean[] = [];
+ setMobileToolbarUndo(cell, owner, redo => operations.push(redo));
+ assert.equal(getMobileToolbarUndo(composer), undefined);
+ assert.equal(getMobileToolbarUndo(cell).owner, owner);
+ getMobileToolbarUndo(cell).run(false);
+ getMobileToolbarUndo(cell).run(true);
+ assert.deepEqual(operations, [false, true]);
+ state.activeElement = {closest: () => composer.wysiwyg.element};
+ composer.wysiwyg.element.dispatchEvent(new Event("focusin"));
+ assert.equal(getMobileToolbarProtyle(), composer);
- range = {startContainer: ownNode, endContainer: foreignNode};
- events.dispatchEvent(new Event("selectionchange"));
- flush();
- assert.equal(renders, 1);
- assert.ok(classes.has("fn__none"));
+ state.activeElement = {closest: () => null};
+ assert.equal(getMobileToolbarProtyle(), composer);
- range = {startContainer: ownNode, endContainer: ownNode};
- collapsed = true;
- events.dispatchEvent(new Event("selectionchange"));
- flush();
- assert.equal(renders, 1);
+ state.activeElement = {closest: () => cell.wysiwyg.element};
+ cell.wysiwyg.element.dispatchEvent(new Event("focusin"));
+ assert.equal(getMobileToolbarProtyle(), cell);
+ assert.equal(changes.at(-1), composer);
- panelFocused = true;
- classes.delete("fn__none");
- events.dispatchEvent(new Event("selectionchange"));
- flush();
- assert.ok(!classes.has("fn__none"));
+ state.activeElement = {closest: () => ({})};
+ assert.equal(getMobileToolbarProtyle(), undefined);
- const pointer = new Event("mousedown", {cancelable: true});
- element.dispatchEvent(pointer);
- assert.ok(pointer.defaultPrevented);
+ state.activeElement = {closest: () => cell.wysiwyg.element};
+ cell.wysiwyg.element.dispatchEvent(new Event("focusin"));
+ cleanups.pop()();
+ assert.equal(getMobileToolbarProtyle(), undefined);
+ assert.equal(changes.at(-1), cell);
- events.dispatchEvent(new Event("selectionchange"));
- cleanup();
- assert.equal(frames.size, 0);
- viewport.height = 600;
- viewport.dispatchEvent(new Event("resize"));
- assert.equal(element.style.top, "292px");
- events.dispatchEvent(new Event("selectionchange"));
- assert.equal(frames.size, 0);
+ state.activeElement = {closest: () => composer.wysiwyg.element};
+ composer.wysiwyg.element.dispatchEvent(new Event("focusin"));
+ Object.defineProperty(composer.element, "isConnected", {value: false});
+ assert.equal(getMobileToolbarProtyle(), undefined);
} finally {
- cleanup?.();
+ cleanups.forEach(cleanup => cleanup());
if (originalWindow) {
Object.defineProperty(globalThis, "window", originalWindow);
} else {
diff --git a/app/src/protyle/lite/mobileToolbar.ts b/app/src/protyle/lite/mobileToolbar.ts
index 3c80c2de37..e863c5b866 100644
--- a/app/src/protyle/lite/mobileToolbar.ts
+++ b/app/src/protyle/lite/mobileToolbar.ts
@@ -1,56 +1,45 @@
+const editors = new Map<Element, IProtyle>();
+let activeEditor: IProtyle;
+const undoContexts = new WeakMap<IProtyle, {owner: IProtyle, run: (redo: boolean) => void}>();
+
+export const setMobileToolbarUndo = (protyle: IProtyle, owner: IProtyle, run: (redo: boolean) => void) => {
+ undoContexts.set(protyle, {owner, run});
+};
+
+export const getMobileToolbarUndo = (protyle: IProtyle) => undoContexts.get(protyle);
+
+export const getMobileToolbarProtyle = () => {
+ const root = document.activeElement?.closest(".protyle-wysiwyg");
+ if (root) {
+ activeEditor = editors.get(root);
+ }
+ if (activeEditor && (!activeEditor.element.isConnected || activeEditor.element.closest("[inert]"))) {
+ activeEditor = undefined;
+ }
+ return activeEditor;
+};
+
export const bindMobileToolbar = (protyle: IProtyle) => {
- let frame: number | undefined;
- const toolbar = protyle.toolbar;
- const viewport = window.visualViewport;
- const position = () => {
- const top = viewport?.offsetTop || 0;
- toolbar.element.style.left = `${viewport?.offsetLeft || 0}px`;
- toolbar.element.style.width = `${viewport?.width || window.innerWidth}px`;
- toolbar.element.style.top = `${Math.max(top, top + (viewport?.height || window.innerHeight) - toolbar.element.offsetHeight)}px`;
- };
- const render = () => {
- if (frame !== undefined) {
+ const element = protyle.wysiwyg.element;
+ editors.set(element, protyle);
+ const activate = (event: Event) => {
+ if ((event.target as Element).closest(".protyle-wysiwyg") !== element || activeEditor === protyle) {
return;
}
- frame = window.requestAnimationFrame(() => {
- frame = undefined;
- if (toolbar.subElement.contains(document.activeElement)) {
- return;
- }
- const selection = window.getSelection();
- const range = selection?.rangeCount > 0 ? selection.getRangeAt(0) : undefined;
- const belongsToEditor = (node: Node) =>
- (node.nodeType === 1 ? node as Element : node.parentElement)?.closest(".protyle-wysiwyg") ===
- protyle.wysiwyg.element;
- if (!range || selection.isCollapsed || !belongsToEditor(range.startContainer) ||
- !belongsToEditor(range.endContainer)) {
- toolbar.element.classList.add("fn__none");
- return;
- }
- toolbar.render(protyle, range);
- position();
- toolbar.element.style.zIndex = (++window.siyuan.zIndex).toString();
- });
+ const previous = activeEditor;
+ activeEditor = protyle;
+ // 共享工具栏切换编辑上下文,保留各编辑器自己的选区和撤销栈。
+ window.dispatchEvent(new CustomEvent("siyuan-mobile-toolbar-editor", {detail: previous}));
};
- const preventBlur = (event: MouseEvent) => event.preventDefault();
- // 工具栏固定在键盘上方,避开原生选区菜单,并脱离宿主的变换和裁剪边界。
- toolbar.element.setAttribute("data-position-boundary", "viewport");
- toolbar.element.classList.add("protyle-toolbar--mobile");
- document.body.appendChild(toolbar.element);
- toolbar.element.addEventListener("mousedown", preventBlur);
- document.addEventListener("selectionchange", render);
- window.addEventListener("resize", position);
- viewport?.addEventListener("resize", position);
- viewport?.addEventListener("scroll", position);
+ element.addEventListener("focusin", activate);
+ element.addEventListener("pointerdown", activate);
return () => {
- window.removeEventListener("resize", position);
- viewport?.removeEventListener("resize", position);
- viewport?.removeEventListener("scroll", position);
- document.removeEventListener("selectionchange", render);
- toolbar.element.removeEventListener("mousedown", preventBlur);
- toolbar.element.remove();
- if (frame !== undefined) {
- window.cancelAnimationFrame(frame);
+ element.removeEventListener("focusin", activate);
+ element.removeEventListener("pointerdown", activate);
+ editors.delete(element);
+ if (activeEditor === protyle) {
+ activeEditor = undefined;
+ window.dispatchEvent(new CustomEvent("siyuan-mobile-toolbar-editor", {detail: protyle}));
}
};
};
diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.ts
index e0a77b26b6..f87494b99b 100644
--- a/app/src/protyle/render/tableCellRichEditor.ts
+++ b/app/src/protyle/render/tableCellRichEditor.ts
@@ -3,6 +3,7 @@ import {showMessage} from "../../dialog/message";
import {hintRef, hintSlash} from "../hint/extend";
import {registerBuiltinSlashHint} from "../hint/builtinSlash";
import {mountProtyleLiteFragment} from "../lite/fragmentEditor";
+import {setMobileToolbarUndo} from "../lite/mobileToolbar";
import {getDefaultToolbar} from "../toolbar/defaults";
import {hideElements} from "../ui/hideElements";
import {updateTransaction} from "../wysiwyg/transaction";
@@ -259,6 +260,24 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
}
};
activeEditor = {cell, finish};
+ const undoCell = (redo: boolean) => {
+ if (composing) {
+ return;
+ }
+ // 先提交当前单元格,再由所属文档撤销,保证切换单元格后仍可连续回退。
+ finish();
+ const range = document.createRange();
+ range.selectNodeContents(cell);
+ range.collapse(true);
+ owner.wysiwyg.element.focus({preventScroll: true});
+ focusByRange(range);
+ if (redo) {
+ owner.undo.redo(owner);
+ } else {
+ owner.undo.undo(owner);
+ }
+ };
+ setMobileToolbarUndo(fragment.protyle, owner, undoCell);
setTableCellRichContext(fragment.protyle, {owner, cell, finish});
const signal = controller.signal;
bindTableCellRichDrag(owner, cell, fragment.wysiwyg, finish, signal,
@@ -309,7 +328,7 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
}, {capture: true, signal});
const belongsToEditor = (target: Node) => host.contains(target) || fragment.hintElement.contains(target) ||
fragment.protyle.toolbar.element.contains(target) || fragment.protyle.toolbar.subElement.contains(target) ||
- !!(target instanceof Element && target.closest("#commonMenu, .b3-dialog"));
+ !!(target instanceof Element && target.closest("#keyboardToolbar, #commonMenu, .b3-dialog"));
document.addEventListener("pointerdown", event => {
if (belongsToEditor(event.target as Node)) {
return;
@@ -377,18 +396,7 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
if (!event.isComposing && !composing && (undo || redo)) {
event.preventDefault();
event.stopImmediatePropagation();
- // 先提交当前单元格,再由所属文档撤销,保证切换单元格后仍可连续回退。
- finish();
- const range = document.createRange();
- range.selectNodeContents(cell);
- range.collapse(true);
- owner.wysiwyg.element.focus({preventScroll: true});
- focusByRange(range);
- if (undo) {
- owner.undo.undo(owner);
- } else {
- owner.undo.redo(owner);
- }
+ undoCell(redo);
return;
}
if (!event.isComposing && !composing && !event.ctrlKey && !event.metaKey && !event.altKey &&
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index d48f72df50..14b3a9eb86 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -285,7 +285,7 @@ export class Toolbar {
const endCellElement = hasClosestByTag(range.endContainer, "TD") ||
hasClosestByTag(range.endContainer, "TH");
const isCrossCell = !!startCellElement && !!endCellElement && startCellElement !== endCellElement;
- if (this.element.hasAttribute("data-entry-empty") || (isMobile() && !protyle.lite) || !nodeElement || protyle.disabled || (!isCrossBlock && (
+ if (this.element.hasAttribute("data-entry-empty") || isMobile() || !nodeElement || protyle.disabled || (!isCrossBlock && (
nodeElement.getAttribute("data-type") === "NodeCodeBlock" ||
nodeElement.classList.contains("av") ||
hasClosestByTag(range.startContainer, "CAPTION")
@@ -719,7 +719,7 @@ export class Toolbar {
return;
}
- const toolbarElement = isMobile() && !protyle.lite ? document.querySelector("#keyboardToolbar .keyboard__dynamic").nextElementSibling : this.element;
+ const toolbarElement = isMobile() ? document.querySelector("#keyboardToolbar .keyboard__dynamic").nextElementSibling : this.element;
const actionBtn = action === "toolbar" ? toolbarElement.querySelector(`[data-type="${type}"]`) : undefined;
const remove = INLINE_FORMAT_TYPES.includes(type) && !textObj ?
getRangesInlineFormats(ranges).includes(type) :
@@ -989,7 +989,7 @@ export class Toolbar {
}
});
}
- const toolbarElement = isMobile() && !protyle.lite ? document.querySelector("#keyboardToolbar .keyboard__dynamic").nextElementSibling : this.element;
+ const toolbarElement = isMobile() ? document.querySelector("#keyboardToolbar .keyboard__dynamic").nextElementSibling : this.element;
const actionBtn = action === "toolbar" ? toolbarElement.querySelector(`[data-type="${type}"]`) : undefined;
const newNodes: Node[] = [];
let startContainer: Node;
diff --git a/app/src/protyle/undo/globalUndo.ts b/app/src/protyle/undo/globalUndo.ts
index 6eacebd7dc..ab1d225ab2 100644
--- a/app/src/protyle/undo/globalUndo.ts
+++ b/app/src/protyle/undo/globalUndo.ts
@@ -12,6 +12,7 @@ import {getActiveTab} from "../../layout/tabUtil";
/// #endif
/// #if MOBILE
import {getCurrentEditor} from "../../mobile/editor";
+import {getMobileToolbarProtyle, getMobileToolbarUndo} from "../lite/mobileToolbar";
/// #endif
// 本地镜像:按 rootID 缓存 {canUndo, canRedo},按钮态零 fetch 读取。
@@ -115,7 +116,10 @@ const applyUndoButtons = (protyle: IProtyle, rootID: string) => {
applyState(protyle.breadcrumb.element.parentElement);
}
/// #if MOBILE
- if (getCurrentEditor()?.protyle === protyle && getUndoRootID(protyle, protyle.toolbar?.range) === rootID) {
+ const toolbarEditor = getMobileToolbarProtyle();
+ const undoOwner = toolbarEditor ? getMobileToolbarUndo(toolbarEditor)?.owner : getCurrentEditor()?.protyle;
+ if (undoOwner === protyle &&
+ getUndoRootID(protyle, protyle.toolbar?.range) === rootID) {
const keyboardToolbar = document.getElementById("keyboardToolbar");
if (keyboardToolbar) {
applyState(keyboardToolbar);