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: Restore caret after undoing block references https://github.com/siyuan-note/siyuan/issues/18626
Daniel610ad59c8574
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.tsindex 1c2ea29d85..5e1293962a 100644--- a/app/src/protyle/hint/index.ts+++ b/app/src/protyle/hint/index.ts@@ -13,6 +13,7 @@ import {getEditorRange,getSelectionOffset,getSelectionPosition,+ getUndoFocusContext,} from "../util/selection";import {genHintItemHTML, hintEmbed, hintRef, hintSlash} from "./extend";import {@@ -668,6 +669,8 @@ ${genHintItemHTML(item)}id = nodeElement.getAttribute("data-node-id");}const html = nodeElement.outerHTML;+ const undoContext = Constants.BLOCK_HINT_KEYS.includes(this.splitChar) ?+ getUndoFocusContext(protyle.wysiwyg.element, range, true) : undefined;// 自顶向下法新建文档后光标定位问题 https://github.com/siyuan-note/siyuan/issues/299if (this.lastIndex > -1) {range.setStart(range.startContainer, this.lastIndex);@@ -678,7 +681,7 @@ ${genHintItemHTML(item)}const prefix = "((newSubDoc ";const fileNames = value.substring(prefix.length, value.length - 4).split(`"${Constants.ZWSP}'`);const realFileName = fileNames.length === 1 ? fileNames[0] : fileNames[1];- newFileBySelectRange(protyle, range, "subDoc", refIsS ? "s" : "d", realFileName);+ newFileBySelectRange(protyle, range, "subDoc", refIsS ? "s" : "d", realFileName, undoContext);return;}// 新建文件@@ -691,7 +694,7 @@ ${genHintItemHTML(item)}const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {type: "id",color: `${id}${Constants.ZWSP}${refIsS ? "s" : "d"}${Constants.ZWSP}${getBlockRefAnchorText(refIsS ? fileNames[0] : realFileName)}`- });+ }, true, undoContext);if (refElement[0]) {protyle.toolbar.range.setEnd(refElement[0].lastChild, refElement[0].lastChild.textContent.length);}@@ -728,7 +731,7 @@ ${genHintItemHTML(item)}const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {type: "id",color: `${tempElement.getAttribute("data-id")}${Constants.ZWSP}${tempElement.getAttribute("data-subtype")}${Constants.ZWSP}${tempElement.textContent}`- });+ }, true, undoContext);if (refElement[0]) {protyle.toolbar.range.setEnd(refElement[0].lastChild, refElement[0].lastChild.textContent.length);}diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.tsindex 56dcf5bb24..f1a49f963c 100644--- a/app/src/protyle/toolbar/index.ts+++ b/app/src/protyle/toolbar/index.ts@@ -358,7 +358,7 @@ export class Toolbar {}public setInlineMark(protyle: IProtyle, type: string, action: "range" | "toolbar",- textObj?: ITextOption, focusRange = true) {+ textObj?: ITextOption, focusRange = true, undoContext?: Record<string, string>) {const nodeElement = hasClosestBlock(this.range.startContainer);if (!nodeElement) {return;@@ -380,7 +380,7 @@ export class Toolbar {if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") {return;}- return this.setInlineMarkInBlock(protyle, type, action, textObj, undefined, focusRange);+ return this.setInlineMarkInBlock(protyle, type, action, textObj, undefined, focusRange, undoContext);}private setBlockRangesInlineMark(protyle: IProtyle, type: string, action: "range" | "toolbar",@@ -521,7 +521,8 @@ export class Toolbar {}private setInlineMarkInBlock(protyle: IProtyle, type: string, action: "range" | "toolbar",- textObj?: ITextOption, remove?: boolean, focusRange = true) {+ textObj?: ITextOption, remove?: boolean, focusRange = true,+ undoContext?: Record<string, string>) {const nodeElement = hasClosestBlock(this.range.startContainer);if (!nodeElement || nodeElement.getAttribute("data-type") === "NodeCodeBlock") {return;@@ -1060,7 +1061,7 @@ export class Toolbar {}nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));if (!isBatch) {- updateTransaction(protyle, nodeElement, html);+ updateTransaction(protyle, nodeElement, html, undoContext);}nodeElement.querySelectorAll("wbr").forEach(item => {item.remove();diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.tsindex 1670e1d5da..3c140b8efe 100644--- a/app/src/util/newFile.ts+++ b/app/src/util/newFile.ts@@ -111,7 +111,7 @@ const insertNewFileRef = (protyle: IProtyle, context: NewFileSelectionContext, iconst refElements = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {type: "id",color: `${id}${Constants.ZWSP}${refSubtype}${Constants.ZWSP}${anchorText}`- }, false);+ }, false, context.undoContext);if (!refElements?.[0]) {return;}@@ -158,7 +158,8 @@ export const newFileBySelect = (protyle: IProtyle, newFileName: string, context:};export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "subDoc" | "configured",- refSubtype: "d" | "s" = "d", name?: string) => {+ refSubtype: "d" | "s" = "d", name?: string,+ undoContext?: Record<string, string>) => {if (!isSameBlockRange(range)) {return;}@@ -176,7 +177,7 @@ export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "s) {selectAll(protyle, nodeElement, range);}- const selectionContext = createNewFileSelectionContext(protyle, range);+ const selectionContext = createNewFileSelectionContext(protyle, range, undoContext);if (!selectionContext) {return;}diff --git a/app/src/util/newFileSelection.test.ts b/app/src/util/newFileSelection.test.tsindex 1686444901..0d9075ca44 100644--- a/app/src/util/newFileSelection.test.ts+++ b/app/src/util/newFileSelection.test.ts@@ -108,8 +108,9 @@ const createFixture = () => {describe("new document selection context", () => {it("captures the selected document and block boundaries", () => {const {protyle, range} = createFixture();+ const undoContext = {undoFocusId: "start", undoFocusStart: "8"};- const context = createNewFileSelectionContext(protyle, range);+ const context = createNewFileSelectionContext(protyle, range, undoContext);assert.notEqual(context?.range, range);assert.equal(context?.notebookId, "notebook");@@ -118,6 +119,7 @@ describe("new document selection context", () => {assert.equal(context?.startBlockID, "start");assert.equal(context?.endBlockID, "end");assert.equal(context?.text, "selected");+ assert.equal(context?.undoContext, undoContext);});it("keeps a connected selection in the original document valid", () => {diff --git a/app/src/util/newFileSelection.ts b/app/src/util/newFileSelection.tsindex 029f2603ef..652afa1c33 100644--- a/app/src/util/newFileSelection.ts+++ b/app/src/util/newFileSelection.ts@@ -8,9 +8,11 @@ export type NewFileSelectionContext = {startBlockID: string;endBlockID: string;text: string;+ undoContext?: Record<string, string>;};-export const createNewFileSelectionContext = (protyle: IProtyle, range: Range) => {+export const createNewFileSelectionContext = (protyle: IProtyle, range: Range,+ undoContext?: Record<string, string>) => {// 固定触发时的文档和选区,异步创建完成后仅修改原位置 https://github.com/siyuan-note/siyuan/issues/16972const selectionRange = range.cloneRange();const startBlockElement = hasClosestBlock(selectionRange.startContainer);@@ -26,6 +28,7 @@ export const createNewFileSelectionContext = (protyle: IProtyle, range: Range) =startBlockID: startBlockElement.getAttribute("data-node-id"),endBlockID: endBlockElement.getAttribute("data-node-id"),text: selectionRange.toString(),+ undoContext,} as NewFileSelectionContext;};