1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-09 00:36:41 +02:00

fix block splitting logic

This commit is contained in:
Ian Storm Taylor
2016-06-24 13:44:22 -07:00
parent 3bf4c8b7b4
commit cd21f2ede8
5 changed files with 81 additions and 9 deletions

View File

@@ -8,17 +8,20 @@
<main> <main>
<div contenteditable> <div contenteditable>
<p> <p>
Some text. <strong>Some <em>text.</em></strong>
</p> </p>
<p> <p>
Some text. <strong>Some <a href="google">text.</a></strong>
</p>
<p>
<strong>Some text.</strong>
</p> </p>
<blockquote> <blockquote>
<p> <p>
Some text. <strong>Some <a href="google">text.</a></strong>
</p> </p>
<p> <p>
Some text. <strong>Some text.</strong>
</p> </p>
</blockquote> </blockquote>
<p></p> <p></p>

View File

@@ -848,20 +848,25 @@ const Node = {
const { startKey } = range const { startKey } = range
const firstText = node.getDescendant(startKey) const firstText = node.getDescendant(startKey)
const secondText = node.getNextText(startKey) const secondText = node.getNextText(startKey)
let firstChild = node.getFurthestInline(firstText) || firstText const firstChild = node.getFurthestInline(firstText) || firstText
let secondChild = node.getFurthestInline(secondText) || secondText const secondChild = node.getFurthestInline(secondText) || secondText
let parent = node.getClosestBlock(firstChild) let parent = node.getClosestBlock(firstChild)
let firstChildren = parent.nodes.takeUntil(n => n == firstChild).push(firstChild)
let secondChildren = parent.nodes.skipUntil(n => n == secondChild)
let d = 0 let d = 0
// While the parent is a block, split the block nodes. // While the parent is a block, split the block nodes.
while (parent && d < depth) { while (parent && d < depth) {
firstChild = parent.merge({ nodes: Block.createList([firstChild]) }) const firstChild = parent.merge({ nodes: firstChildren })
secondChild = Block.create({ const secondChild = Block.create({
nodes: [secondChild], nodes: secondChildren,
type: parent.type, type: parent.type,
data: parent.data data: parent.data
}) })
firstChildren = Block.createList([firstChild])
secondChildren = Block.createList([secondChild])
// Add the new children. // Add the new children.
const grandparent = node.getParent(parent) const grandparent = node.getParent(parent)
const nodes = grandparent.nodes const nodes = grandparent.nodes

View File

@@ -0,0 +1,18 @@
export default function (state) {
const { document, selection } = state
const texts = document.getTextNodes()
const first = texts.first()
const second = texts.last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: second.key,
focusOffset: 2
})
return state
.transform()
.splitBlockAtRange(range)
.apply()
}

View File

@@ -0,0 +1,20 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: one
nodes:
- kind: text
ranges:
- text: word
- kind: block
type: paragraph
nodes:
- kind: inline
type: two
nodes:
- kind: text
ranges:
- text: another

View File

@@ -0,0 +1,26 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: one
nodes:
- kind: text
ranges:
- text: wo
- kind: block
type: paragraph
nodes:
- kind: inline
type: one
nodes:
- kind: text
ranges:
- text: ""
- kind: inline
type: two
nodes:
- kind: text
ranges:
- text: other