mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 19:52:32 +02:00
refactor
This commit is contained in:
@@ -125,12 +125,7 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
|
||||
|
||||
case 'remove_node': {
|
||||
const { path } = op
|
||||
const index = path[path.length - 1]
|
||||
const parent = Node.parent(editor, path)
|
||||
parent.children.splice(index, 1)
|
||||
|
||||
// Transform all of the points in the value, but if the point was in the
|
||||
// node that was removed we need to update the range or remove it.
|
||||
if (selection) {
|
||||
for (const [point, key] of Range.points(selection)) {
|
||||
const result = Point.transform(point, op)
|
||||
@@ -142,30 +137,30 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
|
||||
let next: NodeEntry<Text> | undefined
|
||||
|
||||
for (const [n, p] of Node.texts(editor)) {
|
||||
if (Path.compare(p, path) === -1) {
|
||||
const comparison = Path.compare(p, path)
|
||||
if (comparison === -1) {
|
||||
prev = [n, p]
|
||||
} else {
|
||||
} else if (comparison === 1) {
|
||||
next = [n, p]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
let preferNext = false
|
||||
let prevMoreInCommon = false
|
||||
if (prev && next) {
|
||||
if (Path.equals(next[1], path)) {
|
||||
preferNext = !Path.hasPrevious(next[1])
|
||||
} else {
|
||||
preferNext =
|
||||
Path.common(prev[1], path).length <
|
||||
Path.common(next[1], path).length
|
||||
}
|
||||
prevMoreInCommon =
|
||||
Path.common(prev[1], path).length >
|
||||
Path.common(next[1], path).length
|
||||
}
|
||||
|
||||
if (prev && !preferNext) {
|
||||
if (prev && prevMoreInCommon) {
|
||||
point.path = prev[1]
|
||||
point.offset = prev[0].text.length
|
||||
} else if (next) {
|
||||
point.path = next[1]
|
||||
const transformed = Path.transform(next[1], op)
|
||||
Path.isPath(transformed)
|
||||
|
||||
point.path = transformed
|
||||
point.offset = 0
|
||||
} else {
|
||||
selection = null
|
||||
@@ -174,6 +169,10 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
|
||||
}
|
||||
}
|
||||
|
||||
const index = path[path.length - 1]
|
||||
const parent = Node.parent(editor, path)
|
||||
parent.children.splice(index, 1)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user