mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 10:29:48 +02:00
fixing things
This commit is contained in:
@@ -263,9 +263,17 @@ class State extends StateRecord {
|
||||
// When at start of a node, merge backwards into the previous node.
|
||||
const { startNode } = state
|
||||
if (state.isAtStartOf(startNode)) {
|
||||
const { selection, startOffset } = state
|
||||
const parent = state.getParentNode(startNode)
|
||||
const previous = state.getPreviousNode(parent).nodes.first()
|
||||
const range = selection.moveToEndOf(previous)
|
||||
const range = selection.merge({
|
||||
anchorKey: previous.key,
|
||||
anchorOffset: previous.length,
|
||||
focusKey: startNode.key,
|
||||
focusOffset: 0,
|
||||
isBackward: false
|
||||
})
|
||||
|
||||
state = state.removeRange(range)
|
||||
return state
|
||||
}
|
||||
@@ -314,36 +322,6 @@ class State extends StateRecord {
|
||||
// TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the existing selection's content.
|
||||
*
|
||||
* @param {Selection} selection
|
||||
* @return {State} state
|
||||
*/
|
||||
|
||||
removeSelection(selection) {
|
||||
// if already collapsed, there's nothing to remove
|
||||
if (selection.isCollapsed) return this
|
||||
|
||||
// if the start and end nodes are the same, just remove the matching text
|
||||
const { startKey, startOffset, endKey, endOffset } = selection
|
||||
if (startKey == endKey) return this.removeCharacters(startKey, startOffset, endOffset)
|
||||
|
||||
// otherwise, remove all of the other nodes between them...
|
||||
const nodes = this.nodes
|
||||
.takeUntil(node => node.key == startKey)
|
||||
.take(1)
|
||||
.skipUntil(node => node.key == endKey)
|
||||
.take(Infinity)
|
||||
|
||||
// ...and remove the text from the first and last nodes
|
||||
const startNode = this.getNode(startKey)
|
||||
return this
|
||||
.merge({ nodes })
|
||||
.removeCharacters(startKey, startOffset, startNode.text.length)
|
||||
.removeCharacters(endKey, 0, endOffset)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove characters from a node by `key` between offsets.
|
||||
*
|
||||
@@ -373,61 +351,53 @@ class State extends StateRecord {
|
||||
|
||||
split() {
|
||||
let state = this
|
||||
const { selection } = state
|
||||
state = state.splitRange(selection)
|
||||
const { anchorKey } = state.selection
|
||||
const parent = state.getParentNode(anchorKey)
|
||||
state = state.splitRange()
|
||||
|
||||
const parent = state.getParentNode(state.startKey)
|
||||
const next = state.getNextNode(parent)
|
||||
const text = next.nodes.first()
|
||||
return state.moveTo({
|
||||
anchorKey: text.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: text.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
state = state.moveToStartOf(text)
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the nodes at a `selection`.
|
||||
*
|
||||
* @param {Selection} selection
|
||||
* @param {Selection} selection (optional)
|
||||
* @return {State} state
|
||||
*/
|
||||
|
||||
splitRange(selection) {
|
||||
splitRange(selection = this.selection) {
|
||||
let state = this
|
||||
|
||||
// if there's an existing selection, remove it first
|
||||
if (!selection.isCollapsed) {
|
||||
state = state.removeRange(selection)
|
||||
selection = selection.merge({
|
||||
focusKey: selection.anchorKey,
|
||||
focusOffset: selection.anchorOffset
|
||||
})
|
||||
selection = selection.moveToStart()
|
||||
}
|
||||
|
||||
// then split the node at the selection
|
||||
const { startKey, startOffset } = selection
|
||||
const text = state.getNode(startKey)
|
||||
const parent = state.getParentNode(text)
|
||||
const { startNode, startOffset } = state
|
||||
const parent = state.getParentNode(startNode)
|
||||
|
||||
// split the characters
|
||||
const { characters , length } = text
|
||||
const { characters , length } = startNode
|
||||
const firstCharacters = characters.take(startOffset)
|
||||
const secondCharacters = characters.takeLast(length - startOffset)
|
||||
|
||||
// Create a new first node with only the first set of characters.
|
||||
const firstText = text.set('characters', firstCharacters)
|
||||
const firstText = startNode.set('characters', firstCharacters)
|
||||
const firstNode = parent.updateNode(firstText)
|
||||
|
||||
// Create a brand new second node with the second set of characters.
|
||||
let secondText = Text.create({})
|
||||
secondText = secondText.set('characters', secondCharacters)
|
||||
|
||||
let secondNode = Node.create({
|
||||
type: firstNode.type,
|
||||
data: firstNode.data
|
||||
})
|
||||
|
||||
secondText = secondText.set('characters', secondCharacters)
|
||||
secondNode = secondNode.pushNode(secondText)
|
||||
|
||||
// Replace the old parent node in the grandparent with the two new ones.
|
||||
|
Reference in New Issue
Block a user