@measured/puck-plugin-heading-analyzer 0.19.0-canary.7e231b7 → 0.19.0-canary.83684808

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.mjs CHANGED
@@ -187,7 +187,7 @@ init_react_import();
187
187
 
188
188
  // src/HeadingAnalyzer.tsx
189
189
  init_react_import();
190
- import { useEffect as useEffect6, useState } from "react";
190
+ import { useEffect as useEffect5, useState } from "react";
191
191
 
192
192
  // css-module:/home/runner/work/puck/puck/packages/plugin-heading-analyzer/src/HeadingAnalyzer.module.css#css-module
193
193
  init_react_import();
@@ -356,75 +356,247 @@ init_react_import();
356
356
  // ../core/reducer/index.ts
357
357
  init_react_import();
358
358
 
359
- // ../core/reducer/data.ts
359
+ // ../core/reducer/actions/set.ts
360
360
  init_react_import();
361
361
 
362
- // ../core/lib/reorder.ts
362
+ // ../core/lib/data/walk-tree.ts
363
363
  init_react_import();
364
- var reorder = (list, startIndex, endIndex) => {
365
- const result = Array.from(list);
366
- const [removed] = result.splice(startIndex, 1);
367
- result.splice(endIndex, 0, removed);
368
- return result;
364
+
365
+ // ../core/lib/data/for-each-slot.ts
366
+ init_react_import();
367
+
368
+ // ../core/lib/data/is-slot.ts
369
+ init_react_import();
370
+ var isSlot = (prop) => {
371
+ var _a, _b;
372
+ return Array.isArray(prop) && typeof ((_a = prop[0]) == null ? void 0 : _a.type) === "string" && typeof ((_b = prop[0]) == null ? void 0 : _b.props) === "object";
373
+ };
374
+ var createIsSlotConfig = (config) => (itemType, propName, propValue) => {
375
+ var _a, _b;
376
+ const configForComponent = itemType === "root" ? config == null ? void 0 : config.root : config == null ? void 0 : config.components[itemType];
377
+ if (!configForComponent) return isSlot(propValue);
378
+ return ((_b = (_a = configForComponent.fields) == null ? void 0 : _a[propName]) == null ? void 0 : _b.type) === "slot";
369
379
  };
370
380
 
381
+ // ../core/lib/data/for-each-slot.ts
382
+ var forEachSlot = (item, cb, recursive = false, isSlot2 = isSlot) => {
383
+ const props = item.props || {};
384
+ const propKeys = Object.keys(props);
385
+ for (let i = 0; i < propKeys.length; i++) {
386
+ const propKey = propKeys[i];
387
+ const itemType = "type" in item ? item.type : "root";
388
+ if (isSlot2(itemType, propKey, props[propKey])) {
389
+ const content = props[propKey];
390
+ cb(props.id, propKey, content);
391
+ if (recursive) {
392
+ content.forEach(
393
+ (childItem) => __async(void 0, null, function* () {
394
+ return forEachSlot(childItem, cb, true, isSlot2);
395
+ })
396
+ );
397
+ }
398
+ }
399
+ }
400
+ };
401
+
402
+ // ../core/lib/data/for-related-zones.ts
403
+ init_react_import();
404
+
405
+ // ../core/lib/get-zone-id.ts
406
+ init_react_import();
407
+
371
408
  // ../core/lib/root-droppable-id.ts
372
409
  init_react_import();
373
410
  var rootAreaId = "root";
374
411
  var rootZone = "default-zone";
375
412
  var rootDroppableId = `${rootAreaId}:${rootZone}`;
376
413
 
377
- // ../core/lib/insert.ts
378
- init_react_import();
379
- var insert = (list, index, item) => {
380
- const result = Array.from(list);
381
- result.splice(index, 0, item);
382
- return result;
414
+ // ../core/lib/get-zone-id.ts
415
+ var getZoneId = (zoneCompound) => {
416
+ if (!zoneCompound) {
417
+ return [];
418
+ }
419
+ if (zoneCompound && zoneCompound.indexOf(":") > -1) {
420
+ return zoneCompound.split(":");
421
+ }
422
+ return [rootDroppableId, zoneCompound];
383
423
  };
384
424
 
385
- // ../core/lib/remove.ts
386
- init_react_import();
387
- var remove = (list, index) => {
388
- const result = Array.from(list);
389
- result.splice(index, 1);
390
- return result;
391
- };
425
+ // ../core/lib/data/for-related-zones.ts
426
+ function forRelatedZones(item, data, cb, path = []) {
427
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
428
+ const [parentId] = getZoneId(zoneCompound);
429
+ if (parentId === item.props.id) {
430
+ cb(path, zoneCompound, content);
431
+ }
432
+ });
433
+ }
392
434
 
393
- // ../core/lib/setup-zone.ts
435
+ // ../core/lib/data/strip-slots.ts
394
436
  init_react_import();
395
- var setupZone = (data, zoneKey) => {
396
- if (zoneKey === rootDroppableId) {
397
- return data;
398
- }
399
- const newData = __spreadProps(__spreadValues({}, data), {
400
- zones: data.zones ? __spreadValues({}, data.zones) : {}
437
+ var stripSlots = (data) => {
438
+ return __spreadProps(__spreadValues({}, data), {
439
+ props: Object.entries(data.props).reduce(
440
+ (acc, [propKey, propVal]) => {
441
+ if (isSlot(propVal)) {
442
+ return acc;
443
+ }
444
+ return __spreadProps(__spreadValues({}, acc), { [propKey]: propVal });
445
+ },
446
+ { id: data.props.id }
447
+ )
401
448
  });
402
- newData.zones[zoneKey] = newData.zones[zoneKey] || [];
403
- return newData;
404
449
  };
405
450
 
406
- // ../core/lib/replace.ts
407
- init_react_import();
408
- var replace = (list, index, newItem) => {
409
- const result = Array.from(list);
410
- result.splice(index, 1);
411
- result.splice(index, 0, newItem);
412
- return result;
451
+ // ../core/lib/data/walk-tree.ts
452
+ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
453
+ var _a;
454
+ let newZones = {};
455
+ const newZoneIndex = {};
456
+ const newNodeIndex = {};
457
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
458
+ var _a2;
459
+ const [parentId] = zoneCompound.split(":");
460
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
461
+ const [_2, zone] = zoneCompound.split(":");
462
+ const newZoneCompound = `${newId || parentId}:${zone}`;
463
+ const newContent2 = mappedContent.map(
464
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
465
+ );
466
+ newZoneIndex[newZoneCompound] = {
467
+ contentIds: newContent2.map((item) => item.props.id),
468
+ type: zoneType
469
+ };
470
+ return [newZoneCompound, newContent2];
471
+ };
472
+ const processRelatedZones = (item, newId, initialPath) => {
473
+ forRelatedZones(
474
+ item,
475
+ state.data,
476
+ (relatedPath, relatedZoneCompound, relatedContent) => {
477
+ const [zoneCompound, newContent2] = processContent(
478
+ relatedPath,
479
+ relatedZoneCompound,
480
+ relatedContent,
481
+ "dropzone",
482
+ newId
483
+ );
484
+ newZones[zoneCompound] = newContent2;
485
+ },
486
+ initialPath
487
+ );
488
+ };
489
+ const processItem = (item, path, index) => {
490
+ const mappedItem = mapNodeOrSkip(item, path, index);
491
+ if (!mappedItem) return item;
492
+ const id = mappedItem.props.id;
493
+ const newProps = __spreadValues({}, mappedItem.props);
494
+ forEachSlot(
495
+ mappedItem,
496
+ (parentId2, slotId, content) => {
497
+ const zoneCompound = `${parentId2}:${slotId}`;
498
+ const [_2, newContent2] = processContent(
499
+ path,
500
+ zoneCompound,
501
+ content,
502
+ "slot",
503
+ parentId2
504
+ );
505
+ newProps[slotId] = newContent2;
506
+ },
507
+ false,
508
+ createIsSlotConfig(config)
509
+ );
510
+ processRelatedZones(item, id, path);
511
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
512
+ const thisZoneCompound = path[path.length - 1];
513
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
514
+ newNodeIndex[id] = {
515
+ data: newItem,
516
+ flatData: stripSlots(newItem),
517
+ path,
518
+ parentId,
519
+ zone
520
+ };
521
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
522
+ if (newProps.id === "root") {
523
+ delete finalData["type"];
524
+ delete finalData.props["id"];
525
+ }
526
+ return finalData;
527
+ };
528
+ const zones = state.data.zones || {};
529
+ const [_, newContent] = processContent(
530
+ [],
531
+ rootDroppableId,
532
+ state.data.content,
533
+ "root"
534
+ );
535
+ const processedContent = newContent;
536
+ const zonesAlreadyProcessed = Object.keys(newZones);
537
+ Object.keys(zones || {}).forEach((zoneCompound) => {
538
+ const [parentId] = zoneCompound.split(":");
539
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
540
+ return;
541
+ }
542
+ const [_2, newContent2] = processContent(
543
+ [rootDroppableId],
544
+ zoneCompound,
545
+ zones[zoneCompound],
546
+ "dropzone",
547
+ parentId
548
+ );
549
+ newZones[zoneCompound] = newContent2;
550
+ }, newZones);
551
+ const processedRoot = processItem(
552
+ {
553
+ type: "root",
554
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
555
+ },
556
+ [],
557
+ -1
558
+ );
559
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
560
+ props: processedRoot.props
561
+ });
562
+ return __spreadProps(__spreadValues({}, state), {
563
+ data: {
564
+ root,
565
+ content: processedContent,
566
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
567
+ },
568
+ indexes: {
569
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
570
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
571
+ }
572
+ });
573
+ }
574
+
575
+ // ../core/reducer/actions/set.ts
576
+ var setAction = (state, action, appStore) => {
577
+ if (typeof action.state === "object") {
578
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
579
+ if (action.state.indexes) {
580
+ return newState;
581
+ }
582
+ console.warn(
583
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
584
+ );
585
+ return walkTree(newState, appStore.config);
586
+ }
587
+ return __spreadValues(__spreadValues({}, state), action.state(state));
413
588
  };
414
589
 
415
- // ../core/lib/get-item.ts
590
+ // ../core/reducer/actions/insert.ts
416
591
  init_react_import();
417
- function getItem(selector, data, dynamicProps = {}) {
418
- if (!selector.zone || selector.zone === rootDroppableId) {
419
- const item2 = data.content[selector.index];
420
- return (item2 == null ? void 0 : item2.props) ? __spreadProps(__spreadValues({}, item2), { props: dynamicProps[item2.props.id] || item2.props }) : void 0;
421
- }
422
- const item = setupZone(data, selector.zone).zones[selector.zone][selector.index];
423
- return (item == null ? void 0 : item.props) ? __spreadProps(__spreadValues({}, item), { props: dynamicProps[item.props.id] || item.props }) : void 0;
424
- }
425
592
 
426
- // ../core/lib/reduce-related-zones.ts
593
+ // ../core/lib/data/insert.ts
427
594
  init_react_import();
595
+ var insert = (list, index, item) => {
596
+ const result = Array.from(list || []);
597
+ result.splice(index, 0, item);
598
+ return result;
599
+ };
428
600
 
429
601
  // ../core/lib/generate-id.ts
430
602
  init_react_import();
@@ -488,313 +660,378 @@ var v4_default = v4;
488
660
  // ../core/lib/generate-id.ts
489
661
  var generateId = (type) => type ? `${type}-${v4_default()}` : v4_default();
490
662
 
491
- // ../core/lib/get-zone-id.ts
663
+ // ../core/lib/data/get-ids-for-parent.ts
492
664
  init_react_import();
493
- var getZoneId = (zoneCompound) => {
494
- if (!zoneCompound) {
495
- return [];
496
- }
497
- if (zoneCompound && zoneCompound.indexOf(":") > -1) {
498
- return zoneCompound.split(":");
499
- }
500
- return [rootDroppableId, zoneCompound];
665
+ var getIdsForParent = (zoneCompound, state) => {
666
+ const [parentId] = zoneCompound.split(":");
667
+ const node = state.indexes.nodes[parentId];
668
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
501
669
  };
502
670
 
503
- // ../core/lib/reduce-related-zones.ts
504
- function reduceRelatedZones(item, data, fn) {
505
- return __spreadProps(__spreadValues({}, data), {
506
- zones: Object.keys(data.zones || {}).reduce(
507
- (acc, key) => {
508
- const [parentId] = getZoneId(key);
509
- if (parentId === item.props.id) {
510
- const zones = data.zones;
511
- return fn(acc, key, zones[key]);
512
- }
513
- return __spreadProps(__spreadValues({}, acc), { [key]: data.zones[key] });
514
- },
515
- {}
516
- )
517
- });
671
+ // ../core/reducer/actions/insert.ts
672
+ function insertAction(state, action, appStore) {
673
+ const id = action.id || generateId(action.componentType);
674
+ const emptyComponentData = {
675
+ type: action.componentType,
676
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
677
+ id
678
+ })
679
+ };
680
+ const [parentId] = action.destinationZone.split(":");
681
+ const idsInPath = getIdsForParent(action.destinationZone, state);
682
+ return walkTree(
683
+ state,
684
+ appStore.config,
685
+ (content, zoneCompound) => {
686
+ if (zoneCompound === action.destinationZone) {
687
+ return insert(
688
+ content || [],
689
+ action.destinationIndex,
690
+ emptyComponentData
691
+ );
692
+ }
693
+ return content;
694
+ },
695
+ (childItem, path) => {
696
+ if (childItem.props.id === id || childItem.props.id === parentId) {
697
+ return childItem;
698
+ } else if (idsInPath.includes(childItem.props.id)) {
699
+ return childItem;
700
+ } else if (path.includes(action.destinationZone)) {
701
+ return childItem;
702
+ }
703
+ return null;
704
+ }
705
+ );
518
706
  }
519
- var findRelatedByZoneId = (zoneId, data) => {
520
- const [zoneParentId] = getZoneId(zoneId);
521
- return (data.zones[zoneId] || []).reduce(
522
- (acc, zoneItem) => {
523
- const related = findRelatedByItem(zoneItem, data);
524
- if (zoneItem.props.id === zoneParentId) {
525
- return __spreadProps(__spreadValues(__spreadValues({}, acc), related), { [zoneId]: zoneItem });
707
+
708
+ // ../core/reducer/actions/replace.ts
709
+ init_react_import();
710
+ var replaceAction = (state, action, appStore) => {
711
+ const [parentId] = action.destinationZone.split(":");
712
+ const idsInPath = getIdsForParent(action.destinationZone, state);
713
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
714
+ const idChanged = originalId !== action.data.props.id;
715
+ if (idChanged) {
716
+ throw new Error(
717
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
718
+ );
719
+ }
720
+ return walkTree(
721
+ state,
722
+ appStore.config,
723
+ (content, zoneCompound) => {
724
+ const newContent = [...content];
725
+ if (zoneCompound === action.destinationZone) {
726
+ newContent[action.destinationIndex] = action.data;
526
727
  }
527
- return __spreadValues(__spreadValues({}, acc), related);
728
+ return newContent;
528
729
  },
529
- {}
730
+ (childItem, path) => {
731
+ const pathIds = path.map((p) => p.split(":")[0]);
732
+ if (childItem.props.id === action.data.props.id) {
733
+ return action.data;
734
+ } else if (childItem.props.id === parentId) {
735
+ return childItem;
736
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
737
+ return childItem;
738
+ } else if (pathIds.indexOf(action.data.props.id) > -1) {
739
+ return childItem;
740
+ }
741
+ return null;
742
+ }
530
743
  );
531
744
  };
532
- var findRelatedByItem = (item, data) => {
533
- return Object.keys(data.zones || {}).reduce(
534
- (acc, zoneId) => {
535
- const [zoneParentId] = getZoneId(zoneId);
536
- if (item.props.id === zoneParentId) {
537
- const related = findRelatedByZoneId(zoneId, data);
538
- return __spreadProps(__spreadValues(__spreadValues({}, acc), related), {
539
- [zoneId]: data.zones[zoneId]
745
+
746
+ // ../core/reducer/actions/replace-root.ts
747
+ init_react_import();
748
+ var replaceRootAction = (state, action, appStore) => {
749
+ return walkTree(
750
+ state,
751
+ appStore.config,
752
+ (content) => content,
753
+ (childItem) => {
754
+ if (childItem.props.id === "root") {
755
+ return __spreadProps(__spreadValues({}, childItem), {
756
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
757
+ readOnly: action.root.readOnly
540
758
  });
541
759
  }
542
- return acc;
543
- },
544
- {}
760
+ return childItem;
761
+ }
545
762
  );
546
763
  };
547
- var removeRelatedZones = (item, data) => {
548
- const newData = __spreadValues({}, data);
549
- const related = findRelatedByItem(item, data);
550
- Object.keys(related).forEach((key) => {
551
- delete newData.zones[key];
552
- });
553
- return newData;
554
- };
555
- function duplicateRelatedZones(item, data, newId) {
556
- return reduceRelatedZones(item, data, (acc, key, zone) => {
557
- const dupedZone = zone.map((zoneItem) => __spreadProps(__spreadValues({}, zoneItem), {
558
- props: __spreadProps(__spreadValues({}, zoneItem.props), { id: generateId(zoneItem.type) })
559
- }));
560
- const dupeOfDupes = dupedZone.reduce(
561
- (dupeOfDupes2, item2, index) => __spreadValues(__spreadValues({}, dupeOfDupes2), duplicateRelatedZones(zone[index], data, item2.props.id).zones),
562
- acc
563
- );
564
- const [_, zoneId] = getZoneId(key);
565
- return __spreadProps(__spreadValues({}, dupeOfDupes), {
566
- [key]: zone,
567
- [`${newId}:${zoneId}`]: dupedZone
568
- });
569
- });
764
+
765
+ // ../core/reducer/actions/duplicate.ts
766
+ init_react_import();
767
+
768
+ // ../core/lib/data/get-item.ts
769
+ init_react_import();
770
+ function getItem(selector, state) {
771
+ var _a, _b;
772
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
773
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
570
774
  }
571
775
 
572
- // ../core/reducer/data.ts
573
- var zoneCache = {};
574
- var replaceAction = (data, action) => {
575
- if (action.destinationZone === rootDroppableId) {
576
- return __spreadProps(__spreadValues({}, data), {
577
- content: replace(data.content, action.destinationIndex, action.data)
578
- });
579
- }
580
- const newData = setupZone(data, action.destinationZone);
581
- return __spreadProps(__spreadValues({}, newData), {
582
- zones: __spreadProps(__spreadValues({}, newData.zones), {
583
- [action.destinationZone]: replace(
584
- newData.zones[action.destinationZone],
585
- action.destinationIndex,
586
- action.data
587
- )
776
+ // ../core/reducer/actions/duplicate.ts
777
+ function duplicateAction(state, action, appStore) {
778
+ const item = getItem(
779
+ { index: action.sourceIndex, zone: action.sourceZone },
780
+ state
781
+ );
782
+ const idsInPath = getIdsForParent(action.sourceZone, state);
783
+ const newItem = __spreadProps(__spreadValues({}, item), {
784
+ props: __spreadProps(__spreadValues({}, item.props), {
785
+ id: generateId(item.type)
588
786
  })
589
787
  });
590
- };
591
- function insertAction(data, action, config) {
592
- const emptyComponentData = {
593
- type: action.componentType,
594
- props: __spreadProps(__spreadValues({}, config.components[action.componentType].defaultProps || {}), {
595
- id: action.id || generateId(action.componentType)
596
- })
597
- };
598
- if (action.destinationZone === rootDroppableId) {
599
- return __spreadProps(__spreadValues({}, data), {
600
- content: insert(
601
- data.content,
602
- action.destinationIndex,
603
- emptyComponentData
604
- )
605
- });
606
- }
607
- const newData = setupZone(data, action.destinationZone);
608
- return __spreadProps(__spreadValues({}, data), {
609
- zones: __spreadProps(__spreadValues({}, newData.zones), {
610
- [action.destinationZone]: insert(
611
- newData.zones[action.destinationZone],
612
- action.destinationIndex,
613
- emptyComponentData
614
- )
788
+ const modified = walkTree(
789
+ state,
790
+ appStore.config,
791
+ (content, zoneCompound) => {
792
+ if (zoneCompound === action.sourceZone) {
793
+ return insert(content, action.sourceIndex + 1, item);
794
+ }
795
+ return content;
796
+ },
797
+ (childItem, path, index) => {
798
+ const zoneCompound = path[path.length - 1];
799
+ const parents = path.map((p) => p.split(":")[0]);
800
+ if (parents.indexOf(newItem.props.id) > -1) {
801
+ return __spreadProps(__spreadValues({}, childItem), {
802
+ props: __spreadProps(__spreadValues({}, childItem.props), {
803
+ id: generateId(childItem.type)
804
+ })
805
+ });
806
+ }
807
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
808
+ return newItem;
809
+ }
810
+ const [sourceZoneParent] = action.sourceZone.split(":");
811
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
812
+ return childItem;
813
+ }
814
+ return null;
815
+ }
816
+ );
817
+ return __spreadProps(__spreadValues({}, modified), {
818
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
819
+ itemSelector: {
820
+ index: action.sourceIndex + 1,
821
+ zone: action.sourceZone
822
+ }
615
823
  })
616
824
  });
617
825
  }
618
- var reorderAction = (data, action) => {
619
- if (action.destinationZone === rootDroppableId) {
620
- return __spreadProps(__spreadValues({}, data), {
621
- content: reorder(
622
- data.content,
623
- action.sourceIndex,
624
- action.destinationIndex
625
- )
626
- });
627
- }
628
- const newData = setupZone(data, action.destinationZone);
629
- return __spreadProps(__spreadValues({}, data), {
630
- zones: __spreadProps(__spreadValues({}, newData.zones), {
631
- [action.destinationZone]: reorder(
632
- newData.zones[action.destinationZone],
633
- action.sourceIndex,
634
- action.destinationIndex
635
- )
636
- })
637
- });
826
+
827
+ // ../core/reducer/actions/reorder.ts
828
+ init_react_import();
829
+
830
+ // ../core/reducer/actions/move.ts
831
+ init_react_import();
832
+
833
+ // ../core/lib/data/remove.ts
834
+ init_react_import();
835
+ var remove = (list, index) => {
836
+ const result = Array.from(list);
837
+ result.splice(index, 1);
838
+ return result;
638
839
  };
639
- function reduceData(data, action, config) {
640
- if (action.type === "insert") {
641
- return insertAction(data, action, config);
642
- }
643
- if (action.type === "duplicate") {
644
- const item = getItem(
645
- { index: action.sourceIndex, zone: action.sourceZone },
646
- data
647
- );
648
- const newItem = __spreadProps(__spreadValues({}, item), {
649
- props: __spreadProps(__spreadValues({}, item.props), {
650
- id: generateId(item.type)
651
- })
652
- });
653
- const dataWithRelatedDuplicated = duplicateRelatedZones(
654
- item,
655
- data,
656
- newItem.props.id
657
- );
658
- if (action.sourceZone === rootDroppableId) {
659
- return __spreadProps(__spreadValues({}, dataWithRelatedDuplicated), {
660
- content: insert(data.content, action.sourceIndex + 1, newItem)
661
- });
662
- }
663
- return __spreadProps(__spreadValues({}, dataWithRelatedDuplicated), {
664
- zones: __spreadProps(__spreadValues({}, dataWithRelatedDuplicated.zones), {
665
- [action.sourceZone]: insert(
666
- dataWithRelatedDuplicated.zones[action.sourceZone],
667
- action.sourceIndex + 1,
668
- newItem
669
- )
670
- })
671
- });
672
- }
673
- if (action.type === "reorder") {
674
- return reorderAction(data, action);
840
+
841
+ // ../core/reducer/actions/move.ts
842
+ var moveAction = (state, action, appStore) => {
843
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
844
+ return state;
675
845
  }
676
- if (action.type === "move") {
677
- if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
678
- return data;
679
- }
680
- const newData = setupZone(
681
- setupZone(data, action.sourceZone),
682
- action.destinationZone
683
- );
684
- const item = getItem(
685
- { zone: action.sourceZone, index: action.sourceIndex },
686
- newData
687
- );
688
- if (action.sourceZone === action.destinationZone) {
689
- return reorderAction(data, __spreadProps(__spreadValues({}, action), { type: "reorder" }));
846
+ const item = getItem(
847
+ { zone: action.sourceZone, index: action.sourceIndex },
848
+ state
849
+ );
850
+ if (!item) return state;
851
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
852
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
853
+ return walkTree(
854
+ state,
855
+ appStore.config,
856
+ (content, zoneCompound) => {
857
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
858
+ return insert(
859
+ remove(content, action.sourceIndex),
860
+ action.destinationIndex,
861
+ item
862
+ );
863
+ } else if (zoneCompound === action.sourceZone) {
864
+ return remove(content, action.sourceIndex);
865
+ } else if (zoneCompound === action.destinationZone) {
866
+ return insert(content, action.destinationIndex, item);
867
+ }
868
+ return content;
869
+ },
870
+ (childItem, path) => {
871
+ const [sourceZoneParent] = action.sourceZone.split(":");
872
+ const [destinationZoneParent] = action.destinationZone.split(":");
873
+ const childId = childItem.props.id;
874
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
875
+ return childItem;
876
+ }
877
+ return null;
690
878
  }
691
- if (action.sourceZone === rootDroppableId) {
692
- return __spreadProps(__spreadValues({}, newData), {
693
- content: remove(newData.content, action.sourceIndex),
694
- zones: __spreadProps(__spreadValues({}, newData.zones), {
695
- [action.destinationZone]: insert(
696
- newData.zones[action.destinationZone],
697
- action.destinationIndex,
698
- item
699
- )
700
- })
701
- });
879
+ );
880
+ };
881
+
882
+ // ../core/reducer/actions/reorder.ts
883
+ var reorderAction = (state, action, appStore) => {
884
+ return moveAction(
885
+ state,
886
+ {
887
+ type: "move",
888
+ sourceIndex: action.sourceIndex,
889
+ sourceZone: action.destinationZone,
890
+ destinationIndex: action.destinationIndex,
891
+ destinationZone: action.destinationZone
892
+ },
893
+ appStore
894
+ );
895
+ };
896
+
897
+ // ../core/reducer/actions/remove.ts
898
+ init_react_import();
899
+ var removeAction = (state, action, appStore) => {
900
+ const item = getItem({ index: action.index, zone: action.zone }, state);
901
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
902
+ (acc, [nodeId, nodeData]) => {
903
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
904
+ if (pathIds.includes(item.props.id)) {
905
+ return [...acc, nodeId];
906
+ }
907
+ return acc;
908
+ },
909
+ [item.props.id]
910
+ );
911
+ const newState = walkTree(
912
+ state,
913
+ appStore.config,
914
+ (content, zoneCompound) => {
915
+ if (zoneCompound === action.zone) {
916
+ return remove(content, action.index);
917
+ }
918
+ return content;
702
919
  }
703
- if (action.destinationZone === rootDroppableId) {
704
- return __spreadProps(__spreadValues({}, newData), {
705
- content: insert(newData.content, action.destinationIndex, item),
706
- zones: __spreadProps(__spreadValues({}, newData.zones), {
707
- [action.sourceZone]: remove(
708
- newData.zones[action.sourceZone],
709
- action.sourceIndex
710
- )
711
- })
712
- });
920
+ );
921
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
922
+ const parentId = zoneCompound.split(":")[0];
923
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
924
+ delete newState.data.zones[zoneCompound];
713
925
  }
714
- return __spreadProps(__spreadValues({}, newData), {
715
- zones: __spreadProps(__spreadValues({}, newData.zones), {
716
- [action.sourceZone]: remove(
717
- newData.zones[action.sourceZone],
718
- action.sourceIndex
719
- ),
720
- [action.destinationZone]: insert(
721
- newData.zones[action.destinationZone],
722
- action.destinationIndex,
723
- item
724
- )
725
- })
726
- });
727
- }
728
- if (action.type === "replace") {
729
- return replaceAction(data, action);
730
- }
731
- if (action.type === "remove") {
732
- const item = getItem({ index: action.index, zone: action.zone }, data);
733
- const dataWithRelatedRemoved = setupZone(
734
- removeRelatedZones(item, data),
735
- action.zone
736
- );
737
- if (action.zone === rootDroppableId) {
738
- return __spreadProps(__spreadValues({}, dataWithRelatedRemoved), {
739
- content: remove(data.content, action.index)
740
- });
926
+ });
927
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
928
+ const parentId = zoneCompound.split(":")[0];
929
+ if (nodesToDelete.includes(parentId)) {
930
+ delete newState.indexes.zones[zoneCompound];
741
931
  }
742
- return __spreadProps(__spreadValues({}, dataWithRelatedRemoved), {
743
- zones: __spreadProps(__spreadValues({}, dataWithRelatedRemoved.zones), {
744
- [action.zone]: remove(
745
- dataWithRelatedRemoved.zones[action.zone],
746
- action.index
747
- )
748
- })
749
- });
932
+ });
933
+ nodesToDelete.forEach((id) => {
934
+ delete newState.indexes.nodes[id];
935
+ });
936
+ return newState;
937
+ };
938
+
939
+ // ../core/reducer/actions/register-zone.ts
940
+ init_react_import();
941
+
942
+ // ../core/lib/data/setup-zone.ts
943
+ init_react_import();
944
+ var setupZone = (data, zoneKey) => {
945
+ if (zoneKey === rootDroppableId) {
946
+ return data;
750
947
  }
751
- if (action.type === "registerZone") {
752
- if (zoneCache[action.zone]) {
753
- return __spreadProps(__spreadValues({}, data), {
754
- zones: __spreadProps(__spreadValues({}, data.zones), {
948
+ const newData = __spreadProps(__spreadValues({}, data), {
949
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
950
+ });
951
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
952
+ return newData;
953
+ };
954
+
955
+ // ../core/reducer/actions/register-zone.ts
956
+ var zoneCache = {};
957
+ function registerZoneAction(state, action) {
958
+ if (zoneCache[action.zone]) {
959
+ return __spreadProps(__spreadValues({}, state), {
960
+ data: __spreadProps(__spreadValues({}, state.data), {
961
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
755
962
  [action.zone]: zoneCache[action.zone]
756
963
  })
757
- });
758
- }
759
- return setupZone(data, action.zone);
760
- }
761
- if (action.type === "unregisterZone") {
762
- const _zones = __spreadValues({}, data.zones || {});
763
- if (_zones[action.zone]) {
764
- zoneCache[action.zone] = _zones[action.zone];
765
- delete _zones[action.zone];
766
- }
767
- return __spreadProps(__spreadValues({}, data), { zones: _zones });
964
+ }),
965
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
966
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
967
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
968
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
969
+ type: "dropzone"
970
+ })
971
+ })
972
+ })
973
+ });
768
974
  }
769
- if (action.type === "setData") {
770
- if (typeof action.data === "object") {
771
- return __spreadValues(__spreadValues({}, data), action.data);
772
- }
773
- return __spreadValues(__spreadValues({}, data), action.data(data));
975
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
976
+ }
977
+ function unregisterZoneAction(state, action) {
978
+ const _zones = __spreadValues({}, state.data.zones || {});
979
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
980
+ if (_zones[action.zone]) {
981
+ zoneCache[action.zone] = _zones[action.zone];
982
+ delete _zones[action.zone];
774
983
  }
775
- return data;
984
+ delete zoneIndex[action.zone];
985
+ return __spreadProps(__spreadValues({}, state), {
986
+ data: __spreadProps(__spreadValues({}, state.data), {
987
+ zones: _zones
988
+ }),
989
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
990
+ zones: zoneIndex
991
+ })
992
+ });
776
993
  }
777
994
 
778
- // ../core/reducer/state.ts
995
+ // ../core/reducer/actions/set-data.ts
779
996
  init_react_import();
780
- var reduceUi = (ui, action) => {
781
- if (action.type === "setUi") {
782
- if (typeof action.ui === "object") {
783
- return __spreadValues(__spreadValues({}, ui), action.ui);
784
- }
785
- return __spreadValues(__spreadValues({}, ui), action.ui(ui));
786
- }
787
- if (action.type === "duplicate") {
788
- return __spreadProps(__spreadValues({}, ui), {
789
- itemSelector: { index: action.sourceIndex + 1, zone: action.sourceZone }
790
- });
997
+ var setDataAction = (state, action, appStore) => {
998
+ if (typeof action.data === "object") {
999
+ console.warn(
1000
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
1001
+ );
1002
+ return walkTree(
1003
+ __spreadProps(__spreadValues({}, state), {
1004
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
1005
+ }),
1006
+ appStore.config
1007
+ );
791
1008
  }
792
- if (action.type === "remove") {
793
- return __spreadProps(__spreadValues({}, ui), {
794
- itemSelector: null
1009
+ return walkTree(
1010
+ __spreadProps(__spreadValues({}, state), {
1011
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
1012
+ }),
1013
+ appStore.config
1014
+ );
1015
+ };
1016
+
1017
+ // ../core/reducer/actions/set-ui.ts
1018
+ init_react_import();
1019
+ var setUiAction = (state, action) => {
1020
+ if (typeof action.ui === "object") {
1021
+ return __spreadProps(__spreadValues({}, state), {
1022
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
795
1023
  });
796
1024
  }
797
- return ui;
1025
+ return __spreadProps(__spreadValues({}, state), {
1026
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
1027
+ });
1028
+ };
1029
+
1030
+ // ../core/lib/data/make-state-public.ts
1031
+ init_react_import();
1032
+ var makeStatePublic = (state) => {
1033
+ const { data, ui } = state;
1034
+ return { data, ui };
798
1035
  };
799
1036
 
800
1037
  // ../core/reducer/actions.tsx
@@ -814,29 +1051,54 @@ function storeInterceptor(reducer, record, onAction) {
814
1051
  if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
815
1052
  if (record) record(newAppState);
816
1053
  }
817
- onAction == null ? void 0 : onAction(action, newAppState, state);
1054
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
818
1055
  return newAppState;
819
1056
  };
820
1057
  }
821
- var setAction = (state, action) => {
822
- if (typeof action.state === "object") {
823
- return __spreadValues(__spreadValues({}, state), action.state);
824
- }
825
- return __spreadValues(__spreadValues({}, state), action.state(state));
826
- };
827
1058
  function createReducer({
828
- config,
829
1059
  record,
830
- onAction
1060
+ onAction,
1061
+ appStore
831
1062
  }) {
832
1063
  return storeInterceptor(
833
1064
  (state, action) => {
834
- const data = reduceData(state.data, action, config);
835
- const ui = reduceUi(state.ui, action);
836
1065
  if (action.type === "set") {
837
- return setAction(state, action);
1066
+ return setAction(state, action, appStore);
1067
+ }
1068
+ if (action.type === "insert") {
1069
+ return insertAction(state, action, appStore);
1070
+ }
1071
+ if (action.type === "replace") {
1072
+ return replaceAction(state, action, appStore);
1073
+ }
1074
+ if (action.type === "replaceRoot") {
1075
+ return replaceRootAction(state, action, appStore);
838
1076
  }
839
- return { data, ui };
1077
+ if (action.type === "duplicate") {
1078
+ return duplicateAction(state, action, appStore);
1079
+ }
1080
+ if (action.type === "reorder") {
1081
+ return reorderAction(state, action, appStore);
1082
+ }
1083
+ if (action.type === "move") {
1084
+ return moveAction(state, action, appStore);
1085
+ }
1086
+ if (action.type === "remove") {
1087
+ return removeAction(state, action, appStore);
1088
+ }
1089
+ if (action.type === "registerZone") {
1090
+ return registerZoneAction(state, action);
1091
+ }
1092
+ if (action.type === "unregisterZone") {
1093
+ return unregisterZoneAction(state, action);
1094
+ }
1095
+ if (action.type === "setData") {
1096
+ return setDataAction(state, action, appStore);
1097
+ }
1098
+ if (action.type === "setUi") {
1099
+ return setUiAction(state, action);
1100
+ }
1101
+ return state;
840
1102
  },
841
1103
  record,
842
1104
  onAction
@@ -856,11 +1118,11 @@ init_react_import();
856
1118
  var createStoreImpl = (createState) => {
857
1119
  let state;
858
1120
  const listeners = /* @__PURE__ */ new Set();
859
- const setState = (partial, replace2) => {
1121
+ const setState = (partial, replace) => {
860
1122
  const nextState = typeof partial === "function" ? partial(state) : partial;
861
1123
  if (!Object.is(nextState, state)) {
862
1124
  const previousState = state;
863
- state = (replace2 != null ? replace2 : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
1125
+ state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
864
1126
  listeners.forEach((listener) => listener(state, previousState));
865
1127
  }
866
1128
  };
@@ -924,206 +1186,6 @@ var subscribeWithSelectorImpl = (fn) => (set, get, api) => {
924
1186
  };
925
1187
  var subscribeWithSelector = subscribeWithSelectorImpl;
926
1188
 
927
- // ../core/lib/resolve-data.ts
928
- init_react_import();
929
-
930
- // ../core/lib/resolve-component-data.ts
931
- init_react_import();
932
-
933
- // ../core/lib/get-changed.ts
934
- init_react_import();
935
- var getChanged = (newItem, oldItem) => {
936
- return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
937
- const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
938
- const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
939
- return __spreadProps(__spreadValues({}, acc), {
940
- [item]: oldItemProps[item] !== newItemProps[item]
941
- });
942
- }, {}) : {};
943
- };
944
-
945
- // ../core/lib/resolve-component-data.ts
946
- var cache = { lastChange: {} };
947
- var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd) {
948
- const configForItem = config.components[item.type];
949
- if (configForItem.resolveData) {
950
- const { item: oldItem = null, resolved = {} } = cache.lastChange[item.props.id] || {};
951
- if (item && item === oldItem) {
952
- return resolved;
953
- }
954
- const changed = getChanged(item, oldItem);
955
- if (onResolveStart) {
956
- onResolveStart(item);
957
- }
958
- const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
959
- changed,
960
- lastData: oldItem,
961
- metadata
962
- });
963
- const resolvedItem = __spreadProps(__spreadValues({}, item), {
964
- props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
965
- });
966
- if (Object.keys(readOnly).length) {
967
- resolvedItem.readOnly = readOnly;
968
- }
969
- cache.lastChange[item.props.id] = {
970
- item,
971
- resolved: resolvedItem
972
- };
973
- if (onResolveEnd) {
974
- onResolveEnd(resolvedItem);
975
- }
976
- return resolvedItem;
977
- }
978
- return item;
979
- });
980
-
981
- // ../core/lib/apply-dynamic-props.ts
982
- init_react_import();
983
- var applyDynamicProps = (data, dynamicProps, rootData) => {
984
- return __spreadProps(__spreadValues({}, data), {
985
- root: rootData ? __spreadValues(__spreadValues({}, data.root), rootData ? rootData : {}) : data.root,
986
- content: data.content.map((item) => {
987
- return dynamicProps[item.props.id] ? __spreadValues(__spreadValues({}, item), dynamicProps[item.props.id]) : item;
988
- }),
989
- zones: Object.keys(data.zones || {}).reduce((acc, zoneKey) => {
990
- return __spreadProps(__spreadValues({}, acc), {
991
- [zoneKey]: data.zones[zoneKey].map((item) => {
992
- return dynamicProps[item.props.id] ? __spreadValues(__spreadValues({}, item), dynamicProps[item.props.id]) : item;
993
- })
994
- });
995
- }, {})
996
- });
997
- };
998
-
999
- // ../core/lib/resolve-root-data.ts
1000
- init_react_import();
1001
- var cache2 = {};
1002
- function resolveRootData(data, config, metadata) {
1003
- return __async(this, null, function* () {
1004
- var _a, _b, _c, _d, _e;
1005
- if (((_a = config.root) == null ? void 0 : _a.resolveData) && data.root.props) {
1006
- if (((_b = cache2.lastChange) == null ? void 0 : _b.original) === data.root) {
1007
- return cache2.lastChange.resolved;
1008
- }
1009
- const changed = getChanged(data.root, (_c = cache2.lastChange) == null ? void 0 : _c.original);
1010
- const rootWithProps = data.root;
1011
- const resolvedRoot = yield (_e = config.root) == null ? void 0 : _e.resolveData(rootWithProps, {
1012
- changed,
1013
- lastData: ((_d = cache2.lastChange) == null ? void 0 : _d.original) || {},
1014
- metadata: metadata || {}
1015
- });
1016
- cache2.lastChange = {
1017
- original: data.root,
1018
- resolved: resolvedRoot
1019
- };
1020
- return __spreadProps(__spreadValues(__spreadValues({}, data.root), resolvedRoot), {
1021
- props: __spreadValues(__spreadValues({}, data.root.props), resolvedRoot.props)
1022
- });
1023
- }
1024
- return data.root;
1025
- });
1026
- }
1027
-
1028
- // ../core/lib/flatten-data.ts
1029
- init_react_import();
1030
- var flattenData = (data) => {
1031
- return Object.keys(data.zones || {}).reduce(
1032
- (acc, zone) => [...acc, ...data.zones[zone]],
1033
- data.content
1034
- );
1035
- };
1036
-
1037
- // ../core/lib/resolve-data.ts
1038
- var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1039
- var resolveData = (newAppState, appStoreData) => {
1040
- const {
1041
- state: appState,
1042
- config,
1043
- dispatch,
1044
- resolveDataRuns,
1045
- setComponentLoading,
1046
- unsetComponentLoading,
1047
- metadata,
1048
- permissions
1049
- } = appStoreData;
1050
- const deferredSetStates = {};
1051
- const _setComponentLoading = (id, loading, defer = 0) => {
1052
- if (deferredSetStates[id]) {
1053
- clearTimeout(deferredSetStates[id]);
1054
- delete deferredSetStates[id];
1055
- }
1056
- deferredSetStates[id] = setTimeout(() => {
1057
- if (loading) {
1058
- setComponentLoading(id);
1059
- } else {
1060
- unsetComponentLoading(id);
1061
- }
1062
- delete deferredSetStates[id];
1063
- }, defer);
1064
- };
1065
- const runResolvers = () => __async(void 0, null, function* () {
1066
- const newData = newAppState.data;
1067
- const flatContent = flattenData(newData).filter(
1068
- (item) => {
1069
- var _a;
1070
- return !!((_a = config.components[item.type]) == null ? void 0 : _a.resolveData);
1071
- }
1072
- );
1073
- const applyIfChange = (dynamicDataMap, dynamicRoot) => {
1074
- const processed = applyDynamicProps(
1075
- __spreadValues({}, appState.data),
1076
- dynamicDataMap,
1077
- dynamicRoot
1078
- );
1079
- const processedAppState = __spreadProps(__spreadValues({}, appState), { data: processed });
1080
- const containsChanges = !(0, import_fast_deep_equal.default)(appState, processedAppState);
1081
- if (containsChanges) {
1082
- dispatch({
1083
- type: "set",
1084
- state: (prev) => __spreadProps(__spreadValues({}, prev), {
1085
- data: applyDynamicProps(prev.data, dynamicDataMap, dynamicRoot),
1086
- ui: resolveDataRuns > 0 ? __spreadValues(__spreadValues({}, prev.ui), newAppState.ui) : prev.ui
1087
- }),
1088
- recordHistory: resolveDataRuns > 0
1089
- });
1090
- }
1091
- };
1092
- const promises = [];
1093
- promises.push(
1094
- (() => __async(void 0, null, function* () {
1095
- _setComponentLoading("puck-root", true, 50);
1096
- const dynamicRoot = yield resolveRootData(newData, config, metadata);
1097
- applyIfChange({}, dynamicRoot);
1098
- _setComponentLoading("puck-root", false);
1099
- }))()
1100
- );
1101
- flatContent.forEach((item) => {
1102
- promises.push(
1103
- (() => __async(void 0, null, function* () {
1104
- permissions.resolvePermissions({ item }, true);
1105
- const dynamicData = yield resolveComponentData(
1106
- item,
1107
- config,
1108
- metadata,
1109
- (item2) => {
1110
- _setComponentLoading(item2.props.id, true, 50);
1111
- },
1112
- (item2) => {
1113
- deferredSetStates[item2.props.id];
1114
- _setComponentLoading(item2.props.id, false);
1115
- }
1116
- );
1117
- const dynamicDataMap = { [item.props.id]: dynamicData };
1118
- applyIfChange(dynamicDataMap);
1119
- }))()
1120
- );
1121
- });
1122
- yield Promise.all(promises);
1123
- });
1124
- return runResolvers();
1125
- };
1126
-
1127
1189
  // ../core/store/index.ts
1128
1190
  import { createContext, useContext } from "react";
1129
1191
 
@@ -1231,7 +1293,7 @@ var createHistorySlice = (set, get) => {
1231
1293
  const { dispatch, history } = get();
1232
1294
  dispatch({
1233
1295
  type: "set",
1234
- state: ((_a = history.histories[history.index]) == null ? void 0 : _a.state) || history.initialAppState
1296
+ state: ((_a = history.histories[index]) == null ? void 0 : _a.state) || history.initialAppState
1235
1297
  });
1236
1298
  set({ history: __spreadProps(__spreadValues({}, history), { index }) });
1237
1299
  },
@@ -1241,31 +1303,14 @@ var createHistorySlice = (set, get) => {
1241
1303
 
1242
1304
  // ../core/store/slices/nodes.ts
1243
1305
  init_react_import();
1244
- var import_fast_deep_equal2 = __toESM(require_fast_deep_equal());
1245
- import { useEffect as useEffect3 } from "react";
1246
- var partialDeepEqual = (newItem, existingItem) => {
1247
- const filteredExistingItem = Object.keys(newItem).reduce(
1248
- (acc, key) => __spreadProps(__spreadValues({}, acc), { [key]: existingItem[key] }),
1249
- {}
1250
- );
1251
- return (0, import_fast_deep_equal2.default)(newItem, filteredExistingItem);
1252
- };
1253
1306
  var createNodesSlice = (set, get) => ({
1254
1307
  nodes: {},
1255
1308
  registerNode: (id, node) => {
1256
1309
  const s = get().nodes;
1257
- if (s.nodes[id] && partialDeepEqual(node, s.nodes[id])) {
1258
- return;
1259
- }
1260
1310
  const emptyNode = {
1261
1311
  id,
1262
1312
  methods: { sync: () => null },
1263
- data: { props: { id }, type: "unknown" },
1264
- parentId: "",
1265
- zone: "",
1266
- path: [],
1267
- element: null,
1268
- index: -1
1313
+ element: null
1269
1314
  };
1270
1315
  const existingNode = s.nodes[id];
1271
1316
  set({
@@ -1295,36 +1340,61 @@ var createNodesSlice = (set, get) => ({
1295
1340
 
1296
1341
  // ../core/store/slices/permissions.ts
1297
1342
  init_react_import();
1298
- import { useEffect as useEffect4 } from "react";
1343
+ import { useEffect as useEffect3 } from "react";
1344
+
1345
+ // ../core/lib/data/flatten-data.ts
1346
+ init_react_import();
1347
+ var flattenData = (state, config) => {
1348
+ const data = [];
1349
+ walkTree(
1350
+ state,
1351
+ config,
1352
+ (content) => content,
1353
+ (item) => {
1354
+ data.push(item);
1355
+ return null;
1356
+ }
1357
+ );
1358
+ return data;
1359
+ };
1360
+
1361
+ // ../core/lib/get-changed.ts
1362
+ init_react_import();
1363
+ var getChanged = (newItem, oldItem) => {
1364
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
1365
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
1366
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
1367
+ return __spreadProps(__spreadValues({}, acc), {
1368
+ [item]: oldItemProps[item] !== newItemProps[item]
1369
+ });
1370
+ }, {}) : {};
1371
+ };
1372
+
1373
+ // ../core/store/slices/permissions.ts
1299
1374
  var createPermissionsSlice = (set, get) => {
1300
1375
  const resolvePermissions = (..._0) => __async(void 0, [..._0], function* (params = {}, force) {
1301
- const { state, permissions } = get();
1302
- const { cache: cache3, globalPermissions } = permissions;
1376
+ const { state, permissions, config } = get();
1377
+ const { cache: cache2, globalPermissions } = permissions;
1303
1378
  const resolveDataForItem = (item2, force2 = false) => __async(void 0, null, function* () {
1304
1379
  var _a, _b, _c;
1305
- const {
1306
- config,
1307
- state: appState,
1308
- setComponentLoading,
1309
- unsetComponentLoading
1310
- } = get();
1311
- const componentConfig = item2.type === "root" ? config.root : config.components[item2.type];
1380
+ const { config: config2, state: appState, setComponentLoading } = get();
1381
+ const componentConfig = item2.type === "root" ? config2.root : config2.components[item2.type];
1312
1382
  if (!componentConfig) {
1313
1383
  return;
1314
1384
  }
1315
1385
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), componentConfig.permissions);
1316
1386
  if (componentConfig.resolvePermissions) {
1317
- const changed = getChanged(item2, (_a = cache3[item2.props.id]) == null ? void 0 : _a.lastData);
1387
+ const changed = getChanged(item2, (_a = cache2[item2.props.id]) == null ? void 0 : _a.lastData);
1318
1388
  if (Object.values(changed).some((el) => el === true) || force2) {
1319
- setComponentLoading(item2.props.id);
1389
+ const clearTimeout2 = setComponentLoading(item2.props.id, true, 50);
1320
1390
  const resolvedPermissions = yield componentConfig.resolvePermissions(
1321
1391
  item2,
1322
1392
  {
1323
1393
  changed,
1324
- lastPermissions: ((_b = cache3[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1394
+ lastPermissions: ((_b = cache2[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1325
1395
  permissions: initialPermissions,
1326
- appState,
1327
- lastData: ((_c = cache3[item2.props.id]) == null ? void 0 : _c.lastData) || null
1396
+ appState: makeStatePublic(appState),
1397
+ lastData: ((_c = cache2[item2.props.id]) == null ? void 0 : _c.lastData) || null
1328
1398
  }
1329
1399
  );
1330
1400
  const latest = get().permissions;
@@ -1341,7 +1411,7 @@ var createPermissionsSlice = (set, get) => {
1341
1411
  })
1342
1412
  })
1343
1413
  });
1344
- unsetComponentLoading(item2.props.id);
1414
+ clearTimeout2();
1345
1415
  }
1346
1416
  }
1347
1417
  });
@@ -1351,7 +1421,7 @@ var createPermissionsSlice = (set, get) => {
1351
1421
  // Shim the root data in by conforming to component data shape
1352
1422
  {
1353
1423
  type: "root",
1354
- props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "puck-root" })
1424
+ props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "root" })
1355
1425
  },
1356
1426
  force2
1357
1427
  );
@@ -1360,14 +1430,13 @@ var createPermissionsSlice = (set, get) => {
1360
1430
  if (item) {
1361
1431
  yield resolveDataForItem(item, force);
1362
1432
  } else if (type) {
1363
- flattenData(state.data).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1433
+ flattenData(state, config).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1364
1434
  yield resolveDataForItem(item2, force);
1365
1435
  }));
1366
1436
  } else if (root) {
1367
1437
  resolveDataForRoot(force);
1368
1438
  } else {
1369
- resolveDataForRoot(force);
1370
- flattenData(state.data).map((item2) => __async(void 0, null, function* () {
1439
+ flattenData(state, config).map((item2) => __async(void 0, null, function* () {
1371
1440
  yield resolveDataForItem(item2, force);
1372
1441
  }));
1373
1442
  }
@@ -1397,7 +1466,7 @@ var createPermissionsSlice = (set, get) => {
1397
1466
  } else if (root) {
1398
1467
  const rootConfig = config.root;
1399
1468
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), rootConfig == null ? void 0 : rootConfig.permissions);
1400
- const resolvedForItem = resolvedPermissions["puck-root"];
1469
+ const resolvedForItem = resolvedPermissions["root"];
1401
1470
  return resolvedForItem ? __spreadValues(__spreadValues({}, globalPermissions), resolvedForItem) : initialPermissions;
1402
1471
  }
1403
1472
  return globalPermissions;
@@ -1409,8 +1478,8 @@ var createPermissionsSlice = (set, get) => {
1409
1478
 
1410
1479
  // ../core/store/slices/fields.ts
1411
1480
  init_react_import();
1412
- import { useCallback, useEffect as useEffect5 } from "react";
1413
- var createFieldsStore = (_set, _get) => {
1481
+ import { useCallback, useEffect as useEffect4 } from "react";
1482
+ var createFieldsSlice = (_set, _get) => {
1414
1483
  return {
1415
1484
  fields: {},
1416
1485
  loading: false,
@@ -1418,6 +1487,112 @@ var createFieldsStore = (_set, _get) => {
1418
1487
  };
1419
1488
  };
1420
1489
 
1490
+ // ../core/lib/resolve-component-data.ts
1491
+ init_react_import();
1492
+
1493
+ // ../core/lib/data/map-slots.ts
1494
+ init_react_import();
1495
+ function mapSlotsAsync(_0, _1) {
1496
+ return __async(this, arguments, function* (item, map, recursive = true, isSlot2 = isSlot) {
1497
+ const props = __spreadValues({}, item.props);
1498
+ const propKeys = Object.keys(props);
1499
+ for (let i = 0; i < propKeys.length; i++) {
1500
+ const propKey = propKeys[i];
1501
+ const itemType = "type" in item ? item.type : "root";
1502
+ if (isSlot2(itemType, propKey, props[propKey])) {
1503
+ const content = props[propKey];
1504
+ const mappedContent = recursive ? yield Promise.all(
1505
+ content.map((item2) => __async(this, null, function* () {
1506
+ return yield mapSlotsAsync(item2, map, recursive, isSlot2);
1507
+ }))
1508
+ ) : content;
1509
+ props[propKey] = yield map(mappedContent, propKey);
1510
+ }
1511
+ }
1512
+ return __spreadProps(__spreadValues({}, item), { props });
1513
+ });
1514
+ }
1515
+
1516
+ // ../core/lib/resolve-component-data.ts
1517
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1518
+ var cache = { lastChange: {} };
1519
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace", recursive = true) {
1520
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
1521
+ if ((configForItem == null ? void 0 : configForItem.resolveData) && item.props) {
1522
+ const id = "id" in item.props ? item.props.id : "root";
1523
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
1524
+ if (item && (0, import_fast_deep_equal.default)(item, oldItem)) {
1525
+ return { node: resolved, didChange: false };
1526
+ }
1527
+ const changed = getChanged(item, oldItem);
1528
+ if (onResolveStart) {
1529
+ onResolveStart(item);
1530
+ }
1531
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
1532
+ changed,
1533
+ lastData: oldItem,
1534
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
1535
+ trigger
1536
+ });
1537
+ let resolvedItem = __spreadProps(__spreadValues({}, item), {
1538
+ props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
1539
+ });
1540
+ if (recursive) {
1541
+ resolvedItem = yield mapSlotsAsync(
1542
+ resolvedItem,
1543
+ (content) => __async(void 0, null, function* () {
1544
+ return Promise.all(
1545
+ content.map(
1546
+ (childItem) => __async(void 0, null, function* () {
1547
+ return (yield resolveComponentData(
1548
+ childItem,
1549
+ config,
1550
+ metadata,
1551
+ onResolveStart,
1552
+ onResolveEnd,
1553
+ trigger,
1554
+ false
1555
+ )).node;
1556
+ })
1557
+ )
1558
+ );
1559
+ }),
1560
+ false,
1561
+ createIsSlotConfig(config)
1562
+ );
1563
+ }
1564
+ if (Object.keys(readOnly).length) {
1565
+ resolvedItem.readOnly = readOnly;
1566
+ }
1567
+ cache.lastChange[id] = {
1568
+ item,
1569
+ resolved: resolvedItem
1570
+ };
1571
+ if (onResolveEnd) {
1572
+ onResolveEnd(resolvedItem);
1573
+ }
1574
+ return { node: resolvedItem, didChange: !(0, import_fast_deep_equal.default)(item, resolvedItem) };
1575
+ }
1576
+ return { node: item, didChange: false };
1577
+ });
1578
+
1579
+ // ../core/lib/data/to-root.ts
1580
+ init_react_import();
1581
+ var toRoot = (item) => {
1582
+ if ("type" in item && item.type !== "root") {
1583
+ throw new Error("Converting non-root item to root.");
1584
+ }
1585
+ const { readOnly } = item;
1586
+ if (item.props) {
1587
+ if ("id" in item.props) {
1588
+ const _a = item.props, { id } = _a, props = __objRest(_a, ["id"]);
1589
+ return { props, readOnly };
1590
+ }
1591
+ return { props: item.props, readOnly };
1592
+ }
1593
+ return { props: {}, readOnly };
1594
+ };
1595
+
1421
1596
  // ../core/store/index.ts
1422
1597
  var defaultAppState = {
1423
1598
  data: { content: [], root: {}, zones: {} },
@@ -1438,92 +1613,186 @@ var defaultAppState = {
1438
1613
  controlsVisible: true
1439
1614
  },
1440
1615
  field: { focus: null }
1616
+ },
1617
+ indexes: {
1618
+ nodes: {},
1619
+ zones: {}
1441
1620
  }
1442
1621
  };
1443
1622
  var defaultPageFields = {
1444
1623
  title: { type: "text" }
1445
1624
  };
1446
1625
  var createAppStore = (initialAppStore) => create()(
1447
- subscribeWithSelector((set, get) => __spreadProps(__spreadValues({
1448
- state: defaultAppState,
1449
- config: { components: {} },
1450
- componentState: {},
1451
- plugins: [],
1452
- overrides: {},
1453
- viewports: defaultViewports,
1454
- zoomConfig: {
1455
- autoZoom: 1,
1456
- rootHeight: 0,
1457
- zoom: 1
1458
- },
1459
- status: "LOADING",
1460
- iframe: {},
1461
- metadata: {}
1462
- }, initialAppStore), {
1463
- fields: createFieldsStore(set, get),
1464
- history: createHistorySlice(set, get),
1465
- nodes: createNodesSlice(set, get),
1466
- permissions: createPermissionsSlice(set, get),
1467
- getComponentConfig: (type) => {
1468
- var _a;
1469
- const { config, selectedItem } = get();
1470
- const rootFields = ((_a = config.root) == null ? void 0 : _a.fields) || defaultPageFields;
1471
- return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1472
- },
1473
- dispatch: (action) => set((s) => {
1474
- var _a, _b;
1475
- const { record } = get().history;
1476
- const dispatch = createReducer({ config: s.config, record });
1477
- const state = dispatch(s.state, action);
1478
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state.data) : null;
1479
- (_b = (_a = get()).onAction) == null ? void 0 : _b.call(_a, action, state, get().state);
1480
- return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1481
- }),
1482
- setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1483
- setStatus: (status) => set({ status }),
1484
- setComponentState: (componentState) => set({ componentState }),
1485
- setComponentLoading: (id) => {
1486
- var _a;
1487
- const { setComponentState, componentState } = get();
1488
- setComponentState(__spreadProps(__spreadValues({}, componentState), {
1489
- [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1490
- loadingCount: (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) + 1
1491
- })
1492
- }));
1493
- },
1494
- unsetComponentLoading: (id) => {
1495
- var _a;
1496
- const { setComponentState, componentState } = get();
1497
- setComponentState(__spreadProps(__spreadValues({}, componentState), {
1498
- [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1499
- loadingCount: Math.max(
1500
- (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) - 1,
1501
- 0
1502
- )
1503
- })
1504
- }));
1505
- },
1506
- // Helper
1507
- setUi: (ui, recordHistory) => set((s) => {
1508
- const dispatch = createReducer({
1509
- config: s.config,
1510
- record: () => {
1511
- }
1512
- });
1513
- const state = dispatch(s.state, {
1514
- type: "setUi",
1515
- ui,
1516
- recordHistory
1517
- });
1518
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state.data) : null;
1519
- return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1520
- }),
1521
- resolveDataRuns: 0,
1522
- resolveData: (newAppState) => set((s) => {
1523
- resolveData(newAppState, get());
1524
- return __spreadProps(__spreadValues({}, s), { resolveDataRuns: s.resolveDataRuns + 1 });
1525
- })
1526
- }))
1626
+ subscribeWithSelector((set, get) => {
1627
+ var _a, _b;
1628
+ return __spreadProps(__spreadValues({
1629
+ state: defaultAppState,
1630
+ config: { components: {} },
1631
+ componentState: {},
1632
+ plugins: [],
1633
+ overrides: {},
1634
+ viewports: defaultViewports,
1635
+ zoomConfig: {
1636
+ autoZoom: 1,
1637
+ rootHeight: 0,
1638
+ zoom: 1
1639
+ },
1640
+ status: "LOADING",
1641
+ iframe: {},
1642
+ metadata: {}
1643
+ }, initialAppStore), {
1644
+ fields: createFieldsSlice(set, get),
1645
+ history: createHistorySlice(set, get),
1646
+ nodes: createNodesSlice(set, get),
1647
+ permissions: createPermissionsSlice(set, get),
1648
+ getComponentConfig: (type) => {
1649
+ var _a2;
1650
+ const { config, selectedItem } = get();
1651
+ const rootFields = ((_a2 = config.root) == null ? void 0 : _a2.fields) || defaultPageFields;
1652
+ return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1653
+ },
1654
+ selectedItem: ((_a = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _a.ui.itemSelector) ? getItem(
1655
+ (_b = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _b.ui.itemSelector,
1656
+ initialAppStore.state
1657
+ ) : null,
1658
+ dispatch: (action) => set((s) => {
1659
+ var _a2, _b2;
1660
+ const { record } = get().history;
1661
+ const dispatch = createReducer({
1662
+ record,
1663
+ appStore: s
1664
+ });
1665
+ const state = dispatch(s.state, action);
1666
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1667
+ (_b2 = (_a2 = get()).onAction) == null ? void 0 : _b2.call(_a2, action, state, get().state);
1668
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1669
+ }),
1670
+ setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1671
+ setStatus: (status) => set({ status }),
1672
+ setComponentState: (componentState) => set({ componentState }),
1673
+ pendingLoadTimeouts: {},
1674
+ setComponentLoading: (id, loading = true, defer = 0) => {
1675
+ const { setComponentState, pendingLoadTimeouts } = get();
1676
+ const loadId = generateId();
1677
+ const setLoading = () => {
1678
+ var _a2;
1679
+ const { componentState } = get();
1680
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1681
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1682
+ loadingCount: (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) + 1
1683
+ })
1684
+ }));
1685
+ };
1686
+ const unsetLoading = () => {
1687
+ var _a2;
1688
+ const { componentState } = get();
1689
+ clearTimeout(timeout);
1690
+ delete pendingLoadTimeouts[loadId];
1691
+ set({ pendingLoadTimeouts });
1692
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1693
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1694
+ loadingCount: Math.max(
1695
+ (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) - 1,
1696
+ 0
1697
+ )
1698
+ })
1699
+ }));
1700
+ };
1701
+ const timeout = setTimeout(() => {
1702
+ if (loading) {
1703
+ setLoading();
1704
+ } else {
1705
+ unsetLoading();
1706
+ }
1707
+ delete pendingLoadTimeouts[loadId];
1708
+ set({ pendingLoadTimeouts });
1709
+ }, defer);
1710
+ set({
1711
+ pendingLoadTimeouts: __spreadProps(__spreadValues({}, pendingLoadTimeouts), {
1712
+ [id]: timeout
1713
+ })
1714
+ });
1715
+ return unsetLoading;
1716
+ },
1717
+ unsetComponentLoading: (id) => {
1718
+ const { setComponentLoading } = get();
1719
+ setComponentLoading(id, false);
1720
+ },
1721
+ // Helper
1722
+ setUi: (ui, recordHistory) => set((s) => {
1723
+ const dispatch = createReducer({
1724
+ record: () => {
1725
+ },
1726
+ appStore: s
1727
+ });
1728
+ const state = dispatch(s.state, {
1729
+ type: "setUi",
1730
+ ui,
1731
+ recordHistory
1732
+ });
1733
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1734
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1735
+ }),
1736
+ resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
1737
+ const { config, metadata, setComponentLoading, permissions } = get();
1738
+ const timeouts = {};
1739
+ return yield resolveComponentData(
1740
+ componentData,
1741
+ config,
1742
+ metadata,
1743
+ (item) => {
1744
+ const id = "id" in item.props ? item.props.id : "root";
1745
+ timeouts[id] = setComponentLoading(id, true, 50);
1746
+ },
1747
+ (item) => __async(void 0, null, function* () {
1748
+ const id = "id" in item.props ? item.props.id : "root";
1749
+ if ("type" in item) {
1750
+ yield permissions.refreshPermissions({ item });
1751
+ } else {
1752
+ yield permissions.refreshPermissions({ root: true });
1753
+ }
1754
+ timeouts[id]();
1755
+ }),
1756
+ trigger
1757
+ );
1758
+ }),
1759
+ resolveAndCommitData: () => __async(void 0, null, function* () {
1760
+ const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
1761
+ walkTree(
1762
+ state,
1763
+ config,
1764
+ (content) => content,
1765
+ (childItem) => {
1766
+ resolveComponentData2(childItem, "load").then((resolved) => {
1767
+ const { state: state2 } = get();
1768
+ const node = state2.indexes.nodes[resolved.node.props.id];
1769
+ if (node && resolved.didChange) {
1770
+ if (resolved.node.props.id === "root") {
1771
+ dispatch({
1772
+ type: "replaceRoot",
1773
+ root: toRoot(resolved.node)
1774
+ });
1775
+ } else {
1776
+ const zoneCompound = `${node.parentId}:${node.zone}`;
1777
+ const parentZone = state2.indexes.zones[zoneCompound];
1778
+ const index = parentZone.contentIds.indexOf(
1779
+ resolved.node.props.id
1780
+ );
1781
+ dispatch({
1782
+ type: "replace",
1783
+ data: resolved.node,
1784
+ destinationIndex: index,
1785
+ destinationZone: zoneCompound
1786
+ });
1787
+ }
1788
+ }
1789
+ });
1790
+ return childItem;
1791
+ }
1792
+ );
1793
+ })
1794
+ });
1795
+ })
1527
1796
  );
1528
1797
  var appStoreContext = createContext(createAppStore());
1529
1798
  function useAppStore(selector) {
@@ -1543,12 +1812,12 @@ var useBreadcrumbs = (renderCount) => {
1543
1812
  const config = useAppStore((s) => s.config);
1544
1813
  const path = useAppStore((s) => {
1545
1814
  var _a;
1546
- return (_a = s.nodes.nodes[selectedId]) == null ? void 0 : _a.path;
1815
+ return (_a = s.state.indexes.nodes[selectedId]) == null ? void 0 : _a.path;
1547
1816
  });
1548
1817
  const appStore = useAppStoreApi();
1549
1818
  return useMemo(() => {
1550
1819
  const breadcrumbs = (path == null ? void 0 : path.map((zoneCompound) => {
1551
- var _a, _b;
1820
+ var _a, _b, _c;
1552
1821
  const [componentId] = zoneCompound.split(":");
1553
1822
  if (componentId === "root") {
1554
1823
  return {
@@ -1556,12 +1825,15 @@ var useBreadcrumbs = (renderCount) => {
1556
1825
  selector: null
1557
1826
  };
1558
1827
  }
1559
- const node = appStore.getState().nodes.nodes[componentId];
1560
- const label = node ? (_b = (_a = config.components[node.data.type]) == null ? void 0 : _a.label) != null ? _b : node.data.type : "Component";
1828
+ const node = appStore.getState().state.indexes.nodes[componentId];
1829
+ const parentId = node.path[node.path.length - 1];
1830
+ const contentIds = ((_a = appStore.getState().state.indexes.zones[parentId]) == null ? void 0 : _a.contentIds) || [];
1831
+ const index = contentIds.indexOf(componentId);
1832
+ const label = node ? (_c = (_b = config.components[node.data.type]) == null ? void 0 : _b.label) != null ? _c : node.data.type : "Component";
1561
1833
  return {
1562
1834
  label,
1563
1835
  selector: node ? {
1564
- index: node.index,
1836
+ index,
1565
1837
  zone: node.path[node.path.length - 1]
1566
1838
  } : null
1567
1839
  };
@@ -1582,6 +1854,12 @@ init_react_import();
1582
1854
  // ../core/lib/filter.ts
1583
1855
  init_react_import();
1584
1856
 
1857
+ // ../core/lib/data/reorder.ts
1858
+ init_react_import();
1859
+
1860
+ // ../core/lib/data/replace.ts
1861
+ init_react_import();
1862
+
1585
1863
  // css-module:/home/runner/work/puck/puck/packages/core/components/Loader/styles.module.css#css-module
1586
1864
  init_react_import();
1587
1865
  var styles_module_default3 = { "Loader": "_Loader_nacdm_13", "loader-animation": "_loader-animation_nacdm_1" };
@@ -1763,17 +2041,39 @@ var usePuck = createUsePuck();
1763
2041
  var HeadingAnalyzer = () => {
1764
2042
  const data = usePuck((s) => s.appState.data);
1765
2043
  const [hierarchy, setHierarchy] = useState([]);
1766
- useEffect6(() => {
2044
+ useEffect5(() => {
1767
2045
  const frame = getFrame();
1768
- const entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
1769
- if (!entry) return;
1770
- setHierarchy(buildHierarchy(entry));
1771
- const observer = new MutationObserver(() => {
2046
+ let entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2047
+ const createHierarchy = () => {
1772
2048
  setHierarchy(buildHierarchy(entry));
2049
+ };
2050
+ const entryObserver = new MutationObserver(() => {
2051
+ createHierarchy();
2052
+ });
2053
+ const frameObserver = new MutationObserver(() => {
2054
+ entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2055
+ if (entry) {
2056
+ registerEntryObserver();
2057
+ frameObserver.disconnect();
2058
+ }
1773
2059
  });
1774
- observer.observe(entry, { subtree: true, childList: true });
2060
+ const registerEntryObserver = () => {
2061
+ if (!entry) return;
2062
+ entryObserver.observe(entry, { subtree: true, childList: true });
2063
+ };
2064
+ const registerFrameObserver = () => {
2065
+ if (!frame) return;
2066
+ frameObserver.observe(frame, { subtree: true, childList: true });
2067
+ };
2068
+ if (entry) {
2069
+ createHierarchy();
2070
+ registerEntryObserver();
2071
+ } else {
2072
+ registerFrameObserver();
2073
+ }
1775
2074
  return () => {
1776
- observer.disconnect();
2075
+ entryObserver.disconnect();
2076
+ frameObserver.disconnect();
1777
2077
  };
1778
2078
  }, [data]);
1779
2079
  return /* @__PURE__ */ jsxs2("div", { className: getClassName5(), children: [