Files
outline/shared/editor/lib/sliceWithoutFirstNode.ts
Tom Moor df8f8848ef fix: Crash when pasting a single block into the document title (#13451)
Removing the first node from the pasted slice kept the original open
depths, which are no longer valid for the remaining content. When the
clipboard held a single top-level node the content became empty while
openEnd stayed above zero, and ProseMirror walked into a null child.

Fixes OUTLINE-CLOUD-51V
2026-08-15 20:23:16 -04:00

29 lines
805 B
TypeScript

import { Slice } from "prosemirror-model";
/**
* Removes the first top-level node from a slice. The open depths are clamped to
* the remaining content, as the removed node may have been the one that they
* referred to.
*
* @param slice the slice to remove the first node from.
* @returns a new slice, or undefined if no content remains.
*/
export function sliceWithoutFirstNode(slice: Slice): Slice | undefined {
const { firstChild } = slice.content;
if (!firstChild) {
return undefined;
}
const content = slice.content.cut(firstChild.nodeSize);
if (content.childCount === 0) {
return undefined;
}
const maxOpen = Slice.maxOpen(content);
return new Slice(
content,
Math.min(slice.openStart, maxOpen.openStart),
Math.min(slice.openEnd, maxOpen.openEnd)
);
}