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?)directionis'up','down','left', or'right';distanceis in user-space units,1by default.- The function returns
falsewhen 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 returnstruewhen 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]
)
)
}