mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-29 18:09:49 +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
|
if (!fragment.length) return state
|
||||||
|
|
||||||
// Lookup some nodes for determining the selection next.
|
// Lookup some nodes for determining the selection next.
|
||||||
const texts = fragment.getTexts()
|
const lastText = fragment.getTexts().last()
|
||||||
const lastText = texts.last()
|
|
||||||
const lastInline = fragment.getClosestInline(lastText)
|
const lastInline = fragment.getClosestInline(lastText)
|
||||||
const startText = document.getDescendant(selection.startKey)
|
const beforeTexts = document.getTexts()
|
||||||
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()
|
|
||||||
|
|
||||||
// Insert the fragment.
|
// Insert the fragment.
|
||||||
document = document.insertFragmentAtRange(selection, fragment)
|
document = document.insertFragmentAtRange(selection, fragment)
|
||||||
|
|
||||||
// Determine what the selection should be after inserting.
|
// 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 text = document.getTexts().findLast(n => !keys.includes(n.key))
|
||||||
|
const previousText = text ? document.getPreviousText(text) : null
|
||||||
|
|
||||||
after = text
|
if (text && lastInline && previousText) {
|
||||||
? selection.collapseToStartOf(text).moveForward(lastText.length)
|
after = selection.collapseToEndOf(previousText)
|
||||||
: selection.collapseToStart().moveForward(lastText.length)
|
}
|
||||||
|
|
||||||
|
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.
|
// Update the document and selection.
|
||||||
selection = after
|
selection = after
|
||||||
@@ -1103,11 +1112,22 @@ class State extends new Record(DEFAULTS) {
|
|||||||
selection = selection.moveToRangeOf(text)
|
selection = selection.moveToRangeOf(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else if (selection.startKey == selection.endKey) {
|
||||||
const text = document.getNextText(selection.startKey)
|
const text = document.getNextText(selection.startKey)
|
||||||
selection = selection.moveToRangeOf(text)
|
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 })
|
state = state.merge({ document, selection })
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
@@ -28,15 +28,15 @@ export default function (state) {
|
|||||||
|
|
||||||
const updated = next.document.getTexts().get(1)
|
const updated = next.document.getTexts().get(1)
|
||||||
|
|
||||||
// assert.deepEqual(
|
assert.deepEqual(
|
||||||
// next.selection.toJS(),
|
next.selection.toJS(),
|
||||||
// range.merge({
|
range.merge({
|
||||||
// anchorKey: updated.key,
|
anchorKey: updated.key,
|
||||||
// anchorOffset: last.length,
|
anchorOffset: last.length,
|
||||||
// focusKey: updated.key,
|
focusKey: updated.key,
|
||||||
// focusOffset: last.length
|
focusOffset: last.length
|
||||||
// }).toJS()
|
}).toJS()
|
||||||
// )
|
)
|
||||||
|
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
@@ -22,15 +22,15 @@ export default function (state) {
|
|||||||
const two = next.document.getTexts().get(1)
|
const two = next.document.getTexts().get(1)
|
||||||
const three = next.document.getTexts().get(2)
|
const three = next.document.getTexts().get(2)
|
||||||
|
|
||||||
// assert.deepEqual(
|
assert.deepEqual(
|
||||||
// next.selection.toJS(),
|
next.selection.toJS(),
|
||||||
// range.merge({
|
range.merge({
|
||||||
// anchorKey: two.key,
|
anchorKey: two.key,
|
||||||
// anchorOffset: 0,
|
anchorOffset: 0,
|
||||||
// focusKey: three.key,
|
focusKey: three.key,
|
||||||
// focusOffset: three.length
|
focusOffset: three.length
|
||||||
// }).toJS()
|
}).toJS()
|
||||||
// )
|
)
|
||||||
|
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
@@ -22,15 +22,15 @@ export default function (state) {
|
|||||||
const two = next.document.getTexts().get(1)
|
const two = next.document.getTexts().get(1)
|
||||||
const three = next.document.getTexts().get(2)
|
const three = next.document.getTexts().get(2)
|
||||||
|
|
||||||
// assert.deepEqual(
|
assert.deepEqual(
|
||||||
// next.selection.toJS(),
|
next.selection.toJS(),
|
||||||
// range.merge({
|
range.merge({
|
||||||
// anchorKey: two.key,
|
anchorKey: two.key,
|
||||||
// anchorOffset: 0,
|
anchorOffset: 0,
|
||||||
// focusKey: three.key,
|
focusKey: three.key,
|
||||||
// focusOffset: three.length
|
focusOffset: three.length
|
||||||
// }).toJS()
|
}).toJS()
|
||||||
// )
|
)
|
||||||
|
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user