Repository: ianstormtaylor/slate
File: packages/slate-react/src/plugin/react-editor.ts (TypeScript)

Code before:
  },

  /**
   * Find the DOM node that implements DocumentOrShadowRoot for the editor.
   */

  findDocumentOrShadowRoot(editor: ReactEditor): Document | ShadowRoot {
    const el = ReactEditor.toDOMNode(editor, editor)
    const root = el.getRootNode()

    if (!(root instanceof Document || root instanceof ShadowRoot))
      throw new Error(
        `Unable to find DocumentOrShadowRoot for editor element: ${el}`
      )

    // COMPAT: Only Chrome implements the DocumentOrShadowRoot mixin for
    // ShadowRoot; other browsers still implement it on the Document
    // interface. (2020/08/08)
    // https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot#Properties
    if (root.getSelection === undefined && el.ownerDocument !== null)
      return el.ownerDocument

    return root
  },

  /**
   * Check if the editor is focused.
   */

  isFocused(editor: ReactEditor): boolean {
    return !!IS_FOCUSED.get(editor)
  },

  /**
   * Check if the editor is in read-only mode.
   */

  isReadOnly(editor: ReactEditor): boolean {
    return !!IS_READ_ONLY.get(editor)
  },

  /**
   * Blur the editor.
   */

  blur(editor: ReactEditor): void {
    const el = ReactEditor.toDOMNode(editor, editor)
    const root = ReactEditor.findDocumentOrShadowRoot(editor)
    IS_FOCUSED.set(editor, false)

    if (root.activeElement === el) {

Review comment (suggestion):
Noticed an interesting issue experimenting with this branch myself: The `selectionchange` event fires as the editor unmounts (we have a drawer component that contains the editor -- closing the drawer causes `selectionchange` to fire). When this happens, the `selectionchange` event fires and `findDocumentOrShadowRoot` is called. This error is hit like so:
```
// ... selectionchange event calls this function ...
const el = ReactEditor.toDOMNode(editor, editor) // el is the editor, which isn't currently in the DOM
const root = el.getRootNode() // root is another element that is not in the DOM or the shadow root
// error throws because root is not Document or ShadowRoot
```

Any ideas? No worries if not -- this could definitely be an issue that's unrelated to Slate.

Code after:
      const i = NODE_TO_INDEX.get(child)

      if (i == null) {
        break
      }

      path.unshift(i)
      child = parent
    }

    throw new Error(
      `Unable to find the path for Slate node: ${JSON.stringify(node)}`
    )
  },

  /**
   * Find the DOM node that implements DocumentOrShadowRoot for the editor.
   */

  findDocumentOrShadowRoot(editor: ReactEditor): Document | ShadowRoot {
    const el = ReactEditor.toDOMNode(editor, editor)
    const root = el.getRootNode()

    if (!(root instanceof Document || root instanceof ShadowRoot))
      throw new Error(
        `Unable to find DocumentOrShadowRoot for editor element: ${el}`
      )

    // COMPAT: Only Chrome implements the DocumentOrShadowRoot mixin for
    // ShadowRoot; other browsers still implement it on the Document
    // interface. (2020/08/08)
    // https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot#Properties
    if (root.getSelection === undefined && el.ownerDocument !== null)
      return el.ownerDocument

    return root
  },

  /**
   * Check if the editor is focused.
   */

  isFocused(editor: ReactEditor): boolean {
    return !!IS_FOCUSED.get(editor)
  },

  /**
   * Check if the editor is in read-only mode.
   */

  isReadOnly(editor: ReactEditor): boolean {