Skip to content

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

:bug: Preserve the code block cursor position after redo https://github.com/siyuan-note/siyuan/issues/18717

Danieldba57b72c36e
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 8a98a316bd..2777637a65 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -1021,7 +1021,7 @@ export const setInsertWbrHTML = (nodeElement: HTMLElement, range: Range, protyle
}
};
-export const focusByWbr = (element: Element, range: Range) => {
+export const focusByWbr = (element: Element, range: Range, preserveWbr = false) => {
const wbrElements = element.querySelectorAll("wbr");
if (wbrElements.length === 0) {
return;
@@ -1073,7 +1073,9 @@ export const focusByWbr = (element: Element, range: Range) => {
}
}
range.collapse(true);
- wbrElement.remove();
+ if (!preserveWbr) {
+ wbrElement.remove();
+ }
focusByRange(range);
return range;
};
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 441ed1af89..84a3ce3e53 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -45,7 +45,7 @@ import {
queueHeadingNumberRefresh
} from "../util/headingNumber";
import {MERMAID_LAYOUT_ATTR} from "../render/mermaidLayout";
-import {getPartialUpdateCleanupElements} from "./transactionUpdate";
+import {getPartialUpdateCleanupElements, shouldDeferCodeBlockCaretRestore} from "./transactionUpdate";
import {getMoveAffectedEmbedElements, shouldSyncMoveCopies} from "./transactionEmbed";
const removeTopElement = (updateElement: Element, protyle: IProtyle) => {
@@ -609,14 +609,25 @@ const updateBlock = (updateElements: Element[], protyle: IProtyle, operation: IO
item.previousElementSibling.remove();
const wbrElement = item.querySelector("wbr");
+ const codeElement = item.getAttribute("data-type") === "NodeCodeBlock" ? item.querySelector(".hljs") : undefined;
+ // 未高亮的代码块由 highlightRender 使用 wbr 记录偏移,并在重建 DOM 后恢复光标。
+ const deferCodeBlockCaretRestore = shouldDeferCodeBlockCaretRestore({
+ isRangeBlock,
+ isReplay: isUndo,
+ hasCaret: !!wbrElement,
+ isCodeBlock: !!codeElement,
+ isRendered: codeElement?.getAttribute("data-render") === "true",
+ });
if (isRangeBlock && isUndo) {
if (wbrElement) {
- focusByWbr(item, range);
+ focusByWbr(item, range, deferCodeBlockCaretRestore);
} else {
focusBlock(item);
}
}
- wbrElement?.remove();
+ if (!deferCodeBlockCaretRestore) {
+ wbrElement?.remove();
+ }
// update 操作会生成新表格并替换旧节点,聚焦后需还原滚动,避免表格跳回开头
if (tableScrollLeft > 0) {
(item.firstElementChild as HTMLElement).scrollLeft = tableScrollLeft;
diff --git a/app/src/protyle/wysiwyg/transactionUpdate.test.ts b/app/src/protyle/wysiwyg/transactionUpdate.test.ts
index 644fa90892..4692f0e6d9 100644
--- a/app/src/protyle/wysiwyg/transactionUpdate.test.ts
+++ b/app/src/protyle/wysiwyg/transactionUpdate.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getPartialUpdateCleanupElements} from "./transactionUpdate";
+import {getPartialUpdateCleanupElements, shouldDeferCodeBlockCaretRestore} from "./transactionUpdate";
describe("getPartialUpdateCleanupElements", () => {
const breadcrumb = {id: "breadcrumb"};
@@ -20,3 +20,29 @@ describe("getPartialUpdateCleanupElements", () => {
assert.deepEqual(getPartialUpdateCleanupElements(rootElements, undefined, false), []);
});
});
+
+describe("shouldDeferCodeBlockCaretRestore", () => {
+ const replayCodeBlock = {
+ isRangeBlock: true,
+ isReplay: true,
+ hasCaret: true,
+ isCodeBlock: true,
+ isRendered: false,
+ };
+
+ it("defers the caret until an active replayed code block is highlighted", () => {
+ assert.equal(shouldDeferCodeBlockCaretRestore(replayCodeBlock), true);
+ });
+
+ it("keeps the synchronous restore path for an already highlighted undo snapshot", () => {
+ assert.equal(shouldDeferCodeBlockCaretRestore({...replayCodeBlock, isRendered: true}), false);
+ });
+
+ it("does not let an inactive code block steal focus", () => {
+ assert.equal(shouldDeferCodeBlockCaretRestore({...replayCodeBlock, isRangeBlock: false}), false);
+ });
+
+ it("does not defer caret restoration for ordinary blocks", () => {
+ assert.equal(shouldDeferCodeBlockCaretRestore({...replayCodeBlock, isCodeBlock: false}), false);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/transactionUpdate.ts b/app/src/protyle/wysiwyg/transactionUpdate.ts
index 16f78f613a..e2edb37541 100644
--- a/app/src/protyle/wysiwyg/transactionUpdate.ts
+++ b/app/src/protyle/wysiwyg/transactionUpdate.ts
@@ -8,3 +8,11 @@ export const getPartialUpdateCleanupElements = <T>(
}
return rootElements.filter(item => item !== updateRootElement);
};
+
+export const shouldDeferCodeBlockCaretRestore = (options: {
+ isRangeBlock: boolean,
+ isReplay: boolean,
+ hasCaret: boolean,
+ isCodeBlock: boolean,
+ isRendered: boolean,
+}) => options.isRangeBlock && options.isReplay && options.hasCaret && options.isCodeBlock && !options.isRendered;