@measured/puck-plugin-heading-analyzer 0.19.0-canary.b918900 → 0.19.0-canary.bc5bfff1

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
@@ -153,6 +153,120 @@ var require_classnames = __commonJS({
153
153
  }
154
154
  });
155
155
 
156
+ // ../../node_modules/flat/index.js
157
+ var require_flat = __commonJS({
158
+ "../../node_modules/flat/index.js"(exports2, module2) {
159
+ "use strict";
160
+ init_react_import();
161
+ module2.exports = flatten2;
162
+ flatten2.flatten = flatten2;
163
+ flatten2.unflatten = unflatten2;
164
+ function isBuffer(obj) {
165
+ return obj && obj.constructor && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj);
166
+ }
167
+ function keyIdentity(key) {
168
+ return key;
169
+ }
170
+ function flatten2(target, opts) {
171
+ opts = opts || {};
172
+ const delimiter = opts.delimiter || ".";
173
+ const maxDepth = opts.maxDepth;
174
+ const transformKey = opts.transformKey || keyIdentity;
175
+ const output = {};
176
+ function step(object, prev, currentDepth) {
177
+ currentDepth = currentDepth || 1;
178
+ Object.keys(object).forEach(function(key) {
179
+ const value = object[key];
180
+ const isarray = opts.safe && Array.isArray(value);
181
+ const type = Object.prototype.toString.call(value);
182
+ const isbuffer = isBuffer(value);
183
+ const isobject = type === "[object Object]" || type === "[object Array]";
184
+ const newKey = prev ? prev + delimiter + transformKey(key) : transformKey(key);
185
+ if (!isarray && !isbuffer && isobject && Object.keys(value).length && (!opts.maxDepth || currentDepth < maxDepth)) {
186
+ return step(value, newKey, currentDepth + 1);
187
+ }
188
+ output[newKey] = value;
189
+ });
190
+ }
191
+ step(target);
192
+ return output;
193
+ }
194
+ function unflatten2(target, opts) {
195
+ opts = opts || {};
196
+ const delimiter = opts.delimiter || ".";
197
+ const overwrite = opts.overwrite || false;
198
+ const transformKey = opts.transformKey || keyIdentity;
199
+ const result = {};
200
+ const isbuffer = isBuffer(target);
201
+ if (isbuffer || Object.prototype.toString.call(target) !== "[object Object]") {
202
+ return target;
203
+ }
204
+ function getkey(key) {
205
+ const parsedKey = Number(key);
206
+ return isNaN(parsedKey) || key.indexOf(".") !== -1 || opts.object ? key : parsedKey;
207
+ }
208
+ function addKeys(keyPrefix, recipient, target2) {
209
+ return Object.keys(target2).reduce(function(result2, key) {
210
+ result2[keyPrefix + delimiter + key] = target2[key];
211
+ return result2;
212
+ }, recipient);
213
+ }
214
+ function isEmpty(val) {
215
+ const type = Object.prototype.toString.call(val);
216
+ const isArray = type === "[object Array]";
217
+ const isObject = type === "[object Object]";
218
+ if (!val) {
219
+ return true;
220
+ } else if (isArray) {
221
+ return !val.length;
222
+ } else if (isObject) {
223
+ return !Object.keys(val).length;
224
+ }
225
+ }
226
+ target = Object.keys(target).reduce(function(result2, key) {
227
+ const type = Object.prototype.toString.call(target[key]);
228
+ const isObject = type === "[object Object]" || type === "[object Array]";
229
+ if (!isObject || isEmpty(target[key])) {
230
+ result2[key] = target[key];
231
+ return result2;
232
+ } else {
233
+ return addKeys(
234
+ key,
235
+ result2,
236
+ flatten2(target[key], opts)
237
+ );
238
+ }
239
+ }, {});
240
+ Object.keys(target).forEach(function(key) {
241
+ const split = key.split(delimiter).map(transformKey);
242
+ let key1 = getkey(split.shift());
243
+ let key2 = getkey(split[0]);
244
+ let recipient = result;
245
+ while (key2 !== void 0) {
246
+ if (key1 === "__proto__") {
247
+ return;
248
+ }
249
+ const type = Object.prototype.toString.call(recipient[key1]);
250
+ const isobject = type === "[object Object]" || type === "[object Array]";
251
+ if (!overwrite && !isobject && typeof recipient[key1] !== "undefined") {
252
+ return;
253
+ }
254
+ if (overwrite && !isobject || !overwrite && recipient[key1] == null) {
255
+ recipient[key1] = typeof key2 === "number" && !opts.object ? [] : {};
256
+ }
257
+ recipient = recipient[key1];
258
+ if (split.length > 0) {
259
+ key1 = getkey(split.shift());
260
+ key2 = getkey(split[0]);
261
+ }
262
+ }
263
+ recipient[key1] = unflatten2(target[key], opts);
264
+ });
265
+ return result;
266
+ }
267
+ }
268
+ });
269
+
156
270
  // ../../node_modules/fast-deep-equal/index.js
157
271
  var require_fast_deep_equal = __commonJS({
158
272
  "../../node_modules/fast-deep-equal/index.js"(exports2, module2) {
@@ -199,7 +313,7 @@ init_react_import();
199
313
 
200
314
  // src/HeadingAnalyzer.tsx
201
315
  init_react_import();
202
- var import_react12 = require("react");
316
+ var import_react11 = require("react");
203
317
 
204
318
  // css-module:/home/runner/work/puck/puck/packages/plugin-heading-analyzer/src/HeadingAnalyzer.module.css#css-module
205
319
  init_react_import();
@@ -360,7 +474,7 @@ var ChevronRight = createLucideIcon("ChevronRight", [
360
474
 
361
475
  // ../core/lib/use-breadcrumbs.ts
362
476
  init_react_import();
363
- var import_react11 = require("react");
477
+ var import_react10 = require("react");
364
478
 
365
479
  // ../core/store/index.ts
366
480
  init_react_import();
@@ -368,17 +482,17 @@ init_react_import();
368
482
  // ../core/reducer/index.ts
369
483
  init_react_import();
370
484
 
371
- // ../core/reducer/data.ts
485
+ // ../core/reducer/actions/set.ts
372
486
  init_react_import();
373
487
 
374
- // ../core/lib/reorder.ts
488
+ // ../core/lib/data/walk-app-state.ts
489
+ init_react_import();
490
+
491
+ // ../core/lib/data/for-related-zones.ts
492
+ init_react_import();
493
+
494
+ // ../core/lib/get-zone-id.ts
375
495
  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;
381
- };
382
496
 
383
497
  // ../core/lib/root-droppable-id.ts
384
498
  init_react_import();
@@ -386,58 +500,305 @@ var rootAreaId = "root";
386
500
  var rootZone = "default-zone";
387
501
  var rootDroppableId = `${rootAreaId}:${rootZone}`;
388
502
 
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;
503
+ // ../core/lib/get-zone-id.ts
504
+ var getZoneId = (zoneCompound) => {
505
+ if (!zoneCompound) {
506
+ return [];
507
+ }
508
+ if (zoneCompound && zoneCompound.indexOf(":") > -1) {
509
+ return zoneCompound.split(":");
510
+ }
511
+ return [rootDroppableId, zoneCompound];
395
512
  };
396
513
 
397
- // ../core/lib/remove.ts
514
+ // ../core/lib/data/for-related-zones.ts
515
+ function forRelatedZones(item, data, cb, path = []) {
516
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
517
+ const [parentId] = getZoneId(zoneCompound);
518
+ if (parentId === item.props.id) {
519
+ cb(path, zoneCompound, content);
520
+ }
521
+ });
522
+ }
523
+
524
+ // ../core/lib/data/map-slots.ts
398
525
  init_react_import();
399
- var remove = (list, index) => {
400
- const result = Array.from(list);
401
- result.splice(index, 1);
402
- return result;
403
- };
404
526
 
405
- // ../core/lib/setup-zone.ts
527
+ // ../core/lib/data/is-slot.ts
406
528
  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) : {}
529
+ var isSlot = (prop) => {
530
+ var _a, _b;
531
+ return Array.isArray(prop) && typeof ((_a = prop[0]) == null ? void 0 : _a.type) === "string" && typeof ((_b = prop[0]) == null ? void 0 : _b.props) === "object";
532
+ };
533
+ var createIsSlotConfig = (config) => (itemType, propName, propValue) => {
534
+ var _a, _b;
535
+ const configForComponent = itemType === "root" ? config == null ? void 0 : config.root : config == null ? void 0 : config.components[itemType];
536
+ if (!configForComponent) return isSlot(propValue);
537
+ return ((_b = (_a = configForComponent.fields) == null ? void 0 : _a[propName]) == null ? void 0 : _b.type) === "slot";
538
+ };
539
+
540
+ // ../core/lib/data/map-slots.ts
541
+ function mapSlotsAsync(_0, _1) {
542
+ return __async(this, arguments, function* (item, map, recursive = true, isSlot2 = isSlot) {
543
+ const props = __spreadValues({}, item.props);
544
+ const propKeys = Object.keys(props);
545
+ for (let i = 0; i < propKeys.length; i++) {
546
+ const propKey = propKeys[i];
547
+ const itemType = "type" in item ? item.type : "root";
548
+ if (isSlot2(itemType, propKey, props[propKey])) {
549
+ const content = props[propKey];
550
+ const mappedContent = recursive ? yield Promise.all(
551
+ content.map((item2) => __async(this, null, function* () {
552
+ return yield mapSlotsAsync(item2, map, recursive, isSlot2);
553
+ }))
554
+ ) : content;
555
+ props[propKey] = yield map(mappedContent, propKey);
556
+ }
557
+ }
558
+ return __spreadProps(__spreadValues({}, item), { props });
413
559
  });
414
- newData.zones[zoneKey] = newData.zones[zoneKey] || [];
415
- return newData;
560
+ }
561
+ var walkField = ({
562
+ value,
563
+ fields,
564
+ map,
565
+ propKey = "",
566
+ propPath = "",
567
+ id = ""
568
+ }) => {
569
+ var _a, _b, _c;
570
+ if (((_a = fields[propKey]) == null ? void 0 : _a.type) === "slot") {
571
+ const content = value || [];
572
+ return map(content, id, propPath, fields[propKey], propPath);
573
+ }
574
+ if (value && typeof value === "object") {
575
+ if (Array.isArray(value)) {
576
+ const arrayFields = ((_b = fields[propKey]) == null ? void 0 : _b.type) === "array" ? fields[propKey].arrayFields : null;
577
+ if (!arrayFields) return value;
578
+ return value.map(
579
+ (el, idx) => walkField({
580
+ value: el,
581
+ fields: arrayFields,
582
+ map,
583
+ propKey,
584
+ propPath: `${propPath}[${idx}]`,
585
+ id
586
+ })
587
+ );
588
+ } else if ("$$typeof" in value) {
589
+ return value;
590
+ } else {
591
+ const objectFields = ((_c = fields[propKey]) == null ? void 0 : _c.type) === "object" ? fields[propKey].objectFields : fields;
592
+ return Object.entries(value).reduce(
593
+ (acc, [k, v]) => {
594
+ const newValue = walkField({
595
+ value: v,
596
+ fields: objectFields,
597
+ map,
598
+ propKey: k,
599
+ propPath: `${propPath}.${k}`,
600
+ id
601
+ });
602
+ if (typeof newValue === "undefined" || newValue === v) return acc;
603
+ return __spreadProps(__spreadValues({}, acc), {
604
+ [k]: newValue
605
+ });
606
+ },
607
+ value
608
+ );
609
+ }
610
+ }
611
+ return value;
416
612
  };
613
+ function mapSlotsSync(item, map, config) {
614
+ var _a, _b, _c;
615
+ const itemType = "type" in item ? item.type : "root";
616
+ const componentConfig = itemType === "root" ? config.root : (_a = config.components) == null ? void 0 : _a[itemType];
617
+ const newProps = __spreadValues({}, (_b = item.props) != null ? _b : {});
618
+ Object.entries((_c = item.props) != null ? _c : {}).forEach(([k, v]) => {
619
+ var _a2, _b2;
620
+ const newValue = walkField({
621
+ value: v,
622
+ fields: (_a2 = componentConfig == null ? void 0 : componentConfig.fields) != null ? _a2 : {},
623
+ map,
624
+ propKey: k,
625
+ propPath: k,
626
+ id: (_b2 = item.props.id) != null ? _b2 : "root"
627
+ });
628
+ newProps[k] = newValue;
629
+ }, item.props);
630
+ return __spreadProps(__spreadValues({}, item), {
631
+ props: newProps
632
+ });
633
+ }
417
634
 
418
- // ../core/lib/replace.ts
635
+ // ../core/lib/data/flatten-node.ts
419
636
  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;
425
- };
637
+ var import_flat = __toESM(require_flat());
426
638
 
427
- // ../core/lib/get-item.ts
639
+ // ../core/lib/data/strip-slots.ts
428
640
  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;
641
+ var stripSlots = (data, config) => {
642
+ return mapSlotsSync(data, () => null, config);
643
+ };
644
+
645
+ // ../core/lib/data/flatten-node.ts
646
+ var flattenNode = (node, config) => {
647
+ return __spreadProps(__spreadValues({}, node), {
648
+ props: (0, import_flat.flatten)(stripSlots(node, config).props)
649
+ });
650
+ };
651
+
652
+ // ../core/lib/data/walk-app-state.ts
653
+ function walkAppState(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
654
+ var _a;
655
+ let newZones = {};
656
+ const newZoneIndex = {};
657
+ const newNodeIndex = {};
658
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
659
+ var _a2;
660
+ const [parentId] = zoneCompound.split(":");
661
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
662
+ const [_2, zone] = zoneCompound.split(":");
663
+ const newZoneCompound = `${newId || parentId}:${zone}`;
664
+ const newContent2 = mappedContent.map(
665
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
666
+ );
667
+ newZoneIndex[newZoneCompound] = {
668
+ contentIds: newContent2.map((item) => item.props.id),
669
+ type: zoneType
670
+ };
671
+ return [newZoneCompound, newContent2];
672
+ };
673
+ const processRelatedZones = (item, newId, initialPath) => {
674
+ forRelatedZones(
675
+ item,
676
+ state.data,
677
+ (relatedPath, relatedZoneCompound, relatedContent) => {
678
+ const [zoneCompound, newContent2] = processContent(
679
+ relatedPath,
680
+ relatedZoneCompound,
681
+ relatedContent,
682
+ "dropzone",
683
+ newId
684
+ );
685
+ newZones[zoneCompound] = newContent2;
686
+ },
687
+ initialPath
688
+ );
689
+ };
690
+ const processItem = (item, path, index) => {
691
+ const mappedItem = mapNodeOrSkip(item, path, index);
692
+ if (!mappedItem) return item;
693
+ const id = mappedItem.props.id;
694
+ const newProps = __spreadProps(__spreadValues({}, mapSlotsSync(
695
+ mappedItem,
696
+ (content, parentId2, slotId) => {
697
+ const zoneCompound = `${parentId2}:${slotId}`;
698
+ const [_2, newContent2] = processContent(
699
+ path,
700
+ zoneCompound,
701
+ content,
702
+ "slot",
703
+ parentId2
704
+ );
705
+ return newContent2;
706
+ },
707
+ config
708
+ ).props), {
709
+ id
710
+ });
711
+ processRelatedZones(item, id, path);
712
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
713
+ const thisZoneCompound = path[path.length - 1];
714
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
715
+ newNodeIndex[id] = {
716
+ data: newItem,
717
+ flatData: flattenNode(newItem, config),
718
+ path,
719
+ parentId,
720
+ zone
721
+ };
722
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
723
+ if (newProps.id === "root") {
724
+ delete finalData["type"];
725
+ delete finalData.props["id"];
726
+ }
727
+ return finalData;
728
+ };
729
+ const zones = state.data.zones || {};
730
+ const [_, newContent] = processContent(
731
+ [],
732
+ rootDroppableId,
733
+ state.data.content,
734
+ "root"
735
+ );
736
+ const processedContent = newContent;
737
+ const zonesAlreadyProcessed = Object.keys(newZones);
738
+ Object.keys(zones || {}).forEach((zoneCompound) => {
739
+ const [parentId] = zoneCompound.split(":");
740
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
741
+ return;
742
+ }
743
+ const [_2, newContent2] = processContent(
744
+ [rootDroppableId],
745
+ zoneCompound,
746
+ zones[zoneCompound],
747
+ "dropzone",
748
+ parentId
749
+ );
750
+ newZones[zoneCompound] = newContent2;
751
+ }, newZones);
752
+ const processedRoot = processItem(
753
+ {
754
+ type: "root",
755
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
756
+ },
757
+ [],
758
+ -1
759
+ );
760
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
761
+ props: processedRoot.props
762
+ });
763
+ return __spreadProps(__spreadValues({}, state), {
764
+ data: {
765
+ root,
766
+ content: processedContent,
767
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
768
+ },
769
+ indexes: {
770
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
771
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
772
+ }
773
+ });
436
774
  }
437
775
 
438
- // ../core/lib/reduce-related-zones.ts
776
+ // ../core/reducer/actions/set.ts
777
+ var setAction = (state, action, appStore) => {
778
+ if (typeof action.state === "object") {
779
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
780
+ if (action.state.indexes) {
781
+ return newState;
782
+ }
783
+ console.warn(
784
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
785
+ );
786
+ return walkAppState(newState, appStore.config);
787
+ }
788
+ return __spreadValues(__spreadValues({}, state), action.state(state));
789
+ };
790
+
791
+ // ../core/reducer/actions/insert.ts
439
792
  init_react_import();
440
793
 
794
+ // ../core/lib/data/insert.ts
795
+ init_react_import();
796
+ var insert = (list, index, item) => {
797
+ const result = Array.from(list || []);
798
+ result.splice(index, 0, item);
799
+ return result;
800
+ };
801
+
441
802
  // ../core/lib/generate-id.ts
442
803
  init_react_import();
443
804
 
@@ -500,313 +861,437 @@ var v4_default = v4;
500
861
  // ../core/lib/generate-id.ts
501
862
  var generateId = (type) => type ? `${type}-${v4_default()}` : v4_default();
502
863
 
503
- // ../core/lib/get-zone-id.ts
864
+ // ../core/lib/data/get-ids-for-parent.ts
504
865
  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];
866
+ var getIdsForParent = (zoneCompound, state) => {
867
+ const [parentId] = zoneCompound.split(":");
868
+ const node = state.indexes.nodes[parentId];
869
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
513
870
  };
514
871
 
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] });
872
+ // ../core/lib/data/populate-ids.ts
873
+ init_react_import();
874
+
875
+ // ../core/lib/data/walk-tree.ts
876
+ init_react_import();
877
+ function walkTree(data, config, callbackFn) {
878
+ var _a, _b;
879
+ const walkItem = (item) => {
880
+ return mapSlotsSync(
881
+ item,
882
+ (content, parentId, propName) => {
883
+ var _a2;
884
+ return (_a2 = callbackFn(content, { parentId, propName })) != null ? _a2 : content;
526
885
  },
886
+ config
887
+ );
888
+ };
889
+ if ("props" in data) {
890
+ return walkItem(data);
891
+ }
892
+ const _data = data;
893
+ const zones = (_a = _data.zones) != null ? _a : {};
894
+ const mappedContent = _data.content.map(walkItem);
895
+ return {
896
+ root: walkItem(_data.root),
897
+ content: (_b = callbackFn(mappedContent, {
898
+ parentId: "root",
899
+ propName: "default-zone"
900
+ })) != null ? _b : mappedContent,
901
+ zones: Object.keys(zones).reduce(
902
+ (acc, zoneCompound) => __spreadProps(__spreadValues({}, acc), {
903
+ [zoneCompound]: zones[zoneCompound].map(walkItem)
904
+ }),
527
905
  {}
528
906
  )
529
- });
907
+ };
908
+ }
909
+
910
+ // ../core/lib/data/populate-ids.ts
911
+ var populateIds = (data, config, override = false) => {
912
+ const id = generateId(data.type);
913
+ return walkTree(
914
+ __spreadProps(__spreadValues({}, data), {
915
+ props: override ? __spreadProps(__spreadValues({}, data.props), { id }) : __spreadValues({ id }, data.props)
916
+ }),
917
+ config,
918
+ (contents) => contents.map((item) => {
919
+ const id2 = generateId(item.type);
920
+ return __spreadProps(__spreadValues({}, item), {
921
+ props: override ? __spreadProps(__spreadValues({}, item.props), { id: id2 }) : __spreadValues({ id: id2 }, item.props)
922
+ });
923
+ })
924
+ );
925
+ };
926
+
927
+ // ../core/reducer/actions/insert.ts
928
+ function insertAction(state, action, appStore) {
929
+ const id = action.id || generateId(action.componentType);
930
+ const emptyComponentData = populateIds(
931
+ {
932
+ type: action.componentType,
933
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
934
+ id
935
+ })
936
+ },
937
+ appStore.config
938
+ );
939
+ const [parentId] = action.destinationZone.split(":");
940
+ const idsInPath = getIdsForParent(action.destinationZone, state);
941
+ return walkAppState(
942
+ state,
943
+ appStore.config,
944
+ (content, zoneCompound) => {
945
+ if (zoneCompound === action.destinationZone) {
946
+ return insert(
947
+ content || [],
948
+ action.destinationIndex,
949
+ emptyComponentData
950
+ );
951
+ }
952
+ return content;
953
+ },
954
+ (childItem, path) => {
955
+ if (childItem.props.id === id || childItem.props.id === parentId) {
956
+ return childItem;
957
+ } else if (idsInPath.includes(childItem.props.id)) {
958
+ return childItem;
959
+ } else if (path.includes(action.destinationZone)) {
960
+ return childItem;
961
+ }
962
+ return null;
963
+ }
964
+ );
530
965
  }
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 });
966
+
967
+ // ../core/reducer/actions/replace.ts
968
+ init_react_import();
969
+ var replaceAction = (state, action, appStore) => {
970
+ const [parentId] = action.destinationZone.split(":");
971
+ const idsInPath = getIdsForParent(action.destinationZone, state);
972
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
973
+ const idChanged = originalId !== action.data.props.id;
974
+ if (idChanged) {
975
+ throw new Error(
976
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
977
+ );
978
+ }
979
+ const data = populateIds(action.data, appStore.config);
980
+ return walkAppState(
981
+ state,
982
+ appStore.config,
983
+ (content, zoneCompound) => {
984
+ const newContent = [...content];
985
+ if (zoneCompound === action.destinationZone) {
986
+ newContent[action.destinationIndex] = data;
538
987
  }
539
- return __spreadValues(__spreadValues({}, acc), related);
988
+ return newContent;
540
989
  },
541
- {}
990
+ (childItem, path) => {
991
+ const pathIds = path.map((p) => p.split(":")[0]);
992
+ if (childItem.props.id === data.props.id) {
993
+ return data;
994
+ } else if (childItem.props.id === parentId) {
995
+ return childItem;
996
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
997
+ return childItem;
998
+ } else if (pathIds.indexOf(data.props.id) > -1) {
999
+ return childItem;
1000
+ }
1001
+ return null;
1002
+ }
542
1003
  );
543
1004
  };
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]
1005
+
1006
+ // ../core/reducer/actions/replace-root.ts
1007
+ init_react_import();
1008
+ var replaceRootAction = (state, action, appStore) => {
1009
+ return walkAppState(
1010
+ state,
1011
+ appStore.config,
1012
+ (content) => content,
1013
+ (childItem) => {
1014
+ if (childItem.props.id === "root") {
1015
+ return __spreadProps(__spreadValues({}, childItem), {
1016
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
1017
+ readOnly: action.root.readOnly
552
1018
  });
553
1019
  }
554
- return acc;
555
- },
556
- {}
1020
+ return childItem;
1021
+ }
557
1022
  );
558
1023
  };
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
- });
1024
+
1025
+ // ../core/reducer/actions/duplicate.ts
1026
+ init_react_import();
1027
+
1028
+ // ../core/lib/data/get-item.ts
1029
+ init_react_import();
1030
+ function getItem(selector, state) {
1031
+ var _a, _b;
1032
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
1033
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
582
1034
  }
583
1035
 
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
- )
1036
+ // ../core/reducer/actions/duplicate.ts
1037
+ function duplicateAction(state, action, appStore) {
1038
+ const item = getItem(
1039
+ { index: action.sourceIndex, zone: action.sourceZone },
1040
+ state
1041
+ );
1042
+ const idsInPath = getIdsForParent(action.sourceZone, state);
1043
+ const newItem = __spreadProps(__spreadValues({}, item), {
1044
+ props: __spreadProps(__spreadValues({}, item.props), {
1045
+ id: generateId(item.type)
600
1046
  })
601
1047
  });
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
- )
1048
+ const modified = walkAppState(
1049
+ state,
1050
+ appStore.config,
1051
+ (content, zoneCompound) => {
1052
+ if (zoneCompound === action.sourceZone) {
1053
+ return insert(content, action.sourceIndex + 1, item);
1054
+ }
1055
+ return content;
1056
+ },
1057
+ (childItem, path, index) => {
1058
+ const zoneCompound = path[path.length - 1];
1059
+ const parents = path.map((p) => p.split(":")[0]);
1060
+ if (parents.indexOf(newItem.props.id) > -1) {
1061
+ return __spreadProps(__spreadValues({}, childItem), {
1062
+ props: __spreadProps(__spreadValues({}, childItem.props), {
1063
+ id: generateId(childItem.type)
1064
+ })
1065
+ });
1066
+ }
1067
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
1068
+ return newItem;
1069
+ }
1070
+ const [sourceZoneParent] = action.sourceZone.split(":");
1071
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
1072
+ return childItem;
1073
+ }
1074
+ return null;
1075
+ }
1076
+ );
1077
+ return __spreadProps(__spreadValues({}, modified), {
1078
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
1079
+ itemSelector: {
1080
+ index: action.sourceIndex + 1,
1081
+ zone: action.sourceZone
1082
+ }
627
1083
  })
628
1084
  });
629
1085
  }
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
- });
1086
+
1087
+ // ../core/reducer/actions/reorder.ts
1088
+ init_react_import();
1089
+
1090
+ // ../core/reducer/actions/move.ts
1091
+ init_react_import();
1092
+
1093
+ // ../core/lib/data/remove.ts
1094
+ init_react_import();
1095
+ var remove = (list, index) => {
1096
+ const result = Array.from(list);
1097
+ result.splice(index, 1);
1098
+ return result;
650
1099
  };
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);
1100
+
1101
+ // ../core/reducer/actions/move.ts
1102
+ var moveAction = (state, action, appStore) => {
1103
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
1104
+ return state;
687
1105
  }
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" }));
1106
+ const item = getItem(
1107
+ { zone: action.sourceZone, index: action.sourceIndex },
1108
+ state
1109
+ );
1110
+ if (!item) return state;
1111
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
1112
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
1113
+ return walkAppState(
1114
+ state,
1115
+ appStore.config,
1116
+ (content, zoneCompound) => {
1117
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
1118
+ return insert(
1119
+ remove(content, action.sourceIndex),
1120
+ action.destinationIndex,
1121
+ item
1122
+ );
1123
+ } else if (zoneCompound === action.sourceZone) {
1124
+ return remove(content, action.sourceIndex);
1125
+ } else if (zoneCompound === action.destinationZone) {
1126
+ return insert(content, action.destinationIndex, item);
1127
+ }
1128
+ return content;
1129
+ },
1130
+ (childItem, path) => {
1131
+ const [sourceZoneParent] = action.sourceZone.split(":");
1132
+ const [destinationZoneParent] = action.destinationZone.split(":");
1133
+ const childId = childItem.props.id;
1134
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
1135
+ return childItem;
1136
+ }
1137
+ return null;
702
1138
  }
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
- });
1139
+ );
1140
+ };
1141
+
1142
+ // ../core/reducer/actions/reorder.ts
1143
+ var reorderAction = (state, action, appStore) => {
1144
+ return moveAction(
1145
+ state,
1146
+ {
1147
+ type: "move",
1148
+ sourceIndex: action.sourceIndex,
1149
+ sourceZone: action.destinationZone,
1150
+ destinationIndex: action.destinationIndex,
1151
+ destinationZone: action.destinationZone
1152
+ },
1153
+ appStore
1154
+ );
1155
+ };
1156
+
1157
+ // ../core/reducer/actions/remove.ts
1158
+ init_react_import();
1159
+ var removeAction = (state, action, appStore) => {
1160
+ const item = getItem({ index: action.index, zone: action.zone }, state);
1161
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
1162
+ (acc, [nodeId, nodeData]) => {
1163
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
1164
+ if (pathIds.includes(item.props.id)) {
1165
+ return [...acc, nodeId];
1166
+ }
1167
+ return acc;
1168
+ },
1169
+ [item.props.id]
1170
+ );
1171
+ const newState = walkAppState(
1172
+ state,
1173
+ appStore.config,
1174
+ (content, zoneCompound) => {
1175
+ if (zoneCompound === action.zone) {
1176
+ return remove(content, action.index);
1177
+ }
1178
+ return content;
714
1179
  }
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
- });
1180
+ );
1181
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
1182
+ const parentId = zoneCompound.split(":")[0];
1183
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
1184
+ delete newState.data.zones[zoneCompound];
725
1185
  }
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
- });
1186
+ });
1187
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
1188
+ const parentId = zoneCompound.split(":")[0];
1189
+ if (nodesToDelete.includes(parentId)) {
1190
+ delete newState.indexes.zones[zoneCompound];
753
1191
  }
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
- });
1192
+ });
1193
+ nodesToDelete.forEach((id) => {
1194
+ delete newState.indexes.nodes[id];
1195
+ });
1196
+ return newState;
1197
+ };
1198
+
1199
+ // ../core/reducer/actions/register-zone.ts
1200
+ init_react_import();
1201
+
1202
+ // ../core/lib/data/setup-zone.ts
1203
+ init_react_import();
1204
+ var setupZone = (data, zoneKey) => {
1205
+ if (zoneKey === rootDroppableId) {
1206
+ return data;
762
1207
  }
763
- if (action.type === "registerZone") {
764
- if (zoneCache[action.zone]) {
765
- return __spreadProps(__spreadValues({}, data), {
766
- zones: __spreadProps(__spreadValues({}, data.zones), {
1208
+ const newData = __spreadProps(__spreadValues({}, data), {
1209
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
1210
+ });
1211
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
1212
+ return newData;
1213
+ };
1214
+
1215
+ // ../core/reducer/actions/register-zone.ts
1216
+ var zoneCache = {};
1217
+ function registerZoneAction(state, action) {
1218
+ if (zoneCache[action.zone]) {
1219
+ return __spreadProps(__spreadValues({}, state), {
1220
+ data: __spreadProps(__spreadValues({}, state.data), {
1221
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
767
1222
  [action.zone]: zoneCache[action.zone]
768
1223
  })
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 });
1224
+ }),
1225
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
1226
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
1227
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
1228
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
1229
+ type: "dropzone"
1230
+ })
1231
+ })
1232
+ })
1233
+ });
780
1234
  }
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));
1235
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
1236
+ }
1237
+ function unregisterZoneAction(state, action) {
1238
+ const _zones = __spreadValues({}, state.data.zones || {});
1239
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
1240
+ if (_zones[action.zone]) {
1241
+ zoneCache[action.zone] = _zones[action.zone];
1242
+ delete _zones[action.zone];
786
1243
  }
787
- return data;
1244
+ delete zoneIndex[action.zone];
1245
+ return __spreadProps(__spreadValues({}, state), {
1246
+ data: __spreadProps(__spreadValues({}, state.data), {
1247
+ zones: _zones
1248
+ }),
1249
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
1250
+ zones: zoneIndex
1251
+ })
1252
+ });
788
1253
  }
789
1254
 
790
- // ../core/reducer/state.ts
1255
+ // ../core/reducer/actions/set-data.ts
791
1256
  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
- });
1257
+ var setDataAction = (state, action, appStore) => {
1258
+ if (typeof action.data === "object") {
1259
+ console.warn(
1260
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
1261
+ );
1262
+ return walkAppState(
1263
+ __spreadProps(__spreadValues({}, state), {
1264
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
1265
+ }),
1266
+ appStore.config
1267
+ );
803
1268
  }
804
- if (action.type === "remove") {
805
- return __spreadProps(__spreadValues({}, ui), {
806
- itemSelector: null
1269
+ return walkAppState(
1270
+ __spreadProps(__spreadValues({}, state), {
1271
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
1272
+ }),
1273
+ appStore.config
1274
+ );
1275
+ };
1276
+
1277
+ // ../core/reducer/actions/set-ui.ts
1278
+ init_react_import();
1279
+ var setUiAction = (state, action) => {
1280
+ if (typeof action.ui === "object") {
1281
+ return __spreadProps(__spreadValues({}, state), {
1282
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
807
1283
  });
808
1284
  }
809
- return ui;
1285
+ return __spreadProps(__spreadValues({}, state), {
1286
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
1287
+ });
1288
+ };
1289
+
1290
+ // ../core/lib/data/make-state-public.ts
1291
+ init_react_import();
1292
+ var makeStatePublic = (state) => {
1293
+ const { data, ui } = state;
1294
+ return { data, ui };
810
1295
  };
811
1296
 
812
1297
  // ../core/reducer/actions.tsx
@@ -826,29 +1311,54 @@ function storeInterceptor(reducer, record, onAction) {
826
1311
  if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
827
1312
  if (record) record(newAppState);
828
1313
  }
829
- onAction == null ? void 0 : onAction(action, newAppState, state);
1314
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
830
1315
  return newAppState;
831
1316
  };
832
1317
  }
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
1318
  function createReducer({
840
- config,
841
1319
  record,
842
- onAction
1320
+ onAction,
1321
+ appStore
843
1322
  }) {
844
1323
  return storeInterceptor(
845
1324
  (state, action) => {
846
- const data = reduceData(state.data, action, config);
847
- const ui = reduceUi(state.ui, action);
848
1325
  if (action.type === "set") {
849
- return setAction(state, action);
1326
+ return setAction(state, action, appStore);
1327
+ }
1328
+ if (action.type === "insert") {
1329
+ return insertAction(state, action, appStore);
1330
+ }
1331
+ if (action.type === "replace") {
1332
+ return replaceAction(state, action, appStore);
1333
+ }
1334
+ if (action.type === "replaceRoot") {
1335
+ return replaceRootAction(state, action, appStore);
1336
+ }
1337
+ if (action.type === "duplicate") {
1338
+ return duplicateAction(state, action, appStore);
850
1339
  }
851
- return { data, ui };
1340
+ if (action.type === "reorder") {
1341
+ return reorderAction(state, action, appStore);
1342
+ }
1343
+ if (action.type === "move") {
1344
+ return moveAction(state, action, appStore);
1345
+ }
1346
+ if (action.type === "remove") {
1347
+ return removeAction(state, action, appStore);
1348
+ }
1349
+ if (action.type === "registerZone") {
1350
+ return registerZoneAction(state, action);
1351
+ }
1352
+ if (action.type === "unregisterZone") {
1353
+ return unregisterZoneAction(state, action);
1354
+ }
1355
+ if (action.type === "setData") {
1356
+ return setDataAction(state, action, appStore);
1357
+ }
1358
+ if (action.type === "setUi") {
1359
+ return setUiAction(state, action);
1360
+ }
1361
+ return state;
852
1362
  },
853
1363
  record,
854
1364
  onAction
@@ -868,11 +1378,11 @@ init_react_import();
868
1378
  var createStoreImpl = (createState) => {
869
1379
  let state;
870
1380
  const listeners = /* @__PURE__ */ new Set();
871
- const setState = (partial, replace2) => {
1381
+ const setState = (partial, replace) => {
872
1382
  const nextState = typeof partial === "function" ? partial(state) : partial;
873
1383
  if (!Object.is(nextState, state)) {
874
1384
  const previousState = state;
875
- state = (replace2 != null ? replace2 : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
1385
+ state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
876
1386
  listeners.forEach((listener) => listener(state, previousState));
877
1387
  }
878
1388
  };
@@ -936,208 +1446,8 @@ var subscribeWithSelectorImpl = (fn) => (set, get, api) => {
936
1446
  };
937
1447
  var subscribeWithSelector = subscribeWithSelectorImpl;
938
1448
 
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
1449
  // ../core/store/index.ts
1140
- var import_react10 = require("react");
1450
+ var import_react9 = require("react");
1141
1451
 
1142
1452
  // ../core/store/slices/history.ts
1143
1453
  init_react_import();
@@ -1243,7 +1553,7 @@ var createHistorySlice = (set, get) => {
1243
1553
  const { dispatch, history } = get();
1244
1554
  dispatch({
1245
1555
  type: "set",
1246
- state: ((_a = history.histories[history.index]) == null ? void 0 : _a.state) || history.initialAppState
1556
+ state: ((_a = history.histories[index]) == null ? void 0 : _a.state) || history.initialAppState
1247
1557
  });
1248
1558
  set({ history: __spreadProps(__spreadValues({}, history), { index }) });
1249
1559
  },
@@ -1253,31 +1563,18 @@ var createHistorySlice = (set, get) => {
1253
1563
 
1254
1564
  // ../core/store/slices/nodes.ts
1255
1565
  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
1566
  var createNodesSlice = (set, get) => ({
1266
1567
  nodes: {},
1267
1568
  registerNode: (id, node) => {
1268
1569
  const s = get().nodes;
1269
- if (s.nodes[id] && partialDeepEqual(node, s.nodes[id])) {
1270
- return;
1271
- }
1272
1570
  const emptyNode = {
1273
1571
  id,
1274
- methods: { sync: () => null },
1275
- data: { props: { id }, type: "unknown" },
1276
- parentId: "",
1277
- zone: "",
1278
- path: [],
1279
- element: null,
1280
- index: -1
1572
+ methods: {
1573
+ sync: () => null,
1574
+ hideOverlay: () => null,
1575
+ showOverlay: () => null
1576
+ },
1577
+ element: null
1281
1578
  };
1282
1579
  const existingNode = s.nodes[id];
1283
1580
  set({
@@ -1307,36 +1604,61 @@ var createNodesSlice = (set, get) => ({
1307
1604
 
1308
1605
  // ../core/store/slices/permissions.ts
1309
1606
  init_react_import();
1310
- var import_react8 = require("react");
1607
+ var import_react7 = require("react");
1608
+
1609
+ // ../core/lib/data/flatten-data.ts
1610
+ init_react_import();
1611
+ var flattenData = (state, config) => {
1612
+ const data = [];
1613
+ walkAppState(
1614
+ state,
1615
+ config,
1616
+ (content) => content,
1617
+ (item) => {
1618
+ data.push(item);
1619
+ return null;
1620
+ }
1621
+ );
1622
+ return data;
1623
+ };
1624
+
1625
+ // ../core/lib/get-changed.ts
1626
+ init_react_import();
1627
+ var getChanged = (newItem, oldItem) => {
1628
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
1629
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
1630
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
1631
+ return __spreadProps(__spreadValues({}, acc), {
1632
+ [item]: oldItemProps[item] !== newItemProps[item]
1633
+ });
1634
+ }, {}) : {};
1635
+ };
1636
+
1637
+ // ../core/store/slices/permissions.ts
1311
1638
  var createPermissionsSlice = (set, get) => {
1312
1639
  const resolvePermissions = (..._0) => __async(void 0, [..._0], function* (params = {}, force) {
1313
- const { state, permissions } = get();
1314
- const { cache: cache3, globalPermissions } = permissions;
1640
+ const { state, permissions, config } = get();
1641
+ const { cache: cache2, globalPermissions } = permissions;
1315
1642
  const resolveDataForItem = (item2, force2 = false) => __async(void 0, null, function* () {
1316
1643
  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];
1644
+ const { config: config2, state: appState, setComponentLoading } = get();
1645
+ const componentConfig = item2.type === "root" ? config2.root : config2.components[item2.type];
1324
1646
  if (!componentConfig) {
1325
1647
  return;
1326
1648
  }
1327
1649
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), componentConfig.permissions);
1328
1650
  if (componentConfig.resolvePermissions) {
1329
- const changed = getChanged(item2, (_a = cache3[item2.props.id]) == null ? void 0 : _a.lastData);
1651
+ const changed = getChanged(item2, (_a = cache2[item2.props.id]) == null ? void 0 : _a.lastData);
1330
1652
  if (Object.values(changed).some((el) => el === true) || force2) {
1331
- setComponentLoading(item2.props.id);
1653
+ const clearTimeout2 = setComponentLoading(item2.props.id, true, 50);
1332
1654
  const resolvedPermissions = yield componentConfig.resolvePermissions(
1333
1655
  item2,
1334
1656
  {
1335
1657
  changed,
1336
- lastPermissions: ((_b = cache3[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1658
+ lastPermissions: ((_b = cache2[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1337
1659
  permissions: initialPermissions,
1338
- appState,
1339
- lastData: ((_c = cache3[item2.props.id]) == null ? void 0 : _c.lastData) || null
1660
+ appState: makeStatePublic(appState),
1661
+ lastData: ((_c = cache2[item2.props.id]) == null ? void 0 : _c.lastData) || null
1340
1662
  }
1341
1663
  );
1342
1664
  const latest = get().permissions;
@@ -1353,7 +1675,7 @@ var createPermissionsSlice = (set, get) => {
1353
1675
  })
1354
1676
  })
1355
1677
  });
1356
- unsetComponentLoading(item2.props.id);
1678
+ clearTimeout2();
1357
1679
  }
1358
1680
  }
1359
1681
  });
@@ -1363,7 +1685,7 @@ var createPermissionsSlice = (set, get) => {
1363
1685
  // Shim the root data in by conforming to component data shape
1364
1686
  {
1365
1687
  type: "root",
1366
- props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "puck-root" })
1688
+ props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "root" })
1367
1689
  },
1368
1690
  force2
1369
1691
  );
@@ -1372,14 +1694,13 @@ var createPermissionsSlice = (set, get) => {
1372
1694
  if (item) {
1373
1695
  yield resolveDataForItem(item, force);
1374
1696
  } else if (type) {
1375
- flattenData(state.data).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1697
+ flattenData(state, config).filter((item2) => item2.type === type).map((item2) => __async(void 0, null, function* () {
1376
1698
  yield resolveDataForItem(item2, force);
1377
1699
  }));
1378
1700
  } else if (root) {
1379
1701
  resolveDataForRoot(force);
1380
1702
  } else {
1381
- resolveDataForRoot(force);
1382
- flattenData(state.data).map((item2) => __async(void 0, null, function* () {
1703
+ flattenData(state, config).map((item2) => __async(void 0, null, function* () {
1383
1704
  yield resolveDataForItem(item2, force);
1384
1705
  }));
1385
1706
  }
@@ -1409,7 +1730,7 @@ var createPermissionsSlice = (set, get) => {
1409
1730
  } else if (root) {
1410
1731
  const rootConfig = config.root;
1411
1732
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), rootConfig == null ? void 0 : rootConfig.permissions);
1412
- const resolvedForItem = resolvedPermissions["puck-root"];
1733
+ const resolvedForItem = resolvedPermissions["root"];
1413
1734
  return resolvedForItem ? __spreadValues(__spreadValues({}, globalPermissions), resolvedForItem) : initialPermissions;
1414
1735
  }
1415
1736
  return globalPermissions;
@@ -1421,16 +1742,99 @@ var createPermissionsSlice = (set, get) => {
1421
1742
 
1422
1743
  // ../core/store/slices/fields.ts
1423
1744
  init_react_import();
1424
- var import_react9 = require("react");
1425
- var createFieldsStore = (_set, _get) => {
1745
+ var import_react8 = require("react");
1746
+ var createFieldsSlice = (_set, _get) => {
1426
1747
  return {
1427
1748
  fields: {},
1428
1749
  loading: false,
1429
- lastResolvedData: {}
1750
+ lastResolvedData: {},
1751
+ id: void 0
1430
1752
  };
1431
1753
  };
1432
1754
 
1433
- // ../core/store/index.ts
1755
+ // ../core/lib/resolve-component-data.ts
1756
+ init_react_import();
1757
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1758
+ var cache = { lastChange: {} };
1759
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace", recursive = true) {
1760
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
1761
+ if ((configForItem == null ? void 0 : configForItem.resolveData) && item.props) {
1762
+ const id = "id" in item.props ? item.props.id : "root";
1763
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
1764
+ if (item && (0, import_fast_deep_equal.default)(item, oldItem)) {
1765
+ return { node: resolved, didChange: false };
1766
+ }
1767
+ const changed = getChanged(item, oldItem);
1768
+ if (onResolveStart) {
1769
+ onResolveStart(item);
1770
+ }
1771
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
1772
+ changed,
1773
+ lastData: oldItem,
1774
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
1775
+ trigger
1776
+ });
1777
+ let resolvedItem = __spreadProps(__spreadValues({}, item), {
1778
+ props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
1779
+ });
1780
+ if (recursive) {
1781
+ resolvedItem = yield mapSlotsAsync(
1782
+ resolvedItem,
1783
+ (content) => __async(void 0, null, function* () {
1784
+ return Promise.all(
1785
+ content.map(
1786
+ (childItem) => __async(void 0, null, function* () {
1787
+ return (yield resolveComponentData(
1788
+ childItem,
1789
+ config,
1790
+ metadata,
1791
+ onResolveStart,
1792
+ onResolveEnd,
1793
+ trigger,
1794
+ false
1795
+ )).node;
1796
+ })
1797
+ )
1798
+ );
1799
+ }),
1800
+ false,
1801
+ createIsSlotConfig(config)
1802
+ );
1803
+ }
1804
+ if (Object.keys(readOnly).length) {
1805
+ resolvedItem.readOnly = readOnly;
1806
+ }
1807
+ cache.lastChange[id] = {
1808
+ item,
1809
+ resolved: resolvedItem
1810
+ };
1811
+ if (onResolveEnd) {
1812
+ onResolveEnd(resolvedItem);
1813
+ }
1814
+ return { node: resolvedItem, didChange: !(0, import_fast_deep_equal.default)(item, resolvedItem) };
1815
+ }
1816
+ return { node: item, didChange: false };
1817
+ });
1818
+
1819
+ // ../core/lib/data/to-root.ts
1820
+ init_react_import();
1821
+ var toRoot = (item) => {
1822
+ if ("type" in item && item.type !== "root") {
1823
+ throw new Error("Converting non-root item to root.");
1824
+ }
1825
+ const { readOnly } = item;
1826
+ if (item.props) {
1827
+ if ("id" in item.props) {
1828
+ const _a = item.props, { id } = _a, props = __objRest(_a, ["id"]);
1829
+ return { props, readOnly };
1830
+ }
1831
+ return { props: item.props, readOnly };
1832
+ }
1833
+ return { props: {}, readOnly };
1834
+ };
1835
+
1836
+ // ../core/store/default-app-state.ts
1837
+ init_react_import();
1434
1838
  var defaultAppState = {
1435
1839
  data: { content: [], root: {}, zones: {} },
1436
1840
  ui: {
@@ -1450,100 +1854,196 @@ var defaultAppState = {
1450
1854
  controlsVisible: true
1451
1855
  },
1452
1856
  field: { focus: null }
1857
+ },
1858
+ indexes: {
1859
+ nodes: {},
1860
+ zones: {}
1453
1861
  }
1454
1862
  };
1863
+
1864
+ // ../core/store/index.ts
1455
1865
  var defaultPageFields = {
1456
1866
  title: { type: "text" }
1457
1867
  };
1458
1868
  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
- }))
1869
+ subscribeWithSelector((set, get) => {
1870
+ var _a, _b;
1871
+ return __spreadProps(__spreadValues({
1872
+ state: defaultAppState,
1873
+ config: { components: {} },
1874
+ componentState: {},
1875
+ plugins: [],
1876
+ overrides: {},
1877
+ viewports: defaultViewports,
1878
+ zoomConfig: {
1879
+ autoZoom: 1,
1880
+ rootHeight: 0,
1881
+ zoom: 1
1882
+ },
1883
+ status: "LOADING",
1884
+ iframe: {},
1885
+ metadata: {}
1886
+ }, initialAppStore), {
1887
+ fields: createFieldsSlice(set, get),
1888
+ history: createHistorySlice(set, get),
1889
+ nodes: createNodesSlice(set, get),
1890
+ permissions: createPermissionsSlice(set, get),
1891
+ getComponentConfig: (type) => {
1892
+ var _a2;
1893
+ const { config, selectedItem } = get();
1894
+ const rootFields = ((_a2 = config.root) == null ? void 0 : _a2.fields) || defaultPageFields;
1895
+ return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1896
+ },
1897
+ selectedItem: ((_a = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _a.ui.itemSelector) ? getItem(
1898
+ (_b = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _b.ui.itemSelector,
1899
+ initialAppStore.state
1900
+ ) : null,
1901
+ dispatch: (action) => set((s) => {
1902
+ var _a2, _b2;
1903
+ const { record } = get().history;
1904
+ const dispatch = createReducer({
1905
+ record,
1906
+ appStore: s
1907
+ });
1908
+ const state = dispatch(s.state, action);
1909
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1910
+ (_b2 = (_a2 = get()).onAction) == null ? void 0 : _b2.call(_a2, action, state, get().state);
1911
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1912
+ }),
1913
+ setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1914
+ setStatus: (status) => set({ status }),
1915
+ setComponentState: (componentState) => set({ componentState }),
1916
+ pendingLoadTimeouts: {},
1917
+ setComponentLoading: (id, loading = true, defer = 0) => {
1918
+ const { setComponentState, pendingLoadTimeouts } = get();
1919
+ const loadId = generateId();
1920
+ const setLoading = () => {
1921
+ var _a2;
1922
+ const { componentState } = get();
1923
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1924
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1925
+ loadingCount: (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) + 1
1926
+ })
1927
+ }));
1928
+ };
1929
+ const unsetLoading = () => {
1930
+ var _a2;
1931
+ const { componentState } = get();
1932
+ clearTimeout(timeout);
1933
+ delete pendingLoadTimeouts[loadId];
1934
+ set({ pendingLoadTimeouts });
1935
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1936
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1937
+ loadingCount: Math.max(
1938
+ (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) - 1,
1939
+ 0
1940
+ )
1941
+ })
1942
+ }));
1943
+ };
1944
+ const timeout = setTimeout(() => {
1945
+ if (loading) {
1946
+ setLoading();
1947
+ } else {
1948
+ unsetLoading();
1949
+ }
1950
+ delete pendingLoadTimeouts[loadId];
1951
+ set({ pendingLoadTimeouts });
1952
+ }, defer);
1953
+ set({
1954
+ pendingLoadTimeouts: __spreadProps(__spreadValues({}, pendingLoadTimeouts), {
1955
+ [id]: timeout
1956
+ })
1957
+ });
1958
+ return unsetLoading;
1959
+ },
1960
+ unsetComponentLoading: (id) => {
1961
+ const { setComponentLoading } = get();
1962
+ setComponentLoading(id, false);
1963
+ },
1964
+ // Helper
1965
+ setUi: (ui, recordHistory) => set((s) => {
1966
+ const dispatch = createReducer({
1967
+ record: () => {
1968
+ },
1969
+ appStore: s
1970
+ });
1971
+ const state = dispatch(s.state, {
1972
+ type: "setUi",
1973
+ ui,
1974
+ recordHistory
1975
+ });
1976
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1977
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1978
+ }),
1979
+ resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
1980
+ const { config, metadata, setComponentLoading, permissions } = get();
1981
+ const timeouts = {};
1982
+ return yield resolveComponentData(
1983
+ componentData,
1984
+ config,
1985
+ metadata,
1986
+ (item) => {
1987
+ const id = "id" in item.props ? item.props.id : "root";
1988
+ timeouts[id] = setComponentLoading(id, true, 50);
1989
+ },
1990
+ (item) => __async(void 0, null, function* () {
1991
+ const id = "id" in item.props ? item.props.id : "root";
1992
+ if ("type" in item) {
1993
+ yield permissions.refreshPermissions({ item });
1994
+ } else {
1995
+ yield permissions.refreshPermissions({ root: true });
1996
+ }
1997
+ timeouts[id]();
1998
+ }),
1999
+ trigger
2000
+ );
2001
+ }),
2002
+ resolveAndCommitData: () => __async(void 0, null, function* () {
2003
+ const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
2004
+ walkAppState(
2005
+ state,
2006
+ config,
2007
+ (content) => content,
2008
+ (childItem) => {
2009
+ resolveComponentData2(childItem, "load").then((resolved) => {
2010
+ const { state: state2 } = get();
2011
+ const node = state2.indexes.nodes[resolved.node.props.id];
2012
+ if (node && resolved.didChange) {
2013
+ if (resolved.node.props.id === "root") {
2014
+ dispatch({
2015
+ type: "replaceRoot",
2016
+ root: toRoot(resolved.node)
2017
+ });
2018
+ } else {
2019
+ const zoneCompound = `${node.parentId}:${node.zone}`;
2020
+ const parentZone = state2.indexes.zones[zoneCompound];
2021
+ const index = parentZone.contentIds.indexOf(
2022
+ resolved.node.props.id
2023
+ );
2024
+ dispatch({
2025
+ type: "replace",
2026
+ data: resolved.node,
2027
+ destinationIndex: index,
2028
+ destinationZone: zoneCompound
2029
+ });
2030
+ }
2031
+ }
2032
+ });
2033
+ return childItem;
2034
+ }
2035
+ );
2036
+ })
2037
+ });
2038
+ })
1539
2039
  );
1540
- var appStoreContext = (0, import_react10.createContext)(createAppStore());
2040
+ var appStoreContext = (0, import_react9.createContext)(createAppStore());
1541
2041
  function useAppStore(selector) {
1542
- const context = (0, import_react10.useContext)(appStoreContext);
2042
+ const context = (0, import_react9.useContext)(appStoreContext);
1543
2043
  return useStore(context, selector);
1544
2044
  }
1545
2045
  function useAppStoreApi() {
1546
- return (0, import_react10.useContext)(appStoreContext);
2046
+ return (0, import_react9.useContext)(appStoreContext);
1547
2047
  }
1548
2048
 
1549
2049
  // ../core/lib/use-breadcrumbs.ts
@@ -1555,12 +2055,12 @@ var useBreadcrumbs = (renderCount) => {
1555
2055
  const config = useAppStore((s) => s.config);
1556
2056
  const path = useAppStore((s) => {
1557
2057
  var _a;
1558
- return (_a = s.nodes.nodes[selectedId]) == null ? void 0 : _a.path;
2058
+ return (_a = s.state.indexes.nodes[selectedId]) == null ? void 0 : _a.path;
1559
2059
  });
1560
2060
  const appStore = useAppStoreApi();
1561
- return (0, import_react11.useMemo)(() => {
2061
+ return (0, import_react10.useMemo)(() => {
1562
2062
  const breadcrumbs = (path == null ? void 0 : path.map((zoneCompound) => {
1563
- var _a, _b;
2063
+ var _a, _b, _c;
1564
2064
  const [componentId] = zoneCompound.split(":");
1565
2065
  if (componentId === "root") {
1566
2066
  return {
@@ -1568,12 +2068,15 @@ var useBreadcrumbs = (renderCount) => {
1568
2068
  selector: null
1569
2069
  };
1570
2070
  }
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";
2071
+ const node = appStore.getState().state.indexes.nodes[componentId];
2072
+ const parentId = node.path[node.path.length - 1];
2073
+ const contentIds = ((_a = appStore.getState().state.indexes.zones[parentId]) == null ? void 0 : _a.contentIds) || [];
2074
+ const index = contentIds.indexOf(componentId);
2075
+ const label = node ? (_c = (_b = config.components[node.data.type]) == null ? void 0 : _b.label) != null ? _c : node.data.type : "Component";
1573
2076
  return {
1574
2077
  label,
1575
2078
  selector: node ? {
1576
- index: node.index,
2079
+ index,
1577
2080
  zone: node.path[node.path.length - 1]
1578
2081
  } : null
1579
2082
  };
@@ -1594,6 +2097,12 @@ init_react_import();
1594
2097
  // ../core/lib/filter.ts
1595
2098
  init_react_import();
1596
2099
 
2100
+ // ../core/lib/data/reorder.ts
2101
+ init_react_import();
2102
+
2103
+ // ../core/lib/data/replace.ts
2104
+ init_react_import();
2105
+
1597
2106
  // css-module:/home/runner/work/puck/puck/packages/core/components/Loader/styles.module.css#css-module
1598
2107
  init_react_import();
1599
2108
  var styles_module_default3 = { "Loader": "_Loader_nacdm_13", "loader-animation": "_loader-animation_nacdm_1" };
@@ -1774,18 +2283,40 @@ function buildHierarchy(frame) {
1774
2283
  var usePuck = (0, import_puck.createUsePuck)();
1775
2284
  var HeadingAnalyzer = () => {
1776
2285
  const data = usePuck((s) => s.appState.data);
1777
- const [hierarchy, setHierarchy] = (0, import_react12.useState)([]);
1778
- (0, import_react12.useEffect)(() => {
2286
+ const [hierarchy, setHierarchy] = (0, import_react11.useState)([]);
2287
+ (0, import_react11.useEffect)(() => {
1779
2288
  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(() => {
2289
+ let entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2290
+ const createHierarchy = () => {
1784
2291
  setHierarchy(buildHierarchy(entry));
2292
+ };
2293
+ const entryObserver = new MutationObserver(() => {
2294
+ createHierarchy();
2295
+ });
2296
+ const frameObserver = new MutationObserver(() => {
2297
+ entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2298
+ if (entry) {
2299
+ registerEntryObserver();
2300
+ frameObserver.disconnect();
2301
+ }
1785
2302
  });
1786
- observer.observe(entry, { subtree: true, childList: true });
2303
+ const registerEntryObserver = () => {
2304
+ if (!entry) return;
2305
+ entryObserver.observe(entry, { subtree: true, childList: true });
2306
+ };
2307
+ const registerFrameObserver = () => {
2308
+ if (!frame) return;
2309
+ frameObserver.observe(frame, { subtree: true, childList: true });
2310
+ };
2311
+ if (entry) {
2312
+ createHierarchy();
2313
+ registerEntryObserver();
2314
+ } else {
2315
+ registerFrameObserver();
2316
+ }
1787
2317
  return () => {
1788
- observer.disconnect();
2318
+ entryObserver.disconnect();
2319
+ frameObserver.disconnect();
1789
2320
  };
1790
2321
  }, [data]);
1791
2322
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: getClassName5(), children: [