1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-21 13:51:59 +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>
<div contenteditable>
<p>
Some text.
<strong>Some <em>text.</em></strong>
</p>
<p>
Some text.
<strong>Some <a href="google">text.</a></strong>
</p>
<p>
<strong>Some text.</strong>
</p>
<blockquote>
<p>
Some text.
<strong>Some <a href="google">text.</a></strong>
</p>
<p>
Some text.
<strong>Some text.</strong>
</p>
</blockquote>
<p></p>

View File

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