mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-11 17:53:59 +02:00
fix selection methods resolution of isBackward, closes #512
This commit is contained in:
@@ -597,9 +597,17 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveStartOffset(n = 1) {
|
moveStartOffset(n = 1) {
|
||||||
return this.isBackward
|
if (this.isBackward) {
|
||||||
? this.merge({ focusOffset: this.focusOffset + n })
|
return this.merge({
|
||||||
: this.merge({ anchorOffset: this.anchorOffset + n })
|
focusOffset: this.focusOffset + n,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return this.merge({
|
||||||
|
anchorOffset: this.anchorOffset + n,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -610,9 +618,17 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveEndOffset(n = 1) {
|
moveEndOffset(n = 1) {
|
||||||
return this.isBackward
|
if (this.isBackward) {
|
||||||
? this.merge({ anchorOffset: this.anchorOffset + n })
|
return this.merge({
|
||||||
: this.merge({ focusOffset: this.focusOffset + n })
|
anchorOffset: this.anchorOffset + n,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return this.merge({
|
||||||
|
focusOffset: this.focusOffset + n,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -623,9 +639,19 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveStartTo(key, offset = 0) {
|
moveStartTo(key, offset = 0) {
|
||||||
return this.isBackward
|
if (this.isBackward) {
|
||||||
? this.merge({ focusKey: key, focusOffset: offset })
|
return this.merge({
|
||||||
: this.merge({ anchorKey: key, anchorOffset: offset })
|
focusKey: key,
|
||||||
|
focusOffset: offset,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return this.merge({
|
||||||
|
anchorKey: key,
|
||||||
|
anchorOffset: offset,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -636,9 +662,19 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveEndTo(key, offset = 0) {
|
moveEndTo(key, offset = 0) {
|
||||||
return this.isBackward
|
if (this.isBackward) {
|
||||||
? this.merge({ anchorKey: key, anchorOffset: offset })
|
return this.merge({
|
||||||
: this.merge({ focusKey: key, focusOffset: offset })
|
anchorKey: key,
|
||||||
|
anchorOffset: offset,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return this.merge({
|
||||||
|
focusKey: key,
|
||||||
|
focusOffset: offset,
|
||||||
|
isBackward: null
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,82 +1,26 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Blur the selection.
|
* Auto-generate many transforms based on the `Selection` methods.
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function blur(transform) {
|
export const blur = generate('blur')
|
||||||
const { state } = transform
|
export const collapseToAnchor = generate('collapseToAnchor')
|
||||||
const { selection } = state
|
export const collapseToEnd = generate('collapseToEnd')
|
||||||
const sel = selection.blur()
|
export const collapseToFocus = generate('collapseToFocus')
|
||||||
transform.setSelectionOperation(sel)
|
export const collapseToStart = generate('collapseToStart')
|
||||||
}
|
export const collapseToEndOf = generate('collapseToEndOf')
|
||||||
|
export const collapseToStartOf = generate('collapseToStartOf')
|
||||||
/**
|
export const extendBackward = generate('extendBackward')
|
||||||
* Move the focus point to the anchor point.
|
export const extendForward = generate('extendForward')
|
||||||
*
|
export const extendToEndOf = generate('extendToEndOf')
|
||||||
* @param {Transform} transform
|
export const extendToStartOf = generate('extendToStartOf')
|
||||||
*/
|
export const focus = generate('focus')
|
||||||
|
export const moveBackward = generate('moveBackward')
|
||||||
export function collapseToAnchor(transform) {
|
export const moveForward = generate('moveForward')
|
||||||
const { state } = transform
|
export const moveToOffsets = generate('moveToOffsets')
|
||||||
const { selection } = state
|
export const moveToRangeOf = generate('moveToRangeOf')
|
||||||
const sel = selection.collapseToAnchor()
|
export const moveStartOffset = generate('moveStartOffset')
|
||||||
transform.setSelectionOperation(sel)
|
export const moveEndOffset = generate('moveEndOffset')
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move the start point to the end point.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function collapseToEnd(transform) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.collapseToEnd()
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move the anchor point to the focus point.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function collapseToFocus(transform) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.collapseToFocus()
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move the end point to the start point.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function collapseToStart(transform) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.collapseToStart()
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move to the end of a `node`.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function collapseToEndOf(transform, node) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.collapseToEndOf(node)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the end of the next block.
|
* Move the selection to the end of the next block.
|
||||||
@@ -150,20 +94,6 @@ export function collapseToEndOfPreviousText(transform) {
|
|||||||
transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Move to the start of a `node`.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function collapseToStartOf(transform, node) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.collapseToStartOf(node)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to the start of the next block.
|
* Move the selection to the start of the next block.
|
||||||
*
|
*
|
||||||
@@ -236,103 +166,6 @@ export function collapseToStartOfPreviousText(transform) {
|
|||||||
transform.setSelectionOperation(sel)
|
transform.setSelectionOperation(sel)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Extend the focus point backward `n` characters.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Number} n (optional)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function extendBackward(transform, n) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.extendBackward(n).normalize(document)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extend the focus point forward `n` characters.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Number} n (optional)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function extendForward(transform, n) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.extendForward(n).normalize(document)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extend the focus point to the end of a `node`.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function extendToEndOf(transform, node) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.extendToEndOf(node).normalize(document)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extend the focus point to the start of a `node`.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function extendToStartOf(transform, node) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.extendToStartOf(node).normalize(document)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Focus the selection.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function focus(transform) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.focus()
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move the selection backward `n` characters.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Number} n (optional)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function moveBackward(transform, n) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.moveBackward(n).normalize(document)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move the selection forward `n` characters.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Number} n (optional)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function moveForward(transform, n) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.moveForward(n).normalize(document)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the selection to a specific anchor and focus point.
|
* Move the selection to a specific anchor and focus point.
|
||||||
*
|
*
|
||||||
@@ -344,64 +177,6 @@ export function moveTo(transform, properties) {
|
|||||||
transform.setSelectionOperation(properties)
|
transform.setSelectionOperation(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Move the selection to `anchor` and `focus` offsets.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Number} anchor
|
|
||||||
* @param {Number} focus (optional)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function moveToOffsets(transform, anchor, _focus) {
|
|
||||||
const { state } = transform
|
|
||||||
const { selection } = state
|
|
||||||
const sel = selection.moveToOffsets(anchor, _focus)
|
|
||||||
transform.setSelectionOperation(sel)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move to the entire range of `start` and `end` nodes.
|
|
||||||
*
|
|
||||||
* @param {Transform} transform
|
|
||||||
* @param {Node} start
|
|
||||||
* @param {Node} end (optional)
|
|
||||||
*/
|
|
||||||
|
|
||||||
export function moveToRangeOf(transform, start, end) {
|
|
||||||
const { state } = transform
|
|
||||||
const { document, selection } = state
|
|
||||||
const sel = selection.moveToRangeOf(start, end).normalize(document)
|
|
||||||
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.
|
* Unset the selection's marks.
|
||||||
*
|
*
|
||||||
@@ -440,3 +215,19 @@ export function unsetSelection(transform) {
|
|||||||
isBackward: false
|
isBackward: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a selection transform for `method`.
|
||||||
|
*
|
||||||
|
* @param {String} method
|
||||||
|
* @return {Function}
|
||||||
|
*/
|
||||||
|
|
||||||
|
function generate(method) {
|
||||||
|
return (transform, ...args) => {
|
||||||
|
const { state } = transform
|
||||||
|
const { document, selection } = state
|
||||||
|
const sel = selection[method](...args).normalize(document)
|
||||||
|
transform.setSelectionOperation(sel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user