Expand all

ve.dm.TreeModifier

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:
DataModel tree modifier, following the algorithm in T162762.

Properties

adjustmentTree #

Properties:

Name Type Description
adjustmentTree Object

Sparse tree, paths as indexes

Properties:
Name Type Description
inserted number

Child items inserted at this position

removed number

Child items removed at this position

i Object

Subtree at position i

Source:

data #

Properties:

Name Type Description
data Array

Live document linear data

Source:

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 #

Properties:

Name Type Description
inserter ve.dm.TreeCursor

Tree cursor for insertions

Source:

remover #

Properties:

Name Type Description
remover ve.dm.TreeCursor

Tree cursor for removals

Source:

treeOps #

Properties:

Name Type Description
treeOps Array.<Object>

Array of tree ops being built

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>
Get the adjusted position of a raw inserter position, XXX

adjustRemoverPosition(rawPosition) → {Array.<number>} #

Get the adjusted position of a raw remover position

Parameters:

Name Type Description
rawPosition Array.<number>

The raw remover position

Source:

Returns:

Adjusted pathAndOffset, with offsets inside a ContentBranchNode linearized

Type
Array.<number>
Get the adjusted position of a raw remover position

calculateTreeOperations(transaction) #

Transform linear operations into tree operations

Parameters:

Name Type Description
transaction ve.dm.Transaction

The transaction

Source:
Transform linear operations into tree operations

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
Throw an error if the inserter node cannot take a child of a specified type

checkCanInsertText() #

Throw an error if the inserter is not in a content position

Source:

Throws:

Cannot insert text into a foo node

Type
Error
Throw an error if the inserter is not in a content position

cursorsMatch() → {boolean} #

Test whether both pointers point to the same location

Source:

Returns:

True if the paths and offsets are identical

Type
boolean
Test whether both pointers point to the same location

doesTypeTakeContent(type) → {boolean} #

Test whether a node type takes content

Parameters:

Name Type Description
type string

The name of a ve.dm.Node type

Source:

Returns:

Whether that type takes content

Type
boolean
Test whether a node type takes content

findOrCreateAdjustmentNode(position) → {Object} #

Return adjustment tree node at a given offset path, inserting it if necessary

Parameters:

Name Type Description
position Array.<number>

Offset path in the adjustment tree

Source:

Returns:

The adjustment tree node

Type
Object
Return adjustment tree node at a given offset path, inserting it if necessary

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>
Get the adjusted position of a raw position

getRawInserterPosition() → {Array.<number>} #

Get the raw position at the inserter

Source:

Returns:

The pathAndOffset, with offsets inside a ContentBranchNode linearized

Type
Array.<number>
Get the raw position at the inserter

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>
Get the raw position of a node, with offsets inside a ContentBranchNode linearized

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>
Get the raw position of a node stepped over by the remover

getTypeAtInserter() → {string} #

Get the type of the node in which the inserter lies

Source:

Returns:

The node type

Type
string
Get the type of the node in which the inserter lies

isInsertionContent() → {boolean} #

Test whether the inserter is at a content position

Source:

Returns:

Whether the inserter is at a content position

Type
boolean
Test whether the inserter is at a content position

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:
Modify the adjustment in the adjustment tree at a given offset path

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:
The top level method: modify document tree according to transaction

processImplicitFinalRetain() #

Retain to the end of the content

Source:
Retain to the end of the content

processInsert(itemOrData) #

Process the insertion an open tag, a close tag, or an array of text items

Parameters:

Name Type Description
itemOrData Object | Array

An open tag, a close tag, or an array of text items

Source:
Process the insertion an open tag, a close tag, or an array of text items

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:

Name Type Description
itemOrData Object | Array

An open tag, a close tag, or an array of text items

Source:
Process the removal of some items

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:
Push into treeOps the node insertion at the current inserter position

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:
Push into treeOps the text insertion at the current inserter position

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:
Push into treeOps a move of a node to the current inserter position

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:
Push into treeOps a move of some text to the current inserter position

pushRemoveLast() #

Push into treeOps the removal of the last remover step, which must be 'cross' or 'close'

Source:
Push into treeOps the removal of the last remover step, which must be 'cross' or 'close'

pushRemoveLastIfInDeletions() #

Push into treeOps the removal of the last remover step, if that item marked as deleted

Source:
Push into treeOps the removal of the last remover step, if that item marked as deleted

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:
Push into treeOps a removal of a node

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:
Push into treeOps a removal of some text

setup(document) #

Setup state variables

Parameters:

Name Type Description
document ve.dm.Document

The document to be processed

Source:
Setup state variables

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:
Apply a tree operation to document tree and linear data simultaneously

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:
Apply tree operations to document tree and linear data simultaneously

checkEqualData(actual, expected)static #

Throw an exception if two pieces of linear data are not equal

Parameters:

Name Type Description
actual Array

Document linear data to test

expected Array

Expected linear data to test against

Source:
Throw an exception if two pieces of linear data are not equal