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: focus loss inside Shadow DOM in Firefox. #5676

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/clean-rats-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': minor
---

Changed behaviour of ReactEditor.findDocumentOrShadowRoot. It returns shadow root or document without checking for the existence of the getSelection method.
7 changes: 4 additions & 3 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
DOMText,
getActiveElement,
getDefaultView,
getSelection,
isDOMElement,
isDOMNode,
isPlainTextOnlyPaste,
Expand Down Expand Up @@ -231,7 +232,7 @@ export const Editable = (props: EditableProps) => {
const root = ReactEditor.findDocumentOrShadowRoot(editor)
const { activeElement } = root
const el = ReactEditor.toDOMNode(editor, editor)
const domSelection = root.getSelection()
const domSelection = getSelection(root)

if (activeElement === el) {
state.latestElement = activeElement
Expand Down Expand Up @@ -308,7 +309,7 @@ export const Editable = (props: EditableProps) => {
// Make sure the DOM selection state is in sync.
const { selection } = editor
const root = ReactEditor.findDocumentOrShadowRoot(editor)
const domSelection = root.getSelection()
const domSelection = getSelection(root)

if (
!domSelection ||
Expand Down Expand Up @@ -1090,7 +1091,7 @@ export const Editable = (props: EditableProps) => {
// editable element no longer has focus. Refer to:
// https://stackoverflow.com/questions/12353247/force-contenteditable-div-to-stop-accepting-input-after-it-loses-focus-under-web
if (IS_WEBKIT) {
const domSelection = root.getSelection()
const domSelection = getSelection(root)
domSelection?.removeAllRanges()
}

Expand Down
10 changes: 4 additions & 6 deletions packages/slate-react/src/plugin/react-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DOMSelection,
DOMStaticRange,
DOMText,
getSelection,
hasShadowRoot,
isDOMElement,
isDOMNode,
Expand Down Expand Up @@ -280,7 +281,7 @@ export const ReactEditor: ReactEditorInterface = {
deselect: editor => {
const { selection } = editor
const root = ReactEditor.findDocumentOrShadowRoot(editor)
const domSelection = root.getSelection()
const domSelection = getSelection(root)

if (domSelection && domSelection.rangeCount > 0) {
domSelection.removeAllRanges()
Expand All @@ -295,10 +296,7 @@ export const ReactEditor: ReactEditorInterface = {
const el = ReactEditor.toDOMNode(editor, editor)
const root = el.getRootNode()

if (
(root instanceof Document || root instanceof ShadowRoot) &&
root.getSelection != null
) {
if (root instanceof Document || root instanceof ShadowRoot) {
return root
}

Expand Down Expand Up @@ -437,7 +435,7 @@ export const ReactEditor: ReactEditorInterface = {
if (root.activeElement !== el) {
// Ensure that the DOM selection state is set to the editor's selection
if (editor.selection && root instanceof Document) {
const domSelection = root.getSelection()
const domSelection = getSelection(root)
const domRange = ReactEditor.toDOMRange(editor, editor.selection)
domSelection?.removeAllRanges()
domSelection?.addRange(domRange)
Expand Down
10 changes: 10 additions & 0 deletions packages/slate-react/src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ export const getClipboardData = (
return dataTransfer
}

/**
* Get the dom selection from Shadow Root if possible, otherwise from the document
*/
export const getSelection = (root: Document | ShadowRoot): Selection | null => {
if (root.getSelection != null) {
return root.getSelection()
}
return document.getSelection()
}

/**
* Check whether a mutation originates from a editable element inside the editor.
*/
Expand Down
Loading