mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 19:01:54 +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.
|
// When at start of a node, merge backwards into the previous node.
|
||||||
const { startNode } = state
|
const { startNode } = state
|
||||||
if (state.isAtStartOf(startNode)) {
|
if (state.isAtStartOf(startNode)) {
|
||||||
|
const { selection, startOffset } = state
|
||||||
const parent = state.getParentNode(startNode)
|
const parent = state.getParentNode(startNode)
|
||||||
const previous = state.getPreviousNode(parent).nodes.first()
|
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)
|
state = state.removeRange(range)
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
@@ -314,36 +322,6 @@ class State extends StateRecord {
|
|||||||
// TODO
|
// 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.
|
* Remove characters from a node by `key` between offsets.
|
||||||
*
|
*
|
||||||
@@ -373,61 +351,53 @@ class State extends StateRecord {
|
|||||||
|
|
||||||
split() {
|
split() {
|
||||||
let state = this
|
let state = this
|
||||||
const { selection } = state
|
state = state.splitRange()
|
||||||
state = state.splitRange(selection)
|
|
||||||
const { anchorKey } = state.selection
|
const parent = state.getParentNode(state.startKey)
|
||||||
const parent = state.getParentNode(anchorKey)
|
|
||||||
const next = state.getNextNode(parent)
|
const next = state.getNextNode(parent)
|
||||||
const text = next.nodes.first()
|
const text = next.nodes.first()
|
||||||
return state.moveTo({
|
state = state.moveToStartOf(text)
|
||||||
anchorKey: text.key,
|
|
||||||
anchorOffset: 0,
|
return state
|
||||||
focusKey: text.key,
|
|
||||||
focusOffset: 0
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Split the nodes at a `selection`.
|
* Split the nodes at a `selection`.
|
||||||
*
|
*
|
||||||
* @param {Selection} selection
|
* @param {Selection} selection (optional)
|
||||||
* @return {State} state
|
* @return {State} state
|
||||||
*/
|
*/
|
||||||
|
|
||||||
splitRange(selection) {
|
splitRange(selection = this.selection) {
|
||||||
let state = this
|
let state = this
|
||||||
|
|
||||||
// if there's an existing selection, remove it first
|
// if there's an existing selection, remove it first
|
||||||
if (!selection.isCollapsed) {
|
if (!selection.isCollapsed) {
|
||||||
state = state.removeRange(selection)
|
state = state.removeRange(selection)
|
||||||
selection = selection.merge({
|
selection = selection.moveToStart()
|
||||||
focusKey: selection.anchorKey,
|
|
||||||
focusOffset: selection.anchorOffset
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// then split the node at the selection
|
// then split the node at the selection
|
||||||
const { startKey, startOffset } = selection
|
const { startNode, startOffset } = state
|
||||||
const text = state.getNode(startKey)
|
const parent = state.getParentNode(startNode)
|
||||||
const parent = state.getParentNode(text)
|
|
||||||
|
|
||||||
// split the characters
|
// split the characters
|
||||||
const { characters , length } = text
|
const { characters , length } = startNode
|
||||||
const firstCharacters = characters.take(startOffset)
|
const firstCharacters = characters.take(startOffset)
|
||||||
const secondCharacters = characters.takeLast(length - startOffset)
|
const secondCharacters = characters.takeLast(length - startOffset)
|
||||||
|
|
||||||
// Create a new first node with only the first set of characters.
|
// 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)
|
const firstNode = parent.updateNode(firstText)
|
||||||
|
|
||||||
// Create a brand new second node with the second set of characters.
|
// Create a brand new second node with the second set of characters.
|
||||||
let secondText = Text.create({})
|
let secondText = Text.create({})
|
||||||
secondText = secondText.set('characters', secondCharacters)
|
|
||||||
|
|
||||||
let secondNode = Node.create({
|
let secondNode = Node.create({
|
||||||
type: firstNode.type,
|
type: firstNode.type,
|
||||||
data: firstNode.data
|
data: firstNode.data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
secondText = secondText.set('characters', secondCharacters)
|
||||||
secondNode = secondNode.pushNode(secondText)
|
secondNode = secondNode.pushNode(secondText)
|
||||||
|
|
||||||
// Replace the old parent node in the grandparent with the two new ones.
|
// Replace the old parent node in the grandparent with the two new ones.
|
||||||
|
Reference in New Issue
Block a user