|
| 1 | +import { getSchema } from "@tiptap/core"; |
| 2 | +import { history, undo } from "@tiptap/pm/history"; |
| 3 | +import { EditorState, TextSelection, type Transaction } from "@tiptap/pm/state"; |
| 4 | +import StarterKit from "@tiptap/starter-kit"; |
| 5 | +import { describe, expect, it } from "vite-plus/test"; |
| 6 | + |
| 7 | +import { |
| 8 | + COMPOSER_UNDO_GROUP_DELAY, |
| 9 | + type ComposerChangeKind, |
| 10 | + groupUndoByChangeKind, |
| 11 | + markAsClipboardEdit, |
| 12 | +} from "./composer-undo-grouping"; |
| 13 | + |
| 14 | +const schema = getSchema([StarterKit]); |
| 15 | + |
| 16 | +/** |
| 17 | + * A composer-shaped editor state with the real history plugin, dispatching |
| 18 | + * through the same grouping rule the composer's extension applies. Keystrokes |
| 19 | + * land 80ms apart unless a gap says otherwise. |
| 20 | + */ |
| 21 | +function composer(text = "") { |
| 22 | + let clock = 0; |
| 23 | + let previous: ComposerChangeKind | null = null; |
| 24 | + let state = EditorState.create({ |
| 25 | + schema, |
| 26 | + doc: schema.node("doc", null, [ |
| 27 | + schema.node("paragraph", null, text ? [schema.text(text)] : []), |
| 28 | + ]), |
| 29 | + plugins: [history({ newGroupDelay: COMPOSER_UNDO_GROUP_DELAY })], |
| 30 | + }); |
| 31 | + const dispatch = (tr: Transaction) => { |
| 32 | + tr.setTime(clock); |
| 33 | + previous = groupUndoByChangeKind(tr, previous); |
| 34 | + state = state.apply(tr); |
| 35 | + }; |
| 36 | + dispatch(state.tr.setSelection(TextSelection.atEnd(state.doc))); |
| 37 | + const api = { |
| 38 | + text: () => state.doc.textContent, |
| 39 | + wait: (ms: number) => ((clock += ms), api), |
| 40 | + type: (s: string, gap = 80) => { |
| 41 | + for (const ch of s) { |
| 42 | + clock += gap; |
| 43 | + dispatch(state.tr.insertText(ch)); |
| 44 | + } |
| 45 | + return api; |
| 46 | + }, |
| 47 | + backspace: (n: number) => { |
| 48 | + for (let i = 0; i < n; i += 1) { |
| 49 | + clock += 80; |
| 50 | + const at = state.selection.from; |
| 51 | + dispatch(state.tr.delete(at - 1, at)); |
| 52 | + } |
| 53 | + return api; |
| 54 | + }, |
| 55 | + /** |
| 56 | + * The transaction the composer's `handlePaste` dispatches: a plain insert, |
| 57 | + * tagged by `markAsClipboardEdit`. ProseMirror's own paste path would tag |
| 58 | + * it, but the composer takes every text paste before that path runs. |
| 59 | + */ |
| 60 | + paste: (s: string) => { |
| 61 | + clock += 80; |
| 62 | + dispatch(markAsClipboardEdit(state.tr.insertText(s), "paste")); |
| 63 | + return api; |
| 64 | + }, |
| 65 | + /** The composer's cut handler: a plain delete of the selection, tagged. */ |
| 66 | + cut: (n: number) => { |
| 67 | + clock += 80; |
| 68 | + const at = state.selection.from; |
| 69 | + dispatch(markAsClipboardEdit(state.tr.delete(at - n, at), "cut")); |
| 70 | + return api; |
| 71 | + }, |
| 72 | + /** A store-driven rewrite: autocomplete inserting a chip, list continuation. */ |
| 73 | + rewrite: (next: string) => { |
| 74 | + clock += 80; |
| 75 | + dispatch( |
| 76 | + state.tr.replaceWith( |
| 77 | + 0, |
| 78 | + state.doc.content.size, |
| 79 | + schema.node("paragraph", null, [schema.text(next)]), |
| 80 | + ), |
| 81 | + ); |
| 82 | + dispatch(state.tr.setSelection(TextSelection.atEnd(state.doc))); |
| 83 | + return api; |
| 84 | + }, |
| 85 | + /** IME input replaces its own range on every keystroke. */ |
| 86 | + compose: (s: string) => { |
| 87 | + const start = state.selection.from; |
| 88 | + for (let i = 1; i <= s.length; i += 1) { |
| 89 | + clock += 80; |
| 90 | + dispatch( |
| 91 | + state.tr.insertText(s.slice(0, i), start, start + i - 1).setMeta("composition", 1), |
| 92 | + ); |
| 93 | + } |
| 94 | + return api; |
| 95 | + }, |
| 96 | + undo: () => (undo(state, dispatch), api), |
| 97 | + }; |
| 98 | + return api; |
| 99 | +} |
| 100 | + |
| 101 | +describe("composer undo grouping", () => { |
| 102 | + it("keeps a continuous run of typing as one step", () => { |
| 103 | + expect(composer().type("hello world").undo().text()).toBe(""); |
| 104 | + }); |
| 105 | + |
| 106 | + it("starts a new step when typing turns into deleting, and back", () => { |
| 107 | + const editor = composer().type("hello world").backspace(5).type("there"); |
| 108 | + expect(editor.text()).toBe("hello there"); |
| 109 | + expect(editor.undo().text()).toBe("hello "); |
| 110 | + expect(editor.undo().text()).toBe("hello world"); |
| 111 | + expect(editor.undo().text()).toBe(""); |
| 112 | + }); |
| 113 | + |
| 114 | + it("gives a store rewrite its own step, and typing after it another", () => { |
| 115 | + const editor = composer("draft ").type("one").rewrite("draft one @chip ").type("then"); |
| 116 | + expect(editor.undo().text()).toBe("draft one @chip "); |
| 117 | + expect(editor.undo().text()).toBe("draft one"); |
| 118 | + expect(editor.undo().text()).toBe("draft "); |
| 119 | + }); |
| 120 | + |
| 121 | + it("gives a paste its own step", () => { |
| 122 | + const editor = composer().type("see ").paste("pasted text").type(" ok"); |
| 123 | + expect(editor.undo().text()).toBe("see pasted text"); |
| 124 | + expect(editor.undo().text()).toBe("see "); |
| 125 | + }); |
| 126 | + |
| 127 | + it("gives a cut its own step between deletions", () => { |
| 128 | + const editor = composer().type("hello world").backspace(1).cut(3).backspace(1); |
| 129 | + expect(editor.text()).toBe("hello "); |
| 130 | + expect(editor.undo().text()).toBe("hello w"); |
| 131 | + expect(editor.undo().text()).toBe("hello worl"); |
| 132 | + expect(editor.undo().text()).toBe("hello world"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("keeps IME composition as one run of typing", () => { |
| 136 | + expect(composer().type("go ").compose("にほん").undo().text()).toBe(""); |
| 137 | + }); |
| 138 | + |
| 139 | + it("merges typing across a short pause and splits it across a long one", () => { |
| 140 | + expect(composer().type("hello").wait(700).type(" world").undo().text()).toBe(""); |
| 141 | + expect(composer().type("hello").wait(1100).type(" world").undo().text()).toBe("hello"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments