Skip to content

Commit

Permalink
Prevent native insert at the end of anchors (#4948)
Browse files Browse the repository at this point in the history
* Prevent native insert at the end of anchors

* add changeset
  • Loading branch information
BitPhinix authored Apr 18, 2022
1 parent 5160efe commit 9957c21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-mangos-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Prevent native insert at the end of anchor elements
23 changes: 12 additions & 11 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,20 @@ export const Editable = (props: EditableProps) => {
native = false
}

// Chrome also has issues correctly editing the end of nodes: https://bugs.chromium.org/p/chromium/issues/detail?id=1259100
// Therefore we don't allow native events to insert text at the end of nodes.
// Chrome also has issues correctly editing the end of anchor elements: https://bugs.chromium.org/p/chromium/issues/detail?id=1259100
// Therefore we don't allow native events to insert text at the end of anchor nodes.
const { anchor } = selection
const inline = Editor.above(editor, {
at: anchor,
match: n => Editor.isInline(editor, n),
mode: 'highest',
})
if (inline) {
const [, inlinePath] = inline
if (Editor.isEnd(editor, anchor, anchor.path)) {
const [node] = ReactEditor.toDOMPoint(editor, selection.anchor)
const anchorNode = node.parentElement?.closest('a')

if (anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
const node = ReactEditor.toSlateNode(editor, anchorNode)
const path = ReactEditor.findPath(editor, node)

if (Editor.isEnd(editor, selection.anchor, inlinePath)) {
native = false
if (Editor.isEnd(editor, anchor, path)) {
native = false
}
}
}
}
Expand Down

0 comments on commit 9957c21

Please sign in to comment.