1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-28 17:39:57 +02:00

Fix selection after transform.wrapInline (#872)

* Add failing test for wrapInline

* Adapt tests for wrap-twice and whole-block

* Adapt transform to move selection correctly

* Fix lint errors

* Use keys in yaml
This commit is contained in:
Samy Pessé
2017-06-10 19:34:57 +02:00
committed by Ian Storm Taylor
parent 54d204b87e
commit 57da8da4b4
5 changed files with 77 additions and 8 deletions

View File

@@ -424,7 +424,6 @@ Transforms.wrapInline = (transform, properties) => {
let after let after
const { startKey } = selection const { startKey } = selection
const previous = document.getPreviousText(startKey)
transform.deselect() transform.deselect()
transform.wrapInlineAtRange(selection, properties) transform.wrapInlineAtRange(selection, properties)
@@ -437,8 +436,21 @@ Transforms.wrapInline = (transform, properties) => {
} }
else if (selection.startOffset == 0) { else if (selection.startOffset == 0) {
const text = previous ? document.getNextText(previous.key) : document.getFirstText() // Find the inline that has been inserted.
after = selection.moveToRangeOf(text) // We want to handle multiple wrap, so we need to take the highest parent
const inline = document.getAncestors(startKey)
.find(parent => (
parent.kind == 'inline' &&
parent.getOffset(startKey) == 0
))
const start = inline ? document.getPreviousText(inline.getFirstText().key) : document.getFirstText()
const end = document.getNextText(inline ? inline.getLastText().key : start.key)
// Move selection to wrap around the inline
after = selection
.moveAnchorToEndOf(start)
.moveFocusToStartOf(end)
} }
else if (selection.startKey == selection.endKey) { else if (selection.startKey == selection.endKey) {

View File

@@ -19,15 +19,15 @@ export default function (state) {
.wrapInline('outer') .wrapInline('outer')
.apply() .apply()
const updated = next.document.getTexts().get(1) const anchor = next.document.getTexts().get(0)
const focus = next.document.getTexts().get(4)
// TODO: seems wrong.
assert.deepEqual( assert.deepEqual(
next.selection.toJS(), next.selection.toJS(),
range.merge({ range.merge({
anchorKey: updated.key, anchorKey: anchor.key,
anchorOffset: 0, anchorOffset: anchor.length,
focusKey: updated.key, focusKey: focus.key,
focusOffset: 0 focusOffset: 0
}).toJS() }).toJS()
) )

View File

@@ -0,0 +1,30 @@
import assert from 'assert'
export default function (state) {
const { selection } = state
const range = selection.merge({
anchorKey: 'b',
anchorOffset: 0,
focusKey: 'b',
focusOffset: 4
})
const next = state
.transform()
.select(range)
.wrapInline('hashtag')
.apply()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: '4',
anchorOffset: 0,
focusKey: '5',
focusOffset: 0
}).toJS()
)
return next
}

View File

@@ -0,0 +1,9 @@
nodes:
- kind: block
type: paragraph
key: a
nodes:
- kind: text
key: b
text: word

View File

@@ -0,0 +1,18 @@
nodes:
- kind: block
key: a
type: paragraph
nodes:
- kind: text
key: c
text: ""
- kind: inline
type: hashtag
nodes:
- kind: text
key: b
text: word
- kind: text
key: d
text: ""