Get previous annotation from annotationChanged event

Hey there,

There is no way to get this information from the annotation changed event.

What you could do is maintain a separate cache of annotations in your application, and grab that version of the annot in the annotationChanged event. Keep in mind that annotations are class references, so you will have to clone them in order to maintain the old state.

Something like this should get you started:

  const annotCache = new Map();

  const cacheAnnot = (annot) => {
    const id = annot.Id;
    const copy = instance.annotManager.getAnnotationCopy(annot);
    annotCache.set(id, copy);
  }

  const getCachedAnnot = (id) => {
    return annotCache.get(id);
  }

  // Clear the cache when a new doc is loaded
  instance.docViewer.on('documentLoaded', () => {
    annotCache.clear();
  })

  // create the cache when the annotations are loaded
  instance.docViewer.on('annotationsLoaded', () => {
    const annots = instance.annotManager.getAnnotationsList();
    annots.forEach(annot => {
      cacheAnnot(annot)
    })
  })

  instance.annotManager.on('annotationChanged', (annots) => {
    for (const annot of annots) {
      const id = annot.Id;
      const cachedAnnot = getCachedAnnot(id);
      console.log('Old annotation', cachedAnnot);
      console.log('New annotation', annot);
      cacheAnnot(annot);
    }
  })

Thanks!
Logan

1 Like