@measured/puck-plugin-heading-analyzer 0.19.0-canary.cc5dc01 → 0.19.0-canary.d9be813b

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,7 +356,7 @@ init_react_import();
356
356
  // ../core/reducer/index.ts
357
357
  init_react_import();
358
358
 
359
- // ../core/reducer/data.ts
359
+ // ../core/reducer/reduce.ts
360
360
  init_react_import();
361
361
 
362
362
  // ../core/lib/reorder.ts
@@ -368,16 +368,10 @@ var reorder = (list, startIndex, endIndex) => {
368
368
  return result;
369
369
  };
370
370
 
371
- // ../core/lib/root-droppable-id.ts
372
- init_react_import();
373
- var rootAreaId = "root";
374
- var rootZone = "default-zone";
375
- var rootDroppableId = `${rootAreaId}:${rootZone}`;
376
-
377
371
  // ../core/lib/insert.ts
378
372
  init_react_import();
379
373
  var insert = (list, index, item) => {
380
- const result = Array.from(list);
374
+ const result = Array.from(list || []);
381
375
  result.splice(index, 0, item);
382
376
  return result;
383
377
  };
@@ -392,6 +386,14 @@ var remove = (list, index) => {
392
386
 
393
387
  // ../core/lib/setup-zone.ts
394
388
  init_react_import();
389
+
390
+ // ../core/lib/root-droppable-id.ts
391
+ init_react_import();
392
+ var rootAreaId = "root";
393
+ var rootZone = "default-zone";
394
+ var rootDroppableId = `${rootAreaId}:${rootZone}`;
395
+
396
+ // ../core/lib/setup-zone.ts
395
397
  var setupZone = (data, zoneKey) => {
396
398
  if (zoneKey === rootDroppableId) {
397
399
  return data;
@@ -403,29 +405,14 @@ var setupZone = (data, zoneKey) => {
403
405
  return newData;
404
406
  };
405
407
 
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;
413
- };
414
-
415
408
  // ../core/lib/get-item.ts
416
409
  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;
410
+ function getItem(selector, state) {
411
+ var _a, _b;
412
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
413
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
424
414
  }
425
415
 
426
- // ../core/lib/reduce-related-zones.ts
427
- init_react_import();
428
-
429
416
  // ../core/lib/generate-id.ts
430
417
  init_react_import();
431
418
 
@@ -488,6 +475,43 @@ var v4_default = v4;
488
475
  // ../core/lib/generate-id.ts
489
476
  var generateId = (type) => type ? `${type}-${v4_default()}` : v4_default();
490
477
 
478
+ // ../core/lib/walk-tree.ts
479
+ init_react_import();
480
+
481
+ // ../core/lib/for-each-slot.ts
482
+ init_react_import();
483
+
484
+ // ../core/lib/is-slot.ts
485
+ init_react_import();
486
+ var isSlot = (prop) => {
487
+ var _a, _b;
488
+ return Array.isArray(prop) && typeof ((_a = prop[0]) == null ? void 0 : _a.type) === "string" && typeof ((_b = prop[0]) == null ? void 0 : _b.props) === "object";
489
+ };
490
+
491
+ // ../core/lib/for-each-slot.ts
492
+ var forEachSlot = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, cb, recursive = false, isSlot2 = isSlot) {
493
+ const props = item.props || {};
494
+ const propKeys = Object.keys(props);
495
+ for (let i = 0; i < propKeys.length; i++) {
496
+ const propKey = propKeys[i];
497
+ const itemType = "type" in item ? item.type : "root";
498
+ if (isSlot2(itemType, propKey, props[propKey])) {
499
+ const content = props[propKey];
500
+ yield cb(props.id, propKey, content);
501
+ if (recursive) {
502
+ content.forEach(
503
+ (childItem) => __async(void 0, null, function* () {
504
+ return yield forEachSlot(childItem, cb, true, isSlot2);
505
+ })
506
+ );
507
+ }
508
+ }
509
+ }
510
+ });
511
+
512
+ // ../core/lib/for-related-zones.ts
513
+ init_react_import();
514
+
491
515
  // ../core/lib/get-zone-id.ts
492
516
  init_react_import();
493
517
  var getZoneId = (zoneCompound) => {
@@ -500,302 +524,496 @@ var getZoneId = (zoneCompound) => {
500
524
  return [rootDroppableId, zoneCompound];
501
525
  };
502
526
 
503
- // ../core/lib/reduce-related-zones.ts
504
- function reduceRelatedZones(item, data, fn) {
527
+ // ../core/lib/for-related-zones.ts
528
+ function forRelatedZones(item, data, cb, path = []) {
529
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
530
+ const [parentId] = getZoneId(zoneCompound);
531
+ if (parentId === item.props.id) {
532
+ const newPath = [...path, zoneCompound];
533
+ content.forEach((item2) => forRelatedZones(item2, data, cb, newPath));
534
+ cb(path, zoneCompound, content);
535
+ }
536
+ });
537
+ }
538
+
539
+ // ../core/lib/strip-slots.ts
540
+ init_react_import();
541
+ var stripSlots = (data) => {
505
542
  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]);
543
+ props: Object.entries(data.props).reduce(
544
+ (acc, [propKey, propVal]) => {
545
+ if (isSlot(propVal)) {
546
+ return acc;
512
547
  }
513
- return __spreadProps(__spreadValues({}, acc), { [key]: data.zones[key] });
548
+ return __spreadProps(__spreadValues({}, acc), { [propKey]: propVal });
514
549
  },
515
- {}
550
+ { id: data.props.id }
516
551
  )
517
552
  });
518
- }
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 });
526
- }
527
- return __spreadValues(__spreadValues({}, acc), related);
528
- },
529
- {}
530
- );
531
553
  };
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]
540
- });
554
+
555
+ // ../core/lib/walk-tree.ts
556
+ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
557
+ var _a;
558
+ let newZones = {};
559
+ const newZoneIndex = {};
560
+ const newNodeIndex = {};
561
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
562
+ var _a2;
563
+ const [parentId] = zoneCompound.split(":");
564
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
565
+ const [_2, zone] = zoneCompound.split(":");
566
+ const newZoneCompound = `${newId || parentId}:${zone}`;
567
+ const newContent2 = mappedContent.map(
568
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
569
+ );
570
+ newZoneIndex[newZoneCompound] = {
571
+ contentIds: newContent2.map((item) => item.props.id),
572
+ type: zoneType
573
+ };
574
+ return [newZoneCompound, newContent2];
575
+ };
576
+ const processRelatedZones = (item, newId, initialPath) => {
577
+ forRelatedZones(
578
+ item,
579
+ state.data,
580
+ (relatedPath, relatedZoneCompound, relatedContent) => {
581
+ const [zoneCompound, newContent2] = processContent(
582
+ relatedPath,
583
+ relatedZoneCompound,
584
+ relatedContent,
585
+ "dropzone",
586
+ newId
587
+ );
588
+ newZones[zoneCompound] = newContent2;
589
+ },
590
+ initialPath
591
+ );
592
+ };
593
+ const processItem = (item, path, index) => {
594
+ const mappedItem = mapNodeOrSkip(item, path, index);
595
+ if (!mappedItem) return item;
596
+ const id = mappedItem.props.id;
597
+ processRelatedZones(item, id, path);
598
+ const newProps = __spreadValues({}, mappedItem.props);
599
+ forEachSlot(
600
+ mappedItem,
601
+ (parentId2, slotId, content) => {
602
+ const zoneCompound = `${parentId2}:${slotId}`;
603
+ const [_2, newContent2] = processContent(
604
+ path,
605
+ zoneCompound,
606
+ content,
607
+ "slot",
608
+ parentId2
609
+ );
610
+ newProps[slotId] = newContent2;
611
+ },
612
+ false,
613
+ (itemType, propName, propValue) => {
614
+ var _a2, _b;
615
+ const configForComponent = itemType === "root" ? config == null ? void 0 : config.root : config == null ? void 0 : config.components[itemType];
616
+ if (!configForComponent) return isSlot(propValue);
617
+ return ((_b = (_a2 = configForComponent.fields) == null ? void 0 : _a2[propName]) == null ? void 0 : _b.type) === "slot";
541
618
  }
542
- return acc;
619
+ );
620
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
621
+ const thisZoneCompound = path[path.length - 1];
622
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
623
+ newNodeIndex[id] = {
624
+ data: newItem,
625
+ flatData: stripSlots(newItem),
626
+ path,
627
+ parentId,
628
+ zone
629
+ };
630
+ const finalData = newItem;
631
+ if (newProps.id === "root") {
632
+ delete finalData["type"];
633
+ delete finalData.props["id"];
634
+ }
635
+ return finalData;
636
+ };
637
+ const zones = state.data.zones || {};
638
+ const [_, newContent] = processContent(
639
+ [],
640
+ rootDroppableId,
641
+ state.data.content,
642
+ "root"
643
+ );
644
+ const processedContent = newContent;
645
+ Object.keys(zones || {}).forEach((zoneCompound) => {
646
+ const [parentId] = zoneCompound.split(":");
647
+ const [_2, newContent2] = processContent(
648
+ [rootDroppableId],
649
+ zoneCompound,
650
+ zones[zoneCompound],
651
+ "dropzone",
652
+ parentId
653
+ );
654
+ newZones[zoneCompound] = newContent2;
655
+ }, newZones);
656
+ const processedRoot = processItem(
657
+ {
658
+ type: "root",
659
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
543
660
  },
544
- {}
661
+ [],
662
+ -1
545
663
  );
546
- };
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];
664
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
665
+ props: processedRoot.props
552
666
  });
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
- });
667
+ return __spreadProps(__spreadValues({}, state), {
668
+ data: {
669
+ root,
670
+ // root: state.data.root, // TODO changing root causes it's entire subtree to re-render. Let's keep this disabled until the performance issues are resolved in #644.
671
+ content: processedContent,
672
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
673
+ },
674
+ indexes: {
675
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
676
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
677
+ }
569
678
  });
570
679
  }
571
680
 
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)
681
+ // ../core/lib/deindex.ts
682
+ init_react_import();
683
+ var deindex = (state, componentData) => {
684
+ let zones = __spreadValues({}, state.indexes.zones);
685
+ let nodes = __spreadValues({}, state.indexes.nodes);
686
+ const dindexRelatedZones = (item) => {
687
+ forRelatedZones(item, state.data, (_path, zoneCompound, content) => {
688
+ content.forEach((subItem) => {
689
+ dindexChildren(subItem);
690
+ delete nodes[subItem.props.id];
691
+ });
692
+ delete zones[zoneCompound];
578
693
  });
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
- )
588
- })
589
- });
694
+ };
695
+ const dindexChildren = (item) => {
696
+ forEachSlot(
697
+ item,
698
+ (parentId, slotId, content) => {
699
+ const zoneCompound = `${parentId}:${slotId}`;
700
+ delete zones[zoneCompound];
701
+ content.forEach((item2) => {
702
+ dindexRelatedZones(item2);
703
+ delete nodes[item2.props.id];
704
+ });
705
+ },
706
+ true
707
+ );
708
+ };
709
+ dindexRelatedZones(componentData);
710
+ dindexChildren(componentData);
711
+ delete nodes[componentData.props.id];
712
+ return { nodes, zones };
590
713
  };
591
- function insertAction(data, action, config) {
714
+
715
+ // ../core/reducer/reduce.ts
716
+ var zoneCache = {};
717
+ var getIdsForParent = (zoneCompound, state) => {
718
+ const [parentId] = zoneCompound.split(":");
719
+ const node = state.indexes.nodes[parentId];
720
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
721
+ };
722
+ function insertAction(state, action, config) {
723
+ const id = action.id || generateId(action.componentType);
592
724
  const emptyComponentData = {
593
725
  type: action.componentType,
594
726
  props: __spreadProps(__spreadValues({}, config.components[action.componentType].defaultProps || {}), {
595
- id: action.id || generateId(action.componentType)
727
+ id
596
728
  })
597
729
  };
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
- )
615
- })
616
- });
730
+ const [parentId] = action.destinationZone.split(":");
731
+ return walkTree(
732
+ state,
733
+ config,
734
+ (content, zoneCompound) => {
735
+ if (zoneCompound === action.destinationZone) {
736
+ return insert(
737
+ content || [],
738
+ action.destinationIndex,
739
+ emptyComponentData
740
+ );
741
+ }
742
+ return content;
743
+ },
744
+ (childItem) => {
745
+ if (childItem.props.id === id || childItem.props.id === parentId) {
746
+ return childItem;
747
+ }
748
+ return null;
749
+ }
750
+ );
617
751
  }
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
- });
752
+ var reorderAction = (state, action, appStore) => {
753
+ return walkTree(
754
+ state,
755
+ appStore.config,
756
+ (content, zoneCompound) => {
757
+ if (zoneCompound === action.destinationZone) {
758
+ return reorder(
759
+ content || [],
760
+ action.sourceIndex,
761
+ action.destinationIndex
762
+ );
763
+ }
764
+ return content;
765
+ },
766
+ (childItem, path, index) => {
767
+ const zoneCompound = path[path.length - 1];
768
+ if (index === action.sourceIndex && zoneCompound === action.destinationZone || index === action.destinationIndex && zoneCompound === action.destinationZone) {
769
+ return childItem;
770
+ }
771
+ return null;
772
+ }
773
+ );
774
+ };
775
+ var replaceAction = (state, action, appStore) => {
776
+ const [parentId] = action.destinationZone.split(":");
777
+ const idsInPath = getIdsForParent(action.destinationZone, state);
778
+ return walkTree(
779
+ state,
780
+ appStore.config,
781
+ (content) => content,
782
+ (childItem, path) => {
783
+ const pathIds = path.map((p) => p.split(":")[0]);
784
+ if (childItem.props.id === action.data.props.id) {
785
+ return action.data;
786
+ } else if (childItem.props.id === parentId) {
787
+ return childItem;
788
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
789
+ return childItem;
790
+ } else if (pathIds.indexOf(action.data.props.id) > -1) {
791
+ return childItem;
792
+ }
793
+ return null;
794
+ }
795
+ );
796
+ };
797
+ var setAction = (state, action, appStore) => {
798
+ if (typeof action.state === "object") {
799
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
800
+ if (action.state.indexes) {
801
+ console.warn(
802
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
803
+ );
804
+ return newState;
805
+ }
806
+ return walkTree(newState, appStore.config);
627
807
  }
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
- });
808
+ return __spreadValues(__spreadValues({}, state), action.state(state));
638
809
  };
639
- function reduceData(data, action, config) {
810
+ function reduce(state, action, appStore) {
811
+ if (action.type === "set") {
812
+ return setAction(state, action, appStore);
813
+ }
640
814
  if (action.type === "insert") {
641
- return insertAction(data, action, config);
815
+ return insertAction(state, action, appStore.config);
816
+ }
817
+ if (action.type === "replace") {
818
+ return replaceAction(state, action, appStore);
819
+ }
820
+ if (action.type === "replaceRoot") {
821
+ return walkTree(
822
+ state,
823
+ appStore.config,
824
+ (content) => content,
825
+ (childItem) => {
826
+ if (childItem.props.id === "root") {
827
+ return __spreadProps(__spreadValues({}, childItem), {
828
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
829
+ readOnly: action.root.readOnly
830
+ });
831
+ }
832
+ return childItem;
833
+ }
834
+ );
642
835
  }
643
836
  if (action.type === "duplicate") {
644
837
  const item = getItem(
645
838
  { index: action.sourceIndex, zone: action.sourceZone },
646
- data
839
+ state
647
840
  );
841
+ const idsInPath = getIdsForParent(action.sourceZone, state);
648
842
  const newItem = __spreadProps(__spreadValues({}, item), {
649
843
  props: __spreadProps(__spreadValues({}, item.props), {
650
844
  id: generateId(item.type)
651
845
  })
652
846
  });
653
- const dataWithRelatedDuplicated = duplicateRelatedZones(
654
- item,
655
- data,
656
- newItem.props.id
847
+ const modified = walkTree(
848
+ state,
849
+ appStore.config,
850
+ (content, zoneCompound) => {
851
+ if (zoneCompound === action.sourceZone) {
852
+ return insert(content, action.sourceIndex + 1, item);
853
+ }
854
+ return content;
855
+ },
856
+ (childItem, path, index) => {
857
+ const zoneCompound = path[path.length - 1];
858
+ const parents = path.map((p) => p.split(":")[0]);
859
+ if (parents.indexOf(newItem.props.id) > -1) {
860
+ return __spreadProps(__spreadValues({}, childItem), {
861
+ props: __spreadProps(__spreadValues({}, childItem.props), {
862
+ id: generateId(childItem.type)
863
+ })
864
+ });
865
+ }
866
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
867
+ return newItem;
868
+ }
869
+ const [sourceZoneParent] = action.sourceZone.split(":");
870
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
871
+ return childItem;
872
+ }
873
+ return null;
874
+ }
657
875
  );
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
- )
876
+ return __spreadProps(__spreadValues({}, modified), {
877
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
878
+ itemSelector: {
879
+ index: action.sourceIndex + 1,
880
+ zone: action.sourceZone
881
+ }
670
882
  })
671
883
  });
672
884
  }
673
885
  if (action.type === "reorder") {
674
- return reorderAction(data, action);
886
+ return reorderAction(state, action, appStore);
675
887
  }
676
888
  if (action.type === "move") {
677
889
  if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
678
- return data;
890
+ return state;
679
891
  }
680
- const newData = setupZone(
681
- setupZone(data, action.sourceZone),
682
- action.destinationZone
683
- );
684
892
  const item = getItem(
685
893
  { zone: action.sourceZone, index: action.sourceIndex },
686
- newData
894
+ state
687
895
  );
688
- if (action.sourceZone === action.destinationZone) {
689
- return reorderAction(data, __spreadProps(__spreadValues({}, action), { type: "reorder" }));
690
- }
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],
896
+ if (!item) return state;
897
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
898
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
899
+ return walkTree(
900
+ state,
901
+ appStore.config,
902
+ (content, zoneCompound) => {
903
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
904
+ return insert(
905
+ remove(content, action.sourceIndex),
697
906
  action.destinationIndex,
698
907
  item
699
- )
700
- })
701
- });
702
- }
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
- });
713
- }
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);
908
+ );
909
+ } else if (zoneCompound === action.sourceZone) {
910
+ return remove(content, action.sourceIndex);
911
+ } else if (zoneCompound === action.destinationZone) {
912
+ return insert(content, action.destinationIndex, item);
913
+ }
914
+ return content;
915
+ },
916
+ (childItem) => {
917
+ const [sourceZoneParent] = action.sourceZone.split(":");
918
+ const [destinationZoneParent] = action.destinationZone.split(":");
919
+ const childId = childItem.props.id;
920
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1) {
921
+ return childItem;
922
+ }
923
+ return null;
924
+ }
925
+ );
730
926
  }
731
927
  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
928
+ const item = getItem({ index: action.index, zone: action.zone }, state);
929
+ let deindexed = deindex(state, item);
930
+ const [parentId] = action.zone.split(":");
931
+ return walkTree(
932
+ __spreadProps(__spreadValues({}, state), { indexes: deindexed }),
933
+ appStore.config,
934
+ (content, zoneCompound) => {
935
+ if (zoneCompound === action.zone) {
936
+ return remove(content, action.index);
937
+ }
938
+ return content;
939
+ },
940
+ (childItem, path) => {
941
+ const parentIds = path.map((p) => p.split(":")[0]);
942
+ if (childItem.props.id === parentId || childItem.props.id === item.props.id || parentIds.indexOf(item.props.id) > -1) {
943
+ return childItem;
944
+ }
945
+ return null;
946
+ }
736
947
  );
737
- if (action.zone === rootDroppableId) {
738
- return __spreadProps(__spreadValues({}, dataWithRelatedRemoved), {
739
- content: remove(data.content, action.index)
740
- });
741
- }
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
- });
750
948
  }
751
949
  if (action.type === "registerZone") {
752
950
  if (zoneCache[action.zone]) {
753
- return __spreadProps(__spreadValues({}, data), {
754
- zones: __spreadProps(__spreadValues({}, data.zones), {
755
- [action.zone]: zoneCache[action.zone]
951
+ return __spreadProps(__spreadValues({}, state), {
952
+ data: __spreadProps(__spreadValues({}, state.data), {
953
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
954
+ [action.zone]: zoneCache[action.zone]
955
+ })
956
+ }),
957
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
958
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
959
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
960
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
961
+ type: "dropzone"
962
+ })
963
+ })
756
964
  })
757
965
  });
758
966
  }
759
- return setupZone(data, action.zone);
967
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
760
968
  }
761
969
  if (action.type === "unregisterZone") {
762
- const _zones = __spreadValues({}, data.zones || {});
970
+ const _zones = __spreadValues({}, state.data.zones || {});
971
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
763
972
  if (_zones[action.zone]) {
764
973
  zoneCache[action.zone] = _zones[action.zone];
765
974
  delete _zones[action.zone];
766
975
  }
767
- return __spreadProps(__spreadValues({}, data), { zones: _zones });
976
+ delete zoneIndex[action.zone];
977
+ return __spreadProps(__spreadValues({}, state), {
978
+ data: __spreadProps(__spreadValues({}, state.data), {
979
+ zones: _zones
980
+ }),
981
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
982
+ zones: zoneIndex
983
+ })
984
+ });
768
985
  }
769
986
  if (action.type === "setData") {
770
987
  if (typeof action.data === "object") {
771
- return __spreadValues(__spreadValues({}, data), action.data);
988
+ console.warn(
989
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
990
+ );
991
+ return walkTree(
992
+ __spreadProps(__spreadValues({}, state), {
993
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
994
+ }),
995
+ appStore.config
996
+ );
772
997
  }
773
- return __spreadValues(__spreadValues({}, data), action.data(data));
998
+ return walkTree(
999
+ __spreadProps(__spreadValues({}, state), {
1000
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
1001
+ }),
1002
+ appStore.config
1003
+ );
774
1004
  }
775
- return data;
776
- }
777
-
778
- // ../core/reducer/state.ts
779
- init_react_import();
780
- var reduceUi = (ui, action) => {
781
1005
  if (action.type === "setUi") {
782
1006
  if (typeof action.ui === "object") {
783
- return __spreadValues(__spreadValues({}, ui), action.ui);
1007
+ return __spreadProps(__spreadValues({}, state), {
1008
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
1009
+ });
784
1010
  }
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
- });
791
- }
792
- if (action.type === "remove") {
793
- return __spreadProps(__spreadValues({}, ui), {
794
- itemSelector: null
1011
+ return __spreadProps(__spreadValues({}, state), {
1012
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
795
1013
  });
796
1014
  }
797
- return ui;
798
- };
1015
+ return state;
1016
+ }
799
1017
 
800
1018
  // ../core/reducer/actions.tsx
801
1019
  init_react_import();
@@ -818,25 +1036,17 @@ function storeInterceptor(reducer, record, onAction) {
818
1036
  return newAppState;
819
1037
  };
820
1038
  }
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
1039
  function createReducer({
828
1040
  config,
829
1041
  record,
830
- onAction
1042
+ onAction,
1043
+ appStore
831
1044
  }) {
832
1045
  return storeInterceptor(
833
1046
  (state, action) => {
834
- const data = reduceData(state.data, action, config);
835
- const ui = reduceUi(state.ui, action);
836
- if (action.type === "set") {
837
- return setAction(state, action);
838
- }
839
- return { data, ui };
1047
+ const result = reduce(state, action, appStore);
1048
+ console.log(action.type, state, result);
1049
+ return result;
840
1050
  },
841
1051
  record,
842
1052
  onAction
@@ -856,11 +1066,11 @@ init_react_import();
856
1066
  var createStoreImpl = (createState) => {
857
1067
  let state;
858
1068
  const listeners = /* @__PURE__ */ new Set();
859
- const setState = (partial, replace2) => {
1069
+ const setState = (partial, replace) => {
860
1070
  const nextState = typeof partial === "function" ? partial(state) : partial;
861
1071
  if (!Object.is(nextState, state)) {
862
1072
  const previousState = state;
863
- state = (replace2 != null ? replace2 : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
1073
+ state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
864
1074
  listeners.forEach((listener) => listener(state, previousState));
865
1075
  }
866
1076
  };
@@ -924,206 +1134,6 @@ var subscribeWithSelectorImpl = (fn) => (set, get, api) => {
924
1134
  };
925
1135
  var subscribeWithSelector = subscribeWithSelectorImpl;
926
1136
 
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
1137
  // ../core/store/index.ts
1128
1138
  import { createContext, useContext } from "react";
1129
1139
 
@@ -1241,31 +1251,14 @@ var createHistorySlice = (set, get) => {
1241
1251
 
1242
1252
  // ../core/store/slices/nodes.ts
1243
1253
  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
1254
  var createNodesSlice = (set, get) => ({
1254
1255
  nodes: {},
1255
1256
  registerNode: (id, node) => {
1256
1257
  const s = get().nodes;
1257
- if (s.nodes[id] && partialDeepEqual(node, s.nodes[id])) {
1258
- return;
1259
- }
1260
1258
  const emptyNode = {
1261
1259
  id,
1262
1260
  methods: { sync: () => null },
1263
- data: { props: { id }, type: "unknown" },
1264
- parentId: "",
1265
- zone: "",
1266
- path: [],
1267
- element: null,
1268
- index: -1
1261
+ element: null
1269
1262
  };
1270
1263
  const existingNode = s.nodes[id];
1271
1264
  set({
@@ -1295,36 +1288,66 @@ var createNodesSlice = (set, get) => ({
1295
1288
 
1296
1289
  // ../core/store/slices/permissions.ts
1297
1290
  init_react_import();
1298
- import { useEffect as useEffect4 } from "react";
1291
+ import { useEffect as useEffect3 } from "react";
1292
+
1293
+ // ../core/lib/flatten-data.ts
1294
+ init_react_import();
1295
+ var flattenData = (state, config) => {
1296
+ const data = [];
1297
+ walkTree(
1298
+ state,
1299
+ config,
1300
+ (content) => content,
1301
+ (item) => {
1302
+ data.push(item);
1303
+ return null;
1304
+ }
1305
+ );
1306
+ return data;
1307
+ };
1308
+
1309
+ // ../core/lib/get-changed.ts
1310
+ init_react_import();
1311
+ var getChanged = (newItem, oldItem) => {
1312
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
1313
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
1314
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
1315
+ return __spreadProps(__spreadValues({}, acc), {
1316
+ [item]: oldItemProps[item] !== newItemProps[item]
1317
+ });
1318
+ }, {}) : {};
1319
+ };
1320
+
1321
+ // ../core/store/slices/permissions.ts
1299
1322
  var createPermissionsSlice = (set, get) => {
1300
1323
  const resolvePermissions = (..._0) => __async(void 0, [..._0], function* (params = {}, force) {
1301
- const { state, permissions } = get();
1302
- const { cache: cache3, globalPermissions } = permissions;
1324
+ const { state, permissions, config } = get();
1325
+ const { cache: cache2, globalPermissions } = permissions;
1303
1326
  const resolveDataForItem = (item2, force2 = false) => __async(void 0, null, function* () {
1304
1327
  var _a, _b, _c;
1305
1328
  const {
1306
- config,
1329
+ config: config2,
1307
1330
  state: appState,
1308
1331
  setComponentLoading,
1309
1332
  unsetComponentLoading
1310
1333
  } = get();
1311
- const componentConfig = item2.type === "root" ? config.root : config.components[item2.type];
1334
+ const componentConfig = item2.type === "root" ? config2.root : config2.components[item2.type];
1312
1335
  if (!componentConfig) {
1313
1336
  return;
1314
1337
  }
1315
1338
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), componentConfig.permissions);
1316
1339
  if (componentConfig.resolvePermissions) {
1317
- const changed = getChanged(item2, (_a = cache3[item2.props.id]) == null ? void 0 : _a.lastData);
1340
+ const changed = getChanged(item2, (_a = cache2[item2.props.id]) == null ? void 0 : _a.lastData);
1318
1341
  if (Object.values(changed).some((el) => el === true) || force2) {
1319
1342
  setComponentLoading(item2.props.id);
1320
1343
  const resolvedPermissions = yield componentConfig.resolvePermissions(
1321
1344
  item2,
1322
1345
  {
1323
1346
  changed,
1324
- lastPermissions: ((_b = cache3[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1347
+ lastPermissions: ((_b = cache2[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1325
1348
  permissions: initialPermissions,
1326
1349
  appState,
1327
- lastData: ((_c = cache3[item2.props.id]) == null ? void 0 : _c.lastData) || null
1350
+ lastData: ((_c = cache2[item2.props.id]) == null ? void 0 : _c.lastData) || null
1328
1351
  }
1329
1352
  );
1330
1353
  const latest = get().permissions;
@@ -1360,14 +1383,14 @@ var createPermissionsSlice = (set, get) => {
1360
1383
  if (item) {
1361
1384
  yield resolveDataForItem(item, force);
1362
1385
  } else if (type) {
1363
- flattenData(state.data).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1386
+ flattenData(state, config).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1364
1387
  yield resolveDataForItem(item2, force);
1365
1388
  }));
1366
1389
  } else if (root) {
1367
1390
  resolveDataForRoot(force);
1368
1391
  } else {
1369
1392
  resolveDataForRoot(force);
1370
- flattenData(state.data).map((item2) => __async(void 0, null, function* () {
1393
+ flattenData(state, config).map((item2) => __async(void 0, null, function* () {
1371
1394
  yield resolveDataForItem(item2, force);
1372
1395
  }));
1373
1396
  }
@@ -1409,8 +1432,8 @@ var createPermissionsSlice = (set, get) => {
1409
1432
 
1410
1433
  // ../core/store/slices/fields.ts
1411
1434
  init_react_import();
1412
- import { useCallback, useEffect as useEffect5 } from "react";
1413
- var createFieldsStore = (_set, _get) => {
1435
+ import { useCallback, useEffect as useEffect4 } from "react";
1436
+ var createFieldsSlice = (_set, _get) => {
1414
1437
  return {
1415
1438
  fields: {},
1416
1439
  loading: false,
@@ -1418,6 +1441,106 @@ var createFieldsStore = (_set, _get) => {
1418
1441
  };
1419
1442
  };
1420
1443
 
1444
+ // ../core/lib/resolve-component-data.ts
1445
+ init_react_import();
1446
+
1447
+ // ../core/lib/map-slots.ts
1448
+ init_react_import();
1449
+ function mapSlots(item, map, recursive = true, isSlot2) {
1450
+ return __async(this, null, function* () {
1451
+ const props = __spreadValues({}, item.props);
1452
+ yield forEachSlot(
1453
+ item,
1454
+ (_parentId, propName, content) => __async(this, null, function* () {
1455
+ const mappedContent = recursive ? yield Promise.all(
1456
+ content.map((item2) => __async(this, null, function* () {
1457
+ return yield mapSlots(item2, map, recursive, isSlot2);
1458
+ }))
1459
+ ) : content;
1460
+ props[propName] = yield map(mappedContent, propName);
1461
+ }),
1462
+ false,
1463
+ isSlot2
1464
+ );
1465
+ return __spreadProps(__spreadValues({}, item), { props });
1466
+ });
1467
+ }
1468
+
1469
+ // ../core/lib/resolve-component-data.ts
1470
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1471
+ var cache = { lastChange: {} };
1472
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace", recursive = true) {
1473
+ const configForItem = "type" in item ? config.components[item.type] : config.root;
1474
+ if ((configForItem == null ? void 0 : configForItem.resolveData) && item.props) {
1475
+ const id = "id" in item.props ? item.props.id : "root";
1476
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
1477
+ if (item && item === oldItem) {
1478
+ return resolved;
1479
+ }
1480
+ const changed = getChanged(item, oldItem);
1481
+ if (onResolveStart) {
1482
+ onResolveStart(item);
1483
+ }
1484
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
1485
+ changed,
1486
+ lastData: oldItem,
1487
+ metadata,
1488
+ trigger
1489
+ });
1490
+ let resolvedItem = __spreadProps(__spreadValues({}, item), {
1491
+ props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
1492
+ });
1493
+ if (recursive) {
1494
+ resolvedItem = yield mapSlots(resolvedItem, (content) => __async(void 0, null, function* () {
1495
+ return Promise.all(
1496
+ content.map(
1497
+ (childItem) => __async(void 0, null, function* () {
1498
+ return (yield resolveComponentData(
1499
+ childItem,
1500
+ config,
1501
+ metadata,
1502
+ onResolveStart,
1503
+ onResolveEnd,
1504
+ trigger,
1505
+ false
1506
+ )).node;
1507
+ })
1508
+ )
1509
+ );
1510
+ }));
1511
+ }
1512
+ if (Object.keys(readOnly).length) {
1513
+ resolvedItem.readOnly = readOnly;
1514
+ }
1515
+ cache.lastChange[id] = {
1516
+ item,
1517
+ resolved: resolvedItem
1518
+ };
1519
+ if (onResolveEnd) {
1520
+ onResolveEnd(resolvedItem);
1521
+ }
1522
+ return { node: resolvedItem, didChange: !(0, import_fast_deep_equal.default)(item, resolvedItem) };
1523
+ }
1524
+ return { node: item, didChange: false };
1525
+ });
1526
+
1527
+ // ../core/lib/to-root.ts
1528
+ init_react_import();
1529
+ var toRoot = (item) => {
1530
+ if ("type" in item && item.type !== "root") {
1531
+ throw new Error("Converting non-root item to root.");
1532
+ }
1533
+ const { readOnly } = item;
1534
+ if (item.props) {
1535
+ if ("id" in item.props) {
1536
+ const _a = item.props, { id } = _a, props = __objRest(_a, ["id"]);
1537
+ return { props, readOnly };
1538
+ }
1539
+ return { props: item.props, readOnly };
1540
+ }
1541
+ return { props: {}, readOnly };
1542
+ };
1543
+
1421
1544
  // ../core/store/index.ts
1422
1545
  var defaultAppState = {
1423
1546
  data: { content: [], root: {}, zones: {} },
@@ -1438,6 +1561,10 @@ var defaultAppState = {
1438
1561
  controlsVisible: true
1439
1562
  },
1440
1563
  field: { focus: null }
1564
+ },
1565
+ indexes: {
1566
+ nodes: {},
1567
+ zones: {}
1441
1568
  }
1442
1569
  };
1443
1570
  var defaultPageFields = {
@@ -1460,7 +1587,7 @@ var createAppStore = (initialAppStore) => create()(
1460
1587
  iframe: {},
1461
1588
  metadata: {}
1462
1589
  }, initialAppStore), {
1463
- fields: createFieldsStore(set, get),
1590
+ fields: createFieldsSlice(set, get),
1464
1591
  history: createHistorySlice(set, get),
1465
1592
  nodes: createNodesSlice(set, get),
1466
1593
  permissions: createPermissionsSlice(set, get),
@@ -1473,55 +1600,143 @@ var createAppStore = (initialAppStore) => create()(
1473
1600
  dispatch: (action) => set((s) => {
1474
1601
  var _a, _b;
1475
1602
  const { record } = get().history;
1476
- const dispatch = createReducer({ config: s.config, record });
1603
+ const dispatch = createReducer({
1604
+ config: s.config,
1605
+ record,
1606
+ appStore: s
1607
+ });
1477
1608
  const state = dispatch(s.state, action);
1478
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state.data) : null;
1609
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1479
1610
  (_b = (_a = get()).onAction) == null ? void 0 : _b.call(_a, action, state, get().state);
1480
1611
  return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1481
1612
  }),
1482
1613
  setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1483
1614
  setStatus: (status) => set({ status }),
1484
1615
  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
1616
+ pendingComponentLoads: {},
1617
+ setComponentLoading: (id, loading = true, defer = 0) => {
1618
+ const { setComponentState, pendingComponentLoads } = get();
1619
+ const thisPendingComponentLoads = __spreadValues({}, pendingComponentLoads);
1620
+ const setLoading = () => {
1621
+ var _a;
1622
+ const { componentState } = get();
1623
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1624
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1625
+ loadingCount: (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) + 1
1626
+ })
1627
+ }));
1628
+ };
1629
+ const unsetLoading = () => {
1630
+ var _a;
1631
+ const { componentState } = get();
1632
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1633
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1634
+ loadingCount: Math.max(
1635
+ (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) - 1,
1636
+ 0
1637
+ )
1638
+ })
1639
+ }));
1640
+ };
1641
+ if (thisPendingComponentLoads[id]) {
1642
+ clearTimeout(thisPendingComponentLoads[id]);
1643
+ delete thisPendingComponentLoads[id];
1644
+ set({ pendingComponentLoads: thisPendingComponentLoads });
1645
+ }
1646
+ const timeout = setTimeout(() => {
1647
+ if (loading) {
1648
+ setLoading();
1649
+ } else {
1650
+ unsetLoading();
1651
+ }
1652
+ delete thisPendingComponentLoads[id];
1653
+ set({ pendingComponentLoads: thisPendingComponentLoads });
1654
+ }, defer);
1655
+ set({
1656
+ pendingComponentLoads: __spreadProps(__spreadValues({}, thisPendingComponentLoads), {
1657
+ [id]: timeout
1491
1658
  })
1492
- }));
1659
+ });
1493
1660
  },
1494
1661
  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
- }));
1662
+ const { setComponentLoading } = get();
1663
+ setComponentLoading(id, false);
1505
1664
  },
1506
1665
  // Helper
1507
1666
  setUi: (ui, recordHistory) => set((s) => {
1508
1667
  const dispatch = createReducer({
1509
1668
  config: s.config,
1510
1669
  record: () => {
1511
- }
1670
+ },
1671
+ appStore: s
1512
1672
  });
1513
1673
  const state = dispatch(s.state, {
1514
1674
  type: "setUi",
1515
1675
  ui,
1516
1676
  recordHistory
1517
1677
  });
1518
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state.data) : null;
1678
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1519
1679
  return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1520
1680
  }),
1521
- resolveDataRuns: 0,
1522
- resolveData: (newAppState) => set((s) => {
1523
- resolveData(newAppState, get());
1524
- return __spreadProps(__spreadValues({}, s), { resolveDataRuns: s.resolveDataRuns + 1 });
1681
+ // resolveDataRuns: 0,
1682
+ // resolveData: (newAppState) =>
1683
+ // set((s) => {
1684
+ // resolveData(newAppState, get);
1685
+ // return { ...s, resolveDataRuns: s.resolveDataRuns + 1 };
1686
+ // }),
1687
+ resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
1688
+ const { config, metadata, setComponentLoading } = get();
1689
+ return yield resolveComponentData(
1690
+ componentData,
1691
+ config,
1692
+ metadata,
1693
+ (item) => setComponentLoading(
1694
+ "id" in item.props ? item.props.id : "root",
1695
+ true,
1696
+ 50
1697
+ ),
1698
+ (item) => setComponentLoading(
1699
+ "id" in item.props ? item.props.id : "root",
1700
+ false,
1701
+ 0
1702
+ ),
1703
+ trigger
1704
+ );
1705
+ }),
1706
+ resolveAndCommitData: () => __async(void 0, null, function* () {
1707
+ const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
1708
+ walkTree(
1709
+ state,
1710
+ config,
1711
+ (content) => content,
1712
+ (childItem) => {
1713
+ resolveComponentData2(childItem, "load").then((resolved) => {
1714
+ const { state: state2 } = get();
1715
+ const node = state2.indexes.nodes[resolved.node.props.id];
1716
+ if (node && resolved.didChange) {
1717
+ if (resolved.node.props.id === "root") {
1718
+ dispatch({
1719
+ type: "replaceRoot",
1720
+ root: toRoot(resolved.node)
1721
+ });
1722
+ } else {
1723
+ const zoneCompound = `${node.parentId}:${node.zone}`;
1724
+ const parentZone = state2.indexes.zones[zoneCompound];
1725
+ const index = parentZone.contentIds.indexOf(
1726
+ resolved.node.props.id
1727
+ );
1728
+ dispatch({
1729
+ type: "replace",
1730
+ data: resolved.node,
1731
+ destinationIndex: index,
1732
+ destinationZone: zoneCompound
1733
+ });
1734
+ }
1735
+ }
1736
+ });
1737
+ return childItem;
1738
+ }
1739
+ );
1525
1740
  })
1526
1741
  }))
1527
1742
  );
@@ -1543,12 +1758,12 @@ var useBreadcrumbs = (renderCount) => {
1543
1758
  const config = useAppStore((s) => s.config);
1544
1759
  const path = useAppStore((s) => {
1545
1760
  var _a;
1546
- return (_a = s.nodes.nodes[selectedId]) == null ? void 0 : _a.path;
1761
+ return (_a = s.state.indexes.nodes[selectedId]) == null ? void 0 : _a.path;
1547
1762
  });
1548
1763
  const appStore = useAppStoreApi();
1549
1764
  return useMemo(() => {
1550
1765
  const breadcrumbs = (path == null ? void 0 : path.map((zoneCompound) => {
1551
- var _a, _b;
1766
+ var _a, _b, _c;
1552
1767
  const [componentId] = zoneCompound.split(":");
1553
1768
  if (componentId === "root") {
1554
1769
  return {
@@ -1556,12 +1771,15 @@ var useBreadcrumbs = (renderCount) => {
1556
1771
  selector: null
1557
1772
  };
1558
1773
  }
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";
1774
+ const node = appStore.getState().state.indexes.nodes[componentId];
1775
+ const parentId = node.path[node.path.length - 1];
1776
+ const contentIds = ((_a = appStore.getState().state.indexes.zones[parentId]) == null ? void 0 : _a.contentIds) || [];
1777
+ const index = contentIds.indexOf(componentId);
1778
+ const label = node ? (_c = (_b = config.components[node.data.type]) == null ? void 0 : _b.label) != null ? _c : node.data.type : "Component";
1561
1779
  return {
1562
1780
  label,
1563
1781
  selector: node ? {
1564
- index: node.index,
1782
+ index,
1565
1783
  zone: node.path[node.path.length - 1]
1566
1784
  } : null
1567
1785
  };
@@ -1582,6 +1800,9 @@ init_react_import();
1582
1800
  // ../core/lib/filter.ts
1583
1801
  init_react_import();
1584
1802
 
1803
+ // ../core/lib/replace.ts
1804
+ init_react_import();
1805
+
1585
1806
  // css-module:/home/runner/work/puck/puck/packages/core/components/Loader/styles.module.css#css-module
1586
1807
  init_react_import();
1587
1808
  var styles_module_default3 = { "Loader": "_Loader_nacdm_13", "loader-animation": "_loader-animation_nacdm_1" };
@@ -1763,7 +1984,7 @@ var usePuck = createUsePuck();
1763
1984
  var HeadingAnalyzer = () => {
1764
1985
  const data = usePuck((s) => s.appState.data);
1765
1986
  const [hierarchy, setHierarchy] = useState([]);
1766
- useEffect6(() => {
1987
+ useEffect5(() => {
1767
1988
  const frame = getFrame();
1768
1989
  const entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
1769
1990
  if (!entry) return;