useDeleteSelection

Returns a function that deletes whatever is selected: the selected text range in a text selection, or the selected blocks in a block selection. The usual target of the Delete and Backspace hotkeys.

const deleteSelection = useDeleteSelection()

deleteSelection()
  • With nothing selected, the call is a no-op.
  • With no document loaded, it throws DocumentNotLoadedError.
  • The selection itself is kept: if the deleted blocks are restored by an undo, they come back selected.

Example

import { useMemo } from 'react'
import { createUnfocusedHandler, useDeleteSelection, useHotkeys } from '@sdk-pdf/react'

function useDeleteHotkeys() {
  const deleteSelection = useDeleteSelection()

  useHotkeys(
    useMemo(
      // The wrapper keeps the keys deleting characters while the user edits text or fills
      // a form field.
      () => ({
        Backspace: createUnfocusedHandler(deleteSelection),
        Delete: createUnfocusedHandler(deleteSelection),
      }),
      [deleteSelection]
    )
  )
}

To delete blocks by id instead of by selection, see useBlockDelete.

On this page