useUndoBoundary

Returns a function that keeps the next edit out of the previous undo step, such as for making an AI-driven or automated change undoable separately from the user's typing around it.

const markUndoBoundary = useUndoBoundary()

markUndoBoundary()

Edits made within 500 ms of each other merge into a single undo step, even when they come from different hook calls (see useUndoRedo). A boundary ends the current step, so the next edit starts a new one.

Behavior

  • Call it before an operation, and again after all its edits finish, so the operation doesn't merge with the edits before or after it.
  • A boundary doesn't guarantee that the operation becomes a single undo step: pauses between its edits, or changes on different pages, can split it into several.
  • The call itself doesn't create an empty undo step, doesn't change the document revision, and keeps the redo history.
  • Throws DocumentNotLoadedError when no document is loaded.

Example

import { useProgrammaticEditor, useUndoBoundary } from '@sdk-pdf/react'

function useReplaceParagraphText() {
  const markUndoBoundary = useUndoBoundary()
  const { getParagraphEditor } = useProgrammaticEditor()

  return (paragraphId: string, text: string) => {
    const editor = getParagraphEditor(paragraphId)
    if (!editor) return

    markUndoBoundary()
    try {
      editor.selectAll()
      editor.insertText(text)
    } finally {
      markUndoBoundary()
    }
  }
}

On this page