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

composer-block-shortcuts.test.ts

106 lines3.1 KB

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​
16const mac = typeof navigator !== "undefined" && /Mac|iP(hone|[oa]d)/.test(navigator.platform);
17​
18function chord(spec: string) {
19 const parts = spec.split("-");
20 const key = parts.pop()!;
21 const has = (name: string) => parts.includes(name);
22 const base = key.length === 1 ? key.toUpperCase() : key;
23 return {
24 key: has("Shift") && /^\d$/.test(key) ? "!@#$%^&*("[Number(key) - 1]! : key,
25 keyCode: base.length === 1 ? base.charCodeAt(0) : 0,
26 shiftKey: has("Shift"),
27 altKey: has("Alt"),
28 metaKey: has("Mod") && mac,
29 ctrlKey: has("Mod") && !mac,
30 preventDefault() {},
31 stopPropagation() {},
32 } as unknown as KeyboardEvent;
34​
35function press(value: string, spec: string) {
36 const editor = new Editor({
37 extensions: [
38 StarterKit.configure({
39 blockquote: false,
40 bulletList: false,
41 codeBlock: false,
42 heading: false,
43 horizontalRule: false,
44 listItem: false,
45 orderedList: false,
46 trailingNode: false,
47 }),
48 ComposerCodeBlockExtension,
49 ...ComposerBlockExtensions,
50 ...ComposerListExtensions,
51 ComposerTaskListExtension,
52 ComposerTaskItemExtension,
53 ],
54 content: buildDocJson(value, (name) => ({ label: name, description: null })),
55 });
56 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
57 const event = chord(spec);
58 const before = editor.state.doc;
59 const handled = editor.extensionManager.plugins.some((plugin) =>
60 plugin.props.handleKeyDown?.call(plugin, editor.view, event),
61 );
62 return {
63 handled,
64 changed: !editor.state.doc.eq(before),
65 value: serializeEditorDoc(editor.state.doc).value,
66 };
68​
69const BLOCK_CHORDS = [
70 "Mod-Shift-8",
71 "Mod-Shift-7",
72 "Mod-Shift-9",
73 "Mod-Shift-b",
74 "Mod-Alt-c",
75 "Mod-Alt-1",
76 "Mod-Alt-3",
77];
78​
79describe("Tiptap's block shortcuts", () => {
80 it.each(BLOCK_CHORDS)("leave a quote's text alone on %s", (spec) => {
81 expect(press("> keep this text", spec).value).toBe("> keep this text");
82 });
83​
84 it.each(BLOCK_CHORDS)("leave a list item alone on %s", (spec) => {
85 expect(press("- item", spec).value).toBe("- item");
86 });
87​
88 it.each(BLOCK_CHORDS)("leave a paragraph alone on %s", (spec) => {
89 expect(press("plain text", spec).value).toBe("plain text");
90 });
91​
92 it("still delivers the composer's own keys", () => {
93 expect(press("**bold**", "Mod-b").handled).toBe(true);
94 });
95​
96 it.each([
97 ["- p\n- a", "Tab"],
98 ["- p\n - a", "Shift-Tab"],
99 ["- [ ] p\n- [ ] a", "Tab"],
100 ["- [ ] p\n - [ ] a", "Shift-Tab"],
101 ])("leave the nesting of %j alone on %s", (value, spec) => {
102 const result = press(value, spec);
103 expect(result.changed).toBe(false);
104 expect(result.value).toBe(value);
105 });
106});