A Slice represents a portion of a document cut out from a larger one. It contains a Fragment (the content) and two depth values, openStart and openEnd, which indicate how many levels of nodes are 'open' (cut through) at the beginning and end of the fragment.
This allows a slice to represent not just a flat list of nodes, but a piece of a document that maintains its structural relationship with the surrounding nodes (e.g., a slice that starts inside a paragraph and ends inside a paragraph).
Key Properties
content: The Fragment contained in the slice.openStart: The depth at which the start of the fragment is open.openEnd: The depth at which the end of the fragment is open.size: The number of positions the slice adds when inserted into a document (this.content.size - this.openStart - this.openEnd).
Common Creation Methods
Slice.empty: An empty slice with no content and zero open depth.Slice.maxOpen(fragment, [openIsolating]): Creates a slice from a Fragment by calculating the maximum possible open depth on both sides. Use this when you want the slice to automatically 'fit' into the surrounding structure of the fragment's nodes.
import {Slice} from "@prosemirror/model";
// Create an empty slice
const emptySlice = Slice.empty;
// Create a slice from a fragment with maximum open depth
const slice = Slice.maxOpen(myFragment);
// Check equality
const isSame = slice.eq(otherSlice);
// Convert to JSON
const json = slice.toJSON();
const restoredSlice = Slice.fromJSON(schema, json);