Repository: ianstormtaylor/slate
File: packages/slate-react/src/components/android/android-editable.tsx (TypeScript)

Code before:
  const [contentKey, setContentKey] = useState(0)
  const onRestoreDOM = useCallback(() => {
    setContentKey(prev => prev + 1)
  }, [contentKey])

  // Whenever the editor updates...
  useIsomorphicLayoutEffect(() => {
    // Update element-related weak maps with the DOM element ref.
    let window

    if (ref.current && (window = getDefaultView(ref.current))) {
      EDITOR_TO_WINDOW.set(editor, window)
      EDITOR_TO_ELEMENT.set(editor, ref.current)
      NODE_TO_ELEMENT.set(editor, ref.current)
      ELEMENT_TO_NODE.set(ref.current, editor)
      EDITOR_TO_RESTORE_DOM.set(editor, onRestoreDOM)
    } else {
      NODE_TO_ELEMENT.delete(editor)
      EDITOR_TO_RESTORE_DOM.delete(editor)
    }

    try {
      // Make sure the DOM selection state is in sync.
      const { selection } = editor
      const root = ReactEditor.findDocumentOrShadowRoot(editor)
      const domSelection = root ? root.getSelection() : null

      if (!domSelection || !ReactEditor.isFocused(editor)) {
        return
      }

      const hasDomSelection = domSelection.type !== 'None'

      // If the DOM selection is properly unset, we're done.
      if (!selection && !hasDomSelection) {
        return
      }

      // verify that the dom selection is in the editor
      const editorElement = EDITOR_TO_ELEMENT.get(editor)!
      let hasDomSelectionInEditor = false
      if (
        editorElement.contains(domSelection.anchorNode) &&
        editorElement.contains(domSelection.focusNode)
      ) {
        hasDomSelectionInEditor = true
      }

      // If the DOM selection is in the editor and the editor selection is already correct, we're done.
      if (hasDomSelection && hasDomSelectionInEditor && selection) {
        const slateRange = ReactEditor.toSlateRange(editor, domSelection, {

Review comment (suggestion):
```suggestion
      const domSelection = root?.getSelection() ?? null
```

Code after:
  const [contentKey, setContentKey] = useState(0)
  const onRestoreDOM = useCallback(() => {
    setContentKey(prev => prev + 1)
  }, [contentKey])

  // Whenever the editor updates...
  useIsomorphicLayoutEffect(() => {
    // Update element-related weak maps with the DOM element ref.
    let window

    if (ref.current && (window = getDefaultView(ref.current))) {
      EDITOR_TO_WINDOW.set(editor, window)
      EDITOR_TO_ELEMENT.set(editor, ref.current)
      NODE_TO_ELEMENT.set(editor, ref.current)
      ELEMENT_TO_NODE.set(ref.current, editor)
      EDITOR_TO_RESTORE_DOM.set(editor, onRestoreDOM)
    } else {
      NODE_TO_ELEMENT.delete(editor)
      EDITOR_TO_RESTORE_DOM.delete(editor)
    }

    try {
      // Make sure the DOM selection state is in sync.
      const { selection } = editor
      const root = ReactEditor.findDocumentOrShadowRoot(editor)
      const domSelection = root.getSelection()

      if (!domSelection || !ReactEditor.isFocused(editor)) {
        return
      }

      const hasDomSelection = domSelection.type !== 'None'

      // If the DOM selection is properly unset, we're done.
      if (!selection && !hasDomSelection) {
        return
      }

      // verify that the dom selection is in the editor
      const editorElement = EDITOR_TO_ELEMENT.get(editor)!
      let hasDomSelectionInEditor = false
      if (
        editorElement.contains(domSelection.anchorNode) &&
        editorElement.contains(domSelection.focusNode)
      ) {
        hasDomSelectionInEditor = true
      }

      // If the DOM selection is in the editor and the editor selection is already correct, we're done.
      if (hasDomSelection && hasDomSelectionInEditor && selection) {
        const slateRange = ReactEditor.toSlateRange(editor, domSelection, {