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

composer-list-split.browser.test.ts

246 lines8.7 KB

1import { Editor } from "@tiptap/core";
2import { TaskList } from "@tiptap/extension-task-list";
3import { TextSelection } from "@tiptap/pm/state";
4import StarterKit from "@tiptap/starter-kit";
5import { afterEach, describe, expect, it } from "vite-plus/test";
6​
7import {
8 backspaceAcrossList,
9 buildDocJson,
10 buildTiptapContent,
11 ComposerBlockExtensions,
12 ComposerCodeBlockExtension,
13 ComposerListExtensions,
14 ComposerTaskItemExtension,
15 convertBulletItemToTask,
16 deleteAcrossList,
17 serializeEditorDoc,
18 splitOrLiftListItem,
19} from "./composer-rich-text-doc";
20​
21// ProseMirror's DOM observer can flush on a timer after a test ends, which
22// throws once its DOM is gone, so every editor is destroyed after its test.
23const editors: Editor[] = [];
24afterEach(() => {
25 for (const editor of editors.splice(0)) editor.destroy();
26});
27​
28function makeEditor(value: string) {
29 const editor = new Editor({
30 extensions: [
31 StarterKit.configure({
32 bulletList: false,
33 orderedList: false,
34 listItem: false,
35 codeBlock: false,
36 blockquote: false,
37 heading: false,
38 horizontalRule: false,
39 trailingNode: false,
40 listKeymap: false,
41 }),
42 ...ComposerListExtensions,
43 TaskList,
44 ComposerTaskItemExtension,
45 ComposerCodeBlockExtension,
46 ...ComposerBlockExtensions,
47 ],
48 content: buildDocJson(value, (name) => ({ label: name, description: null })),
49 });
50 editors.push(editor);
51 return editor;
53​
54function splitLastItem(value: string, type: "listItem" | "taskItem", overrides: object) {
55 const editor = makeEditor(value);
56 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
57 editor.commands.splitListItem(type, overrides);
58 editor.view.dispatch(editor.state.tr.insertText("b"));
59 return serializeEditorDoc(editor.state.doc).value;
61​
62describe("splitting a list item", () => {
63 it.each([
64 ["* a", { space: " " }, "* a\n* b"],
65 ["+ a", { space: " " }, "+ a\n+ b"],
66 ["- p\n - a", { space: " " }, "- p\n - a\n - b"],
67 ["3) a", { marker: "4)", space: " " }, "3) a\n4) b"],
68 ])("keeps the source marker and indent of %s", (value, overrides, expected) => {
69 expect(splitLastItem(value, "listItem", overrides)).toBe(expected);
70 });
71​
72 it("keeps the indent of a nested task", () => {
73 expect(splitLastItem("- [ ] p\n - [ ] a", "taskItem", { checked: false })).toBe(
74 "- [ ] p\n - [ ] a\n - [ ] b",
75 );
76 });
77});
78​
79function placeCaret(editor: Editor, text: string, offset = 0) {
80 let caret = -1;
81 editor.state.doc.descendants((node, pos) => {
82 if (caret < 0 && node.isText && node.text!.includes(text)) {
83 caret = pos + node.text!.indexOf(text) + offset;
84 }
85 });
86 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, caret)));
88​
89function storedDraft(editor: Editor) {
90 const stored = serializeEditorDoc(editor.state.doc).value;
91 expect(makeEditor(stored).state.doc.toString()).toBe(editor.state.doc.toString());
92 return stored;
94​
95describe("Shift+Enter twice on an item", () => {
96 it.each([
97 ["- a\n - x", "- a\n - x\n- b"],
98 ["* a\n * x", "* a\n * x\n* b"],
99 ["1. a\n 1. x", "1. a\n 1. x\n2. b"],
100 ["- a\n - x\n- c", "- a\n - x\n- b\n- c"],
101 ["- a\n - x\n - y", "- a\n - x\n- b\n - y"],
102 ["- [ ] a\n - [ ] x", "- [ ] a\n - [ ] x\n- [ ] b"],
103 ["- [x] a\n - [ ] x\n- [ ] c", "- [x] a\n - [ ] x\n- [ ] b\n- [ ] c"],
104 ])("after x in %j writes %j", (value, expected) => {
105 const editor = makeEditor(value);
106 let caret = -1;
107 editor.state.doc.descendants((node, pos) => {
108 if (node.isText && node.text === "x") caret = pos + 1;
109 });
110 editor.view.dispatch(
111 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, caret)),
112 );
113 expect(splitOrLiftListItem(editor)).toBe(true);
114 expect(splitOrLiftListItem(editor)).toBe(true);
115 editor.view.dispatch(editor.state.tr.insertText("b"));
116 const stored = serializeEditorDoc(editor.state.doc).value;
117 expect(stored).toBe(expected);
118 expect(makeEditor(stored).state.doc.toString()).toBe(editor.state.doc.toString());
119 });
120});
121​
122describe("Shift+Enter in the middle of an ordered list", () => {
123 it.each([
124 ["1. a\n2. x\n3. c\n4. d", "1. a\n2. x\n3. b\n4. c\n5. d"],
125 ["1) a\n2) x\n3) c", "1) a\n2) x\n3) b\n4) c"],
126 ["1. a\n 1. x\n 2. c\n2. d", "1. a\n 1. x\n 2. b\n 3. c\n2. d"],
127 ])("after x in %j writes %j", (value, expected) => {
128 const editor = makeEditor(value);
129 placeCaret(editor, "x", 1);
130 expect(splitOrLiftListItem(editor)).toBe(true);
131 editor.view.dispatch(editor.state.tr.insertText("b"));
132 expect(storedDraft(editor)).toBe(expected);
133 });
134});
135​
136describe("Shift+Enter on an empty task under a bullet", () => {
137 it.each(["- a\n - [ ] x", "- [ ] a\n - x"])("in %j keeps the draft honest", (value) => {
138 const editor = makeEditor(value);
139 placeCaret(editor, "x", 1);
140 splitOrLiftListItem(editor);
141 splitOrLiftListItem(editor);
142 storedDraft(editor);
143 });
144});
145​
146describe("Backspace at the start of a line", () => {
147 it.each([
148 ["- a\n - x", "x", "- a\n- x"],
149 ["- a\n - x\n - y", "x", "- a\n- x\n - y"],
150 ["- [ ] a\n - [ ] x", "x", "- [ ] a\n- [ ] x"],
151 ["- a\n- b\n- c", "b", "- a\nb\n- c"],
152 ["- a\n - [ ] x", "x", "- ax"],
153 ["- a\n - x\nb", "b", "- a\n - xb"],
154 ["1. a\nb", "b", "1. ab"],
155 ])("in %j at %s writes %j", (value, at, expected) => {
156 const editor = makeEditor(value);
157 placeCaret(editor, at);
158 expect(backspaceAcrossList(editor)).toBe(true);
159 expect(storedDraft(editor)).toBe(expected);
160 });
161​
162 it.each([
163 ["- a\n```\nl1\nl2\n```", "- a\nl1\nl2"],
164 ["p\n```ts\nl1\n```", "p\nl1"],
165 ])("removes the fence of %j rather than joining its code up", (value, expected) => {
166 const editor = makeEditor(value);
167 placeCaret(editor, "l1");
168 editor.commands.keyboardShortcut("Backspace");
169 expect(storedDraft(editor)).toBe(expected);
170 });
171});
172​
173describe("Delete at the end of a line", () => {
174 it.each([
175 ["- a\n- b", "- ab"],
176 ["- a\n - x\n- c", "- ax\n- c"],
177 ["- a\n\nb", "- a\nb"],
178 ])("in %j writes %j", (value, expected) => {
179 const editor = makeEditor(value);
180 placeCaret(editor, "a", 1);
181 expect(deleteAcrossList(editor)).toBe(true);
182 expect(storedDraft(editor)).toBe(expected);
183 });
184​
185 it.each(["- a\n```\nl1\n```", "a\n```\nl1\n```"])("leaves the fence in %j alone", (value) => {
186 const editor = makeEditor(value);
187 placeCaret(editor, "a", 1);
188 editor.commands.keyboardShortcut("Delete");
189 expect(storedDraft(editor)).toBe(value);
190 });
191});
192​
193describe("blocks that a list item or quote cannot hold", () => {
194 it("puts lines pasted at the end of an item after the list", () => {
195 const editor = makeEditor("- a");
196 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
197 const lines = buildTiptapContent("one\ntwo", (name) => ({ label: name, description: null }));
198 editor.commands.insertContent(lines);
199 expect(storedDraft(editor)).toBe("- a\none\ntwo");
200 });
201​
202 it("will not make a task list inside a quote", () => {
203 const editor = makeEditor("> q");
204 placeCaret(editor, "q");
205 expect(editor.commands.wrapInList("taskList")).toBe(false);
206 expect(storedDraft(editor)).toBe("> q");
207 });
208});
209​
210describe("[ ] typed at the start of a bullet item", () => {
211 it.each([
212 ["* [ ]x", "- [ ] x"],
213 ["* a\n* [ ]x\n* c", "* a\n- [ ] x\n* c"],
214 ["- a\n * [ ]x", "- a\n - [ ] x"],
215 ["- a\n - [ ]x\n - y", "- a\n - [ ] x\n - y"],
216 ["- [ ]x\n - y", "- [ ] x\n - y"],
217 ["- [ ]x", "- [ ] x"],
218 ])("makes the item in %j a task where it stands", (value, expected) => {
219 const editor = makeEditor(value);
220 placeCaret(editor, "[ ]x");
221 const from = editor.state.selection.from;
222 editor.view.dispatch(
223 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, from + 3)),
224 );
225 const { tr } = editor.state;
226 convertBulletItemToTask(tr, from, from + 3, false);
227 editor.view.dispatch(tr);
228 expect(editor.state.selection.$from.parent.textContent).toBe("x");
229 expect(editor.state.selection.$from.parentOffset).toBe(0);
230 expect(storedDraft(editor)).toBe(expected);
231 });
232});
233​
234describe("a rule at the end of the draft", () => {
235 it("keeps an empty line after it to type on", () => {
236 const editor = makeEditor("a\n---");
237 editor.view.dispatch(
238 editor.state.tr.delete(editor.state.doc.content.size - 2, editor.state.doc.content.size),
239 );
240 expect(editor.state.doc.lastChild?.type.name).toBe("paragraph");
241 expect(storedDraft(editor)).toBe("a\n---");
242 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
243 editor.view.dispatch(editor.state.tr.insertText("b"));
244 expect(storedDraft(editor)).toBe("a\n---\nb");
245 });
246});