1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 10:29:48 +02:00

got pasting fragments working

This commit is contained in:
Ian Storm Taylor
2016-06-24 18:35:54 -07:00
parent d9a33657a8
commit 0caa49fb10
3 changed files with 64 additions and 27 deletions

View File

@@ -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 })
},

View File

@@ -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
}

View File

@@ -163,7 +163,6 @@ const Transforms = {
*/
insertFragmentAtRange(range, fragment) {
debugger
range = range.normalize(this)
let node = this