Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): stop swallowing mixed paste payloads into the target list item (#2786)

Nick Gomez2bc45d46f3af
diff --git a/.changeset/mixed-paste-list-swallow.md b/.changeset/mixed-paste-list-swallow.md
new file mode 100644
index 000000000..4f565bc7d
--- /dev/null
+++ b/.changeset/mixed-paste-list-swallow.md
@@ -0,0 +1,5 @@
+---
+'@inkeep/open-knowledge': patch
+---
+
+Pasting headings, paragraphs, or code blocks inside a list no longer swallows them into the list item. Mixed payloads now split the list at the caret: non-list blocks land as siblings of the list, and any leading/trailing pasted lists continue the list as item siblings. This also removes the stray blank lines (loose-list synthesis) and the degenerate double list marker some of these pastes produced.
diff --git a/packages/app/src/editor/clipboard/handle-paste.mixed-payload-placement.test.ts b/packages/app/src/editor/clipboard/handle-paste.mixed-payload-placement.test.ts
new file mode 100644
index 000000000..783492cae
--- /dev/null
+++ b/packages/app/src/editor/clipboard/handle-paste.mixed-payload-placement.test.ts
@@ -0,0 +1,396 @@
+/**
+ * Mixed-payload paste placement at a list-interior caret.
+ *
+ * Pasting a payload that is NOT 100% lists (heading+list, paragraph+list,
+ * code block+list, lone/multiple headings, list+heading) at a caret inside a
+ * list item must SPLIT the list at the caret and place the non-list blocks as
+ * siblings of the list — the placement typing them at top level would produce
+ * — while leading/trailing list runs in the payload splice as item siblings
+ * (list continuation), mirroring the all-list sibling-splice path. Nothing may
+ * be demoted into the target list item.
+ *
+ * Each test mounts a real TipTap editor over the core schema (real list
+ * nodes, real MarkdownManager) and drives the REAL createHandlePaste
+ * dispatcher, so the whole path runs: markdown-first tiebreak →
+ * MarkdownManager.parse → applyJsonSlice → buildListSiblingSpliceTr.
+ */
+
+import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';
+import { Editor, type JSONContent } from '@tiptap/core';
+import type { Node as ProseMirrorNode } from '@tiptap/pm/model';
+import { TextSelection } from '@tiptap/pm/state';
+import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest';
+import { installDomGlobals } from '../walk-currency-test-harness';
+
+vi.mock('sonner', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('sonner')>();
+ return { ...actual, toast: { error: vi.fn() } };
+});
+
+const mdManager = new MarkdownManager({ extensions: sharedExtensions });
+
+let createHandlePaste: typeof import('./handle-paste').createHandlePaste;
+
+let restoreDomGlobals: (() => void) | null = null;
+const editors: Editor[] = [];
+
+beforeAll(async () => {
+ restoreDomGlobals = installDomGlobals();
+ ({ createHandlePaste } = await import('./handle-paste'));
+});
+
+afterAll(() => {
+ restoreDomGlobals?.();
+ restoreDomGlobals = null;
+});
+
+afterEach(() => {
+ while (editors.length > 0) editors.pop()?.destroy();
+});
+
+function mountEditor(md: string): Editor {
+ const host = document.createElement('div');
+ document.body.appendChild(host);
+ const editor = new Editor({
+ element: host,
+ content: mdManager.parse(md) as JSONContent,
+ extensions: [...sharedExtensions],
+ });
+ editors.push(editor);
+ return editor;
+}
+
+type PasteFlavor = 'B' | 'E' | 'D';
+
+/** Drive the dispatcher with clipboard payloads shaped to hit a specific branch. */
+function paste(
+ editor: Editor,
+ flavor: PasteFlavor,
+ payload: { md?: string; html?: string },
+): boolean {
+ const plain = payload.md ?? '';
+ const html = payload.html ?? '';
+ const types: string[] = [];
+ if (flavor === 'B') types.push('text/plain', 'text/html');
+ if (flavor === 'E') types.push('text/plain');
+ if (flavor === 'D') types.push('text/html');
+ const dt = {
+ clipboardData: {
+ types,
+ getData: (k: string) => {
+ if (k === 'text/plain' && flavor !== 'D') return plain;
+ if (k === 'text/html' && flavor !== 'E') return html || '<ul><li>x</li></ul>';
+ return '';
+ },
+ },
+ } as unknown as ClipboardEvent;
+ return createHandlePaste({ mdManager })(editor.view, dt);
+}
+
+/** Doc position of the nth list item's first paragraph (0-based, doc order). */
+function itemParaRange(doc: ProseMirrorNode, n: number): { start: number; end: number } {
+ let i = 0;
+ let found: { start: number; end: number } | null = null;
+ doc.descendants((node, pos) => {
+ if (found) return false;
+ if (node.type.name === 'listItem') {
+ if (i === n) {
+ const para = node.firstChild;
+ const start = pos + 2;
+ found = { start, end: start + (para ? para.content.size : 0) };
+ return false;
+ }
+ i++;
+ }
+ return true;
+ });
+ if (!found) throw new Error(`listItem ${n} not found`);
+ return found;
+}
+
+function setCaret(editor: Editor, pos: number): void {
+ editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, pos)));
+}
+
+/** Where did the pasted heading land? */
+function headingPlacement(doc: ProseMirrorNode): 'top-level' | 'inside-list' | 'absent' {
+ let topLevel = false;
+ doc.forEach((node) => {
+ if (node.type.name === 'heading') topLevel = true;
+ });
+ if (topLevel) return 'top-level';
+ let insideList = false;
+ doc.descendants((node) => {
+ if (node.type.name === 'heading') {
+ insideList = true;
+ return false;
+ }
+ return true;
+ });
+ return insideList ? 'inside-list' : 'absent';
+}
+
+function serialize(editor: Editor): string {
+ return mdManager.serialize(editor.getJSON() as JSONContent);
+}
+
+function topLevelTypes(doc: ProseMirrorNode): string[] {
+ const names: string[] = [];
+ doc.forEach((node) => {
+ names.push(node.type.name);
+ });
+ return names;
+}
+
+const BASE = '- alpha\n- beta\n- gamma\n';
+
+type CaretLabel =
+ | 'first-item-start'
+ | 'first-item-mid'
+ | 'first-item-end'
+ | 'mid-item-mid'
+ | 'last-item-end';
+
+const CARETS: Array<{ label: CaretLabel; item: number; at: 'start' | 'mid' | 'end' }> = [
+ { label: 'first-item-start', item: 0, at: 'start' },
+ { label: 'first-item-mid', item: 0, at: 'mid' },
+ { label: 'first-item-end', item: 0, at: 'end' },
+ { label: 'mid-item-mid', item: 1, at: 'mid' },
+ { label: 'last-item-end', item: 2, at: 'end' },
+];
+
+interface MixedCase {
+ label: string;
+ payloadMd: string;
+ /** Expected serialized doc (trimEnd-compared) per caret position. */
+ expected: Record<CaretLabel, string>;
+ /** Expected top-level node types replacing the original list. */
+ segments: Record<CaretLabel, string[]>;
+}
+
+const MIXED_PAYLOADS: MixedCase[] = [
+ {
+ label: 'heading+list',
+ payloadMd: '## Section\n\n- one\n- two\n',
+ expected: {
+ 'first-item-start': '## Section\n\n- one\n- two\n- alpha\n- beta\n- gamma',
+ 'first-item-mid': '- al\n\n## Section\n\n- one\n- two\n- pha\n- beta\n- gamma',
+ 'first-item-end': '- alpha\n\n## Section\n\n- one\n- two\n- beta\n- gamma',
+ 'mid-item-mid': '- alpha\n- be\n\n## Section\n\n- one\n- two\n- ta\n- gamma',
+ 'last-item-end': '- alpha\n- beta\n- gamma\n\n## Section\n\n- one\n- two',
+ },
+ segments: {
+ 'first-item-start': ['heading', 'list'],
+ 'first-item-mid': ['list', 'heading', 'list'],
+ 'first-item-end': ['list', 'heading', 'list'],
+ 'mid-item-mid': ['list', 'heading', 'list'],
+ 'last-item-end': ['list', 'heading', 'list'],
+ },
+ },
+ {
+ label: 'paragraph+list',
+ payloadMd: 'Intro para\n\n- one\n- two\n',
+ expected: {
+ 'first-item-start': 'Intro para\n\n- one\n- two\n- alpha\n- beta\n- gamma',
+ 'first-item-mid': '- al\n\nIntro para\n\n- one\n- two\n- pha\n- beta\n- gamma',
+ 'first-item-end': '- alpha\n\nIntro para\n\n- one\n- two\n- beta\n- gamma',
+ 'mid-item-mid': '- alpha\n- be\n\nIntro para\n\n- one\n- two\n- ta\n- gamma',
+ 'last-item-end': '- alpha\n- beta\n- gamma\n\nIntro para\n\n- one\n- two',
+ },
+ segments: {
+ 'first-item-start': ['paragraph', 'list'],
+ 'first-item-mid': ['list', 'paragraph', 'list'],
+ 'first-item-end': ['list', 'paragraph', 'list'],
+ 'mid-item-mid': ['list', 'paragraph', 'list'],
+ 'last-item-end': ['list', 'paragraph', 'list'],
+ },
+ },
+ {
+ label: 'list+heading',
+ payloadMd: '- one\n- two\n\n## Section\n',
+ expected: {
+ 'first-item-start': '- one\n- two\n\n## Section\n\n- alpha\n- beta\n- gamma',
+ 'first-item-mid': '- al\n- one\n- two\n\n## Section\n\n- pha\n- beta\n- gamma',
+ 'first-item-end': '- alpha\n- one\n- two\n\n## Section\n\n- beta\n- gamma',
+ 'mid-item-mid': '- alpha\n- be\n- one\n- two\n\n## Section\n\n- ta\n- gamma',
+ 'last-item-end': '- alpha\n- beta\n- gamma\n- one\n- two\n\n## Section',
+ },
+ segments: {
+ 'first-item-start': ['list', 'heading', 'list'],
+ 'first-item-mid': ['list', 'heading', 'list'],
+ 'first-item-end': ['list', 'heading', 'list'],
+ 'mid-item-mid': ['list', 'heading', 'list'],
+ 'last-item-end': ['list', 'heading'],
+ },
+ },
+ {
+ label: 'two-headings',
+ payloadMd: '## A\n\n## B\n',
+ expected: {
+ 'first-item-start': '## A\n\n## B\n\n- alpha\n- beta\n- gamma',
+ 'first-item-mid': '- al\n\n## A\n\n## B\n\n- pha\n- beta\n- gamma',
+ 'first-item-end': '- alpha\n\n## A\n\n## B\n\n- beta\n- gamma',
+ 'mid-item-mid': '- alpha\n- be\n\n## A\n\n## B\n\n- ta\n- gamma',
+ 'last-item-end': '- alpha\n- beta\n- gamma\n\n## A\n\n## B',
+ },
+ segments: {
+ 'first-item-start': ['heading', 'heading', 'list'],
+ 'first-item-mid': ['list', 'heading', 'heading', 'list'],
+ 'first-item-end': ['list', 'heading', 'heading', 'list'],
+ 'mid-item-mid': ['list', 'heading', 'heading', 'list'],
+ 'last-item-end': ['list', 'heading', 'heading'],
+ },
+ },
+ {
+ label: 'codeblock+list',
+ payloadMd: '```js\ncode();\n```\n\n- one\n',
+ expected: {
+ 'first-item-start': '```js\ncode();\n```\n\n- one\n- alpha\n- beta\n- gamma',
+ 'first-item-mid': '- al\n\n```js\ncode();\n```\n\n- one\n- pha\n- beta\n- gamma',
+ 'first-item-end': '- alpha\n\n```js\ncode();\n```\n\n- one\n- beta\n- gamma',
+ 'mid-item-mid': '- alpha\n- be\n\n```js\ncode();\n```\n\n- one\n- ta\n- gamma',
+ 'last-item-end': '- alpha\n- beta\n- gamma\n\n```js\ncode();\n```\n\n- one',
+ },
+ segments: {
+ 'first-item-start': ['codeBlock', 'list'],
+ 'first-item-mid': ['list', 'codeBlock', 'list'],
+ 'first-item-end': ['list', 'codeBlock', 'list'],
+ 'mid-item-mid': ['list', 'codeBlock', 'list'],
+ 'last-item-end': ['list', 'codeBlock', 'list'],
+ },
+ },
+ {
+ label: 'lone-heading',
+ payloadMd: '## Section\n',
+ expected: {
+ 'first-item-start': '## Section\n\n- alpha\n- beta\n- gamma',
+ 'first-item-mid': '- al\n\n## Section\n\n- pha\n- beta\n- gamma',
+ 'first-item-end': '- alpha\n\n## Section\n\n- beta\n- gamma',
+ 'mid-item-mid': '- alpha\n- be\n\n## Section\n\n- ta\n- gamma',
+ 'last-item-end': '- alpha\n- beta\n- gamma\n\n## Section',
+ },
+ segments: {
+ 'first-item-start': ['heading', 'list'],
+ 'first-item-mid': ['list', 'heading', 'list'],
+ 'first-item-end': ['list', 'heading', 'list'],
+ 'mid-item-mid': ['list', 'heading', 'list'],
+ 'last-item-end': ['list', 'heading'],
+ },
+ },
+];
+
+function caretPos(editor: Editor, item: number, at: 'start' | 'mid' | 'end'): number {
+ const r = itemParaRange(editor.state.doc, item);
+ return at === 'start' ? r.start : at === 'end' ? r.end : Math.floor((r.start + r.end) / 2);
+}
+
+describe('matrix: mixed payload x caret position (Branch B markdown-first)', () => {
+ for (const payload of MIXED_PAYLOADS) {
+ for (const caret of CARETS) {
+ test(`${payload.label} @ ${caret.label}`, () => {
+ const editor = mountEditor(BASE);
+ setCaret(editor, caretPos(editor, caret.item, caret.at));
+ const topLevelBefore = topLevelTypes(editor.state.doc);
+ expect(paste(editor, 'B', { md: payload.payloadMd })).toBe(true);
+ // The original single top-level list is replaced by the expected
+ // segment sequence; anything after it (e.g. a trailing empty
+ // paragraph from parsing BASE) is untouched.
+ expect(topLevelTypes(editor.state.doc)).toEqual([
+ ...payload.segments[caret.label],
+ ...topLevelBefore.slice(1),
+ ]);
+ if (payload.payloadMd.includes('##')) {
+ expect(headingPlacement(editor.state.doc)).toBe('top-level');
+ }
+ expect(serialize(editor).trimEnd()).toBe(payload.expected[caret.label]);
+ });
+ }
+ }
+});
+
+describe('all-list control keeps the #609 sibling splice', () => {
+ for (const caret of CARETS) {
+ test(`all-list @ ${caret.label}`, () => {
+ const editor = mountEditor(BASE);
+ setCaret(editor, caretPos(editor, caret.item, caret.at));
+ const topLevelBefore = topLevelTypes(editor.state.doc);
+ expect(paste(editor, 'B', { md: '- one\n- two\n' })).toBe(true);
+ // Pure-list payloads splice as item siblings inside the original list;
+ // nothing escapes to doc level (exact shapes are pinned by
+ // handle-paste.list-placement.test.ts).
+ expect(topLevelTypes(editor.state.doc)).toEqual(topLevelBefore);
+ });
+ }
+});
+
+describe('paste flavor variation (heading+list @ mid-item-mid)', () => {
+ const HTML = '<h2>Section</h2><ul><li>one</li><li>two</li></ul>';
+ const EXPECTED = '- alpha\n- be\n\n## Section\n\n- one\n- two\n- ta\n- gamma';
+ for (const flavor of ['B', 'E', 'D'] as const) {
+ test(`flavor ${flavor}`, () => {
+ const editor = mountEditor(BASE);
+ setCaret(editor, caretPos(editor, 1, 'mid'));
+ expect(paste(editor, flavor, { md: '## Section\n\n- one\n- two\n', html: HTML })).toBe(true);
+ // B, E, and D all funnel through applyJsonSlice, so the placement fix
+ // applies uniformly across flavors.
+ expect(headingPlacement(editor.state.doc)).toBe('top-level');
+ expect(serialize(editor).trimEnd()).toBe(EXPECTED);
+ });
+ }
+});
+
+describe('nested caret escapes to doc level', () => {
+ test('heading+list at a nested-item caret', () => {
+ const editor = mountEditor('- alpha\n - child\n- beta\n');
+ // item index 1 is the nested "child" item in doc order
+ setCaret(editor, caretPos(editor, 1, 'mid'));
+ expect(paste(editor, 'B', { md: '## Section\n\n- one\n' })).toBe(true);
+ // The heading escapes the whole nesting stack to doc level. The remainder
+ // of the split nested item ("ild") lifts to the top list level rather
+ // than minting an empty parent bullet.
+ expect(headingPlacement(editor.state.doc)).toBe('top-level');
+ expect(serialize(editor).trimEnd()).toBe(
+ '- alpha\n - ch\n\n## Section\n\n- one\n- ild\n- beta',
+ );
+ });
+
+ test('lone heading at a nested-item caret', () => {
+ const editor = mountEditor('- alpha\n - child\n- beta\n');
+ setCaret(editor, caretPos(editor, 1, 'mid'));
+ expect(paste(editor, 'B', { md: '## Section\n' })).toBe(true);
+ expect(headingPlacement(editor.state.doc)).toBe('top-level');
+ expect(serialize(editor).trimEnd()).toBe('- alpha\n - ch\n\n## Section\n\n- ild\n- beta');
+ });
+});
+
+describe('controls', () => {
+ test('same payload at a top-level paragraph caret places heading top-level', () => {
+ const editor = mountEditor('plain intro\n');
+ setCaret(editor, 3);
+ expect(paste(editor, 'B', { md: '## Section\n\n- one\n- two\n' })).toBe(true);
+ expect(headingPlacement(editor.state.doc)).toBe('top-level');
+ });
+
+ test('dispatcher no longer matches the closed-slice swallow output', () => {
+ const payload = '## Section\n\n- one\n- two\n';
+ const viaDispatcher = mountEditor(BASE);
+ setCaret(viaDispatcher, caretPos(viaDispatcher, 1, 'mid'));
+ paste(viaDispatcher, 'B', { md: payload });
+
+ // Direct closed-slice replaceSelection is the swallow mechanism: PM's
+ // fitter legally nests the payload inside the target item because
+ // listItem admits every block type. The placement layer (not the schema)
+ // is what routes around it, so the raw mechanism still swallows while
+ // the dispatcher splits.
+ const direct = mountEditor(BASE);
+ setCaret(direct, caretPos(direct, 1, 'mid'));
+ const node = direct.state.schema.nodeFromJSON(mdManager.parse(payload) as JSONContent);
+ direct.view.dispatch(direct.state.tr.replaceSelection(node.slice(0, node.content.size)));
+
+ expect(headingPlacement(direct.state.doc)).toBe('inside-list');
+ expect(serialize(viaDispatcher)).not.toBe(serialize(direct));
+ expect(serialize(viaDispatcher).trimEnd()).toBe(
+ '- alpha\n- be\n\n## Section\n\n- one\n- two\n- ta\n- gamma',
+ );
+ });
+});
diff --git a/packages/app/src/editor/clipboard/handle-paste.ts b/packages/app/src/editor/clipboard/handle-paste.ts
index 8bcd16527..8485ab9d9 100644
--- a/packages/app/src/editor/clipboard/handle-paste.ts
+++ b/packages/app/src/editor/clipboard/handle-paste.ts
@@ -21,10 +21,12 @@
* else verbatim plain-text insert.
*
* Placement: the markdown branches insert the re-parsed JSON as a closed
- * slice, EXCEPT when the caret is inside a list item and the pasted content
- * is entirely lists — then `buildListSiblingSpliceTr` splices the pasted
- * items as siblings at the caret's list level instead of letting the fitter
- * nest or orphan them (issue #609).
+ * slice, EXCEPT when the caret is inside a list item — then a list-aware
+ * splice runs instead of letting the fitter nest or orphan the content
+ * (issue #609). All-list payloads splice as item siblings at the caret's
+ * list level (`buildListSiblingSpliceTr`); mixed payloads split the list at
+ * the caret and place non-list blocks as siblings of the list itself
+ * (`buildMixedSiblingSpliceTr`).
*
* codeBlock short-circuit: cursor inside a codeBlock → skip all branches,
* insert text/plain verbatim.
@@ -545,6 +547,201 @@ function buildListSiblingSpliceTr(
return tr;
}
+function listItemsOf(list: ProseMirrorNode): ProseMirrorNode[] {
+ const items: ProseMirrorNode[] = [];
+ list.forEach((item) => {
+ if (item.type.name === 'listItem') items.push(item);
+ });
+ return items;
+}
+
+/**
+ * Drop blank items minted at the trailing cut edge, recursing into a
+ * trailing nested list so a caret at the start of a nested item doesn't
+ * leave an empty nested bullet behind. Returns null when the node empties
+ * out entirely.
+ */
+function pruneTrailingCutBlanks(node: ProseMirrorNode): ProseMirrorNode | null {
+ if (node.type.name === 'list') {
+ const items = listItemsOf(node);
+ while (items.length > 0) {
+ const pruned = pruneTrailingCutBlanks(items[items.length - 1]);
+ if (pruned) {
+ items[items.length - 1] = pruned;
+ break;
+ }
+ items.pop();
+ }
+ return items.length > 0 ? node.copy(Fragment.fromArray(items)) : null;
+ }
+ if (node.type.name === 'listItem') {
+ const children: ProseMirrorNode[] = [];
+ node.forEach((child) => {
+ children.push(child);
+ });
+ if (children.length > 0 && children[children.length - 1].type.name === 'list') {
+ const pruned = pruneTrailingCutBlanks(children[children.length - 1]);
+ if (pruned) children[children.length - 1] = pruned;
+ else children.pop();
+ }
+ const rebuilt = node.copy(Fragment.fromArray(children));
+ return isBlankListItem(rebuilt) ? null : rebuilt;
+ }
+ return node;
+}
+
+/**
+ * A leading cut edge can mint a wrapper item whose own text sits entirely
+ * before the caret: empty paragraphs followed by a single nested list. Lift
+ * that nested list's items up a level (no empty parent bullet) and drop
+ * items the cut left fully blank.
+ */
+function normalizeLeadingCutItems(items: ProseMirrorNode[]): ProseMirrorNode[] {
+ let normalized = items;
+ while (normalized.length > 0) {
+ const first = normalized[0];
+ if (isBlankListItem(first)) {
+ normalized = normalized.slice(1);
+ continue;
+ }
+ const lifted = liftBlankWrapperItem(first);
+ if (lifted) {
+ normalized = [...lifted, ...normalized.slice(1)];
+ continue;
+ }
+ break;
+ }
+ return normalized;
+}
+
+function liftBlankWrapperItem(item: ProseMirrorNode): ProseMirrorNode[] | null {
+ if (item.childCount === 0) return null;
+ const last = item.child(item.childCount - 1);
+ if (last.type.name !== 'list') return null;
+ for (let i = 0; i < item.childCount - 1; i++) {
+ const child = item.child(i);
+ if (child.type.name !== 'paragraph' || child.content.size > 0) return null;
+ }
+ return listItemsOf(last);
+}
+
+/**
+ * List-aware placement for MIXED payloads — the issue #609 sibling-splice
+ * intent extended beyond all-list payloads.
+ *
+ * A pasted doc holding at least one non-list top-level block, at a caret
+ * inside a list item, must not be nested into that item (schema-legal for
+ * the closed-slice fallback because `listItem` is `paragraph block*`, but a
+ * structural demotion users read as the paste being swallowed). Instead the
+ * OUTERMOST ancestor list splits at the caret and the payload lands where
+ * typing it at top level would:
+ *
+ * [before-half + leading payload list run] non-list blocks (and interior
+ * lists) verbatim [trailing payload list run + after-half]
+ *
+ * Leading/trailing payload list runs continue the list (item siblings,
+ * keeping the original list node) exactly like the all-list splice; payload
+ * lists with no original items to merge with are kept verbatim. Blank cut
+ * halves are dropped per the all-list path's blank-item rule, and a nested
+ * caret's remainder lifts to the top list level rather than minting an
+ * empty parent bullet.
+ *
+ * Returns a built transaction, or null to fall back: ranged selection,
+ * all-list payload (the item-level splice owns it), caret not inside a
+ * plain list/listItem chain (tables etc. keep the closed-slice behavior).
+ */
+function buildMixedSiblingSpliceTr(
+ state: EditorState,
+ docNode: ProseMirrorNode,
+): Transaction | null {
+ const { selection } = state;
+ if (!selection.empty) return null;
+ if (docNode.content.childCount === 0) return null;
+
+ let hasNonList = false;
+ docNode.content.forEach((child) => {
+ if (child.type.name !== 'list') hasNonList = true;
+ });
+ if (!hasNonList) return null;
+
+ const { $from } = selection;
+ if (!$from.parent.isTextblock) return null;
+
+ // Outermost ancestor list: mixed payloads split the whole nesting stack so
+ // non-list blocks escape to the list's own sibling level.
+ let listDepth = -1;
+ for (let depth = 1; depth <= $from.depth; depth++) {
+ if ($from.node(depth).type.name === 'list') {
+ listDepth = depth;
+ break;
+ }
+ }
+ if (listDepth < 0) return null;
+ // Only split through plain list/listItem nesting; a caret inside another
+ // container (table cell, blockquote) within an item keeps current behavior.
+ for (let depth = listDepth; depth < $from.depth; depth++) {
+ const name = $from.node(depth).type.name;
+ if (name !== 'list' && name !== 'listItem') return null;
+ }
+ if ($from.node($from.depth - 1)?.type.name !== 'listItem') return null;
+
+ const listNode = $from.node(listDepth);
+ const listStart = $from.before(listDepth);
+ const listEnd = $from.after(listDepth);
+ const offset = $from.pos - $from.start(listDepth);
+ const beforeHalf = pruneTrailingCutBlanks(listNode.cut(0, offset));
+ const beforeItems = beforeHalf ? listItemsOf(beforeHalf) : [];
+ const afterItems = normalizeLeadingCutItems(listItemsOf(listNode.cut(offset)));
+
+ const children: ProseMirrorNode[] = [];
+ docNode.content.forEach((child) => {
+ children.push(child);
+ });
+ let lead = 0;
+ while (lead < children.length && children[lead].type.name === 'list') lead++;
+ let trail = children.length;
+ while (trail > lead && children[trail - 1].type.name === 'list') trail--;
+ const leadingLists = children.slice(0, lead);
+ const middle = children.slice(lead, trail);
+ const trailingLists = children.slice(trail);
+
+ const out: ProseMirrorNode[] = [];
+ if (beforeItems.length > 0) {
+ out.push(
+ listNode.copy(Fragment.fromArray([...beforeItems, ...leadingLists.flatMap(listItemsOf)])),
+ );
+ } else {
+ out.push(...leadingLists);
+ }
+ out.push(...middle);
+ if (afterItems.length > 0) {
+ out.push(
+ listNode.copy(Fragment.fromArray([...trailingLists.flatMap(listItemsOf), ...afterItems])),
+ );
+ } else {
+ out.push(...trailingLists);
+ }
+
+ const tr = state.tr.replaceWith(listStart, listEnd, Fragment.fromArray(out));
+
+ // Caret lands at the end of the pasted content, mirroring the all-list
+ // splice: after the last pasted item inside a merged trailing list, else
+ // at the end of the last payload-derived node.
+ let caretPos = listStart;
+ if (trailingLists.length > 0 && afterItems.length > 0) {
+ for (let i = 0; i < out.length - 1; i++) caretPos += out[i].nodeSize;
+ caretPos += 1;
+ for (const list of trailingLists) {
+ for (const item of listItemsOf(list)) caretPos += item.nodeSize;
+ }
+ } else {
+ const lastPayloadIndex = out.length - 1 - (afterItems.length > 0 ? 1 : 0);
+ for (let i = 0; i <= lastPayloadIndex; i++) caretPos += out[i].nodeSize;
+ }
+ tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(caretPos, tr.doc.content.size)), -1));
+ return tr;
+}
+
function applyJsonSlice(
view: EditorView,
json: JSONContent,
@@ -560,7 +757,8 @@ function applyJsonSlice(
// though the closed slice could have delivered it.
let spliceTr: Transaction | null = null;
try {
- spliceTr = buildListSiblingSpliceTr(view.state, node);
+ spliceTr =
+ buildListSiblingSpliceTr(view.state, node) ?? buildMixedSiblingSpliceTr(view.state, node);
} catch {
spliceTr = null;
}