diff --git a/src/models/node.js b/src/models/node.js index 00dd79b45..addd35f7f 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -387,6 +387,9 @@ const Node = { */ getCommonAncestor(one, two) { + if (one == this.key) return this + if (two == this.key) return this + this.assertDescendant(one) this.assertDescendant(two) let ancestors = new List() diff --git a/src/models/transform.js b/src/models/transform.js index 857d3554e..7f7fba219 100644 --- a/src/models/transform.js +++ b/src/models/transform.js @@ -99,7 +99,8 @@ class Transform { Object.keys(Transforms).forEach((type) => { Transform.prototype[type] = function (...args) { debug(type, { args }) - return Transforms[type](this, ...args) + Transforms[type](this, ...args) + return this } }) diff --git a/src/transforms/apply-operation.js b/src/transforms/apply-operation.js index d31ba66b2..a4e50479e 100644 --- a/src/transforms/apply-operation.js +++ b/src/transforms/apply-operation.js @@ -40,7 +40,6 @@ const OPERATIONS = { * * @param {Transform} transform * @param {Object} operation - * @return {Transform} */ export function applyOperation(transform, operation) { @@ -53,11 +52,8 @@ export function applyOperation(transform, operation) { } debug(type, operation) - transform.state = fn(state, operation) transform.operations = operations.concat([operation]) - - return transform } /** @@ -289,16 +285,11 @@ function removeNode(state, operation) { function removeText(state, operation) { const { path, offset, length } = operation + const rangeOffset = offset + length let { document, selection } = state const { startKey, endKey, startOffset, endOffset } = selection let node = document.assertPath(path) - const rangeOffset = offset + length - - // Update the document - node = node.removeText(offset, length) - document = document.updateDescendant(node) - // Update the selection if (startKey == node.key && startOffset >= rangeOffset) { selection = selection.moveStartOffset(-length) @@ -307,6 +298,8 @@ function removeText(state, operation) { selection = selection.moveEndOffset(-length) } + node = node.removeText(offset, length) + document = document.updateDescendant(node) state = state.merge({ document, selection }) return state } diff --git a/src/transforms/at-current-range.js b/src/transforms/at-current-range.js index 78cac2ded..5c1e6813a 100644 --- a/src/transforms/at-current-range.js +++ b/src/transforms/at-current-range.js @@ -6,99 +6,48 @@ import Normalize from '../utils/normalize' * * @param {Transform} transform * @param {Mark} mark - * @return {Transform} */ export function addMark(transform, mark) { mark = Normalize.mark(mark) - const { state } = transform const { document, selection } = state if (selection.isExpanded) { - return transform.addMarkAtRange(selection, mark) + transform.addMarkAtRange(selection, mark) + return } - else if (selection.marks) { + if (selection.marks) { const marks = selection.marks.add(mark) const sel = selection.merge({ marks }) - return transform.moveTo(sel) + transform.moveTo(sel) + return } - else { - const marks = document.getMarksAtRange(selection).add(mark) - const sel = selection.merge({ marks }) - return transform.moveTo(sel) - } + const marks = document.getMarksAtRange(selection).add(mark) + const sel = selection.merge({ marks }) + transform.moveTo(sel) } /** * Delete at the current selection. * * @param {Transform} transform - * @return {Transform} */ export function _delete(transform) { const { state } = transform - const { document, selection } = state + const { selection } = state + if (selection.isCollapsed) return - // If the selection is collapsed, there's nothing to delete. - if (selection.isCollapsed) return transform - - const { startText } = state - const { startKey, startOffset, endKey, endOffset } = selection - const block = document.getClosestBlock(startKey) - const highest = block.getHighestChild(startKey) - const previous = block.getPreviousSibling(highest.key) - const next = block.getNextSibling(highest.key) - let after - - // If there's a previous node, and we're at the start of the current node, - // and the selection encompasses the entire current node, it won't exist after - // deleting, so we need to update the selection's keys. - if ( - previous && - startOffset == 0 && - (endKey != startKey || endOffset == startText.length) - ) { - - // If the nodes on either sides are text nodes, they will end up being - // combined, so we need to set the selection to right in between them. - if (previous.kind == 'text' && next && next.kind == 'text') { - after = selection.merge({ - anchorKey: previous.key, - anchorOffset: previous.length, - focusKey: previous.key, - focusOffset: previous.length - }) - } - - // Otherwise, if only the previous node is a text node, it won't be merged, - // so collapse to the end of it. - else if (previous.kind == 'text') { - after = selection.collapseToEndOf(previous) - } - - // Otherwise, if the previous node isn't a text node, we need to get the - // last text node inside of it and collapse to the end of that. - else { - const last = previous.getLastText() - after = selection.collapseToEndOf(last) - } - } - - // Otherwise, if the inline is an online child - - // Otherwise simply collapse the selection. - else { - after = selection.collapseToStart() - } - - return transform - .unsetSelection() + transform + .snapshotSelection() .deleteAtRange(selection) - .moveTo(after) + // Ensure that the selection is collapsed to the start, because in certain + // cases when deleting across inline nodes this isn't guaranteed. + .collapseToStart() + .snapshotSelection() } /** @@ -106,16 +55,12 @@ export function _delete(transform) { * * @param {Transform} transform * @param {Number} n (optional) - * @return {Transform} */ export function deleteBackward(transform, n = 1) { const { state } = transform const { selection } = state - - return transform - .deleteBackwardAtRange(selection, n) - .collapseToEnd() + transform.deleteBackwardAtRange(selection, n) } /** @@ -123,16 +68,12 @@ export function deleteBackward(transform, n = 1) { * * @param {Transform} transform * @param {Number} n (optional) - * @return {Transform} */ export function deleteForward(transform, n = 1) { const { state } = transform const { selection } = state - - return transform - .deleteForwardAtRange(selection, n) - .collapseToEnd() + transform.deleteForwardAtRange(selection, n) } /** @@ -140,23 +81,17 @@ export function deleteForward(transform, n = 1) { * * @param {Transform} transform * @param {String|Object|Block} block - * @return {Transform} */ export function insertBlock(transform, block) { - let { state } = transform - let { document, selection } = state - const keys = document.getTexts().map(text => text.key) - - transform.unsetSelection() + block = Normalize.block(block) + const { state } = transform + const { selection } = state transform.insertBlockAtRange(selection, block) - state = transform.state - document = state.document - const text = document.getTexts().find(n => !keys.includes(n.key)) - const after = selection.collapseToEndOf(text) - - return transform.moveTo(after) + // If the node was successfully inserted, update the selection. + const node = transform.state.document.getNode(block.key) + if (node) transform.collapseToEndOf(node) } /** @@ -164,14 +99,13 @@ export function insertBlock(transform, block) { * * @param {Transform} transform * @param {Document} fragment - * @return {Transform} */ export function insertFragment(transform, fragment) { let { state } = transform let { document, selection } = state - if (!fragment.length) return transform + if (!fragment.length) return const { startText, endText } = state const lastText = fragment.getLastText() @@ -207,7 +141,7 @@ export function insertFragment(transform, fragment) { .moveForward(lastText.length) } - return transform.moveTo(after) + transform.moveTo(after) } /** @@ -215,38 +149,17 @@ export function insertFragment(transform, fragment) { * * @param {Transform} transform * @param {String|Object|Block} inline - * @return {Transform} */ export function insertInline(transform, inline) { - let { state } = transform - let { document, selection, startText } = state - let after - - const hasVoid = document.hasVoidParent(startText.key) - const keys = document.getTexts().map(text => text.key) - - transform.unsetSelection() + inline = Normalize.inline(inline) + const { state } = transform + const { selection } = state transform.insertInlineAtRange(selection, inline) - state = transform.state - document = state.document - if (hasVoid) { - after = selection - } - - else { - const text = document.getTexts().find((n) => { - if (keys.includes(n.key)) return false - const parent = document.getParent(n.key) - if (parent.kind != 'inline') return false - return true - }) - - after = selection.collapseToEndOf(text) - } - - return transform.moveTo(after) + // If the node was successfully inserted, update the selection. + const node = transform.state.document.getNode(inline.key) + if (node) transform.collapseToEndOf(node) } /** @@ -255,34 +168,19 @@ export function insertInline(transform, inline) { * @param {Transform} transform * @param {String} text * @param {Set} marks (optional) - * @return {Transform} */ export function insertText(transform, text, marks) { const { state } = transform const { document, selection } = state - const { startKey } = selection - const isVoid = document.hasVoidParent(startKey) - let after - - if (isVoid) { - after = selection - } - - else if (selection.isExpanded) { - after = selection.collapseToStart().moveForward(text.length) - } - - else { - after = selection.moveForward(text.length) - } - marks = marks || selection.marks + transform.insertTextAtRange(selection, text, marks) - return transform - .unsetSelection() - .insertTextAtRange(selection, text, marks) - .moveTo(after) + // If the text was successfully inserted, and the selection had marks on it, + // unset the selection's marks. + if (selection.marks && document != transform.state.document) { + transform.unsetMarks() + } } /** @@ -290,13 +188,12 @@ export function insertText(transform, text, marks) { * * @param {Transform} transform * @param {Object} properties - * @return {Transform} */ export function setBlock(transform, properties) { const { state } = transform const { selection } = state - return transform.setBlockAtRange(selection, properties) + transform.setBlockAtRange(selection, properties) } /** @@ -304,13 +201,12 @@ export function setBlock(transform, properties) { * * @param {Transform} transform * @param {Object} properties - * @return {Transform} */ export function setInline(transform, properties) { const { state } = transform const { selection } = state - return transform.setInlineAtRange(selection, properties) + transform.setInlineAtRange(selection, properties) } /** @@ -318,43 +214,16 @@ export function setInline(transform, properties) { * * @param {Transform} transform * @param {Number} depth (optional) - * @return {Transform} */ export function splitBlock(transform, depth = 1) { let { state } = transform - let { document, selection } = state - - transform.unsetSelection() - transform.splitBlockAtRange(selection, depth) - - state = transform.state - document = state.document - - const { startKey, startOffset } = selection - const startText = document.getNode(startKey) - const startBlock = document.getClosestBlock(startKey) - const startInline = startBlock.getFurthestInline(startKey) - const nextText = document.getNextText(startText.key) - let after - - // If the selection is at the start of the highest inline child inside the - // block, the starting text node won't need to be split. - if ( - (startOffset == 0) && - (startBlock.text != '') && - (!startInline || startInline.getOffset(startText.key) == 0) - ) { - after = selection.collapseToStartOf(startText) - } - - // Otherwise, we'll need to move the selection forward one to account for the - // text node that was split. - else { - after = selection.collapseToStartOf(nextText) - } - - return transform.moveTo(after) + let { selection } = state + transform + .snapshotSelection() + .splitBlockAtRange(selection, depth) + .collapseToEnd() + .snapshotSelection() } /** @@ -362,49 +231,15 @@ export function splitBlock(transform, depth = 1) { * * @param {Transform} transform * @param {Number} depth (optional) - * @return {Transform} */ export function splitInline(transform, depth = Infinity) { let { state } = transform - let { document, selection } = state - - // If the selection is expanded, remove it first. - if (selection.isExpanded) { - transform.delete() - state = transform.state - document = state.document - selection = state.selection - } - - let after = selection - const { startKey, startOffset } = selection - let startNode = document.assertDescendant(startKey) - const furthestInline = document.getFurthestInline(startKey) - const offset = furthestInline.getOffset(startNode.key) - - // If the selection is at the start of end of the furthest inline, there isn't - // anything to split, so abort. - if ( - (offset + startOffset == 0) || - (offset + startNode.length == startOffset) - ) { - return transform - } - - transform.unsetSelection() - transform.splitInlineAtRange(selection, depth) - state = transform.state - document = state.document - const closestInline = document.getClosestInline(startKey) - - if (closestInline) { - startNode = document.getDescendant(startKey) - const nextNode = document.getNextText(startNode.key) - after = selection.collapseToStartOf(nextNode) - } - - return transform.moveTo(after) + let { selection } = state + transform + .snapshotSelection() + .splitInlineAtRange(selection, depth) + .snapshotSelection() } /** @@ -412,30 +247,28 @@ export function splitInline(transform, depth = Infinity) { * * @param {Transform} transform * @param {Mark} mark - * @return {Transform} */ export function removeMark(transform, mark) { mark = Normalize.mark(mark) - const { state } = transform const { document, selection } = state if (selection.isExpanded) { - return transform.removeMarkAtRange(selection, mark) + transform.removeMarkAtRange(selection, mark) + return } - else if (selection.marks) { + if (selection.marks) { const marks = selection.marks.remove(mark) const sel = selection.merge({ marks }) - return transform.moveTo(sel) + transform.moveTo(sel) + return } - else { - const marks = document.getMarksAtRange(selection).remove(mark) - const sel = selection.merge({ marks }) - return transform.moveTo(sel) - } + const marks = document.getMarksAtRange(selection).remove(mark) + const sel = selection.merge({ marks }) + transform.moveTo(sel) } /** @@ -444,19 +277,17 @@ export function removeMark(transform, mark) { * * @param {Transform} transform * @param {Mark} mark - * @return {Transform} */ export function toggleMark(transform, mark) { mark = Normalize.mark(mark) - const { state } = transform const exists = state.marks.some(m => m.equals(mark)) if (exists) { - return transform.removeMark(mark) + transform.removeMark(mark) } else { - return transform.addMark(mark) + transform.addMark(mark) } } @@ -465,13 +296,12 @@ export function toggleMark(transform, mark) { * * @param {Transform} transform * @param {Object|String} properties - * @return {Transform} */ export function unwrapBlock(transform, properties) { const { state } = transform const { selection } = state - return transform.unwrapBlockAtRange(selection, properties) + transform.unwrapBlockAtRange(selection, properties) } /** @@ -479,13 +309,12 @@ export function unwrapBlock(transform, properties) { * * @param {Transform} transform * @param {Object|String} properties - * @return {Transform} */ export function unwrapInline(transform, properties) { const { state } = transform const { selection } = state - return transform.unwrapInlineAtRange(selection, properties) + transform.unwrapInlineAtRange(selection, properties) } /** @@ -494,13 +323,12 @@ export function unwrapInline(transform, properties) { * * @param {Transform} transform * @param {Object|String} properties - * @return {Transform} */ export function wrapBlock(transform, properties) { const { state } = transform const { selection } = state - return transform.wrapBlockAtRange(selection, properties) + transform.wrapBlockAtRange(selection, properties) } /** @@ -508,7 +336,6 @@ export function wrapBlock(transform, properties) { * * @param {Transform} transform * @param {Object|String} properties - * @return {Transform} */ export function wrapInline(transform, properties) { @@ -551,7 +378,7 @@ export function wrapInline(transform, properties) { } after = after.normalize(document) - return transform.moveTo(after) + transform.moveTo(after) } /** @@ -560,28 +387,19 @@ export function wrapInline(transform, properties) { * @param {Transform} transform * @param {String} prefix * @param {String} suffix - * @return {Transform} */ export function wrapText(transform, prefix, suffix = prefix) { const { state } = transform const { selection } = state - const { anchorOffset, anchorKey, focusOffset, focusKey, isBackward } = selection - let after + transform.wrapTextAtRange(selection, prefix, suffix) - if (anchorKey == focusKey) { - after = selection.moveForward(prefix.length) + // Adding the suffix will have pushed the end of the selection further on, so + // we need to move it back to account for this. + transform.moveEndOffset(0 - suffix.length) + + // If the selection was collapsed, it will have moved the start offset too. + if (selection.isCollapsed) { + transform.moveStartOffset(0 - prefix.length) } - - else { - after = selection.merge({ - anchorOffset: isBackward ? anchorOffset : anchorOffset + prefix.length, - focusOffset: isBackward ? focusOffset + prefix.length : focusOffset - }) - } - - return transform - .unsetSelection() - .wrapTextAtRange(selection, prefix, suffix) - .moveTo(after) } diff --git a/src/transforms/at-range.js b/src/transforms/at-range.js index 0820d1d1e..0b126220b 100644 --- a/src/transforms/at-range.js +++ b/src/transforms/at-range.js @@ -4,6 +4,16 @@ import Normalize from '../utils/normalize' import SCHEMA from '../schemas/core' import { List } from 'immutable' +/** + * An options object with normalize set to `false`. + * + * @type {Object} + */ + +const OPTS = { + normalize: false +} + /** * Add a new `mark` to the characters at `range`. * @@ -12,11 +22,10 @@ import { List } from 'immutable' * @param {Mixed} mark * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function addMarkAtRange(transform, range, mark, options = {}) { - if (range.isCollapsed) return transform + if (range.isCollapsed) return const { normalize = true } = options const { state } = transform @@ -35,8 +44,6 @@ export function addMarkAtRange(transform, range, mark, options = {}) { transform.addMarkByKey(key, index, length, mark, { normalize }) }) - - return transform } /** @@ -46,11 +53,10 @@ export function addMarkAtRange(transform, range, mark, options = {}) { * @param {Selection} range * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function deleteAtRange(transform, range, options = {}) { - if (range.isCollapsed) return transform + if (range.isCollapsed) return const { normalize = true } = options const { startKey, startOffset, endKey, endOffset } = range @@ -58,7 +64,8 @@ export function deleteAtRange(transform, range, options = {}) { if (startKey == endKey) { const index = startOffset const length = endOffset - startOffset - return transform.removeTextByKey(startKey, index, length, { normalize }) + transform.removeTextByKey(startKey, index, length, { normalize }) + return } let { state } = transform @@ -71,8 +78,8 @@ export function deleteAtRange(transform, range, options = {}) { const startOff = (startChild.kind == 'text' ? 0 : startChild.getOffset(startKey)) + startOffset const endOff = (endChild.kind == 'text' ? 0 : endChild.getOffset(endKey)) + endOffset - transform.splitNodeByKey(startChild.key, startOff, { normalize: false }) - transform.splitNodeByKey(endChild.key, endOff, { normalize: false }) + transform.splitNodeByKey(startChild.key, startOff, OPTS) + transform.splitNodeByKey(endChild.key, endOff, OPTS) state = transform.state document = state.document @@ -90,7 +97,7 @@ export function deleteAtRange(transform, range, options = {}) { if (middles.size) { // remove first nodes directly so the document is not normalized middles.forEach(child => { - transform.removeNodeByKey(child.key, { normalize: false }) + transform.removeNodeByKey(child.key, OPTS) }) } @@ -98,18 +105,16 @@ export function deleteAtRange(transform, range, options = {}) { endBlock.nodes.forEach((child, i) => { const newKey = startBlock.key const newIndex = startBlock.nodes.size + i - transform.moveNodeByKey(child.key, newKey, newIndex, { normalize: false }) + transform.moveNodeByKey(child.key, newKey, newIndex, OPTS) }) const lonely = document.getFurthest(endBlock.key, p => p.nodes.size == 1) || endBlock - transform.removeNodeByKey(lonely.key, { normalize: false }) + transform.removeNodeByKey(lonely.key, OPTS) } if (normalize) { transform.normalizeNodeByKey(ancestor.key, SCHEMA) } - - return transform } /** @@ -120,7 +125,6 @@ export function deleteAtRange(transform, range, options = {}) { * @param {Number} n (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) { @@ -130,21 +134,24 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) { const { startKey, focusOffset } = range if (range.isExpanded) { - return transform.deleteAtRange(range, { normalize }) + transform.deleteAtRange(range, { normalize }) + return } const block = document.getClosestBlock(startKey) if (block && block.isVoid) { - return transform.removeNodeByKey(block.key, { normalize }) + transform.removeNodeByKey(block.key, { normalize }) + return } const inline = document.getClosestInline(startKey) if (inline && inline.isVoid) { - return transform.removeNodeByKey(inline.key, { normalize }) + transform.removeNodeByKey(inline.key, { normalize }) + return } if (range.isAtStartOf(document)) { - return transform + return } const text = document.getDescendant(startKey) @@ -154,11 +161,13 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) { const prevInline = document.getClosestInline(prev.key) if (prevBlock && prevBlock.isVoid) { - return transform.removeNodeByKey(prevBlock.key, { normalize }) + transform.removeNodeByKey(prevBlock.key, { normalize }) + return } if (prevInline && prevInline.isVoid) { - return transform.removeNodeByKey(prevInline.key, { normalize }) + transform.removeNodeByKey(prevInline.key, { normalize }) + return } range = range.merge({ @@ -166,7 +175,8 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) { anchorOffset: prev.length, }) - return transform.deleteAtRange(range, { normalize }) + transform.deleteAtRange(range, { normalize }) + return } range = range.merge({ @@ -174,7 +184,7 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) { isBackward: true, }) - return transform.deleteAtRange(range, { normalize }) + transform.deleteAtRange(range, { normalize }) } /** @@ -185,7 +195,6 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) { * @param {Number} n (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function deleteForwardAtRange(transform, range, n = 1, options = {}) { @@ -195,21 +204,24 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) { const { startKey, focusOffset } = range if (range.isExpanded) { - return transform.deleteAtRange(range, { normalize }) + transform.deleteAtRange(range, { normalize }) + return } const block = document.getClosestBlock(startKey) if (block && block.isVoid) { - return transform.removeNodeByKey(block.key, { normalize }) + transform.removeNodeByKey(block.key, { normalize }) + return } const inline = document.getClosestInline(startKey) if (inline && inline.isVoid) { - return transform.removeNodeByKey(inline.key, { normalize }) + transform.removeNodeByKey(inline.key, { normalize }) + return } if (range.isAtEndOf(document)) { - return transform + return } const text = document.getDescendant(startKey) @@ -219,11 +231,13 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) { const nextInline = document.getClosestInline(next.key) if (nextBlock && nextBlock.isVoid) { - return transform.removeNodeByKey(nextBlock.key, { normalize }) + transform.removeNodeByKey(nextBlock.key, { normalize }) + return } if (nextInline && nextInline.isVoid) { - return transform.removeNodeByKey(nextInline.key, { normalize }) + transform.removeNodeByKey(nextInline.key, { normalize }) + return } range = range.merge({ @@ -231,14 +245,15 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) { focusOffset: 0 }) - return transform.deleteAtRange(range, { normalize }) + transform.deleteAtRange(range, { normalize }) + return } range = range.merge({ focusOffset: focusOffset + n }) - return transform.deleteAtRange(range, { normalize }) + transform.deleteAtRange(range, { normalize }) } /** @@ -249,7 +264,6 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) { * @param {Block|String|Object} block * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function insertBlockAtRange(transform, range, block, options = {}) { @@ -295,8 +309,6 @@ export function insertBlockAtRange(transform, range, block, options = {}) { if (normalize) { transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -307,7 +319,6 @@ export function insertBlockAtRange(transform, range, block, options = {}) { * @param {Document} fragment * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function insertFragmentAtRange(transform, range, fragment, options = {}) { @@ -315,14 +326,12 @@ export function insertFragmentAtRange(transform, range, fragment, options = {}) // If the range is expanded, delete it first. if (range.isExpanded) { - transform.deleteAtRange(range, { normalize: false }) + transform.deleteAtRange(range, OPTS) range = range.collapseToStart() } // If the fragment is empty, there's nothing to do after deleting. - if (!fragment.length) { - return transform - } + if (!fragment.length) return // Regenerate the keys for all of the fragments nodes, so that they're // guaranteed not to collide with the existing keys in the document. Otherwise @@ -358,13 +367,13 @@ export function insertFragmentAtRange(transform, range, fragment, options = {}) fragment.nodes.forEach((node, i) => { const newIndex = startIndex + i + 1 - transform.insertNodeByKey(parent.key, newIndex, node, { normalize: false }) + transform.insertNodeByKey(parent.key, newIndex, node, OPTS) }) } // Check if we need to split the node. if (startOffset != 0) { - transform.splitNodeByKey(startChild.key, offset, { normalize: false }) + transform.splitNodeByKey(startChild.key, offset, OPTS) } // Update our variables with the new state. @@ -384,15 +393,15 @@ export function insertFragmentAtRange(transform, range, fragment, options = {}) nextNodes.forEach((node, i) => { const newIndex = lastIndex + i - transform.moveNodeByKey(node.key, lastBlock.key, newIndex, { normalize: false }) + transform.moveNodeByKey(node.key, lastBlock.key, newIndex, OPTS) }) } // If the starting block is empty, we replace it entirely with the first block // of the fragment, since this leads to a more expected behavior for the user. if (startBlock.isEmpty) { - transform.removeNodeByKey(startBlock.key, { normalize: false }) - transform.insertNodeByKey(parent.key, index, firstBlock, { normalize: false }) + transform.removeNodeByKey(startBlock.key, OPTS) + transform.insertNodeByKey(parent.key, index, firstBlock, OPTS) } // Otherwise, we maintain the starting block, and insert all of the first @@ -404,7 +413,7 @@ export function insertFragmentAtRange(transform, range, fragment, options = {}) firstBlock.nodes.forEach((inline, i) => { const o = startOffset == 0 ? 0 : 1 const newIndex = inlineIndex + i + o - transform.insertNodeByKey(startBlock.key, newIndex, inline, { normalize: false }) + transform.insertNodeByKey(startBlock.key, newIndex, inline, OPTS) }) } @@ -412,8 +421,6 @@ export function insertFragmentAtRange(transform, range, fragment, options = {}) if (normalize) { transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -424,7 +431,6 @@ export function insertFragmentAtRange(transform, range, fragment, options = {}) * @param {Inline|String|Object} inline * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function insertInlineAtRange(transform, range, inline, options = {}) { @@ -432,7 +438,7 @@ export function insertInlineAtRange(transform, range, inline, options = {}) { inline = Normalize.inline(inline) if (range.isExpanded) { - transform.deleteAtRange(range, { normalize: false }) + transform.deleteAtRange(range, OPTS) range = range.collapseToStart() } @@ -443,18 +449,14 @@ export function insertInlineAtRange(transform, range, inline, options = {}) { const startText = document.assertDescendant(startKey) const index = parent.nodes.indexOf(startText) - if (parent.isVoid) { - return transform - } + if (parent.isVoid) return - transform.splitNodeByKey(startKey, startOffset, { normalize: false }) - transform.insertNodeByKey(parent.key, index + 1, inline, { normalize: false }) + transform.splitNodeByKey(startKey, startOffset, OPTS) + transform.insertNodeByKey(parent.key, index + 1, inline, OPTS) if (normalize) { transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -466,7 +468,6 @@ export function insertInlineAtRange(transform, range, inline, options = {}) { * @param {Set} marks (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function insertTextAtRange(transform, range, text, marks, options = {}) { @@ -476,12 +477,10 @@ export function insertTextAtRange(transform, range, text, marks, options = {}) { const { startKey, startOffset } = range const parent = document.getParent(startKey) - if (parent.isVoid) { - return transform - } + if (parent.isVoid) return if (range.isExpanded) { - transform.deleteAtRange(range, { normalize: false }) + transform.deleteAtRange(range, OPTS) } // PERF: Unless specified, don't normalize if only inserting text. @@ -489,7 +488,7 @@ export function insertTextAtRange(transform, range, text, marks, options = {}) { normalize = range.isExpanded } - return transform.insertTextByKey(startKey, startOffset, text, marks, { normalize }) + transform.insertTextByKey(startKey, startOffset, text, marks, { normalize }) } /** @@ -500,11 +499,10 @@ export function insertTextAtRange(transform, range, text, marks, options = {}) { * @param {Mark|String} mark (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function removeMarkAtRange(transform, range, mark, options = {}) { - if (range.isCollapsed) return transform + if (range.isCollapsed) return const { normalize = true } = options const { state } = transform @@ -523,8 +521,6 @@ export function removeMarkAtRange(transform, range, mark, options = {}) { transform.removeMarkByKey(key, index, length, mark, { normalize }) }) - - return transform } /** @@ -535,7 +531,6 @@ export function removeMarkAtRange(transform, range, mark, options = {}) { * @param {Object|String} properties * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function setBlockAtRange(transform, range, properties, options = {}) { @@ -547,8 +542,6 @@ export function setBlockAtRange(transform, range, properties, options = {}) { blocks.forEach((block) => { transform.setNodeByKey(block.key, properties, { normalize }) }) - - return transform } /** @@ -559,7 +552,6 @@ export function setBlockAtRange(transform, range, properties, options = {}) { * @param {Object|String} properties * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function setInlineAtRange(transform, range, properties, options = {}) { @@ -571,8 +563,6 @@ export function setInlineAtRange(transform, range, properties, options = {}) { inlines.forEach((inline) => { transform.setNodeByKey(inline.key, properties, { normalize}) }) - - return transform } /** @@ -583,7 +573,6 @@ export function setInlineAtRange(transform, range, properties, options = {}) { * @param {Number} height (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function splitBlockAtRange(transform, range, height = 1, options = {}) { @@ -609,7 +598,7 @@ export function splitBlockAtRange(transform, range, height = 1, options = {}) { h++ } - return transform.splitNodeByKey(node.key, offset, { normalize }) + transform.splitNodeByKey(node.key, offset, { normalize }) } /** @@ -620,7 +609,6 @@ export function splitBlockAtRange(transform, range, height = 1, options = {}) { * @param {Number} height (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function splitInlineAtRange(transform, range, height = Infinity, options = {}) { @@ -646,7 +634,7 @@ export function splitInlineAtRange(transform, range, height = Infinity, options h++ } - return transform.splitNodeByKey(node.key, offset, { normalize }) + transform.splitNodeByKey(node.key, offset, { normalize }) } /** @@ -658,11 +646,10 @@ export function splitInlineAtRange(transform, range, height = Infinity, options * @param {Mixed} mark * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function toggleMarkAtRange(transform, range, mark, options = {}) { - if (range.isCollapsed) return transform + if (range.isCollapsed) return mark = Normalize.mark(mark) @@ -677,8 +664,6 @@ export function toggleMarkAtRange(transform, range, mark, options = {}) { } else { transform.addMarkAtRange(range, mark, { normalize }) } - - return transform } /** @@ -689,7 +674,6 @@ export function toggleMarkAtRange(transform, range, mark, options = {}) { * @param {String|Object} properties * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function unwrapBlockAtRange(transform, range, properties, options = {}) { @@ -728,17 +712,17 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) { if (first == firstMatch && last == lastMatch) { block.nodes.forEach((child, i) => { - transform.moveNodeByKey(child.key, parent.key, index + i, { normalize: false }) + transform.moveNodeByKey(child.key, parent.key, index + i, OPTS) }) - transform.removeNodeByKey(block.key, { normalize: false }) + transform.removeNodeByKey(block.key, OPTS) } else if (last == lastMatch) { block.nodes .skipUntil(n => n == firstMatch) .forEach((child, i) => { - transform.moveNodeByKey(child.key, parent.key, index + 1 + i, { normalize: false }) + transform.moveNodeByKey(child.key, parent.key, index + 1 + i, OPTS) }) } @@ -747,23 +731,23 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) { .takeUntil(n => n == lastMatch) .push(lastMatch) .forEach((child, i) => { - transform.moveNodeByKey(child.key, parent.key, index + i, { normalize: false }) + transform.moveNodeByKey(child.key, parent.key, index + i, OPTS) }) } else { const offset = block.getOffset(firstMatch.key) - transform.splitNodeByKey(block.key, offset, { normalize: false }) + transform.splitNodeByKey(block.key, offset, OPTS) state = transform.state document = state.document const extra = document.getPreviousSibling(firstMatch.key) children.forEach((child, i) => { - transform.moveNodeByKey(child.key, parent.key, index + 1 + i, { normalize: false }) + transform.moveNodeByKey(child.key, parent.key, index + 1 + i, OPTS) }) - transform.removeNodeByKey(extra.key, { normalize: false }) + transform.removeNodeByKey(extra.key, OPTS) } }) @@ -771,8 +755,6 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) { if (normalize) { transform.normalizeDocument(SCHEMA) } - - return transform } /** @@ -783,7 +765,6 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) { * @param {String|Object} properties * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function unwrapInlineAtRange(transform, range, properties, options = {}) { @@ -812,7 +793,7 @@ export function unwrapInlineAtRange(transform, range, properties, options = {}) const index = parent.nodes.indexOf(inline) inline.nodes.forEach((child, i) => { - transform.moveNodeByKey(child.key, parent.key, index + i, { normalize: false }) + transform.moveNodeByKey(child.key, parent.key, index + i, OPTS) }) }) @@ -820,8 +801,6 @@ export function unwrapInlineAtRange(transform, range, properties, options = {}) if (normalize) { transform.normalizeDocument(SCHEMA) } - - return transform } /** @@ -832,7 +811,6 @@ export function unwrapInlineAtRange(transform, range, properties, options = {}) * @param {Block|Object|String} block * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function wrapBlockAtRange(transform, range, block, options = {}) { @@ -882,18 +860,16 @@ export function wrapBlockAtRange(transform, range, block, options = {}) { } // inject the new block node into the parent - transform.insertNodeByKey(parent.key, index, block, { normalize: false }) + transform.insertNodeByKey(parent.key, index, block, OPTS) // move the sibling nodes into the new block node siblings.forEach((node, i) => { - transform.moveNodeByKey(node.key, block.key, i, { normalize: false }) + transform.moveNodeByKey(node.key, block.key, i, OPTS) }) if (normalize) { transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -904,11 +880,10 @@ export function wrapBlockAtRange(transform, range, block, options = {}) { * @param {Inline|Object|String} inline * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function wrapInlineAtRange(transform, range, inline, options = {}) { - if (range.isCollapsed) return transform + if (range.isCollapsed) return inline = Normalize.inline(inline) inline = inline.merge({ nodes: inline.nodes.clear() }) @@ -935,11 +910,11 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { if (startBlock == endBlock) { if (endOff != endChild.length) { - transform.splitNodeByKey(endChild.key, endOff, { normalize: false }) + transform.splitNodeByKey(endChild.key, endOff, OPTS) } if (startOff != 0) { - transform.splitNodeByKey(startChild.key, startOff, { normalize: false }) + transform.splitNodeByKey(startChild.key, startOff, OPTS) } state = transform.state @@ -961,10 +936,10 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { const node = inline.regenerateKey() - transform.insertNodeByKey(startBlock.key, startInnerIndex, node, { normalize: false }) + transform.insertNodeByKey(startBlock.key, startInnerIndex, node, OPTS) inlines.forEach((child, i) => { - transform.moveNodeByKey(child.key, node.key, i, { normalize: false }) + transform.moveNodeByKey(child.key, node.key, i, OPTS) }) if (normalize) { @@ -973,8 +948,8 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { } else { - transform.splitNodeByKey(startChild.key, startOff, { normalize: false }) - transform.splitNodeByKey(endChild.key, endOff, { normalize: false }) + transform.splitNodeByKey(startChild.key, startOff, OPTS) + transform.splitNodeByKey(endChild.key, endOff, OPTS) state = transform.state document = state.document @@ -986,15 +961,15 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { const startNode = inline.regenerateKey() const endNode = inline.regenerateKey() - transform.insertNodeByKey(startBlock.key, startIndex - 1, startNode, { normalize: false }) - transform.insertNodeByKey(endBlock.key, endIndex, endNode, { normalize: false }) + transform.insertNodeByKey(startBlock.key, startIndex - 1, startNode, OPTS) + transform.insertNodeByKey(endBlock.key, endIndex, endNode, OPTS) startInlines.forEach((child, i) => { - transform.moveNodeByKey(child.key, startNode.key, i, { normalize: false }) + transform.moveNodeByKey(child.key, startNode.key, i, OPTS) }) endInlines.forEach((child, i) => { - transform.moveNodeByKey(child.key, endNode.key, i, { normalize: false }) + transform.moveNodeByKey(child.key, endNode.key, i, OPTS) }) if (normalize) { @@ -1005,10 +980,10 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { blocks.slice(1, -1).forEach((block) => { const node = inline.regenerateKey() - transform.insertNodeByKey(block.key, 0, node, { normalize: false }) + transform.insertNodeByKey(block.key, 0, node, OPTS) block.nodes.forEach((child, i) => { - transform.moveNodeByKey(child.key, node.key, i, { normalize: false }) + transform.moveNodeByKey(child.key, node.key, i, OPTS) }) if (normalize) { @@ -1016,8 +991,6 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { } }) } - - return transform } /** @@ -1029,7 +1002,6 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) { * @param {String} suffix (optional) * @param {Object} options * @property {Boolean} normalize - * @return {Transform} */ export function wrapTextAtRange(transform, range, prefix, suffix = prefix, options = {}) { @@ -1042,7 +1014,6 @@ export function wrapTextAtRange(transform, range, prefix, suffix = prefix, optio end = end.moveForward(prefix.length) } - return transform - .insertTextAtRange(start, prefix, [], { normalize }) - .insertTextAtRange(end, suffix, [], { normalize }) + transform.insertTextAtRange(start, prefix, [], { normalize }) + transform.insertTextAtRange(end, suffix, [], { normalize }) } diff --git a/src/transforms/by-key.js b/src/transforms/by-key.js index aab7bbfc3..3a128f803 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -11,8 +11,7 @@ import SCHEMA from '../schemas/core' * @param {Number} length * @param {Mixed} mark * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function addMarkByKey(transform, key, offset, length, mark, options = {}) { @@ -28,8 +27,6 @@ export function addMarkByKey(transform, key, offset, length, mark, options = {}) const parent = document.getParent(key) transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -40,8 +37,7 @@ export function addMarkByKey(transform, key, offset, length, mark, options = {}) * @param {Number} index * @param {Node} node * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function insertNodeByKey(transform, key, index, node, options = {}) { @@ -55,8 +51,6 @@ export function insertNodeByKey(transform, key, index, node, options = {}) { if (normalize) { transform.normalizeNodeByKey(key, SCHEMA) } - - return transform } /** @@ -68,8 +62,7 @@ export function insertNodeByKey(transform, key, index, node, options = {}) { * @param {String} text * @param {Set} marks (optional) * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function insertTextByKey(transform, key, offset, text, marks, options = {}) { @@ -84,8 +77,6 @@ export function insertTextByKey(transform, key, offset, text, marks, options = { const parent = document.getParent(key) transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -95,8 +86,7 @@ export function insertTextByKey(transform, key, offset, text, marks, options = { * @param {String} key * @param {String} withKey * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function joinNodeByKey(transform, key, withKey, options = {}) { @@ -110,14 +100,8 @@ export function joinNodeByKey(transform, key, withKey, options = {}) { if (normalize) { const parent = document.getCommonAncestor(key, withKey) - if (parent) { - transform.normalizeNodeByKey(parent.key, SCHEMA) - } else { - transform.normalizeDocument(SCHEMA) - } + transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -129,8 +113,7 @@ export function joinNodeByKey(transform, key, withKey, options = {}) { * @param {String} newKey * @param {Number} index * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) { @@ -143,11 +126,9 @@ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) { transform.moveNodeOperation(path, newPath, newIndex) if (normalize) { - const parent = document.key == newKey ? document : document.getCommonAncestor(key, newKey) + const parent = document.getCommonAncestor(key, newKey) transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -159,8 +140,7 @@ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) { * @param {Number} length * @param {Mark} mark * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function removeMarkByKey(transform, key, offset, length, mark, options = {}) { @@ -176,8 +156,6 @@ export function removeMarkByKey(transform, key, offset, length, mark, options = const parent = document.getParent(key) transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -186,8 +164,7 @@ export function removeMarkByKey(transform, key, offset, length, mark, options = * @param {Transform} transform * @param {String} key * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function removeNodeByKey(transform, key, options = {}) { @@ -200,14 +177,8 @@ export function removeNodeByKey(transform, key, options = {}) { if (normalize) { const parent = document.getParent(key) - if (parent) { - transform.normalizeNodeByKey(parent.key, SCHEMA) - } else { - transform.normalizeDocument(SCHEMA) - } + transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -218,8 +189,7 @@ export function removeNodeByKey(transform, key, options = {}) { * @param {Number} offset * @param {Number} length * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function removeTextByKey(transform, key, offset, length, options = {}) { @@ -231,11 +201,9 @@ export function removeTextByKey(transform, key, offset, length, options = {}) { transform.removeTextOperation(path, offset, length) if (normalize) { - const parent = document.getParent(key) - transform.normalizeParentsByKey(parent.key, SCHEMA) + const block = document.getClosestBlock(key) + transform.normalizeNodeByKey(block.key, SCHEMA) } - - return transform } /** @@ -247,8 +215,7 @@ export function removeTextByKey(transform, key, offset, length, options = {}) { * @param {Number} length * @param {Mark} mark * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function setMarkByKey(transform, key, offset, length, mark, properties, options = {}) { @@ -266,8 +233,6 @@ export function setMarkByKey(transform, key, offset, length, mark, properties, o const parent = document.getParent(key) transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -277,8 +242,7 @@ export function setMarkByKey(transform, key, offset, length, mark, properties, o * @param {String} key * @param {Object|String} properties * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function setNodeByKey(transform, key, properties, options = {}) { @@ -292,14 +256,8 @@ export function setNodeByKey(transform, key, properties, options = {}) { if (normalize) { const parent = document.getParent(key) - if (parent) { - transform.normalizeNodeByKey(parent.key, SCHEMA) - } else { - transform.normalizeDocument(SCHEMA) - } + transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -309,8 +267,7 @@ export function setNodeByKey(transform, key, properties, options = {}) { * @param {String} key * @param {Number} offset * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function splitNodeByKey(transform, key, offset, options = {}) { @@ -323,14 +280,8 @@ export function splitNodeByKey(transform, key, offset, options = {}) { if (normalize) { const parent = document.getParent(key) - if (parent) { - transform.normalizeNodeByKey(parent.key, SCHEMA) - } else { - transform.normalizeDocument(SCHEMA) - } + transform.normalizeNodeByKey(parent.key, SCHEMA) } - - return transform } /** @@ -340,16 +291,17 @@ export function splitNodeByKey(transform, key, offset, options = {}) { * @param {String} key * @param {Object|String} properties * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function unwrapInlineByKey(transform, key, properties, options) { const { state } = transform const { document, selection } = state const node = document.assertDescendant(key) - const range = selection.moveToRangeOf(node.getFirstText(), node.getLastText()) - return transform.unwrapInlineAtRange(range, properties, options) + const first = node.getFirstText() + const last = node.getLastText() + const range = selection.moveToRangeOf(first, last) + transform.unwrapInlineAtRange(range, properties, options) } /** @@ -359,16 +311,17 @@ export function unwrapInlineByKey(transform, key, properties, options) { * @param {String} key * @param {Object|String} properties * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function unwrapBlockByKey(transform, key, properties, options) { const { state } = transform const { document, selection } = state const node = document.assertDescendant(key) - const range = selection.moveToRangeOf(node.getFirstText(), node.getLastText()) - return transform.unwrapBlockAtRange(range, properties, options) + const first = node.getFirstText() + const last = node.getLastText() + const range = selection.moveToRangeOf(first, last) + transform.unwrapBlockAtRange(range, properties, options) } /** @@ -378,8 +331,7 @@ export function unwrapBlockByKey(transform, key, properties, options) { * @param {String} key The node to wrap * @param {Block|Object|String} block The wrapping block (its children are discarded) * @param {Object} options - * @param {Boolean} normalize - * @return {Transform} + * @property {Boolean} normalize */ export function wrapBlockByKey(transform, key, block, options) { @@ -391,7 +343,6 @@ export function wrapBlockByKey(transform, key, block, options) { const parent = document.getParent(node.key) const index = parent.nodes.indexOf(node) - return transform - .insertNodeByKey(parent.key, index, block, { normalize: false }) - .moveNodeByKey(node.key, block.key, 0, options) + transform.insertNodeByKey(parent.key, index, block, { normalize: false }) + transform.moveNodeByKey(node.key, block.key, 0, options) } diff --git a/src/transforms/index.js b/src/transforms/index.js index a1c69037d..8ee1eac6f 100644 --- a/src/transforms/index.js +++ b/src/transforms/index.js @@ -126,10 +126,14 @@ import { focus, moveBackward, moveForward, + moveEndOffset, + moveStartOffset, moveTo, moveToOffsets, moveToRangeOf, + unsetMarks, unsetSelection, + snapshotSelection, } from './on-selection' /** @@ -151,7 +155,6 @@ import { normalizeDocument, normalizeSelection, normalizeNodeByKey, - normalizeParentsByKey, } from './normalize' /** @@ -278,10 +281,14 @@ export default { focus, moveBackward, moveForward, + moveEndOffset, + moveStartOffset, moveTo, moveToOffsets, moveToRangeOf, + unsetMarks, unsetSelection, + snapshotSelection, /** * History. @@ -299,5 +306,4 @@ export default { normalizeDocument, normalizeSelection, normalizeNodeByKey, - normalizeParentsByKey, } diff --git a/src/transforms/normalize.js b/src/transforms/normalize.js index c3f193b1f..76ecbae59 100644 --- a/src/transforms/normalize.js +++ b/src/transforms/normalize.js @@ -8,13 +8,11 @@ import warn from '../utils/warn' * * @param {Transform} transform * @param {Schema} schema - * @return {Transform} */ export function normalize(transform, schema) { transform.normalizeDocument(schema) transform.normalizeSelection(schema) - return transform } /** @@ -22,14 +20,12 @@ export function normalize(transform, schema) { * * @param {Transform} transform * @param {Schema} schema - * @return {Transform} */ export function normalizeDocument(transform, schema) { const { state } = transform const { document } = state transform.normalizeNodeByKey(document.key, schema) - return transform } /** @@ -38,57 +34,26 @@ export function normalizeDocument(transform, schema) { * @param {Transform} transform * @param {Node|String} key * @param {Schema} schema - * @return {Transform} */ export function normalizeNodeByKey(transform, key, schema) { assertSchema(schema) - key = Normalize.key(key) // If the schema has no validation rules, there's nothing to normalize. - if (!schema.hasValidators) { - return transform - } + if (!schema.hasValidators) return + key = Normalize.key(key) const { state } = transform const { document } = state const node = document.assertNode(key) - normalizeNodeWith(transform, node, schema) - return transform + normalizeNodeAndChildren(transform, node, schema) } /** - * Normalize a `node` and its parents with a `schema`. + * Normalize the selection. * * @param {Transform} transform - * @param {Node|String} key - * @param {Schema} schema - * @return {Transform} - */ - -export function normalizeParentsByKey(transform, key, schema) { - assertSchema(schema) - key = Normalize.key(key) - - // If the schema has no validation rules, there's nothing to normalize. - if (!schema.hasValidators) { - return transform - } - - const { state } = transform - const { document } = state - const node = document.assertNode(key) - - normalizeParentsWith(transform, node, schema) - return transform -} - -/** - * Normalize only the selection. - * - * @param {Transform} transform - * @return {Transform} */ export function normalizeSelection(transform) { @@ -117,7 +82,6 @@ export function normalizeSelection(transform) { state = state.merge({ selection }) transform.state = state - return transform } /** @@ -126,58 +90,29 @@ export function normalizeSelection(transform) { * @param {Transform} transform * @param {Node} node * @param {Schema} schema - * @return {Transform} */ -function normalizeNodeWith(transform, node, schema) { - // For performance considerations, we will check if the transform was changed. - const opCount = transform.operations.length +function normalizeNodeAndChildren(transform, node, schema) { + // For performance considerations, we will check if the transform has actually + // added operations to the queue. + const count = transform.operations.length // Iterate over its children. - normalizeChildrenWith(transform, node, schema) + if (node.kind != 'text') { + node.nodes.forEach((child) => { + normalizeNodeAndChildren(transform, child, schema) + }) + } // Re-find the node reference if necessary. - if (transform.operations.length != opCount) { + if (transform.operations.length != count) { node = refindNode(transform, node) } // Now normalize the node itself if it still exists. if (node) { - normalizeNodeOnly(transform, node, schema) + normalizeNode(transform, node, schema) } - - return transform -} - -/** - * Normalize a `node` and its parents with a `schema`. - * - * @param {Transform} transform - * @param {Node} node - * @param {Schema} schema - * @return {Transform} - */ - -function normalizeParentsWith(transform, node, schema) { - normalizeNodeOnly(transform, node, schema) - - // Normalize went back up to the very top of the document. - if (node.kind == 'document') { - return transform - } - - // Re-find the node first. - node = refindNode(transform, node) - - if (!node) { - return transform - } - - const { state } = transform - const { document } = state - const parent = document.getParent(node.key) - - return normalizeParentsWith(transform, parent, schema) } /** @@ -197,51 +132,30 @@ function refindNode(transform, node) { : document.getDescendant(node.key) } -/** - * Normalize the children of a `node` with a `schema`. - * - * @param {Transform} transform - * @param {Node} node - * @param {Schema} schema - * @return {Transform} - */ - -function normalizeChildrenWith(transform, node, schema) { - if (node.kind == 'text') return transform - - node.nodes.forEach((child) => { - normalizeNodeWith(transform, child, schema) - }) - - return transform -} - /** * Normalize a `node` with a `schema`, but not its children. * * @param {Transform} transform * @param {Node} node * @param {Schema} schema - * @return {Transform} */ -function normalizeNodeOnly(transform, node, schema) { +function normalizeNode(transform, node, schema) { let max = schema.rules.length let iterations = 0 function iterate(t, n) { const failure = n.validate(schema) - if (!failure) return t + if (!failure) return + // Run the `normalize` function for the rule with the invalid value. const { value, rule } = failure - - // Rule the `normalize` function for the rule with the invalid value. rule.normalize(t, n, value) // Re-find the node reference, in case it was updated. If the node no longer // exists, we're done for this branch. n = refindNode(t, n) - if (!n) return t + if (!n) return // Increment the iterations counter, and check to make sure that we haven't // exceeded the max. Without this check, it's easy for the `validate` or @@ -254,10 +168,10 @@ function normalizeNodeOnly(transform, node, schema) { } // Otherwise, iterate again. - return iterate(t, n) + iterate(t, n) } - return iterate(transform, node) + iterate(transform, node) } /** @@ -267,9 +181,9 @@ function normalizeNodeOnly(transform, node, schema) { */ function assertSchema(schema) { - if (schema instanceof Schema) return - - if (schema == null) { + if (schema instanceof Schema) { + return + } else if (schema == null) { throw new Error('You must pass a `schema` object.') } else { throw new Error(`You passed an invalid \`schema\` object: ${schema}.`) diff --git a/src/transforms/on-history.js b/src/transforms/on-history.js index eb4b2631c..5daa478a1 100644 --- a/src/transforms/on-history.js +++ b/src/transforms/on-history.js @@ -3,7 +3,6 @@ * Redo to the next state in the history. * * @param {Transform} transform - * @return {Transform} */ export function redo(transform) { @@ -13,7 +12,7 @@ export function redo(transform) { // If there's no next snapshot, abort. let next = redos.peek() - if (!next) return transform + if (!next) return // Shift the next state into the undo stack. redos = redos.pop() @@ -31,7 +30,6 @@ export function redo(transform) { // Update the transform. transform.state = state - return transform } /** @@ -39,7 +37,6 @@ export function redo(transform) { * * @param {Transform} transform * @param {Object} options - * @return {Transform} */ export function save(transform, options = {}) { @@ -49,7 +46,7 @@ export function save(transform, options = {}) { let { undos, redos } = history // If there are no operations, abort. - if (!operations.length) return transform + if (!operations.length) return // Create a new save point or merge the operations into the previous one. if (merge) { @@ -71,14 +68,12 @@ export function save(transform, options = {}) { // Update the transform. transform.state = state - return transform } /** * Undo the previous operations in the history. * * @param {Transform} transform - * @return {Transform} */ export function undo(transform) { @@ -88,7 +83,7 @@ export function undo(transform) { // If there's no previous snapshot, abort. let previous = undos.peek() - if (!previous) return transform + if (!previous) return // Shift the previous operations into the redo stack. undos = undos.pop() @@ -108,5 +103,4 @@ export function undo(transform) { // Update the transform. transform.state = state - return transform } diff --git a/src/transforms/on-selection.js b/src/transforms/on-selection.js index 27b3365fb..9eb177dbc 100644 --- a/src/transforms/on-selection.js +++ b/src/transforms/on-selection.js @@ -3,70 +3,65 @@ * Blur the selection. * * @param {Transform} transform - * @return {Transform} */ export function blur(transform) { const { state } = transform const { selection } = state const sel = selection.blur() - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the focus point to the anchor point. * * @param {Transform} transform - * @return {Transform} */ export function collapseToAnchor(transform) { const { state } = transform const { selection } = state const sel = selection.collapseToAnchor() - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the start point to the end point. * * @param {Transform} transform - * @return {Transform} */ export function collapseToEnd(transform) { const { state } = transform const { selection } = state const sel = selection.collapseToEnd() - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the anchor point to the focus point. * * @param {Transform} transform - * @return {Transform} */ export function collapseToFocus(transform) { const { state } = transform const { selection } = state const sel = selection.collapseToFocus() - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the end point to the start point. * * @param {Transform} transform - * @return {Transform} */ export function collapseToStart(transform) { const { state } = transform const { selection } = state const sel = selection.collapseToStart() - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -74,21 +69,19 @@ export function collapseToStart(transform) { * * @param {Transform} transform * @param {Node} node - * @return {Transform} */ export function collapseToEndOf(transform, node) { const { state } = transform const { selection } = state const sel = selection.collapseToEndOf(node) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the end of the next block. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToEndOfNextBlock(transform) { @@ -97,17 +90,16 @@ export function collapseToEndOfNextBlock(transform) { const blocks = document.getBlocksAtRange(selection) const last = blocks.last() const next = document.getNextBlock(last) - if (!next) return transform + if (!next) return const sel = selection.collapseToEndOf(next) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the end of the next text. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToEndOfNextText(transform) { @@ -116,17 +108,16 @@ export function collapseToEndOfNextText(transform) { const texts = document.getTextsAtRange(selection) const last = texts.last() const next = document.getNextText(last) - if (!next) return transform + if (!next) return const sel = selection.collapseToEndOf(next) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the end of the previous block. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToEndOfPreviousBlock(transform) { @@ -135,17 +126,16 @@ export function collapseToEndOfPreviousBlock(transform) { const blocks = document.getBlocksAtRange(selection) const first = blocks.first() const previous = document.getPreviousBlock(first) - if (!previous) return transform + if (!previous) return const sel = selection.collapseToEndOf(previous) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the end of the previous text. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToEndOfPreviousText(transform) { @@ -154,10 +144,10 @@ export function collapseToEndOfPreviousText(transform) { const texts = document.getTextsAtRange(selection) const first = texts.first() const previous = document.getPreviousText(first) - if (!previous) return transform + if (!previous) return const sel = selection.collapseToEndOf(previous) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -165,21 +155,19 @@ export function collapseToEndOfPreviousText(transform) { * * @param {Transform} transform * @param {Node} node - * @return {Transform} */ export function collapseToStartOf(transform, node) { const { state } = transform const { selection } = state const sel = selection.collapseToStartOf(node) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the start of the next block. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToStartOfNextBlock(transform) { @@ -188,17 +176,16 @@ export function collapseToStartOfNextBlock(transform) { const blocks = document.getBlocksAtRange(selection) const last = blocks.last() const next = document.getNextBlock(last) - if (!next) return transform + if (!next) return const sel = selection.collapseToStartOf(next) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the start of the next text. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToStartOfNextText(transform) { @@ -207,17 +194,16 @@ export function collapseToStartOfNextText(transform) { const texts = document.getTextsAtRange(selection) const last = texts.last() const next = document.getNextText(last) - if (!next) return transform + if (!next) return const sel = selection.collapseToStartOf(next) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the start of the previous block. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToStartOfPreviousBlock(transform) { @@ -226,17 +212,16 @@ export function collapseToStartOfPreviousBlock(transform) { const blocks = document.getBlocksAtRange(selection) const first = blocks.first() const previous = document.getPreviousBlock(first) - if (!previous) return transform + if (!previous) return const sel = selection.collapseToStartOf(previous) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Move the selection to the start of the previous text. * * @param {Transform} tansform - * @return {Transform} */ export function collapseToStartOfPreviousText(transform) { @@ -245,10 +230,10 @@ export function collapseToStartOfPreviousText(transform) { const texts = document.getTextsAtRange(selection) const first = texts.first() const previous = document.getPreviousText(first) - if (!previous) return transform + if (!previous) return const sel = selection.collapseToStartOf(previous) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -256,14 +241,13 @@ export function collapseToStartOfPreviousText(transform) { * * @param {Transform} transform * @param {Number} n (optional) - * @return {Transform} */ export function extendBackward(transform, n) { const { state } = transform const { document, selection } = state const sel = selection.extendBackward(n).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -271,14 +255,13 @@ export function extendBackward(transform, n) { * * @param {Transform} transform * @param {Number} n (optional) - * @return {Transform} */ export function extendForward(transform, n) { const { state } = transform const { document, selection } = state const sel = selection.extendForward(n).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -286,14 +269,13 @@ export function extendForward(transform, n) { * * @param {Transform} transform * @param {Node} node - * @return {Transform} */ export function extendToEndOf(transform, node) { const { state } = transform const { document, selection } = state const sel = selection.extendToEndOf(node).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -301,28 +283,26 @@ export function extendToEndOf(transform, node) { * * @param {Transform} transform * @param {Node} node - * @return {Transform} */ export function extendToStartOf(transform, node) { const { state } = transform const { document, selection } = state const sel = selection.extendToStartOf(node).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** * Focus the selection. * * @param {Transform} transform - * @return {Transform} */ export function focus(transform) { const { state } = transform const { selection } = state const sel = selection.focus() - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -330,14 +310,13 @@ export function focus(transform) { * * @param {Transform} transform * @param {Number} n (optional) - * @return {Transform} */ export function moveBackward(transform, n) { const { state } = transform const { document, selection } = state const sel = selection.moveBackward(n).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -345,14 +324,13 @@ export function moveBackward(transform, n) { * * @param {Transform} transform * @param {Number} n (optional) - * @return {Transform} */ export function moveForward(transform, n) { const { state } = transform const { document, selection } = state const sel = selection.moveForward(n).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) } /** @@ -360,11 +338,10 @@ export function moveForward(transform, n) { * * @param {Transform} transform * @param {Object} properties - * @return {Transform} */ export function moveTo(transform, properties) { - return transform.setSelectionOperation(properties) + transform.setSelectionOperation(properties) } /** @@ -373,14 +350,13 @@ export function moveTo(transform, properties) { * @param {Transform} transform * @param {Number} anchor * @param {Number} focus (optional) - * @return {Transform} */ -export function moveToOffsets(transform, anchor, fokus) { +export function moveToOffsets(transform, anchor, _focus) { const { state } = transform const { selection } = state - const sel = selection.moveToOffsets(anchor, fokus) - return transform.setSelectionOperation(sel) + const sel = selection.moveToOffsets(anchor, _focus) + transform.setSelectionOperation(sel) } /** @@ -389,25 +365,73 @@ export function moveToOffsets(transform, anchor, fokus) { * @param {Transform} transform * @param {Node} start * @param {Node} end (optional) - * @return {Transform} */ export function moveToRangeOf(transform, start, end) { const { state } = transform const { document, selection } = state const sel = selection.moveToRangeOf(start, end).normalize(document) - return transform.setSelectionOperation(sel) + transform.setSelectionOperation(sel) +} + +/** + * Move the start offset by `n`. + * + * @param {Transform} transform + * @param {Number} n + */ + +export function moveStartOffset(transform, n) { + const { state } = transform + const { document, selection } = state + const sel = selection.moveStartOffset(n).normalize(document) + transform.setSelectionOperation(sel) +} + +/** + * Move the end offset by `n`. + * + * @param {Transform} transform + * @param {Number} n + */ + +export function moveEndOffset(transform, n) { + const { state } = transform + const { document, selection } = state + const sel = selection.moveEndOffset(n).normalize(document) + transform.setSelectionOperation(sel) +} + +/** + * Unset the selection's marks. + * + * @param {Transform} transform + */ + +export function unsetMarks(transform) { + transform.setSelectionOperation({ marks: null }) +} + +/** + * Snapshot the current selection. + * + * @param {Transform} transform + */ + +export function snapshotSelection(transform) { + const { state } = transform + const { selection } = state + transform.setSelectionOperation(selection, { snapshot: true }) } /** * Unset the selection, removing an association to a node. * * @param {Transform} transform - * @return {Transform} */ export function unsetSelection(transform) { - return transform.setSelectionOperation({ + transform.setSelectionOperation({ anchorKey: null, anchorOffset: 0, focusKey: null, diff --git a/src/transforms/operations.js b/src/transforms/operations.js index e9ae80939..923919192 100644 --- a/src/transforms/operations.js +++ b/src/transforms/operations.js @@ -9,7 +9,6 @@ import Normalize from '../utils/normalize' * @param {Number} offset * @param {Number} length * @param {Mixed} mark - * @return {Transform} */ export function addMarkOperation(transform, path, offset, length, mark) { @@ -30,7 +29,7 @@ export function addMarkOperation(transform, path, offset, length, mark) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -40,7 +39,6 @@ export function addMarkOperation(transform, path, offset, length, mark) { * @param {Array} path * @param {Number} index * @param {Node} node - * @return {Transform} */ export function insertNodeOperation(transform, path, index, node) { @@ -58,7 +56,7 @@ export function insertNodeOperation(transform, path, index, node) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -69,7 +67,6 @@ export function insertNodeOperation(transform, path, index, node) { * @param {Number} offset * @param {String} text * @param {Set} marks (optional) - * @return {Transform} */ export function insertTextOperation(transform, path, offset, text, marks) { @@ -90,7 +87,7 @@ export function insertTextOperation(transform, path, offset, text, marks) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -99,7 +96,6 @@ export function insertTextOperation(transform, path, offset, text, marks) { * @param {Transform} transform * @param {Array} path * @param {Array} withPath - * @return {Transform} */ export function joinNodeOperation(transform, path, withPath) { @@ -134,7 +130,7 @@ export function joinNodeOperation(transform, path, withPath) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -144,7 +140,6 @@ export function joinNodeOperation(transform, path, withPath) { * @param {Array} path * @param {Array} newPath * @param {Number} newIndex - * @return {Transform} */ export function moveNodeOperation(transform, path, newPath, newIndex) { @@ -167,7 +162,7 @@ export function moveNodeOperation(transform, path, newPath, newIndex) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -178,7 +173,6 @@ export function moveNodeOperation(transform, path, newPath, newIndex) { * @param {Number} offset * @param {Number} length * @param {Mark} mark - * @return {Transform} */ export function removeMarkOperation(transform, path, offset, length, mark) { @@ -199,7 +193,7 @@ export function removeMarkOperation(transform, path, offset, length, mark) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -207,7 +201,6 @@ export function removeMarkOperation(transform, path, offset, length, mark) { * * @param {Transform} transform * @param {Array} path - * @return {Transform} */ export function removeNodeOperation(transform, path) { @@ -230,7 +223,7 @@ export function removeNodeOperation(transform, path) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -240,7 +233,6 @@ export function removeNodeOperation(transform, path) { * @param {Array} path * @param {Number} offset * @param {Number} length - * @return {Transform} */ export function removeTextOperation(transform, path, offset, length) { @@ -282,7 +274,7 @@ export function removeTextOperation(transform, path, offset, length) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -294,7 +286,6 @@ export function removeTextOperation(transform, path, offset, length) { * @param {Number} length * @param {Mark} mark * @param {Mark} newMark - * @return {Transform} */ export function setMarkOperation(transform, path, offset, length, mark, newMark) { @@ -317,7 +308,7 @@ export function setMarkOperation(transform, path, offset, length, mark, newMark) inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -326,7 +317,6 @@ export function setMarkOperation(transform, path, offset, length, mark, newMark) * @param {Transform} transform * @param {Array} path * @param {Object} properties - * @return {Transform} */ export function setNodeOperation(transform, path, properties) { @@ -352,7 +342,7 @@ export function setNodeOperation(transform, path, properties) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -360,10 +350,9 @@ export function setNodeOperation(transform, path, properties) { * * @param {Transform} transform * @param {Mixed} selection - * @return {Transform} */ -export function setSelectionOperation(transform, properties) { +export function setSelectionOperation(transform, properties, options = {}) { properties = Normalize.selectionProperties(properties) const { state } = transform @@ -375,7 +364,7 @@ export function setSelectionOperation(transform, properties) { // create a dictionary of the previous values for all of the properties that // are being changed, for the inverse operation. for (const k in properties) { - if (properties[k] == selection[k]) continue + if (!options.snapshot && properties[k] == selection[k]) continue props[k] = properties[k] prevProps[k] = selection[k] } @@ -432,7 +421,7 @@ export function setSelectionOperation(transform, properties) { } // Apply the operation. - return transform.applyOperation(operation) + transform.applyOperation(operation) } /** @@ -441,7 +430,6 @@ export function setSelectionOperation(transform, properties) { * @param {Transform} transform * @param {Array} path * @param {Number} offset - * @return {Transform} */ export function splitNodeOperation(transform, path, offset) { @@ -462,5 +450,5 @@ export function splitNodeOperation(transform, path, offset) { inverse, } - return transform.applyOperation(operation) + transform.applyOperation(operation) }