Namespace: history

History API.

The History API includes methods to undo and redo annotation operations: creation, updates and deletions may be reverted and restored by means of this API.

The implementation does not fully revert an annotation to its previous state:

  • The updatedAt field will have changed to the current time.
  • If an annotation deletion is undone, the restored annotation will have a different id than the original.
  • If an annotation deletion is undone, the restored annotation will appear at the front, regardless of its original stacking position.
  • Annotation changes that only affect the updatedAt property are not tracked, and the updated annotation is considered identical to the previous one in this case.
  • Newly created empty text annotations are not recorded in the history. This ensures that accidental creation of such annotations, followed by pressing escape or clicking outside, will not persist in the undo and redo history.

The feature only accounts for annotations modified locally, wether using the API or the toolbar Undo and Redo buttons. If an annotation is modified externally, by another Instant client, for example, undoing will not revert the annotation state to the one just before the external change, but to the previous to that one: external annotation operations are not undone, but overridden.

Annotation operations performed while the History API is disabled can also be considered external for that effect. This is also the case for annotation operations that result from Instant Comments changes, like deleting the last comment of a comment thread, which results on the comment marker being deleted, and which cannot therefore be undone.

However, comment markers directly deleted with the API may be restored with its former comments.

Annotation presets are not restored by undo and redo operations.

Methods




Methods

canRedo() → {boolean}

Returns true if it's possible to redo a previously undone operation, false otherwise, also if the History API is disabled.

Returns:

Wether it's possible to redo a previously undone operation.

Type
boolean

canUndo() → {boolean}

Returns true if it's possible to undo a previous operation, false otherwise, also if the History API is disabled.

Returns:

Wether it's possible to undo a previous operation.

Type
boolean

clear()

Removes all undoable and redoable operations available.

Returns:

Clear undo and redo history.

disable()

Disables the History API: attempting to undo or redo previous operations with the API or the UI will not be possible, but the previous undoable and redoable operations will be preserved, and available if the History API is enabled again with PSPDFKit.Instance.history.enable.

Returns:

Disable tracking undo and redo history.

enable()

Enables the History API, making undoing and redoing possible. If there were previous undoable or redoable operations, they will be now available.

Returns:

Enable tracking undo and redo history.

redo() → {Promise.<boolean>}

When called, the last undone annotation operation will be performed again.

Note that if an annotation deletion has been undone, and then redone by calling this function, it will reappear in front of any other annotations, even if that was not its original stacking order.

Returns true if the operation has been redone successfully, false if there are no redoable operations available or the History API is disabled.

Returns:

The result of the redo operation.

Type
Promise.<boolean>
Example
await instance.create(new PSPDFKit.Annotations.RectangleAnnotation({
  pageIndex: 0,
  boundingBox: new PSPDFKit.Geometry.Rect({
    left: 200,
    top: 150,
    width: 250,
    height: 75
  })
}));
console.log("Annotation created!");
await instance.delete();
console.log("Annotation deleted!");
await instance.history.undo();
console.log("Annotation creation undone: annotation deleted!");
await instance.history.redo();
console.log("Annotation creation redone: annotation created!");

undo() → {Promise.<boolean>}

When called, the last local annotation operation will be reverted. The outcome will vary depending on the type of that operation:

  • Annotation creation: the annotation will be deleted.
  • Annotation modification: the previous state of the annotation will be reverted.
  • Annotation deletion: the annotation will be restored.

Note that if a deleted annotation is restored by calling this function, it will reappear in front of any other annotations, even if that was not its original stacking order.

Returns true if the operation has been undone successfully, false if there are no undoable operations available or the History API is disabled.

Returns:

The result of the undo operation.

Type
Promise.<boolean>
Examples
await instance.create(new PSPDFKit.Annotations.RectangleAnnotation({
  pageIndex: 0,
  boundingBox: new PSPDFKit.Geometry.Rect({
    left: 200,
    top: 150,
    width: 250,
    height: 75
  })
}));
console.log("Annotation created!");
await instance.history.undo();
console.log("Annotation creation undone: annotation deleted!");
// Undo all previous actions
while (await instance.history.undo()) {}