Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
visit(node) {
let method = node.type;
assert(`Cannot visit node of type ${node.type}`, !!this[method]);
this[method](node);
}
getAtomTypeFromIndex(index) {
const atomType = this.atomTypes[index];
assert(`No atom definition found at index ${index}`, !!atomType);
return atomType;
}
_breakNestedAtCursor() {
assert('Cannot call _breakNestedAtCursor if not nested', this._isNested);
let parent = this.cursorSection.parent;
let cursorAtEndOfList = this.cursorPosition.isEqual(parent.tailPosition());
if (cursorAtEndOfList) {
let blank = this.builder.createMarkupSection();
this._insertSectionAfter(blank, parent);
} else {
let [, blank,] = this._breakListAtCursor();
this.cursorPosition = blank.tailPosition();
}
}
buildMarkerType(type, value) {
switch (type) {
case MOBILEDOC_MARKUP_MARKER_TYPE:
return this.builder.createMarker(value, this.markups.slice());
case MOBILEDOC_ATOM_MARKER_TYPE: {
const [atomName, atomValue, atomPayload] = this.getAtomTypeFromIndex(value);
return this.builder.createAtom(atomName, atomValue, atomPayload, this.markups.slice());
}
default:
assert(`Unexpected marker type ${type}`, false);
}
}
}
_insertLeafSection(section) {
assert('Can only _insertLeafSection when cursor is at end of section',
this.cursorPosition.isTail());
this._hasInsertedFirstLeafSection = true;
section = section.clone();
if (this.cursorSection.isBlank) {
assert('Cannot insert leaf non-markerable section when cursor is nested',
!(section.isMarkerable && this._isNested));
this._replaceSection(this.cursorSection, [section]);
} else if (this.cursorSection.next && this.cursorSection.next.isBlank) {
this._replaceSection(this.cursorSection.next, [section]);
} else {
let reference = this.cursorSection.next;
this._insertSectionBefore(section, reference);
}
}
move(direction) {
assert(`Must pass DIRECTION.FORWARD (${DIRECTION.FORWARD}) or DIRECTION.BACKWARD (${DIRECTION.BACKWARD}) to Range#move`,
direction === DIRECTION.FORWARD || direction === DIRECTION.BACKWARD);
let { focusedPosition, isCollapsed } = this;
if (isCollapsed) {
return new Range(focusedPosition.move(direction));
} else {
return this._collapse(direction);
}
}
buildMarkerType(type, value) {
switch (type) {
case MOBILEDOC_MARKUP_MARKER_TYPE:
return this.builder.createMarker(value, this.markups.slice());
case MOBILEDOC_ATOM_MARKER_TYPE: {
const [atomName, atomValue, atomPayload] = this.getAtomTypeFromIndex(value);
return this.builder.createAtom(atomName, atomValue, atomPayload, this.markups.slice());
}
default:
assert(`Unexpected marker type ${type}`, false);
}
}
}
_insertLeafSection(section) {
assert('Can only _insertLeafSection when cursor is at end of section',
this.cursorPosition.isTail());
this._hasInsertedFirstLeafSection = true;
section = section.clone();
if (this.cursorSection.isBlank) {
assert('Cannot insert leaf non-markerable section when cursor is nested',
!(section.isMarkerable && this._isNested));
this._replaceSection(this.cursorSection, [section]);
} else if (this.cursorSection.next && this.cursorSection.next.isBlank) {
this._replaceSection(this.cursorSection.next, [section]);
} else {
let reference = this.cursorSection.next;
this._insertSectionBefore(section, reference);
}
}
}
_mergeSection(section) {
assert('Can only merge markerable sections',
this._isMarkerable && section.isMarkerable);
this._hasInsertedFirstLeafSection = true;
let markers = section.markers.map(m => m.clone());
let position = this.postEditor.insertMarkers(this.cursorPosition, markers);
this.cursorPosition = position;
}
_insertSectionAfter(section, parent) {
assert('Cannot _insertSectionAfter nested section', !parent.isNested);
let reference = parent.next;
let collection = this._post.sections;
this.postEditor.insertSectionBefore(collection, section, reference);
this.cursorPosition = section.tailPosition();
}