Skip to content

Commit 41c4b1f

Browse files
soorriaclaude
andcommitted
fix(web): composer drafts reload without an extra line after lists
The blank line written between a list or quote and the paragraph after it came back as an empty paragraph when a draft reloaded. The parser now drops it. The leading-space check looks at the first node, so a paragraph that starts with a chip still gets the blank line. When loading adds blank lines, the caret moves past them. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 45de109 commit 41c4b1f

5 files changed

Lines changed: 94 additions & 15 deletions

‎apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ it("keeps the caret inside unknown dollar text across rich and plain remounts",
239239
expect(serializeEditorDoc(editor.state.doc).value).toBe("echo $HOXYME now");
240240
});
241241

242+
it.each([
243+
["- item\npara", "- item\n\nparaX"],
244+
["> quote\npara", "> quote\n\nparaX"],
245+
])("keeps the caret at the end of %j when loading adds a blank line", async (value, typed) => {
246+
await using screen = await mountEditor(value);
247+
const { editor } = screen;
248+
await act(async () => {
249+
editor.commands.insertContent("X");
250+
});
251+
expect(serializeEditorDoc(editor.state.doc).value).toBe(typed);
252+
});
253+
242254
const file = {
243255
type: "file",
244256
id: "file-1",

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import {
8484
pmToFlat,
8585
serializeEditorDoc,
8686
serializeSelection,
87+
shiftPastAddedBlankLines,
8788
skillChipReplacements,
8889
splitOrLiftListItem,
8990
stepCaretAcrossStyledEdge,
@@ -1563,7 +1564,10 @@ function ComposerPromptEditorTiptapInner(
15631564
);
15641565
}
15651566
const map = serializeEditorDoc(editor.state.doc);
1566-
const flat = markdownToFlat(map, normalizedExpandedCursor);
1567+
const flat = markdownToFlat(
1568+
map,
1569+
shiftPastAddedBlankLines(value, map.value, normalizedExpandedCursor),
1570+
);
15671571
editor.commands.setTextSelection(flatToPm(map, flat));
15681572
if (isFocused) scrollTiptapCaretIntoView(editor);
15691573
if (pendingCitation) {
@@ -1634,7 +1638,12 @@ function ComposerPromptEditorTiptapInner(
16341638
const expandedCursor =
16351639
exactExpandedCursor ?? expandEditorCursor(snapshotRef.current.value, boundedCursor);
16361640
const map = serializeEditorDoc(editor.state.doc);
1637-
editor.commands.setTextSelection(flatToPm(map, markdownToFlat(map, expandedCursor)));
1641+
const markdownCursor = shiftPastAddedBlankLines(
1642+
snapshotRef.current.value,
1643+
map.value,
1644+
expandedCursor,
1645+
);
1646+
editor.commands.setTextSelection(flatToPm(map, markdownToFlat(map, markdownCursor)));
16381647
scrollTiptapCaretIntoView(editor);
16391648
if (boundedCursor === snapshotRef.current.cursor) return;
16401649
snapshotRef.current = {

‎apps/web/src/composer-list-split.browser.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function storedDraft(editor: Editor) {
6060

6161
describe("Backspace at the start of a line", () => {
6262
it.each([
63-
["- a\n```\nl1\nl2\n```", "- a\nl1\nl2"],
63+
["- a\n```\nl1\nl2\n```", "- a\n\nl1\nl2"],
6464
["p\n```ts\nl1\n```", "p\nl1"],
6565
])("removes the fence of %j rather than joining its code up", (value, expected) => {
6666
using editor = makeEditor(value);
@@ -95,6 +95,6 @@ describe("blocks that a list item or quote cannot hold", () => {
9595
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
9696
const lines = buildTiptapContent("one\ntwo", (name) => ({ label: name, description: null }));
9797
editor.commands.insertContent(lines);
98-
expect(storedDraft(editor)).toBe("- a\none\ntwo");
98+
expect(storedDraft(editor)).toBe("- a\n\none\ntwo");
9999
});
100100
});

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,16 @@ describe("composer rich text document model", () => {
592592
["1. one\n2. two\nafter", "1. one\n2. two\n\nafter"],
593593
["- [ ] task\nafter", "- [ ] task\n\nafter"],
594594
["- a\n - b\nafter", "- a\n - b\n\nafter"],
595-
])("separates %j from the list, or Markdown folds it into the last item", (glued, settled) => {
595+
["- item\[email protected] please", "- item\n\[email protected] please"],
596+
["> quoted\nafter", "> quoted\n\nafter"],
597+
])("separates %j from the block above, which Markdown would fold it into", (glued, settled) => {
596598
expect(roundTrip(glued).value).toBe(settled);
597599
expect(roundTrip(settled).value).toBe(settled);
600+
const blockTypes = (value: string) =>
601+
buildDocJson(value, (name) => ({ label: name, description: null })).content.map(
602+
(block) => block.type,
603+
);
604+
expect(blockTypes(settled)).toEqual(blockTypes(glued));
598605
});
599606

600607
it.each([

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

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ export function buildTiptapContent(
770770
| { rule: string }
771771
| { heading: { level: number; space: string; inline: InlineJson[] } }
772772
| { quote: { prefix: string; inline: InlineJson[] } }
773-
| { line: DocLine }
773+
| { line: DocLine; source: string }
774774
)[] = [];
775775
const restoreSources = (line: string) =>
776776
line.split(sentinel).reduce((joined, piece, index) => {
@@ -784,6 +784,7 @@ export function buildTiptapContent(
784784
if (index === 0 && literal) {
785785
entries.push({
786786
line: { list: null, inline: [textJsonForSpan(literal, []), ...buildInline(line)] },
787+
source: literal + line,
787788
});
788789
continue;
789790
}
@@ -806,7 +807,7 @@ export function buildTiptapContent(
806807
}
807808
const quote = blockSyntax ? /^(>[ \t]*)([^\n]*)$/.exec(line) : null;
808809
if (quote) entries.push({ quote: { prefix: quote[1]!, inline: buildInline(quote[2]!) } });
809-
else entries.push({ line: buildDocLine(line) });
810+
else entries.push({ line: buildDocLine(line), source: line });
810811
continue;
811812
}
812813
const language = restoreSources(opening.language);
@@ -857,7 +858,23 @@ export function buildTiptapContent(
857858
});
858859
openQuote = null;
859860
};
860-
for (const entry of entries) {
861+
const lazyTarget = (entry: (typeof entries)[number] | undefined) =>
862+
!entry ? null : "quote" in entry ? "quote" : "line" in entry && entry.line.list ? "list" : null;
863+
for (const [index, entry] of entries.entries()) {
864+
const next = entries[index + 1];
865+
if (
866+
"line" in entry &&
867+
entry.source === "" &&
868+
next &&
869+
"line" in next &&
870+
!next.line.list &&
871+
needsBlankLineBetween(lazyTarget(entries[index - 1]), {
872+
empty: next.source === "",
873+
indented: /^[ \t]/.test(next.source),
874+
})
875+
) {
876+
continue;
877+
}
861878
if ("quote" in entry) {
862879
flushLists();
863880
if (openQuote && openQuote.prefix !== entry.quote.prefix) flushQuote();
@@ -1004,12 +1021,37 @@ function pushBreakRun(acc: RichAccumulator, position?: number): void {
10041021
acc.md += 1;
10051022
}
10061023

1007-
// Separate prose from quotes and lists to avoid lazy continuation; an empty paragraph already
1008-
// separates them. An indented line after a list continues the item on purpose.
1009-
function needsBlankLineBeforeParagraph(previous: ProseMirrorNode, block: ProseMirrorNode): boolean {
1010-
if (block.type.name !== "paragraph" || block.content.size === 0) return false;
1011-
if (previous.type.name === "blockquote") return true;
1012-
return LIST_NODE_NAMES.has(previous.type.name) && !/^[ \t]/.test(block.textContent);
1024+
// Markdown folds a paragraph line into the quote or list right above it, so the serializer writes
1025+
// a blank line between them and the parser drops it. An empty paragraph is that line already, and
1026+
// an indented line after a list continues the item on purpose.
1027+
function needsBlankLineBetween(
1028+
previous: "quote" | "list" | null,
1029+
paragraph: { empty: boolean; indented: boolean },
1030+
): boolean {
1031+
if (!previous || paragraph.empty) return false;
1032+
return previous === "quote" || !paragraph.indented;
1033+
}
1034+
1035+
function lazyContinuationTarget(block: ProseMirrorNode): "quote" | "list" | null {
1036+
if (block.type.name === "blockquote") return "quote";
1037+
return LIST_NODE_NAMES.has(block.type.name) ? "list" : null;
1038+
}
1039+
1040+
// Loading a draft can add the blank line Markdown needs after a quote or list. Moves an offset in
1041+
// `value` to the same text in `serialized`.
1042+
export function shiftPastAddedBlankLines(
1043+
value: string,
1044+
serialized: string,
1045+
offset: number,
1046+
): number {
1047+
if (value === serialized) return offset;
1048+
let shift = 0;
1049+
for (let index = 0; index < offset; index += 1) {
1050+
while (value[index] !== serialized[index + shift] && serialized[index + shift] === "\n") {
1051+
shift += 1;
1052+
}
1053+
}
1054+
return Math.min(serialized.length, offset + shift);
10131055
}
10141056

10151057
// Blank-line runs own no document characters; offsets clamp to the paragraph start.
@@ -1455,7 +1497,16 @@ function serializeDoc(doc: ProseMirrorNode): RichDocMap {
14551497
let pmBlockStart = 0;
14561498
blocks.forEach((block, blockIndex) => {
14571499
if (blockIndex > 0 && !isTrailingLineAfterRule(doc, blockIndex)) pushBreakRun(acc);
1458-
if (blockIndex > 0 && needsBlankLineBeforeParagraph(blocks[blockIndex - 1]!, block)) {
1500+
const first = block.firstChild;
1501+
if (
1502+
blockIndex > 0 &&
1503+
block.type.name === "paragraph" &&
1504+
needsBlankLineBetween(lazyContinuationTarget(blocks[blockIndex - 1]!), {
1505+
empty: !first,
1506+
// Checks the first node, not textContent, which skips a leading chip.
1507+
indented: Boolean(first?.isText && !first.marks.length && /^[ \t]/.test(first.text!)),
1508+
})
1509+
) {
14591510
pushBlankLineRun(acc, pmBlockStart + 1);
14601511
}
14611512
if (LIST_NODE_NAMES.has(block.type.name)) {

0 commit comments

Comments
 (0)