Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Aug 10, 2021
1 parent fa54145 commit 45a8aab
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions packages/slate/src/transforms/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 45a8aab

Please sign in to comment.