@measured/puck-plugin-heading-analyzer 0.19.0-canary.1fc19b5 → 0.19.0-canary.22f5e3a3

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
+ };
530
759
  }
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 });
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
+ );
538
802
  }
539
- return __spreadValues(__spreadValues({}, acc), related);
803
+ return content;
540
804
  },
541
- {}
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
+ }
542
815
  );
543
- };
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]
552
- });
816
+ }
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;
553
838
  }
554
- return acc;
839
+ return newContent;
555
840
  },
556
- {}
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
+ }
557
854
  );
558
855
  };
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;
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
869
+ });
870
+ }
871
+ return childItem;
872
+ }
873
+ );
566
874
  };
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);
850
1190
  }
851
- return { data, ui };
1191
+ if (action.type === "reorder") {
1192
+ return reorderAction(state, action, appStore);
1193
+ }
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,14 @@ 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
1423
  methods: { sync: () => null },
1275
- data: { props: { id }, type: "unknown" },
1276
- parentId: "",
1277
- zone: "",
1278
- path: [],
1279
- element: null,
1280
- index: -1
1424
+ element: null
1281
1425
  };
1282
1426
  const existingNode = s.nodes[id];
1283
1427
  set({
@@ -1307,36 +1451,61 @@ var createNodesSlice = (set, get) => ({
1307
1451
 
1308
1452
  // ../core/store/slices/permissions.ts
1309
1453
  init_react_import();
1310
- var import_react8 = require("react");
1454
+ var import_react7 = require("react");
1455
+
1456
+ // ../core/lib/data/flatten-data.ts
1457
+ init_react_import();
1458
+ var flattenData = (state, config) => {
1459
+ const data = [];
1460
+ walkAppState(
1461
+ state,
1462
+ config,
1463
+ (content) => content,
1464
+ (item) => {
1465
+ data.push(item);
1466
+ return null;
1467
+ }
1468
+ );
1469
+ return data;
1470
+ };
1471
+
1472
+ // ../core/lib/get-changed.ts
1473
+ init_react_import();
1474
+ var getChanged = (newItem, oldItem) => {
1475
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
1476
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
1477
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
1478
+ return __spreadProps(__spreadValues({}, acc), {
1479
+ [item]: oldItemProps[item] !== newItemProps[item]
1480
+ });
1481
+ }, {}) : {};
1482
+ };
1483
+
1484
+ // ../core/store/slices/permissions.ts
1311
1485
  var createPermissionsSlice = (set, get) => {
1312
1486
  const resolvePermissions = (..._0) => __async(void 0, [..._0], function* (params = {}, force) {
1313
- const { state, permissions } = get();
1314
- const { cache: cache3, globalPermissions } = permissions;
1487
+ const { state, permissions, config } = get();
1488
+ const { cache: cache2, globalPermissions } = permissions;
1315
1489
  const resolveDataForItem = (item2, force2 = false) => __async(void 0, null, function* () {
1316
1490
  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];
1491
+ const { config: config2, state: appState, setComponentLoading } = get();
1492
+ const componentConfig = item2.type === "root" ? config2.root : config2.components[item2.type];
1324
1493
  if (!componentConfig) {
1325
1494
  return;
1326
1495
  }
1327
1496
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), componentConfig.permissions);
1328
1497
  if (componentConfig.resolvePermissions) {
1329
- const changed = getChanged(item2, (_a = cache3[item2.props.id]) == null ? void 0 : _a.lastData);
1498
+ const changed = getChanged(item2, (_a = cache2[item2.props.id]) == null ? void 0 : _a.lastData);
1330
1499
  if (Object.values(changed).some((el) => el === true) || force2) {
1331
- setComponentLoading(item2.props.id);
1500
+ const clearTimeout2 = setComponentLoading(item2.props.id, true, 50);
1332
1501
  const resolvedPermissions = yield componentConfig.resolvePermissions(
1333
1502
  item2,
1334
1503
  {
1335
1504
  changed,
1336
- lastPermissions: ((_b = cache3[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1505
+ lastPermissions: ((_b = cache2[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1337
1506
  permissions: initialPermissions,
1338
- appState,
1339
- lastData: ((_c = cache3[item2.props.id]) == null ? void 0 : _c.lastData) || null
1507
+ appState: makeStatePublic(appState),
1508
+ lastData: ((_c = cache2[item2.props.id]) == null ? void 0 : _c.lastData) || null
1340
1509
  }
1341
1510
  );
1342
1511
  const latest = get().permissions;
@@ -1353,7 +1522,7 @@ var createPermissionsSlice = (set, get) => {
1353
1522
  })
1354
1523
  })
1355
1524
  });
1356
- unsetComponentLoading(item2.props.id);
1525
+ clearTimeout2();
1357
1526
  }
1358
1527
  }
1359
1528
  });
@@ -1363,7 +1532,7 @@ var createPermissionsSlice = (set, get) => {
1363
1532
  // Shim the root data in by conforming to component data shape
1364
1533
  {
1365
1534
  type: "root",
1366
- props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "puck-root" })
1535
+ props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "root" })
1367
1536
  },
1368
1537
  force2
1369
1538
  );
@@ -1372,14 +1541,13 @@ var createPermissionsSlice = (set, get) => {
1372
1541
  if (item) {
1373
1542
  yield resolveDataForItem(item, force);
1374
1543
  } else if (type) {
1375
- flattenData(state.data).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1544
+ flattenData(state, config).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1376
1545
  yield resolveDataForItem(item2, force);
1377
1546
  }));
1378
1547
  } else if (root) {
1379
1548
  resolveDataForRoot(force);
1380
1549
  } else {
1381
- resolveDataForRoot(force);
1382
- flattenData(state.data).map((item2) => __async(void 0, null, function* () {
1550
+ flattenData(state, config).map((item2) => __async(void 0, null, function* () {
1383
1551
  yield resolveDataForItem(item2, force);
1384
1552
  }));
1385
1553
  }
@@ -1409,7 +1577,7 @@ var createPermissionsSlice = (set, get) => {
1409
1577
  } else if (root) {
1410
1578
  const rootConfig = config.root;
1411
1579
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), rootConfig == null ? void 0 : rootConfig.permissions);
1412
- const resolvedForItem = resolvedPermissions["puck-root"];
1580
+ const resolvedForItem = resolvedPermissions["root"];
1413
1581
  return resolvedForItem ? __spreadValues(__spreadValues({}, globalPermissions), resolvedForItem) : initialPermissions;
1414
1582
  }
1415
1583
  return globalPermissions;
@@ -1421,16 +1589,99 @@ var createPermissionsSlice = (set, get) => {
1421
1589
 
1422
1590
  // ../core/store/slices/fields.ts
1423
1591
  init_react_import();
1424
- var import_react9 = require("react");
1425
- var createFieldsStore = (_set, _get) => {
1592
+ var import_react8 = require("react");
1593
+ var createFieldsSlice = (_set, _get) => {
1426
1594
  return {
1427
1595
  fields: {},
1428
1596
  loading: false,
1429
- lastResolvedData: {}
1597
+ lastResolvedData: {},
1598
+ id: void 0
1430
1599
  };
1431
1600
  };
1432
1601
 
1433
- // ../core/store/index.ts
1602
+ // ../core/lib/resolve-component-data.ts
1603
+ init_react_import();
1604
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1605
+ var cache = { lastChange: {} };
1606
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace", recursive = true) {
1607
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
1608
+ if ((configForItem == null ? void 0 : configForItem.resolveData) && item.props) {
1609
+ const id = "id" in item.props ? item.props.id : "root";
1610
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
1611
+ if (item && (0, import_fast_deep_equal.default)(item, oldItem)) {
1612
+ return { node: resolved, didChange: false };
1613
+ }
1614
+ const changed = getChanged(item, oldItem);
1615
+ if (onResolveStart) {
1616
+ onResolveStart(item);
1617
+ }
1618
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
1619
+ changed,
1620
+ lastData: oldItem,
1621
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
1622
+ trigger
1623
+ });
1624
+ let resolvedItem = __spreadProps(__spreadValues({}, item), {
1625
+ props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
1626
+ });
1627
+ if (recursive) {
1628
+ resolvedItem = yield mapSlotsAsync(
1629
+ resolvedItem,
1630
+ (content) => __async(void 0, null, function* () {
1631
+ return Promise.all(
1632
+ content.map(
1633
+ (childItem) => __async(void 0, null, function* () {
1634
+ return (yield resolveComponentData(
1635
+ childItem,
1636
+ config,
1637
+ metadata,
1638
+ onResolveStart,
1639
+ onResolveEnd,
1640
+ trigger,
1641
+ false
1642
+ )).node;
1643
+ })
1644
+ )
1645
+ );
1646
+ }),
1647
+ false,
1648
+ createIsSlotConfig(config)
1649
+ );
1650
+ }
1651
+ if (Object.keys(readOnly).length) {
1652
+ resolvedItem.readOnly = readOnly;
1653
+ }
1654
+ cache.lastChange[id] = {
1655
+ item,
1656
+ resolved: resolvedItem
1657
+ };
1658
+ if (onResolveEnd) {
1659
+ onResolveEnd(resolvedItem);
1660
+ }
1661
+ return { node: resolvedItem, didChange: !(0, import_fast_deep_equal.default)(item, resolvedItem) };
1662
+ }
1663
+ return { node: item, didChange: false };
1664
+ });
1665
+
1666
+ // ../core/lib/data/to-root.ts
1667
+ init_react_import();
1668
+ var toRoot = (item) => {
1669
+ if ("type" in item && item.type !== "root") {
1670
+ throw new Error("Converting non-root item to root.");
1671
+ }
1672
+ const { readOnly } = item;
1673
+ if (item.props) {
1674
+ if ("id" in item.props) {
1675
+ const _a = item.props, { id } = _a, props = __objRest(_a, ["id"]);
1676
+ return { props, readOnly };
1677
+ }
1678
+ return { props: item.props, readOnly };
1679
+ }
1680
+ return { props: {}, readOnly };
1681
+ };
1682
+
1683
+ // ../core/store/default-app-state.ts
1684
+ init_react_import();
1434
1685
  var defaultAppState = {
1435
1686
  data: { content: [], root: {}, zones: {} },
1436
1687
  ui: {
@@ -1450,100 +1701,196 @@ var defaultAppState = {
1450
1701
  controlsVisible: true
1451
1702
  },
1452
1703
  field: { focus: null }
1704
+ },
1705
+ indexes: {
1706
+ nodes: {},
1707
+ zones: {}
1453
1708
  }
1454
1709
  };
1710
+
1711
+ // ../core/store/index.ts
1455
1712
  var defaultPageFields = {
1456
1713
  title: { type: "text" }
1457
1714
  };
1458
1715
  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
- }))
1716
+ subscribeWithSelector((set, get) => {
1717
+ var _a, _b;
1718
+ return __spreadProps(__spreadValues({
1719
+ state: defaultAppState,
1720
+ config: { components: {} },
1721
+ componentState: {},
1722
+ plugins: [],
1723
+ overrides: {},
1724
+ viewports: defaultViewports,
1725
+ zoomConfig: {
1726
+ autoZoom: 1,
1727
+ rootHeight: 0,
1728
+ zoom: 1
1729
+ },
1730
+ status: "LOADING",
1731
+ iframe: {},
1732
+ metadata: {}
1733
+ }, initialAppStore), {
1734
+ fields: createFieldsSlice(set, get),
1735
+ history: createHistorySlice(set, get),
1736
+ nodes: createNodesSlice(set, get),
1737
+ permissions: createPermissionsSlice(set, get),
1738
+ getComponentConfig: (type) => {
1739
+ var _a2;
1740
+ const { config, selectedItem } = get();
1741
+ const rootFields = ((_a2 = config.root) == null ? void 0 : _a2.fields) || defaultPageFields;
1742
+ return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1743
+ },
1744
+ selectedItem: ((_a = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _a.ui.itemSelector) ? getItem(
1745
+ (_b = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _b.ui.itemSelector,
1746
+ initialAppStore.state
1747
+ ) : null,
1748
+ dispatch: (action) => set((s) => {
1749
+ var _a2, _b2;
1750
+ const { record } = get().history;
1751
+ const dispatch = createReducer({
1752
+ record,
1753
+ appStore: s
1754
+ });
1755
+ const state = dispatch(s.state, action);
1756
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1757
+ (_b2 = (_a2 = get()).onAction) == null ? void 0 : _b2.call(_a2, action, state, get().state);
1758
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1759
+ }),
1760
+ setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1761
+ setStatus: (status) => set({ status }),
1762
+ setComponentState: (componentState) => set({ componentState }),
1763
+ pendingLoadTimeouts: {},
1764
+ setComponentLoading: (id, loading = true, defer = 0) => {
1765
+ const { setComponentState, pendingLoadTimeouts } = get();
1766
+ const loadId = generateId();
1767
+ const setLoading = () => {
1768
+ var _a2;
1769
+ const { componentState } = get();
1770
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1771
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1772
+ loadingCount: (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) + 1
1773
+ })
1774
+ }));
1775
+ };
1776
+ const unsetLoading = () => {
1777
+ var _a2;
1778
+ const { componentState } = get();
1779
+ clearTimeout(timeout);
1780
+ delete pendingLoadTimeouts[loadId];
1781
+ set({ pendingLoadTimeouts });
1782
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1783
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1784
+ loadingCount: Math.max(
1785
+ (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) - 1,
1786
+ 0
1787
+ )
1788
+ })
1789
+ }));
1790
+ };
1791
+ const timeout = setTimeout(() => {
1792
+ if (loading) {
1793
+ setLoading();
1794
+ } else {
1795
+ unsetLoading();
1796
+ }
1797
+ delete pendingLoadTimeouts[loadId];
1798
+ set({ pendingLoadTimeouts });
1799
+ }, defer);
1800
+ set({
1801
+ pendingLoadTimeouts: __spreadProps(__spreadValues({}, pendingLoadTimeouts), {
1802
+ [id]: timeout
1803
+ })
1804
+ });
1805
+ return unsetLoading;
1806
+ },
1807
+ unsetComponentLoading: (id) => {
1808
+ const { setComponentLoading } = get();
1809
+ setComponentLoading(id, false);
1810
+ },
1811
+ // Helper
1812
+ setUi: (ui, recordHistory) => set((s) => {
1813
+ const dispatch = createReducer({
1814
+ record: () => {
1815
+ },
1816
+ appStore: s
1817
+ });
1818
+ const state = dispatch(s.state, {
1819
+ type: "setUi",
1820
+ ui,
1821
+ recordHistory
1822
+ });
1823
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1824
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1825
+ }),
1826
+ resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
1827
+ const { config, metadata, setComponentLoading, permissions } = get();
1828
+ const timeouts = {};
1829
+ return yield resolveComponentData(
1830
+ componentData,
1831
+ config,
1832
+ metadata,
1833
+ (item) => {
1834
+ const id = "id" in item.props ? item.props.id : "root";
1835
+ timeouts[id] = setComponentLoading(id, true, 50);
1836
+ },
1837
+ (item) => __async(void 0, null, function* () {
1838
+ const id = "id" in item.props ? item.props.id : "root";
1839
+ if ("type" in item) {
1840
+ yield permissions.refreshPermissions({ item });
1841
+ } else {
1842
+ yield permissions.refreshPermissions({ root: true });
1843
+ }
1844
+ timeouts[id]();
1845
+ }),
1846
+ trigger
1847
+ );
1848
+ }),
1849
+ resolveAndCommitData: () => __async(void 0, null, function* () {
1850
+ const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
1851
+ walkAppState(
1852
+ state,
1853
+ config,
1854
+ (content) => content,
1855
+ (childItem) => {
1856
+ resolveComponentData2(childItem, "load").then((resolved) => {
1857
+ const { state: state2 } = get();
1858
+ const node = state2.indexes.nodes[resolved.node.props.id];
1859
+ if (node && resolved.didChange) {
1860
+ if (resolved.node.props.id === "root") {
1861
+ dispatch({
1862
+ type: "replaceRoot",
1863
+ root: toRoot(resolved.node)
1864
+ });
1865
+ } else {
1866
+ const zoneCompound = `${node.parentId}:${node.zone}`;
1867
+ const parentZone = state2.indexes.zones[zoneCompound];
1868
+ const index = parentZone.contentIds.indexOf(
1869
+ resolved.node.props.id
1870
+ );
1871
+ dispatch({
1872
+ type: "replace",
1873
+ data: resolved.node,
1874
+ destinationIndex: index,
1875
+ destinationZone: zoneCompound
1876
+ });
1877
+ }
1878
+ }
1879
+ });
1880
+ return childItem;
1881
+ }
1882
+ );
1883
+ })
1884
+ });
1885
+ })
1539
1886
  );
1540
- var appStoreContext = (0, import_react10.createContext)(createAppStore());
1887
+ var appStoreContext = (0, import_react9.createContext)(createAppStore());
1541
1888
  function useAppStore(selector) {
1542
- const context = (0, import_react10.useContext)(appStoreContext);
1889
+ const context = (0, import_react9.useContext)(appStoreContext);
1543
1890
  return useStore(context, selector);
1544
1891
  }
1545
1892
  function useAppStoreApi() {
1546
- return (0, import_react10.useContext)(appStoreContext);
1893
+ return (0, import_react9.useContext)(appStoreContext);
1547
1894
  }
1548
1895
 
1549
1896
  // ../core/lib/use-breadcrumbs.ts
@@ -1555,12 +1902,12 @@ var useBreadcrumbs = (renderCount) => {
1555
1902
  const config = useAppStore((s) => s.config);
1556
1903
  const path = useAppStore((s) => {
1557
1904
  var _a;
1558
- return (_a = s.nodes.nodes[selectedId]) == null ? void 0 : _a.path;
1905
+ return (_a = s.state.indexes.nodes[selectedId]) == null ? void 0 : _a.path;
1559
1906
  });
1560
1907
  const appStore = useAppStoreApi();
1561
- return (0, import_react11.useMemo)(() => {
1908
+ return (0, import_react10.useMemo)(() => {
1562
1909
  const breadcrumbs = (path == null ? void 0 : path.map((zoneCompound) => {
1563
- var _a, _b;
1910
+ var _a, _b, _c;
1564
1911
  const [componentId] = zoneCompound.split(":");
1565
1912
  if (componentId === "root") {
1566
1913
  return {
@@ -1568,12 +1915,15 @@ var useBreadcrumbs = (renderCount) => {
1568
1915
  selector: null
1569
1916
  };
1570
1917
  }
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";
1918
+ const node = appStore.getState().state.indexes.nodes[componentId];
1919
+ const parentId = node.path[node.path.length - 1];
1920
+ const contentIds = ((_a = appStore.getState().state.indexes.zones[parentId]) == null ? void 0 : _a.contentIds) || [];
1921
+ const index = contentIds.indexOf(componentId);
1922
+ const label = node ? (_c = (_b = config.components[node.data.type]) == null ? void 0 : _b.label) != null ? _c : node.data.type : "Component";
1573
1923
  return {
1574
1924
  label,
1575
1925
  selector: node ? {
1576
- index: node.index,
1926
+ index,
1577
1927
  zone: node.path[node.path.length - 1]
1578
1928
  } : null
1579
1929
  };
@@ -1594,6 +1944,12 @@ init_react_import();
1594
1944
  // ../core/lib/filter.ts
1595
1945
  init_react_import();
1596
1946
 
1947
+ // ../core/lib/data/reorder.ts
1948
+ init_react_import();
1949
+
1950
+ // ../core/lib/data/replace.ts
1951
+ init_react_import();
1952
+
1597
1953
  // css-module:/home/runner/work/puck/puck/packages/core/components/Loader/styles.module.css#css-module
1598
1954
  init_react_import();
1599
1955
  var styles_module_default3 = { "Loader": "_Loader_nacdm_13", "loader-animation": "_loader-animation_nacdm_1" };
@@ -1774,18 +2130,40 @@ function buildHierarchy(frame) {
1774
2130
  var usePuck = (0, import_puck.createUsePuck)();
1775
2131
  var HeadingAnalyzer = () => {
1776
2132
  const data = usePuck((s) => s.appState.data);
1777
- const [hierarchy, setHierarchy] = (0, import_react12.useState)([]);
1778
- (0, import_react12.useEffect)(() => {
2133
+ const [hierarchy, setHierarchy] = (0, import_react11.useState)([]);
2134
+ (0, import_react11.useEffect)(() => {
1779
2135
  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(() => {
2136
+ let entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2137
+ const createHierarchy = () => {
1784
2138
  setHierarchy(buildHierarchy(entry));
2139
+ };
2140
+ const entryObserver = new MutationObserver(() => {
2141
+ createHierarchy();
2142
+ });
2143
+ const frameObserver = new MutationObserver(() => {
2144
+ entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2145
+ if (entry) {
2146
+ registerEntryObserver();
2147
+ frameObserver.disconnect();
2148
+ }
1785
2149
  });
1786
- observer.observe(entry, { subtree: true, childList: true });
2150
+ const registerEntryObserver = () => {
2151
+ if (!entry) return;
2152
+ entryObserver.observe(entry, { subtree: true, childList: true });
2153
+ };
2154
+ const registerFrameObserver = () => {
2155
+ if (!frame) return;
2156
+ frameObserver.observe(frame, { subtree: true, childList: true });
2157
+ };
2158
+ if (entry) {
2159
+ createHierarchy();
2160
+ registerEntryObserver();
2161
+ } else {
2162
+ registerFrameObserver();
2163
+ }
1787
2164
  return () => {
1788
- observer.disconnect();
2165
+ entryObserver.disconnect();
2166
+ frameObserver.disconnect();
1789
2167
  };
1790
2168
  }, [data]);
1791
2169
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: getClassName5(), children: [