Class Action

The base class for creating custom editing actions. While built-in commands are applied through an editor, custom actions are activated through a particular view. This allows custom actions to mix low-level commands (through view.editor.apply) with higher-level functionality like View.type.

An action is typically activated when a view receives an input that maps to the action in one of its InputMaps. For example, the action may be added to a view's gesture map with a keyboard shortcut.

Example: Define an action that makes the current selection transparent

class MakeInvisibleAction extends micd.Action {
  canApplyTo(view) {
    return view.editor.hasSelection() && !view.editor.readOnly;
  }
  applyTo(view) {
    const ed = view.editor;
    ed.beginCompoundEdit();
    try {
      const selection = micd.Clip.from(ed);
      ed.apply(micd.EditorCommand.deleteSelection);
      view.type(".RGB0000 ");
      selection.applyTo(ed);
      ed.apply(micd.EditorCommand.forwardEnter);
    } finally {
      ed.endCompoundEdit();
    }
  }
  toString() {
    return "Make invisible";
  }
}

view.gestureMap.put(micd.Keystroke.CtrlI,
    new MakeInvisibleAction());
see

InputMap

Hierarchy

  • Action

Index

Constructors

Methods

Constructors

constructor

  • Creates a new action.

    Returns Action

Methods

applyTo

  • applyTo(view: View): void
  • Applies the action, if possible. The base class reminds developers that the method must be overridden, so subclasses should not invoke the super implementation.

    Parameters

    • view: View

      The view that the action was registered with.

    Returns void

canApplyTo

  • canApplyTo(view: View): boolean
  • Returns whether the action can currently be applied. The base class will always return true.

    This method must not modify the contents of the editor document.

    Parameters

    • view: View

      The view that the action was registered with.

    Returns boolean

    Returns true if and only if the action can be applied.

toString

  • toString(): string
  • Returns a string that describes the action to end users.

    Returns string

    A brief, human-friendly description of the action.

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