Skip to content

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

:bug: Refine cursor restoration after undoing block insertion https://github.com/siyuan-note/siyuan/issues/18137

Daniel478742746dfe
diff --git a/app/src/protyle/undo/index.ts b/app/src/protyle/undo/index.ts
index db7ffc911f..f74ce833b0 100644
--- a/app/src/protyle/undo/index.ts
+++ b/app/src/protyle/undo/index.ts
@@ -27,6 +27,12 @@ interface IOperations {
undoOperations: IOperation[];
}
+const syncToolbarRange = (protyle: IProtyle) => {
+ if (getSelection().rangeCount > 0) {
+ protyle.toolbar.range = getSelection().getRangeAt(0);
+ }
+};
+
// 撤销权威栈已下沉到 kernel(GlobalUndoLog),前端按 rootID 共享。
// 本类仅保留发起窗口本地乐观应用的渲染逻辑(renderLocal,走 isUndo=true 分支,
// 保住光标恢复/折叠/zoom 兜底),以及按钮态刷新。
@@ -70,9 +76,7 @@ export class Undo implements IUndo {
preventScroll(protyle);
// 同步 toolbar range,避免 undo/redo 替换 DOM 后 range 变为 detached,
// 导致后续异步操作(如 F3 创建子文档)读到无效 range 而报错 https://github.com/siyuan-note/siyuan/issues/17896
- if (getSelection().rangeCount > 0) {
- protyle.toolbar.range = getSelection().getRangeAt(0);
- }
+ syncToolbarRange(protyle);
}
// add 降级为:不压栈(kernel 已在 commit 后 Record),仅置位本地镜像 + 刷新按钮态。
@@ -176,6 +180,7 @@ export class LocalUndo implements IUndo {
onTransaction(protyle, state.doOperations, true);
transaction(protyle, state.doOperations, undefined, {skipSync: true});
}
+ syncToolbarRange(protyle);
document.querySelector(".av__panel")?.remove();
preventScroll(protyle);
scrollCenter(protyle);
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 4003fb9075..57f8b9b682 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -377,9 +377,17 @@ export const getUndoFocusContext = (editorElement: Element, range?: Range): Reco
if (!blockElement || !blockElement.contains(range.endContainer)) {
return undefined;
}
- const position = getSelectionOffset(blockElement, undefined, range);
+ const editableElement = getContenteditableElement(blockElement) || blockElement;
+ if (!editableElement.contains(range.startContainer) || !editableElement.contains(range.endContainer)) {
+ return undefined;
+ }
+ const blockElements = Array.from(editorElement.querySelectorAll(
+ `[data-node-id="${blockElement.getAttribute("data-node-id")}"]`
+ ));
+ const position = getSelectionOffset(editableElement, undefined, range);
return {
undoFocusId: blockElement.getAttribute("data-node-id"),
+ undoFocusIndex: blockElements.indexOf(blockElement).toString(),
undoFocusStart: position.start.toString(),
undoFocusEnd: position.end.toString(),
};
@@ -399,7 +407,9 @@ export const restoreUndoFocus = (protyle: IProtyle, operations: IOperation[]) =>
const blockElements = Array.from(protyle.wysiwyg.element.querySelectorAll(
`[data-node-id="${operation.context.undoFocusId}"]`
));
- const blockElement = blockElements.find(item => !isInEmbedBlock(item, false)) || blockElements[0];
+ const index = Number(operation.context.undoFocusIndex);
+ const indexedElement = Number.isInteger(index) && index >= 0 ? blockElements[index] : undefined;
+ const blockElement = indexedElement || blockElements.find(item => !isInEmbedBlock(item, false)) || blockElements[0];
if (!blockElement) {
return false;
}