Skip to content

Type Editor


Type Editor / @type-editor/changeset / Change / Change

Class: Change<Data>

Defined in: Change.ts:34

Represents a change between two document versions with metadata.

A Change tracks a replaced range in the document, including both what was deleted from the old version and what was inserted in the new version. It uses two coordinate systems:

  • A coordinates: positions in the old document
  • B coordinates: positions in the new document

Type Parameters

Type ParameterDefault typeDescription
DataanyThe type of metadata associated with the changed content.

Constructors

Constructor

ts
new Change<Data>(
   fromA,
   toA,
   fromB,
   toB,
   deleted,
   inserted): Change<Data>;

Defined in: Change.ts:55

Creates a new Change representing a document modification.

Parameters

ParameterTypeDescription
fromAnumberThe start position of the range in the old document (A coordinates).
toAnumberThe end position of the range in the old document (A coordinates).
fromBnumberThe start position of the range in the new document (B coordinates).
toBnumberThe end position of the range in the new document (B coordinates).
deletedreadonly Span<Data>[]Metadata spans for the deleted content. The total length of these spans must equal toA - fromA.
insertedreadonly Span<Data>[]Metadata spans for the inserted content. The total length of these spans must equal toB - fromB.

Returns

Change<Data>

Accessors

deleted

Get Signature

ts
get deleted(): readonly Span<Data>[];

Defined in: Change.ts:90

The spans of deleted content with associated metadata.

Returns

readonly Span<Data>[]


fromA

Get Signature

ts
get fromA(): number;

Defined in: Change.ts:70

The start position in the old document (A coordinates).

Returns

number


fromB

Get Signature

ts
get fromB(): number;

Defined in: Change.ts:80

The start position in the new document (B coordinates).

Returns

number


inserted

Get Signature

ts
get inserted(): readonly Span<Data>[];

Defined in: Change.ts:95

The spans of inserted content with associated metadata.

Returns

readonly Span<Data>[]


lenA

Get Signature

ts
get lenA(): number;

Defined in: Change.ts:100

The length of the deleted range in the old document.

Returns

number


lenB

Get Signature

ts
get lenB(): number;

Defined in: Change.ts:105

The length of the inserted range in the new document.

Returns

number


toA

Get Signature

ts
get toA(): number;

Defined in: Change.ts:75

The end position in the old document (A coordinates).

Returns

number


toB

Get Signature

ts
get toB(): number;

Defined in: Change.ts:85

The end position in the new document (B coordinates).

Returns

number

Methods

slice()

ts
slice(
   startA,
   endA,
   startB,
   endB): Change<Data>;

Defined in: Change.ts:424

Creates a sub-change by slicing ranges from both coordinate systems.

This extracts a portion of this change, specified by ranges in both the A and B coordinate systems. If the slice covers the entire change, returns this instance to avoid allocation.

Parameters

ParameterTypeDescription
startAnumberThe start position in A coordinates (relative to this change).
endAnumberThe end position in A coordinates (relative to this change).
startBnumberThe start position in B coordinates (relative to this change).
endBnumberThe end position in B coordinates (relative to this change).

Returns

Change<Data>

A new Change representing the specified slice, or this if unchanged.


toJSON()

ts
toJSON(): ChangeJSON<Data>;

Defined in: Change.ts:453

Serializes this Change to a JSON-compatible representation.

Since the Change class structure matches the ChangeJSON interface, this method returns the instance itself.

Returns

ChangeJSON<Data>

A JSON representation of this change.


fromJSON()

ts
static fromJSON<Data>(json): Change<Data>;

Defined in: Change.ts:202

Deserializes a Change from its JSON representation.

Type Parameters

Type ParameterDescription
DataThe type of metadata in the change.

Parameters

ParameterTypeDescription
jsonChangeJSON<Data>The JSON object representing the change.

Returns

Change<Data>

A new Change instance reconstructed from the JSON data.


merge()

ts
static merge<Data>(
   x,
   y,
   combine): readonly Change<Data>[];

Defined in: Change.ts:126

Merges two changesets into a single changeset.

This combines two sequential changesets where the end document of the first changeset is the start document of the second. The result is a single changeset spanning from the start of the first to the end of the second.

The merge operates by synchronizing over a "middle" coordinate system:

  • For the first changeset (x): the B coordinates represent the middle document
  • For the second changeset (y): the A coordinates represent the middle document

Type Parameters

Type ParameterDescription
DataThe type of metadata in the changes.

Parameters

ParameterTypeDescription
xreadonly Change<Data>[]The first changeset.
yreadonly Change<Data>[]The second changeset applied after x.
combine(dataA, dataB) => DataFunction to combine metadata when spans need to be merged.

Returns

readonly Change<Data>[]

A single changeset representing both transformations.