diff --git a/lib/models/node.js b/lib/models/node.js index da5f791c2..d177ea871 100644 --- a/lib/models/node.js +++ b/lib/models/node.js @@ -119,6 +119,34 @@ const Node = { }, Character.createList()) }, + /** + * Get children after a child by `key`. + * + * @param {String or Node} key + * @return {Node} node + */ + + getChildrenAfter(key) { + const child = this.getChild(key) + const index = this.nodes.indexOf(child) + const nodes = this.nodes.slice(index + 1) + return nodes + }, + + /** + * Get children after a child by `key`, including the child. + * + * @param {String or Node} key + * @return {Node} node + */ + + getChildrenAfterIncluding(key) { + const child = this.getChild(key) + const index = this.nodes.indexOf(child) + const nodes = this.nodes.slice(index) + return nodes + }, + /** * Get children before a child by `key`. * @@ -148,31 +176,35 @@ const Node = { }, /** - * Get children after a child by `key`. + * Get children between two child keys. * - * @param {String or Node} key + * @param {String or Node} start + * @param {String or Node} end * @return {Node} node */ - getChildrenAfter(key) { - const child = this.getChild(key) - const index = this.nodes.indexOf(child) - const nodes = this.nodes.slice(index + 1) - return nodes + getChildrenBetween(start, end) { + start = this.getChild(start) + start = this.nodes.indexOf(start) + end = this.getChild(end) + end = this.nodes.indexOf(end) + return this.nodes.slice(start + 1, end) }, /** - * Get children after a child by `key`, including the child. + * Get children between two child keys, including the two children. * - * @param {String or Node} key + * @param {String or Node} start + * @param {String or Node} end * @return {Node} node */ - getChildrenAfterIncluding(key) { - const child = this.getChild(key) - const index = this.nodes.indexOf(child) - const nodes = this.nodes.slice(index) - return nodes + getChildrenBetweenIncluding(start, end) { + start = this.getChild(start) + start = this.nodes.indexOf(start) + end = this.getChild(end) + end = this.nodes.indexOf(end) + return this.nodes.slice(start, end + 1) }, /** @@ -287,26 +319,27 @@ const Node = { if (range.isCollapsed) return Document.create({ nodes }) // Make sure the children exist. - const { startKey, endKey } = range + const { startKey, startOffset, endKey, endOffset } = range node.assertHasDescendant(startKey) node.assertHasDescendant(endKey) // Split at the start and end. const start = range.moveToStart() - const end = range.moveToEnd() node = node.splitBlockAtRange(start, Infinity) + + const next = node.getNextText(startKey) + const end = startKey == endKey + ? range.moveToStartOf(next).moveForward(endOffset - startOffset) + : range.moveToEnd() node = node.splitBlockAtRange(end, Infinity) // Get the start and end nodes. - const startNode = node.getHighestChild(startKey) - const endNode = node.getHighestChild(endKey) - - nodes = node.nodes - .skipUntil(node => node == startNode) - .rest() - .takeUntil(node => node == endNode) - .push(endNode) + const startNode = node.getNextSibling(node.getHighestChild(startKey)) + const endNode = startKey == endKey + ? node.getHighestChild(next) + : node.getHighestChild(endKey) + nodes = node.getChildrenBetweenIncluding(startNode, endNode) return Document.create({ nodes }) }, diff --git a/lib/models/state.js b/lib/models/state.js index bbbf2922f..51445eb43 100644 --- a/lib/models/state.js +++ b/lib/models/state.js @@ -419,8 +419,13 @@ class State extends Record(DEFAULTS) { document = document.insertFragmentAtRange(selection, fragment) // Determine what the selection should be after inserting. - const last = fragment.getTextNodes().last() - selection = selection.moveToEndOf(last) + const texts = fragment.getTextNodes() + const first = texts.first() + const last = texts.last() + selection = first == last + ? selection.moveForward(fragment.length) + : selection.moveToEndOf(last) + state = state.merge({ document, selection }) return state } diff --git a/lib/models/transforms.js b/lib/models/transforms.js index f79c4e1bf..454b53ca2 100644 --- a/lib/models/transforms.js +++ b/lib/models/transforms.js @@ -163,7 +163,6 @@ const Transforms = { */ insertFragmentAtRange(range, fragment) { - debugger range = range.normalize(this) let node = this