mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-24 09:13:24 +01:00
update paths in selection operations
This commit is contained in:
parent
68e8be229d
commit
983d4d3b27
@ -26,6 +26,7 @@ const OPERATIONS = {
|
||||
set_mark: setMark,
|
||||
// Node operations.
|
||||
insert_node: insertNode,
|
||||
join_node: joinNode,
|
||||
move_node: moveNode,
|
||||
remove_node: removeNode,
|
||||
set_node: setNode,
|
||||
@ -114,6 +115,36 @@ function insertText(state, operation) {
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a node by `path` with a node `withPath`.
|
||||
*
|
||||
* @param {State} state
|
||||
* @param {Object} operation
|
||||
* @return {State}
|
||||
*/
|
||||
|
||||
function joinNode(state, operation) {
|
||||
const { path, withPath } = operation
|
||||
let { document } = state
|
||||
let target = document.assertPath(path)
|
||||
let node = document.assertPath(withPath)
|
||||
let parent = document.getParent(target)
|
||||
const isParent = document == parent
|
||||
const index = parent.nodes.indexOf(target)
|
||||
const size = node.nodes.size
|
||||
|
||||
target.nodes.forEach((child, i) => {
|
||||
node = node.insertNode(size + i, child)
|
||||
})
|
||||
|
||||
parent = parent.removeNode(index)
|
||||
document = isParent ? parent : document.updateDescendant(parent)
|
||||
document = document.updateDescendant(node)
|
||||
document = document.normalize()
|
||||
state = state.merge({ document })
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a node by `path` to a new parent by `path` and `index`.
|
||||
*
|
||||
@ -247,8 +278,21 @@ function setNode(state, operation) {
|
||||
*/
|
||||
|
||||
function setSelection(state, operation) {
|
||||
let { properties } = operation
|
||||
let { selection } = state
|
||||
let properties = { ...operation.properties }
|
||||
let { document, selection } = state
|
||||
|
||||
if (properties.anchorPath) {
|
||||
const anchorNode = document.assertPath(properties.anchorPath)
|
||||
properties.anchorKey = anchorNode.key
|
||||
delete properties.anchorPath
|
||||
}
|
||||
|
||||
if (properties.focusPath) {
|
||||
const focusNode = document.assertPath(properties.focusPath)
|
||||
properties.focusKey = focusNode.key
|
||||
delete properties.focusPath
|
||||
}
|
||||
|
||||
selection = selection.merge(properties)
|
||||
state = state.merge({ selection })
|
||||
return state
|
||||
|
@ -94,6 +94,37 @@ export function insertTextOperation(transform, path, offset, text, marks) {
|
||||
return transform.applyOperation(operation)
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a node by `path` with a node `withPath`.
|
||||
*
|
||||
* @param {Transform} transform
|
||||
* @param {Array} path
|
||||
* @param {Array} withPath
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
export function joinNodeOperation(transform, path, withPath) {
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
const node = document.assertPath(path)
|
||||
const offset = node.length
|
||||
|
||||
const inverse = [{
|
||||
type: 'split_node',
|
||||
path: withPath,
|
||||
offset,
|
||||
}]
|
||||
|
||||
const operation = {
|
||||
type: 'join_node',
|
||||
path,
|
||||
withPath,
|
||||
inverse,
|
||||
}
|
||||
|
||||
return transform.applyOperation(operation)
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a node by `path` to a `newPath` and `newIndex`.
|
||||
*
|
||||
@ -327,7 +358,7 @@ export function setSelectionOperation(transform, properties) {
|
||||
properties = Normalize.selectionProperties(properties)
|
||||
|
||||
const { state } = transform
|
||||
const { selection } = state
|
||||
const { document, selection } = state
|
||||
const prevProps = {}
|
||||
|
||||
if (properties.marks == selection.marks) {
|
||||
@ -338,6 +369,20 @@ export function setSelectionOperation(transform, properties) {
|
||||
prevProps[k] = selection[k]
|
||||
}
|
||||
|
||||
if (properties.anchorKey) {
|
||||
properties.anchorPath = document.getPath(properties.anchorKey)
|
||||
prevProps.anchorPath = document.getPath(prevProps.anchorKey)
|
||||
delete properties.anchorKey
|
||||
delete prevProps.anchorKey
|
||||
}
|
||||
|
||||
if (properties.focusKey) {
|
||||
properties.focusPath = document.getPath(properties.focusKey)
|
||||
prevProps.focusPath = document.getPath(prevProps.focusKey)
|
||||
delete properties.focusKey
|
||||
delete prevProps.focusKey
|
||||
}
|
||||
|
||||
const inverse = [{
|
||||
type: 'set_selection',
|
||||
properties: prevProps
|
||||
@ -362,7 +407,13 @@ export function setSelectionOperation(transform, properties) {
|
||||
*/
|
||||
|
||||
export function splitNodeOperation(transform, path, offset) {
|
||||
const inverse = []
|
||||
const inverseIndex = path[path.length - 1] + 1
|
||||
const inversePath = path.slice(0, -1).concat([inverseIndex])
|
||||
const inverse = [{
|
||||
type: 'join_node',
|
||||
path: inversePath,
|
||||
withPath: path,
|
||||
}]
|
||||
|
||||
const operation = {
|
||||
type: 'split_node',
|
||||
|
Loading…
x
Reference in New Issue
Block a user