Class: PostEditor

PostEditor

The PostEditor is used to modify a post. It should not be instantiated directly. Instead, a new instance of a PostEditor is created by the editor and passed as the argument to the callback in Editor#run.

Usage:

editor.run((postEditor) => {
  // postEditor is an instance of PostEditor that can operate on the
  // editor's post
});

Constructor

new PostEditor()

Source:

Methods

addMarkupToRange(range, markup)

Given a markerRange (for example editor.range) mark all markers inside it as a given markup. The markup must be provided as a post abstract node.

Usage:

let range = editor.range;
let strongMarkup = editor.builder.createMarkup('strong');
editor.run((postEditor) => {
  postEditor.addMarkupToRange(range, strongMarkup);
});
// Will result some markers possibly being split, and the markup
// being applied to all markers between the split.

The return value will be all markers between the split, the same return value as splitMarkers.

Parameters:
Name Type Description
range Range
markup Markup

A markup post abstract node

Source:

deleteFrom(position, direction) → {Position}

Remove a character from a {marker, offset} position, in either forward or backward (default) direction.

Usage:

let marker = editor.post.sections.head.markers.head;
// marker has text of "Howdy!"
editor.run((postEditor) => {
  postEditor.deleteFrom({section, offset: 3});
});
// marker has text of "Hody!"

deleteFrom may remove a character from a different marker or join the marker's section with the previous/next section (depending on the deletion direction) if direction is BACKWARD and the offset is 0, or direction is FORWARD and the offset is equal to the length of the marker.

Parameters:
Name Type Description
position Position

object with {section, offset} the marker and offset to delete from

direction Number

The direction to delete in (default is BACKWARD)

Source:
Returns:

for positioning the cursor

Type
Position

deleteRange(range) → {Position}

Delete a range from the post

Usage:

    let { range } = editor;
    editor.run((postEditor) => {
      postEditor.deleteRange(range);
    });
Parameters:
Name Type Description
range Range

Cursor Range object with head and tail Positions

Source:
Returns:

The position where the cursor would go after deletion

Type
Position

insertMarkers(position, markers) → {Position}

Insert an array of markers at the given position. If the position is in a non-markerable section (like a card section), this method throws an error.

Parameters:
Name Type Description
position Position
markers Array.<Marker>
Source:
Returns:

The position that represents the end of the inserted markers.

Type
Position

insertSection(section)

Insert the given section after the current active section, or, if no section is active, at the end of the document.

Parameters:
Name Type Description
section Section
Source:

insertSectionAtEnd(section)

Insert the given section at the end of the document.

Parameters:
Name Type Description
section Section
Source:

insertSectionBefore(collection, section, beforeSection)

Insert a given section before another one, updating the post abstract and the rendered UI.

Usage:

    let markerRange = editor.range;
    let sectionWithCursor = markerRange.headMarker.section;
    let section = editor.builder.createCardSection('my-image');
    let collection = sectionWithCursor.parent.sections;
    editor.run((postEditor) => {
      postEditor.insertSectionBefore(collection, section, sectionWithCursor);
    });
Parameters:
Name Type Description
collection LinkedList

The list of sections to insert into

section Object

The new section

beforeSection Object

Optional The section "before" is relative to, if falsy the new section will be appended to the collection

Source:

insertText(position, text) → {Position}

Insert the text at the given position Inherits the markups already at that position, if any.

Parameters:
Name Type Description
position Position
text String
Source:
Returns:

position at the end of the inserted text.

Type
Position

insertTextWithMarkup(position, text, markups) → {Position}

Inserts text with the given markups, ignoring the existing markups at the position, if any.

Parameters:
Name Type Description
position Position
text String
markups Array.<Markup>
Source:
Returns:

position at the end of the inserted text

Type
Position

moveSectionDown(section)

Parameters:
Name Type Description
section Section

A section that is already in DOM

Source:

moveSectionUp(section)

Parameters:
Name Type Description
section Section

A section that is already in DOM

Source:

removeSection(section)

Remove a given section from the post abstract and the rendered UI.

Usage:

    let { range } = editor;
    let sectionWithCursor = range.head.section;
    editor.run((postEditor) => {
      postEditor.removeSection(sectionWithCursor);
    });
Parameters:
Name Type Description
section Object

The section to remove

Source:

replaceSection(section, newSection)

Parameters:
Name Type Description
section Section
newSection Section
Source:
Returns:

null

schedule(callback, onceopt)

A method for adding work the deferred queue

Parameters:
Name Type Attributes Default Description
callback function

to run during completion

once Boolean <optional>
false

Whether to only schedule the callback once.

Source:

scheduleDidUpdate()

Schedule a notification that the post has been changed. The notification will result in the editor firing its postDidChange hook after the postEditor completes its work (at the end of Editor#run).

Source:

scheduleOnce(callback)

A method for adding work the deferred queue. The callback will only be added to the queue once, even if scheduleOnce is called multiple times. The function cannot be an anonymous function.

Parameters:
Name Type Description
callback function

to run during completion

Source:

scheduleRerender()

Add a rerender job to the queue

Source:

splitSection(position) → {Array}

Split the section at the position.

Usage:

    let position = editor.cursor.offsets.head;
    editor.run((postEditor) => {
      postEditor.splitSection(position);
    });
    // Will result in the creation of two new sections
    // replacing the old one at the cursor position

The return value will be the two new sections. One or both of these sections can be blank (contain only a blank marker), for example if the headMarkerOffset is 0.

Parameters:
Name Type Description
position Position
Source:
Returns:

new sections, one for the first half and one for the second

Type
Array

toggleMarkup(markupOrString, range)

Toggle the given markup on the current selection. If anything in the current selection has the markup, the markup will be removed from it. If nothing in the selection has the markup, the markup will be added to everything in the selection.

Usage:

// Remove any 'strong' markup if it exists in the selection, otherwise
// make it all 'strong'
editor.run(postEditor => postEditor.toggleMarkup('strong'));

// add/remove a link to 'bustle.com' to the selection
editor.run(postEditor => {
  const linkMarkup = postEditor.builder.createMarkup('a', {href: 'http://bustle.com'});
  postEditor.toggleMarkup(linkMarkup);
});
Parameters:
Name Type Description
markupOrString Markup | String

Either a markup object created using the builder (useful when adding a markup with attributes, like an 'a' markup), or, if a string, the tag name of the markup (e.g. 'strong', 'em') to toggle.

range Range

in which to toggle, defaults to current editor range

Source:

toggleSection(sectionTagName, range)

Toggles the tagName of the active section or sections. If every section has the tag name, they will all be reset to default sections. Otherwise, every section will be changed to the requested type

Parameters:
Name Type Description
sectionTagName String

A valid markup section or list section tag name (e.g. 'blockquote', 'h2', 'ul')

range Range

The range over which to toggle. Defaults to the current editor's offsets

Source: