|
| 1 | +import { getSchema } from "@tiptap/core"; |
| 2 | +import StarterKit from "@tiptap/starter-kit"; |
| 3 | +import { history, redo, undo } from "@tiptap/pm/history"; |
| 4 | +import { EditorState, TextSelection } from "@tiptap/pm/state"; |
| 5 | +import { describe, expect, it } from "vite-plus/test"; |
| 6 | + |
| 7 | +import { buildDocJson, ComposerCodeExtension, serializeEditorDoc } from "./composer-rich-text-doc"; |
| 8 | +import { createComposerTypingMarksPlugin } from "./composer-typing-marks"; |
| 9 | + |
| 10 | +const schema = getSchema([StarterKit.configure({ code: false }), ComposerCodeExtension]); |
| 11 | + |
| 12 | +function createState(value: string, cursor: number) { |
| 13 | + const doc = schema.nodeFromJSON(buildDocJson(value, () => ({ label: "", description: null }))); |
| 14 | + return EditorState.create({ |
| 15 | + doc, |
| 16 | + selection: TextSelection.create(doc, cursor), |
| 17 | + plugins: [createComposerTypingMarksPlugin(), history()], |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function move(state: EditorState, cursor: number) { |
| 22 | + return state.applyTransaction(state.tr.setSelection(TextSelection.create(state.doc, cursor))) |
| 23 | + .state; |
| 24 | +} |
| 25 | + |
| 26 | +function type(state: EditorState, text: string) { |
| 27 | + for (const character of text) |
| 28 | + state = state.applyTransaction(state.tr.insertText(character)).state; |
| 29 | + return state; |
| 30 | +} |
| 31 | + |
| 32 | +function value(state: EditorState) { |
| 33 | + return serializeEditorDoc(state.doc).value; |
| 34 | +} |
| 35 | + |
| 36 | +describe("composer typing marks after moving the caret", () => { |
| 37 | + it.each(["**b**", "*b*", "~~b~~", "`b`", "***b***", "**`b`**"])( |
| 38 | + "types plain text after navigating away from and back to %s", |
| 39 | + (markdown) => { |
| 40 | + let state = createState(markdown, 2); |
| 41 | + state = state.applyTransaction(state.tr.setStoredMarks([])).state; |
| 42 | + state = move(move(state, 1), 2); |
| 43 | + expect(value(type(state, " plain"))).toBe(`${markdown} plain`); |
| 44 | + }, |
| 45 | + ); |
| 46 | + |
| 47 | + it("covers caret placement after a controlled rewrite, including an unchanged position", () => { |
| 48 | + let state = createState("x", 2); |
| 49 | + const content = schema.nodeFromJSON( |
| 50 | + buildDocJson("**b**", () => ({ label: "", description: null })), |
| 51 | + ); |
| 52 | + state = state.applyTransaction( |
| 53 | + state.tr.replaceWith(0, state.doc.content.size, content.content), |
| 54 | + ).state; |
| 55 | + state = move(state, 2); |
| 56 | + expect(value(type(state, " plain"))).toBe("**b** plain"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("resets typing marks after a paste and subsequent navigation", () => { |
| 60 | + let state = createState("", 1); |
| 61 | + const content = schema.nodeFromJSON( |
| 62 | + buildDocJson("**b**", () => ({ label: "", description: null })), |
| 63 | + ); |
| 64 | + state = state.applyTransaction( |
| 65 | + state.tr.replaceSelectionWith(content.firstChild!, false).setMeta("uiEvent", "paste"), |
| 66 | + ).state; |
| 67 | + expect(value(type(state, " plain"))).toBe("**b** plain"); |
| 68 | + state = move(move(state, 1), 2); |
| 69 | + expect(value(type(state, " plain"))).toBe("**b** plain"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("continues the following span after pasting in the middle of a line", () => { |
| 73 | + let state = createState("tail", 1); |
| 74 | + state = state.applyTransaction( |
| 75 | + state.tr |
| 76 | + .insertText("b") |
| 77 | + .addMark(1, 2, schema.marks.bold!.create()) |
| 78 | + .setMeta("uiEvent", "paste"), |
| 79 | + ).state; |
| 80 | + expect(value(type(state, "xy"))).toBe("**b**xytail"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("resets typing marks after dropping formatted text", () => { |
| 84 | + let state = createState("", 1); |
| 85 | + state = state.applyTransaction( |
| 86 | + state.tr |
| 87 | + .insertText("b") |
| 88 | + .addMark(1, 2, schema.marks.bold!.create()) |
| 89 | + .setMeta("uiEvent", "drop"), |
| 90 | + ).state; |
| 91 | + expect(value(type(state, " plain"))).toBe("**b** plain"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("preserves formatting while typing inside a span", () => { |
| 95 | + const state = move(createState("**bold**", 5), 3); |
| 96 | + expect(value(type(state, "xy"))).toBe("**boxyld**"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("adopts following text marks at a boundary", () => { |
| 100 | + const state = move(createState("**b***tail*", 6), 2); |
| 101 | + expect(value(type(state, "xy"))).toBe("**b***xytail*"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("matches the opening marker when placing the caret before formatted text", () => { |
| 105 | + const state = move(createState("x **bold**", 7), 3); |
| 106 | + expect(value(type(state, "xy"))).toBe("x **xybold**"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("preserves explicit formatting on and off over several keystrokes", () => { |
| 110 | + let state = move(createState("**b**", 2), 2); |
| 111 | + state = state.applyTransaction(state.tr.addStoredMark(schema.marks.bold!.create())).state; |
| 112 | + state = type(state, "xy"); |
| 113 | + expect(value(state)).toBe("**bxy**"); |
| 114 | + state = state.applyTransaction(state.tr.removeStoredMark(schema.marks.bold!)).state; |
| 115 | + expect(value(type(state, " plain"))).toBe("**bxy** plain"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("preserves an explicit mark decision in a selection transaction", () => { |
| 119 | + let state = createState("**b**", 1); |
| 120 | + state = state.applyTransaction( |
| 121 | + state.tr |
| 122 | + .setSelection(TextSelection.create(state.doc, 2)) |
| 123 | + .addStoredMark(schema.marks.italic!.create()), |
| 124 | + ).state; |
| 125 | + expect(value(type(state, "xy"))).toBe("**b*xy***"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does not alter ranged selection replacement", () => { |
| 129 | + let state = createState("**bold**", 5); |
| 130 | + state = state.applyTransaction( |
| 131 | + state.tr.setSelection(TextSelection.create(state.doc, 2, 4)), |
| 132 | + ).state; |
| 133 | + expect(value(type(state, "x"))).toBe("**bxd**"); |
| 134 | + }); |
| 135 | + |
| 136 | + it("leaves normal typing transactions alone", () => { |
| 137 | + const state = createState("**b**", 2); |
| 138 | + expect(value(type(state, "xy"))).toBe("**bxy**"); |
| 139 | + expect(state.applyTransaction(state.tr.scrollIntoView()).transactions).toHaveLength(1); |
| 140 | + }); |
| 141 | + |
| 142 | + it("adds no undo step and preserves undo/redo content and selection", () => { |
| 143 | + let state = move(move(createState("**b**", 2), 1), 2); |
| 144 | + state = type(state, " plain"); |
| 145 | + expect( |
| 146 | + undo(state, (transaction) => { |
| 147 | + state = state.applyTransaction(transaction).state; |
| 148 | + }), |
| 149 | + ).toBe(true); |
| 150 | + expect(value(state)).toBe("**b**"); |
| 151 | + expect(state.selection.from).toBe(2); |
| 152 | + expect(value(type(state, " again"))).toBe("**b** again"); |
| 153 | + expect( |
| 154 | + redo(state, (transaction) => { |
| 155 | + state = state.applyTransaction(transaction).state; |
| 156 | + }), |
| 157 | + ).toBe(true); |
| 158 | + expect(value(state)).toBe("**b** plain"); |
| 159 | + expect(state.selection.from).toBe(8); |
| 160 | + }); |
| 161 | +}); |
0 commit comments