@contentful/experiences-visual-editor-react 3.2.0-dev-20250812T1256-c477cd9.0 → 3.2.0-dev-20250812T1437-66fa8cb.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4920,7 +4920,7 @@ const collectNodeCoordinates = (node, nodeToCoordinatesMap) => {
4920
4920
  node.children.forEach((child) => collectNodeCoordinates(child, nodeToCoordinatesMap));
4921
4921
  };
4922
4922
  function waitForImageToBeLoaded(imageNode) {
4923
- if (imageNode.complete) {
4923
+ if (imageNode.complete && (imageNode.naturalWidth > 0 || imageNode.naturalHeight > 0)) {
4924
4924
  return Promise.resolve();
4925
4925
  }
4926
4926
  return new Promise((resolve, reject) => {
@@ -4954,8 +4954,8 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
4954
4954
  trailing: true,
4955
4955
  }), []);
4956
4956
  const debouncedCollectImages = useMemo(() => debounce(() => {
4957
- return Array.from(document.querySelectorAll('img'));
4958
- }, 300, { leading: true, trailing: true }), []);
4957
+ setImages((prev) => ({ ...prev, allImages: findAllImages() }));
4958
+ }, 300, { trailing: true }), []);
4959
4959
  // Store tree in a ref to avoid the need to deactivate & reactivate the mutation observer
4960
4960
  // when the tree changes. This is important to avoid missing out on some mutation events.
4961
4961
  const treeRef = useRef(tree);
@@ -4964,13 +4964,17 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
4964
4964
  }, [tree]);
4965
4965
  // Handling window resize events
4966
4966
  useEffect(() => {
4967
- const resizeEventListener = () => debouncedUpdateGeometry(treeRef.current, 'resize');
4967
+ const resizeEventListener = () => {
4968
+ debouncedUpdateGeometry(treeRef.current, 'resize');
4969
+ // find all images on resize
4970
+ debouncedCollectImages();
4971
+ };
4968
4972
  window.addEventListener('resize', resizeEventListener);
4969
4973
  return () => window.removeEventListener('resize', resizeEventListener);
4970
- }, [debouncedUpdateGeometry]);
4974
+ }, [debouncedCollectImages, debouncedUpdateGeometry]);
4971
4975
  const [{ allImages, loadedImages }, setImages] = useState(() => {
4972
- const allImages = debouncedCollectImages();
4973
- const loadedImages = new WeakSet();
4976
+ const allImages = findAllImages();
4977
+ const loadedImages = new WeakMap();
4974
4978
  return { allImages, loadedImages };
4975
4979
  });
4976
4980
  // Handling DOM mutations
@@ -4978,8 +4982,7 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
4978
4982
  const observer = new MutationObserver(() => {
4979
4983
  debouncedUpdateGeometry(treeRef.current, 'mutation');
4980
4984
  // find all images on any DOM change
4981
- const allImages = debouncedCollectImages();
4982
- setImages((prevState) => ({ ...prevState, allImages }));
4985
+ debouncedCollectImages();
4983
4986
  });
4984
4987
  // send initial geometry in case the tree is empty
4985
4988
  debouncedUpdateGeometry(treeRef.current, 'mutation');
@@ -4995,13 +4998,14 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
4995
4998
  useEffect(() => {
4996
4999
  let isCurrent = true;
4997
5000
  allImages.forEach(async (imageNode) => {
4998
- if (loadedImages.has(imageNode)) {
5001
+ const lastSrc = loadedImages.get(imageNode);
5002
+ if (lastSrc === imageNode.currentSrc) {
4999
5003
  return;
5000
5004
  }
5001
5005
  // update the geometry after each image is loaded, as it can shift the layout
5002
5006
  await waitForImageToBeLoaded(imageNode);
5003
5007
  if (isCurrent) {
5004
- loadedImages.add(imageNode);
5008
+ loadedImages.set(imageNode, imageNode.currentSrc);
5005
5009
  debouncedUpdateGeometry(treeRef.current, 'imageLoad');
5006
5010
  }
5007
5011
  });
@@ -5028,6 +5032,9 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
5028
5032
  return () => document.removeEventListener('wheel', onWheel);
5029
5033
  }, [canvasMode]);
5030
5034
  };
5035
+ function findAllImages() {
5036
+ return Array.from(document.querySelectorAll('img'));
5037
+ }
5031
5038
 
5032
5039
  const RootRenderer = ({ inMemoryEntitiesStore, canvasMode }) => {
5033
5040
  useEditorSubscriber(inMemoryEntitiesStore);