The MarkupEditor logo

Global

Members

(constant) MU

The object whose methods comprise the MarkupEditor API.

Methods

activeConfig() → {object|null}

Return the active editor's configurations as one object keyed to the config type.

Returns:

Object containing {"toolbar": ToolbarConfig, "keymap": KeymapConfig, "behavior": BehaviorConfig}.

Type
object | null

activeView() → {EditorView|null}

Return the active editor's view.

Returns:

The ProseMirror EditorView that is currently active.

Type
EditorView | null

addButton(id, parentId, cssClass, label)

Parameters:
Name Type Description
id string

The element ID of the button that will be added.

parentId string

The element ID of the parent DIV to place the button in.

cssClass string

The CSS class of the button.

label string

The label for the button.

addCol(direction)

Add a column before or after the current selection, whether it's in the header or body.

In MarkupEditor, the header is always colspanned fully, so we need to merge the headers if adding a column in created a new element in the header row.

Parameters:
Name Type Description
direction string

Either 'BEFORE' or 'AFTER' to identify where the new column goes relative to the selection.

addDiv(id, parentId, cssClass, attributesJSON, buttonGroupJSON, htmlContents)

Add a div with id to parentId.

Note that divs that contain a static button group are created in a single call that includes the buttonGroupJSON. However, button groups can also be added and removed dynamically. In that case, a button group div is added to a parent div using this call, and the parent has to already exist so that we can find it.

Parameters:
Name Type Description
id string

The id of the dive to add

parentId string

The id of the parent for this div

cssClass string

The class to assign to this div

attributesJSON string

A stringified object containing the editable attributes

buttonGroupJSON string

A stringified object containing a group of buttons in this div

htmlContents string

The inner HTML to place in this div

addHeader(colspan)

Add a header to the table at the selection.

Parameters:
Name Type Description
colspan boolean

Whether the header should span all columns of the table or not.

addRow(direction)

Add a row before or after the current selection, whether it's in the header or body. For rows, AFTER = below; otherwise above.

Parameters:
Name Type Description
direction string

Either 'BEFORE' or 'AFTER' to identify where the new row goes relative to the selection.

borderTable(border)

Set the class of the table to style it using CSS. The default draws a border around everything.

Parameters:
Name Type Description
border 'outer' | 'header' | 'cell' | 'none'

Set the class of the table to correspond to caller's notion of border, so it displays properly.

cancelSearch()

Cancel searching, resetting search state.

cmdItem(cmd, options) → {MenuItem}

Return a MenuItem that runs the command when selected.

The label is the same as the title, and the MenuItem will be enabled/disabled based on what cmd(state) returns unless otherwise specified in options.

Parameters:
Name Type Description
cmd Command

A ProseMirror Command

options object

The spec for the MenuItem

Returns:

Similar to prosemirror-menu, but MarkupEditor-modified

Type
MenuItem

consoleLog(string)

Callback to show a string in the console, like console.log(), but for environments like Xcode.

Parameters:
Name Type Description
string string

The string to package up as a console log message in a callback

cutImage()

Cut the selected image from the document.

Copy before deleting the image is done via a callback, which avoids potential CORS issues. Similarly, copying of an image (e.g., Ctrl-C) is all done by the side holding the copy buffer, not via JavaScript.

deactivateSearch(view)

Deactivate search mode, stop intercepting Enter to search.

Parameters:
Name Type Description
view EditorView | null

The view whose activeSearcher should be deactivated.

Remove the link at the selection, maintaining the same selection.

The selection can be at any point within the link or contain the full link, but cannot include areas outside of the link.

deleteTableArea(area)

Delete the area at the table selection, either the row, col, or the entire table.

Parameters:
Name Type Description
area 'ROW' | 'COL' | 'TABLE'

The area of the table to be deleted.

doRedo()

Redo the previously undone action.

doUndo()

Undo the previous action.

emptyDocument()

Clean out the document and replace it with an empty paragraph

emptyHTML() → {string}

Return the HTML that is contained in an empty document.

Returns:

An empty paragraph

Type
string

focus()

Focus immediately, leaving range alone.

focusOn(id)

Focus on the DIV with id.

Parameters:
Name Type Description
id string

The ID of the DIV to focus on.

focused(element)

Set the active document and report focus.

Parameters:
Name Type Description
element HTMLElement

The HTML element whose root node should become active

getDataImages() → {Array.<string>}

Return an array of src attributes for images that are encoded as data, empty if there are none.

Returns:
Type
Array.<string>

getHTML(pretty, clean, divID) → {string}

Get the contents of the div with id divID or of the full doc.

Note: Clean is needed to avoid the selected ResizableImage from being passed-back with spans around it, which is what are used internally to represent the resizing handles and box around the selected image. However, this content of the DOM is only for visualization within the MarkupEditor and should not be included with the HTML contents. It is available here with clean !== true as an option in case it's needed for debugging.

Parameters:
Name Type Description
pretty string

Set to "true" to format nicely for reading.

clean string

Set to "true" to remove spans and empty text nodes first.

divID string

The ID for the DIV to return HTML from.

Returns:

The HTML for the div with id divID or of the full doc.

Type
string

getHeight()

Return the height of the editor element that encloses the text.

The padding-block is set in CSS to allow touch selection outside of text on iOS. An unfortunate side-effect of that setting is that getBoundingClientRect() returns a height that has nothing to do with the actual text, because it's been padded. A workaround for this is to get the computed style for editor using window.getComputedStyle(editor, null), and then asking that for the height. It does not include padding. This kind of works, except that I found the height changed as soon as I add a single character to the text. So, for example, it shows 21px when it opens with just a single <p>Foo</p>, but once you add a character to the text, the height shows up as 36px. If you remove padding-block, then the behavior goes away. To work around the problem, we set the padding block to 0 before getting height, and then set it back afterward. With this change, both the touch-outside-of-text works and the height is reported accurately. Height needs to be reported accurately for auto-sizing of a WKWebView based on its contents.

getImageAttributes() → {object}

Return the image attributes at the selection

Returns:

An Object whose properties are the image attributes (like src, alt, width, height, scale) at the selection.

Type
object

getLinkAttributes() → {object}

Return the link attributes at the selection.

Returns:

An Object whose properties are the link attributes (like href, link) at the selection.

Type
object

getSelectionState() → {string}

Populate a dictionary of properties about the current selection and return it in a JSON form. This is the primary means that the find out what the selection is in the document, so we can tell if the selection is in a bolded word or a list or a table, etc.

Returns:

The stringified dictionary of selectionState.

Type
string

getTestHTML(sel)

Get the HTML contents and mark the selection from/to using the text identified by sel.

Parameters:
Name Type Description
sel string

An embedded character in contents indicating selection point(s)

indent()

Do a context-sensitive indent.

If in a list, indent the item to a more nested level in the list if appropriate. If in a blockquote, add another blockquote to indent further. Else, put into a blockquote to indent.

insertImage(src, alt)

Insert the image at src with alt text, signaling state changed when done loading. We leave the selection after the inserted image.

Parameters:
Name Type Description
src string

The url of the image.

alt string

The alt text describing the image.

Insert a link to url. When the selection is collapsed, the url is inserted at the selection point as a link.

When done, leave the link selected.

Parameters:
Name Type Description
url string

The url/href to use for the link

insertTable(rows, cols)

Insert an empty table with the specified number of rows and cols.

Parameters:
Name Type Description
rows number

The number of rows in the table to be created.

cols number

The number of columns in the table to be created.

loadUserFiles(scriptFile, cssFile, target, nonce)

Called to load user script and CSS before loading html.

The scriptFile and cssFile are loaded in sequence, with the single 'loadedUserFiles' callback only happening after their load events trigger. If neither scriptFile nor cssFile are specified, then the 'loadedUserFiles' callback happens anyway, since this ends up driving the loading process further.

Parameters:
Name Type Description
scriptFile string

The filename for a script that should be loaded

cssFile string

The filename for a CSS style that should be linked

target string

The first element found with tag target will have the script or link added to it; default "body" for script, "head" for CSS

nonce string

The "nonce" attribute to be applied

modifyImage(src, alt)

Modify the attributes of the image at selection.

Parameters:
Name Type Description
src string

The url of the image.

alt string

The alt text describing the image.

openImageDialog()

Open the default dialog to insert/edit images.

openLinkDialog()

Open the default dialog to insert/edit links.

outdent()

Do a context-sensitive outdent.

If in a list, outdent the item to a less nested level in the list if appropriate. If in a blockquote, remove a blockquote to outdent further. Else, do nothing.

Note that outdenting of a top-level list with a sublist doesn't work. TBH, I'm not sure why, but liftTarget returns null at the top-level in that case. As a result, the outdenting has to be done at least twice, the first of which splits the sublist from the top level. When this happens, we should probably just do the equivalent of toggleListType.

padBottom(fullHeight)

Pad the bottom of the text in editor to fill fullHeight.

Setting padBottom pads the editor all the way to the bottom, so that the focus area occupies the entire view. This allows long-press on iOS to bring up the context menu anywhere on the screen, even when text only occupies a small portion of the screen.

Parameters:
Name Type Description
fullHeight string

The full height of the screen to be padded-to

pasteHTML(html, event)

Paste html at the selection, replacing the selection as-needed.

Parameters:
Name Type Description
html string

The HTML to be pasted

event ClipboardEvent | bull

A mocked ClipboardEvent for testing

pasteText(html, event)

Do a custom paste operation of "text only", which we will extract from the html ourselves. First we get a node that conforms to the schema, which by definition only includes elements in a form we recognize, no spans, styles, etc. The trick here is that we want to use the same code to paste text as we do for HTML, but we want to paste something that is the MarkupEditor-equivalent of unformatted text.

Parameters:
Name Type Description
html string

The HTML to be pasted

event ClipboardEvent | bull

A mocked ClipboardEvent for testing

registerAugmentation(toolbar, name)

Add the toolbar to the registry. A toolbar holds cmdItems that can either be prepended or appended to the normal MarkupEditor toolbar.

If name is supplied, it is used to track it; else, the class of toolbar, derived using toolbar.constructor.name is used.

Parameters:
Name Type Description
toolbar object

A toolbar that holds cmdItems.

name string

A name used to retrieve the toolbar.

registerConfig(config, name)

Add the config to the registry. If name is supplied, it is used to track it; else, the class of config, derived using config.constructor.name is used.

Parameters:
Name Type Description
config ToolbarConfig | KeymapConfig | BehaviorConfig | object

One of the "configs" from the MarkupEditor API or appropriate object.

name string

A name used to retrieve the config.

registerDelegate(delegate, name)

Add the delegate to the registry. If name is supplied, it is used to track it; else, the class of delegate, derived using delegate.constructor.name is used.

Parameters:
Name Type Description
delegate MarkupDelegate | object

A MarkupDelegate or appropriate object.

name string

A name used to retrieve the delegate.

registerMessageHandler(handler, name)

Add the handler to the registry. If name is supplied, it is used to track it; else, the class of handler, derived using handler.constructor.name is used.

Parameters:
Name Type Description
handler MessageHandler | object

An instance of MessageHandler or appropriate object.

name string

A name used to retrieve the config.

removeAllDivs()

Remove all divs in the document.

removeButton(id)

Remove the HTMLButton element with the given id.

Parameters:
Name Type Description
id string

The ID of the button to be removed.

removeDiv(id)

Remove the div with the given id, and restore the selection to what it was before it is removed.

Parameters:
Name Type Description
id string

The id of the div to remove

renderDropdownItems(items, view) → {object}

Render the items in a Dropdown in the view.

Parameters:
Name Type Description
items Array.<MenuItem>

The content to be rendered in a Dropdown

view EditorView

The EditorView to render the items in

Returns:

An object containing the DOM for the Dropdown and an update function to display it

Type
object

renderGrouped(view, content) → {object}

Render the content in view as a group of MenuItems.

Parameters:
Name Type Description
view EditorView

The view to render content in

content Array.<MenuItem>

The menuitems to be rendered

Returns:

Object with the DOM containing content and an update method to display it.

Type
object

resetSelection()

Reset the selection to the beginning of the document.

savedDataImage(oldSrc, newSrc)

We saved an image at a new location or translated it from data to a file reference, so we need to update the document to reflect it.

Parameters:
Name Type Description
oldSrc string

Some or all of the original src for the image

newSrc string

The src that should replace the old src

searchFor(text, direction, activate) → {object}

Search for text in direction.

When text is empty, search is canceled.

CAUTION: When activate is "true", search must be cancelled once started, or Enter will be intercepted to mean searcher.searchForward()/searchBackward()

Parameters:
Name Type Description
text string

The string to search for in a case-insensitive manner.

direction string

Search direction, either forward or backward.

activate "true" | "false"

Set to "true" to activate "search mode", where Enter/Shift-Enter = Search forward/backward.

Returns:

The {to: number, from: number} location of the match.

Type
object

setActiveView(view)

Set the active editor's active view.

Parameters:
Name Type Description
view EditorView

The ProseMirror EditorView that should be active.

setHTML(contents, focusAfterLoad, base, editorView)

Set the contents of the editor.

The exported placeholderText is set after setting the contents.

Parameters:
Name Type Description
contents string

The HTML for the editor

focusAfterLoad boolean

Whether we should focus after load

base string

Value for base element for resolving relative src and hrefs

editorView EditorView

The EditorView to set HTML for, activeView() default

setStyle(style)

Set the paragraph style at the selection to style

Parameters:
Name Type Description
style string

One of the styles P or H1-H6 to set the selection to.

setTestHTML(contents, sel)

Set the HTML contents and select the text identified by sel, removing the sel markers in the process.

Note that because we run multiple tests against a given view, and we use setTestHTML to set the contents, we need to reset the view state completely each time. Otherwise, the history can be left in a state where an undo will work because the previous test executed redo.

Parameters:
Name Type Description
contents string

The HTML for the editor

sel string

An embedded character in contents marking selection point(s)

setTopLevelAttributes(jsonString)

Called to set attributes to the editor div, typically to , set spellcheck and autocorrect. Note that contenteditable should not be set for the editor element, even if it is included in the jsonString attributes. The same attributes are used for contenteditable divs, and the attribute is relevant in that case.

Parameters:
Name Type Description
jsonString string

The stringified object containing the attributes to set for the editor

testBlockquoteEnter()

For testing purposes, invoke _doBlockquoteEnter programmatically.

TODO: Implement

testExtractContents()

For testing purposes, invoke extractContents() on the selected range to make sure the selection is as expected.

TODO: Implement

testListEnter()

For testing purposes, invoke the splitCommand programmatically.

testPasteHTMLPreprocessing(html)

For testing purposes, create a ProseMirror Node that conforms to the MarkupEditor schema and return the resulting html as a string. Testing in this way lets us do simple pasteHTML tests with clean HTML and test the effect of schema-conformance on HTML contents separately. The html passed here is (typically) obtained from the paste buffer.

Parameters:
Name Type Description
html string

The HTML to paste

testPasteTextPreprocessing(html)

Use the same approach as testPasteHTMLPreprocessing, but augment with _minimalHTML to get a MarkupEditor-equivalent of unformatted text.

Parameters:
Name Type Description
html string

The HTML to paste

toggleBold()

Toggle the selection to/from bold.

toggleCode()

Toggle the selection to/from code.

toggleItalic()

Toggle the selection to/from italic.

toggleListItem(listType)

Turn the list tag on and off for the selection, doing the right thing for different cases of selection.

If the selection is in a list of type listType, then outdent the items in the selection.

If the selection is in a list type that is different than listType, then wrap it in a new list.

We use a single command returned by multiWrapInList because the command can be assigned to a single button in JavaScript.

Parameters:
Name Type Description
listType string

The kind of list we want the list item to be in if we are turning it on or changing it.

toggleSearch()

Toggle the search bar off and on.

toggleStrike()

Toggle the selection to/from strikethrough.

toggleSubscript()

Toggle the selection to/from subscript.

toggleSuperscript()

Toggle the selection to/from superscript.

toggleUnderline()

Toggle the selection to/from underline.

Table of contents