1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-08 08:16:30 +02:00

fix double insert in anchor element decorations (#4951)

This commit is contained in:
Eric Meier
2022-04-19 16:02:23 -04:00
committed by GitHub
parent 9ce0a08c2a
commit 5b51e87d51
2 changed files with 18 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': patch
---
Fix double insert in anchor element decorations

View File

@@ -42,6 +42,7 @@ import {
isDOMElement, isDOMElement,
isDOMNode, isDOMNode,
isPlainTextOnlyPaste, isPlainTextOnlyPaste,
DOMText,
} from '../utils/dom' } from '../utils/dom'
import { import {
@@ -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 // 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. // Therefore we don't allow native events to insert text at the end of anchor nodes.
const { anchor } = selection 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, offset] = ReactEditor.toDOMPoint(editor, anchor)
const node = ReactEditor.toSlateNode(editor, anchorNode) const anchorNode = node.parentElement?.closest('a')
const path = ReactEditor.findPath(editor, node)
if (Editor.isEnd(editor, anchor, path)) { if (anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
native = false 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
} }
} }
} }