Skip to content

Commit

Permalink
fix selection setting in wrapInline transform
Browse files Browse the repository at this point in the history
  • Loading branch information
ianstormtaylor committed Jul 28, 2016
1 parent d80bda5 commit 5f73c63
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/models/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class Selection extends new Record(DEFAULTS) {
anchorOffset: 0,
focusKey: end.key,
focusOffset: end.length,
isBackward: null
isBackward: start == end ? false : null
})
}

Expand Down
9 changes: 6 additions & 3 deletions lib/models/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,9 @@ class State extends new Record(DEFAULTS) {
wrapInline(type, data) {
let state = this
let { document, selection } = state
const { startKey } = selection
const previous = document.getPreviousText(startKey)

document = document.wrapInlineAtRange(selection, type, data)

// Determine what the selection should be after wrapping.
Expand All @@ -1064,15 +1067,15 @@ class State extends new Record(DEFAULTS) {
}

else if (selection.startOffset == 0) {
const text = document.getDescendant(selection.startKey)
const text = previous
? document.getNextText(previous)
: document.getTexts().first()
selection = selection.moveToRangeOf(text)
selection = selection.normalize(document)
}

else {
const text = document.getNextText(selection.startKey)
selection = selection.moveToRangeOf(text)
selection = selection.normalize(document)
}

state = state.merge({ document, selection })
Expand Down
2 changes: 1 addition & 1 deletion lib/models/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ const Transforms = {
: node.updateDescendant(parent.merge({ nodes }))
})

return node
return node.normalize()
}

}
Expand Down
12 changes: 12 additions & 0 deletions test/transforms/fixtures/wrap-inline/twice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,17 @@ export default function (state) {
.wrapInline('outer')
.apply()

const updated = next.document.getTexts().get(1)

assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: 0,
focusKey: updated.key,
focusOffset: updated.length
}).toJS()
)

return next
}

0 comments on commit 5f73c63

Please sign in to comment.