Disable Resizing of Annotations

It’s possible to disable resizing of certain annotation types. For example, here’s how to disable resizing of an ink signature:

// Customize `PSPDFKit.Options` here.

PSPDFKit.load({
  container: "#container",
  pdf: "./assets/example.pdf",
  licenseKey: ""
}).then((instance) => {
  let isInkSignatureSelected = false;
  let draggingInkSignature = false;
  let resizingInkSignature = false;

  instance.addEventListener(
    "annotations.press",
    (event) => {
      isInkSignatureSelected =
        event.annotation instanceof
          PSPDFKit.Annotations.InkAnnotation &&
        event.annotation.isSignature;

      if (!isInkSignatureSelected) {
        draggingInkSignature = false;
        resizingInkSignature = false;
        return;
      }

      const { nativeEvent } = event;
      let target = nativeEvent.target;
      if (target.matches(".PSPDFKit-Resize-Anchor")) {
        // ".PSPDFKit-Resize-Anchor" is present on our internal resize handle.
        resizingInkSignature = true;
        draggingInkSignature = false;
        instance.contentDocument.addEventListener(
          "pointerup",
          () => {
            resizingInkSignature = false;
          },
          { capture: true, once: true }
        );
      } else if (target.matches(".PSPDFKit-Selection-Outline-Border")) {
        // ".PSPDFKit-Selection-Outline-Border" is present on our internal drag handle.
        draggingInkSignature = true;
        instance.contentDocument.addEventListener(
          "pointerup",
          () => {
            draggingInkSignature = false;
          },
          { capture: true, once: true }
        );
      } else {
        draggingInkSignature = false;
      }
    },
    true
  );

  instance.contentDocument.addEventListener(
    "pointermove",
    (event) => {
      if (resizingInkSignature) {
        // Avoid running if computation is not needed.
        event.stopImmediatePropagation();
      } else {
        if (draggingInkSignature) {
          return;
        }
        let target = event.target;
        const annotation = instance.getSelectedAnnotation();
        const isInkSignatureSelected =
          annotation &&
          annotation instanceof PSPDFKit.Annotations.InkAnnotation;
        if (
          !draggingInkSignature &&
          isInkSignatureSelected &&
          target.matches(".PSPDFKit-Selection-Outline-Border")
        ) {
          draggingInkSignature = true;
          event.stopImmediatePropagation();
        }
      }
    },
    { capture: true }
  );
});