@contentful/experiences-visual-editor-react 3.8.1 → 3.8.2-dev-20251007T1425-20923d2.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/renderApp.js CHANGED
@@ -51307,26 +51307,6 @@ const collectNodeCoordinates = (node, nodeToCoordinatesMap) => {
51307
51307
  }
51308
51308
  node.children.forEach((child) => collectNodeCoordinates(child, nodeToCoordinatesMap));
51309
51309
  };
51310
- function waitForImageToBeLoaded(imageNode) {
51311
- if (imageNode.complete && (imageNode.naturalWidth > 0 || imageNode.naturalHeight > 0)) {
51312
- return Promise.resolve();
51313
- }
51314
- return new Promise((resolve, reject) => {
51315
- const handleImageLoad = (event) => {
51316
- imageNode.removeEventListener('load', handleImageLoad);
51317
- imageNode.removeEventListener('error', handleImageLoad);
51318
- if (event.type === 'error') {
51319
- debug$1.warn('[experiences-visual-editor-react::canvasGeometry] Image failed to load:', imageNode);
51320
- reject();
51321
- }
51322
- else {
51323
- resolve();
51324
- }
51325
- };
51326
- imageNode.addEventListener('load', handleImageLoad);
51327
- imageNode.addEventListener('error', handleImageLoad);
51328
- });
51329
- }
51330
51310
  // calculates the content height by finding the deepest node in the first 2 levels of the body
51331
51311
  function measureBodyContentHeight(depth = 2, node = document.body) {
51332
51312
  if (depth <= 0)
@@ -51366,9 +51346,24 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
51366
51346
  // yet show the need for this. So we might be able to drop this later to boost performance.
51367
51347
  trailing: true,
51368
51348
  }), []);
51369
- const debouncedCollectImages = reactExports.useMemo(() => debounce(() => {
51370
- setImages((prev) => ({ ...prev, allImages: findAllImages() }));
51371
- }, 300, { trailing: true }), []);
51349
+ // Handling image or video resizing separately,
51350
+ // as they can load at a different time, some might be hidden or lazy loaded
51351
+ // resulting in a layout shift.
51352
+ //
51353
+ // We observe every media element for resizing using the same ResizeObserver instance.
51354
+ // It's safe to call .observe() multiple times on the same element.
51355
+ // Also don't need to unobserve removed elements, as they should be garbage collected.
51356
+ const [mediaResizeObserver] = reactExports.useState(() => {
51357
+ return new ResizeObserver(() => {
51358
+ debouncedUpdateGeometry(treeRef.current, 'mediaResize');
51359
+ });
51360
+ });
51361
+ const debouncedObserveMediaResizing = reactExports.useMemo(() => debounce(() => {
51362
+ const allMedia = findAllMedia();
51363
+ for (const media of allMedia) {
51364
+ mediaResizeObserver.observe(media);
51365
+ }
51366
+ }, 300, { trailing: true }), [mediaResizeObserver]);
51372
51367
  // Store tree in a ref to avoid the need to deactivate & reactivate the mutation observer
51373
51368
  // when the tree changes. This is important to avoid missing out on some mutation events.
51374
51369
  const treeRef = reactExports.useRef(tree);
@@ -51379,23 +51374,16 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
51379
51374
  reactExports.useEffect(() => {
51380
51375
  const resizeEventListener = () => {
51381
51376
  debouncedUpdateGeometry(treeRef.current, 'resize');
51382
- // find all images on resize
51383
- debouncedCollectImages();
51384
51377
  };
51385
51378
  window.addEventListener('resize', resizeEventListener);
51386
51379
  return () => window.removeEventListener('resize', resizeEventListener);
51387
- }, [debouncedCollectImages, debouncedUpdateGeometry]);
51388
- const [{ allImages, loadedImages }, setImages] = reactExports.useState(() => {
51389
- const allImages = findAllImages();
51390
- const loadedImages = new WeakMap();
51391
- return { allImages, loadedImages };
51392
- });
51380
+ }, [debouncedUpdateGeometry]);
51393
51381
  // Handling DOM mutations
51394
51382
  reactExports.useEffect(() => {
51395
51383
  const observer = new MutationObserver(() => {
51396
51384
  debouncedUpdateGeometry(treeRef.current, 'mutation');
51397
- // find all images on any DOM change
51398
- debouncedCollectImages();
51385
+ // start observing all media on any DOM change, as there can be newly added elements
51386
+ debouncedObserveMediaResizing();
51399
51387
  });
51400
51388
  // send initial geometry in case the tree is empty
51401
51389
  debouncedUpdateGeometry(treeRef.current, 'mutation');
@@ -51405,27 +51393,7 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
51405
51393
  attributes: true,
51406
51394
  });
51407
51395
  return () => observer.disconnect();
51408
- }, [debouncedCollectImages, debouncedUpdateGeometry]);
51409
- // Handling image loading separately,
51410
- // as each image can load at a different time, some might be hidden or lazy loaded
51411
- reactExports.useEffect(() => {
51412
- let isCurrent = true;
51413
- allImages.forEach(async (imageNode) => {
51414
- const lastSrc = loadedImages.get(imageNode);
51415
- if (lastSrc === imageNode.currentSrc) {
51416
- return;
51417
- }
51418
- // update the geometry after each image is loaded, as it can shift the layout
51419
- await waitForImageToBeLoaded(imageNode);
51420
- if (isCurrent) {
51421
- loadedImages.set(imageNode, imageNode.currentSrc);
51422
- debouncedUpdateGeometry(treeRef.current, 'imageLoad');
51423
- }
51424
- });
51425
- return () => {
51426
- isCurrent = false;
51427
- };
51428
- }, [allImages, loadedImages, debouncedUpdateGeometry]);
51396
+ }, [debouncedObserveMediaResizing, debouncedUpdateGeometry]);
51429
51397
  // Delegate scrolling to the canvas
51430
51398
  reactExports.useEffect(() => {
51431
51399
  if (canvasMode !== StudioCanvasMode$2.EDITOR)
@@ -51445,8 +51413,8 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
51445
51413
  return () => document.removeEventListener('wheel', onWheel);
51446
51414
  }, [canvasMode]);
51447
51415
  };
51448
- function findAllImages() {
51449
- return Array.from(document.querySelectorAll('img'));
51416
+ function findAllMedia() {
51417
+ return Array.from(document.querySelectorAll('img, video'));
51450
51418
  }
51451
51419
 
51452
51420
  var css_248z = "body {\n margin: 0;\n}\n\nhtml {\n -ms-overflow-style: none; /* Internet Explorer 10+ */\n scrollbar-width: none; /* Firefox */\n}\n\nhtml::-webkit-scrollbar {\n display: none;\n}\n";