|
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 { Fragment, Node as ProseMirrorNode } from "@tiptap/pm/model"; |
| 4 | +import { Node as ProseMirrorNode } from "@tiptap/pm/model"; |
5 | 5 | import { EditorState, TextSelection } from "@tiptap/pm/state"; |
6 | 6 | import { describe, expect, it } from "vite-plus/test"; |
7 | 7 |
|
@@ -370,12 +370,20 @@ describe("composer rich text document model", () => { |
370 | 370 | describe("pasting into a path query", () => { |
371 | 371 | const skillLabelFor = (name: string) => ({ label: name, description: null }); |
372 | 372 |
|
373 | | - // Inserts `pasted` at the end of `prompt` the way the composer's paste handler does. |
374 | | - function paste(prompt: string, pasted: string) { |
375 | | - const literalLength = pastedPathQueryLength(prompt, prompt.length, pasted); |
| 373 | + // Inserts `pasted` at the end of a one-line prompt the way the composer's |
| 374 | + // paste handler does. `prompt` may be a document, to place the caret after a chip. |
| 375 | + function paste(prompt: string | ProseMirrorNode, pasted: string) { |
| 376 | + const promptDoc = |
| 377 | + typeof prompt === "string" |
| 378 | + ? ProseMirrorNode.fromJSON(schema, buildDocJson(prompt, skillLabelFor)) |
| 379 | + : prompt; |
| 380 | + const value = serializeEditorDoc(promptDoc).value; |
| 381 | + const typed = promptDoc.lastChild!.lastChild; |
| 382 | + const typedLength = typed?.isText ? typed.text!.length : 0; |
| 383 | + const literalLength = pastedPathQueryLength(value, value.length, pasted, typedLength); |
376 | 384 | const blocks = buildTiptapContent(pasted, skillLabelFor, { literalLength }); |
377 | 385 | const [first, ...rest] = blocks.map((block) => ProseMirrorNode.fromJSON(schema, block)); |
378 | | - const head = Fragment.from(schema.text(prompt)).append(first!.content); |
| 386 | + const head = promptDoc.lastChild!.content.append(first!.content); |
379 | 387 | const doc = schema.node("doc", null, [first!.copy(head), ...rest]); |
380 | 388 | doc.check(); |
381 | 389 | return doc; |
@@ -414,6 +422,20 @@ describe("pasting into a path query", () => { |
414 | 422 | expect(boldText(paste("Open @a ", "__b__"))).toEqual(["b"]); |
415 | 423 | expect(boldText(paste("Open @", " __b__"))).toEqual(["b"]); |
416 | 424 | }); |
| 425 | + |
| 426 | + it("parses markdown pasted right after a mention chip", () => { |
| 427 | + const withSpace = ProseMirrorNode.fromJSON( |
| 428 | + schema, |
| 429 | + buildDocJson("Open @README.md ", skillLabelFor), |
| 430 | + ); |
| 431 | + const paragraph = withSpace.firstChild!; |
| 432 | + expect(paragraph.lastChild!.text).toBe(" "); |
| 433 | + const chipLast = schema.node("doc", null, [ |
| 434 | + paragraph.copy(paragraph.content.cut(0, paragraph.content.size - 1)), |
| 435 | + ]); |
| 436 | + expect(chipLast.firstChild!.lastChild!.type.name).toBe("composer-mention"); |
| 437 | + expect(boldText(paste(chipLast, "__b__"))).toEqual(["b"]); |
| 438 | + }); |
417 | 439 | }); |
418 | 440 |
|
419 | 441 | describe("caret stops at styled edges", () => { |
|
0 commit comments