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

fix: toSlatePoint suppressThrow leaf without text node (#4813)

This commit is contained in:
Eric Meier
2022-01-28 02:09:05 +01:00
committed by GitHub
parent e998752989
commit a5fd62ddd6
2 changed files with 32 additions and 24 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': patch
---
Don't throw in toSlatePoint while using supressThrow if leaf has no text node

View File

@@ -491,33 +491,36 @@ export const ReactEditor = {
// Calculate how far into the text node the `nearestNode` is, so that we // Calculate how far into the text node the `nearestNode` is, so that we
// can determine what the offset relative to the text node is. // can determine what the offset relative to the text node is.
if (leafNode) { if (leafNode) {
textNode = leafNode.closest('[data-slate-node="text"]')! textNode = leafNode.closest('[data-slate-node="text"]')
const window = ReactEditor.getWindow(editor)
const range = window.document.createRange()
range.setStart(textNode, 0)
range.setEnd(nearestNode, nearestOffset)
const contents = range.cloneContents() if (textNode) {
const removals = [ const window = ReactEditor.getWindow(editor)
...Array.prototype.slice.call( const range = window.document.createRange()
contents.querySelectorAll('[data-slate-zero-width]') range.setStart(textNode, 0)
), range.setEnd(nearestNode, nearestOffset)
...Array.prototype.slice.call(
contents.querySelectorAll('[contenteditable=false]')
),
]
removals.forEach(el => { const contents = range.cloneContents()
el!.parentNode!.removeChild(el) const removals = [
}) ...Array.prototype.slice.call(
contents.querySelectorAll('[data-slate-zero-width]')
),
...Array.prototype.slice.call(
contents.querySelectorAll('[contenteditable=false]')
),
]
// COMPAT: Edge has a bug where Range.prototype.toString() will removals.forEach(el => {
// convert \n into \r\n. The bug causes a loop when slate-react el!.parentNode!.removeChild(el)
// attempts to reposition its cursor to match the native position. Use })
// textContent.length instead.
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10291116/ // COMPAT: Edge has a bug where Range.prototype.toString() will
offset = contents.textContent!.length // convert \n into \r\n. The bug causes a loop when slate-react
domNode = textNode // attempts to reposition its cursor to match the native position. Use
// textContent.length instead.
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10291116/
offset = contents.textContent!.length
domNode = textNode
}
} else if (voidNode) { } else if (voidNode) {
// For void nodes, the element with the offset key will be a cousin, not an // For void nodes, the element with the offset key will be a cousin, not an
// ancestor, so find it by going down from the nearest void parent. // ancestor, so find it by going down from the nearest void parent.