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