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