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: Fix undo routing for embedded block edits https://github.com/siyuan-note/siyuan/issues/18423
Danielbe010bd26bd1
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.tsindex e2556c8c1a..c1d4d6f9c3 100644--- a/app/src/mobile/util/keyboardToolbar.ts+++ b/app/src/mobile/util/keyboardToolbar.ts@@ -16,7 +16,7 @@ import {isInAndroid, isInEdge, isInHarmony} from "../../protyle/util/compatibiliimport {tabCodeBlock} from "../../protyle/wysiwyg/codeBlock";import {callMobileAppShowKeyboard, canInput, keyboardLockUntil} from "./mobileAppUtil";import {isNotEditBlock} from "../../protyle/wysiwyg/getBlock";-import {getMirror} from "../../protyle/undo/globalUndo";+import {getMirror, getUndoRootID} from "../../protyle/undo/globalUndo";let renderKeyboardToolbarTimeout: number;let scrollSelectionIntoViewTimeout: number;@@ -365,7 +365,8 @@ const renderKeyboardToolbar = () => {protyle.toolbar.range = range;if (!dynamicElements[0].classList.contains("fn__none")) {// 撤销权威栈在 kernel,本地按 rootID 读镜像设按钮态(零 fetch)- const undoState = protyle.block?.rootID ? getMirror(protyle.block.rootID) : {+ const undoRootID = getUndoRootID(protyle, range);+ const undoState = undoRootID ? getMirror(undoRootID) : {canUndo: false,canRedo: false};diff --git a/app/src/protyle/breadcrumb/index.ts b/app/src/protyle/breadcrumb/index.tsindex 8c3bcf3480..a80db0ecf8 100644--- a/app/src/protyle/breadcrumb/index.ts+++ b/app/src/protyle/breadcrumb/index.ts@@ -35,6 +35,7 @@ import {listIndent, listOutdent} from "../wysiwyg/list";import {improveBreadcrumbAppearance} from "../wysiwyg/renderBacklink";import {getCloudURL} from "../../config/util/about";import {escapeAriaLabel} from "../../util/escape";+import {refreshUndoButtons} from "../undo/globalUndo";export class Breadcrumb {public element: HTMLElement;@@ -906,6 +907,7 @@ ${padHTML}if (protyle.element.getAttribute("disabled-forever") === "true") {return;}+ refreshUndoButtons(protyle);/// #if !MOBILElet range: Range;let blockElement: Element;diff --git a/app/src/protyle/render/blockRender.ts b/app/src/protyle/render/blockRender.tsindex 8d613ca2f7..7b752ccfe9 100644--- a/app/src/protyle/render/blockRender.ts+++ b/app/src/protyle/render/blockRender.ts@@ -113,7 +113,8 @@ const renderEmbed = (blocks: {}}const childOperationAttr = blocksItem.allowChildOperation ? " data-allow-child-operation=\"true\"" : "";- html += `<div class="protyle-wysiwyg__embed" data-id="${blocksItem.block.id}"${childOperationAttr}>+ const rootIDAttr = blocksItem.block.rootID ? ` data-root-id="${blocksItem.block.rootID}"` : "";+ html += `<div class="protyle-wysiwyg__embed" data-id="${blocksItem.block.id}"${rootIDAttr}${childOperationAttr}>${popover}${breadcrumbHTML}${blocksItem.block.content}</div>`;});diff --git a/app/src/protyle/undo/globalUndo.ts b/app/src/protyle/undo/globalUndo.tsindex 740019d5cf..55145a87a2 100644--- a/app/src/protyle/undo/globalUndo.ts+++ b/app/src/protyle/undo/globalUndo.ts@@ -3,6 +3,7 @@ import {fetchPost} from "../../util/fetch";import {confirmDialog} from "../../dialog/confirmDialog";import {showMessage} from "../../dialog/message";import {waitForPendingTransactions} from "../util/transactionQueue";+import {hasClosestByClassName} from "../util/hasClosest";/// #if !MOBILEimport {getActiveTab} from "../../layout/tabUtil";/// #endif@@ -29,6 +30,40 @@ export const getMirror = (rootID: string): IUndoStateMirror => {return undoStateMirror.get(rootID) || {canUndo: false, canRedo: false};};+const getRangeRootID = (protyle: IProtyle, range?: Range) => {+ if (!range?.startContainer?.isConnected || !protyle.wysiwyg.element.contains(range.startContainer)) {+ return;+ }+ const embedElement = hasClosestByClassName(range.startContainer, "protyle-wysiwyg__embed");+ if (embedElement) {+ const rootID = embedElement.getAttribute("data-root-id");+ if (rootID) {+ return rootID;+ }+ }+ return protyle.block?.rootID;+};++// 嵌入块是源文档在当前编辑器中的投影,撤销目标应由具体查询结果决定,外层查询块不绑定单一文档。+export const getUndoRootID = (protyle: IProtyle, range?: Range, fallbackRootID?: string) => {+ if (!protyle) {+ return fallbackRootID;+ }+ const rangeRootID = getRangeRootID(protyle, range);+ if (rangeRootID) {+ return rangeRootID;+ }+ const selection = getSelection();+ if (selection.rangeCount > 0) {+ const selectionRootID = getRangeRootID(protyle, selection.getRangeAt(0));+ if (selectionRootID) {+ return selectionRootID;+ }+ }+ const toolbarRootID = getRangeRootID(protyle, protyle.toolbar?.range);+ return toolbarRootID || fallbackRootID || protyle.block?.rootID;+};+// 从 WS 广播 context.undoState 批量更新镜像(多窗口/多端同步)export const syncMirrorFromBroadcast = (undoState: { [rootID: string]: { canUndo: boolean; canRedo: boolean } }) => {if (!undoState) {@@ -53,11 +88,11 @@ export const initMirror = (rootID: string) => {};// 刷新指定 protyle 的撤销/重做按钮态(读镜像,零 fetch)-export const refreshUndoButtons = (protyle: IProtyle) => {- if (!protyle.block?.rootID) {+export const refreshUndoButtons = (protyle: IProtyle, rootID = getUndoRootID(protyle)) => {+ if (!rootID) {return;}- const state = getMirror(protyle.block.rootID);+ const state = getMirror(rootID);if (protyle.breadcrumb) {const parent = protyle.breadcrumb.element.parentElement;const undoElement = parent.querySelector('[data-type="undo"]') as HTMLElement;@@ -139,38 +174,44 @@ const focusRootIDs = (rootIDs: string[], focusBlockId?: string) => {}};-// 请求撤销:读镜像判可撤销 → 跨文档提示 → 调 kernel undo → 本地乐观应用 + 更新镜像-export const requestUndo = async (protyle: IProtyle) => {+const fetchUndoState = (rootID: string) => new Promise<{+ canUndo: boolean;+ canRedo: boolean;+ peekMutatedRootIDs: string[];+}>((resolve) => {+ fetchPost("/api/transactions/undoState", {rootID}, (response) => {+ resolve({+ canUndo: !!response.data?.canUndo,+ canRedo: !!response.data?.canRedo,+ peekMutatedRootIDs: response.data?.peekMutatedRootIDs || [],+ });+ });+});++// 请求撤销:读取 kernel 权威状态 → 跨文档提示 → 调 kernel undo → 本地乐观应用 + 更新镜像+export const requestUndo = async (protyle: IProtyle, rootID?: string) => {if (!protyle || isUndoing) {return;}- const rootID = protyle.block?.rootID;if (!rootID) {return;}- const state = getMirror(rootID);- if (!state.canUndo) {- return; // 语义 B:栈空不做事- }-// 尽早置锁,阻止确认对话框期间触发新的撤销/重做(含 peek 与确认阶段)isUndoing = true;await waitForPendingTransactions(protyle);- // 跨文档提示(标准①):先 peek 栈顶的 mutatedRootIDs- let peekMutatedRootIDs: string[] = [];- await new Promise<void>((resolve) => {- fetchPost("/api/transactions/undoState", {rootID}, (response) => {- if (response.data?.peekMutatedRootIDs) {- peekMutatedRootIDs = response.data.peekMutatedRootIDs;- }- resolve();- });- });+ // 等待输入事务提交后读取权威状态,避免本地镜像尚未更新时吞掉立即撤销。+ const state = await fetchUndoState(rootID);+ markMirror(rootID, {canUndo: state.canUndo, canRedo: state.canRedo});+ if (!state.canUndo) {+ isUndoing = false;+ refreshUndoButtons(protyle, rootID);+ return;+ }- if (peekMutatedRootIDs.length > 1) {- const names = await resolveRootNames(peekMutatedRootIDs);+ if (state.peekMutatedRootIDs.length > 1) {+ const names = await resolveRootNames(state.peekMutatedRootIDs);// 确认期间拦截当前编辑器的键盘输入(遮罩只挡鼠标点击,不挡键盘冒泡)const blockInput = (e: Event) => {e.stopImmediatePropagation();@@ -234,22 +275,23 @@ export const requestUndo = async (protyle: IProtyle) => {};// 请求重做:对称,redo 不提示(其逆已在 undo 中确认)-export const requestRedo = async (protyle: IProtyle) => {+export const requestRedo = async (protyle: IProtyle, rootID?: string) => {if (!protyle || isUndoing) {return;}- const rootID = protyle.block?.rootID;if (!rootID) {return;}- const state = getMirror(rootID);+ isUndoing = true;+ await waitForPendingTransactions(protyle);+ const state = await fetchUndoState(rootID);+ markMirror(rootID, {canUndo: state.canUndo, canRedo: state.canRedo});if (!state.canRedo) {+ isUndoing = false;+ refreshUndoButtons(protyle, rootID);return;}-- isUndoing = true;- await waitForPendingTransactions(protyle);fetchPost("/api/transactions/redo", {rootID,app: Constants.SIYUAN_APPID,diff --git a/app/src/protyle/undo/index.ts b/app/src/protyle/undo/index.tsindex 73caa7acd6..5e27f636c4 100644--- a/app/src/protyle/undo/index.ts+++ b/app/src/protyle/undo/index.ts@@ -5,7 +5,7 @@ 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 {getUndoRootID, markMirror, refreshUndoButtons, requestRedo, requestUndo} from "./globalUndo";import {scrollCenter} from "../../util/highlightById";// 撤销/重做统一契约:kernel 模式由 Undo 实现(转发 kernel),lite 模式由 LocalUndo 实现(前端操作日志)。@@ -37,21 +37,28 @@ const syncToolbarRange = (protyle: IProtyle) => {// 本类仅保留发起窗口本地乐观应用的渲染逻辑(renderLocal,走 isUndo=true 分支,// 保住光标恢复/折叠/zoom 兜底),以及按钮态刷新。export class Undo implements IUndo {+ private lastHistoryRootID?: string;+public undo(protyle: IProtyle) {if (protyle.disabled) {return;}+ // 输入提交或嵌入块重渲染可能使选区失效,需提前保存实际编辑文档。+ const rootID = getUndoRootID(protyle);+ this.lastHistoryRootID = rootID;protyle.wysiwyg.flushPendingInput();// 转发到全局 Manager,由 kernel 弹栈 + 广播,发起窗口本地乐观应用- requestUndo(protyle);+ requestUndo(protyle, rootID);}public redo(protyle: IProtyle) {if (protyle.disabled) {return;}+ const rootID = getUndoRootID(protyle, undefined, this.lastHistoryRootID);+ this.lastHistoryRootID = rootID;protyle.wysiwyg.flushPendingInput();- requestRedo(protyle);+ requestRedo(protyle, rootID);}// renderLocal 仅在发起窗口本地应用操作(isUndo=true),不 POST 到 kernel@@ -84,10 +91,12 @@ export class Undo implements IUndo {// add 降级为:不压栈(kernel 已在 commit 后 Record),仅置位本地镜像 + 刷新按钮态。// 保留签名以兼容 transaction.ts 的调用点。public add(doOperations: IOperation[], undoOperations: IOperation[], protyle: IProtyle) {- if (protyle.block?.rootID) {- markMirror(protyle.block.rootID, {canUndo: true, canRedo: false});+ const rootID = getUndoRootID(protyle);+ this.lastHistoryRootID = rootID;+ if (rootID) {+ markMirror(rootID, {canUndo: true, canRedo: false});}- refreshUndoButtons(protyle);+ refreshUndoButtons(protyle, rootID);}public clear() {