|
1 | 1 | import { getSchemaByResolvedExtensions, Node, resolveExtensions } from "@tiptap/core"; |
2 | 2 | import StarterKit from "@tiptap/starter-kit"; |
3 | 3 | import { TaskList } from "@tiptap/extension-task-list"; |
| 4 | +import { history, redo, undo, undoDepth } from "@tiptap/pm/history"; |
4 | 5 | import { Node as ProseMirrorNode } from "@tiptap/pm/model"; |
5 | 6 | import { EditorState, TextSelection } from "@tiptap/pm/state"; |
6 | 7 | import { describe, expect, it } from "vite-plus/test"; |
@@ -378,6 +379,81 @@ describe("caret stops at styled edges", () => { |
378 | 379 | return serializeEditorDoc(state.apply(state.tr.insertText(text)).doc).value; |
379 | 380 | } |
380 | 381 |
|
| 382 | + function typeCharacters(state: EditorState, text: string) { |
| 383 | + for (const character of text) state = state.apply(state.tr.insertText(character)); |
| 384 | + return state; |
| 385 | + } |
| 386 | + |
| 387 | + it("keeps typing plain after stepping outside pasted bold", () => { |
| 388 | + let state = stateAt("", 1); |
| 389 | + state = state.apply( |
| 390 | + state.tr |
| 391 | + .replaceSelectionWith(schema.text("bold", [schema.marks.bold!.create()]), false) |
| 392 | + .setMeta("uiEvent", "paste"), |
| 393 | + ); |
| 394 | + state = state.apply(stepCaretAcrossStyledEdge(state, 1)!); |
| 395 | + state = typeCharacters(state, " plain"); |
| 396 | + expect(serializeEditorDoc(state.doc).value).toBe("**bold** plain"); |
| 397 | + }); |
| 398 | + |
| 399 | + it("can step outside bold after cutting its trailing plain text", () => { |
| 400 | + let state = stateAt("**b** plain", 8); |
| 401 | + state = state.apply( |
| 402 | + state.tr |
| 403 | + .setSelection(TextSelection.create(state.doc, 2, 8)) |
| 404 | + .deleteRange(2, 8) |
| 405 | + .setMeta("uiEvent", "cut"), |
| 406 | + ); |
| 407 | + state = state.apply(stepCaretAcrossStyledEdge(state, 1)!); |
| 408 | + state = typeCharacters(state, " again"); |
| 409 | + expect(serializeEditorDoc(state.doc).value).toBe("**b** again"); |
| 410 | + }); |
| 411 | + |
| 412 | + it("keeps caret steps out of history and can recover either stop after undo", () => { |
| 413 | + let state = stateAt("**b**", 2).reconfigure({ plugins: [history()] }); |
| 414 | + state = state.apply(stepCaretAcrossStyledEdge(state, 1)!); |
| 415 | + expect(undoDepth(state)).toBe(0); |
| 416 | + state = typeCharacters(state, " plain"); |
| 417 | + expect(undoDepth(state)).toBe(1); |
| 418 | + |
| 419 | + expect( |
| 420 | + undo(state, (transaction) => { |
| 421 | + state = state.apply(transaction); |
| 422 | + }), |
| 423 | + ).toBe(true); |
| 424 | + expect(serializeEditorDoc(state.doc).value).toBe("**b**"); |
| 425 | + expect(state.selection.from).toBe(2); |
| 426 | + expect(undoDepth(state)).toBe(0); |
| 427 | + |
| 428 | + expect( |
| 429 | + redo(state, (transaction) => { |
| 430 | + state = state.apply(transaction); |
| 431 | + }), |
| 432 | + ).toBe(true); |
| 433 | + expect(serializeEditorDoc(state.doc).value).toBe("**b** plain"); |
| 434 | + expect(state.selection.from).toBe(8); |
| 435 | + |
| 436 | + expect( |
| 437 | + undo(state, (transaction) => { |
| 438 | + state = state.apply(transaction); |
| 439 | + }), |
| 440 | + ).toBe(true); |
| 441 | + expect(typed(state, "x")).toBe("**bx**"); |
| 442 | + state = state.apply(stepCaretAcrossStyledEdge(state, 1)!); |
| 443 | + expect(typed(state, "x")).toBe("**b**x"); |
| 444 | + }); |
| 445 | + |
| 446 | + it("sustains explicit formatting toggles across several characters", () => { |
| 447 | + let state = stateAt("**b**", 2); |
| 448 | + state = state.apply(stepCaretAcrossStyledEdge(state, 1)!); |
| 449 | + state = state.apply(state.tr.addStoredMark(schema.marks.bold!.create())); |
| 450 | + state = typeCharacters(state, "xy"); |
| 451 | + expect(serializeEditorDoc(state.doc).value).toBe("**bxy**"); |
| 452 | + state = state.apply(state.tr.removeStoredMark(schema.marks.bold!)); |
| 453 | + state = typeCharacters(state, " plain"); |
| 454 | + expect(serializeEditorDoc(state.doc).value).toBe("**bxy** plain"); |
| 455 | + }); |
| 456 | + |
381 | 457 | it("lets the caret step out in front of bold that starts the line", () => { |
382 | 458 | const inside = stateAt("**bold** tail", 1); |
383 | 459 | expect(caretTakesMarksBefore(inside)).toBe(false); |
|
0 commit comments