Skip to content

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

:bug: Prevent detached list updates in backlink contexts https://github.com/siyuan-note/siyuan/issues/18558

Daniel6d0cea2d0586
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index ee0afadae2..b9a40602a0 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -38,6 +38,7 @@ import {
queueHeadingNumberRefresh
} from "../util/headingNumber";
import {MERMAID_LAYOUT_ATTR} from "../render/mermaidLayout";
+import {getPartialUpdateCleanupElements} from "./transactionUpdate";
const removeTopElement = (updateElement: Element, protyle: IProtyle) => {
// 移动到其他文档中,该块需移除
@@ -556,6 +557,10 @@ const deleteBlock = (updateElements: Element[], id: string, protyle: IProtyle, i
const updateBlock = (updateElements: Element[], protyle: IProtyle, operation: IOperation, isUndo: boolean) => {
const range = getSelection().rangeCount > 0 ? getSelection().getRangeAt(0) : null;
updateElements.forEach(item => {
+ // 前序局部回放可能已替换包含该块的祖先,跳过失效引用。
+ if (!item.parentElement) {
+ return;
+ }
let isRangeBlock = false;
if (range && item.contains(range.startContainer)) {
isRangeBlock = true;
@@ -734,10 +739,15 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
operation.data = newTempElement.outerHTML;
operation.id = newUpdateId;
// https://github.com/siyuan-note/siyuan/issues/14326#issuecomment-2746140335
- for (let i = 1; i < protyle.wysiwyg.element.childElementCount; i++) {
- protyle.wysiwyg.element.childNodes[i].remove();
- i--;
- }
+ // 普通缩放编辑器只保留局部更新块;反向链接包含面包屑和其他上下文,不能清理根节点。
+ const rootElements = Array.from(protyle.wysiwyg.element.children);
+ const updateRootElement = rootElements.find(item =>
+ item === newUpdateElement || item.contains(newUpdateElement));
+ getPartialUpdateCleanupElements(
+ rootElements,
+ updateRootElement,
+ Boolean(protyle.options.backlinkData),
+ ).forEach(item => item.remove());
}
}
}
diff --git a/app/src/protyle/wysiwyg/transactionUpdate.test.ts b/app/src/protyle/wysiwyg/transactionUpdate.test.ts
new file mode 100644
index 0000000000..644fa90892
--- /dev/null
+++ b/app/src/protyle/wysiwyg/transactionUpdate.test.ts
@@ -0,0 +1,22 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getPartialUpdateCleanupElements} from "./transactionUpdate";
+
+describe("getPartialUpdateCleanupElements", () => {
+ const breadcrumb = {id: "breadcrumb"};
+ const updateRoot = {id: "update-root"};
+ const sibling = {id: "sibling"};
+ const rootElements = [breadcrumb, updateRoot, sibling];
+
+ it("preserves backlink breadcrumbs and sibling contexts", () => {
+ assert.deepEqual(getPartialUpdateCleanupElements(rootElements, updateRoot, true), []);
+ });
+
+ it("keeps only the actual partial-update root in a zoomed editor", () => {
+ assert.deepEqual(getPartialUpdateCleanupElements(rootElements, updateRoot, false), [breadcrumb, sibling]);
+ });
+
+ it("does not clean up roots when the update root cannot be resolved", () => {
+ assert.deepEqual(getPartialUpdateCleanupElements(rootElements, undefined, false), []);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/transactionUpdate.ts b/app/src/protyle/wysiwyg/transactionUpdate.ts
new file mode 100644
index 0000000000..16f78f613a
--- /dev/null
+++ b/app/src/protyle/wysiwyg/transactionUpdate.ts
@@ -0,0 +1,10 @@
+export const getPartialUpdateCleanupElements = <T>(
+ rootElements: T[],
+ updateRootElement: T | undefined,
+ isBacklink: boolean,
+) => {
+ if (isBacklink || !updateRootElement) {
+ return [];
+ }
+ return rootElements.filter(item => item !== updateRootElement);
+};