Skip to content

Type Editor


Type Editor / @type-editor/model / elements/Slice / Slice

Class: Slice

Defined in: packages/model/src/elements/Slice.ts:16

A slice represents a piece cut out of a larger document. It stores not only a fragment, but also the depth up to which nodes on both side are 'open' (cut through).

Implements

Constructors

Constructor

ts
new Slice(
   content,
   openStart,
   openEnd): Slice;

Defined in: packages/model/src/elements/Slice.ts:59

Create a slice. When specifying a non-zero open depth, you must make sure that there are nodes of at least that depth at the appropriate side of the fragment—i.e. if the fragment is an empty paragraph node, openStart and openEnd can't be greater than 1.

It is not necessary for the content of open nodes to conform to the schema's content constraints, though it should be a valid start/end/middle for such a node, depending on which sides are open.

Parameters

ParameterTypeDescription
contentFragmentThe slice's content.
openStartnumberThe open depth at the start of the fragment.
openEndnumberThe open depth at the end.

Returns

Slice

Throws

If openStart or openEnd are negative.

Accessors

content

Get Signature

ts
get content(): Fragment;

Defined in: packages/model/src/elements/Slice.ts:102

The fragment containing the slice's content.

Returns

Fragment


elementType

Get Signature

ts
get elementType(): ElementType;

Defined in: packages/model/src/elements/Slice.ts:68

Returns

ElementType

Implementation of

PmElement.elementType


openEnd

Get Signature

ts
get openEnd(): number;

Defined in: packages/model/src/elements/Slice.ts:118

The open depth at the end of the slice. Indicates how many parent nodes are open (cut through) at the end.

Returns

number


openStart

Get Signature

ts
get openStart(): number;

Defined in: packages/model/src/elements/Slice.ts:110

The open depth at the start of the slice. Indicates how many parent nodes are open (cut through) at the beginning.

Returns

number


size

Get Signature

ts
get size(): number;

Defined in: packages/model/src/elements/Slice.ts:129

The size this slice would add when inserted into a document. This is calculated as the content size minus the open depths on both sides, since open nodes don't add to the insertion size.

Returns

number

The effective size of the slice for insertion purposes.


empty

Get Signature

ts
get static empty(): Slice;

Defined in: packages/model/src/elements/Slice.ts:92

Returns the singleton empty slice instance. An empty slice has no content and zero open depths on both sides.

Returns

Slice

A shared empty Slice instance.

Methods

eq()

ts
eq(other): boolean;

Defined in: packages/model/src/elements/Slice.ts:232

Tests whether this slice is equal to another slice. Two slices are equal if they have the same content, openStart, and openEnd values.

Parameters

ParameterTypeDescription
otherSliceThe slice to compare with.

Returns

boolean

True if the slices are equal, false otherwise.


insertAt()

ts
insertAt(pos, fragment): Slice;

Defined in: packages/model/src/elements/Slice.ts:198

Insert a fragment at a specific position within this slice.

Parameters

ParameterTypeDescription
posnumberThe position at which to insert the fragment.
fragmentFragmentThe fragment to insert.

Returns

Slice

A new Slice with the fragment inserted, or null if insertion is not possible.

Throws

If the position is invalid (negative).


removeBetween()

ts
removeBetween(from, to): Slice;

Defined in: packages/model/src/elements/Slice.ts:214

Remove content between two positions in this slice.

Parameters

ParameterTypeDescription
fromnumberThe starting position of the range to remove.
tonumberThe ending position of the range to remove.

Returns

Slice

A new Slice with the specified range removed.

Throws

If the range is invalid or not flat (spans multiple depth levels).


toJSON()

ts
toJSON(): SliceJSON;

Defined in: packages/model/src/elements/Slice.ts:252

Convert a slice to a JSON-serializable representation.

Returns

SliceJSON

A JSON representation of the slice, or null if the slice is empty.


toString()

ts
toString(): string;

Defined in: packages/model/src/elements/Slice.ts:243

Return a string representation of the slice for debugging purposes.

Returns

string

A string representation showing the content and open depths.


fromJSON()

ts
static fromJSON(schema, json): Slice;

Defined in: packages/model/src/elements/Slice.ts:140

Deserialize a slice from its JSON representation.

Parameters

ParameterTypeDescription
schemaSchemaThe schema to use for deserializing the content.
jsonSliceJSONThe JSON representation of the slice.

Returns

Slice

A new Slice instance or the empty slice if json is null/undefined.


maxOpen()

ts
static maxOpen(fragment, openIsolating?): Slice;

Defined in: packages/model/src/elements/Slice.ts:164

Create a slice from a fragment by taking the maximum possible open value on both sides of the fragment. This traverses down the first and last children to determine how many levels can be opened.

Parameters

ParameterTypeDefault valueDescription
fragmentFragmentundefinedThe fragment to create a slice from.
openIsolatingbooleantrueWhether to open through isolating nodes. Defaults to true.

Returns

Slice

A new Slice with maximum possible open depths.