1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-31 19:01:54 +02:00

handle regenerating keys on insert

This commit is contained in:
Ian Storm Taylor
2016-10-06 11:11:10 -07:00
parent 7d9620d707
commit 105fba0456
8 changed files with 86 additions and 5 deletions

View File

@@ -941,6 +941,24 @@ const Node = {
*/
insertNode(index, node) {
let keys = new Set([ this.key ])
this.findDescendant((desc) => {
keys = keys.add(desc.key)
})
if (keys.contains(node.key)) {
node = node.regenerateKey()
}
if (node.kind != 'text') {
node = node.mapDescendants((desc) => {
return keys.contains(desc.key)
? desc.regenerateKey()
: desc
})
}
const nodes = this.nodes.splice(index, 0, node)
return this.merge({ nodes })
},
@@ -1155,6 +1173,16 @@ const Node = {
return node
},
/**
* Regenerate the node's key.
*
* @return {Node} node
*/
regenerateKey() {
return this.merge({ key: uid() })
},
/**
* Remove a `node` from the children node map.
*

View File

@@ -34,7 +34,7 @@ class Text extends new Record(DEFAULTS) {
* Create a new `Text` with `properties`.
*
* @param {Object} properties
* @return {Text} text
* @return {Text}
*/
static create(properties = {}) {
@@ -223,7 +223,7 @@ class Text extends new Record(DEFAULTS) {
* @param {Numbder} index
* @param {String} text
* @param {String} marks (optional)
* @return {Text} text
* @return {Text}
*/
insertText(index, text, marks) {
@@ -238,6 +238,16 @@ class Text extends new Record(DEFAULTS) {
return this.merge({ characters })
}
/**
* Regenerate the node's key.
*
* @return {Text}
*/
regenerateKey() {
return this.merge({ key: uid() })
}
/**
* Remove a `mark` at `index` and `length`.
*
@@ -265,7 +275,7 @@ class Text extends new Record(DEFAULTS) {
*
* @param {Number} index
* @param {Number} length
* @return {Text} text
* @return {Text}
*/
removeText(index, length) {

View File

@@ -90,8 +90,7 @@ function insertNode(state, operation) {
let { document } = state
let parent = document.assertPath(path)
const isParent = document == parent
const nodes = parent.nodes.splice(index, 0, node)
parent = parent.merge({ nodes })
parent = parent.insertNode(index, node)
document = isParent ? parent : document.updateDescendant(parent)
state = state.merge({ document })
return state

View File

@@ -729,6 +729,7 @@ export function unwrapInlineAtRange(transform, range, properties) {
export function wrapBlockAtRange(transform, range, block) {
block = Normalize.block(block)
block = block.merge({ nodes: block.nodes.clear() })
const { state } = transform
const { document } = state
@@ -799,6 +800,7 @@ export function wrapInlineAtRange(transform, range, inline) {
if (range.isCollapsed) return transform
inline = Normalize.inline(inline)
inline = inline.merge({ nodes: inline.nodes.clear() })
const { startKey, startOffset, endKey, endOffset } = range
let { state } = transform

View File

@@ -37,6 +37,7 @@ export function insertNodeByKey(transform, key, index, node) {
const { document } = state
const path = document.getPath(key)
const newPath = path.slice().push(index)
transform.insertNodeOperation(path, index, node)
// If the node is an inline void, the parent is a block, and the node will be

View File

@@ -0,0 +1,22 @@
import assert from 'assert'
import { Block } from '../../../../../..'
export default function (state) {
const { document, selection } = state
const first = document.getBlocks().first()
debugger
const next = state
.transform()
.insertNodeByKey(document.key, 0, first)
.apply()
const one = next.document.getBlocks().first()
const two = next.document.getBlocks().last()
assert.equal(one.type, two.type)
assert.notEqual(one.key, two.key)
return next
}

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: block
type: paragraph
nodes:
- kind: text
text: one