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

finish refactoring selection transforms

This commit is contained in:
Ian Storm Taylor
2016-08-18 14:33:40 -07:00
parent 99faf3153c
commit 52820af7ae

View File

@@ -62,8 +62,8 @@ export function collapseToEndOf(transform, node) {
/** /**
* Move the selection to the end of the next block. * Move the selection to the end of the next block.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToEndOfNextBlock(transform) { export function collapseToEndOfNextBlock(transform) {
@@ -81,8 +81,8 @@ export function collapseToEndOfNextBlock(transform) {
/** /**
* Move the selection to the end of the next text. * Move the selection to the end of the next text.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToEndOfNextText(transform) { export function collapseToEndOfNextText(transform) {
@@ -100,8 +100,8 @@ export function collapseToEndOfNextText(transform) {
/** /**
* Move the selection to the end of the previous block. * Move the selection to the end of the previous block.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToEndOfPreviousBlock(transform) { export function collapseToEndOfPreviousBlock(transform) {
@@ -119,8 +119,8 @@ export function collapseToEndOfPreviousBlock(transform) {
/** /**
* Move the selection to the end of the previous text. * Move the selection to the end of the previous text.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToEndOfPreviousText(transform) { export function collapseToEndOfPreviousText(transform) {
@@ -136,9 +136,10 @@ export function collapseToEndOfPreviousText(transform) {
} }
/** /**
* Move to the start of a `node * Move to the start of a `node`.
* `. *
* @param {Transform} transform * @param {Transform} transform
* @param {Node} node
* @return {Transform} * @return {Transform}
*/ */
@@ -152,8 +153,8 @@ export function collapseToStartOf(transform, node) {
/** /**
* Move the selection to the start of the next block. * Move the selection to the start of the next block.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToStartOfNextBlock(transform) { export function collapseToStartOfNextBlock(transform) {
@@ -171,8 +172,8 @@ export function collapseToStartOfNextBlock(transform) {
/** /**
* Move the selection to the start of the next text. * Move the selection to the start of the next text.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToStartOfNextText(transform) { export function collapseToStartOfNextText(transform) {
@@ -190,8 +191,8 @@ export function collapseToStartOfNextText(transform) {
/** /**
* Move the selection to the start of the previous block. * Move the selection to the start of the previous block.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToStartOfPreviousBlock(transform) { export function collapseToStartOfPreviousBlock(transform) {
@@ -209,8 +210,8 @@ export function collapseToStartOfPreviousBlock(transform) {
/** /**
* Move the selection to the start of the previous text. * Move the selection to the start of the previous text.
* *
* @param {State} state * @param {Transform} tansform
* @return {State} * @return {Transform}
*/ */
export function collapseToStartOfPreviousText(transform) { export function collapseToStartOfPreviousText(transform) {
@@ -228,69 +229,61 @@ export function collapseToStartOfPreviousText(transform) {
/** /**
* Extend the focus point backward `n` characters. * Extend the focus point backward `n` characters.
* *
* @param {Transform} transform
* @param {Number} n (optional) * @param {Number} n (optional)
* @return {Transform} * @return {Transform}
*/ */
export function extendBackward(transform, ...args) { export function extendBackward(transform, n) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.extendBackward(...args) const sel = selection.extendBackward(n).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Extend the focus point forward `n` characters. * Extend the focus point forward `n` characters.
* *
* @param {Transform} transform
* @param {Number} n (optional) * @param {Number} n (optional)
* @return {Transform} * @return {Transform}
*/ */
export function extendForward(transform, ...args) { export function extendForward(transform, n) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.extendForward(...args) const sel = selection.extendForward(n).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Extend the focus point to the end of a `node`. * Extend the focus point to the end of a `node`.
* *
* @param {Transform} transform
* @param {Node} node * @param {Node} node
* @return {Transform} * @return {Transform}
*/ */
export function extendToEndOf(transform, ...args) { export function extendToEndOf(transform, node) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.extendToEndOf(...args) const sel = selection.extendToEndOf(node).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Extend the focus point to the start of a `node`. * Extend the focus point to the start of a `node`.
* *
* @param {Transform} transform
* @param {Node} node * @param {Node} node
* @return {Transform} * @return {Transform}
*/ */
export function extendToStartOf(transform, ...args) { export function extendToStartOf(transform, node) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.extendToStartOf(...args) const sel = selection.extendToStartOf(node).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
@@ -300,119 +293,89 @@ export function extendToStartOf(transform, ...args) {
* @return {Transform} * @return {Transform}
*/ */
export function focus(transform, ...args) { export function focus(transform) {
let { state } = transform const { state } = transform
let { document, selection } = state const { selection } = state
selection = selection.focus(...args) const sel = selection.focus()
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Move the selection backward `n` characters. * Move the selection backward `n` characters.
* *
* @param {Transform} transform
* @param {Number} n (optional) * @param {Number} n (optional)
* @return {Transform} * @return {Transform}
*/ */
export function moveBackward(transform, ...args) { export function moveBackward(transform, n) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.moveBackward(...args) const sel = selection.moveBackward(n).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Move the selection forward `n` characters. * Move the selection forward `n` characters.
* *
* @param {Transform} transform
* @param {Number} n (optional) * @param {Number} n (optional)
* @return {Transform} * @return {Transform}
*/ */
export function moveForward(transform, ...args) { export function moveForward(transform, n) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.moveForward(...args) const sel = selection.moveForward(n).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Move the selection to a specific anchor and focus point. * Move the selection to a specific anchor and focus point.
* *
* @param {State} state * @param {Transform} transform
* @param {Object} properties * @param {Object} properties
* @return {State} * @return {Transform}
*/ */
export function moveTo(transform, properties) { export function moveTo(transform, properties) {
let { state } = transform properties = Normalize.selection(properties)
let { document, selection } = state const { state } = transform
const { document, selection } = state
// Allow for passing a `Selection` object. const sel = selection.merge(properties).normalize(document)
if (properties instanceof Selection) { return transform.updateSelection(sel)
properties = {
anchorKey: properties.anchorKey,
anchorOffset: properties.anchorOffset,
focusKey: properties.focusKey,
focusOffset: properties.focusOffset,
isFocused: properties.isFocused
}
}
// Pass in properties, and force `isBackward` to be re-resolved.
selection = selection.merge({
...properties,
isBackward: null
})
selection = selection.normalize(document)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Move the selection to `anchor` and `focus` offsets. * Move the selection to `anchor` and `focus` offsets.
* *
* @param {Transform} transform
* @param {Number} anchor * @param {Number} anchor
* @param {Number} focus (optional) * @param {Number} focus (optional)
* @return {Transform} * @return {Transform}
*/ */
export function moveToOffsets(transform, ...args) { export function moveToOffsets(transform, anchor, fokus) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.moveToOffsets(...args) const sel = selection.moveToOffsets(anchor, fokus)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**
* Move to the entire range of `start` and `end` nodes. * Move to the entire range of `start` and `end` nodes.
* *
* @param {Transform} transform
* @param {Node} start * @param {Node} start
* @param {Node} end (optional) * @param {Node} end (optional)
* @return {Transform} * @return {Transform}
*/ */
export function moveToRangeOf(transform, ...args) { export function moveToRangeOf(transform, start, end) {
let { state } = transform const { state } = transform
let { document, selection } = state const { document, selection } = state
selection = selection.moveToRangeOf(...args) const sel = selection.moveToRangeOf(start, end).normalize(document)
selection = selection.normalize(document) return transform.updateSelection(sel)
state = state.merge({ selection })
transform.state = state
return transform
} }
/** /**