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 |
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}
#
pushAttributeChanges(changes, oldAttrs)
#
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. |
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 |
Returns:
End offset of the insertion
- Type
- number
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 |
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 |
Returns:
End offset of the removal
- Type
- number
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 |
pushReplaceInternal(remove, insert, [insertedDataOffset], [insertedDataLength])private
#
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 |
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 |
Throws:
-
Cannot retain backwards
- Type
- Error
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
|
annotation |
ve.dm.Annotation | Annotation to set or clear |
Returns:
Transaction that annotates content
- Type
- ve.dm.Transaction
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 |
Returns:
Transaction that changes an element
- Type
- ve.dm.Transaction
Throws:
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 |
Returns:
Transaction that converts content branches
- Type
- ve.dm.Transaction
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) |
Returns:
Transaction that inserts the nodes and updates the internal list
- Type
- ve.dm.Transaction
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 |
Returns:
Transaction that inserts data
- Type
- ve.dm.Transaction
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:
- Elements are only removed if range being removed covers the entire element
- Elements can only be merged if
ve.dm.Node#canBeMergedWith
returns true - 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 |
Returns:
Transaction that removes data
- Type
- ve.dm.Transaction
Throws:
-
Invalid range
- Type
- Error
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 |
Returns:
Transaction that replaces data
- Type
- ve.dm.Transaction
Throws:
-
Invalid range
- Type
- Error
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 |
Returns:
- Type
- ve.dm.Transaction