Expand all

ve.dm.TransactionBuilder

Constructor

new ve.dm.TransactionBuilder() #

Transaction builder: build transactions based on current ve.dm.Document state

Source:
Transaction builder: build transactions based on current ve.dm.Document state

Methods

addSafeRemoveOps(doc, removeStart, removeEnd, [removeMetadata]) → {number} #

Adds a replace op to remove the desired range and, where required, splices in retain ops to prevent the deletion of undeletable nodes.

Parameters:

Name Type Attributes Default Description
doc ve.dm.Document

The document in the state to which the transaction applies

removeStart number

Offset to start removing from

removeEnd number

Offset to remove to

removeMetadata boolean optional
false

Remove metadata instead of collapsing it

Source:

Returns:

End offset of the removal

Type
number

Adds a replace op to remove the desired range and, where required, splices in retain ops to prevent the deletion of undeletable nodes.

getTransaction() → {ve.dm.Transaction} #

Get the transaction

Source:

Returns:

The transaction

Type
ve.dm.Transaction
Get the transaction

pushAttributeChanges(changes, oldAttrs) #

Add a series of element attribute change operations.

Parameters:

Name Type Description
changes Object

Object mapping attribute names to new values

oldAttrs Object

Object mapping attribute names to old values

Source:
Add a series of element attribute change operations.

pushFinalRetain(doc, offset)private #

Add a final retain operation to finish off a transaction (internal helper).

Parameters:

Name Type Description
doc ve.dm.Document

The document in the state to which the transaction applies

offset number

Final offset edited by the transaction up to this point.

Source:
Add a final retain operation to finish off a transaction (internal helper).

pushInsertion(doc, currentOffset, insertOffset, data) → {number}private #

Internal helper method for newFromInsertion and newFromReplacement. Adds an insertion to an existing transaction object.

Parameters:

Name Type Description
doc ve.dm.Document

The document in the state to which the transaction applies

currentOffset number

Offset up to which the transaction has gone already

insertOffset number

Offset to insert at

data Array

Linear model data to insert

Source:

Returns:

End offset of the insertion

Type
number
Internal helper method for newFromInsertion and newFromReplacement.

pushMeta(doc, offset, metaItems) #

Insert meta items, as near to the current transaction end as is legal

Parameters:

Name Type Description
doc ve.dm.Document

Document in pre-transaction state

offset number

Offset of the end of the current (partial) operations list

metaItems Array

linear data containing just meta items

Source:
Insert meta items, as near to the current transaction end as is legal

pushRemoval(doc, currentOffset, range, [removeMetadata]) → {number}private #

Internal helper method for newFromRemoval and newFromReplacement. Adds a removal to an existing transaction object.

Parameters:

Name Type Attributes Default Description
doc ve.dm.Document

The document in the state to which the transaction applies

currentOffset number

Offset up to which the transaction has gone already

range ve.Range

Range to remove

removeMetadata boolean optional
false

Remove metadata instead of collapsing it

Source:

Returns:

End offset of the removal

Type
number
Internal helper method for newFromRemoval and newFromReplacement.

pushReplaceElementAttribute(key, from, to) #

Add an element attribute change operation.

Parameters:

Name Type Description
key string

Name of attribute to change

from any

Value change attribute from, or undefined if not previously set

to any

Value to change attribute to, or undefined to remove

Source:
Add an element attribute change operation.

pushReplaceInternal(remove, insert, [insertedDataOffset], [insertedDataLength])private #

Add a replace operation (internal helper).

Parameters:

Name Type Attributes Description
remove Array

Data removed.

insert Array

Data to insert.

insertedDataOffset number optional

Inserted data offset

insertedDataLength number optional

Inserted data length

Source:
Add a replace operation (internal helper).

pushReplacement(doc, offset, removeLength, insert, removeMetadata, [insertedDataOffset], [insertedDataLength]) #

Add a replace operation

If metadata is collapsed instead of removed, it will be shifted backwards if necessary to reach a legal position for metadata in the new document structure.

Parameters:

Name Type Attributes Description
doc ve.dm.Document

The document in the state to which the transaction applies

offset number

Offset to start at

removeLength number

Number of data items to remove

insert Array

Data to insert

removeMetadata boolean

Remove metadata instead of collapsing it

insertedDataOffset number optional

Offset of the originally inserted data in the resulting operation data

insertedDataLength number optional

Length of the originally inserted data in the resulting operation data

Source:

Add a replace operation

If metadata is collapsed instead of removed, it will be shifted backwards if necessary to reach a legal position for metadata in the new document structure.

pushRetain(length) #

Add a retain operation.

Parameters:

Name Type Description
length number

Length of content data to retain

Source:

Throws:

Cannot retain backwards

Type
Error
Add a retain operation.

newFromAnnotation(doc, range, method, annotation) → {ve.dm.Transaction}static #

Generate a transaction that annotates content.

Parameters:

Name Type Description
doc ve.dm.Document

Document in pre-transaction state

range ve.Range

Range to annotate

method string

Annotation mode

  • set: Adds annotation to all content in range
  • clear: Removes instances of annotation from content in range
annotation ve.dm.Annotation

Annotation to set or clear

Source:

Returns:

Transaction that annotates content

Type
ve.dm.Transaction
Generate a transaction that annotates content.

newFromAttributeChanges(doc, offset, attr) → {ve.dm.Transaction}static #

Generate a transaction that changes one or more attributes.

Parameters:

Name Type Description
doc ve.dm.Document

Document in pre-transaction state

offset number

Offset of element

attr Object.<string, any>

List of attribute key and value pairs, use undefined value to remove an attribute

Source:

Returns:

Transaction that changes an element

Type
ve.dm.Transaction

Throws:

  • Cannot set attributes to non-element data

    Type
    Error
  • Cannot set attributes on closing element

    Type
    Error
Generate a transaction that changes one or more attributes.

newFromContentBranchConversion(doc, range, type, [attr], [internal]) → {ve.dm.Transaction}static #

Generate a transaction that converts elements that can contain content.

Parameters:

Name Type Attributes Description
doc ve.dm.Document

Document in pre-transaction state

range ve.Range

Range to convert

type string

Symbolic name of element type to convert to

attr Object optional

Attributes to initialize element with

internal Object optional

Internal attributes to initialize element with

Source:

Returns:

Transaction that converts content branches

Type
ve.dm.Transaction
Generate a transaction that converts elements that can contain content.

newFromDocumentInsertion(doc, offset, newDoc, [newDocRange]) → {ve.dm.Transaction}static #

Build a transaction that inserts the contents of a document at a given offset.

This is typically used to merge changes to a document slice back into the main document. If newDoc is a document slice of doc, it's assumed that there were no changes to doc's internal list since the slice, so any differences between internal items that doc and newDoc have in common will be resolved in newDoc's favor.

Parameters:

Name Type Attributes Description
doc ve.dm.Document

Main document in the state to which the transaction start applies

offset number

Offset to insert at

newDoc ve.dm.Document

Document to insert

newDocRange ve.Range optional

Range from the new document to insert (defaults to entire document)

Source:

Returns:

Transaction that inserts the nodes and updates the internal list

Type
ve.dm.Transaction
Build a transaction that inserts the contents of a document at a given offset.

newFromInsertion(doc, offset, data) → {ve.dm.Transaction}static #

Generate a transaction that inserts data at an offset.

Parameters:

Name Type Description
doc ve.dm.Document

Document in pre-transaction state

offset number

Offset to insert at

data Array

Data to insert

Source:

Returns:

Transaction that inserts data

Type
ve.dm.Transaction
Generate a transaction that inserts data at an offset.

newFromRemoval(doc, range, [removeMetadata]) → {ve.dm.Transaction}static #

Generate a transaction that removes data from a range.

There are three possible results from a removal:

  • Remove content only
    • Occurs when the range starts and ends on elements of different type, depth or ancestry
  • Remove entire elements and their content
    • Occurs when the range spans across an entire element
  • Merge two elements by removing the end of one and the beginning of another
    • Occurs when the range starts and ends on elements of similar type, depth and ancestry

This function uses the following logic to decide what to actually remove:

  1. Elements are only removed if range being removed covers the entire element
  2. Elements can only be merged if ve.dm.Node#canBeMergedWith returns true
  3. Merges take place at the highest common ancestor

Parameters:

Name Type Attributes Default Description
doc ve.dm.Document

Document in pre-transaction state

range ve.Range

Range of data to remove

removeMetadata boolean optional
false

Remove metadata instead of collapsing it

Source:

Returns:

Transaction that removes data

Type
ve.dm.Transaction

Throws:

Invalid range

Type
Error
Generate a transaction that removes data from a range.

newFromReplacement(doc, range, data, [removeMetadata]) → {ve.dm.Transaction}static #

Generate a transaction that replaces data in a range.

Parameters:

Name Type Attributes Default Description
doc ve.dm.Document

Document in pre-transaction state

range ve.Range

Range of data to remove

data Array

Data to insert

removeMetadata boolean optional
false

Remove metadata instead of collapsing it

Source:

Returns:

Transaction that replaces data

Type
ve.dm.Transaction

Throws:

Invalid range

Type
Error
Generate a transaction that replaces data in a range.

newFromWrap(doc, range, unwrapOuter, wrapOuter, unwrapEach, wrapEach) → {ve.dm.Transaction}static #

Generate a transaction that wraps, unwraps or replaces structure.

The unwrap parameters are checked against the actual model data, and an exception is thrown if the type fields don't match. This means you can omit attributes from the unwrap parameters, those are automatically picked up from the model data instead.

NOTE: This function currently does not fix invalid parent/child relationships, so it will happily convert paragraphs to listItems without wrapping them in a list if that's what you ask it to do. We'll probably fix this later but for now the caller is responsible for giving valid instructions.

Changing a paragraph to a header: Before: [ {type: 'paragraph'}, 'a', 'b', 'c', {type: '/paragraph'} ] newFromWrap( new ve.Range( 1, 4 ), [ {type: 'paragraph'} ], [ {type: 'heading', level: 1 } ] ); After: [ {type: 'heading', level: 1 }, 'a', 'b', 'c', {type: '/heading'} ]

Changing a set of paragraphs to a list: Before: [ {type: 'paragraph'}, 'a', {type: '/paragraph'}, {'type':'paragraph'}, 'b', {'type':'/paragraph'} ] newFromWrap( new ve.Range( 0, 6 ), [], [ {type: 'list' } ], [], [ {type: 'listItem', attributes: {styles: ['bullet']}} ] ); After: [ {type: 'list'}, {type: 'listItem', attributes: {styles: ['bullet']}}, {'type':'paragraph'} 'a', {type: '/paragraph'}, {type: '/listItem'}, {type: 'listItem', attributes: {styles: ['bullet']}}, {type: 'paragraph'}, 'b', {type: '/paragraph'}, {type: '/listItem'}, {type: '/list'} ]

Parameters:

Name Type Description
doc ve.dm.Document

Document in pre-transaction state

range ve.Range

Range to wrap/unwrap/replace around

unwrapOuter Array

Opening elements to unwrap. These must be immediately outside the range

wrapOuter Array

Opening elements to wrap around the range

unwrapEach Array

Opening elements to unwrap from each top-level element in the range

wrapEach Array

Opening elements to wrap around each top-level element in the range

Source:

Returns:

Type
ve.dm.Transaction
Generate a transaction that wraps, unwraps or replaces structure.