ailon / markerjs2

Add image annotation to your web apps.
https://markerjs.com
Other
142 stars 39 forks source link

Marker-level events not firing after restoring old state #183

Closed KedroX closed 9 months ago

KedroX commented 9 months ago

Hi,

I don't know if it's a bug or simply not implemented but I'm trying to use Marker-level events to handle marker change. It works as expected for new created markers during current editing sessions, but not after restoring an old state. In this case events are not firing.

Is there a way to do this ?

I'm using marker.js 2 from UNPKG CDN

https://unpkg.com/markerjs2/markerjs2.js

Here is an example with "markerchange" event. After restoring state, if you modify a restored marker from old state, event is not fired :

import "./styles.css";
import * as markerjs2 from "markerjs2";

let sourceImage, targetRoot, maState;

// save references to the original image and its parent div (positioning root)
function setSourceImage(source) {
  sourceImage = source;
  targetRoot = source.parentElement;
}

function showMarkerArea(target) {
  const markerArea = new markerjs2.MarkerArea(sourceImage);
  // since the container div is set to position: relative it is now our positioning root
  // end we have to let marker.js know that
  markerArea.targetRoot = targetRoot;
  markerArea.addEventListener("render", (event) => {
    target.src = event.dataUrl;
    // save the state of MarkerArea
    maState = event.state;
  });
  markerArea.show();
  // if previous state is present - restore it
  if (maState) {
    markerArea.restoreState(maState);
  }

  markerArea.addEventListener("markerchange", (event) => {
    alert("change");
  });
}

setSourceImage(document.getElementById("sourceImage"));

Thanks a lot for your help! And congratulations for this good library !

L.

ailon commented 9 months ago

Hmm... Not sure what could be wrong here. It's very similar to the save and restore state demo and it works there. Do you get any errors in the console maybe?

KedroX commented 9 months ago

Hi, Yes it works in the demo, but only during the current editing session. I just added an alert in the markerchange event to check. Here are the steps to reproduce :

ailon commented 9 months ago

Oh, I get and see it now. Thank you for the details! I will investigate and fix this.

ailon commented 9 months ago

This should be fixed in 2.31.1. Thank you for reporting this bug, @KedroX!

KedroX commented 9 months ago

Thanks to you for your job!