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/by-key.js b/src/transforms/by-key.js index aab7bbfc3..4791e4224 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -12,7 +12,6 @@ import SCHEMA from '../schemas/core' * @param {Mixed} mark * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -41,7 +38,6 @@ export function addMarkByKey(transform, key, offset, length, mark, options = {}) * @param {Node} node * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -69,7 +63,6 @@ export function insertNodeByKey(transform, key, index, node, options = {}) { * @param {Set} marks (optional) * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -96,7 +87,6 @@ export function insertTextByKey(transform, key, offset, text, marks, options = { * @param {String} withKey * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -130,7 +114,6 @@ export function joinNodeByKey(transform, key, withKey, options = {}) { * @param {Number} index * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -160,7 +141,6 @@ export function moveNodeByKey(transform, key, newKey, newIndex, options = {}) { * @param {Mark} mark * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -187,7 +165,6 @@ export function removeMarkByKey(transform, key, offset, length, mark, options = * @param {String} key * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -219,7 +190,6 @@ export function removeNodeByKey(transform, key, options = {}) { * @param {Number} length * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ export function removeTextByKey(transform, key, offset, length, options = {}) { @@ -234,8 +204,6 @@ export function removeTextByKey(transform, key, offset, length, options = {}) { const parent = document.getParent(key) transform.normalizeParentsByKey(parent.key, SCHEMA) } - - return transform } /** @@ -248,7 +216,6 @@ export function removeTextByKey(transform, key, offset, length, options = {}) { * @param {Mark} mark * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -278,7 +243,6 @@ export function setMarkByKey(transform, key, offset, length, mark, properties, o * @param {Object|String} properties * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -310,7 +268,6 @@ export function setNodeByKey(transform, key, properties, options = {}) { * @param {Number} offset * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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 } /** @@ -341,15 +292,16 @@ export function splitNodeByKey(transform, key, offset, options = {}) { * @param {Object|String} properties * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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) } /** @@ -360,15 +312,16 @@ export function unwrapInlineByKey(transform, key, properties, options) { * @param {Object|String} properties * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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) } /** @@ -379,7 +332,6 @@ export function unwrapBlockByKey(transform, key, properties, options) { * @param {Block|Object|String} block The wrapping block (its children are discarded) * @param {Object} options * @param {Boolean} normalize - * @return {Transform} */ 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) }