diff --git a/lib/models/state.js b/lib/models/state.js index 908c1f45a..252608de2 100644 --- a/lib/models/state.js +++ b/lib/models/state.js @@ -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 } diff --git a/test/transforms/fixtures/insert-fragment/middle-inline-fragment-inline/index.js b/test/transforms/fixtures/insert-fragment/middle-inline-fragment-inline/index.js index 1a381fb1e..d9d042c7f 100644 --- a/test/transforms/fixtures/insert-fragment/middle-inline-fragment-inline/index.js +++ b/test/transforms/fixtures/insert-fragment/middle-inline-fragment-inline/index.js @@ -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 } diff --git a/test/transforms/fixtures/wrap-inline/across-blocks/index.js b/test/transforms/fixtures/wrap-inline/across-blocks/index.js index 67102af9d..235be000a 100644 --- a/test/transforms/fixtures/wrap-inline/across-blocks/index.js +++ b/test/transforms/fixtures/wrap-inline/across-blocks/index.js @@ -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 } diff --git a/test/transforms/fixtures/wrap-inline/across-inlines/index.js b/test/transforms/fixtures/wrap-inline/across-inlines/index.js index 67102af9d..235be000a 100644 --- a/test/transforms/fixtures/wrap-inline/across-inlines/index.js +++ b/test/transforms/fixtures/wrap-inline/across-inlines/index.js @@ -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 }