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:
@@ -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()
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user