Skip to content

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

:bug: Preserve formula content when switching editor panels https://github.com/siyuan-note/siyuan/issues/18730

Danieleaf8ba35cae4
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index e9050d2cc4..0d911f62e9 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -102,6 +102,7 @@ import {
} from "../wysiwyg/list";
import {applyHeadingLevelUpdates, getHeadingLevelUpdateOperations} from "../util/headingTransform";
import {getBacklinkGutterContentTop, getGutterMarginHeight} from "./layout";
+import {closeSubElement} from "../toolbar/subElementLifecycle";
// 块类型 data-type 到本地化名称键的映射,用于块标提示中的 ${x}
const BLOCK_TYPE_LANG_KEYS: { [key: string]: string } = {
@@ -1372,6 +1373,7 @@ export class Gutter {
this.showMobileAppearance(protyle);
/// #else
protyle.toolbar.element.classList.add("fn__none");
+ closeSubElement(protyle.toolbar);
protyle.toolbar.subElement.innerHTML = "";
protyle.toolbar.subElement.style.width = "";
protyle.toolbar.subElement.style.padding = "";
@@ -1380,7 +1382,6 @@ export class Gutter {
protyle.toolbar.subElement.style.zIndex = (++window.siyuan.zIndex).toString();
protyle.toolbar.subElement.classList.remove("fn__none");
limitRecentFontStyleRows(fontMenuElement);
- protyle.toolbar.subElementCloseCB = undefined;
const position = selectsElement[0].getBoundingClientRect();
setPosition(protyle.toolbar.subElement, position.left, position.top);
/// #endif
@@ -2672,6 +2673,7 @@ export class Gutter {
this.showMobileAppearance(protyle);
/// #else
protyle.toolbar.element.classList.add("fn__none");
+ closeSubElement(protyle.toolbar);
protyle.toolbar.subElement.innerHTML = "";
protyle.toolbar.subElement.style.width = "";
protyle.toolbar.subElement.style.padding = "";
@@ -2680,7 +2682,6 @@ export class Gutter {
protyle.toolbar.subElement.style.zIndex = (++window.siyuan.zIndex).toString();
protyle.toolbar.subElement.classList.remove("fn__none");
limitRecentFontStyleRows(fontMenuElement);
- protyle.toolbar.subElementCloseCB = undefined;
const position = nodeElement.getBoundingClientRect();
setPosition(protyle.toolbar.subElement, position.left, position.top);
/// #endif
diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts
index 5e05480f76..17183df4db 100644
--- a/app/src/protyle/toolbar/Font.ts
+++ b/app/src/protyle/toolbar/Font.ts
@@ -5,6 +5,7 @@ import {Constants} from "../../constants";
import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
import {updateBatchTransaction} from "../wysiwyg/transaction";
import {lineNumberRender} from "../render/highlightRender";
+import {closeSubElement} from "./subElementLifecycle";
const MAX_RECENT_FONT_STYLES = 14;
@@ -40,6 +41,7 @@ export class Font extends ToolbarItem {
const triggerRect = this.element.getBoundingClientRect();
const visibleTriggerRect = triggerRect.width > 0 && triggerRect.height > 0 ? triggerRect : undefined;
protyle.toolbar.element.classList.add("fn__none");
+ closeSubElement(protyle.toolbar);
protyle.toolbar.subElement.innerHTML = "";
protyle.toolbar.subElement.style.width = "";
protyle.toolbar.subElement.style.padding = "";
@@ -48,7 +50,6 @@ export class Font extends ToolbarItem {
protyle.toolbar.subElement.style.zIndex = (++window.siyuan.zIndex).toString();
protyle.toolbar.subElement.classList.remove("fn__none");
limitRecentFontStyleRows(appearanceElement);
- protyle.toolbar.subElementCloseCB = undefined;
focusByRange(protyle.toolbar.range);
/// #if !MOBILE
protyle.toolbar.setSelectionElementPosition(
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index a78ac202f9..3b72193d2d 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -59,6 +59,7 @@ import {activeBlur, updateMobilePluginToolbar} from "../../mobile/util/keyboardT
import {FormatPainter} from "./FormatPainter";
import {IFormatPainterSnapshot} from "./formatPainterCore";
import {clearDisallowedTextInputHotkey} from "../../util/hotKeyPolicy";
+import {closeSubElement} from "./subElementLifecycle";
const filterPluginToolbar = (toolbar: Array<string | IMenuItem>, lite: boolean) => {
if (!lite) {
@@ -1194,6 +1195,13 @@ export class Toolbar {
public showRender(protyle: IProtyle, renderElement: Element, updateElements?: Element[],
oldHTML?: string | Map<string, string>) {
+ if (!protyle.wysiwyg.element.contains(renderElement) || !hasClosestBlock(renderElement)) {
+ return;
+ }
+ this.clearSubElement();
+ if (!protyle.wysiwyg.element.contains(renderElement)) {
+ return;
+ }
const nodeElement = hasClosestBlock(renderElement);
if (!nodeElement) {
return;
@@ -1256,7 +1264,6 @@ export class Toolbar {
} else if (isInlineMemo) {
title = window.siyuan.languages.memo;
}
- this.clearSubElement();
this.subElement.style.padding = "0";
this.subElement.style.display = "flex";
this.subElement.style.flexDirection = "column";
@@ -1645,6 +1652,13 @@ export class Toolbar {
}
public showCodeLanguage(protyle: IProtyle, languageElements: HTMLElement[]) {
+ if (!protyle.wysiwyg.element.contains(languageElements[0]) || !hasClosestBlock(languageElements[0])) {
+ return;
+ }
+ this.clearSubElement();
+ if (!protyle.wysiwyg.element.contains(languageElements[0])) {
+ return;
+ }
const nodeElement = hasClosestBlock(languageElements[0]);
if (!nodeElement) {
return;
@@ -1652,7 +1666,6 @@ export class Toolbar {
hideElements(["hint"], protyle);
window.siyuan.menus.menu.remove();
this.range = getEditorRange(nodeElement);
- this.clearSubElement();
this.subElement.innerHTML = `<div data-id="codeLanguage" class="fn__flex-column" style="max-height:50vh">
<input placeholder="${window.siyuan.languages.searchPlaceholder}" style="margin: 0 8px 4px 8px" class="b3-text-field"/>
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative"></div>
@@ -1786,9 +1799,15 @@ export class Toolbar {
}
public showMultiSelectMode(protyle: IProtyle, blockElement: HTMLElement) {
+ if (!protyle.wysiwyg.element.contains(blockElement)) {
+ return;
+ }
+ this.clearSubElement();
+ if (!protyle.wysiwyg.element.contains(blockElement)) {
+ return;
+ }
blockElement.classList.add("protyle-wysiwyg--select");
window.siyuan.menus.menu.remove();
- this.clearSubElement();
this.subElement.style.width = window.innerWidth - 16 + "px";
this.subElement.style.padding = "0";
this.subElement.innerHTML = `<div class="block__icons">
@@ -1829,10 +1848,16 @@ export class Toolbar {
}
public showTpl(protyle: IProtyle, nodeElement: HTMLElement, range: Range) {
+ if (!protyle.wysiwyg.element.contains(nodeElement)) {
+ return;
+ }
+ this.clearSubElement();
+ if (!protyle.wysiwyg.element.contains(nodeElement)) {
+ return;
+ }
this.range = range;
hideElements(["hint"], protyle);
window.siyuan.menus.menu.remove();
- this.clearSubElement();
this.subElement.innerHTML = `<div style="max-height:50vh" class="fn__flex">
<div class="fn__flex-column" style="${isMobile() ? "width: 100%" : "width: 256px"}">
<div class="fn__flex" style="margin: 0 8px 4px 8px">
@@ -2011,10 +2036,16 @@ export class Toolbar {
}
public showWidget(protyle: IProtyle, nodeElement: HTMLElement, range: Range) {
+ if (!protyle.wysiwyg.element.contains(nodeElement)) {
+ return;
+ }
+ this.clearSubElement();
+ if (!protyle.wysiwyg.element.contains(nodeElement)) {
+ return;
+ }
this.range = range;
hideElements(["hint"], protyle);
window.siyuan.menus.menu.remove();
- this.clearSubElement();
this.subElement.innerHTML = `<div class="fn__flex-column" style="max-height:50vh">
<input style="margin: 0 8px 4px 8px" class="b3-text-field"/>
<div class="b3-list fn__flex-1 b3-list--background" style="position: relative"><img style="margin: 0 auto;display: block;width: 64px;height:64px" src="/stage/loading-pure.svg"></div>
@@ -2088,9 +2119,15 @@ export class Toolbar {
}
public showContent(protyle: IProtyle, range: Range, nodeElement: Element, pluginMenus: IMenu[] = []) {
+ if (!protyle.wysiwyg.element.contains(nodeElement)) {
+ return;
+ }
+ this.clearSubElement();
+ if (!protyle.wysiwyg.element.contains(nodeElement)) {
+ return;
+ }
this.range = range;
hideElements(["hint"], protyle);
- this.clearSubElement();
this.subElement.style.width = "auto";
this.subElement.style.padding = "0 8px";
let html = "";
@@ -2330,8 +2367,7 @@ export class Toolbar {
}
private clearSubElement() {
+ closeSubElement(this);
this.subElement.removeAttribute("style");
- this.subElementCloseCB = null;
- this.subElementResizeCB = null;
}
}
diff --git a/app/src/protyle/toolbar/subElementLifecycle.test.ts b/app/src/protyle/toolbar/subElementLifecycle.test.ts
new file mode 100644
index 0000000000..cec1725a86
--- /dev/null
+++ b/app/src/protyle/toolbar/subElementLifecycle.test.ts
@@ -0,0 +1,63 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {closeSubElement, ISubElementLifecycleState} from "./subElementLifecycle";
+
+describe("closeSubElement", () => {
+ it("runs a pending close callback exactly once", () => {
+ let callCount = 0;
+ const state: ISubElementLifecycleState = {
+ subElementCloseCB: () => {
+ callCount++;
+ },
+ subElementResizeCB: () => undefined,
+ };
+
+ closeSubElement(state);
+ closeSubElement(state);
+
+ assert.equal(callCount, 1);
+ assert.equal(state.subElementCloseCB, undefined);
+ assert.equal(state.subElementResizeCB, undefined);
+ });
+
+ it("clears the callback before invoking it to prevent reentrant commits", () => {
+ let callCount = 0;
+ const state: ISubElementLifecycleState = {};
+ state.subElementCloseCB = () => {
+ callCount++;
+ closeSubElement(state);
+ };
+
+ closeSubElement(state);
+
+ assert.equal(callCount, 1);
+ });
+
+ it("preserves replacement callbacks installed while closing", () => {
+ const replacementClose: () => void = () => undefined;
+ const replacementResize: () => void = () => undefined;
+ const state: ISubElementLifecycleState = {};
+ state.subElementCloseCB = () => {
+ state.subElementCloseCB = replacementClose;
+ state.subElementResizeCB = replacementResize;
+ };
+
+ closeSubElement(state);
+
+ assert.equal(state.subElementCloseCB, replacementClose);
+ assert.equal(state.subElementResizeCB, replacementResize);
+ });
+
+ it("keeps lifecycle state cleared when the close handler throws", () => {
+ const state: ISubElementLifecycleState = {
+ subElementCloseCB: () => {
+ throw new Error("close failed");
+ },
+ subElementResizeCB: () => undefined,
+ };
+
+ assert.throws(() => closeSubElement(state), /close failed/);
+ assert.equal(state.subElementCloseCB, undefined);
+ assert.equal(state.subElementResizeCB, undefined);
+ });
+});
diff --git a/app/src/protyle/toolbar/subElementLifecycle.ts b/app/src/protyle/toolbar/subElementLifecycle.ts
new file mode 100644
index 0000000000..123ff53df6
--- /dev/null
+++ b/app/src/protyle/toolbar/subElementLifecycle.ts
@@ -0,0 +1,11 @@
+export interface ISubElementLifecycleState {
+ subElementCloseCB?: (() => void) | null;
+ subElementResizeCB?: (() => void) | null;
+}
+
+export const closeSubElement = (state: ISubElementLifecycleState) => {
+ const closeCallback = state.subElementCloseCB;
+ state.subElementCloseCB = undefined;
+ state.subElementResizeCB = undefined;
+ closeCallback?.();
+};
diff --git a/app/src/protyle/toolbar/tableCell.ts b/app/src/protyle/toolbar/tableCell.ts
index 06ee2175fb..8ff79804d4 100644
--- a/app/src/protyle/toolbar/tableCell.ts
+++ b/app/src/protyle/toolbar/tableCell.ts
@@ -2,6 +2,7 @@ import {setPosition} from "../../util/setPosition";
import {appearanceMenu, fontEvent, limitRecentFontStyleRows} from "./Font";
import {toolbarKeyToMenu} from "./util";
import {matchHotKey} from "../util/hotKey";
+import {closeSubElement} from "./subElementLifecycle";
const TABLE_CELL_INLINE_TYPES = ["strong", "em", "u", "s", "mark", "sup", "sub", "kbd", "code"];
@@ -22,6 +23,7 @@ export const openTableCellAppearance = (protyle: IProtyle, cellElements: HTMLTab
}
window.siyuan.menus.menu.remove();
protyle.toolbar.element.classList.add("fn__none");
+ closeSubElement(protyle.toolbar);
protyle.toolbar.subElement.innerHTML = "";
protyle.toolbar.subElement.style.width = "";
protyle.toolbar.subElement.style.padding = "";
@@ -34,7 +36,6 @@ export const openTableCellAppearance = (protyle: IProtyle, cellElements: HTMLTab
protyle.toolbar.subElement.style.zIndex = (++window.siyuan.zIndex).toString();
protyle.toolbar.subElement.classList.remove("fn__none");
limitRecentFontStyleRows(appearanceElement);
- protyle.toolbar.subElementCloseCB = undefined;
const rect = cells[0].getBoundingClientRect();
const gap = 4;
const top = rect.bottom + gap;
diff --git a/app/src/protyle/ui/hideElements.ts b/app/src/protyle/ui/hideElements.ts
index 4b658b3ef8..cb7fb2b390 100644
--- a/app/src/protyle/ui/hideElements.ts
+++ b/app/src/protyle/ui/hideElements.ts
@@ -2,6 +2,7 @@ import {getAllEditor} from "../../layout/getAll";
import {hideRectResizeHandles} from "../../asset/rectAnnotationResize";
import {isIPhone} from "../util/compatibility";
import {hideGutterElements} from "./gutterVisibility";
+import {closeSubElement} from "../toolbar/subElementLifecycle";
// "gutter", "toolbar", "select", "hint", "util", "dialog", "gutterOnly"
export const hideElements = (panels: string[], protyle?: IProtyle, focusHide = false) => {
@@ -48,10 +49,7 @@ export const hideElements = (panels: string[], protyle?: IProtyle, focusHide = f
if (!protyle.toolbar.isMultiSelectMode() &&
(focusHide || !pinElement || (pinElement && pinElement.getAttribute("aria-label") === window.siyuan.languages.pin))) {
protyle.toolbar.subElement.classList.add("fn__none");
- if (protyle.toolbar.subElementCloseCB) {
- protyle.toolbar.subElementCloseCB();
- protyle.toolbar.subElementCloseCB = undefined;
- }
+ closeSubElement(protyle.toolbar);
}
}
if (panels.includes("select")) {
@@ -78,10 +76,7 @@ export const hideAllElements = (types: string[]) => {
if (!item.protyle.toolbar.isMultiSelectMode() &&
(!pinElement || (pinElement && pinElement.getAttribute("aria-label") === window.siyuan.languages.pin))) {
item.protyle.toolbar.subElement.classList.add("fn__none");
- if (item.protyle.toolbar.subElementCloseCB) {
- item.protyle.toolbar.subElementCloseCB();
- item.protyle.toolbar.subElementCloseCB = undefined;
- }
+ closeSubElement(item.protyle.toolbar);
}
}
});
diff --git a/app/src/protyle/util/destroy.ts b/app/src/protyle/util/destroy.ts
index 710b165fdf..443bfeac38 100644
--- a/app/src/protyle/util/destroy.ts
+++ b/app/src/protyle/util/destroy.ts
@@ -6,7 +6,7 @@ export const destroy = (protyle: IProtyle) => {
if (!protyle) {
return;
}
- hideElements(["util"], protyle);
+ hideElements(["util"], protyle, true);
destroyAIEditor(protyle);
protyle.hint?.destroy();
protyle.preview?.destroy();