Skip to content

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

:bug: Restore the caret after undoing forward deletion https://github.com/siyuan-note/siyuan/issues/11248

Danield6378e3ecfac
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 11194c9531..11b63c1e03 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -3824,6 +3824,8 @@ export class WYSIWYG {
let beforeInputCompositionHandled = false;
// 原生软换行在 input 触发前已经修改 DOM,需预存选区供撤销恢复。
let lineBreakUndoContext: Record<string, string>;
+ // 原生向前删除在 input 触发前已经修改 DOM,需预存光标供撤销恢复。
+ let forwardDeleteUndoContext: Record<string, string>;
// 仅矫正从数据库外进入的占位光标,避免重置数据库内部的方向键导航。
let arrowStartElement: false | HTMLElement | undefined;
// Android 软键盘在空块中会先发送 Unidentified 并清空选区,需在实际回车或退格事件前恢复。
@@ -4122,6 +4124,9 @@ export class WYSIWYG {
lineBreakUndoContext = !event.defaultPrevented && event.inputType === "insertLineBreak" &&
selection.rangeCount > 0 ?
getUndoFocusContext(protyle.wysiwyg.element, selection.getRangeAt(0)) : undefined;
+ forwardDeleteUndoContext = !event.defaultPrevented && event.inputType === "deleteContentForward" &&
+ selection.rangeCount > 0 && selection.getRangeAt(0).collapsed ?
+ getUndoFocusContext(protyle.wysiwyg.element, selection.getRangeAt(0), true) : undefined;
if (event.defaultPrevented || !isTextInputType(event.inputType, event.data) ||
selection.rangeCount === 0) {
return;
@@ -4148,15 +4153,18 @@ export class WYSIWYG {
});
this.element.addEventListener("input", (event: InputEvent) => {
- let lineBreakInputOperations: Parameters<typeof input>[5];
- if (event.inputType === "insertLineBreak" && lineBreakUndoContext) {
- lineBreakInputOperations = {
+ const undoContext = event.inputType === "insertLineBreak" ? lineBreakUndoContext :
+ event.inputType === "deleteContentForward" ? forwardDeleteUndoContext : undefined;
+ let inputOperations: Parameters<typeof input>[5];
+ if (undoContext) {
+ inputOperations = {
doOperations: [],
undoOperations: [],
- undoContext: lineBreakUndoContext,
+ undoContext,
};
}
lineBreakUndoContext = undefined;
+ forwardDeleteUndoContext = undefined;
if (getAVTemplateInteractiveElement(event.target)) {
event.stopPropagation();
return;
@@ -4204,7 +4212,7 @@ export class WYSIWYG {
Constants.TIMEOUT_INPUT, false);
} else {
this.scheduleInput(() => input(protyle, blockElement, range, true, event,
- lineBreakInputOperations));
+ inputOperations));
}
}
event.stopPropagation();
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index afd33cd42f..1846c67a1f 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -1381,6 +1381,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
// 段末反向删除 https://github.com/siyuan-note/insider/issues/274
if (isEndOfBlock(range) || editElement.textContent.substring(position.start) === "\n") {
const cloneRange = range.cloneRange();
+ const undoFocusContext = getUndoFocusContext(protyle.wysiwyg.element, cloneRange, true);
const nextElement = getNextBlock(getTopAloneElement(nodeElement));
if (nextElement) {
const nextRange = focusBlock(nextElement);
@@ -1399,7 +1400,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
event.preventDefault();
forwardBlockRemovalPending = true;
try {
- await removeBlock(protyle, nextBlockElement, nextRange, "Delete");
+ await removeBlock(protyle, nextBlockElement, nextRange, "Delete",
+ false, false, undoFocusContext);
} finally {
forwardBlockRemovalPending = false;
if (caretElement.isConnected) {
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index 94d31d1141..b4d2dce852 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -995,7 +995,8 @@ export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Ra
export const removeBlock = async (protyle: IProtyle, blockElement: Element, range: Range,
type: "Delete" | "Backspace" | "remove", skipRefCheck = false,
- restoreSelectionModeAfterZoom = false) => {
+ restoreSelectionModeAfterZoom = false,
+ undoFocusContext?: Record<string, string>) => {
const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
if (selectElements?.length > 0) {
const selectedTopElement = selectElements.length === 1 ? getTopAloneElement(selectElements[0]) : undefined;
@@ -1773,6 +1774,9 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
id: previousId,
});
}
+ if (undoFocusContext && undoOperations[0]) {
+ undoOperations[0].context = undoFocusContext;
+ }
if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && getSbChildBlockCount(parentElement) === 1) {
const sbData = await cancelSB(protyle, parentElement);
transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations));