@contentful/experiences-visual-editor-react 3.8.1-prerelease-20251001T1110-0a9573b.0 → 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/index.js CHANGED
@@ -5111,7 +5111,7 @@ const useComponentProps = ({ node, entityStore, areEntitiesFetched, resolveDesig
5111
5111
  let boundValue;
5112
5112
  // TODO: Temporary fix while we look into SPA-3212 it occurs where we have prebound props but data source link is missing
5113
5113
  // this only occurs after live updates of nested patterns.
5114
- if (!link && isPreboundProp(variableMapping) && variableMapping.isPrebound) {
5114
+ if (!link) {
5115
5115
  return {
5116
5116
  ...acc,
5117
5117
  [variableName]: variableDefinition.defaultValue,
@@ -5361,26 +5361,6 @@ const collectNodeCoordinates = (node, nodeToCoordinatesMap) => {
5361
5361
  }
5362
5362
  node.children.forEach((child) => collectNodeCoordinates(child, nodeToCoordinatesMap));
5363
5363
  };
5364
- function waitForImageToBeLoaded(imageNode) {
5365
- if (imageNode.complete && (imageNode.naturalWidth > 0 || imageNode.naturalHeight > 0)) {
5366
- return Promise.resolve();
5367
- }
5368
- return new Promise((resolve, reject) => {
5369
- const handleImageLoad = (event) => {
5370
- imageNode.removeEventListener('load', handleImageLoad);
5371
- imageNode.removeEventListener('error', handleImageLoad);
5372
- if (event.type === 'error') {
5373
- debug$1.warn('[experiences-visual-editor-react::canvasGeometry] Image failed to load:', imageNode);
5374
- reject();
5375
- }
5376
- else {
5377
- resolve();
5378
- }
5379
- };
5380
- imageNode.addEventListener('load', handleImageLoad);
5381
- imageNode.addEventListener('error', handleImageLoad);
5382
- });
5383
- }
5384
5364
  // calculates the content height by finding the deepest node in the first 2 levels of the body
5385
5365
  function measureBodyContentHeight(depth = 2, node = document.body) {
5386
5366
  if (depth <= 0)
@@ -5420,9 +5400,24 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
5420
5400
  // yet show the need for this. So we might be able to drop this later to boost performance.
5421
5401
  trailing: true,
5422
5402
  }), []);
5423
- const debouncedCollectImages = useMemo(() => debounce(() => {
5424
- setImages((prev) => ({ ...prev, allImages: findAllImages() }));
5425
- }, 300, { trailing: true }), []);
5403
+ // Handling image or video resizing separately,
5404
+ // as they can load at a different time, some might be hidden or lazy loaded
5405
+ // resulting in a layout shift.
5406
+ //
5407
+ // We observe every media element for resizing using the same ResizeObserver instance.
5408
+ // It's safe to call .observe() multiple times on the same element.
5409
+ // Also don't need to unobserve removed elements, as they should be garbage collected.
5410
+ const [mediaResizeObserver] = useState(() => {
5411
+ return new ResizeObserver(() => {
5412
+ debouncedUpdateGeometry(treeRef.current, 'mediaResize');
5413
+ });
5414
+ });
5415
+ const debouncedObserveMediaResizing = useMemo(() => debounce(() => {
5416
+ const allMedia = findAllMedia();
5417
+ for (const media of allMedia) {
5418
+ mediaResizeObserver.observe(media);
5419
+ }
5420
+ }, 300, { trailing: true }), [mediaResizeObserver]);
5426
5421
  // Store tree in a ref to avoid the need to deactivate & reactivate the mutation observer
5427
5422
  // when the tree changes. This is important to avoid missing out on some mutation events.
5428
5423
  const treeRef = useRef(tree);
@@ -5433,23 +5428,16 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
5433
5428
  useEffect(() => {
5434
5429
  const resizeEventListener = () => {
5435
5430
  debouncedUpdateGeometry(treeRef.current, 'resize');
5436
- // find all images on resize
5437
- debouncedCollectImages();
5438
5431
  };
5439
5432
  window.addEventListener('resize', resizeEventListener);
5440
5433
  return () => window.removeEventListener('resize', resizeEventListener);
5441
- }, [debouncedCollectImages, debouncedUpdateGeometry]);
5442
- const [{ allImages, loadedImages }, setImages] = useState(() => {
5443
- const allImages = findAllImages();
5444
- const loadedImages = new WeakMap();
5445
- return { allImages, loadedImages };
5446
- });
5434
+ }, [debouncedUpdateGeometry]);
5447
5435
  // Handling DOM mutations
5448
5436
  useEffect(() => {
5449
5437
  const observer = new MutationObserver(() => {
5450
5438
  debouncedUpdateGeometry(treeRef.current, 'mutation');
5451
- // find all images on any DOM change
5452
- debouncedCollectImages();
5439
+ // start observing all media on any DOM change, as there can be newly added elements
5440
+ debouncedObserveMediaResizing();
5453
5441
  });
5454
5442
  // send initial geometry in case the tree is empty
5455
5443
  debouncedUpdateGeometry(treeRef.current, 'mutation');
@@ -5459,27 +5447,7 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
5459
5447
  attributes: true,
5460
5448
  });
5461
5449
  return () => observer.disconnect();
5462
- }, [debouncedCollectImages, debouncedUpdateGeometry]);
5463
- // Handling image loading separately,
5464
- // as each image can load at a different time, some might be hidden or lazy loaded
5465
- useEffect(() => {
5466
- let isCurrent = true;
5467
- allImages.forEach(async (imageNode) => {
5468
- const lastSrc = loadedImages.get(imageNode);
5469
- if (lastSrc === imageNode.currentSrc) {
5470
- return;
5471
- }
5472
- // update the geometry after each image is loaded, as it can shift the layout
5473
- await waitForImageToBeLoaded(imageNode);
5474
- if (isCurrent) {
5475
- loadedImages.set(imageNode, imageNode.currentSrc);
5476
- debouncedUpdateGeometry(treeRef.current, 'imageLoad');
5477
- }
5478
- });
5479
- return () => {
5480
- isCurrent = false;
5481
- };
5482
- }, [allImages, loadedImages, debouncedUpdateGeometry]);
5450
+ }, [debouncedObserveMediaResizing, debouncedUpdateGeometry]);
5483
5451
  // Delegate scrolling to the canvas
5484
5452
  useEffect(() => {
5485
5453
  if (canvasMode !== StudioCanvasMode$2.EDITOR)
@@ -5499,8 +5467,8 @@ const useCanvasGeometryUpdates = ({ tree, canvasMode }) => {
5499
5467
  return () => document.removeEventListener('wheel', onWheel);
5500
5468
  }, [canvasMode]);
5501
5469
  };
5502
- function findAllImages() {
5503
- return Array.from(document.querySelectorAll('img'));
5470
+ function findAllMedia() {
5471
+ return Array.from(document.querySelectorAll('img, video'));
5504
5472
  }
5505
5473
 
5506
5474
  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";