Skip to content

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

:bug: Prevent cursor jumps after undoing cross-block paste https://github.com/siyuan-note/siyuan/issues/18420

Danielc1406a324925
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index eafa85f57b..7eeab676ff 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -873,8 +873,17 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
const blockRanges = !range.collapsed ? getBlockRanges(protyle.wysiwyg.element, range) : [];
const isCrossBlockRange = blockRanges.length > 1 && blockRanges[0].blockElement === blockElement;
- const crossBlockUndoFocusContext = isCrossBlockRange ?
- getUndoFocusContext(protyle.wysiwyg.element, range, true) : undefined;
+ let crossBlockUndoFocusContext: Record<string, string> | undefined;
+ if (isCrossBlockRange) {
+ const undoRange = document.createRange();
+ undoRange.setStart(blockRanges[0].range.startContainer, blockRanges[0].range.startOffset);
+ const endRange = blockRanges[blockRanges.length - 1].range;
+ undoRange.setEnd(endRange.endContainer, endRange.endOffset);
+ crossBlockUndoFocusContext = getUndoFocusContext(protyle.wysiwyg.element, undoRange, true);
+ if (crossBlockUndoFocusContext) {
+ crossBlockUndoFocusContext.undoFocusCollapseToEnd = "true";
+ }
+ }
const removeElements: HTMLElement[] = [];
if (isCrossBlockRange) {
const startElement = blockRanges[0].blockElement;
@@ -949,13 +958,14 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
});
});
}
+ const crossBlockOldHTML = isCrossBlockRange ? blockElement.outerHTML : undefined;
let id = blockElement.getAttribute("data-node-id");
const rangeStartWbrElement = document.createElement("wbr");
range.insertNode(rangeStartWbrElement);
if (isCrossBlockRange) {
range.setStartAfter(rangeStartWbrElement);
}
- let oldHTML = blockElement.outerHTML;
+ let oldHTML = crossBlockOldHTML ?? blockElement.outerHTML;
const type = blockElement.getAttribute("data-type");
const isNodeCodeBlock = type === "NodeCodeBlock";
const editableElement = getContenteditableElement(blockElement);
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 090217b933..ada27c2f91 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -536,6 +536,9 @@ export const restoreUndoFocus = (protyle: IProtyle, operations: IOperation[]) =>
return false;
}
const ignoreZWSP = operation.context.undoFocusIgnoreZWSP === "true";
+ if (operation.context.undoFocusCollapseToEnd === "true") {
+ return !!focusByOffset(endBlockElement, end, end, true, ignoreZWSP);
+ }
if (startBlockElement === endBlockElement) {
return !!focusByOffset(startBlockElement, start, end, true, ignoreZWSP);
}