Skip to content

Commit 8aa5736

Browse files
committed
fix(web): markdown pasted right after a file chip is still styled
A mention chip serializes to the same @path text as a typed query, so a paste with the caret directly after a chip counted as continuing a path query and stayed literal. The query now has to be plain text directly before the caret.
1 parent f15794c commit 8aa5736

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,9 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
10721072
) {
10731073
if (offset > 0 && !/\s/.test(map.value[offset - 1]!)) text = ` ${text}`;
10741074
}
1075-
const literalLength = pastedPathQueryLength(map.value, offset, text);
1075+
const typed = view.state.selection.$from.nodeBefore;
1076+
const typedLength = typed?.isText ? typed.text!.length : 0;
1077+
const literalLength = pastedPathQueryLength(map.value, offset, text, typedLength);
10761078
const editorInstance = editorHolder.current;
10771079
if (editorInstance) {
10781080
const options = { styling: richText, literalLength };

‎apps/web/src/composer-logic.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,19 @@ export function detectComposerTrigger(text: string, cursorInput: number): Compos
291291
};
292292
}
293293

294-
/** Length of the leading part of `pasted` that continues an `@` path query at `cursor`. */
295-
export function pastedPathQueryLength(text: string, cursor: number, pasted: string): number {
296-
if (detectComposerTrigger(text, cursor)?.kind !== "path") return 0;
294+
/**
295+
* Length of the leading part of `pasted` that continues an `@` path query at
296+
* `cursor`. `typedLength` is the plain text directly before the cursor: a
297+
* query is typed text, while a mention chip serializes to the same `@path`.
298+
*/
299+
export function pastedPathQueryLength(
300+
text: string,
301+
cursor: number,
302+
pasted: string,
303+
typedLength: number,
304+
): number {
305+
const trigger = detectComposerTrigger(text, cursor);
306+
if (trigger?.kind !== "path" || cursor - trigger.rangeStart > typedLength) return 0;
297307
return /^\S*/.exec(pasted)![0].length;
298308
}
299309

‎apps/web/src/composer-rich-text-doc.test.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getSchemaByResolvedExtensions, Node, resolveExtensions } from "@tiptap/core";
22
import StarterKit from "@tiptap/starter-kit";
33
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";
55
import { EditorState, TextSelection } from "@tiptap/pm/state";
66
import { describe, expect, it } from "vite-plus/test";
77

@@ -370,12 +370,20 @@ describe("composer rich text document model", () => {
370370
describe("pasting into a path query", () => {
371371
const skillLabelFor = (name: string) => ({ label: name, description: null });
372372

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);
376384
const blocks = buildTiptapContent(pasted, skillLabelFor, { literalLength });
377385
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);
379387
const doc = schema.node("doc", null, [first!.copy(head), ...rest]);
380388
doc.check();
381389
return doc;
@@ -414,6 +422,20 @@ describe("pasting into a path query", () => {
414422
expect(boldText(paste("Open @a ", "__b__"))).toEqual(["b"]);
415423
expect(boldText(paste("Open @", " __b__"))).toEqual(["b"]);
416424
});
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+
});
417439
});
418440

419441
describe("caret stops at styled edges", () => {

0 commit comments

Comments
 (0)