Class Walker

Explores and transforms the contents of an Editor. A walker maintains a position within an editor's document similar to, but generally independent of, the caret position. This position can be moved through the document, and the Nodes that make up the document can be examined and modified.

The walker position is changed using any of a collection of movement methods accessed through the moveTo property. Content can be inserted at the walker position using a collection of methods accessed through the insert property. Other editor commands can be applied at the walker position using the apply method. Each of these also has a can variant that tests whether an action is possible without actually performing it.

The document node at the walker position can be examined using the node property. To explore or modify nodes recursively, visit the nodes with a VisitorFunction.

Example: Use a Walker to append a variable to the end of a document

let walker = new micd.Walker(editor);
walker.moveTo.documentEnd();
walker.insert.variable("x");

Hierarchy

  • Walker

Index

Constructors

constructor

  • Creates a new walker that can examine and modify the contents of the specified editor.

    Parameters

    • sourceEditor: Editor

      The editor to walk through.

    Returns Walker

Properties

Readonly canInsert

A set of methods used to check whether it is possible to insert a given type of content at the current position without actually inserting anything. For example, walker.canInsert.object("squareRoot") would return whether it is possible to insert a sqaure root object at the current position. Note that if an insertion is allowed, in rare cases it may not occur at the exact walker position. For example, it is allowed to insert a math object inside of a text node, but the result will be to split the text node and place the math object between the two parts.

Readonly canMoveTo

A set of methods used to check whether a given move is possible without actually performing it. For example, walker.canMoveTo.line(99) will return false if the document has less than 100 lines.

Readonly clipOf

A set of methods used to create Clips of content that can then be inserted into the document. For example, walker.clipOf.childField(0) would return a copy of the entire contents of the first child field of the node at the current position as a Clip.

Readonly insert

A set of methods used to insert content at the current position. Content is inserted as if the caret was just before the node (if any) at the current position. For example, walker.insert.number(1.618) will insert the number 1.618 as a sequence of digit nodes at the current position. To check whether an insertion is possible without actually doing it, use canInsert instead.

Readonly moveTo

A set of methods used to move this walker to a new position in the document. For example, walker.moveTo.caret() will move this walker to the current caret position. To check whether a move is possible without actually moving, use canMoveTo instead.

Readonly node

node: Node

The node at the current position, or null if there is no node there. (There is no node in the last position of a field as this position is used to append new nodes to the field.)

Readonly numLines

numLines: number

The number of lines in the document. Equivalent to walker.rootNode.numFields.

position

position: string

A string that describes this walker's current position within the editor document as a path starting from the document root node. This can be used to get or set the current position directly.

throws

Throws an error if set to a string that is null or that does not describe a valid position.

Readonly relativePosition

relativePosition: [Node, number, number]

An array that describes the position of this walker relative to the node that directly contains that position, consisting of the node itself, the field, and the offset from the start of the field. If this position refers to a non-null node, then the value is the same as node.relativePosition. However, this property is defined even if node is null.

Readonly rootNode

rootNode: Node

The root node of the edited document. The editor always has a valid document root, although the root can change if the entire document is replaced using Editor.value.

Readonly visit

A set of methods used to visit parts of a document by calling a visitor function on each node in turn.

Methods

apply

  • Applies the specified command at the current position of the walker. The effect is as if the caret was moved to the position of the walker, the command was applied, and then the walker position was updated to the match the new position of the caret. If there is a selection active when this is called, and the selection includes the current position, then the command will be applied using the selection. Otherwise, it will be applied as if there were no selection.

    Parameters

    Returns boolean

    True if the command was applied.

canApply

  • Returns whether the specified command can be applied at the current position of the walker. This may or may not be the same as the position of the caret.

    Parameters

    Returns boolean

    True if the command is valid and can be applied at the current position.

moveCaretToPosition

  • moveCaretToPosition(): void
  • Moves the caret position in the editor to match the current position in this walker. Equivalent to editor.caretPosition = walker.position.

    Returns void

This API is still under development and is subject to change. Copyright © Math I Can Do Solutions Incorporated and/or its licensors.