mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-08 22:20:41 +02:00
handle regenerating keys on insert
This commit is contained in:
@@ -941,6 +941,24 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
insertNode(index, 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)
|
const nodes = this.nodes.splice(index, 0, node)
|
||||||
return this.merge({ nodes })
|
return this.merge({ nodes })
|
||||||
},
|
},
|
||||||
@@ -1155,6 +1173,16 @@ const Node = {
|
|||||||
return 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.
|
* Remove a `node` from the children node map.
|
||||||
*
|
*
|
||||||
|
@@ -34,7 +34,7 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
* Create a new `Text` with `properties`.
|
* Create a new `Text` with `properties`.
|
||||||
*
|
*
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
* @return {Text} text
|
* @return {Text}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
@@ -223,7 +223,7 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
* @param {Numbder} index
|
* @param {Numbder} index
|
||||||
* @param {String} text
|
* @param {String} text
|
||||||
* @param {String} marks (optional)
|
* @param {String} marks (optional)
|
||||||
* @return {Text} text
|
* @return {Text}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
insertText(index, text, marks) {
|
insertText(index, text, marks) {
|
||||||
@@ -238,6 +238,16 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
return this.merge({ characters })
|
return this.merge({ characters })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regenerate the node's key.
|
||||||
|
*
|
||||||
|
* @return {Text}
|
||||||
|
*/
|
||||||
|
|
||||||
|
regenerateKey() {
|
||||||
|
return this.merge({ key: uid() })
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a `mark` at `index` and `length`.
|
* Remove a `mark` at `index` and `length`.
|
||||||
*
|
*
|
||||||
@@ -265,7 +275,7 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
*
|
*
|
||||||
* @param {Number} index
|
* @param {Number} index
|
||||||
* @param {Number} length
|
* @param {Number} length
|
||||||
* @return {Text} text
|
* @return {Text}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
removeText(index, length) {
|
removeText(index, length) {
|
||||||
|
@@ -90,8 +90,7 @@ function insertNode(state, operation) {
|
|||||||
let { document } = state
|
let { document } = state
|
||||||
let parent = document.assertPath(path)
|
let parent = document.assertPath(path)
|
||||||
const isParent = document == parent
|
const isParent = document == parent
|
||||||
const nodes = parent.nodes.splice(index, 0, node)
|
parent = parent.insertNode(index, node)
|
||||||
parent = parent.merge({ nodes })
|
|
||||||
document = isParent ? parent : document.updateDescendant(parent)
|
document = isParent ? parent : document.updateDescendant(parent)
|
||||||
state = state.merge({ document })
|
state = state.merge({ document })
|
||||||
return state
|
return state
|
||||||
|
@@ -729,6 +729,7 @@ export function unwrapInlineAtRange(transform, range, properties) {
|
|||||||
|
|
||||||
export function wrapBlockAtRange(transform, range, block) {
|
export function wrapBlockAtRange(transform, range, block) {
|
||||||
block = Normalize.block(block)
|
block = Normalize.block(block)
|
||||||
|
block = block.merge({ nodes: block.nodes.clear() })
|
||||||
|
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document } = state
|
const { document } = state
|
||||||
@@ -799,6 +800,7 @@ export function wrapInlineAtRange(transform, range, inline) {
|
|||||||
if (range.isCollapsed) return transform
|
if (range.isCollapsed) return transform
|
||||||
|
|
||||||
inline = Normalize.inline(inline)
|
inline = Normalize.inline(inline)
|
||||||
|
inline = inline.merge({ nodes: inline.nodes.clear() })
|
||||||
|
|
||||||
const { startKey, startOffset, endKey, endOffset } = range
|
const { startKey, startOffset, endKey, endOffset } = range
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
|
@@ -37,6 +37,7 @@ export function insertNodeByKey(transform, key, index, node) {
|
|||||||
const { document } = state
|
const { document } = state
|
||||||
const path = document.getPath(key)
|
const path = document.getPath(key)
|
||||||
const newPath = path.slice().push(index)
|
const newPath = path.slice().push(index)
|
||||||
|
|
||||||
transform.insertNodeOperation(path, index, node)
|
transform.insertNodeOperation(path, index, node)
|
||||||
|
|
||||||
// If the node is an inline void, the parent is a block, and the node will be
|
// If the node is an inline void, the parent is a block, and the node will be
|
||||||
|
@@ -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
|
||||||
|
}
|
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
Reference in New Issue
Block a user