An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Restore cursor position after undoing block insertion https://github.com/siyuan-note/siyuan/issues/18137
Daniel0918d1a721bf
diff --git a/app/src/block/util.ts b/app/src/block/util.tsindex 1cac18f694..bb0e0ace3d 100644--- a/app/src/block/util.ts+++ b/app/src/block/util.ts@@ -1,4 +1,4 @@-import {focusByWbr, getEditorRange} from "../protyle/util/selection";+import {focusByWbr, getEditorRange, getUndoFocusContext} from "../protyle/util/selection";import {hasClosestBlock, hasClosestByClassName} from "../protyle/util/hasClosest";import {getContenteditableElement,@@ -235,6 +235,7 @@ export const insertEmptyBlock = async (protyle: IProtyle, position: InsertPositiif (!blockElement) {return;}+ const undoFocusContext = getUndoFocusContext(protyle.wysiwyg.element, range);protyle.observerLoad?.disconnect();let newElement = genEmptyElement(false, true);let orderIndex = 1;@@ -258,7 +259,7 @@ export const insertEmptyBlock = async (protyle: IProtyle, position: InsertPositiif (blockElement.getAttribute("data-type") === "NodeListItem" && blockElement.getAttribute("data-subtype") === "o" &&!newElement.parentElement.classList.contains("protyle-wysiwyg")) {updateListOrder(newElement.parentElement, orderIndex);- updateTransaction(protyle, newElement.parentElement, parentOldHTML);+ updateTransaction(protyle, newElement.parentElement, parentOldHTML, undoFocusContext);} else {let doOperations: IOperation[];if (position === "beforebegin") {@@ -279,6 +280,7 @@ export const insertEmptyBlock = async (protyle: IProtyle, position: InsertPositiconst undoOperations: IOperation[] = [{action: "delete",id: newId,+ context: undoFocusContext,}];if (blockElement.parentElement.classList.contains("sb") &&blockElement.parentElement.getAttribute("data-sb-layout") === "col") {diff --git a/app/src/protyle/undo/index.ts b/app/src/protyle/undo/index.tsindex ee910d3b1b..db7ffc911f 100644--- a/app/src/protyle/undo/index.ts+++ b/app/src/protyle/undo/index.ts@@ -3,6 +3,7 @@ import {preventScroll} from "../scroll/preventScroll";import {Constants} from "../../constants";import {hideElements} from "../ui/hideElements";import {matchHotKey} from "../util/hotKey";+import {restoreUndoFocus} from "../util/selection";import {ipcRenderer} from "electron";import {markMirror, refreshUndoButtons, requestRedo, requestUndo} from "./globalUndo";import {scrollCenter} from "../../util/highlightById";@@ -62,6 +63,9 @@ export class Undo implements IUndo {}}onTransaction(protyle, operations, true);+ if (restoreUndoFocus(protyle, operations)) {+ scrollCenter(protyle);+ }document.querySelector(".av__panel")?.remove();preventScroll(protyle);// 同步 toolbar range,避免 undo/redo 替换 DOM 后 range 变为 detached,@@ -157,6 +161,7 @@ export class LocalUndo implements IUndo {}onTransaction(protyle, state.undoOperations, true);transaction(protyle, state.undoOperations, undefined, {skipSync: true});+ restoreUndoFocus(protyle, state.undoOperations);} else {for (let i = state.doOperations.length - 1; i >= 0; i--) {if (state.doOperations[i].action === "insert") {diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.tsindex 652d46bd26..4003fb9075 100644--- a/app/src/protyle/util/selection.ts+++ b/app/src/protyle/util/selection.ts@@ -6,7 +6,7 @@ import {isContainerBlock,isNotEditBlock} from "../wysiwyg/getBlock";-import {hasClosestBlock, hasClosestByAttribute, hasClosestByTag} from "./hasClosest";+import {hasClosestBlock, hasClosestByAttribute, hasClosestByTag, isInEmbedBlock} from "./hasClosest";import {countBlockWord, countSelectWord} from "../../layout/status";import {hideElements} from "../ui/hideElements";import {genRenderFrame} from "../render/util";@@ -368,6 +368,44 @@ export const getSelectionOffset = (selectElement: Node, editorElement?: Element,return position;};+// 记录插入块前的焦点位置,供撤销回放完成后恢复。+export const getUndoFocusContext = (editorElement: Element, range?: Range): Record<string, string> | undefined => {+ if (!range || !editorElement.contains(range.startContainer) || !editorElement.contains(range.endContainer)) {+ return undefined;+ }+ const blockElement = hasClosestBlock(range.startContainer);+ if (!blockElement || !blockElement.contains(range.endContainer)) {+ return undefined;+ }+ const position = getSelectionOffset(blockElement, undefined, range);+ return {+ undoFocusId: blockElement.getAttribute("data-node-id"),+ undoFocusStart: position.start.toString(),+ undoFocusEnd: position.end.toString(),+ };+};++// 在撤销操作全部应用后,根据插入前保存的焦点位置重建选区。+export const restoreUndoFocus = (protyle: IProtyle, operations: IOperation[]) => {+ const operation = operations.find(item => item.context?.undoFocusId);+ if (!operation) {+ return false;+ }+ const start = Number(operation.context.undoFocusStart);+ const end = Number(operation.context.undoFocusEnd);+ if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end < 0) {+ return false;+ }+ const blockElements = Array.from(protyle.wysiwyg.element.querySelectorAll(+ `[data-node-id="${operation.context.undoFocusId}"]`+ ));+ const blockElement = blockElements.find(item => !isInEmbedBlock(item, false)) || blockElements[0];+ if (!blockElement) {+ return false;+ }+ return !!focusByOffset(blockElement, start, end);+};+const searchNode = (container: Node,startNode: Node,@@ -827,4 +865,3 @@ export const focusSideBlock = (updateElement: Element) => {}focusByRange(range);};-diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex ae4a673dc2..1e88964434 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -1704,7 +1704,7 @@ const processFold = (operation: IOperation, protyle: IProtyle) => {}};-export const updateTransaction = (protyle: IProtyle, element: Element, oldHTML: string) => {+export const updateTransaction = (protyle: IProtyle, element: Element, oldHTML: string, undoContext?: Record<string, string>) => {const id = element.getAttribute("data-node-id");const newHTML = element.outerHTML;if (newHTML === oldHTML.replace("<wbr>", "")) {@@ -1718,7 +1718,8 @@ export const updateTransaction = (protyle: IProtyle, element: Element, oldHTML:}], [{id,data: oldHTML,- action: "update"+ action: "update",+ context: undoContext,}]);};