1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 17:23:07 +01:00

fix insertFragment for nested blocks

This commit is contained in:
Ian Storm Taylor 2016-08-11 14:41:51 -07:00
parent 16348ab04f
commit 7ee58a55e2
17 changed files with 146 additions and 1 deletions

View File

@ -468,7 +468,7 @@ const Node = {
*/ */
getHighestOnlyChildParent(key) { getHighestOnlyChildParent(key) {
let child = this.assertChild(key) let child = this.assertDescendant(key)
let match = null let match = null
let parent let parent

View File

@ -0,0 +1,17 @@
nodes:
- kind: block
type: list
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment one
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment two

View File

@ -0,0 +1,25 @@
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: 2,
focusKey: first.key,
focusOffset: 2
})
return state
.transform()
.insertFragmentAtRange(range, fragment)
.apply()
}

View File

@ -0,0 +1,15 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: wofragment one
- kind: block
type: list
nodes:
- kind: block
type: list-item
nodes:
- kind: text
text: fragment tword

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: list
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment one
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment two

View File

@ -0,0 +1,42 @@
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 last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: first.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().last()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: last.length,
focusKey: updated.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,15 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: wofragment one
- kind: block
type: list
nodes:
- kind: block
type: list-item
nodes:
- kind: text
text: fragment tword