diff --git a/src/transforms/at-current-range.js b/src/transforms/at-current-range.js index 545424b9e..90639d7e9 100644 --- a/src/transforms/at-current-range.js +++ b/src/transforms/at-current-range.js @@ -499,22 +499,14 @@ export function wrapInline(transform, properties) { export function wrapText(transform, prefix, suffix = prefix) { const { state } = transform const { selection } = state - const { anchorOffset, anchorKey, focusOffset, focusKey, isBackward } = selection - let after + transform.wrapTextAtRange(selection, prefix, suffix) - if (anchorKey == focusKey) { - after = selection.moveForward(prefix.length) + // Adding the suffix will have pushed the end of the selection further on, so + // we need to move it back to account for this. + transform.moveEndOffset(0 - suffix.length) + + // If the selection was collapsed, it will have moved the start offset too. + if (selection.isCollapsed) { + transform.moveStartOffset(0 - prefix.length) } - - else { - after = selection.merge({ - anchorOffset: isBackward ? anchorOffset : anchorOffset + prefix.length, - focusOffset: isBackward ? focusOffset + prefix.length : focusOffset - }) - } - - transform - .unsetSelection() - .wrapTextAtRange(selection, prefix, suffix) - .moveTo(after) } diff --git a/src/transforms/index.js b/src/transforms/index.js index 3a0d165ae..3c0205f36 100644 --- a/src/transforms/index.js +++ b/src/transforms/index.js @@ -126,6 +126,8 @@ import { focus, moveBackward, moveForward, + moveEndOffset, + moveStartOffset, moveTo, moveToOffsets, moveToRangeOf, @@ -279,6 +281,8 @@ export default { focus, moveBackward, moveForward, + moveEndOffset, + moveStartOffset, moveTo, moveToOffsets, moveToRangeOf, diff --git a/src/transforms/on-selection.js b/src/transforms/on-selection.js index 639522b24..8aac5e4f8 100644 --- a/src/transforms/on-selection.js +++ b/src/transforms/on-selection.js @@ -352,10 +352,10 @@ export function moveTo(transform, properties) { * @param {Number} focus (optional) */ -export function moveToOffsets(transform, anchor, fokus) { +export function moveToOffsets(transform, anchor, _focus) { const { state } = transform const { selection } = state - const sel = selection.moveToOffsets(anchor, fokus) + const sel = selection.moveToOffsets(anchor, _focus) transform.setSelectionOperation(sel) } @@ -374,6 +374,34 @@ export function moveToRangeOf(transform, start, end) { transform.setSelectionOperation(sel) } +/** + * Move the start offset by `n`. + * + * @param {Transform} transform + * @param {Number} n + */ + +export function moveStartOffset(transform, n) { + const { state } = transform + const { document, selection } = state + const sel = selection.moveStartOffset(n).normalize(document) + transform.setSelectionOperation(sel) +} + +/** + * Move the end offset by `n`. + * + * @param {Transform} transform + * @param {Number} n + */ + +export function moveEndOffset(transform, n) { + const { state } = transform + const { document, selection } = state + const sel = selection.moveEndOffset(n).normalize(document) + transform.setSelectionOperation(sel) +} + /** * Unset the selection's marks. *