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 after native spelling corrections
Daniel372fd29c5c1e
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex ace8ba0efd..dc3817fb10 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -1,3 +1,4 @@+import {recordReplacementUndo} from "./replacementInput";import {visibleTabsSelectionHTML} from "../render/tabsVisibility";import {prepareInlineElementBoundaryMutation} from "../util/inlineElementBoundary";import {repairHiddenTabSelection} from "../util/tabsSelection";@@ -4172,6 +4173,7 @@ export class WYSIWYG {});this.element.addEventListener("beforeinput", async (event: InputEvent) => {+ recordReplacementUndo(event, this.element, this.lastHTMLs);if ((event.inputType.startsWith("insert") || event.inputType.startsWith("delete")) &&getSelection().rangeCount > 0) {prepareInlineElementBoundaryMutation(getSelection().getRangeAt(0));diff --git a/app/src/protyle/wysiwyg/replacementInput.test.ts b/app/src/protyle/wysiwyg/replacementInput.test.tsnew file mode 100644index 0000000000..910ed72172--- /dev/null+++ b/app/src/protyle/wysiwyg/replacementInput.test.ts@@ -0,0 +1,74 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {recordReplacementUndo} from "./replacementInput";++const setup = (type = "NodeParagraph") => {+ const block = {+ nodeType: 1,+ tagName: "DIV",+ classList: {contains: () => false},+ hasAttribute: (name: string) => name === "data-node-id",+ getAttribute: (name: string) => name === "data-node-id" ? "block" : type,+ outerHTML: `<div data-node-id="block" data-type="${type}"><div contenteditable="true"><strong>beautful</strong></div></div>`,+ };+ const text = {nodeType: 3, parentElement: block};+ const range = {startContainer: text, endContainer: text};+ const element = {+ contains: (node: unknown) => node === text,+ ownerDocument: {getSelection: () => ({rangeCount: 1, getRangeAt: () => range})},+ } as unknown as HTMLElement;+ const event = {+ inputType: "insertReplacementText",+ getTargetRanges: () => [range],+ } as unknown as InputEvent;+ return {block, element, event};+};++describe("native spelling replacement undo", () => {+ it("captures a previously untouched block before replacement, including heading and inline formatting", () => {+ for (const type of ["NodeParagraph", "NodeHeading"]) {+ const {block, element, event} = setup(type);+ const snapshots: {[key: string]: string} = {};+ const original = block.outerHTML;+ recordReplacementUndo(event, element, snapshots);+ block.outerHTML = original.replace("beautful", "beautiful");+ assert.equal(snapshots.block, original);+ }+ });++ it("refreshes stale snapshots for consecutive corrections", () => {+ const {block, element, event} = setup();+ const snapshots = {block: "stale"};+ recordReplacementUndo(event, element, snapshots);+ assert.equal(snapshots.block, block.outerHTML);+ block.outerHTML = block.outerHTML.replace("beautful", "beautiful");+ recordReplacementUndo(event, element, snapshots);+ assert.equal(snapshots.block, block.outerHTML);+ });++ it("uses the replacement target even when the current selection is elsewhere", () => {+ const {block, element, event} = setup();+ element.ownerDocument.getSelection = () => null;+ const snapshots: {[key: string]: string} = {};+ recordReplacementUndo(event, element, snapshots);+ assert.equal(snapshots.block, block.outerHTML);+ });++ it("falls back to the selection when no target ranges are supplied", () => {+ const {block, element, event} = setup();+ event.getTargetRanges = () => [];+ const snapshots: {[key: string]: string} = {};+ recordReplacementUndo(event, element, snapshots);+ assert.equal(snapshots.block, block.outerHTML);+ });++ it("ignores canceled, composing, unrelated and outside-editor input", () => {+ for (const overrides of [{defaultPrevented: true}, {isComposing: true}, {inputType: "insertText"},+ {getTargetRanges: () => [{startContainer: {}, endContainer: {}}]}]) {+ const {element, event} = setup();+ const snapshots = {};+ recordReplacementUndo({...event, ...overrides} as InputEvent, element, snapshots);+ assert.deepEqual(snapshots, {});+ }+ });+});diff --git a/app/src/protyle/wysiwyg/replacementInput.ts b/app/src/protyle/wysiwyg/replacementInput.tsnew file mode 100644index 0000000000..c61559e759--- /dev/null+++ b/app/src/protyle/wysiwyg/replacementInput.ts@@ -0,0 +1,20 @@+import {hasClosestBlock} from "../util/hasClosest";++export const recordReplacementUndo = (event: InputEvent, element: HTMLElement,+ lastHTMLs: {[key: string]: string}) => {+ if (event.defaultPrevented || event.inputType !== "insertReplacementText" || event.isComposing) {+ return;+ }+ const selection = element.ownerDocument.getSelection();+ const range = event.getTargetRanges()[0] ||+ (selection?.rangeCount > 0 ? selection.getRangeAt(0) : undefined);+ if (!range || !element.contains(range.startContainer) || !element.contains(range.endContainer)) {+ return;+ }+ const blockElement = hasClosestBlock(range.startContainer);+ if (!blockElement || blockElement !== hasClosestBlock(range.endContainer)) {+ return;+ }+ // 原生纠错不经过键盘输入,在浏览器替换文本前保存完整块,供撤销恢复内容及格式。+ lastHTMLs[blockElement.getAttribute("data-node-id")] = blockElement.outerHTML;+};