Constructor
new ve.dm.TreeModifier()
#
DataModel tree modifier, following the algorithm in T162762.
The top-level process() method applies a transaction to the document in two stages. First, calculateTreeOperations() translates the linear transaction into tree operations; then, applyTreeOperations() updates both the linear model and the document tree simultaneously.
Each tree operation takes one of the following forms:
{ type: 'insertNode', isContent: {boolean}, at: {Path}, element: {OpenElementLinModItem} } { type: 'removeNode', isContent: {boolean}, at: {Path}, element: {OpenElementLinModItem} } { type: 'moveNode', isContent: {boolean}, from: {Path}, to: {Path} } { type: 'insertText', isContent: true, at: {Path}, data: {TextLinModItem[]} } { type: 'removeText', isContent: true, at: {Path}, data: {TextLinModItem[]} } { type: 'moveText', isContent: true, from: {Path}, to: {Path}, length: {number} }
Note that moveNode/moveText do not specify what content is being moved, and all the Node operations always operate on a single node at a time.
{Path} is number[] representing the tree path from the DocumentNode to the position, except that within ContentBranchNodes, the offset is the linearized offset of the position.
{OpenElementLinModItem} is the linear model value representing the node being inserted or removed, like { type: 'paragraph' } .
{TextLinModItem[]} is the linear model values representing the text, like [ 'y', 'o', 'u', ' ', [ 'm', [ 'he4e7c54e2204d10b ] ], [ 'e' 'he4e7c54e2204d10b ] ] .
The isContent flag is true if the operation is taking place inside a ContentBranchNode (so it is always true for text).
NOTE: Instances of this class are not recyclable: you can only call .process( tx ) on them once.
- Source:
Properties
adjustmentTree
#
deletions
#
Properties:
Name | Type | Description |
---|---|---|
deletions |
Array.<ve.dm.Node> | Array (acting as set) of removed nodes |
- Source:
insertedNodes
#
Properties:
Name | Type | Description |
---|---|---|
insertedNodes |
Array.<number> | Nodes to be inserted at context |
- Source:
insertedPositions
#
Properties:
Name | Type | Description |
---|---|---|
insertedPositions |
Array.<number> | Position within nodes to be inserted |
- Source:
inserter
#
remover
#
treeOps
#
Properties:
- Source:
Methods
adjustInserterPosition(rawPosition) → {Array.<number>}
#
Get the adjusted position of a raw inserter position, XXX
Parameters:
Name | Type | Description |
---|---|---|
rawPosition |
Array.<number> | The raw inserter position (must be its current position) |
- Source:
Returns:
Adjusted pathAndOffset, with offsets inside a ContentBranchNode linearized, and including current position within nodes to be inserted, if any
- Type
- Array.<number>
adjustRemoverPosition(rawPosition) → {Array.<number>}
#
calculateTreeOperations(transaction)
#
Transform linear operations into tree operations
Parameters:
Name | Type | Description |
---|---|---|
transaction |
ve.dm.Transaction | The transaction |
- Source:
checkCanInsertNodeType(nodeType)
#
Throw an error if the inserter node cannot take a child of a specified type
Parameters:
Name | Type | Description |
---|---|---|
nodeType |
string | The node type |
- Source:
Throws:
-
Cannot add child
- Type
- Error
checkCanInsertText()
#
Throw an error if the inserter is not in a content position
- Source:
Throws:
-
Cannot insert text into a foo node
- Type
- Error
cursorsMatch() → {boolean}
#
Test whether both pointers point to the same location
- Source:
Returns:
True if the paths and offsets are identical
- Type
- boolean
doesTypeTakeContent(type) → {boolean}
#
Test whether a node type takes content
Parameters:
Name | Type | Description |
---|---|---|
type |
string | The name of a |
- Source:
Returns:
Whether that type takes content
- Type
- boolean
findOrCreateAdjustmentNode(position) → {Object}
#
getAdjustedPosition(position, isInserter) → {Array.<number>}
#
Get the adjusted position of a raw position
Parameters:
Name | Type | Description |
---|---|---|
position |
Array.<number> | The raw position |
isInserter |
boolean | True for an inserter position; false for a remover position |
- Source:
Returns:
Adjusted pathAndOffset, with offsets inside a ContentBranchNode linearized
- Type
- Array.<number>
getRawInserterPosition() → {Array.<number>}
#
Get the raw position at the inserter
- Source:
Returns:
The pathAndOffset, with offsets inside a ContentBranchNode linearized
- Type
- Array.<number>
getRawPosition(path, offset, node) → {Array.<number>}
#
Get the raw position of a node, with offsets inside a ContentBranchNode linearized
Parameters:
Name | Type | Description |
---|---|---|
path |
Array.<number> | Path to a node |
offset |
number | Offset within the node |
node |
ve.dm.Node | The node |
- Source:
Returns:
The pathAndOffset, with offsets inside a ContentBranchNode linearized
- Type
- Array.<number>
getRawRemoverPosition(step) → {Array.<number>}
#
Get the raw position of a node stepped over by the remover
Parameters:
Name | Type | Description |
---|---|---|
step |
ve.dm.TreeCursor.Step | Remover step |
- Source:
Returns:
The pathAndOffset, with offsets inside a ContentBranchNode linearized
- Type
- Array.<number>
getTypeAtInserter() → {string}
#
Get the type of the node in which the inserter lies
- Source:
Returns:
The node type
- Type
- string
isInsertionContent() → {boolean}
#
Test whether the inserter is at a content position
- Source:
Returns:
Whether the inserter is at a content position
- Type
- boolean
modifyAdjustmentTree(rawPosition, diff, deleteDescendants)
#
Modify the adjustment in the adjustment tree at a given offset path
Parameters:
Name | Type | Description |
---|---|---|
rawPosition |
Array.<number> | Unadjusted offset path |
diff |
number | Adjustment at rawPosition |
deleteDescendants |
boolean | If true, delete all adjustments at paths descending from here |
- Source:
process(document, transaction)
#
The top level method: modify document tree according to transaction
Parameters:
Name | Type | Description |
---|---|---|
document |
ve.dm.Document | The document |
transaction |
ve.dm.Transaction | The transaction |
- Source:
processImplicitFinalRetain()
#
Retain to the end of the content
- Source:
processInsert(itemOrData)
#
Process the insertion an open tag, a close tag, or an array of text items
Parameters:
- Source:
processLinearOperation(linearOp)
#
Translate a linear operation into tree operations
#processImplicitFinalRetain should be called once all operations have been processed
Parameters:
Name | Type | Description |
---|---|---|
linearOp |
Object | The linear operation |
- Source:
Translate a linear operation into tree operations
#processImplicitFinalRetain should be called once all operations have been processed
processRemove(itemOrData)
#
Process the removal of some items
Parameters:
- Source:
processRetain(maxLength) → {number}
#
Process the retention of content passed by one step of the remover
If the inserter and remover are at the same place, just skip content.
Else the remover feeds the inserter:
- The inserter plans insertions in the (imagined, hypothetical) document
- But the only move it can make in the (immutable) real document is to step out of nodes
- It cannot skip past nodes (other than skipping remaining siblings when stepping out)
- If the remover steps into a node, the inserter creates a node of the same type
- If the remover crosses content, it is moved to the inserter
- If the remover steps out of a node, the inserter steps out of its node
Parameters:
Name | Type | Description |
---|---|---|
maxLength |
number | The maximum amount of content to retain |
- Source:
Returns:
The amount of content retained
- Type
- number
Process the retention of content passed by one step of the remover
If the inserter and remover are at the same place, just skip content.
pushInsertNodeOp(element)
#
Push into treeOps the node insertion at the current inserter position
Parameters:
Name | Type | Description |
---|---|---|
element |
ve.dm.Node | The element to insert (inserted into treeOps without copying) |
- Source:
pushInsertTextOp(data)
#
Push into treeOps the text insertion at the current inserter position
Parameters:
Name | Type | Description |
---|---|---|
data |
Array | The text data to insert |
- Source:
pushMoveNodeOp(removerStep)
#
Push into treeOps a move of a node to the current inserter position
Parameters:
Name | Type | Description |
---|---|---|
removerStep |
ve.dm.TreeCursor.Step | The remover step over the node |
- Source:
pushMoveTextOp(removerStep)
#
Push into treeOps a move of some text to the current inserter position
Parameters:
Name | Type | Description |
---|---|---|
removerStep |
ve.dm.TreeCursor.Step | The remover step over the text |
- Source:
pushRemoveLast()
#
Push into treeOps the removal of the last remover step, which must be 'cross' or 'close'
- Source:
pushRemoveLastIfInDeletions()
#
Push into treeOps the removal of the last remover step, if that item marked as deleted
- Source:
pushRemoveNodeOp(removerStep)
#
Push into treeOps a removal of a node
Parameters:
Name | Type | Description |
---|---|---|
removerStep |
ve.dm.TreeCursor.Step | The remover step over the node |
- Source:
pushRemoveTextOp(removerStep)
#
Push into treeOps a removal of some text
Parameters:
Name | Type | Description |
---|---|---|
removerStep |
ve.dm.TreeCursor.Step | The remover step over the text |
- Source:
setup(document)
#
Setup state variables
Parameters:
Name | Type | Description |
---|---|---|
document |
ve.dm.Document | The document to be processed |
- Source:
applyTreeOperation(isReversed, document, treeOp)static
#
Apply a tree operation to document tree and linear data simultaneously
Parameters:
Name | Type | Description |
---|---|---|
isReversed |
boolean | Whether the transaction is an undo |
document |
ve.dm.Document | The document to modify |
treeOp |
Object | The tree operation |
- Source:
applyTreeOperations(isReversed, document, treeOps)static
#
Apply tree operations to document tree and linear data simultaneously
Parameters:
Name | Type | Description |
---|---|---|
isReversed |
boolean | Whether the transaction is an undo |
document |
ve.dm.Document | The document to modify |
treeOps |
Array.<Object> | The tree operations |
- Source: