Class Editor

Editors manage and manipulate math content, track the caret position and selection, support the execution of commands, and enable serialization and conversion of the content under their control.

Each editor is responsible for maintaining a model of its content, the math document. In order to ensure that the document remains consistent, it cannot be accessed or mutated directly. The most common way to manipulate the document model is by applying commands. To implement more complex operations, a Walker can be used to safely and efficiently examine or modify the document.

The editor also manages access to the document's metadata, a string map of key and value pairs that can be used to store both standard and user-defined keys as part of the document.

An editor does not include a user interface. To present the contents of an editor to the end user and/or allow them to edit its content, create a View for it.

Example: Create an editor, apply a command to it, and view the result

let ed = new micd.Editor();
ed.apply(micd.MathObject.thereExists);
let view = new micd.View(ed);
view.addToDom(document.body);

Hierarchy

  • Editor

Index

Constructors

constructor

  • Creates a new editor with a new, empty document.

    Parameters

    • Optional options: NewEditorOptions

      Options that configure the new editor's features.

    Returns Editor

Properties

caretPosition

caretPosition: string

Sets or gets the caret position.

throws

TypeError If set to a value that is not a valid position.

see

Walker.position

Readonly inCompoundEdit

inCompoundEdit: boolean

Returns whether the document is currently in the middle of an edit operation. This is true, for example, when a compound edit has begun but not yet ended. Example:

editor.beginCompoundEdit();
try {
   console.log(editor.inCompoundEdit); // true
} finally {
   editor.endCompoundEdit();
}

Readonly metadata

Provides access to the document's metadata, such as its title and memory cell contents.

readOnly

readOnly: boolean

Sets or gets whether the user can change the editor's content. When false, commands that modify the document will not be applicable: canApply will return false for them and they will have no effect if applied. This does not prevent the document from being modified directly: for example, the document can be replaced by setting the value property.

selection

selection: string

Sets or gets the selection range. Setting this to null will clear the selection, if any. If the selection is set to a range that does not contain the caret position, the caret will be moved to the end of the selection.

throws

TypeError If set to a value that is not a valid selection.

Readonly stateToken

stateToken: number

Returns a token that changes to a different unique value each time the document is edited, the caret is moved, or the selection changes. Compound edits count as a single combined state change, the the token changes as soon as the edit begins. Changes to the document metadata do not affect the state token. The state token can be used to determine whether a previously validated action still applies. For example, if an Action requires an asynchronous step, such as awaiting data from a server, an implementation can compare the state token value from when the action was approved to the state token value after the data is received. If they are different, the action should either be revalidated or cancelled.

value

value: string

Sets or gets the content of the editor. Setting this to null replaces the content with a new, empty document. This property accepts and returns strings in the Math I Can Do document format.

throws

TypeError If set to an invalid document, or to a newer version than the current API version can support.

Methods

addEventListener

  • addEventListener<E>(type: E, listener: EventListener<EditorEventTypes[E]>): void
  • Adds a listener for the specified type of event.

    throws

    TypeError If the event type is not a known type.

    see

    removeEventListener

    Type parameters

    • E: keyof EditorEventTypes

    Parameters

    • type: E

      A string naming the event type. For example, the "change" type is used to listen for document modifications.

    • listener: EventListener<EditorEventTypes[E]>

      The event listener to call when the event occurs.

    Returns void

apply

  • Applies the specified command if possible.

    see

    canApply

    Parameters

    Returns boolean

    True if the command could be applied.

beginCompoundEdit

  • beginCompoundEdit(): void
  • Begins a compound edit operation that lasts until endCompoundEdit is called. Changes to the document made between these calls, such as by applying commands, are combined into a single macro edit. Views will not update until the edit is completed, and the entire macro edit will form a single entry in the edit history. This call can be nested; the compound edit will not be complete until a matching number of calls are made to endCompoundEdit.

    Returns void

canApply

  • Returns whether the specified command can currently be applied.

    see

    apply

    Parameters

    Returns boolean

    True if the command could be applied.

canRedo

  • canRedo(): boolean
  • Returns whether there is an edit in the edit history that can be redone.

    Returns boolean

    Returns true if redo is possible.

canUndo

  • canUndo(): boolean
  • Returns whether there is an edit in the edit history that can be undone.

    Returns boolean

    Returns true if undo is possible.

clearSelection

  • clearSelection(): void
  • Clears the current selection, if any.

    Returns void

downloadImage

  • Downloads an image of the document content asynchronously. If a title is set in the document metadata, it will be used for the download's file name if possible. Image conversion may fail if the content is so large that it exceeds reasonable limits on image size or memory use.

    Parameters

    • preferSelection: boolean

      If true, uses the selection instead of the whole document if possible.

    • options: ImageFormatOptions

      Options that control the image format and features.

    Returns void

endCompoundEdit

  • endCompoundEdit(): void
  • Ends a compound edit operation started by a previous beginCompoundEdit. Because compound edits nest, it is vital that calls to this method are placed in a finally clause to ensure that "begins" and "ends" are always executed in pairs. Otherwise, an error during the compound operation may result in the editor becoming unusable. Example:

    editor.beginCompoundEdit();
    try {
       // ... edits ...
    } finally {
       editor.endCompoundEdit();
    }
    
    throws

    Error If called when not paired with a previous call to beginCompoundEdit.

    Returns void

fromSemanticObject

  • Replaces the content of this editor by converting the supplied semantic object into the internal document format.

    experimental

    May change or be removed in future releases.

    Parameters

    Returns SemanticObjectParserError[]

    An array, possibly empty, of any errors encountered during the import process.

hasSelection

  • hasSelection(): boolean
  • Returns whether the editor has an active selection.

    Returns boolean

    Returns true if content is selected in the editor.

isEmpty

  • isEmpty(): boolean
  • Returns whether the editor has any content. If true, the document consists of a single empty line.

    Returns boolean

    True if the document is empty.

redo

  • redo(): void
  • Redoes an edit, if possible. Otherwise does nothing.

    Returns void

removeEventListener

  • removeEventListener<E>(type: E, listener: EventListener<EditorEventTypes[E]>): void
  • Removes a previously added listener for the specified type of event. This method does nothing if the listener was never added.

    see

    addEventListener

    Type parameters

    • E: keyof EditorEventTypes

    Parameters

    • type: E

      A string naming the event type.

    • listener: EventListener<EditorEventTypes[E]>

      The event listener to stop notifying when the event occurs.

    Returns void

toImage

  • toImage(preferSelection?: boolean, options?: ImageFormatOptions): Promise<string | Blob>
  • Returns a Promise that resolves to an image representation of the document content. The form that the image takes depends on the container property of the supplied options, if any. By default, the promise will resolve to a data URL for the image. It can also be supplied as a Blob, or, if the image format is SVG, as a string. Other options allow the caller to specify colours, size, margins, and other features.

    Image conversion may fail if the content or scale causes the image to exceed reasonable limits on image size or memory use. Because the conversion process relies on the host platform's rendering capabilities, results will vary by device and browser.

    Parameters

    • Optional preferSelection: boolean

      If true, uses the selection instead of the whole document if possible.

    • Optional options: ImageFormatOptions

      Options that control the image format and features.

    Returns Promise<string | Blob>

    A Promise that resolves to a URL, Blob, or SVG document for the image.

toLatex

  • Returns a Promise that resolves to a LaTeX representation of the document content.

    Parameters

    • Optional preferSelection: boolean

      If true, uses the selection instead of the whole document if possible.

    • Optional options: LatexFormatOptions

      Options that control the conversion.

    Returns Promise<string>

    A Promise that resolves to the converted string of LaTeX.

toMml

  • Returns a Promise that resolves to a MathML representation of the document content.

    Parameters

    • Optional preferSelection: boolean

      If true, uses the selection instead of the whole document if possible.

    • Optional options: MmlFormatOptions

      Options that affect the conversion.

    Returns Promise<string>

    A Promise that resolves to the converted string of MathML.

toSemanticObject

  • Converts the content of the editor into a semantic object, a high-level representation useful for processing and conversion.

    experimental

    May change or be removed in future releases.

    Returns SemanticObject

    The root of a semantic representation of the document.

undo

  • undo(): void
  • Undoes an edit, if possible. Otherwise does nothing.

    Returns void

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