Skip to content

Commit 89b7b52

Browse files
fix(web): join converted list lines with neighbouring lists
A line converted into a list next to a compatible list now joins it, so the editor matches the Markdown it saves. Pending conversions also flush after an IME composition ends instead of waiting for the next edit. Drops the evidence driver script. AI-assisted; verified with the tests and tools listed above.
1 parent 764a296 commit 89b7b52

3 files changed

Lines changed: 79 additions & 712 deletions

File tree

‎apps/web/src/composer-block-shortcuts.test.ts‎

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { closeHistory, redo, undo } from "@tiptap/pm/history";
33
import { splitBlock } from "@tiptap/pm/commands";
44
import { TextSelection } from "@tiptap/pm/state";
55
import StarterKit from "@tiptap/starter-kit";
6-
import { describe, expect, it, onTestFinished } from "vite-plus/test";
6+
import { describe, expect, it, onTestFinished, vi } from "vite-plus/test";
77

88
import {
99
buildDocJson,
@@ -62,6 +62,14 @@ function createEditor(value: string) {
6262
return editor;
6363
}
6464

65+
function expectViewMatchesMarkdown(editor: Editor) {
66+
const markdown = serializeEditorDoc(editor.state.doc).value;
67+
const rebuilt = editor.schema.nodeFromJSON(
68+
buildDocJson(markdown, (name) => ({ label: name, description: null })),
69+
);
70+
expect(editor.state.doc.toString()).toBe(rebuilt.toString());
71+
}
72+
6573
function press(value: string, spec: string) {
6674
const editor = createEditor(value);
6775
const event = chord(spec);
@@ -115,27 +123,32 @@ describe("composer list normalization", () => {
115123
expect(editor.state.doc.firstChild?.type.name).toBe("bulletList");
116124
expect(editor.state.doc.firstChild?.firstChild?.textContent).toBe("get md");
117125
expect(serializeEditorDoc(editor.state.doc).value).toBe("- get md\n");
118-
expect(
119-
editor.state.doc.eq(
120-
editor.schema.nodeFromJSON(
121-
buildDocJson(serializeEditorDoc(editor.state.doc).value, (name) => ({
122-
label: name,
123-
description: null,
124-
})),
125-
),
126-
),
127-
).toBe(true);
126+
expectViewMatchesMarkdown(editor);
128127
});
129128

130-
it("converts a dash and space typed before existing text", () => {
131-
const editor = createEditor("existing text");
132-
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atStart(editor.state.doc)));
133-
editor.view.dispatch(editor.state.tr.insertText("-"));
134-
expect(editor.state.doc.firstChild?.type.name).toBe("paragraph");
135-
editor.view.dispatch(editor.state.tr.insertText(" "));
136-
expect(editor.state.doc.firstChild?.type.name).toBe("bulletList");
137-
expect(editor.state.selection.$from.parentOffset).toBe(0);
138-
expect(serializeEditorDoc(editor.state.doc).value).toBe("- existing text");
129+
it.each([
130+
["- a\nb\n- c", "- "],
131+
["* a\nb", "* "],
132+
["1. a\nb", "2. "],
133+
["- [ ] a\nb", "- [ ] "],
134+
])("joins the converted line into the neighbouring list in %j", (value, marker) => {
135+
const editor = createEditor(value);
136+
let paragraph = 0;
137+
editor.state.doc.forEach((node, pos) => {
138+
if (node.type.name === "paragraph") paragraph = pos + 1;
139+
});
140+
editor.view.dispatch(editor.state.tr.insertText(marker, paragraph));
141+
expect(editor.state.doc.childCount).toBe(1);
142+
expectViewMatchesMarkdown(editor);
143+
});
144+
145+
it("keeps a converted line apart from a list with another marker", () => {
146+
const editor = createEditor("* a\nb");
147+
editor.view.dispatch(
148+
editor.state.tr.insertText("- ", editor.state.doc.firstChild!.nodeSize + 1),
149+
);
150+
expect(editor.state.doc.childCount).toBe(2);
151+
expectViewMatchesMarkdown(editor);
139152
});
140153

141154
it.each(["[link](url)", "[ordinary text]", "[x]text"])(
@@ -192,12 +205,6 @@ describe("composer list normalization", () => {
192205
expect(editor.state.doc.firstChild?.firstChild?.attrs.checked).toBe(true);
193206
});
194207

195-
it("normalizes pasted markers without waiting for another keystroke", () => {
196-
const editor = createEditor("existing text");
197-
editor.view.dispatch(editor.state.tr.insertText("- ", 1).setMeta("uiEvent", "paste"));
198-
expect(editor.state.doc.firstChild?.type.name).toBe("bulletList");
199-
});
200-
201208
it.each(["```\ncode\n```", "- outer\n - inner", "> quoted"])(
202209
"does not convert paragraphs inside %j",
203210
(value) => {
@@ -228,7 +235,18 @@ describe("composer list normalization", () => {
228235
expect(editor.state.doc.firstChild?.type.name).toBe("paragraph");
229236
editor.view.dispatch(editor.state.tr.insertText("確定", 3, 6).setMeta("composition", 1));
230237
expect(editor.state.doc.firstChild?.type.name).toBe("paragraph");
231-
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
238+
vi.useFakeTimers();
239+
onTestFinished(() => {
240+
vi.useRealTimers();
241+
});
242+
for (const plugin of editor.state.plugins) {
243+
plugin.props.handleDOMEvents?.compositionend?.call(
244+
plugin,
245+
editor.view,
246+
new Event("compositionend") as CompositionEvent,
247+
);
248+
}
249+
vi.runAllTimers();
232250
expect(editor.state.doc.firstChild?.type.name).toBe("bulletList");
233251
expect(serializeEditorDoc(editor.state.doc).value).toBe("- 確定");
234252
expect(undo(editor.state, editor.view.dispatch)).toBe(true);

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
mergeAttributes,
3030
} from "@tiptap/core";
3131
import { isHistoryTransaction } from "@tiptap/pm/history";
32-
import { findWrapping } from "@tiptap/pm/transform";
32+
import { canJoin, findWrapping } from "@tiptap/pm/transform";
3333
import { BulletList, ListItem, OrderedList } from "@tiptap/extension-list";
3434
import { Strike, inputRegex as strikeInputRegex } from "@tiptap/extension-strike";
3535
import { TaskItem } from "@tiptap/extension-task-item";
@@ -629,6 +629,19 @@ function listNormalizationPlugin(isComposing: () => boolean) {
629629
const key = new PluginKey("composerListNormalization");
630630
return new Plugin({
631631
key,
632+
props: {
633+
handleDOMEvents: {
634+
// ProseMirror tags the DOM flush after compositionend as composition, so nothing else would normalize.
635+
compositionend(view) {
636+
setTimeout(() => {
637+
if (!view.isDestroyed && !view.composing && pending.size > 0) {
638+
view.dispatch(view.state.tr);
639+
}
640+
});
641+
return false;
642+
},
643+
},
644+
},
632645
appendTransaction(transactions, oldState, state) {
633646
if (transactions.some(isHistoryTransaction)) {
634647
pending.clear();
@@ -711,13 +724,33 @@ function listNormalizationPlugin(isComposing: () => boolean) {
711724
: { indent: prefix.indent, marker: prefix.marker, space: prefix.space },
712725
);
713726
tr.delete(pos + 3, pos + 3 + markerLength);
727+
joinCompatibleLists(tr, pos + tr.doc.nodeAt(pos)!.nodeSize);
728+
joinCompatibleLists(tr, pos);
714729
}
715730
pending = new Set([...pending].map((pos) => tr.mapping.map(pos, 1)));
716731
return tr.docChanged ? tr : null;
717732
},
718733
});
719734
}
720735

736+
/** Rebuilding from Markdown merges adjacent lists of one type whose markers share a delimiter. */
737+
function joinCompatibleLists(tr: Transaction, pos: number): void {
738+
const $pos = tr.doc.resolve(pos);
739+
const before = $pos.nodeBefore;
740+
const after = $pos.nodeAfter;
741+
const delimiter = (list: ProseMirrorNode) =>
742+
String(list.firstChild?.attrs.marker ?? "").slice(-1);
743+
if (
744+
before &&
745+
after &&
746+
before.type === after.type &&
747+
delimiter(before) === delimiter(after) &&
748+
canJoin(tr.doc, pos)
749+
) {
750+
tr.join(pos);
751+
}
752+
}
753+
721754
function listKey(prefix: ListLinePrefix): string {
722755
if (prefix.kind === "task") return "task";
723756
if (prefix.kind === "ordered") return `ordered:${prefix.marker.slice(-1)}`;

0 commit comments

Comments
 (0)