1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-25 16:20:49 +02:00

handle surrounding inline void nodes on insert

This commit is contained in:
Ian Storm Taylor
2016-09-23 11:33:29 -07:00
parent 2ed1efbdd4
commit 9df223bce9
3 changed files with 22 additions and 5 deletions

View File

@@ -53,9 +53,9 @@ class Block extends new Record(DEFAULTS) {
properties.isVoid = !!properties.isVoid
properties.nodes = Block.createList(properties.nodes)
// if (properties.nodes.size == 0) {
// properties.nodes = properties.nodes.push(Text.create())
// }
if (properties.nodes.size == 0) {
properties.nodes = properties.nodes.push(Text.create())
}
return new Block(properties).normalize()
}

View File

@@ -37,7 +37,25 @@ export function insertNodeByKey(transform, key, index, node) {
const { document } = state
const path = document.getPath(key)
const newPath = path.slice().push(index)
return 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
// inserted at the block's edge, we need to add surrounding text nodes.
if (node.kind == 'inline' && node.isVoid) {
const parent = document.assertDescendant(key)
if (index == 0) {
const text = Text.create()
transform.insertNodeByKey(key, index, text)
}
if (index == parent.nodes.size) {
const text = Text.create()
transform.insertNodeByKey(key, index + 1, text)
}
}
return transform
}
/**

View File

@@ -18,7 +18,6 @@ describe('transforms', () => {
for (const transform of transforms) {
if (transform[0] == '.') continue
if (transform == 'insert-node-by-key') continue
describe(`${toCamel(transform)}()`, () => {
const transformDir = resolve(__dirname, './fixtures/by-key', transform)