@measured/puck-plugin-heading-analyzer 0.19.0-canary.59cb0d9 → 0.19.0-canary.61865257

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -199,7 +199,7 @@ init_react_import();
199
199
 
200
200
  // src/HeadingAnalyzer.tsx
201
201
  init_react_import();
202
- var import_react12 = require("react");
202
+ var import_react11 = require("react");
203
203
 
204
204
  // css-module:/home/runner/work/puck/puck/packages/plugin-heading-analyzer/src/HeadingAnalyzer.module.css#css-module
205
205
  init_react_import();
@@ -360,7 +360,7 @@ var ChevronRight = createLucideIcon("ChevronRight", [
360
360
 
361
361
  // ../core/lib/use-breadcrumbs.ts
362
362
  init_react_import();
363
- var import_react11 = require("react");
363
+ var import_react10 = require("react");
364
364
 
365
365
  // ../core/store/index.ts
366
366
  init_react_import();
@@ -368,75 +368,247 @@ init_react_import();
368
368
  // ../core/reducer/index.ts
369
369
  init_react_import();
370
370
 
371
- // ../core/reducer/data.ts
371
+ // ../core/reducer/actions/set.ts
372
372
  init_react_import();
373
373
 
374
- // ../core/lib/reorder.ts
374
+ // ../core/lib/data/walk-app-state.ts
375
375
  init_react_import();
376
- var reorder = (list, startIndex, endIndex) => {
377
- const result = Array.from(list);
378
- const [removed] = result.splice(startIndex, 1);
379
- result.splice(endIndex, 0, removed);
380
- return result;
376
+
377
+ // ../core/lib/data/for-each-slot.ts
378
+ init_react_import();
379
+
380
+ // ../core/lib/data/is-slot.ts
381
+ init_react_import();
382
+ var isSlot = (prop) => {
383
+ var _a, _b;
384
+ return Array.isArray(prop) && typeof ((_a = prop[0]) == null ? void 0 : _a.type) === "string" && typeof ((_b = prop[0]) == null ? void 0 : _b.props) === "object";
385
+ };
386
+ var createIsSlotConfig = (config) => (itemType, propName, propValue) => {
387
+ var _a, _b;
388
+ const configForComponent = itemType === "root" ? config == null ? void 0 : config.root : config == null ? void 0 : config.components[itemType];
389
+ if (!configForComponent) return isSlot(propValue);
390
+ return ((_b = (_a = configForComponent.fields) == null ? void 0 : _a[propName]) == null ? void 0 : _b.type) === "slot";
391
+ };
392
+
393
+ // ../core/lib/data/for-each-slot.ts
394
+ var forEachSlot = (item, cb, recursive = false, isSlot2 = isSlot) => {
395
+ const props = item.props || {};
396
+ const propKeys = Object.keys(props);
397
+ for (let i = 0; i < propKeys.length; i++) {
398
+ const propKey = propKeys[i];
399
+ const itemType = "type" in item ? item.type : "root";
400
+ if (isSlot2(itemType, propKey, props[propKey])) {
401
+ const content = props[propKey];
402
+ cb(props.id, propKey, content);
403
+ if (recursive) {
404
+ content.forEach(
405
+ (childItem) => __async(void 0, null, function* () {
406
+ return forEachSlot(childItem, cb, true, isSlot2);
407
+ })
408
+ );
409
+ }
410
+ }
411
+ }
381
412
  };
382
413
 
414
+ // ../core/lib/data/for-related-zones.ts
415
+ init_react_import();
416
+
417
+ // ../core/lib/get-zone-id.ts
418
+ init_react_import();
419
+
383
420
  // ../core/lib/root-droppable-id.ts
384
421
  init_react_import();
385
422
  var rootAreaId = "root";
386
423
  var rootZone = "default-zone";
387
424
  var rootDroppableId = `${rootAreaId}:${rootZone}`;
388
425
 
389
- // ../core/lib/insert.ts
390
- init_react_import();
391
- var insert = (list, index, item) => {
392
- const result = Array.from(list);
393
- result.splice(index, 0, item);
394
- return result;
426
+ // ../core/lib/get-zone-id.ts
427
+ var getZoneId = (zoneCompound) => {
428
+ if (!zoneCompound) {
429
+ return [];
430
+ }
431
+ if (zoneCompound && zoneCompound.indexOf(":") > -1) {
432
+ return zoneCompound.split(":");
433
+ }
434
+ return [rootDroppableId, zoneCompound];
395
435
  };
396
436
 
397
- // ../core/lib/remove.ts
398
- init_react_import();
399
- var remove = (list, index) => {
400
- const result = Array.from(list);
401
- result.splice(index, 1);
402
- return result;
403
- };
437
+ // ../core/lib/data/for-related-zones.ts
438
+ function forRelatedZones(item, data, cb, path = []) {
439
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
440
+ const [parentId] = getZoneId(zoneCompound);
441
+ if (parentId === item.props.id) {
442
+ cb(path, zoneCompound, content);
443
+ }
444
+ });
445
+ }
404
446
 
405
- // ../core/lib/setup-zone.ts
447
+ // ../core/lib/data/strip-slots.ts
406
448
  init_react_import();
407
- var setupZone = (data, zoneKey) => {
408
- if (zoneKey === rootDroppableId) {
409
- return data;
410
- }
411
- const newData = __spreadProps(__spreadValues({}, data), {
412
- zones: data.zones ? __spreadValues({}, data.zones) : {}
449
+ var stripSlots = (data) => {
450
+ return __spreadProps(__spreadValues({}, data), {
451
+ props: Object.entries(data.props).reduce(
452
+ (acc, [propKey, propVal]) => {
453
+ if (isSlot(propVal)) {
454
+ return acc;
455
+ }
456
+ return __spreadProps(__spreadValues({}, acc), { [propKey]: propVal });
457
+ },
458
+ { id: data.props.id }
459
+ )
413
460
  });
414
- newData.zones[zoneKey] = newData.zones[zoneKey] || [];
415
- return newData;
416
461
  };
417
462
 
418
- // ../core/lib/replace.ts
419
- init_react_import();
420
- var replace = (list, index, newItem) => {
421
- const result = Array.from(list);
422
- result.splice(index, 1);
423
- result.splice(index, 0, newItem);
424
- return result;
463
+ // ../core/lib/data/walk-app-state.ts
464
+ function walkAppState(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
465
+ var _a;
466
+ let newZones = {};
467
+ const newZoneIndex = {};
468
+ const newNodeIndex = {};
469
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
470
+ var _a2;
471
+ const [parentId] = zoneCompound.split(":");
472
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
473
+ const [_2, zone] = zoneCompound.split(":");
474
+ const newZoneCompound = `${newId || parentId}:${zone}`;
475
+ const newContent2 = mappedContent.map(
476
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
477
+ );
478
+ newZoneIndex[newZoneCompound] = {
479
+ contentIds: newContent2.map((item) => item.props.id),
480
+ type: zoneType
481
+ };
482
+ return [newZoneCompound, newContent2];
483
+ };
484
+ const processRelatedZones = (item, newId, initialPath) => {
485
+ forRelatedZones(
486
+ item,
487
+ state.data,
488
+ (relatedPath, relatedZoneCompound, relatedContent) => {
489
+ const [zoneCompound, newContent2] = processContent(
490
+ relatedPath,
491
+ relatedZoneCompound,
492
+ relatedContent,
493
+ "dropzone",
494
+ newId
495
+ );
496
+ newZones[zoneCompound] = newContent2;
497
+ },
498
+ initialPath
499
+ );
500
+ };
501
+ const processItem = (item, path, index) => {
502
+ const mappedItem = mapNodeOrSkip(item, path, index);
503
+ if (!mappedItem) return item;
504
+ const id = mappedItem.props.id;
505
+ const newProps = __spreadValues({}, mappedItem.props);
506
+ forEachSlot(
507
+ mappedItem,
508
+ (parentId2, slotId, content) => {
509
+ const zoneCompound = `${parentId2}:${slotId}`;
510
+ const [_2, newContent2] = processContent(
511
+ path,
512
+ zoneCompound,
513
+ content,
514
+ "slot",
515
+ parentId2
516
+ );
517
+ newProps[slotId] = newContent2;
518
+ },
519
+ false,
520
+ createIsSlotConfig(config)
521
+ );
522
+ processRelatedZones(item, id, path);
523
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
524
+ const thisZoneCompound = path[path.length - 1];
525
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
526
+ newNodeIndex[id] = {
527
+ data: newItem,
528
+ flatData: stripSlots(newItem),
529
+ path,
530
+ parentId,
531
+ zone
532
+ };
533
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
534
+ if (newProps.id === "root") {
535
+ delete finalData["type"];
536
+ delete finalData.props["id"];
537
+ }
538
+ return finalData;
539
+ };
540
+ const zones = state.data.zones || {};
541
+ const [_, newContent] = processContent(
542
+ [],
543
+ rootDroppableId,
544
+ state.data.content,
545
+ "root"
546
+ );
547
+ const processedContent = newContent;
548
+ const zonesAlreadyProcessed = Object.keys(newZones);
549
+ Object.keys(zones || {}).forEach((zoneCompound) => {
550
+ const [parentId] = zoneCompound.split(":");
551
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
552
+ return;
553
+ }
554
+ const [_2, newContent2] = processContent(
555
+ [rootDroppableId],
556
+ zoneCompound,
557
+ zones[zoneCompound],
558
+ "dropzone",
559
+ parentId
560
+ );
561
+ newZones[zoneCompound] = newContent2;
562
+ }, newZones);
563
+ const processedRoot = processItem(
564
+ {
565
+ type: "root",
566
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
567
+ },
568
+ [],
569
+ -1
570
+ );
571
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
572
+ props: processedRoot.props
573
+ });
574
+ return __spreadProps(__spreadValues({}, state), {
575
+ data: {
576
+ root,
577
+ content: processedContent,
578
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
579
+ },
580
+ indexes: {
581
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
582
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
583
+ }
584
+ });
585
+ }
586
+
587
+ // ../core/reducer/actions/set.ts
588
+ var setAction = (state, action, appStore) => {
589
+ if (typeof action.state === "object") {
590
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
591
+ if (action.state.indexes) {
592
+ return newState;
593
+ }
594
+ console.warn(
595
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
596
+ );
597
+ return walkAppState(newState, appStore.config);
598
+ }
599
+ return __spreadValues(__spreadValues({}, state), action.state(state));
425
600
  };
426
601
 
427
- // ../core/lib/get-item.ts
602
+ // ../core/reducer/actions/insert.ts
428
603
  init_react_import();
429
- function getItem(selector, data, dynamicProps = {}) {
430
- if (!selector.zone || selector.zone === rootDroppableId) {
431
- const item2 = data.content[selector.index];
432
- return (item2 == null ? void 0 : item2.props) ? __spreadProps(__spreadValues({}, item2), { props: dynamicProps[item2.props.id] || item2.props }) : void 0;
433
- }
434
- const item = setupZone(data, selector.zone).zones[selector.zone][selector.index];
435
- return (item == null ? void 0 : item.props) ? __spreadProps(__spreadValues({}, item), { props: dynamicProps[item.props.id] || item.props }) : void 0;
436
- }
437
604
 
438
- // ../core/lib/reduce-related-zones.ts
605
+ // ../core/lib/data/insert.ts
439
606
  init_react_import();
607
+ var insert = (list, index, item) => {
608
+ const result = Array.from(list || []);
609
+ result.splice(index, 0, item);
610
+ return result;
611
+ };
440
612
 
441
613
  // ../core/lib/generate-id.ts
442
614
  init_react_import();
@@ -500,313 +672,477 @@ var v4_default = v4;
500
672
  // ../core/lib/generate-id.ts
501
673
  var generateId = (type) => type ? `${type}-${v4_default()}` : v4_default();
502
674
 
503
- // ../core/lib/get-zone-id.ts
675
+ // ../core/lib/data/get-ids-for-parent.ts
504
676
  init_react_import();
505
- var getZoneId = (zoneCompound) => {
506
- if (!zoneCompound) {
507
- return [];
508
- }
509
- if (zoneCompound && zoneCompound.indexOf(":") > -1) {
510
- return zoneCompound.split(":");
511
- }
512
- return [rootDroppableId, zoneCompound];
677
+ var getIdsForParent = (zoneCompound, state) => {
678
+ const [parentId] = zoneCompound.split(":");
679
+ const node = state.indexes.nodes[parentId];
680
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
513
681
  };
514
682
 
515
- // ../core/lib/reduce-related-zones.ts
516
- function reduceRelatedZones(item, data, fn) {
517
- return __spreadProps(__spreadValues({}, data), {
518
- zones: Object.keys(data.zones || {}).reduce(
519
- (acc, key) => {
520
- const [parentId] = getZoneId(key);
521
- if (parentId === item.props.id) {
522
- const zones = data.zones;
523
- return fn(acc, key, zones[key]);
524
- }
525
- return __spreadProps(__spreadValues({}, acc), { [key]: data.zones[key] });
526
- },
683
+ // ../core/lib/data/populate-ids.ts
684
+ init_react_import();
685
+
686
+ // ../core/lib/data/walk-tree.ts
687
+ init_react_import();
688
+
689
+ // ../core/lib/data/map-slots.ts
690
+ init_react_import();
691
+ function mapSlotsAsync(_0, _1) {
692
+ return __async(this, arguments, function* (item, map, recursive = true, isSlot2 = isSlot) {
693
+ const props = __spreadValues({}, item.props);
694
+ const propKeys = Object.keys(props);
695
+ for (let i = 0; i < propKeys.length; i++) {
696
+ const propKey = propKeys[i];
697
+ const itemType = "type" in item ? item.type : "root";
698
+ if (isSlot2(itemType, propKey, props[propKey])) {
699
+ const content = props[propKey];
700
+ const mappedContent = recursive ? yield Promise.all(
701
+ content.map((item2) => __async(this, null, function* () {
702
+ return yield mapSlotsAsync(item2, map, recursive, isSlot2);
703
+ }))
704
+ ) : content;
705
+ props[propKey] = yield map(mappedContent, propKey);
706
+ }
707
+ }
708
+ return __spreadProps(__spreadValues({}, item), { props });
709
+ });
710
+ }
711
+ function mapSlotsSync(item, map, isSlot2 = isSlot) {
712
+ var _a, _b;
713
+ const props = __spreadValues({}, item.props);
714
+ const propKeys = Object.keys(props);
715
+ for (let i = 0; i < propKeys.length; i++) {
716
+ const propKey = propKeys[i];
717
+ const itemType = "type" in item ? item.type : "root";
718
+ if (isSlot2(itemType, propKey, props[propKey])) {
719
+ const content = props[propKey];
720
+ const mappedContent = content.map((item2) => {
721
+ return mapSlotsSync(item2, map, isSlot2);
722
+ });
723
+ props[propKey] = (_b = map(mappedContent, (_a = props.id) != null ? _a : "root", propKey)) != null ? _b : mappedContent;
724
+ }
725
+ }
726
+ return __spreadProps(__spreadValues({}, item), { props });
727
+ }
728
+
729
+ // ../core/lib/data/walk-tree.ts
730
+ function walkTree(data, config, callbackFn) {
731
+ var _a, _b;
732
+ const isSlot2 = createIsSlotConfig(config);
733
+ const walkItem = (item) => {
734
+ return mapSlotsSync(
735
+ item,
736
+ (content, parentId, propName) => callbackFn(content, { parentId, propName }),
737
+ isSlot2
738
+ );
739
+ };
740
+ if ("props" in data) {
741
+ return walkItem(data);
742
+ }
743
+ const _data = data;
744
+ const zones = (_a = _data.zones) != null ? _a : {};
745
+ const mappedContent = _data.content.map(walkItem);
746
+ return {
747
+ root: walkItem(_data.root),
748
+ content: (_b = callbackFn(mappedContent, {
749
+ parentId: "root",
750
+ propName: "default-zone"
751
+ })) != null ? _b : mappedContent,
752
+ zones: Object.keys(zones).reduce(
753
+ (acc, zoneCompound) => __spreadProps(__spreadValues({}, acc), {
754
+ [zoneCompound]: zones[zoneCompound].map(walkItem)
755
+ }),
527
756
  {}
528
757
  )
529
- });
758
+ };
759
+ }
760
+
761
+ // ../core/lib/data/populate-ids.ts
762
+ var populateIds = (data, config, override = false) => {
763
+ const id = generateId(data.type);
764
+ return walkTree(
765
+ __spreadProps(__spreadValues({}, data), {
766
+ props: override ? __spreadProps(__spreadValues({}, data.props), { id }) : __spreadValues({ id }, data.props)
767
+ }),
768
+ config,
769
+ (contents) => contents.map((item) => {
770
+ const id2 = generateId(item.type);
771
+ return __spreadProps(__spreadValues({}, item), {
772
+ props: override ? __spreadProps(__spreadValues({}, item.props), { id: id2 }) : __spreadValues({ id: id2 }, item.props)
773
+ });
774
+ })
775
+ );
776
+ };
777
+
778
+ // ../core/reducer/actions/insert.ts
779
+ function insertAction(state, action, appStore) {
780
+ const id = action.id || generateId(action.componentType);
781
+ const emptyComponentData = populateIds(
782
+ {
783
+ type: action.componentType,
784
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
785
+ id
786
+ })
787
+ },
788
+ appStore.config
789
+ );
790
+ const [parentId] = action.destinationZone.split(":");
791
+ const idsInPath = getIdsForParent(action.destinationZone, state);
792
+ return walkAppState(
793
+ state,
794
+ appStore.config,
795
+ (content, zoneCompound) => {
796
+ if (zoneCompound === action.destinationZone) {
797
+ return insert(
798
+ content || [],
799
+ action.destinationIndex,
800
+ emptyComponentData
801
+ );
802
+ }
803
+ return content;
804
+ },
805
+ (childItem, path) => {
806
+ if (childItem.props.id === id || childItem.props.id === parentId) {
807
+ return childItem;
808
+ } else if (idsInPath.includes(childItem.props.id)) {
809
+ return childItem;
810
+ } else if (path.includes(action.destinationZone)) {
811
+ return childItem;
812
+ }
813
+ return null;
814
+ }
815
+ );
530
816
  }
531
- var findRelatedByZoneId = (zoneId, data) => {
532
- const [zoneParentId] = getZoneId(zoneId);
533
- return (data.zones[zoneId] || []).reduce(
534
- (acc, zoneItem) => {
535
- const related = findRelatedByItem(zoneItem, data);
536
- if (zoneItem.props.id === zoneParentId) {
537
- return __spreadProps(__spreadValues(__spreadValues({}, acc), related), { [zoneId]: zoneItem });
817
+
818
+ // ../core/reducer/actions/replace.ts
819
+ init_react_import();
820
+ var replaceAction = (state, action, appStore) => {
821
+ const [parentId] = action.destinationZone.split(":");
822
+ const idsInPath = getIdsForParent(action.destinationZone, state);
823
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
824
+ const idChanged = originalId !== action.data.props.id;
825
+ if (idChanged) {
826
+ throw new Error(
827
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
828
+ );
829
+ }
830
+ const data = populateIds(action.data, appStore.config);
831
+ return walkAppState(
832
+ state,
833
+ appStore.config,
834
+ (content, zoneCompound) => {
835
+ const newContent = [...content];
836
+ if (zoneCompound === action.destinationZone) {
837
+ newContent[action.destinationIndex] = data;
538
838
  }
539
- return __spreadValues(__spreadValues({}, acc), related);
839
+ return newContent;
540
840
  },
541
- {}
841
+ (childItem, path) => {
842
+ const pathIds = path.map((p) => p.split(":")[0]);
843
+ if (childItem.props.id === data.props.id) {
844
+ return data;
845
+ } else if (childItem.props.id === parentId) {
846
+ return childItem;
847
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
848
+ return childItem;
849
+ } else if (pathIds.indexOf(data.props.id) > -1) {
850
+ return childItem;
851
+ }
852
+ return null;
853
+ }
542
854
  );
543
855
  };
544
- var findRelatedByItem = (item, data) => {
545
- return Object.keys(data.zones || {}).reduce(
546
- (acc, zoneId) => {
547
- const [zoneParentId] = getZoneId(zoneId);
548
- if (item.props.id === zoneParentId) {
549
- const related = findRelatedByZoneId(zoneId, data);
550
- return __spreadProps(__spreadValues(__spreadValues({}, acc), related), {
551
- [zoneId]: data.zones[zoneId]
856
+
857
+ // ../core/reducer/actions/replace-root.ts
858
+ init_react_import();
859
+ var replaceRootAction = (state, action, appStore) => {
860
+ return walkAppState(
861
+ state,
862
+ appStore.config,
863
+ (content) => content,
864
+ (childItem) => {
865
+ if (childItem.props.id === "root") {
866
+ return __spreadProps(__spreadValues({}, childItem), {
867
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
868
+ readOnly: action.root.readOnly
552
869
  });
553
870
  }
554
- return acc;
555
- },
556
- {}
871
+ return childItem;
872
+ }
557
873
  );
558
874
  };
559
- var removeRelatedZones = (item, data) => {
560
- const newData = __spreadValues({}, data);
561
- const related = findRelatedByItem(item, data);
562
- Object.keys(related).forEach((key) => {
563
- delete newData.zones[key];
564
- });
565
- return newData;
566
- };
567
- function duplicateRelatedZones(item, data, newId) {
568
- return reduceRelatedZones(item, data, (acc, key, zone) => {
569
- const dupedZone = zone.map((zoneItem) => __spreadProps(__spreadValues({}, zoneItem), {
570
- props: __spreadProps(__spreadValues({}, zoneItem.props), { id: generateId(zoneItem.type) })
571
- }));
572
- const dupeOfDupes = dupedZone.reduce(
573
- (dupeOfDupes2, item2, index) => __spreadValues(__spreadValues({}, dupeOfDupes2), duplicateRelatedZones(zone[index], data, item2.props.id).zones),
574
- acc
575
- );
576
- const [_, zoneId] = getZoneId(key);
577
- return __spreadProps(__spreadValues({}, dupeOfDupes), {
578
- [key]: zone,
579
- [`${newId}:${zoneId}`]: dupedZone
580
- });
581
- });
875
+
876
+ // ../core/reducer/actions/duplicate.ts
877
+ init_react_import();
878
+
879
+ // ../core/lib/data/get-item.ts
880
+ init_react_import();
881
+ function getItem(selector, state) {
882
+ var _a, _b;
883
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
884
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
582
885
  }
583
886
 
584
- // ../core/reducer/data.ts
585
- var zoneCache = {};
586
- var replaceAction = (data, action) => {
587
- if (action.destinationZone === rootDroppableId) {
588
- return __spreadProps(__spreadValues({}, data), {
589
- content: replace(data.content, action.destinationIndex, action.data)
590
- });
591
- }
592
- const newData = setupZone(data, action.destinationZone);
593
- return __spreadProps(__spreadValues({}, newData), {
594
- zones: __spreadProps(__spreadValues({}, newData.zones), {
595
- [action.destinationZone]: replace(
596
- newData.zones[action.destinationZone],
597
- action.destinationIndex,
598
- action.data
599
- )
887
+ // ../core/reducer/actions/duplicate.ts
888
+ function duplicateAction(state, action, appStore) {
889
+ const item = getItem(
890
+ { index: action.sourceIndex, zone: action.sourceZone },
891
+ state
892
+ );
893
+ const idsInPath = getIdsForParent(action.sourceZone, state);
894
+ const newItem = __spreadProps(__spreadValues({}, item), {
895
+ props: __spreadProps(__spreadValues({}, item.props), {
896
+ id: generateId(item.type)
600
897
  })
601
898
  });
602
- };
603
- function insertAction(data, action, config) {
604
- const emptyComponentData = {
605
- type: action.componentType,
606
- props: __spreadProps(__spreadValues({}, config.components[action.componentType].defaultProps || {}), {
607
- id: action.id || generateId(action.componentType)
608
- })
609
- };
610
- if (action.destinationZone === rootDroppableId) {
611
- return __spreadProps(__spreadValues({}, data), {
612
- content: insert(
613
- data.content,
614
- action.destinationIndex,
615
- emptyComponentData
616
- )
617
- });
618
- }
619
- const newData = setupZone(data, action.destinationZone);
620
- return __spreadProps(__spreadValues({}, data), {
621
- zones: __spreadProps(__spreadValues({}, newData.zones), {
622
- [action.destinationZone]: insert(
623
- newData.zones[action.destinationZone],
624
- action.destinationIndex,
625
- emptyComponentData
626
- )
899
+ const modified = walkAppState(
900
+ state,
901
+ appStore.config,
902
+ (content, zoneCompound) => {
903
+ if (zoneCompound === action.sourceZone) {
904
+ return insert(content, action.sourceIndex + 1, item);
905
+ }
906
+ return content;
907
+ },
908
+ (childItem, path, index) => {
909
+ const zoneCompound = path[path.length - 1];
910
+ const parents = path.map((p) => p.split(":")[0]);
911
+ if (parents.indexOf(newItem.props.id) > -1) {
912
+ return __spreadProps(__spreadValues({}, childItem), {
913
+ props: __spreadProps(__spreadValues({}, childItem.props), {
914
+ id: generateId(childItem.type)
915
+ })
916
+ });
917
+ }
918
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
919
+ return newItem;
920
+ }
921
+ const [sourceZoneParent] = action.sourceZone.split(":");
922
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
923
+ return childItem;
924
+ }
925
+ return null;
926
+ }
927
+ );
928
+ return __spreadProps(__spreadValues({}, modified), {
929
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
930
+ itemSelector: {
931
+ index: action.sourceIndex + 1,
932
+ zone: action.sourceZone
933
+ }
627
934
  })
628
935
  });
629
936
  }
630
- var reorderAction = (data, action) => {
631
- if (action.destinationZone === rootDroppableId) {
632
- return __spreadProps(__spreadValues({}, data), {
633
- content: reorder(
634
- data.content,
635
- action.sourceIndex,
636
- action.destinationIndex
637
- )
638
- });
639
- }
640
- const newData = setupZone(data, action.destinationZone);
641
- return __spreadProps(__spreadValues({}, data), {
642
- zones: __spreadProps(__spreadValues({}, newData.zones), {
643
- [action.destinationZone]: reorder(
644
- newData.zones[action.destinationZone],
645
- action.sourceIndex,
646
- action.destinationIndex
647
- )
648
- })
649
- });
937
+
938
+ // ../core/reducer/actions/reorder.ts
939
+ init_react_import();
940
+
941
+ // ../core/reducer/actions/move.ts
942
+ init_react_import();
943
+
944
+ // ../core/lib/data/remove.ts
945
+ init_react_import();
946
+ var remove = (list, index) => {
947
+ const result = Array.from(list);
948
+ result.splice(index, 1);
949
+ return result;
650
950
  };
651
- function reduceData(data, action, config) {
652
- if (action.type === "insert") {
653
- return insertAction(data, action, config);
654
- }
655
- if (action.type === "duplicate") {
656
- const item = getItem(
657
- { index: action.sourceIndex, zone: action.sourceZone },
658
- data
659
- );
660
- const newItem = __spreadProps(__spreadValues({}, item), {
661
- props: __spreadProps(__spreadValues({}, item.props), {
662
- id: generateId(item.type)
663
- })
664
- });
665
- const dataWithRelatedDuplicated = duplicateRelatedZones(
666
- item,
667
- data,
668
- newItem.props.id
669
- );
670
- if (action.sourceZone === rootDroppableId) {
671
- return __spreadProps(__spreadValues({}, dataWithRelatedDuplicated), {
672
- content: insert(data.content, action.sourceIndex + 1, newItem)
673
- });
674
- }
675
- return __spreadProps(__spreadValues({}, dataWithRelatedDuplicated), {
676
- zones: __spreadProps(__spreadValues({}, dataWithRelatedDuplicated.zones), {
677
- [action.sourceZone]: insert(
678
- dataWithRelatedDuplicated.zones[action.sourceZone],
679
- action.sourceIndex + 1,
680
- newItem
681
- )
682
- })
683
- });
684
- }
685
- if (action.type === "reorder") {
686
- return reorderAction(data, action);
951
+
952
+ // ../core/reducer/actions/move.ts
953
+ var moveAction = (state, action, appStore) => {
954
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
955
+ return state;
687
956
  }
688
- if (action.type === "move") {
689
- if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
690
- return data;
691
- }
692
- const newData = setupZone(
693
- setupZone(data, action.sourceZone),
694
- action.destinationZone
695
- );
696
- const item = getItem(
697
- { zone: action.sourceZone, index: action.sourceIndex },
698
- newData
699
- );
700
- if (action.sourceZone === action.destinationZone) {
701
- return reorderAction(data, __spreadProps(__spreadValues({}, action), { type: "reorder" }));
957
+ const item = getItem(
958
+ { zone: action.sourceZone, index: action.sourceIndex },
959
+ state
960
+ );
961
+ if (!item) return state;
962
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
963
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
964
+ return walkAppState(
965
+ state,
966
+ appStore.config,
967
+ (content, zoneCompound) => {
968
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
969
+ return insert(
970
+ remove(content, action.sourceIndex),
971
+ action.destinationIndex,
972
+ item
973
+ );
974
+ } else if (zoneCompound === action.sourceZone) {
975
+ return remove(content, action.sourceIndex);
976
+ } else if (zoneCompound === action.destinationZone) {
977
+ return insert(content, action.destinationIndex, item);
978
+ }
979
+ return content;
980
+ },
981
+ (childItem, path) => {
982
+ const [sourceZoneParent] = action.sourceZone.split(":");
983
+ const [destinationZoneParent] = action.destinationZone.split(":");
984
+ const childId = childItem.props.id;
985
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
986
+ return childItem;
987
+ }
988
+ return null;
702
989
  }
703
- if (action.sourceZone === rootDroppableId) {
704
- return __spreadProps(__spreadValues({}, newData), {
705
- content: remove(newData.content, action.sourceIndex),
706
- zones: __spreadProps(__spreadValues({}, newData.zones), {
707
- [action.destinationZone]: insert(
708
- newData.zones[action.destinationZone],
709
- action.destinationIndex,
710
- item
711
- )
712
- })
713
- });
990
+ );
991
+ };
992
+
993
+ // ../core/reducer/actions/reorder.ts
994
+ var reorderAction = (state, action, appStore) => {
995
+ return moveAction(
996
+ state,
997
+ {
998
+ type: "move",
999
+ sourceIndex: action.sourceIndex,
1000
+ sourceZone: action.destinationZone,
1001
+ destinationIndex: action.destinationIndex,
1002
+ destinationZone: action.destinationZone
1003
+ },
1004
+ appStore
1005
+ );
1006
+ };
1007
+
1008
+ // ../core/reducer/actions/remove.ts
1009
+ init_react_import();
1010
+ var removeAction = (state, action, appStore) => {
1011
+ const item = getItem({ index: action.index, zone: action.zone }, state);
1012
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
1013
+ (acc, [nodeId, nodeData]) => {
1014
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
1015
+ if (pathIds.includes(item.props.id)) {
1016
+ return [...acc, nodeId];
1017
+ }
1018
+ return acc;
1019
+ },
1020
+ [item.props.id]
1021
+ );
1022
+ const newState = walkAppState(
1023
+ state,
1024
+ appStore.config,
1025
+ (content, zoneCompound) => {
1026
+ if (zoneCompound === action.zone) {
1027
+ return remove(content, action.index);
1028
+ }
1029
+ return content;
714
1030
  }
715
- if (action.destinationZone === rootDroppableId) {
716
- return __spreadProps(__spreadValues({}, newData), {
717
- content: insert(newData.content, action.destinationIndex, item),
718
- zones: __spreadProps(__spreadValues({}, newData.zones), {
719
- [action.sourceZone]: remove(
720
- newData.zones[action.sourceZone],
721
- action.sourceIndex
722
- )
723
- })
724
- });
1031
+ );
1032
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
1033
+ const parentId = zoneCompound.split(":")[0];
1034
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
1035
+ delete newState.data.zones[zoneCompound];
725
1036
  }
726
- return __spreadProps(__spreadValues({}, newData), {
727
- zones: __spreadProps(__spreadValues({}, newData.zones), {
728
- [action.sourceZone]: remove(
729
- newData.zones[action.sourceZone],
730
- action.sourceIndex
731
- ),
732
- [action.destinationZone]: insert(
733
- newData.zones[action.destinationZone],
734
- action.destinationIndex,
735
- item
736
- )
737
- })
738
- });
739
- }
740
- if (action.type === "replace") {
741
- return replaceAction(data, action);
742
- }
743
- if (action.type === "remove") {
744
- const item = getItem({ index: action.index, zone: action.zone }, data);
745
- const dataWithRelatedRemoved = setupZone(
746
- removeRelatedZones(item, data),
747
- action.zone
748
- );
749
- if (action.zone === rootDroppableId) {
750
- return __spreadProps(__spreadValues({}, dataWithRelatedRemoved), {
751
- content: remove(data.content, action.index)
752
- });
1037
+ });
1038
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
1039
+ const parentId = zoneCompound.split(":")[0];
1040
+ if (nodesToDelete.includes(parentId)) {
1041
+ delete newState.indexes.zones[zoneCompound];
753
1042
  }
754
- return __spreadProps(__spreadValues({}, dataWithRelatedRemoved), {
755
- zones: __spreadProps(__spreadValues({}, dataWithRelatedRemoved.zones), {
756
- [action.zone]: remove(
757
- dataWithRelatedRemoved.zones[action.zone],
758
- action.index
759
- )
760
- })
761
- });
1043
+ });
1044
+ nodesToDelete.forEach((id) => {
1045
+ delete newState.indexes.nodes[id];
1046
+ });
1047
+ return newState;
1048
+ };
1049
+
1050
+ // ../core/reducer/actions/register-zone.ts
1051
+ init_react_import();
1052
+
1053
+ // ../core/lib/data/setup-zone.ts
1054
+ init_react_import();
1055
+ var setupZone = (data, zoneKey) => {
1056
+ if (zoneKey === rootDroppableId) {
1057
+ return data;
762
1058
  }
763
- if (action.type === "registerZone") {
764
- if (zoneCache[action.zone]) {
765
- return __spreadProps(__spreadValues({}, data), {
766
- zones: __spreadProps(__spreadValues({}, data.zones), {
1059
+ const newData = __spreadProps(__spreadValues({}, data), {
1060
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
1061
+ });
1062
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
1063
+ return newData;
1064
+ };
1065
+
1066
+ // ../core/reducer/actions/register-zone.ts
1067
+ var zoneCache = {};
1068
+ function registerZoneAction(state, action) {
1069
+ if (zoneCache[action.zone]) {
1070
+ return __spreadProps(__spreadValues({}, state), {
1071
+ data: __spreadProps(__spreadValues({}, state.data), {
1072
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
767
1073
  [action.zone]: zoneCache[action.zone]
768
1074
  })
769
- });
770
- }
771
- return setupZone(data, action.zone);
772
- }
773
- if (action.type === "unregisterZone") {
774
- const _zones = __spreadValues({}, data.zones || {});
775
- if (_zones[action.zone]) {
776
- zoneCache[action.zone] = _zones[action.zone];
777
- delete _zones[action.zone];
778
- }
779
- return __spreadProps(__spreadValues({}, data), { zones: _zones });
1075
+ }),
1076
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
1077
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
1078
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
1079
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
1080
+ type: "dropzone"
1081
+ })
1082
+ })
1083
+ })
1084
+ });
780
1085
  }
781
- if (action.type === "setData") {
782
- if (typeof action.data === "object") {
783
- return __spreadValues(__spreadValues({}, data), action.data);
784
- }
785
- return __spreadValues(__spreadValues({}, data), action.data(data));
1086
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
1087
+ }
1088
+ function unregisterZoneAction(state, action) {
1089
+ const _zones = __spreadValues({}, state.data.zones || {});
1090
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
1091
+ if (_zones[action.zone]) {
1092
+ zoneCache[action.zone] = _zones[action.zone];
1093
+ delete _zones[action.zone];
786
1094
  }
787
- return data;
1095
+ delete zoneIndex[action.zone];
1096
+ return __spreadProps(__spreadValues({}, state), {
1097
+ data: __spreadProps(__spreadValues({}, state.data), {
1098
+ zones: _zones
1099
+ }),
1100
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
1101
+ zones: zoneIndex
1102
+ })
1103
+ });
788
1104
  }
789
1105
 
790
- // ../core/reducer/state.ts
1106
+ // ../core/reducer/actions/set-data.ts
791
1107
  init_react_import();
792
- var reduceUi = (ui, action) => {
793
- if (action.type === "setUi") {
794
- if (typeof action.ui === "object") {
795
- return __spreadValues(__spreadValues({}, ui), action.ui);
796
- }
797
- return __spreadValues(__spreadValues({}, ui), action.ui(ui));
798
- }
799
- if (action.type === "duplicate") {
800
- return __spreadProps(__spreadValues({}, ui), {
801
- itemSelector: { index: action.sourceIndex + 1, zone: action.sourceZone }
802
- });
1108
+ var setDataAction = (state, action, appStore) => {
1109
+ if (typeof action.data === "object") {
1110
+ console.warn(
1111
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
1112
+ );
1113
+ return walkAppState(
1114
+ __spreadProps(__spreadValues({}, state), {
1115
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
1116
+ }),
1117
+ appStore.config
1118
+ );
803
1119
  }
804
- if (action.type === "remove") {
805
- return __spreadProps(__spreadValues({}, ui), {
806
- itemSelector: null
1120
+ return walkAppState(
1121
+ __spreadProps(__spreadValues({}, state), {
1122
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
1123
+ }),
1124
+ appStore.config
1125
+ );
1126
+ };
1127
+
1128
+ // ../core/reducer/actions/set-ui.ts
1129
+ init_react_import();
1130
+ var setUiAction = (state, action) => {
1131
+ if (typeof action.ui === "object") {
1132
+ return __spreadProps(__spreadValues({}, state), {
1133
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
807
1134
  });
808
1135
  }
809
- return ui;
1136
+ return __spreadProps(__spreadValues({}, state), {
1137
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
1138
+ });
1139
+ };
1140
+
1141
+ // ../core/lib/data/make-state-public.ts
1142
+ init_react_import();
1143
+ var makeStatePublic = (state) => {
1144
+ const { data, ui } = state;
1145
+ return { data, ui };
810
1146
  };
811
1147
 
812
1148
  // ../core/reducer/actions.tsx
@@ -826,29 +1162,54 @@ function storeInterceptor(reducer, record, onAction) {
826
1162
  if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
827
1163
  if (record) record(newAppState);
828
1164
  }
829
- onAction == null ? void 0 : onAction(action, newAppState, state);
1165
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
830
1166
  return newAppState;
831
1167
  };
832
1168
  }
833
- var setAction = (state, action) => {
834
- if (typeof action.state === "object") {
835
- return __spreadValues(__spreadValues({}, state), action.state);
836
- }
837
- return __spreadValues(__spreadValues({}, state), action.state(state));
838
- };
839
1169
  function createReducer({
840
- config,
841
1170
  record,
842
- onAction
1171
+ onAction,
1172
+ appStore
843
1173
  }) {
844
1174
  return storeInterceptor(
845
1175
  (state, action) => {
846
- const data = reduceData(state.data, action, config);
847
- const ui = reduceUi(state.ui, action);
848
1176
  if (action.type === "set") {
849
- return setAction(state, action);
1177
+ return setAction(state, action, appStore);
1178
+ }
1179
+ if (action.type === "insert") {
1180
+ return insertAction(state, action, appStore);
1181
+ }
1182
+ if (action.type === "replace") {
1183
+ return replaceAction(state, action, appStore);
1184
+ }
1185
+ if (action.type === "replaceRoot") {
1186
+ return replaceRootAction(state, action, appStore);
1187
+ }
1188
+ if (action.type === "duplicate") {
1189
+ return duplicateAction(state, action, appStore);
1190
+ }
1191
+ if (action.type === "reorder") {
1192
+ return reorderAction(state, action, appStore);
850
1193
  }
851
- return { data, ui };
1194
+ if (action.type === "move") {
1195
+ return moveAction(state, action, appStore);
1196
+ }
1197
+ if (action.type === "remove") {
1198
+ return removeAction(state, action, appStore);
1199
+ }
1200
+ if (action.type === "registerZone") {
1201
+ return registerZoneAction(state, action);
1202
+ }
1203
+ if (action.type === "unregisterZone") {
1204
+ return unregisterZoneAction(state, action);
1205
+ }
1206
+ if (action.type === "setData") {
1207
+ return setDataAction(state, action, appStore);
1208
+ }
1209
+ if (action.type === "setUi") {
1210
+ return setUiAction(state, action);
1211
+ }
1212
+ return state;
852
1213
  },
853
1214
  record,
854
1215
  onAction
@@ -868,11 +1229,11 @@ init_react_import();
868
1229
  var createStoreImpl = (createState) => {
869
1230
  let state;
870
1231
  const listeners = /* @__PURE__ */ new Set();
871
- const setState = (partial, replace2) => {
1232
+ const setState = (partial, replace) => {
872
1233
  const nextState = typeof partial === "function" ? partial(state) : partial;
873
1234
  if (!Object.is(nextState, state)) {
874
1235
  const previousState = state;
875
- state = (replace2 != null ? replace2 : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
1236
+ state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
876
1237
  listeners.forEach((listener) => listener(state, previousState));
877
1238
  }
878
1239
  };
@@ -936,208 +1297,8 @@ var subscribeWithSelectorImpl = (fn) => (set, get, api) => {
936
1297
  };
937
1298
  var subscribeWithSelector = subscribeWithSelectorImpl;
938
1299
 
939
- // ../core/lib/resolve-data.ts
940
- init_react_import();
941
-
942
- // ../core/lib/resolve-component-data.ts
943
- init_react_import();
944
-
945
- // ../core/lib/get-changed.ts
946
- init_react_import();
947
- var getChanged = (newItem, oldItem) => {
948
- return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
949
- const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
950
- const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
951
- return __spreadProps(__spreadValues({}, acc), {
952
- [item]: oldItemProps[item] !== newItemProps[item]
953
- });
954
- }, {}) : {};
955
- };
956
-
957
- // ../core/lib/resolve-component-data.ts
958
- var cache = { lastChange: {} };
959
- var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd) {
960
- const configForItem = config.components[item.type];
961
- if (configForItem.resolveData) {
962
- const { item: oldItem = null, resolved = {} } = cache.lastChange[item.props.id] || {};
963
- if (item && item === oldItem) {
964
- return resolved;
965
- }
966
- const changed = getChanged(item, oldItem);
967
- if (onResolveStart) {
968
- onResolveStart(item);
969
- }
970
- const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
971
- changed,
972
- lastData: oldItem,
973
- metadata
974
- });
975
- const resolvedItem = __spreadProps(__spreadValues({}, item), {
976
- props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
977
- });
978
- if (Object.keys(readOnly).length) {
979
- resolvedItem.readOnly = readOnly;
980
- }
981
- cache.lastChange[item.props.id] = {
982
- item,
983
- resolved: resolvedItem
984
- };
985
- if (onResolveEnd) {
986
- onResolveEnd(resolvedItem);
987
- }
988
- return resolvedItem;
989
- }
990
- return item;
991
- });
992
-
993
- // ../core/lib/apply-dynamic-props.ts
994
- init_react_import();
995
- var applyDynamicProps = (data, dynamicProps, rootData) => {
996
- return __spreadProps(__spreadValues({}, data), {
997
- root: rootData ? __spreadValues(__spreadValues({}, data.root), rootData ? rootData : {}) : data.root,
998
- content: data.content.map((item) => {
999
- return dynamicProps[item.props.id] ? __spreadValues(__spreadValues({}, item), dynamicProps[item.props.id]) : item;
1000
- }),
1001
- zones: Object.keys(data.zones || {}).reduce((acc, zoneKey) => {
1002
- return __spreadProps(__spreadValues({}, acc), {
1003
- [zoneKey]: data.zones[zoneKey].map((item) => {
1004
- return dynamicProps[item.props.id] ? __spreadValues(__spreadValues({}, item), dynamicProps[item.props.id]) : item;
1005
- })
1006
- });
1007
- }, {})
1008
- });
1009
- };
1010
-
1011
- // ../core/lib/resolve-root-data.ts
1012
- init_react_import();
1013
- var cache2 = {};
1014
- function resolveRootData(data, config, metadata) {
1015
- return __async(this, null, function* () {
1016
- var _a, _b, _c, _d, _e;
1017
- if (((_a = config.root) == null ? void 0 : _a.resolveData) && data.root.props) {
1018
- if (((_b = cache2.lastChange) == null ? void 0 : _b.original) === data.root) {
1019
- return cache2.lastChange.resolved;
1020
- }
1021
- const changed = getChanged(data.root, (_c = cache2.lastChange) == null ? void 0 : _c.original);
1022
- const rootWithProps = data.root;
1023
- const resolvedRoot = yield (_e = config.root) == null ? void 0 : _e.resolveData(rootWithProps, {
1024
- changed,
1025
- lastData: ((_d = cache2.lastChange) == null ? void 0 : _d.original) || {},
1026
- metadata: metadata || {}
1027
- });
1028
- cache2.lastChange = {
1029
- original: data.root,
1030
- resolved: resolvedRoot
1031
- };
1032
- return __spreadProps(__spreadValues(__spreadValues({}, data.root), resolvedRoot), {
1033
- props: __spreadValues(__spreadValues({}, data.root.props), resolvedRoot.props)
1034
- });
1035
- }
1036
- return data.root;
1037
- });
1038
- }
1039
-
1040
- // ../core/lib/flatten-data.ts
1041
- init_react_import();
1042
- var flattenData = (data) => {
1043
- return Object.keys(data.zones || {}).reduce(
1044
- (acc, zone) => [...acc, ...data.zones[zone]],
1045
- data.content
1046
- );
1047
- };
1048
-
1049
- // ../core/lib/resolve-data.ts
1050
- var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1051
- var resolveData = (newAppState, appStoreData) => {
1052
- const {
1053
- state: appState,
1054
- config,
1055
- dispatch,
1056
- resolveDataRuns,
1057
- setComponentLoading,
1058
- unsetComponentLoading,
1059
- metadata,
1060
- permissions
1061
- } = appStoreData;
1062
- const deferredSetStates = {};
1063
- const _setComponentLoading = (id, loading, defer = 0) => {
1064
- if (deferredSetStates[id]) {
1065
- clearTimeout(deferredSetStates[id]);
1066
- delete deferredSetStates[id];
1067
- }
1068
- deferredSetStates[id] = setTimeout(() => {
1069
- if (loading) {
1070
- setComponentLoading(id);
1071
- } else {
1072
- unsetComponentLoading(id);
1073
- }
1074
- delete deferredSetStates[id];
1075
- }, defer);
1076
- };
1077
- const runResolvers = () => __async(void 0, null, function* () {
1078
- const newData = newAppState.data;
1079
- const flatContent = flattenData(newData).filter(
1080
- (item) => {
1081
- var _a;
1082
- return !!((_a = config.components[item.type]) == null ? void 0 : _a.resolveData);
1083
- }
1084
- );
1085
- const applyIfChange = (dynamicDataMap, dynamicRoot) => {
1086
- const processed = applyDynamicProps(
1087
- __spreadValues({}, appState.data),
1088
- dynamicDataMap,
1089
- dynamicRoot
1090
- );
1091
- const processedAppState = __spreadProps(__spreadValues({}, appState), { data: processed });
1092
- const containsChanges = !(0, import_fast_deep_equal.default)(appState, processedAppState);
1093
- if (containsChanges) {
1094
- dispatch({
1095
- type: "set",
1096
- state: (prev) => __spreadProps(__spreadValues({}, prev), {
1097
- data: applyDynamicProps(prev.data, dynamicDataMap, dynamicRoot),
1098
- ui: resolveDataRuns > 0 ? __spreadValues(__spreadValues({}, prev.ui), newAppState.ui) : prev.ui
1099
- }),
1100
- recordHistory: resolveDataRuns > 0
1101
- });
1102
- }
1103
- };
1104
- const promises = [];
1105
- promises.push(
1106
- (() => __async(void 0, null, function* () {
1107
- _setComponentLoading("puck-root", true, 50);
1108
- const dynamicRoot = yield resolveRootData(newData, config, metadata);
1109
- applyIfChange({}, dynamicRoot);
1110
- _setComponentLoading("puck-root", false);
1111
- }))()
1112
- );
1113
- flatContent.forEach((item) => {
1114
- promises.push(
1115
- (() => __async(void 0, null, function* () {
1116
- permissions.resolvePermissions({ item }, true);
1117
- const dynamicData = yield resolveComponentData(
1118
- item,
1119
- config,
1120
- metadata,
1121
- (item2) => {
1122
- _setComponentLoading(item2.props.id, true, 50);
1123
- },
1124
- (item2) => {
1125
- deferredSetStates[item2.props.id];
1126
- _setComponentLoading(item2.props.id, false);
1127
- }
1128
- );
1129
- const dynamicDataMap = { [item.props.id]: dynamicData };
1130
- applyIfChange(dynamicDataMap);
1131
- }))()
1132
- );
1133
- });
1134
- yield Promise.all(promises);
1135
- });
1136
- return runResolvers();
1137
- };
1138
-
1139
1300
  // ../core/store/index.ts
1140
- var import_react10 = require("react");
1301
+ var import_react9 = require("react");
1141
1302
 
1142
1303
  // ../core/store/slices/history.ts
1143
1304
  init_react_import();
@@ -1243,7 +1404,7 @@ var createHistorySlice = (set, get) => {
1243
1404
  const { dispatch, history } = get();
1244
1405
  dispatch({
1245
1406
  type: "set",
1246
- state: ((_a = history.histories[history.index]) == null ? void 0 : _a.state) || history.initialAppState
1407
+ state: ((_a = history.histories[index]) == null ? void 0 : _a.state) || history.initialAppState
1247
1408
  });
1248
1409
  set({ history: __spreadProps(__spreadValues({}, history), { index }) });
1249
1410
  },
@@ -1253,31 +1414,18 @@ var createHistorySlice = (set, get) => {
1253
1414
 
1254
1415
  // ../core/store/slices/nodes.ts
1255
1416
  init_react_import();
1256
- var import_fast_deep_equal2 = __toESM(require_fast_deep_equal());
1257
- var import_react7 = require("react");
1258
- var partialDeepEqual = (newItem, existingItem) => {
1259
- const filteredExistingItem = Object.keys(newItem).reduce(
1260
- (acc, key) => __spreadProps(__spreadValues({}, acc), { [key]: existingItem[key] }),
1261
- {}
1262
- );
1263
- return (0, import_fast_deep_equal2.default)(newItem, filteredExistingItem);
1264
- };
1265
1417
  var createNodesSlice = (set, get) => ({
1266
1418
  nodes: {},
1267
1419
  registerNode: (id, node) => {
1268
1420
  const s = get().nodes;
1269
- if (s.nodes[id] && partialDeepEqual(node, s.nodes[id])) {
1270
- return;
1271
- }
1272
1421
  const emptyNode = {
1273
1422
  id,
1274
- methods: { sync: () => null },
1275
- data: { props: { id }, type: "unknown" },
1276
- parentId: "",
1277
- zone: "",
1278
- path: [],
1279
- element: null,
1280
- index: -1
1423
+ methods: {
1424
+ sync: () => null,
1425
+ hideOverlay: () => null,
1426
+ showOverlay: () => null
1427
+ },
1428
+ element: null
1281
1429
  };
1282
1430
  const existingNode = s.nodes[id];
1283
1431
  set({
@@ -1307,36 +1455,61 @@ var createNodesSlice = (set, get) => ({
1307
1455
 
1308
1456
  // ../core/store/slices/permissions.ts
1309
1457
  init_react_import();
1310
- var import_react8 = require("react");
1458
+ var import_react7 = require("react");
1459
+
1460
+ // ../core/lib/data/flatten-data.ts
1461
+ init_react_import();
1462
+ var flattenData = (state, config) => {
1463
+ const data = [];
1464
+ walkAppState(
1465
+ state,
1466
+ config,
1467
+ (content) => content,
1468
+ (item) => {
1469
+ data.push(item);
1470
+ return null;
1471
+ }
1472
+ );
1473
+ return data;
1474
+ };
1475
+
1476
+ // ../core/lib/get-changed.ts
1477
+ init_react_import();
1478
+ var getChanged = (newItem, oldItem) => {
1479
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
1480
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
1481
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
1482
+ return __spreadProps(__spreadValues({}, acc), {
1483
+ [item]: oldItemProps[item] !== newItemProps[item]
1484
+ });
1485
+ }, {}) : {};
1486
+ };
1487
+
1488
+ // ../core/store/slices/permissions.ts
1311
1489
  var createPermissionsSlice = (set, get) => {
1312
1490
  const resolvePermissions = (..._0) => __async(void 0, [..._0], function* (params = {}, force) {
1313
- const { state, permissions } = get();
1314
- const { cache: cache3, globalPermissions } = permissions;
1491
+ const { state, permissions, config } = get();
1492
+ const { cache: cache2, globalPermissions } = permissions;
1315
1493
  const resolveDataForItem = (item2, force2 = false) => __async(void 0, null, function* () {
1316
1494
  var _a, _b, _c;
1317
- const {
1318
- config,
1319
- state: appState,
1320
- setComponentLoading,
1321
- unsetComponentLoading
1322
- } = get();
1323
- const componentConfig = item2.type === "root" ? config.root : config.components[item2.type];
1495
+ const { config: config2, state: appState, setComponentLoading } = get();
1496
+ const componentConfig = item2.type === "root" ? config2.root : config2.components[item2.type];
1324
1497
  if (!componentConfig) {
1325
1498
  return;
1326
1499
  }
1327
1500
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), componentConfig.permissions);
1328
1501
  if (componentConfig.resolvePermissions) {
1329
- const changed = getChanged(item2, (_a = cache3[item2.props.id]) == null ? void 0 : _a.lastData);
1502
+ const changed = getChanged(item2, (_a = cache2[item2.props.id]) == null ? void 0 : _a.lastData);
1330
1503
  if (Object.values(changed).some((el) => el === true) || force2) {
1331
- setComponentLoading(item2.props.id);
1504
+ const clearTimeout2 = setComponentLoading(item2.props.id, true, 50);
1332
1505
  const resolvedPermissions = yield componentConfig.resolvePermissions(
1333
1506
  item2,
1334
1507
  {
1335
1508
  changed,
1336
- lastPermissions: ((_b = cache3[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1509
+ lastPermissions: ((_b = cache2[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1337
1510
  permissions: initialPermissions,
1338
- appState,
1339
- lastData: ((_c = cache3[item2.props.id]) == null ? void 0 : _c.lastData) || null
1511
+ appState: makeStatePublic(appState),
1512
+ lastData: ((_c = cache2[item2.props.id]) == null ? void 0 : _c.lastData) || null
1340
1513
  }
1341
1514
  );
1342
1515
  const latest = get().permissions;
@@ -1353,7 +1526,7 @@ var createPermissionsSlice = (set, get) => {
1353
1526
  })
1354
1527
  })
1355
1528
  });
1356
- unsetComponentLoading(item2.props.id);
1529
+ clearTimeout2();
1357
1530
  }
1358
1531
  }
1359
1532
  });
@@ -1363,7 +1536,7 @@ var createPermissionsSlice = (set, get) => {
1363
1536
  // Shim the root data in by conforming to component data shape
1364
1537
  {
1365
1538
  type: "root",
1366
- props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "puck-root" })
1539
+ props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "root" })
1367
1540
  },
1368
1541
  force2
1369
1542
  );
@@ -1372,14 +1545,13 @@ var createPermissionsSlice = (set, get) => {
1372
1545
  if (item) {
1373
1546
  yield resolveDataForItem(item, force);
1374
1547
  } else if (type) {
1375
- flattenData(state.data).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1548
+ flattenData(state, config).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1376
1549
  yield resolveDataForItem(item2, force);
1377
1550
  }));
1378
1551
  } else if (root) {
1379
1552
  resolveDataForRoot(force);
1380
1553
  } else {
1381
- resolveDataForRoot(force);
1382
- flattenData(state.data).map((item2) => __async(void 0, null, function* () {
1554
+ flattenData(state, config).map((item2) => __async(void 0, null, function* () {
1383
1555
  yield resolveDataForItem(item2, force);
1384
1556
  }));
1385
1557
  }
@@ -1409,7 +1581,7 @@ var createPermissionsSlice = (set, get) => {
1409
1581
  } else if (root) {
1410
1582
  const rootConfig = config.root;
1411
1583
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), rootConfig == null ? void 0 : rootConfig.permissions);
1412
- const resolvedForItem = resolvedPermissions["puck-root"];
1584
+ const resolvedForItem = resolvedPermissions["root"];
1413
1585
  return resolvedForItem ? __spreadValues(__spreadValues({}, globalPermissions), resolvedForItem) : initialPermissions;
1414
1586
  }
1415
1587
  return globalPermissions;
@@ -1421,16 +1593,99 @@ var createPermissionsSlice = (set, get) => {
1421
1593
 
1422
1594
  // ../core/store/slices/fields.ts
1423
1595
  init_react_import();
1424
- var import_react9 = require("react");
1425
- var createFieldsStore = (_set, _get) => {
1596
+ var import_react8 = require("react");
1597
+ var createFieldsSlice = (_set, _get) => {
1426
1598
  return {
1427
1599
  fields: {},
1428
1600
  loading: false,
1429
- lastResolvedData: {}
1601
+ lastResolvedData: {},
1602
+ id: void 0
1430
1603
  };
1431
1604
  };
1432
1605
 
1433
- // ../core/store/index.ts
1606
+ // ../core/lib/resolve-component-data.ts
1607
+ init_react_import();
1608
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1609
+ var cache = { lastChange: {} };
1610
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace", recursive = true) {
1611
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
1612
+ if ((configForItem == null ? void 0 : configForItem.resolveData) && item.props) {
1613
+ const id = "id" in item.props ? item.props.id : "root";
1614
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
1615
+ if (item && (0, import_fast_deep_equal.default)(item, oldItem)) {
1616
+ return { node: resolved, didChange: false };
1617
+ }
1618
+ const changed = getChanged(item, oldItem);
1619
+ if (onResolveStart) {
1620
+ onResolveStart(item);
1621
+ }
1622
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
1623
+ changed,
1624
+ lastData: oldItem,
1625
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
1626
+ trigger
1627
+ });
1628
+ let resolvedItem = __spreadProps(__spreadValues({}, item), {
1629
+ props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
1630
+ });
1631
+ if (recursive) {
1632
+ resolvedItem = yield mapSlotsAsync(
1633
+ resolvedItem,
1634
+ (content) => __async(void 0, null, function* () {
1635
+ return Promise.all(
1636
+ content.map(
1637
+ (childItem) => __async(void 0, null, function* () {
1638
+ return (yield resolveComponentData(
1639
+ childItem,
1640
+ config,
1641
+ metadata,
1642
+ onResolveStart,
1643
+ onResolveEnd,
1644
+ trigger,
1645
+ false
1646
+ )).node;
1647
+ })
1648
+ )
1649
+ );
1650
+ }),
1651
+ false,
1652
+ createIsSlotConfig(config)
1653
+ );
1654
+ }
1655
+ if (Object.keys(readOnly).length) {
1656
+ resolvedItem.readOnly = readOnly;
1657
+ }
1658
+ cache.lastChange[id] = {
1659
+ item,
1660
+ resolved: resolvedItem
1661
+ };
1662
+ if (onResolveEnd) {
1663
+ onResolveEnd(resolvedItem);
1664
+ }
1665
+ return { node: resolvedItem, didChange: !(0, import_fast_deep_equal.default)(item, resolvedItem) };
1666
+ }
1667
+ return { node: item, didChange: false };
1668
+ });
1669
+
1670
+ // ../core/lib/data/to-root.ts
1671
+ init_react_import();
1672
+ var toRoot = (item) => {
1673
+ if ("type" in item && item.type !== "root") {
1674
+ throw new Error("Converting non-root item to root.");
1675
+ }
1676
+ const { readOnly } = item;
1677
+ if (item.props) {
1678
+ if ("id" in item.props) {
1679
+ const _a = item.props, { id } = _a, props = __objRest(_a, ["id"]);
1680
+ return { props, readOnly };
1681
+ }
1682
+ return { props: item.props, readOnly };
1683
+ }
1684
+ return { props: {}, readOnly };
1685
+ };
1686
+
1687
+ // ../core/store/default-app-state.ts
1688
+ init_react_import();
1434
1689
  var defaultAppState = {
1435
1690
  data: { content: [], root: {}, zones: {} },
1436
1691
  ui: {
@@ -1450,100 +1705,196 @@ var defaultAppState = {
1450
1705
  controlsVisible: true
1451
1706
  },
1452
1707
  field: { focus: null }
1708
+ },
1709
+ indexes: {
1710
+ nodes: {},
1711
+ zones: {}
1453
1712
  }
1454
1713
  };
1714
+
1715
+ // ../core/store/index.ts
1455
1716
  var defaultPageFields = {
1456
1717
  title: { type: "text" }
1457
1718
  };
1458
1719
  var createAppStore = (initialAppStore) => create()(
1459
- subscribeWithSelector((set, get) => __spreadProps(__spreadValues({
1460
- state: defaultAppState,
1461
- config: { components: {} },
1462
- componentState: {},
1463
- plugins: [],
1464
- overrides: {},
1465
- viewports: defaultViewports,
1466
- zoomConfig: {
1467
- autoZoom: 1,
1468
- rootHeight: 0,
1469
- zoom: 1
1470
- },
1471
- status: "LOADING",
1472
- iframe: {},
1473
- metadata: {}
1474
- }, initialAppStore), {
1475
- fields: createFieldsStore(set, get),
1476
- history: createHistorySlice(set, get),
1477
- nodes: createNodesSlice(set, get),
1478
- permissions: createPermissionsSlice(set, get),
1479
- getComponentConfig: (type) => {
1480
- var _a;
1481
- const { config, selectedItem } = get();
1482
- const rootFields = ((_a = config.root) == null ? void 0 : _a.fields) || defaultPageFields;
1483
- return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1484
- },
1485
- dispatch: (action) => set((s) => {
1486
- var _a, _b;
1487
- const { record } = get().history;
1488
- const dispatch = createReducer({ config: s.config, record });
1489
- const state = dispatch(s.state, action);
1490
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state.data) : null;
1491
- (_b = (_a = get()).onAction) == null ? void 0 : _b.call(_a, action, state, get().state);
1492
- return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1493
- }),
1494
- setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1495
- setStatus: (status) => set({ status }),
1496
- setComponentState: (componentState) => set({ componentState }),
1497
- setComponentLoading: (id) => {
1498
- var _a;
1499
- const { setComponentState, componentState } = get();
1500
- setComponentState(__spreadProps(__spreadValues({}, componentState), {
1501
- [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1502
- loadingCount: (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) + 1
1503
- })
1504
- }));
1505
- },
1506
- unsetComponentLoading: (id) => {
1507
- var _a;
1508
- const { setComponentState, componentState } = get();
1509
- setComponentState(__spreadProps(__spreadValues({}, componentState), {
1510
- [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1511
- loadingCount: Math.max(
1512
- (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) - 1,
1513
- 0
1514
- )
1515
- })
1516
- }));
1517
- },
1518
- // Helper
1519
- setUi: (ui, recordHistory) => set((s) => {
1520
- const dispatch = createReducer({
1521
- config: s.config,
1522
- record: () => {
1523
- }
1524
- });
1525
- const state = dispatch(s.state, {
1526
- type: "setUi",
1527
- ui,
1528
- recordHistory
1529
- });
1530
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state.data) : null;
1531
- return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1532
- }),
1533
- resolveDataRuns: 0,
1534
- resolveData: (newAppState) => set((s) => {
1535
- resolveData(newAppState, get());
1536
- return __spreadProps(__spreadValues({}, s), { resolveDataRuns: s.resolveDataRuns + 1 });
1537
- })
1538
- }))
1720
+ subscribeWithSelector((set, get) => {
1721
+ var _a, _b;
1722
+ return __spreadProps(__spreadValues({
1723
+ state: defaultAppState,
1724
+ config: { components: {} },
1725
+ componentState: {},
1726
+ plugins: [],
1727
+ overrides: {},
1728
+ viewports: defaultViewports,
1729
+ zoomConfig: {
1730
+ autoZoom: 1,
1731
+ rootHeight: 0,
1732
+ zoom: 1
1733
+ },
1734
+ status: "LOADING",
1735
+ iframe: {},
1736
+ metadata: {}
1737
+ }, initialAppStore), {
1738
+ fields: createFieldsSlice(set, get),
1739
+ history: createHistorySlice(set, get),
1740
+ nodes: createNodesSlice(set, get),
1741
+ permissions: createPermissionsSlice(set, get),
1742
+ getComponentConfig: (type) => {
1743
+ var _a2;
1744
+ const { config, selectedItem } = get();
1745
+ const rootFields = ((_a2 = config.root) == null ? void 0 : _a2.fields) || defaultPageFields;
1746
+ return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1747
+ },
1748
+ selectedItem: ((_a = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _a.ui.itemSelector) ? getItem(
1749
+ (_b = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _b.ui.itemSelector,
1750
+ initialAppStore.state
1751
+ ) : null,
1752
+ dispatch: (action) => set((s) => {
1753
+ var _a2, _b2;
1754
+ const { record } = get().history;
1755
+ const dispatch = createReducer({
1756
+ record,
1757
+ appStore: s
1758
+ });
1759
+ const state = dispatch(s.state, action);
1760
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1761
+ (_b2 = (_a2 = get()).onAction) == null ? void 0 : _b2.call(_a2, action, state, get().state);
1762
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1763
+ }),
1764
+ setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1765
+ setStatus: (status) => set({ status }),
1766
+ setComponentState: (componentState) => set({ componentState }),
1767
+ pendingLoadTimeouts: {},
1768
+ setComponentLoading: (id, loading = true, defer = 0) => {
1769
+ const { setComponentState, pendingLoadTimeouts } = get();
1770
+ const loadId = generateId();
1771
+ const setLoading = () => {
1772
+ var _a2;
1773
+ const { componentState } = get();
1774
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1775
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1776
+ loadingCount: (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) + 1
1777
+ })
1778
+ }));
1779
+ };
1780
+ const unsetLoading = () => {
1781
+ var _a2;
1782
+ const { componentState } = get();
1783
+ clearTimeout(timeout);
1784
+ delete pendingLoadTimeouts[loadId];
1785
+ set({ pendingLoadTimeouts });
1786
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1787
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1788
+ loadingCount: Math.max(
1789
+ (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) - 1,
1790
+ 0
1791
+ )
1792
+ })
1793
+ }));
1794
+ };
1795
+ const timeout = setTimeout(() => {
1796
+ if (loading) {
1797
+ setLoading();
1798
+ } else {
1799
+ unsetLoading();
1800
+ }
1801
+ delete pendingLoadTimeouts[loadId];
1802
+ set({ pendingLoadTimeouts });
1803
+ }, defer);
1804
+ set({
1805
+ pendingLoadTimeouts: __spreadProps(__spreadValues({}, pendingLoadTimeouts), {
1806
+ [id]: timeout
1807
+ })
1808
+ });
1809
+ return unsetLoading;
1810
+ },
1811
+ unsetComponentLoading: (id) => {
1812
+ const { setComponentLoading } = get();
1813
+ setComponentLoading(id, false);
1814
+ },
1815
+ // Helper
1816
+ setUi: (ui, recordHistory) => set((s) => {
1817
+ const dispatch = createReducer({
1818
+ record: () => {
1819
+ },
1820
+ appStore: s
1821
+ });
1822
+ const state = dispatch(s.state, {
1823
+ type: "setUi",
1824
+ ui,
1825
+ recordHistory
1826
+ });
1827
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1828
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1829
+ }),
1830
+ resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
1831
+ const { config, metadata, setComponentLoading, permissions } = get();
1832
+ const timeouts = {};
1833
+ return yield resolveComponentData(
1834
+ componentData,
1835
+ config,
1836
+ metadata,
1837
+ (item) => {
1838
+ const id = "id" in item.props ? item.props.id : "root";
1839
+ timeouts[id] = setComponentLoading(id, true, 50);
1840
+ },
1841
+ (item) => __async(void 0, null, function* () {
1842
+ const id = "id" in item.props ? item.props.id : "root";
1843
+ if ("type" in item) {
1844
+ yield permissions.refreshPermissions({ item });
1845
+ } else {
1846
+ yield permissions.refreshPermissions({ root: true });
1847
+ }
1848
+ timeouts[id]();
1849
+ }),
1850
+ trigger
1851
+ );
1852
+ }),
1853
+ resolveAndCommitData: () => __async(void 0, null, function* () {
1854
+ const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
1855
+ walkAppState(
1856
+ state,
1857
+ config,
1858
+ (content) => content,
1859
+ (childItem) => {
1860
+ resolveComponentData2(childItem, "load").then((resolved) => {
1861
+ const { state: state2 } = get();
1862
+ const node = state2.indexes.nodes[resolved.node.props.id];
1863
+ if (node && resolved.didChange) {
1864
+ if (resolved.node.props.id === "root") {
1865
+ dispatch({
1866
+ type: "replaceRoot",
1867
+ root: toRoot(resolved.node)
1868
+ });
1869
+ } else {
1870
+ const zoneCompound = `${node.parentId}:${node.zone}`;
1871
+ const parentZone = state2.indexes.zones[zoneCompound];
1872
+ const index = parentZone.contentIds.indexOf(
1873
+ resolved.node.props.id
1874
+ );
1875
+ dispatch({
1876
+ type: "replace",
1877
+ data: resolved.node,
1878
+ destinationIndex: index,
1879
+ destinationZone: zoneCompound
1880
+ });
1881
+ }
1882
+ }
1883
+ });
1884
+ return childItem;
1885
+ }
1886
+ );
1887
+ })
1888
+ });
1889
+ })
1539
1890
  );
1540
- var appStoreContext = (0, import_react10.createContext)(createAppStore());
1891
+ var appStoreContext = (0, import_react9.createContext)(createAppStore());
1541
1892
  function useAppStore(selector) {
1542
- const context = (0, import_react10.useContext)(appStoreContext);
1893
+ const context = (0, import_react9.useContext)(appStoreContext);
1543
1894
  return useStore(context, selector);
1544
1895
  }
1545
1896
  function useAppStoreApi() {
1546
- return (0, import_react10.useContext)(appStoreContext);
1897
+ return (0, import_react9.useContext)(appStoreContext);
1547
1898
  }
1548
1899
 
1549
1900
  // ../core/lib/use-breadcrumbs.ts
@@ -1555,12 +1906,12 @@ var useBreadcrumbs = (renderCount) => {
1555
1906
  const config = useAppStore((s) => s.config);
1556
1907
  const path = useAppStore((s) => {
1557
1908
  var _a;
1558
- return (_a = s.nodes.nodes[selectedId]) == null ? void 0 : _a.path;
1909
+ return (_a = s.state.indexes.nodes[selectedId]) == null ? void 0 : _a.path;
1559
1910
  });
1560
1911
  const appStore = useAppStoreApi();
1561
- return (0, import_react11.useMemo)(() => {
1912
+ return (0, import_react10.useMemo)(() => {
1562
1913
  const breadcrumbs = (path == null ? void 0 : path.map((zoneCompound) => {
1563
- var _a, _b;
1914
+ var _a, _b, _c;
1564
1915
  const [componentId] = zoneCompound.split(":");
1565
1916
  if (componentId === "root") {
1566
1917
  return {
@@ -1568,12 +1919,15 @@ var useBreadcrumbs = (renderCount) => {
1568
1919
  selector: null
1569
1920
  };
1570
1921
  }
1571
- const node = appStore.getState().nodes.nodes[componentId];
1572
- const label = node ? (_b = (_a = config.components[node.data.type]) == null ? void 0 : _a.label) != null ? _b : node.data.type : "Component";
1922
+ const node = appStore.getState().state.indexes.nodes[componentId];
1923
+ const parentId = node.path[node.path.length - 1];
1924
+ const contentIds = ((_a = appStore.getState().state.indexes.zones[parentId]) == null ? void 0 : _a.contentIds) || [];
1925
+ const index = contentIds.indexOf(componentId);
1926
+ const label = node ? (_c = (_b = config.components[node.data.type]) == null ? void 0 : _b.label) != null ? _c : node.data.type : "Component";
1573
1927
  return {
1574
1928
  label,
1575
1929
  selector: node ? {
1576
- index: node.index,
1930
+ index,
1577
1931
  zone: node.path[node.path.length - 1]
1578
1932
  } : null
1579
1933
  };
@@ -1594,6 +1948,12 @@ init_react_import();
1594
1948
  // ../core/lib/filter.ts
1595
1949
  init_react_import();
1596
1950
 
1951
+ // ../core/lib/data/reorder.ts
1952
+ init_react_import();
1953
+
1954
+ // ../core/lib/data/replace.ts
1955
+ init_react_import();
1956
+
1597
1957
  // css-module:/home/runner/work/puck/puck/packages/core/components/Loader/styles.module.css#css-module
1598
1958
  init_react_import();
1599
1959
  var styles_module_default3 = { "Loader": "_Loader_nacdm_13", "loader-animation": "_loader-animation_nacdm_1" };
@@ -1774,18 +2134,40 @@ function buildHierarchy(frame) {
1774
2134
  var usePuck = (0, import_puck.createUsePuck)();
1775
2135
  var HeadingAnalyzer = () => {
1776
2136
  const data = usePuck((s) => s.appState.data);
1777
- const [hierarchy, setHierarchy] = (0, import_react12.useState)([]);
1778
- (0, import_react12.useEffect)(() => {
2137
+ const [hierarchy, setHierarchy] = (0, import_react11.useState)([]);
2138
+ (0, import_react11.useEffect)(() => {
1779
2139
  const frame = getFrame();
1780
- const entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
1781
- if (!entry) return;
1782
- setHierarchy(buildHierarchy(entry));
1783
- const observer = new MutationObserver(() => {
2140
+ let entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2141
+ const createHierarchy = () => {
1784
2142
  setHierarchy(buildHierarchy(entry));
2143
+ };
2144
+ const entryObserver = new MutationObserver(() => {
2145
+ createHierarchy();
2146
+ });
2147
+ const frameObserver = new MutationObserver(() => {
2148
+ entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2149
+ if (entry) {
2150
+ registerEntryObserver();
2151
+ frameObserver.disconnect();
2152
+ }
1785
2153
  });
1786
- observer.observe(entry, { subtree: true, childList: true });
2154
+ const registerEntryObserver = () => {
2155
+ if (!entry) return;
2156
+ entryObserver.observe(entry, { subtree: true, childList: true });
2157
+ };
2158
+ const registerFrameObserver = () => {
2159
+ if (!frame) return;
2160
+ frameObserver.observe(frame, { subtree: true, childList: true });
2161
+ };
2162
+ if (entry) {
2163
+ createHierarchy();
2164
+ registerEntryObserver();
2165
+ } else {
2166
+ registerFrameObserver();
2167
+ }
1787
2168
  return () => {
1788
- observer.disconnect();
2169
+ entryObserver.disconnect();
2170
+ frameObserver.disconnect();
1789
2171
  };
1790
2172
  }, [data]);
1791
2173
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: getClassName5(), children: [