Keep current annotation tool enabled

Q: I want to implement a persistent annotation button that remains on so users can quickly add multiple annotations.

A: After a new annotation is created, the PSPDFKit.ViewState#interactionMode goes back to the default value of null, meaning that no interaction mode is enabled.

You can override this behavior by preserving the previous interaction mode after an annotation has been created by adding a handler of annotationSelection.change events to programmatically change the interaction mode back to the desired one when the current (new) annotation is deselected.

The following code snippet shows how to keep the text annotation tool selected after a text annotation has been created. You can adapt it for other types of annotations if you wish so:

let newAnnotation = null;
let annotationClass = PSPDFKit.Annotations.NoteAnnotation;
let interactionMode = PSPDFKit.InteractionMode.NOTE;

instance.addEventListener("annotations.create", annotations => {
  if (
    annotations.size === 1 && 
    annotations.get(0) instanceof annotationClass && 
    instance.viewState.interactionMode === interactionMode) 
  {
    newAnnotation = annotations.get(0);
  }
});

instance.addEventListener("annotationSelection.change", (annotation) => {  
  if (annotation === null && newAnnotation) {
    // Optionally set the current annotation preset again eg.
    // instance.setCurrentAnnotationPreset('rectangle');

    instance.setViewState(state =>
      state.set("interactionMode", interactionMode)
    );
  } 
  newAnnotation = null;
});

This has been tested with PSPDFKit for Web 2019.5.4.