Class View

Views allow end users to interact with an Editor through an interface component similar to a text field. Views support editing by keyboard, mouse, and touch. For a richer user experience, the view can be augmented by a Shell or used as a foundation for a custom interface.

New views are not visible until their rootElement is added to the DOM. When adding a view to the DOM, the addToDom method must be called to ensure that the view "connects" to the editor to receive and process updates.

A single editor may have multiple views. However, a given view is always tied to a single editor instance, the one passed to its constructor. To create the illusion of a single view switching between multiple editors, either use multiple views and hide those not in use, or destroy and replace a single view on demand.

Example: Create and add a View to the page

let view = new micd.View();
view.addToDom(document.body);
view.focus();

Hierarchy

  • View

Index

Constructors

constructor

  • Creates a new view for the specified editor. If no editor is given, a new editor is created automatically.

    Parameters

    • Optional editor: Editor

      The editor instance that this view will present.

    Returns View

Properties

abbreviationExpansionVisible

abbreviationExpansionVisible: boolean

Gets or sets whether this view will display a floating control near the caret that shows what the effect of expanding an abbreviation would be. The default is true.

see

abbreviationMap

Readonly abbreviationMap

abbreviationMap: InputMap<string>

Gets this view's abbreviation map.

The keys in this map are abbreviations: strings of characters that can be expanded to convert them into math objects or other actions. Abbreviations are used by typing them into the editor and then using the expand abbreviation command, which is bound to the space key by default. Abbreviations can consist of letters (which match scalar variables), digits, and operator symbols (which match simple operators using that symbol). They may not begin with digits. Abbreviations that contain operator symbols are matched more strictly than other types.

Abbreviation maps contain the following types of abbreviations:

  • Standard: A standard abbreviation maps to a command or custom action. When a standard abbreviation is the longest matching abbreviation, it is guaranteed to be selected when the expand abbreviation command is used. It may also be selected if only a prefix has been entered, if no better match is found.
  • Fragment: A fragment can only be mapped to a command. It consists of lowercase letters from a to z and is matched case-insensitively against the text to be expanded. Unlike other abbreviation types, a fragment can match multiple commands. Fragments are useful for assigning aliases to existing commands. Fragments are marked by prefixing the abbreviation string with a # (this is not considered part of the abbreviation).
  • Prefix mapper: A prefix mapper matches a specific prefix and then determines how to expand the abbreviation by evaluating a function against the remaining characters. Prefix mappers are marked by a * following the prefix that they match. For example, the unit mapper u* is applied to abbreviations starting with the letter u to create unit objects. At this time, they can be removed from, but not added to, the map (except by adding the default mappings).
  • Generic mapper: Like a prefix mapper, a generic mapper evaluates a function to determine what, if any, expansion it should produce. However, generic mappers are always consulted for every expansion attempt. Generic mappers consist of a . followed by a string that describes the mapper's purpose. At this time, they can be removed from, but not added to, the map (except by adding the default mappings).
see

abbreviationExpansionVisible

disabled

disabled: boolean

Gets or sets whether this view is disabled. A disabled view is non-interactive. It ignores keyboard, mouse, and touch input and is rendered in a different style. This does not prevent document content from being modified (see Editor.readOnly).

Readonly editor

editor: Editor

The editor controlled by this view.

fontScale

fontScale: number

Gets or sets this view's relative font size. This is a scaling factor relative to the default font size for the view. The initial value is 1. Setting this to a value greater than 1 will increase the size, while setting this to a positive value less than 1 will decrease the size. The base font size for new views is determined by the ThemeOptions. By default, this size is ultimately derived from the default font size set by the user in their browser options.

throws

RangeError If the scale is not a positive number.

Readonly gestureMap

gestureMap: InputMap<string>

Gets this view's key binding map.

The keys in this map are strings that describe keyboard gestures. They can consist of either a single printable character, or else a description of a key combination. A key combination consists of an optional modifier name separated from a key name by a space character, such as "Ctrl ArrowLeft". On Apple devices, modifier names that include Ctrl or Alt are automatically mapped to the Command and Option keys, respectively.

To improve cross-platform compatibility, only certain modifier key combinations are allowed; a typed enumeration of these combinations is available as micd.Keystroke.

lineNumbers

lineNumbers: boolean

Gets or sets whether this view numbers the document lines. Attempting to set this to true when the editor is single line will have no effect.

menuCustomizer

menuCustomizer: function

The menuCustomizer property can be set to a function that will be called to customize the context menu before it is shown. The function is called with the default menu template and this view instance. It can return the template with or without changes, or return null to prevent display of the menu.

Type declaration

Readonly rootElement

rootElement: HTMLElement

Gets the DOM element that contains the view.

see

addToDom

Methods

addEventListener

  • addEventListener<E>(type: E, listener: EventListener<ViewEventTypes[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: "caretescape"

    Parameters

    • type: E

      A string naming the event type.

    • listener: EventListener<ViewEventTypes[E]>

      The event listener to call when the event occurs.

    Returns void

addToDom

  • addToDom(parentNode?: Element, beforeThisSibling?: Element): void
  • Adds the view's root element to the HTML document tree, or informs the view that it was just added. For a view to function correctly, it must be aware of whether or not it is part of the DOM. As there is no way to determine this automatically in all supported browsers, this method must be called explicitly. Typical use is to pass a parent node already in the DOM to this method. It will then add the root element as if by parent.appendChild(rootElement). Or, if the second argument is also supplied, as if by parent.insertBefore(rootElement, beforeThisSibling). If a more complex insertion is required, rootElement can be added to the DOM separately and then this method called with no arguments.

    throws

    Error If the editor element is not, in fact, in the document tree (after any positioning specified by the arguments).

    see

    removeFromDom

    Parameters

    • Optional parentNode: Element

      If supplied, the view's root element will be appended to this node's children.

    • Optional beforeThisSibling: Element

      If supplied in addition to parentNode, the view's root element will be inserted before this node in the parent's children instead of appending it to the end.

    Returns void

apply

  • Applies the specified command if possible. This method is a convenience that will route the request through the appropriate objects.

    see

    apply

    Parameters

    Returns boolean

    True if the command could be applied.

  • Applies the specified Action if possible. This method is a convenience that will route the request through the appropriate objects.

    see

    apply

    Parameters

    Returns boolean

    True if the action could be applied.

  • Applies the specified Clip if possible. This method is a convenience that will route the request through the appropriate objects.

    see

    apply

    Parameters

    Returns boolean

    True if the clip could be applied.

blur

  • blur(): void
  • Requests that the view lose input focus, if it currently has it.

    see

    isFocused

    Returns void

canApply

  • Returns whether the specified command can currently be applied. This method is a convenience that will route the request through the appropriate objects.

    see

    apply

    Parameters

    Returns boolean

    True if the command could be applied.

  • Returns whether the specified Action can currently be applied. This method is a convenience that will route the request through the appropriate objects.

    see

    apply

    Parameters

    • toApply: Action

      The action to test.

    Returns boolean

    True if the action could be applied.

  • Returns whether the specified Clip can currently be applied. This method is a convenience that will route the request through the appropriate objects.

    see

    apply

    Parameters

    Returns boolean

    True if the clip could be applied.

focus

  • focus(): void
  • Requests that the view be given input focus.

    see

    isFocused

    Returns void

getNodeRects

  • getNodeRects(node: Node, field?: number): DOMRect[]
  • Returns an array of DOMRect objects that describe bounding rectangles for the visual representation of the node in this view. If the node is not in the view, possibly because the view is still updating, the array will be empty. Otherwise, in most cases the node will be represented as a single rectangle. The exception is if the node has been wrapped over multiple view lines. In this case, there will be one rectangle for each line that the node spans.

    see

    Walker

    experimental

    May change or be removed in future releases.

    Parameters

    • node: Node

      The math document node to get bounding rectangles of.

    • Optional field: number

      The field within the node to get bounding rectangles of, or -1 for the entire node. The default is -1.

    Returns DOMRect[]

    Zero or more rectangles, with coordinates relative to the upper-left corner of the page.

isFocused

  • isFocused(): boolean
  • Returns whether the view has input focus.

    see

    focus

    Returns boolean

    True if the view will currently receive keyboard input.

onceUpToDate

  • onceUpToDate(): Promise<View>
  • Returns a Promise that resolves once any pending updates to this view have been completed.

    Returns Promise<View>

    A Promise that resolves when this view is up to date.

pressKey

  • pressKey(gesture?: string): void
  • Simulates the user pressing a single key combination. This method does not require the view to have input focus. Passing null or undefined has no effect.

    throws

    TypeError If the string is not a valid gesture.

    see

    type

    Parameters

    • Optional gesture: string

      The key to simulate, described in the same format used by View.gestureMap.

    Returns void

print

  • print(): Promise<View>
  • Prints the math document to a printer.

    Returns Promise<View>

    A Promise that resolves to this view once the layout is prepared and printing begins.

removeEventListener

  • removeEventListener<E>(type: E, listener: EventListener<ViewEventTypes[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: "caretescape"

    Parameters

    • type: E

      A string naming the event type.

    • listener: EventListener<ViewEventTypes[E]>

      The event listener to stop notifying when the event occurs.

    Returns void

removeFromDom

  • removeFromDom(): void
  • Removes the view's root element from the HTML document tree, or informs the view that it was just removed. For the View to function correctly, it must be aware of whether or not it is part of the DOM. As there is no way to determine this automatically in all supported browsers, this method must be called explicitly. If the root element is in the DOM when this method is called, it will be removed by removing it from its immediate parent.

    see

    addToDom

    Returns void

scrollToCaret

  • scrollToCaret(): void
  • Scrolls the view so that the caret is visible. Scrolling may not be immediate; it may be scheduled to occur in the near future after other pending updates. The View will not be scrolled if it does not have focus at the scheduled time. This method only scrolls within the View. It does not scroll the page (or other container) to ensure that the View is visible on the page. This can be done by calling, for example, view.rootElement.scrollIntoView().

    Returns void

scrollToNode

  • scrollToNode(node: Node): void
  • Scrolls the view so that the specified document node is visible. Has no effect if the node is null or the node is not in the document of this view.

    see

    Walker

    Parameters

    • node: Node

      The math document node to scroll to.

    Returns void

scrollToPosition

  • scrollToPosition(position: string): void
  • Scrolls to the specified document position. Has no effect if the position is null or invalid.

    Parameters

    • position: string

      The position to scroll to.

    Returns void

type

  • type(input: string): void
  • Simulates the user typing a sequence of characters on the keyboard. The view does not need to be focused or even added to the DOM. In addition to regular printable characters, the string may include \n (U+000A) to simulate typing Enter, \b (U+0008) to simulate typing Backspace, and U+007F to simulate typing Delete. Abbreviations can be used to insert complex math objects (for example, ".sum "—note the space). Commands that do not have an assigned abbreviation can be selected by their full name (for example, `".moveLeft "), although Editor.apply is more efficient.

    see

    pressKey

    Parameters

    • input: string

      The characters to simulate typing.

    Returns void

Static getAddedViews

  • getAddedViews(): View[]
  • Returns a new array of all of the views that are currently on the page.

    see

    shell.getAddedShells

    Returns View[]

    A (possibly empty) array in which each element is a view that has been added to the DOM.

Static thatContainsElement

  • thatContainsElement(element: Element): View
  • Returns the view that contains the specified element, or null if the element is not part of a view.

    see

    shell.thatContainsElement

    Parameters

    • element: Element

      The element (or DOM node) that may be a descendent of a view.

    Returns View

    The view that the element is a descendent of, or null.

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