1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-03 04:02:33 +02:00

Fix inserting fragment at the end of current text node (#405)

* fix insertFragment

* insert multi-block fragment at the end of current text node test case
This commit is contained in:
Nikita Zyulyaev
2016-10-26 21:30:45 +03:00
committed by Ian Storm Taylor
parent f380943926
commit 270c2ab219
5 changed files with 84 additions and 5 deletions

View File

@@ -262,6 +262,7 @@ export function insertFragment(transform, fragment) {
const lastText = fragment.getTexts().last()
const lastInline = fragment.getClosestInline(lastText)
const beforeTexts = document.getTexts()
const appending = selection.hasEdgeAtEndOf(document.getDescendant(selection.endKey))
transform.unsetSelection()
transform.insertFragmentAtRange(selection, fragment)
@@ -270,17 +271,13 @@ export function insertFragment(transform, fragment) {
const keys = beforeTexts.map(text => text.key)
const news = document.getTexts().filter(n => !keys.includes(n.key))
const text = news.size ? news.takeLast(2).first() : null
const text = appending ? news.last() : news.takeLast(2).first()
let after
if (text && lastInline) {
after = selection.collapseToEndOf(text)
}
else if (text && lastInline) {
after = selection.collapseToStart()
}
else if (text) {
after = selection
.collapseToStartOf(text)

View File

@@ -0,0 +1,17 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
text: fragment
- kind: block
type: list-item
nodes:
- kind: text
text: second fragment
- kind: block
type: list-item
nodes:
- kind: text
text: third fragment

View File

@@ -0,0 +1,41 @@
import assert from 'assert'
import path from 'path'
import readMetadata from 'read-metadata'
import { Raw } from '../../../../../..'
export default function (state) {
const file = path.resolve(__dirname, 'fragment.yaml')
const raw = readMetadata.sync(file)
const fragment = Raw.deserialize(raw, { terse: true }).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: first.length,
focusKey: first.key,
focusOffset: first.length
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const last = next.document.getTexts().last()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: last.key,
anchorOffset: last.length,
focusKey: last.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word

View File

@@ -0,0 +1,17 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: wordfragment
- kind: block
type: list-item
nodes:
- kind: text
text: second fragment
- kind: block
type: list-item
nodes:
- kind: text
text: third fragment