Open History to see the last commit
1import { Editor } from "@tiptap/core";
2import { TextSelection } from "@tiptap/pm/state";
3import StarterKit from "@tiptap/starter-kit";
4import { describe, expect, it } from "vite-plus/test";
5
6import {
7 buildDocJson,
8 ComposerBlockExtensions,
9 ComposerCodeBlockExtension,
10 ComposerListExtensions,
11 ComposerTaskItemExtension,
12 ComposerTaskListExtension,
13 serializeEditorDoc,
14} from "./composer-rich-text-doc";
15
16// The same modifier ProseMirror's keymap reads "Mod" as.
17const mac = typeof navigator !== "undefined" && /Mac|iP(hone|[oa]d)/.test(navigator.platform);
18
19/** A key chord as the browser reports it: key, keyCode and modifiers. */
20function chord(spec: string) {
21 const parts = spec.split("-");
22 const key = parts.pop()!;
23 const has = (name: string) => parts.includes(name);
24 const base = key.length === 1 ? key.toUpperCase() : key;
25 return {
26 key: has("Shift") && /^\d$/.test(key) ? "!@#$%^&*("[Number(key) - 1]! : key,
27 keyCode: base.length === 1 ? base.charCodeAt(0) : 0,
28 shiftKey: has("Shift"),
29 altKey: has("Alt"),
30 metaKey: has("Mod") && mac,
31 ctrlKey: has("Mod") && !mac,
32 preventDefault() {},
33 stopPropagation() {},
34 } as unknown as KeyboardEvent;
35}
36
37/**
38 * Presses `spec` with the caret inside `value`, the way the composer's
39 * editor would receive it, and returns the stored Markdown.
40 */
41function press(value: string, spec: string) {
42 const editor = new Editor({
43 extensions: [
44 StarterKit.configure({
45 blockquote: false,
46 bulletList: false,
47 codeBlock: false,
48 heading: false,
49 horizontalRule: false,
50 listItem: false,
51 orderedList: false,
52 trailingNode: false,
53 }),
54 ComposerCodeBlockExtension,
55 ...ComposerBlockExtensions,
56 ...ComposerListExtensions,
57 ComposerTaskListExtension,
58 ComposerTaskItemExtension,
59 ],
60 content: buildDocJson(value, (name) => ({ label: name, description: null })),
61 });
62 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
63 // What the view's `someProp("handleKeyDown")` does, without mounting one:
64 // offer the key to each extension plugin in order until one handles it.
65 // An unmounted editor's state does not carry them yet, so they come from
66 // the extension manager.
67 const event = chord(spec);
68 const before = editor.state.doc;
69 const handled = editor.extensionManager.plugins.some((plugin) =>
70 plugin.props.handleKeyDown?.call(plugin, editor.view, event),
71 );
72 return {
73 handled,
74 changed: !editor.state.doc.eq(before),
75 value: serializeEditorDoc(editor.state.doc).value,
76 };
77}
78
79// Tiptap's own block shortcuts would nest a list, task list, code block or
80// heading inside a quote, or restructure a list item. The serializers only
81// write what the composer's grammar has, so the nested block, and the text
82// in it, would vanish from the stored draft. None of these are the
83// composer's shortcuts; they must do nothing.
84const BLOCK_CHORDS = [
85 "Mod-Shift-8",
86 "Mod-Shift-7",
87 "Mod-Shift-9",
88 "Mod-Shift-b",
89 "Mod-Alt-c",
90 "Mod-Alt-1",
91 "Mod-Alt-3",
92];
93
94describe("Tiptap's block shortcuts", () => {
95 it.each(BLOCK_CHORDS)("leave a quote's text alone on %s", (spec) => {
96 expect(press("> keep this text", spec).value).toBe("> keep this text");
97 });
98
99 it.each(BLOCK_CHORDS)("leave a list item alone on %s", (spec) => {
100 expect(press("- item", spec).value).toBe("- item");
101 });
102
103 it.each(BLOCK_CHORDS)("leave a paragraph alone on %s", (spec) => {
104 expect(press("plain text", spec).value).toBe("plain text");
105 });
106
107 // Guards the harness itself: a chord the composer keeps must still reach
108 // the keymap, or every case above would pass without pressing anything.
109 it("still delivers the composer's own keys", () => {
110 expect(press("**bold**", "Mod-b").handled).toBe(true);
111 });
112
113 // Sinking or lifting an item moves it without touching its `indent`, so
114 // the stored draft keeps the old nesting and the next rebuild undoes the
115 // move. Nesting belongs to the composer's own Tab, which edits the source.
116 it.each([
117 ["- p\n- a", "Tab"],
118 ["- p\n - a", "Shift-Tab"],
119 ["- [ ] p\n- [ ] a", "Tab"],
120 ["- [ ] p\n - [ ] a", "Shift-Tab"],
121 ])("leave the nesting of %j alone on %s", (value, spec) => {
122 const result = press(value, spec);
123 expect(result.changed).toBe(false);
124 expect(result.value).toBe(value);
125 });
126});