Repository: ianstormtaylor/slate
File: packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts (TypeScript)

Code before:
    // COMPAT: SelectionChange event is fired after the action is performed, so we
    // have to manually get the selection here to ensure it's up-to-date.
    const window = ReactEditor.getWindow(editor)
    const domSelection = window.getSelection()
    if (!targetRange && domSelection) {
      nativeTargetRange = domSelection
      targetRange = ReactEditor.toSlateRange(editor, domSelection, {
        exactMatch: false,
        suppressThrow: true,
      })
    }

    targetRange = targetRange ?? editor.selection
    if (!targetRange) {
      return
    }

    // By default, the input manager tries to store text diffs so that we can
    // defer flushing them at a later point in time. We don't want to flush
    // for every input event as this can be expensive. However, there are some
    // scenarios where we cannot safely store the text diff and must instead
    // schedule an action to let Slate normalize the editor state.
    let canStoreDiff = true

    if (type.startsWith('delete')) {
      if (Range.isExpanded(targetRange)) {
        const [start, end] = Range.edges(targetRange)
        const leaf = Node.leaf(editor, start.path)

        if (leaf.text.length === start.offset && end.offset === 0) {
          const next = Editor.next(editor, {
            at: start.path,
            match: Text.isText,
            voids: true,
          })
          if (next && Path.equals(next[1], end.path)) {
            targetRange = { anchor: end, focus: end }
          }
        }
      }

      const direction = type.endsWith('Backward') ? 'backward' : 'forward'
      const [start, end] = Range.edges(targetRange)
      const [leaf, path] = Editor.leaf(editor, start.path)

      const diff = {
        text: '',
        start: start.offset,
        end: end.offset,
      }
      const pendingDiffs = EDITOR_TO_PENDING_DIFFS.get(editor)

Review comment (suggestion):
Just re-structuring the code paths here to avoid checking `type.startsWith('delete')` multiple times

Code after:
    // COMPAT: SelectionChange event is fired after the action is performed, so we
    // have to manually get the selection here to ensure it's up-to-date.
    const window = ReactEditor.getWindow(editor)
    const domSelection = window.getSelection()
    if (!targetRange && domSelection) {
      nativeTargetRange = domSelection
      targetRange = ReactEditor.toSlateRange(editor, domSelection, {
        exactMatch: false,
        suppressThrow: true,
      })
    }

    targetRange = targetRange ?? editor.selection
    if (!targetRange) {
      return
    }

    // By default, the input manager tries to store text diffs so that we can
    // defer flushing them at a later point in time. We don't want to flush
    // for every input event as this can be expensive. However, there are some
    // scenarios where we cannot safely store the text diff and must instead
    // schedule an action to let Slate normalize the editor state.
    let canStoreDiff = true

    if (type.startsWith('delete')) {
      if (Range.isExpanded(targetRange)) {
        const [start, end] = Range.edges(targetRange)
        const leaf = Node.leaf(editor, start.path)

        if (leaf.text.length === start.offset && end.offset === 0) {
          const next = Editor.next(editor, {
            at: start.path,
            match: Text.isText,
          })
          if (next && Path.equals(next[1], end.path)) {
            targetRange = { anchor: end, focus: end }
          }
        }
      }

      const direction = type.endsWith('Backward') ? 'backward' : 'forward'
      const [start, end] = Range.edges(targetRange)
      const [leaf, path] = Editor.leaf(editor, start.path)

      const diff = {
        text: '',
        start: start.offset,
        end: end.offset,
      }
      const pendingDiffs = EDITOR_TO_PENDING_DIFFS.get(editor)
      const relevantPendingDiffs = pendingDiffs?.find(change =>