Skip to content

Commit 9c3c3ac

Browse files
fix(web): preserve styled literal composer list markers
1 parent 0eea928 commit 9c3c3ac

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function createEditor(value: string) {
5555
],
5656
content: buildDocJson(value, (name) => ({ label: name, description: null })),
5757
});
58+
// Headless editors need the plugins installed explicitly because there is no mounted view.
5859
editor.view.updateState(editor.state.reconfigure({ plugins: editor.extensionManager.plugins }));
5960
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
6061
onTestFinished(() => editor.destroy());
@@ -219,27 +220,15 @@ describe("composer list normalization", () => {
219220
},
220221
);
221222

222-
it("waits until composition ends before changing paragraph structure", async () => {
223+
it("waits for a non-composition transaction before changing paragraph structure", () => {
223224
const editor = createEditor("");
224225
editor.view.dispatch(editor.state.tr.insertText("- text").setMeta("composition", 1));
225226
expect(editor.state.doc.firstChild?.type.name).toBe("paragraph");
226227
editor.view.dispatch(editor.state.tr.insertText("日本語", 3, 7).setMeta("composition", 1));
227228
expect(editor.state.doc.firstChild?.type.name).toBe("paragraph");
228-
const normalization = editor.state.plugins.find(
229-
(plugin) => plugin.props.handleDOMEvents?.compositionend && plugin.spec.appendTransaction,
230-
)!;
231-
normalization.props.handleDOMEvents!.compositionend!.call(
232-
normalization,
233-
editor.view,
234-
new Event("compositionend") as CompositionEvent,
235-
);
236-
// ProseMirror flushes the final composition text in a microtask after this event.
237-
queueMicrotask(() => {
238-
editor.view.dispatch(editor.state.tr.insertText("確定", 3, 6).setMeta("composition", 1));
239-
});
240-
await Promise.resolve();
229+
editor.view.dispatch(editor.state.tr.insertText("確定", 3, 6).setMeta("composition", 1));
241230
expect(editor.state.doc.firstChild?.type.name).toBe("paragraph");
242-
await Promise.resolve();
231+
editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
243232
expect(editor.state.doc.firstChild?.type.name).toBe("bulletList");
244233
expect(serializeEditorDoc(editor.state.doc).value).toBe("- 確定");
245234
expect(undo(editor.state, editor.view.dispatch)).toBe(true);
@@ -263,7 +252,7 @@ describe("composer list normalization", () => {
263252

264253
it("preserves formatting and the caret when removing the literal prefix", () => {
265254
const editor = createEditor("**text**");
266-
editor.view.dispatch(editor.state.tr.insertText("- ", 1));
255+
editor.view.dispatch(editor.state.tr.insert(1, editor.schema.text("- ")));
267256
expect(editor.state.selection.$from.parent.type.name).toBe("paragraph");
268257
expect(editor.state.selection.$from.parentOffset).toBe(4);
269258
expect(serializeEditorDoc(editor.state.doc).value).toBe("- **text**");
@@ -276,6 +265,21 @@ describe("composer list normalization", () => {
276265
expect(serializeEditorDoc(editor.state.doc).value).toBe("`- text`");
277266
});
278267

268+
it.each(["**- x**", "*-* x", "~~- x~~", "a\n**1.** b"])(
269+
"preserves styled markers when restoring %j",
270+
(value) => {
271+
const editor = createEditor("");
272+
editor.commands.setContent(
273+
buildDocJson(value, (name) => ({ label: name, description: null })),
274+
{ emitUpdate: false },
275+
);
276+
expect(serializeEditorDoc(editor.state.doc).value).toBe(value);
277+
expect(editor.state.doc.lastChild?.type.name).toBe("paragraph");
278+
editor.view.dispatch(editor.state.tr.insertText("x", editor.state.doc.content.size - 1));
279+
expect(editor.state.doc.lastChild?.type.name).toBe("paragraph");
280+
},
281+
);
282+
279283
it("only normalizes changed blocks", () => {
280284
const editor = createEditor("");
281285
editor.view.updateState(editor.state.reconfigure({ plugins: [] }));

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -456,25 +456,12 @@ function listNormalizationPlugin(isComposing: () => boolean) {
456456
const key = new PluginKey("composerListNormalization");
457457
return new Plugin({
458458
key,
459-
props: {
460-
handleDOMEvents: {
461-
compositionend(view) {
462-
// ProseMirror queues its final DOM flush later in this event's compositionend handler.
463-
queueMicrotask(() =>
464-
queueMicrotask(() => {
465-
if (!view.isDestroyed && pending.size > 0)
466-
view.dispatch(view.state.tr.setMeta(key, true));
467-
}),
468-
);
469-
return false;
470-
},
471-
},
472-
},
473459
appendTransaction(transactions, oldState, state) {
474460
if (transactions.some(isHistoryTransaction)) {
475461
pending.clear();
476462
return null;
477463
}
464+
if (pending.size === 0 && !transactions.some((tr) => tr.docChanged)) return null;
478465
const changes = combineTransactionSteps(oldState.doc, [...transactions]);
479466
pending = new Set(
480467
[...pending].flatMap((pos) => {
@@ -488,6 +475,7 @@ function listNormalizationPlugin(isComposing: () => boolean) {
488475
return false;
489476
});
490477
}
478+
// The next non-composition transaction can normalize after the browser finishes its DOM edits.
491479
if (isComposing() || transactions.some((tr) => tr.getMeta("composition") != null))
492480
return null;
493481

@@ -499,10 +487,13 @@ function listNormalizationPlugin(isComposing: () => boolean) {
499487
if (node?.type.name !== "paragraph") continue;
500488
const text = node.textBetween(0, node.content.size, undefined, "\ufffc");
501489
const parsed = parseListPrefix(text);
490+
// Input rules only introduce unindented top-level lists; keep the same boundary here.
502491
if (!parsed || parsed.prefix.indent) continue;
503492
const { prefix, markerLength } = parsed;
504-
const code = state.schema.marks.code;
505-
if (code && node.rangeHasMark(0, markerLength, code)) continue;
493+
if (
494+
Object.values(state.schema.marks).some((mark) => node.rangeHasMark(0, markerLength, mark))
495+
)
496+
continue;
506497
const active = state.selection.$from.depth === 1 && state.selection.$from.before(1) === pos;
507498
const content = text.slice(markerLength);
508499
// Keep `- ` and unfinished task markers literal while the user is entering the gesture.

0 commit comments

Comments
 (0)