|
1 | | -import { getSchemaByResolvedExtensions, Node, resolveExtensions } from "@tiptap/core"; |
| 1 | +import { Editor, getSchemaByResolvedExtensions, Node, resolveExtensions } from "@tiptap/core"; |
2 | 2 | import StarterKit from "@tiptap/starter-kit"; |
3 | 3 | import { TaskList } from "@tiptap/extension-task-list"; |
4 | 4 | import { Node as ProseMirrorNode } from "@tiptap/pm/model"; |
5 | 5 | import { EditorState, TextSelection } from "@tiptap/pm/state"; |
6 | 6 | import { splitBlockKeepMarks } from "@tiptap/pm/commands"; |
7 | | -import { describe, expect, it } from "vite-plus/test"; |
| 7 | +import { describe, expect, it, onTestFinished } from "vite-plus/test"; |
8 | 8 |
|
9 | 9 | import { collapseExpandedComposerCursor, pastedPathQueryLength } from "./composer-logic"; |
10 | 10 |
|
@@ -1156,3 +1156,122 @@ describe("caret stops at styled edges", () => { |
1156 | 1156 | expect(caretTakesMarksBefore(stateAt("plain text", 1))).toBe(false); |
1157 | 1157 | }); |
1158 | 1158 | }); |
| 1159 | + |
| 1160 | +describe("inline code entry", () => { |
| 1161 | + function editorFor(block = "paragraph", mark?: string) { |
| 1162 | + const editor = new Editor({ |
| 1163 | + element: null, |
| 1164 | + extensions: [ |
| 1165 | + StarterKit.configure({ code: false, codeBlock: false, trailingNode: false }), |
| 1166 | + ComposerCodeExtension, |
| 1167 | + ComposerCodeBlockExtension, |
| 1168 | + stubAtom("composer-mention", { path: { default: "" }, source: { default: "" } }), |
| 1169 | + ], |
| 1170 | + content: { type: "doc", content: [{ type: block }] }, |
| 1171 | + }); |
| 1172 | + // Unmounted editors need the same plugins that Tiptap installs on mount. |
| 1173 | + editor.view.updateState(editor.state.reconfigure({ plugins: editor.extensionManager.plugins })); |
| 1174 | + if (mark) editor.commands.setMark(mark); |
| 1175 | + onTestFinished(() => editor.destroy()); |
| 1176 | + return editor; |
| 1177 | + } |
| 1178 | + |
| 1179 | + function type(editor: Editor, text: string) { |
| 1180 | + for (const ch of text) { |
| 1181 | + const { from, to } = editor.state.selection; |
| 1182 | + const insert = () => editor.state.tr.insertText(ch, from, to); |
| 1183 | + const handled = editor.state.plugins.some((plugin) => |
| 1184 | + plugin.props.handleTextInput?.call(plugin, editor.view, from, to, ch, insert), |
| 1185 | + ); |
| 1186 | + if (!handled) editor.view.dispatch(insert()); |
| 1187 | + } |
| 1188 | + } |
| 1189 | + |
| 1190 | + it.each(["", "prefix ", "@README.md"])( |
| 1191 | + "formats a filled pair after %j, leaves following text plain, and keeps the normal input rule", |
| 1192 | + (prefix) => { |
| 1193 | + const editor = editorFor(); |
| 1194 | + if (prefix === "@README.md") { |
| 1195 | + editor.commands.insertContent({ |
| 1196 | + type: "composer-mention", |
| 1197 | + attrs: { path: "README.md", source: prefix }, |
| 1198 | + }); |
| 1199 | + } else { |
| 1200 | + type(editor, prefix); |
| 1201 | + } |
| 1202 | + const offset = editor.state.selection.from - 1; |
| 1203 | + type(editor, "``"); |
| 1204 | + editor.commands.setTextSelection(offset + 2); |
| 1205 | + type(editor, "hello world"); |
| 1206 | + expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`hello world\``); |
| 1207 | + expect(editor.state.doc.firstChild?.lastChild?.marks).toEqual([]); |
| 1208 | + |
| 1209 | + editor.commands.setTextSelection(offset + 14); |
| 1210 | + expect(editor.state.doc.textContent).toBe( |
| 1211 | + `${prefix === "@README.md" ? "" : prefix}hello world`, |
| 1212 | + ); |
| 1213 | + expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}\`hello world\``); |
| 1214 | + type(editor, " next `typed`"); |
| 1215 | + expect(serializeEditorDoc(editor.state.doc).value).toBe( |
| 1216 | + `${prefix}\`hello world\` next \`typed\``, |
| 1217 | + ); |
| 1218 | + const formatted = editor.state.doc.toJSON(); |
| 1219 | + let undos = 0; |
| 1220 | + while (editor.commands.undo()) undos += 1; |
| 1221 | + expect(undos).toBeGreaterThan(0); |
| 1222 | + expect(serializeEditorDoc(editor.state.doc).value).toBe(""); |
| 1223 | + for (let i = 0; i < undos; i += 1) expect(editor.commands.redo()).toBe(true); |
| 1224 | + expect(editor.state.doc.toJSON()).toEqual(formatted); |
| 1225 | + }, |
| 1226 | + ); |
| 1227 | + |
| 1228 | + it("keeps composition edits literal until the caret leaves the typed pair", () => { |
| 1229 | + const editor = editorFor(); |
| 1230 | + type(editor, "``"); |
| 1231 | + editor.commands.setTextSelection(2); |
| 1232 | + editor.view.dispatch(editor.state.tr.insertText("世").setMeta("composition", 1)); |
| 1233 | + editor.view.dispatch(editor.state.tr.insertText("世界", 2, 3).setMeta("composition", 1)); |
| 1234 | + expect(editor.state.doc.textContent).toBe("`世界`"); |
| 1235 | + expect(editor.state.doc.firstChild?.firstChild?.marks).toEqual([]); |
| 1236 | + editor.commands.setTextSelection(5); |
| 1237 | + expect(editor.state.doc.textContent).toBe("世界"); |
| 1238 | + expect(serializeEditorDoc(editor.state.doc).value).toBe("`世界`"); |
| 1239 | + }); |
| 1240 | + |
| 1241 | + it.each(["paste", "setContent"])("keeps a %s pair literal after moving through it", (source) => { |
| 1242 | + const editor = editorFor(); |
| 1243 | + const literal = "`literal`"; |
| 1244 | + if (source === "paste") { |
| 1245 | + editor.view.dispatch(editor.state.tr.insertText(literal).setMeta("uiEvent", "paste")); |
| 1246 | + } else { |
| 1247 | + type(editor, "``"); |
| 1248 | + editor.commands.setContent({ |
| 1249 | + type: "doc", |
| 1250 | + content: [{ type: "paragraph", content: [{ type: "text", text: literal }] }], |
| 1251 | + }); |
| 1252 | + } |
| 1253 | + const before = editor.state.doc.toJSON(); |
| 1254 | + editor.commands.setTextSelection(2); |
| 1255 | + editor.commands.setTextSelection(literal.length + 1); |
| 1256 | + expect(editor.state.doc.toJSON()).toEqual(before); |
| 1257 | + }); |
| 1258 | + |
| 1259 | + it.each([ |
| 1260 | + { name: "doubled delimiters", delimiters: "````", caret: 3 }, |
| 1261 | + { name: "doubled closing delimiters", delimiters: "```", caret: 2 }, |
| 1262 | + { name: "bold text", delimiters: "``", caret: 2, mark: "bold" }, |
| 1263 | + { name: "existing inline code", delimiters: "``", caret: 2, mark: "code" }, |
| 1264 | + { name: "a code block", delimiters: "``", caret: 2, block: "codeBlock" }, |
| 1265 | + ])("leaves $name unchanged on caret movement", ({ delimiters, caret, mark, block }) => { |
| 1266 | + const editor = editorFor(block, mark); |
| 1267 | + type(editor, delimiters); |
| 1268 | + editor.commands.setTextSelection(caret); |
| 1269 | + type(editor, "literal"); |
| 1270 | + const before = editor.state.doc.toJSON(); |
| 1271 | + // Visit both the gap between closing ticks and the end of the pair. |
| 1272 | + editor.commands.setTextSelection(caret + "literal".length + 1); |
| 1273 | + expect(editor.state.doc.toJSON()).toEqual(before); |
| 1274 | + editor.commands.setTextSelection(editor.state.doc.content.size - 1); |
| 1275 | + expect(editor.state.doc.toJSON()).toEqual(before); |
| 1276 | + }); |
| 1277 | +}); |
0 commit comments