Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix double insert in anchor element decorations #4951

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-students-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fix double insert in anchor element decorations
22 changes: 13 additions & 9 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
isDOMElement,
isDOMNode,
isPlainTextOnlyPaste,
DOMText,
} from '../utils/dom'

import {
Expand Down Expand Up @@ -371,17 +372,20 @@ export const Editable = (props: EditableProps) => {
// 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
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)
const [node, offset] = ReactEditor.toDOMPoint(editor, anchor)
const anchorNode = node.parentElement?.closest('a')

if (Editor.isEnd(editor, anchor, path)) {
native = false
}
if (anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
const { document } = ReactEditor.getWindow(editor)

// Find the last text node inside the anchor.
const lastText = document
.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT)
.lastChild() as DOMText | null

if (lastText === node && lastText.textContent?.length === offset) {
native = false
}
}
}
Expand Down