1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-31 02:49:56 +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()) }, 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`. * 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 * @return {Node} node
*/ */
getChildrenAfter(key) { getChildrenBetween(start, end) {
const child = this.getChild(key) start = this.getChild(start)
const index = this.nodes.indexOf(child) start = this.nodes.indexOf(start)
const nodes = this.nodes.slice(index + 1) end = this.getChild(end)
return nodes 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 * @return {Node} node
*/ */
getChildrenAfterIncluding(key) { getChildrenBetweenIncluding(start, end) {
const child = this.getChild(key) start = this.getChild(start)
const index = this.nodes.indexOf(child) start = this.nodes.indexOf(start)
const nodes = this.nodes.slice(index) end = this.getChild(end)
return nodes 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 }) if (range.isCollapsed) return Document.create({ nodes })
// Make sure the children exist. // Make sure the children exist.
const { startKey, endKey } = range const { startKey, startOffset, endKey, endOffset } = range
node.assertHasDescendant(startKey) node.assertHasDescendant(startKey)
node.assertHasDescendant(endKey) node.assertHasDescendant(endKey)
// Split at the start and end. // Split at the start and end.
const start = range.moveToStart() const start = range.moveToStart()
const end = range.moveToEnd()
node = node.splitBlockAtRange(start, Infinity) 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) node = node.splitBlockAtRange(end, Infinity)
// Get the start and end nodes. // Get the start and end nodes.
const startNode = node.getHighestChild(startKey) const startNode = node.getNextSibling(node.getHighestChild(startKey))
const endNode = node.getHighestChild(endKey) const endNode = startKey == endKey
? node.getHighestChild(next)
nodes = node.nodes : node.getHighestChild(endKey)
.skipUntil(node => node == startNode)
.rest()
.takeUntil(node => node == endNode)
.push(endNode)
nodes = node.getChildrenBetweenIncluding(startNode, endNode)
return Document.create({ nodes }) return Document.create({ nodes })
}, },

View File

@@ -419,8 +419,13 @@ class State extends Record(DEFAULTS) {
document = document.insertFragmentAtRange(selection, fragment) document = document.insertFragmentAtRange(selection, fragment)
// Determine what the selection should be after inserting. // Determine what the selection should be after inserting.
const last = fragment.getTextNodes().last() const texts = fragment.getTextNodes()
selection = selection.moveToEndOf(last) const first = texts.first()
const last = texts.last()
selection = first == last
? selection.moveForward(fragment.length)
: selection.moveToEndOf(last)
state = state.merge({ document, selection }) state = state.merge({ document, selection })
return state return state
} }

View File

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