The Editor is a core component of mobiledoc-kit. After instantiating an editor, use render to display the editor on the web page.

An editor uses a Post internally to represent the displayed document. The post can be serialized as mobiledoc using serialize. Mobiledoc is the transportable "over-the-wire" format (JSON) that is suited for persisting and sharing between editors and renderers (for display, e.g.), whereas the Post model is better suited for programmatic editing.

The editor will call registered callbacks for certain state changes. These are:

  • cursorDidChange -- The cursor position or selection changed.
  • postDidChange -- The contents of the post changed due to user input or programmatic editing. This hook can be used with serialize to auto-save a post as it is being edited.
  • inputModeDidChange -- The active section(s) or markup(s) at the current cursor position or selection have changed. This hook can be used with activeMarkups and activeSections to implement a custom toolbar.
  • onTextInput -- Register callbacks when the user enters text that matches a given string or regex.
  • beforeToggleMarkup -- Register callbacks that will be run before applying changes from toggleMarkup

Hierarchy

  • Editor

Implements

  • EditorOptions

Constructors

Properties

atoms: AtomData[]
autofocus: boolean
cardOptions: {}

Type declaration

    cards: CardData[]
    element: HTMLElement
    hasRendered: boolean
    html: Option<string>
    isDestroyed: boolean
    isEditable: boolean
    mobiledoc: Option<Mobiledoc>
    parserPlugins: SectionParserPlugin[]
    placeholder: string
    post: default
    showLinkTooltips: boolean
    spellcheck: boolean
    text: Option<string>
    tooltipPlugin: TooltipPlugin
    undoBlockTimeout: number
    undoDepth: number
    unknownAtomHandler: CardRenderHook
    unknownCardHandler: CardRenderHook

    Accessors

    • get activeSections(): default[]
    • The sections from the cursor's selection start to the selection end

      Returns default[]

    Methods

    • Parameters

      • queueName: string
      • callback: LifecycleCallback

      Returns void

    • Parameters

      • queueName: string
      • callback: LifecycleCallback

      Returns void

    • Register a callback that will be run before toggleMarkup is applied. If any callback returns literal false, the toggling of markup will be canceled. Note this only applies to calling editor#toggleMarkup. Using editor.run and modifying markup with the postEditor will skip any beforeToggleMarkup callbacks.

      Parameters

      • callback: LifecycleCallback

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called every time the cursor position (or selection) changes.

      Returns void

    • Convenience for PostEditor#deleteAtPosition. Deletes and puts the cursor in the new position.

      Parameters

      • position: Position
      • direction: number
      • __namedParameters: { unit: TextUnit }
        • unit: TextUnit

      Returns void

    • Convenience for PostEditor#deleteRange. Deletes and puts the cursor in the new position.

      Parameters

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called after deleting.

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called after the editor is rendered.

      Returns void

    • Parameters

      • callback: LifecycleCallback

        Called with postEditor as its argument.

      Returns void

    • Keep the user from directly editing the post using the keyboard and mouse. Modification via the programmatic API is still permitted.

      See

      Editor#enableEditing

      Returns void

    • Change a cardSection into display mode If called before the card has been rendered, it will be marked so that it is rendered in display mode when it gets rendered.

      Returns

      undefined

      Parameters

      • cardSection: default<CardPayload>

      Returns void

    • Change a cardSection into edit mode If called before the card has been rendered, it will be marked so that it is rendered in edit mode when it gets rendered.

      Parameters

      • cardSection: default<CardPayload>

      Returns void

    • Allow the user to directly interact with editing a post via keyboard and mouse input. Editor instances are editable by default. Use this method to re-enable editing after disabling it.

      See

      Editor#disableEditing

      Returns void

    • Turns on verbose logging for the editor.

      Parameters

      • Optional logTypes: string[] = []

        If present, only the given log types will be logged.

      Returns void

    • Whether the editor has a cursor (or a selected range). It is possible for the editor to be focused but not have a selection. In this case, key events will fire but the editor will not be able to determine a cursor position, so they will be ignored.

      Returns

      Returns boolean

    • Parameters

      • callback: LifecycleCallback

        Called when the editor's state (active markups or active sections) has changed, either via user input or programmatically

      Returns void

    • Inserts an atom at the current cursor position. If the editor has no current cursor position, nothing will be inserted. If the editor's range is not collapsed, it will be deleted before insertion.

      Returns

      The inserted atom.

      Parameters

      • atomName: string
      • atomText: string = ''
      • atomPayload: AtomPayload = {}

      Returns Maybe<default>

    • Inserts a card at the section after the current cursor position. If the editor has no current cursor position, nothing will be inserted. If the editor's range is not collapsed, it will be deleted before insertion. If the cursor is in a blank section, it will be replaced with a card section. The editor's cursor will be placed at the end of the inserted card.

      Returns

      The inserted Card section.

      Parameters

      • cardName: string
      • cardPayload: CardPayload = {}
      • inEditMode: boolean = false

        Whether the card should be inserted in edit mode.

      Returns Maybe<default<CardPayload>>

    • Inserts the text at the current cursor position. If the editor has no current cursor position, nothing will be inserted. If the editor's range is not collapsed, it will be deleted before insertion.

      Parameters

      • text: string

      Returns void

    • Register a handler that will be invoked by the editor after the user enters matching text.

      Parameters

      • inputHandler: TextInputHandlerListener

      Returns void

    • Parameters

      • callback: LifecycleCallback

        Called when the post has changed, either via user input or programmatically. Use with serialize to retrieve the post in portable mobiledoc format.

      Returns void

    • Parameters

      • rawKeyCommand: KeyCommand

      Returns void

    • Removes an attribute from the current active section(s).

      See

      PostEditor#removeAttribute

      Parameters

      • key: string

        The attribute. The only valid attribute is 'text-align'.

      Returns void

    • Parameters

      • element: HTMLElement

        The DOM element to render into. Its contents will be replaced by the editor's rendered post.

      Returns void

    • Run a new post editing session. Yields a block with a new PostEditor instance. This instance can be used to interact with the post abstract. Rendering will be deferred until after the callback is completed.

      Usage:

        let markerRange = this.range;
      editor.run((postEditor) => {
      postEditor.deleteRange(markerRange);
      // editing surface not updated yet
      postEditor.schedule(() => {
      console.log('logs during rerender flush');
      });
      // logging not yet flushed
      });
      // editing surface now updated.
      // logging now flushed

      Returns

      The return value of callback.

      Type Parameters

      Type Parameters

      • T

      Parameters

      • callback: ((postEditor: default) => T)

        Called with an instance of PostEditor as its argument.

          • (postEditor: default): T
          • Parameters

            • postEditor: default

            Returns T

      Returns T

    • Parameters

      • queueName: string
      • Optional args: unknown[]

      Returns void

    • Selects the given range or position. If given a collapsed range or a position, this positions the cursor at the range's position. Otherwise a selection is created in the editor surface encompassing the range.

      Parameters

      Returns void

    • Returns

      Serialized mobiledoc

      Parameters

      • version: keyof VersionTypes = MOBILEDOC_VERSION

        The mobiledoc version to serialize to.

      Returns Mobiledoc

    • Serialize the editor's post to the requested format. Note that only mobiledoc format is lossless. If cards or atoms are present in the post, the html and text formats will omit them in output because the editor does not have access to the html and text versions of the cards/atoms.

      Returns

      The editor's post, serialized to format

      Parameters

      • format: MOBILEDOC

        The format to serialize ('mobiledoc', 'text', 'html')

      Returns Mobiledoc

    • Parameters

      • format: HTML | TEXT

      Returns string

    • Sets an attribute for the current active section(s).

      See

      PostEditor#setAttribute

      Parameters

      • key: string

        The attribute. The only valid attribute is 'text-align'.

      • value: string

        The value of the attribute.

      Returns void

    • Toggles the given markup at the editor's current Range. If the range is collapsed this changes the editor's state so that the next characters typed will be affected. If there is text selected (aka a non-collapsed range), the selections' markup will be toggled. If the editor is not focused and has no active range, nothing happens. Hooks added using #beforeToggleMarkup will be run before toggling, and if any of them returns literal false, toggling the markup will be canceled and no change will be applied.

      See

      PostEditor#toggleMarkup

      Parameters

      • markupOrString: string | Markup
      • attributes: Dict<string> = {}

        e.g. { href: "https://bdg.com" }

      Returns void

    • Toggles the tagName for the current active section(s). This will skip non-markerable sections. E.g. if the editor's range includes a "P" MarkupSection and a CardSection, only the MarkupSection will be toggled.

      See

      PostEditor#toggleSection

      Parameters

      • tagName: string

        The new tagname to change to.

      Returns void

    • Parameters

      • context: HTMLElement
      • eventName: "compositionstart" | "compositionend" | "copy" | "cut" | "paste" | "drop" | "keydown" | "keypress" | "keyup"
      • event: DOMEventForType<"compositionstart" | "compositionend" | "copy" | "cut" | "paste" | "drop" | "keydown" | "keypress" | "keyup">

      Returns void

    • Parameters

      • name: string

        If the keyCommand event has a name attribute it can be removed.

      Returns void

    • Unregister text input handler by name

      Parameters

      • name: string

        The name of handler to be removed

      Returns void

    • Parameters

      • callback: LifecycleCallback

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called before deleting.

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called before handling new line.

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called before pasting.

      Returns void

    • Parameters

      • callback: LifecycleCallback

        This callback will be called before the editor is rendered.

      Returns void

    Generated using TypeDoc