An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: https://github.com/siyuan-note/siyuan/issues/18151
Vanessa25c04a94e6fe
diff --git a/app/src/protyle/undo/index.ts b/app/src/protyle/undo/index.tsindex 52e96ce7b2..ee910d3b1b 100644--- a/app/src/protyle/undo/index.ts+++ b/app/src/protyle/undo/index.ts@@ -1,15 +1,35 @@-import {onTransaction} from "../wysiwyg/transaction";+import {onTransaction, transaction} from "../wysiwyg/transaction";import {preventScroll} from "../scroll/preventScroll";import {Constants} from "../../constants";import {hideElements} from "../ui/hideElements";import {matchHotKey} from "../util/hotKey";import {ipcRenderer} from "electron";import {markMirror, refreshUndoButtons, requestRedo, requestUndo} from "./globalUndo";+import {scrollCenter} from "../../util/highlightById";++// 撤销/重做统一契约:kernel 模式由 Undo 实现(转发 kernel),lite 模式由 LocalUndo 实现(前端操作日志)。+export interface IUndo {+ undo(protyle: IProtyle): void;++ redo(protyle: IProtyle): void;++ add(doOperations: IOperation[], undoOperations: IOperation[], protyle: IProtyle): void;++ clear(): void;++ // kernel 模式独有:发起窗口本地乐观应用操作(lite 模式的 LocalUndo 不需要)。+ renderLocal?(protyle: IProtyle, operations: IOperation[]): void;+}++interface IOperations {+ doOperations: IOperation[];+ undoOperations: IOperation[];+}// 撤销权威栈已下沉到 kernel(GlobalUndoLog),前端按 rootID 共享。// 本类仅保留发起窗口本地乐观应用的渲染逻辑(renderLocal,走 isUndo=true 分支,// 保住光标恢复/折叠/zoom 兜底),以及按钮态刷新。-export class Undo {+export class Undo implements IUndo {public undo(protyle: IProtyle) {if (protyle.disabled) {return;@@ -66,6 +86,137 @@ export class Undo {}}+// lite 模式的前端撤销:不落盘、无 rootID,无法用 kernel 的 GlobalUndoLog,+// 故在前端以 IOperation 操作日志维护撤销/重做。回放时用 onTransaction(ops, true) 本地应用 DOM。+export class LocalUndo implements IUndo {+ private hasUndo = false;+ public redoStack: IOperations[];+ public undoStack: IOperations[];++ constructor() {+ this.redoStack = [];+ this.undoStack = [];+ }++ public undo(protyle: IProtyle) {+ if (protyle.disabled) {+ return;+ }+ if (this.undoStack.length === 0) {+ return;+ }+ const state = this.undoStack.pop();+ this.render(protyle, state, false);+ this.hasUndo = true;+ this.redoStack.push(state);+ if (protyle.breadcrumb) {+ const undoElement = protyle.breadcrumb.element.parentElement.querySelector('[data-type="undo"]');+ if (undoElement) {+ if (this.undoStack.length === 0) {+ undoElement.setAttribute("disabled", "true");+ }+ protyle.breadcrumb.element.parentElement.querySelector('[data-type="redo"]').removeAttribute("disabled");+ }+ }+ }++ public redo(protyle: IProtyle) {+ if (protyle.disabled) {+ return;+ }+ if (this.redoStack.length === 0) {+ return;+ }+ const state = this.redoStack.pop();+ this.render(protyle, state, true);+ this.undoStack.push(state);+ if (protyle.breadcrumb) {+ const redoElement = protyle.breadcrumb.element.parentElement.querySelector('[data-type="redo"]');+ if (redoElement) {+ protyle.breadcrumb.element.parentElement.querySelector('[data-type="undo"]').removeAttribute("disabled");+ if (this.redoStack.length === 0) {+ redoElement.setAttribute("disabled", "true");+ }+ }+ }+ }++ private render(protyle: IProtyle, state: IOperations, redo: boolean) {+ hideElements(["hint", "gutter"], protyle);+ protyle.wysiwyg.lastHTMLs = {};+ if (!redo) {+ for (let i = state.undoOperations.length - 1; i >= 0; i--) {+ if (state.undoOperations[i].action === "insert") {+ if (state.undoOperations[i].context) {+ state.undoOperations[i].context.setRange = "true";+ } else {+ state.undoOperations[i].context = {setRange: "true"};+ }+ break;+ }+ }+ onTransaction(protyle, state.undoOperations, true);+ transaction(protyle, state.undoOperations, undefined, {skipSync: true});+ } else {+ for (let i = state.doOperations.length - 1; i >= 0; i--) {+ if (state.doOperations[i].action === "insert") {+ if (state.doOperations[i].context) {+ state.doOperations[i].context.setRange = "true";+ } else {+ state.doOperations[i].context = {setRange: "true"};+ }+ break;+ }+ }+ onTransaction(protyle, state.doOperations, true);+ transaction(protyle, state.doOperations, undefined, {skipSync: true});+ }+ document.querySelector(".av__panel")?.remove();+ preventScroll(protyle);+ scrollCenter(protyle);+ }++ public replace(doOperations: IOperation[], protyle: IProtyle) {+ // undo 引发 replace 导致 stack 错误 https://github.com/siyuan-note/siyuan/issues/9178+ if (this.hasUndo && this.redoStack.length > 0) {+ this.undoStack.push(this.redoStack.pop());+ this.redoStack = [];+ this.hasUndo = false;+ if (protyle.breadcrumb) {+ const redoElement = protyle.breadcrumb.element.parentElement.querySelector('[data-type="redo"]');+ if (redoElement) {+ redoElement.setAttribute("disabled", "true");+ }+ }+ }+ if (this.undoStack.length > 0) {+ this.undoStack[this.undoStack.length - 1].doOperations = doOperations;+ }+ }++ public add(doOperations: IOperation[], undoOperations: IOperation[], protyle: IProtyle) {+ this.undoStack.push({undoOperations, doOperations});+ if (this.undoStack.length > Constants.SIZE_UNDO) {+ this.undoStack.shift();+ }+ if (this.hasUndo) {+ this.redoStack = [];+ this.hasUndo = false;+ }+ if (protyle.breadcrumb) {+ const undoElement = protyle.breadcrumb.element.parentElement.querySelector('[data-type="undo"]');+ if (undoElement) {+ undoElement.removeAttribute("disabled");+ }+ }+ }++ public clear() {+ this.undoStack = [];+ this.redoStack = [];+ }+}+export const electronUndo = (event: KeyboardEvent) => {/// #if !BROWSERif (matchHotKey(window.siyuan.config.keymap.editor.general.undo.custom, event)) {