mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-29 09:59:48 +02:00
fix selection handling for insertFragment and wrapInline
This commit is contained in:
@@ -798,28 +798,37 @@ class State extends new Record(DEFAULTS) {
|
||||
if (!fragment.length) return state
|
||||
|
||||
// Lookup some nodes for determining the selection next.
|
||||
const texts = fragment.getTexts()
|
||||
const lastText = texts.last()
|
||||
const lastText = fragment.getTexts().last()
|
||||
const lastInline = fragment.getClosestInline(lastText)
|
||||
const startText = document.getDescendant(selection.startKey)
|
||||
const startBlock = document.getClosestBlock(startText)
|
||||
const startInline = document.getClosestInline(startText)
|
||||
const nextText = document.getNextText(startText)
|
||||
const nextBlock = nextText ? document.getClosestBlock(nextText) : null
|
||||
const nextNextText = nextText ? document.getNextText(nextText) : null
|
||||
|
||||
const docTexts = document.getTexts()
|
||||
const beforeTexts = document.getTexts()
|
||||
|
||||
// Insert the fragment.
|
||||
document = document.insertFragmentAtRange(selection, fragment)
|
||||
|
||||
// Determine what the selection should be after inserting.
|
||||
const keys = docTexts.map(text => text.key)
|
||||
const keys = beforeTexts.map(text => text.key)
|
||||
const text = document.getTexts().findLast(n => !keys.includes(n.key))
|
||||
const previousText = text ? document.getPreviousText(text) : null
|
||||
|
||||
after = text
|
||||
? selection.collapseToStartOf(text).moveForward(lastText.length)
|
||||
: selection.collapseToStart().moveForward(lastText.length)
|
||||
if (text && lastInline && previousText) {
|
||||
after = selection.collapseToEndOf(previousText)
|
||||
}
|
||||
|
||||
else if (text && lastInline) {
|
||||
after = selection.collapseToStart()
|
||||
}
|
||||
|
||||
else if (text) {
|
||||
after = selection
|
||||
.collapseToStartOf(text)
|
||||
.moveForward(lastText.length)
|
||||
}
|
||||
|
||||
else {
|
||||
after = selection
|
||||
.collapseToStart()
|
||||
.moveForward(lastText.length)
|
||||
}
|
||||
|
||||
// Update the document and selection.
|
||||
selection = after
|
||||
@@ -1103,11 +1112,22 @@ class State extends new Record(DEFAULTS) {
|
||||
selection = selection.moveToRangeOf(text)
|
||||
}
|
||||
|
||||
else {
|
||||
else if (selection.startKey == selection.endKey) {
|
||||
const text = document.getNextText(selection.startKey)
|
||||
selection = selection.moveToRangeOf(text)
|
||||
}
|
||||
|
||||
else {
|
||||
const anchor = document.getNextText(selection.anchorKey)
|
||||
const focus = document.getDescendant(selection.focusKey)
|
||||
selection = selection.merge({
|
||||
anchorKey: anchor.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: focus.key,
|
||||
focusOffset: selection.focusOffset
|
||||
})
|
||||
}
|
||||
|
||||
state = state.merge({ document, selection })
|
||||
return state
|
||||
}
|
||||
|
@@ -28,15 +28,15 @@ export default function (state) {
|
||||
|
||||
const updated = next.document.getTexts().get(1)
|
||||
|
||||
// assert.deepEqual(
|
||||
// next.selection.toJS(),
|
||||
// range.merge({
|
||||
// anchorKey: updated.key,
|
||||
// anchorOffset: last.length,
|
||||
// focusKey: updated.key,
|
||||
// focusOffset: last.length
|
||||
// }).toJS()
|
||||
// )
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
||||
|
@@ -22,15 +22,15 @@ export default function (state) {
|
||||
const two = next.document.getTexts().get(1)
|
||||
const three = next.document.getTexts().get(2)
|
||||
|
||||
// assert.deepEqual(
|
||||
// next.selection.toJS(),
|
||||
// range.merge({
|
||||
// anchorKey: two.key,
|
||||
// anchorOffset: 0,
|
||||
// focusKey: three.key,
|
||||
// focusOffset: three.length
|
||||
// }).toJS()
|
||||
// )
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: two.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: three.key,
|
||||
focusOffset: three.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
||||
|
@@ -22,15 +22,15 @@ export default function (state) {
|
||||
const two = next.document.getTexts().get(1)
|
||||
const three = next.document.getTexts().get(2)
|
||||
|
||||
// assert.deepEqual(
|
||||
// next.selection.toJS(),
|
||||
// range.merge({
|
||||
// anchorKey: two.key,
|
||||
// anchorOffset: 0,
|
||||
// focusKey: three.key,
|
||||
// focusOffset: three.length
|
||||
// }).toJS()
|
||||
// )
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: two.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: three.key,
|
||||
focusOffset: three.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
||||
|
Reference in New Issue
Block a user