Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-undo-grouping.test.ts

135 lines4.5 KB

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