Skip to content

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

:art: Support cross-document undo https://github.com/siyuan-note/siyuan/issues/4866

Daniel664758b1880b
diff --git a/app/src/protyle/undo/globalUndo.ts b/app/src/protyle/undo/globalUndo.ts
index 7e6d72e4b2..2e5986040c 100644
--- a/app/src/protyle/undo/globalUndo.ts
+++ b/app/src/protyle/undo/globalUndo.ts
@@ -122,12 +122,12 @@ const resolveRootNames = async (rootIDs: string[]): Promise<string[]> => {
const focusRootIDs = (rootIDs: string[], focusBlockId?: string) => {
// 只滚动发起窗口的焦点 protyle 到变更块;其它文档不强制重开(撤销物理结果在发起文档)
const protyle = getActiveProtyle();
- if (protyle && rootIDs.includes(protyle.block?.rootID)) {
- // 优先滚动到指定的变更块;未指定时滚到文档首块(兜底)
- const targetId = focusBlockId || protyle.wysiwyg.element.querySelector("[data-node-id]")?.getAttribute("data-node-id");
- if (targetId) {
- const target = protyle.wysiwyg.element.querySelector(`[data-node-id="${targetId}"]`);
- if (target) {
+ if (protyle && rootIDs.includes(protyle.block?.rootID) && focusBlockId) {
+ const target = protyle.wysiwyg.element.querySelector(`[data-node-id="${focusBlockId}"]`);
+ if (target) {
+ const rect = target.getBoundingClientRect();
+ // 仅在变更块不在视口内时才滚动,避免打断用户当前的滚动位置
+ if (rect.bottom < 0 || rect.top > window.innerHeight) {
target.scrollIntoView({behavior: "smooth", block: "center"});
}
}
@@ -221,7 +221,7 @@ export const requestUndo = async (protyle: IProtyle) => {
// 单文档撤销:发起窗口本地乐观应用 doOperations(kernel 实际执行的操作,如 insert 恢复块)
protyle.undo.renderLocal(protyle, data.doOperations, false);
refreshUndoButtons(protyle);
- const focusBlockId = data.doOperations?.find((op: IOperation) => op.action === "insert")?.id;
+ const focusBlockId = data.doOperations?.find((op: IOperation) => op.id)?.id;
focusRootIDs(mutatedRootIDs, focusBlockId);
}
});
@@ -273,7 +273,7 @@ export const requestRedo = async (protyle: IProtyle) => {
} else {
protyle.undo.renderLocal(protyle, data.doOperations, true);
refreshUndoButtons(protyle);
- const focusBlockId = data.doOperations?.find((op: IOperation) => op.action === "insert")?.id;
+ const focusBlockId = data.doOperations?.find((op: IOperation) => op.id)?.id;
focusRootIDs(mutatedRootIDs, focusBlockId);
}
});
diff --git a/app/src/protyle/undo/index.ts b/app/src/protyle/undo/index.ts
index 899acfdffe..c01aa2aa43 100644
--- a/app/src/protyle/undo/index.ts
+++ b/app/src/protyle/undo/index.ts
@@ -2,7 +2,6 @@ import {onTransaction} from "../wysiwyg/transaction";
import {preventScroll} from "../scroll/preventScroll";
import {Constants} from "../../constants";
import {hideElements} from "../ui/hideElements";
-import {scrollCenter} from "../../util/highlightById";
import {matchHotKey} from "../util/hotKey";
import {ipcRenderer} from "electron";
import {markMirror, refreshUndoButtons, requestRedo, requestUndo} from "./globalUndo";
@@ -45,7 +44,6 @@ export class Undo {
onTransaction(protyle, operations, true);
document.querySelector(".av__panel")?.remove();
preventScroll(protyle);
- scrollCenter(protyle);
}
// add 降级为:不压栈(kernel 已在 commit 后 Record),仅置位本地镜像 + 刷新按钮态。
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 88c1cc073b..fad48dc61b 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -388,8 +388,12 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
if (operation.action === "unfoldHeading") {
protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {
item.removeAttribute("fold");
- // undo 会走 transaction
if (isUndo) {
+ // kernel 权威撤销:retData 已由 doUnfoldHeading 填充,需要插入子块 HTML 恢复折叠的内容
+ if (operation.retData) {
+ removeUnfoldRepeatBlock(operation.retData, protyle);
+ item.insertAdjacentHTML("afterend", operation.retData);
+ }
return;
}
const embedElement = isInEmbedBlock(item);
@@ -398,7 +402,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
blockRender(protyle, embedElement);
return;
}
- if (operation.retData) { // undo 的时候没有 retData
+ if (operation.retData) {
removeUnfoldRepeatBlock(operation.retData, protyle);
item.insertAdjacentHTML("afterend", operation.retData);
}