mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 23:12:52 +02:00
Don't native insert in elements with white-space="pre" containing tab chars (#5045)
* don't native insert in elements with white-space="pre" containing tab chars * apply suggestions from code review
This commit is contained in:
5
.changeset/hot-seahorses-smile.md
Normal file
5
.changeset/hot-seahorses-smile.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Don't native insert inside blocks with whitespace="pre" containing tab chars to work around https://bugs.chromium.org/p/chromium/issues/detail?id=1219139
|
@@ -380,11 +380,15 @@ export const Editable = (props: EditableProps) => {
|
|||||||
const [node, offset] = ReactEditor.toDOMPoint(editor, anchor)
|
const [node, offset] = ReactEditor.toDOMPoint(editor, anchor)
|
||||||
const anchorNode = node.parentElement?.closest('a')
|
const anchorNode = node.parentElement?.closest('a')
|
||||||
|
|
||||||
if (anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
|
const window = ReactEditor.getWindow(editor)
|
||||||
const { document } = ReactEditor.getWindow(editor)
|
|
||||||
|
|
||||||
|
if (
|
||||||
|
native &&
|
||||||
|
anchorNode &&
|
||||||
|
ReactEditor.hasDOMNode(editor, anchorNode)
|
||||||
|
) {
|
||||||
// Find the last text node inside the anchor.
|
// Find the last text node inside the anchor.
|
||||||
const lastText = document
|
const lastText = window?.document
|
||||||
.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT)
|
.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT)
|
||||||
.lastChild() as DOMText | null
|
.lastChild() as DOMText | null
|
||||||
|
|
||||||
@@ -392,6 +396,23 @@ export const Editable = (props: EditableProps) => {
|
|||||||
native = false
|
native = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Chrome has issues with the presence of tab characters inside elements with whiteSpace = 'pre'
|
||||||
|
// causing abnormal insert behavior: https://bugs.chromium.org/p/chromium/issues/detail?id=1219139
|
||||||
|
if (
|
||||||
|
native &&
|
||||||
|
node.parentElement &&
|
||||||
|
window?.getComputedStyle(node.parentElement)?.whiteSpace === 'pre'
|
||||||
|
) {
|
||||||
|
const block = Editor.above(editor, {
|
||||||
|
at: anchor.path,
|
||||||
|
match: n => Editor.isBlock(editor, n),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (block && Node.string(block[0]).includes('\t')) {
|
||||||
|
native = false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// COMPAT: For the deleting forward/backward input types we don't want
|
// COMPAT: For the deleting forward/backward input types we don't want
|
||||||
|
Reference in New Issue
Block a user