useMoveSelection

Returns a function that moves the selected blocks by a distance in a direction, such as for nudging the selection with the arrow keys.

const moveSelection = useMoveSelection()

const isHandled = moveSelection(direction, distance?)
  • direction is 'up', 'down', 'left', or 'right'; distance is in user-space units, 1 by default.
  • The function returns false when the command is inapplicable (the selection is not a block selection), so a hotkey handler can hand the arrow keys back to the browser; it returns true when the move was applied.
  • With no document loaded, it throws DocumentNotLoadedError.

Example

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

function useArrowNudgeHotkeys() {
  const moveSelection = useMoveSelection()

  useHotkeys(
    useMemo(
      () => ({
        // Returning false lets the browser scroll when no blocks are selected, and the
        // wrapper keeps the arrow keys moving the caret while the user edits text or
        // fills a form field.
        ArrowUp: createUnfocusedHandler(() => moveSelection('up')),
        ArrowDown: createUnfocusedHandler(() => moveSelection('down')),
        ArrowLeft: createUnfocusedHandler(() => moveSelection('left')),
        ArrowRight: createUnfocusedHandler(() => moveSelection('right')),
      }),
      [moveSelection]
    )
  )
}

On this page