Skip to content

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

:bug: Sync outline highlight when editing rich text in table cells https://github.com/siyuan-note/siyuan/issues/19188

Danielee332bd32ee1
diff --git a/app/src/protyle/lite/fragmentEditor.ts b/app/src/protyle/lite/fragmentEditor.ts
index 0f64eacf4c..baa853d959 100644
--- a/app/src/protyle/lite/fragmentEditor.ts
+++ b/app/src/protyle/lite/fragmentEditor.ts
@@ -6,8 +6,9 @@ import type {ProtyleRuntimeCapabilities} from "../runtimeCapabilities";
import {removeLoading} from "../ui/initUI";
import {focusBlock} from "../util/selection";
import {invalidateTrackedRanges} from "../util/trackedRange";
+import {PROTYLE_LITE_FRAGMENT_CLASS} from "../util/liteFragment";
-export const PROTYLE_LITE_FRAGMENT_CLASS = "protyle-lite-fragment";
+export {PROTYLE_LITE_FRAGMENT_CLASS};
export const PROTYLE_LITE_HINT_OVERLAY_CLASS = "protyle-hint--lite-overlay";
export interface ProtyleLiteFragmentOptions {
diff --git a/app/src/protyle/render/av/richTextEditor.ts b/app/src/protyle/render/av/richTextEditor.ts
index 99d0ba8c9d..3e6088a756 100644
--- a/app/src/protyle/render/av/richTextEditor.ts
+++ b/app/src/protyle/render/av/richTextEditor.ts
@@ -10,6 +10,7 @@ import {positionAVRichTextEditor} from "./richTextEditorPosition";
import {getAVData} from "./virtualScroll";
import {resolveAVSelectedCell} from "./selectionState";
import {beginAVEditorSession} from "./editorSession";
+import {updateOutlineCurrentBlock} from "../../util/outlineBlock";
import {
configureAVRichTextLute,
createAVRichTextValue,
@@ -247,5 +248,7 @@ export const openAVRichTextEditor = (options: AVRichTextEditorOptions) => {
}
}, true);
fragment.focus(true);
+ // 浮层编辑器自行接管焦点,编辑区收不到块级点击,需在此按所属数据库块同步大纲高亮
+ updateOutlineCurrentBlock(options.protyle, options.nodeElement);
callMobileAppShowKeyboard();
};
diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.ts
index 1e7e24d10c..47d0ee12d4 100644
--- a/app/src/protyle/render/tableCellRichEditor.ts
+++ b/app/src/protyle/render/tableCellRichEditor.ts
@@ -22,6 +22,7 @@ import {matchHotKey} from "../util/hotKey";
import {bindTableCellRichDrag} from "../util/tableCellRichDrag";
import {getTableCellEditorLute} from "../util/tableCellRichLute";
import {setTableCellRichContext} from "../util/tableCellRichContext";
+import {updateOutlineCurrentBlock} from "../util/outlineBlock";
let activeEditor: {cell: Element, finish: () => void} | undefined;
@@ -179,6 +180,8 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
onChange: () => {
contentChanged = true;
updateTableCellContentLayout(host, fragment.getBlockHTML());
+ // 单元格内容变化可能改变其所属块,需同步大纲高亮
+ updateOutlineCurrentBlock(owner, cell);
if (!finished && !composing) {
commit();
}
@@ -262,6 +265,8 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
if (fragment.wysiwyg.contains(event.target as Node)) {
hideElements(["toolbar"], fragment.protyle);
}
+ // 单元格编辑器自行接管焦点,编辑区收不到块级点击,需在此同步大纲高亮
+ updateOutlineCurrentBlock(owner, cell);
}, {capture: true, signal});
host.addEventListener("mousedown", event => {
if (event.button !== 0 || event.ctrlKey || event.metaKey || event.altKey || event.shiftKey ||
@@ -431,6 +436,8 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
}, {capture: true, signal});
observer.observe(owner.element, {childList: true, subtree: true});
fragment.focus(true);
+ // 进入单元格编辑即按所属表格块同步大纲高亮
+ updateOutlineCurrentBlock(owner, cell);
if (restoredSelection && restoreRichCellSelection(fragment.wysiwyg, restoredSelection)) {
undoSelection = restoredSelection;
return;
diff --git a/app/src/protyle/util/liteFragment.test.ts b/app/src/protyle/util/liteFragment.test.ts
new file mode 100644
index 0000000000..33ad6cfb92
--- /dev/null
+++ b/app/src/protyle/util/liteFragment.test.ts
@@ -0,0 +1,53 @@
+import * as assert from "node:assert/strict";
+import {describe, it} from "node:test";
+import {getLiteFragmentHost} from "./liteFragment";
+
+interface ITestElement {
+ className: string;
+ parentElement: ITestElement | null;
+ classList: { contains: (className: string) => boolean };
+}
+
+// 模拟 DOM:非片段内容向上查找时以 null 结束
+const createElement = (className: string, parentElement: ITestElement | null = null): ITestElement => {
+ const element: ITestElement = {
+ className,
+ parentElement,
+ classList: {
+ contains: (name: string) => element.className.split(" ").includes(name),
+ },
+ };
+ return element;
+};
+
+describe("getLiteFragmentHost", () => {
+ it("resolves the fragment host from content inside a table cell editor", () => {
+ const documentWysiwyg = createElement("protyle-wysiwyg");
+ const tableBlock = createElement("table", documentWysiwyg);
+ const cell = createElement("", tableBlock);
+ const host = createElement("table__cell-editor protyle-lite-fragment", cell);
+ const fragmentWysiwyg = createElement("protyle-wysiwyg", host);
+ const paragraph = createElement("p", fragmentWysiwyg);
+
+ assert.equal(getLiteFragmentHost(paragraph as unknown as HTMLElement), host);
+ assert.equal(getLiteFragmentHost(fragmentWysiwyg as unknown as HTMLElement), host);
+ assert.equal(getLiteFragmentHost(host as unknown as HTMLElement), host);
+ });
+
+ it("keeps the outermost fragment host when fragments are nested", () => {
+ const outerHost = createElement("table__cell-editor protyle-lite-fragment");
+ const outerWysiwyg = createElement("protyle-wysiwyg", outerHost);
+ const innerHost = createElement("av__cell-editor protyle-lite-fragment", outerWysiwyg);
+ const innerWysiwyg = createElement("protyle-wysiwyg", innerHost);
+ const paragraph = createElement("p", innerWysiwyg);
+
+ assert.equal(getLiteFragmentHost(paragraph as unknown as HTMLElement), outerHost);
+ });
+
+ it("returns undefined for content that is not inside a fragment", () => {
+ const wysiwyg = createElement("protyle-wysiwyg");
+ const heading = createElement("h2", wysiwyg);
+
+ assert.equal(getLiteFragmentHost(heading as unknown as HTMLElement), undefined);
+ });
+});
diff --git a/app/src/protyle/util/liteFragment.ts b/app/src/protyle/util/liteFragment.ts
new file mode 100644
index 0000000000..dc037d8822
--- /dev/null
+++ b/app/src/protyle/util/liteFragment.ts
@@ -0,0 +1,18 @@
+// 片段编辑器宿主的类名,供轻量工具与片段编辑器共用,避免工具模块依赖编辑器实现
+export const PROTYLE_LITE_FRAGMENT_CLASS = "protyle-lite-fragment";
+
+/**
+ * 片段编辑器(表格单元格、数据库单元格等)内的块由前端生成临时块 ID,不对应任何文档块。
+ * 返回最外层的片段宿主元素,便于继续向上解析所属文档块。
+ */
+export const getLiteFragmentHost = (element: HTMLElement) => {
+ let hostElement: HTMLElement;
+ let currentElement: HTMLElement = element;
+ while (currentElement) {
+ if (currentElement.classList.contains(PROTYLE_LITE_FRAGMENT_CLASS)) {
+ hostElement = currentElement;
+ }
+ currentElement = currentElement.parentElement;
+ }
+ return hostElement;
+};
diff --git a/app/src/protyle/util/outlineBlock.ts b/app/src/protyle/util/outlineBlock.ts
new file mode 100644
index 0000000000..5da1d719ed
--- /dev/null
+++ b/app/src/protyle/util/outlineBlock.ts
@@ -0,0 +1,36 @@
+import {getAllModels} from "../../layout/getAll";
+import {getLiteFragmentHost} from "./liteFragment";
+import {hasClosestBlock} from "./hasClosest";
+
+/**
+ * 解析元素所属的文档块。片段编辑器(表格单元格、数据库单元格等)内的块由前端生成临时块 ID,
+ * 不对应任何文档块,需越过片段宿主向上解析,否则大纲等按块 ID 工作的功能会取到无效 ID。
+ */
+export const resolveDocumentBlockElement = (element: HTMLElement) => {
+ if (!element) {
+ return undefined;
+ }
+ const blockElement = hasClosestBlock(element);
+ if (blockElement) {
+ return blockElement as HTMLElement;
+ }
+ const fragmentElement = getLiteFragmentHost(element);
+ const ownerElement = fragmentElement && hasClosestBlock(fragmentElement);
+ return ownerElement ? ownerElement as HTMLElement : undefined;
+};
+
+/**
+ * 光标进入表格单元格等内容后同步大纲高亮。片段编辑器自行接管焦点,不会触发编辑区的块级点击处理,
+ * 需显式按所属文档块更新大纲,参数应传片段宿主或单元格等片段外元素。
+ */
+export const updateOutlineCurrentBlock = (protyle: IProtyle, element: HTMLElement) => {
+ const blockElement = resolveDocumentBlockElement(element);
+ if (!blockElement || !protyle.model) {
+ return;
+ }
+ getAllModels().outline.forEach(item => {
+ if (item.blockId === protyle.block.rootID) {
+ item.setCurrent(blockElement);
+ }
+ });
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index bda11a70c9..81dce7acc7 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -36,6 +36,7 @@ import {
} from "../util/selection";
import {Constants} from "../../constants";
import {mergeTableCellContents} from "../util/tableCellRich";
+import {resolveDocumentBlockElement} from "../util/outlineBlock";
import {isMobile} from "../../util/functions";
import {previewDocImage} from "../preview/image";
import {getDiagramBlock, previewDiagram} from "../preview/diagram";
@@ -626,7 +627,7 @@ export class WYSIWYG {
private setEmptyOutline(protyle: IProtyle, element: HTMLElement) {
let nodeElement = element;
if (!element.getAttribute("data-node-id")) {
- const tempElement = hasClosestBlock(element);
+ const tempElement = resolveDocumentBlockElement(element);
if (!tempElement) {
return;
}