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

composer-code-block.test.ts

261 lines9.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import { Editor, Node } from "@tiptap/core";
3import { newlineInCode } from "@tiptap/pm/commands";
4import { TextSelection } from "@tiptap/pm/state";
5​
6import {
7 indentCodeBlock,
8 convertCodeFenceOnEnter,
9 exitCodeBlockOnClosingFence,
10 exitCodeBlockOnTrailingBlankLines,
11 indentedNewlineInCodeBlock,
12} from "./composer-code-block";
13import StarterKit from "@tiptap/starter-kit";
14​
15import {
16 ComposerCodeBlockExtension,
17 ComposerListExtensions,
18 serializeEditorDoc,
19} from "./composer-rich-text-doc";
20​
21const extensions = [
22 StarterKit.configure({ codeBlock: false, trailingNode: false }),
23 ComposerCodeBlockExtension,
24 ...ComposerListExtensions,
25 Node.create({
26 name: "composer-mention",
27 group: "inline",
28 inline: true,
29 atom: true,
30 addAttributes: () => ({ path: { default: "" }, source: { default: "" } }),
31 }),
32];
33​
34function codeEditor(code: string, at?: { from: number; to?: number }) {
35 const editor = new Editor({
36 extensions,
37 content: {
38 type: "doc",
39 content: [{ type: "codeBlock", content: [{ type: "text", text: code }] }],
40 },
41 });
42 const start = 1;
43 const from = start + (at?.from ?? code.length);
44 const to = start + (at?.to ?? at?.from ?? code.length);
45 editor.view.dispatch(
46 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, from, to)),
47 );
48 return editor;
50​
51const textOf = (editor: Editor) => editor.state.doc.textContent;
52​
53describe("indentedNewlineInCodeBlock", () => {
54 it("carries the current indentation onto the new line", () => {
55 const editor = codeEditor("function f() {\n body()");
56 const handled = indentedNewlineInCodeBlock(editor.state, (tr) => editor.view.dispatch(tr));
57 expect(handled).toBe(true);
58 expect(textOf(editor)).toBe("function f() {\n body()\n ");
59 });
60​
61 it("indents from the line the caret is on, not the last line", () => {
62 const editor = codeEditor(" deep()\nflush()", { from: 10 });
63 indentedNewlineInCodeBlock(editor.state, (tr) => editor.view.dispatch(tr));
64 expect(textOf(editor)).toBe(" deep()\n \nflush()");
65 });
66});
67​
68function pressEnter(editor: Editor) {
69 const dispatch = (tr: Parameters<typeof editor.view.dispatch>[0]) => editor.view.dispatch(tr);
70 return (
71 exitCodeBlockOnTrailingBlankLines(editor.view) ||
72 indentedNewlineInCodeBlock(editor.state, dispatch) ||
73 newlineInCode(editor.state, dispatch)
74 );
76​
77describe("exitCodeBlockOnTrailingBlankLines", () => {
78 it("leaves the block once two blank lines end it", () => {
79 const editor = codeEditor("flush()");
80 pressEnter(editor);
81 pressEnter(editor);
82 expect(editor.state.doc.childCount).toBe(1);
83 pressEnter(editor);
84 expect(editor.state.doc.firstChild?.textContent).toBe("flush()");
85 expect(editor.state.selection.$from.parent.type.name).toBe("paragraph");
86 });
87​
88 it("closes an unclosed fence on the way out", () => {
89 const editor = codeEditor("flush()");
90 editor.view.dispatch(editor.state.tr.setNodeAttribute(0, "close", ""));
91 pressEnter(editor);
92 pressEnter(editor);
93 pressEnter(editor);
94 expect(editor.state.doc.firstChild?.attrs.close).toBe("\n```");
95 });
96});
97​
98describe("exitCodeBlockOnClosingFence", () => {
99 const exit = (editor: Editor) =>
100 exitCodeBlockOnClosingFence(editor.state, (tr) => editor.view.dispatch(tr));
101​
102 it("ends the block at a typed closing fence and starts a line after it", () => {
103 const editor = codeEditor("const a = 1\n```");
104 editor.view.dispatch(editor.state.tr.setNodeAttribute(0, "language", "ts"));
105 expect(exit(editor)).toBe(true);
106 editor.view.dispatch(editor.state.tr.insertText("explain this"));
107 expect(serializeEditorDoc(editor.state.doc).value).toBe(
108 "```ts\nconst a = 1\n```\nexplain this",
109 );
110 });
111​
112 it("moves the lines after the fence out of the block", () => {
113 const editor = codeEditor("a\n```\nb", { from: "a\n```".length });
114 expect(exit(editor)).toBe(true);
115 expect(serializeEditorDoc(editor.state.doc).value).toBe("```\na\n```\n\nb");
116 expect(editor.state.selection.$from.parent.type.name).toBe("paragraph");
117 expect(editor.state.selection.$from.parent.textContent).toBe("");
118 });
119​
120 it.each([
121 ["a line that cannot close the fence", "a\n``", undefined],
122 ["a caret before the end of the line", "a\n```", { from: "a\n``".length }],
123 ])("ignores %s", (_, code, at) => {
124 expect(exit(codeEditor(code, at))).toBe(false);
125 });
126});
127​
128describe("indentCodeBlock", () => {
129 it("indents every line a selection touches", () => {
130 const editor = codeEditor("one\ntwo\nthree", { from: 1, to: 9 });
131 indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr));
132 expect(textOf(editor)).toBe(" one\n two\n three");
133 });
134​
135 it("leaves the line a selection ends at the start of", () => {
136 const editor = codeEditor("one\ntwo", { from: 0, to: 4 });
137 indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr));
138 expect(textOf(editor)).toBe(" one\ntwo");
139 });
140​
141 it("outdents every line a selection touches", () => {
142 const editor = codeEditor(" one\n two", { from: 2, to: 9 });
143 indentCodeBlock(editor.state, "out", (tr) => editor.view.dispatch(tr));
144 expect(textOf(editor)).toBe("one\ntwo");
145 });
146​
147 it("keeps the selection across the shifted lines", () => {
148 const editor = codeEditor("one\ntwo", { from: 0, to: 7 });
149 indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr));
150 indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr));
151 expect(textOf(editor)).toBe(" one\n two");
152 });
153​
154 it("reports handled when outdenting can go no further, so Tab never escapes the editor", () => {
155 const editor = codeEditor("one\ntwo", { from: 0, to: 7 });
156 expect(indentCodeBlock(editor.state, "out", (tr) => editor.view.dispatch(tr))).toBe(true);
157 expect(textOf(editor)).toBe("one\ntwo");
158 });
159});
160​
161describe("selections across two identical fences", () => {
162 function twoBlockEditor() {
163 const block = { type: "codeBlock", content: [{ type: "text", text: " a" }] };
164 const editor = new Editor({ extensions, content: { type: "doc", content: [block, block] } });
165 editor.view.dispatch(
166 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 2, 7)),
167 );
168 return editor;
169 }
170​
171 it("leaves Tab and Enter alone rather than editing through the boundary", () => {
172 const editor = twoBlockEditor();
173 const before = editor.getJSON();
174 expect(indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr))).toBe(false);
175 expect(indentedNewlineInCodeBlock(editor.state, (tr) => editor.view.dispatch(tr))).toBe(false);
176 expect(editor.getJSON()).toEqual(before);
177 });
178});
179​
180describe("convertCodeFenceOnEnter", () => {
181 function paragraphEditor(text: string) {
182 const editor = new Editor({
183 extensions,
184 content: { type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text }] }] },
185 });
186 editor.view.dispatch(
187 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 1 + text.length)),
188 );
189 return editor;
190 }
191​
192 it.each([
193 ["```", "```", ""],
194 ["```ts", "```", "ts"],
195 ["~~~py", "~~~", "py"],
196 ["````", "````", ""],
197 ["```ts title=example", "```", "ts title=example"],
198 ["``` js", "```", " js"],
199 ])("turns %s into a code block", (text, fence, language) => {
200 const editor = paragraphEditor(text);
201 expect(convertCodeFenceOnEnter(editor.state, (tr) => editor.view.dispatch(tr))).toBe(true);
202 const block = editor.state.doc.firstChild!;
203 expect(block.type.name).toBe("codeBlock");
204 expect(block.attrs).toMatchObject({ fence, language, close: `\n${fence}` });
205 expect(block.textContent).toBe("");
206 expect(editor.state.selection.from).toBe(1);
207 });
208​
209 it.each(["``", "text ```", "```a`b"])("leaves %s alone", (text) => {
210 const editor = paragraphEditor(text);
211 expect(convertCodeFenceOnEnter(editor.state, (tr) => editor.view.dispatch(tr))).toBe(false);
212 });
213​
214 it("refuses to open a fence inside a list item", () => {
215 const text = "```ts";
216 const editor = new Editor({
217 extensions,
218 content: {
219 type: "doc",
220 content: [
221 {
222 type: "bulletList",
223 content: [
224 {
225 type: "listItem",
226 content: [{ type: "paragraph", content: [{ type: "text", text }] }],
227 },
228 ],
229 },
230 ],
231 },
232 });
233 editor.view.dispatch(
234 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 3 + text.length)),
235 );
236 const before = editor.getJSON();
237 expect(convertCodeFenceOnEnter(editor.state, (tr) => editor.view.dispatch(tr))).toBe(false);
238 expect(editor.getJSON()).toEqual(before);
239 });
240​
241 it("keeps a chip's source in the info string", () => {
242 const editor = new Editor({
243 extensions,
244 content: {
245 type: "doc",
246 content: [
247 {
248 type: "paragraph",
249 content: [
250 { type: "text", text: "```ts " },
251 { type: "composer-mention", attrs: { path: "a.md", source: "@a.md" } },
252 ],
253 },
254 ],
255 },
256 });
257 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
258 expect(convertCodeFenceOnEnter(editor.state, (tr) => editor.view.dispatch(tr))).toBe(true);
259 expect(editor.state.doc.firstChild?.attrs.language).toBe("ts @a.md");
260 });
261});