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

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 = flatten3;
162
+ flatten3.flatten = flatten3;
163
+ flatten3.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 flatten3(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
+ flatten3(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) {
@@ -368,35 +482,16 @@ init_react_import();
368
482
  // ../core/reducer/index.ts
369
483
  init_react_import();
370
484
 
371
- // ../core/reducer/reduce.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
375
489
  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
490
 
383
- // ../core/lib/insert.ts
491
+ // ../core/lib/data/for-related-zones.ts
384
492
  init_react_import();
385
- var insert = (list, index, item) => {
386
- const result = Array.from(list || []);
387
- result.splice(index, 0, item);
388
- return result;
389
- };
390
493
 
391
- // ../core/lib/remove.ts
392
- init_react_import();
393
- var remove = (list, index) => {
394
- const result = Array.from(list);
395
- result.splice(index, 1);
396
- return result;
397
- };
398
-
399
- // ../core/lib/setup-zone.ts
494
+ // ../core/lib/get-zone-id.ts
400
495
  init_react_import();
401
496
 
402
497
  // ../core/lib/root-droppable-id.ts
@@ -405,127 +500,7 @@ var rootAreaId = "root";
405
500
  var rootZone = "default-zone";
406
501
  var rootDroppableId = `${rootAreaId}:${rootZone}`;
407
502
 
408
- // ../core/lib/setup-zone.ts
409
- var setupZone = (data, zoneKey) => {
410
- if (zoneKey === rootDroppableId) {
411
- return data;
412
- }
413
- const newData = __spreadProps(__spreadValues({}, data), {
414
- zones: data.zones ? __spreadValues({}, data.zones) : {}
415
- });
416
- newData.zones[zoneKey] = newData.zones[zoneKey] || [];
417
- return newData;
418
- };
419
-
420
- // ../core/lib/get-item.ts
421
- init_react_import();
422
- function getItem(selector, state) {
423
- var _a, _b;
424
- const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
425
- return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
426
- }
427
-
428
- // ../core/lib/generate-id.ts
429
- init_react_import();
430
-
431
- // ../../node_modules/uuid/dist/esm-node/index.js
432
- init_react_import();
433
-
434
- // ../../node_modules/uuid/dist/esm-node/rng.js
435
- init_react_import();
436
- var import_crypto = __toESM(require("crypto"));
437
- var rnds8Pool = new Uint8Array(256);
438
- var poolPtr = rnds8Pool.length;
439
- function rng() {
440
- if (poolPtr > rnds8Pool.length - 16) {
441
- import_crypto.default.randomFillSync(rnds8Pool);
442
- poolPtr = 0;
443
- }
444
- return rnds8Pool.slice(poolPtr, poolPtr += 16);
445
- }
446
-
447
- // ../../node_modules/uuid/dist/esm-node/stringify.js
448
- init_react_import();
449
- var byteToHex = [];
450
- for (let i = 0; i < 256; ++i) {
451
- byteToHex.push((i + 256).toString(16).slice(1));
452
- }
453
- function unsafeStringify(arr, offset = 0) {
454
- return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
455
- }
456
-
457
- // ../../node_modules/uuid/dist/esm-node/v4.js
458
- init_react_import();
459
-
460
- // ../../node_modules/uuid/dist/esm-node/native.js
461
- init_react_import();
462
- var import_crypto2 = __toESM(require("crypto"));
463
- var native_default = {
464
- randomUUID: import_crypto2.default.randomUUID
465
- };
466
-
467
- // ../../node_modules/uuid/dist/esm-node/v4.js
468
- function v4(options, buf, offset) {
469
- if (native_default.randomUUID && !buf && !options) {
470
- return native_default.randomUUID();
471
- }
472
- options = options || {};
473
- const rnds = options.random || (options.rng || rng)();
474
- rnds[6] = rnds[6] & 15 | 64;
475
- rnds[8] = rnds[8] & 63 | 128;
476
- if (buf) {
477
- offset = offset || 0;
478
- for (let i = 0; i < 16; ++i) {
479
- buf[offset + i] = rnds[i];
480
- }
481
- return buf;
482
- }
483
- return unsafeStringify(rnds);
484
- }
485
- var v4_default = v4;
486
-
487
- // ../core/lib/generate-id.ts
488
- var generateId = (type) => type ? `${type}-${v4_default()}` : v4_default();
489
-
490
- // ../core/lib/walk-tree.ts
491
- init_react_import();
492
-
493
- // ../core/lib/for-each-slot.ts
494
- init_react_import();
495
-
496
- // ../core/lib/is-slot.ts
497
- init_react_import();
498
- var isSlot = (prop) => {
499
- var _a, _b;
500
- return Array.isArray(prop) && typeof ((_a = prop[0]) == null ? void 0 : _a.type) === "string" && typeof ((_b = prop[0]) == null ? void 0 : _b.props) === "object";
501
- };
502
-
503
- // ../core/lib/for-each-slot.ts
504
- var forEachSlot = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, cb, recursive = false, isSlot2 = isSlot) {
505
- const props = item.props || {};
506
- const propKeys = Object.keys(props);
507
- for (let i = 0; i < propKeys.length; i++) {
508
- const propKey = propKeys[i];
509
- const itemType = "type" in item ? item.type : "root";
510
- if (isSlot2(itemType, propKey, props[propKey])) {
511
- const content = props[propKey];
512
- yield cb(props.id, propKey, content);
513
- if (recursive) {
514
- content.forEach(
515
- (childItem) => __async(void 0, null, function* () {
516
- return yield forEachSlot(childItem, cb, true, isSlot2);
517
- })
518
- );
519
- }
520
- }
521
- }
522
- });
523
-
524
- // ../core/lib/for-related-zones.ts
525
- init_react_import();
526
-
527
503
  // ../core/lib/get-zone-id.ts
528
- init_react_import();
529
504
  var getZoneId = (zoneCompound) => {
530
505
  if (!zoneCompound) {
531
506
  return [];
@@ -536,36 +511,169 @@ var getZoneId = (zoneCompound) => {
536
511
  return [rootDroppableId, zoneCompound];
537
512
  };
538
513
 
539
- // ../core/lib/for-related-zones.ts
514
+ // ../core/lib/data/for-related-zones.ts
540
515
  function forRelatedZones(item, data, cb, path = []) {
541
516
  Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
542
517
  const [parentId] = getZoneId(zoneCompound);
543
518
  if (parentId === item.props.id) {
544
- const newPath = [...path, zoneCompound];
545
- content.forEach((item2) => forRelatedZones(item2, data, cb, newPath));
546
519
  cb(path, zoneCompound, content);
547
520
  }
548
521
  });
549
522
  }
550
523
 
551
- // ../core/lib/strip-slots.ts
524
+ // ../core/lib/data/map-slots.ts
552
525
  init_react_import();
553
- var stripSlots = (data) => {
554
- return __spreadProps(__spreadValues({}, data), {
555
- props: Object.entries(data.props).reduce(
556
- (acc, [propKey, propVal]) => {
557
- if (isSlot(propVal)) {
558
- return acc;
559
- }
560
- return __spreadProps(__spreadValues({}, acc), { [propKey]: propVal });
561
- },
562
- { id: data.props.id }
563
- )
526
+ var isPromise = (v) => !!v && typeof v.then === "function";
527
+ var flatten = (values) => values.reduce((acc, item) => __spreadValues(__spreadValues({}, acc), item), {});
528
+ var containsPromise = (arr) => arr.some(isPromise);
529
+ var walkField = ({
530
+ value,
531
+ fields,
532
+ map,
533
+ propKey = "",
534
+ propPath = "",
535
+ id = "",
536
+ config,
537
+ recurseSlots = false
538
+ }) => {
539
+ var _a, _b, _c;
540
+ if (((_a = fields[propKey]) == null ? void 0 : _a.type) === "slot") {
541
+ const content = value || [];
542
+ const mappedContent = recurseSlots ? content.map((el) => {
543
+ var _a2;
544
+ const componentConfig = config.components[el.type];
545
+ if (!componentConfig) {
546
+ throw new Error(`Could not find component config for ${el.type}`);
547
+ }
548
+ const fields2 = (_a2 = componentConfig.fields) != null ? _a2 : {};
549
+ return walkField({
550
+ value: el,
551
+ fields: fields2,
552
+ map,
553
+ id: el.props.id,
554
+ config,
555
+ recurseSlots
556
+ });
557
+ }) : content;
558
+ if (containsPromise(mappedContent)) {
559
+ return Promise.all(mappedContent);
560
+ }
561
+ return map(mappedContent, id, propPath, fields[propKey], propPath);
562
+ }
563
+ if (value && typeof value === "object") {
564
+ if (Array.isArray(value)) {
565
+ const arrayFields = ((_b = fields[propKey]) == null ? void 0 : _b.type) === "array" ? fields[propKey].arrayFields : null;
566
+ if (!arrayFields) return value;
567
+ const newValue = value.map(
568
+ (el, idx) => walkField({
569
+ value: el,
570
+ fields: arrayFields,
571
+ map,
572
+ propKey,
573
+ propPath: `${propPath}[${idx}]`,
574
+ id,
575
+ config,
576
+ recurseSlots
577
+ })
578
+ );
579
+ if (containsPromise(newValue)) {
580
+ return Promise.all(newValue);
581
+ }
582
+ return newValue;
583
+ } else if ("$$typeof" in value) {
584
+ return value;
585
+ } else {
586
+ const objectFields = ((_c = fields[propKey]) == null ? void 0 : _c.type) === "object" ? fields[propKey].objectFields : fields;
587
+ return walkObject({
588
+ value,
589
+ fields: objectFields,
590
+ map,
591
+ id,
592
+ getPropPath: (k) => `${propPath}.${k}`,
593
+ config,
594
+ recurseSlots
595
+ });
596
+ }
597
+ }
598
+ return value;
599
+ };
600
+ var walkObject = ({
601
+ value,
602
+ fields,
603
+ map,
604
+ id,
605
+ getPropPath,
606
+ config,
607
+ recurseSlots
608
+ }) => {
609
+ const newProps = Object.entries(value).map(([k, v]) => {
610
+ const opts = {
611
+ value: v,
612
+ fields,
613
+ map,
614
+ propKey: k,
615
+ propPath: getPropPath(k),
616
+ id,
617
+ config,
618
+ recurseSlots
619
+ };
620
+ const newValue = walkField(opts);
621
+ if (isPromise(newValue)) {
622
+ return newValue.then((resolvedValue) => ({
623
+ [k]: resolvedValue
624
+ }));
625
+ }
626
+ return {
627
+ [k]: newValue
628
+ };
629
+ }, {});
630
+ if (containsPromise(newProps)) {
631
+ return Promise.all(newProps).then(flatten);
632
+ }
633
+ return flatten(newProps);
634
+ };
635
+ function mapSlots(item, map, config, recurseSlots = false) {
636
+ var _a, _b, _c, _d;
637
+ const itemType = "type" in item ? item.type : "root";
638
+ const componentConfig = itemType === "root" ? config.root : (_a = config.components) == null ? void 0 : _a[itemType];
639
+ const newProps = walkObject({
640
+ value: (_b = item.props) != null ? _b : {},
641
+ fields: (_c = componentConfig == null ? void 0 : componentConfig.fields) != null ? _c : {},
642
+ map,
643
+ id: item.props ? (_d = item.props.id) != null ? _d : "root" : "root",
644
+ getPropPath: (k) => k,
645
+ config,
646
+ recurseSlots
647
+ });
648
+ if (isPromise(newProps)) {
649
+ return newProps.then((resolvedProps) => __spreadProps(__spreadValues({}, item), {
650
+ props: resolvedProps
651
+ }));
652
+ }
653
+ return __spreadProps(__spreadValues({}, item), {
654
+ props: newProps
655
+ });
656
+ }
657
+
658
+ // ../core/lib/data/flatten-node.ts
659
+ init_react_import();
660
+ var import_flat = __toESM(require_flat());
661
+
662
+ // ../core/lib/data/strip-slots.ts
663
+ init_react_import();
664
+ var stripSlots = (data, config) => {
665
+ return mapSlots(data, () => null, config);
666
+ };
667
+
668
+ // ../core/lib/data/flatten-node.ts
669
+ var flattenNode = (node, config) => {
670
+ return __spreadProps(__spreadValues({}, node), {
671
+ props: (0, import_flat.flatten)(stripSlots(node, config).props)
564
672
  });
565
673
  };
566
674
 
567
- // ../core/lib/walk-tree.ts
568
- function walkTree(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
675
+ // ../core/lib/data/walk-app-state.ts
676
+ function walkAppState(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
569
677
  var _a;
570
678
  let newZones = {};
571
679
  const newZoneIndex = {};
@@ -606,11 +714,9 @@ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSki
606
714
  const mappedItem = mapNodeOrSkip(item, path, index);
607
715
  if (!mappedItem) return item;
608
716
  const id = mappedItem.props.id;
609
- processRelatedZones(item, id, path);
610
- const newProps = __spreadValues({}, mappedItem.props);
611
- forEachSlot(
717
+ const newProps = __spreadProps(__spreadValues({}, mapSlots(
612
718
  mappedItem,
613
- (parentId2, slotId, content) => {
719
+ (content, parentId2, slotId) => {
614
720
  const zoneCompound = `${parentId2}:${slotId}`;
615
721
  const [_2, newContent2] = processContent(
616
722
  path,
@@ -619,27 +725,24 @@ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSki
619
725
  "slot",
620
726
  parentId2
621
727
  );
622
- newProps[slotId] = newContent2;
728
+ return newContent2;
623
729
  },
624
- false,
625
- (itemType, propName, propValue) => {
626
- var _a2, _b;
627
- const configForComponent = itemType === "root" ? config == null ? void 0 : config.root : config == null ? void 0 : config.components[itemType];
628
- if (!configForComponent) return isSlot(propValue);
629
- return ((_b = (_a2 = configForComponent.fields) == null ? void 0 : _a2[propName]) == null ? void 0 : _b.type) === "slot";
630
- }
631
- );
730
+ config
731
+ ).props), {
732
+ id
733
+ });
734
+ processRelatedZones(item, id, path);
632
735
  const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
633
736
  const thisZoneCompound = path[path.length - 1];
634
737
  const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
635
738
  newNodeIndex[id] = {
636
739
  data: newItem,
637
- flatData: stripSlots(newItem),
740
+ flatData: flattenNode(newItem, config),
638
741
  path,
639
742
  parentId,
640
743
  zone
641
744
  };
642
- const finalData = newItem;
745
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
643
746
  if (newProps.id === "root") {
644
747
  delete finalData["type"];
645
748
  delete finalData.props["id"];
@@ -654,8 +757,12 @@ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSki
654
757
  "root"
655
758
  );
656
759
  const processedContent = newContent;
760
+ const zonesAlreadyProcessed = Object.keys(newZones);
657
761
  Object.keys(zones || {}).forEach((zoneCompound) => {
658
762
  const [parentId] = zoneCompound.split(":");
763
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
764
+ return;
765
+ }
659
766
  const [_2, newContent2] = processContent(
660
767
  [rootDroppableId],
661
768
  zoneCompound,
@@ -679,7 +786,6 @@ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSki
679
786
  return __spreadProps(__spreadValues({}, state), {
680
787
  data: {
681
788
  root,
682
- // root: state.data.root, // TODO changing root causes it's entire subtree to re-render. Let's keep this disabled until the performance issues are resolved in #644.
683
789
  content: processedContent,
684
790
  zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
685
791
  },
@@ -690,59 +796,175 @@ function walkTree(state, config, mapContent = (content) => content, mapNodeOrSki
690
796
  });
691
797
  }
692
798
 
693
- // ../core/lib/deindex.ts
694
- init_react_import();
695
- var deindex = (state, componentData) => {
696
- let zones = __spreadValues({}, state.indexes.zones);
697
- let nodes = __spreadValues({}, state.indexes.nodes);
698
- const dindexRelatedZones = (item) => {
699
- forRelatedZones(item, state.data, (_path, zoneCompound, content) => {
700
- content.forEach((subItem) => {
701
- dindexChildren(subItem);
702
- delete nodes[subItem.props.id];
703
- });
704
- delete zones[zoneCompound];
705
- });
706
- };
707
- const dindexChildren = (item) => {
708
- forEachSlot(
709
- item,
710
- (parentId, slotId, content) => {
711
- const zoneCompound = `${parentId}:${slotId}`;
712
- delete zones[zoneCompound];
713
- content.forEach((item2) => {
714
- dindexRelatedZones(item2);
715
- delete nodes[item2.props.id];
716
- });
717
- },
718
- true
799
+ // ../core/reducer/actions/set.ts
800
+ var setAction = (state, action, appStore) => {
801
+ if (typeof action.state === "object") {
802
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
803
+ if (action.state.indexes) {
804
+ return newState;
805
+ }
806
+ console.warn(
807
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
719
808
  );
720
- };
721
- dindexRelatedZones(componentData);
722
- dindexChildren(componentData);
723
- delete nodes[componentData.props.id];
724
- return { nodes, zones };
809
+ return walkAppState(newState, appStore.config);
810
+ }
811
+ return __spreadValues(__spreadValues({}, state), action.state(state));
725
812
  };
726
813
 
727
- // ../core/reducer/reduce.ts
728
- var zoneCache = {};
814
+ // ../core/reducer/actions/insert.ts
815
+ init_react_import();
816
+
817
+ // ../core/lib/data/insert.ts
818
+ init_react_import();
819
+ var insert = (list, index, item) => {
820
+ const result = Array.from(list || []);
821
+ result.splice(index, 0, item);
822
+ return result;
823
+ };
824
+
825
+ // ../core/lib/generate-id.ts
826
+ init_react_import();
827
+
828
+ // ../../node_modules/uuid/dist/esm-node/index.js
829
+ init_react_import();
830
+
831
+ // ../../node_modules/uuid/dist/esm-node/rng.js
832
+ init_react_import();
833
+ var import_crypto = __toESM(require("crypto"));
834
+ var rnds8Pool = new Uint8Array(256);
835
+ var poolPtr = rnds8Pool.length;
836
+ function rng() {
837
+ if (poolPtr > rnds8Pool.length - 16) {
838
+ import_crypto.default.randomFillSync(rnds8Pool);
839
+ poolPtr = 0;
840
+ }
841
+ return rnds8Pool.slice(poolPtr, poolPtr += 16);
842
+ }
843
+
844
+ // ../../node_modules/uuid/dist/esm-node/stringify.js
845
+ init_react_import();
846
+ var byteToHex = [];
847
+ for (let i = 0; i < 256; ++i) {
848
+ byteToHex.push((i + 256).toString(16).slice(1));
849
+ }
850
+ function unsafeStringify(arr, offset = 0) {
851
+ return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
852
+ }
853
+
854
+ // ../../node_modules/uuid/dist/esm-node/v4.js
855
+ init_react_import();
856
+
857
+ // ../../node_modules/uuid/dist/esm-node/native.js
858
+ init_react_import();
859
+ var import_crypto2 = __toESM(require("crypto"));
860
+ var native_default = {
861
+ randomUUID: import_crypto2.default.randomUUID
862
+ };
863
+
864
+ // ../../node_modules/uuid/dist/esm-node/v4.js
865
+ function v4(options, buf, offset) {
866
+ if (native_default.randomUUID && !buf && !options) {
867
+ return native_default.randomUUID();
868
+ }
869
+ options = options || {};
870
+ const rnds = options.random || (options.rng || rng)();
871
+ rnds[6] = rnds[6] & 15 | 64;
872
+ rnds[8] = rnds[8] & 63 | 128;
873
+ if (buf) {
874
+ offset = offset || 0;
875
+ for (let i = 0; i < 16; ++i) {
876
+ buf[offset + i] = rnds[i];
877
+ }
878
+ return buf;
879
+ }
880
+ return unsafeStringify(rnds);
881
+ }
882
+ var v4_default = v4;
883
+
884
+ // ../core/lib/generate-id.ts
885
+ var generateId = (type) => type ? `${type}-${v4_default()}` : v4_default();
886
+
887
+ // ../core/lib/data/get-ids-for-parent.ts
888
+ init_react_import();
729
889
  var getIdsForParent = (zoneCompound, state) => {
730
890
  const [parentId] = zoneCompound.split(":");
731
891
  const node = state.indexes.nodes[parentId];
732
892
  return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
733
893
  };
734
- function insertAction(state, action, config) {
735
- const id = action.id || generateId(action.componentType);
736
- const emptyComponentData = {
737
- type: action.componentType,
738
- props: __spreadProps(__spreadValues({}, config.components[action.componentType].defaultProps || {}), {
739
- id
740
- })
894
+
895
+ // ../core/lib/data/populate-ids.ts
896
+ init_react_import();
897
+
898
+ // ../core/lib/data/walk-tree.ts
899
+ init_react_import();
900
+ function walkTree(data, config, callbackFn) {
901
+ var _a, _b;
902
+ const walkItem = (item) => {
903
+ return mapSlots(
904
+ item,
905
+ (content, parentId, propName) => {
906
+ var _a2;
907
+ return (_a2 = callbackFn(content, { parentId, propName })) != null ? _a2 : content;
908
+ },
909
+ config,
910
+ true
911
+ );
741
912
  };
742
- const [parentId] = action.destinationZone.split(":");
913
+ if ("props" in data) {
914
+ return walkItem(data);
915
+ }
916
+ const _data = data;
917
+ const zones = (_a = _data.zones) != null ? _a : {};
918
+ const mappedContent = _data.content.map(walkItem);
919
+ return {
920
+ root: walkItem(_data.root),
921
+ content: (_b = callbackFn(mappedContent, {
922
+ parentId: "root",
923
+ propName: "default-zone"
924
+ })) != null ? _b : mappedContent,
925
+ zones: Object.keys(zones).reduce(
926
+ (acc, zoneCompound) => __spreadProps(__spreadValues({}, acc), {
927
+ [zoneCompound]: zones[zoneCompound].map(walkItem)
928
+ }),
929
+ {}
930
+ )
931
+ };
932
+ }
933
+
934
+ // ../core/lib/data/populate-ids.ts
935
+ var populateIds = (data, config, override = false) => {
936
+ const id = generateId(data.type);
743
937
  return walkTree(
744
- state,
938
+ __spreadProps(__spreadValues({}, data), {
939
+ props: override ? __spreadProps(__spreadValues({}, data.props), { id }) : __spreadValues({}, data.props)
940
+ }),
745
941
  config,
942
+ (contents) => contents.map((item) => {
943
+ const id2 = generateId(item.type);
944
+ return __spreadProps(__spreadValues({}, item), {
945
+ props: override ? __spreadProps(__spreadValues({}, item.props), { id: id2 }) : __spreadValues({ id: id2 }, item.props)
946
+ });
947
+ })
948
+ );
949
+ };
950
+
951
+ // ../core/reducer/actions/insert.ts
952
+ function insertAction(state, action, appStore) {
953
+ const id = action.id || generateId(action.componentType);
954
+ const emptyComponentData = populateIds(
955
+ {
956
+ type: action.componentType,
957
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
958
+ id
959
+ })
960
+ },
961
+ appStore.config
962
+ );
963
+ const [parentId] = action.destinationZone.split(":");
964
+ const idsInPath = getIdsForParent(action.destinationZone, state);
965
+ return walkAppState(
966
+ state,
967
+ appStore.config,
746
968
  (content, zoneCompound) => {
747
969
  if (zoneCompound === action.destinationZone) {
748
970
  return insert(
@@ -753,279 +975,348 @@ function insertAction(state, action, config) {
753
975
  }
754
976
  return content;
755
977
  },
756
- (childItem) => {
978
+ (childItem, path) => {
757
979
  if (childItem.props.id === id || childItem.props.id === parentId) {
758
980
  return childItem;
981
+ } else if (idsInPath.includes(childItem.props.id)) {
982
+ return childItem;
983
+ } else if (path.includes(action.destinationZone)) {
984
+ return childItem;
759
985
  }
760
986
  return null;
761
987
  }
762
988
  );
763
989
  }
764
- var reorderAction = (state, action, appStore) => {
765
- return walkTree(
990
+
991
+ // ../core/reducer/actions/replace.ts
992
+ init_react_import();
993
+ var replaceAction = (state, action, appStore) => {
994
+ const [parentId] = action.destinationZone.split(":");
995
+ const idsInPath = getIdsForParent(action.destinationZone, state);
996
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
997
+ const idChanged = originalId !== action.data.props.id;
998
+ if (idChanged) {
999
+ throw new Error(
1000
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
1001
+ );
1002
+ }
1003
+ const data = populateIds(action.data, appStore.config);
1004
+ return walkAppState(
1005
+ state,
1006
+ appStore.config,
1007
+ (content, zoneCompound) => {
1008
+ const newContent = [...content];
1009
+ if (zoneCompound === action.destinationZone) {
1010
+ newContent[action.destinationIndex] = data;
1011
+ }
1012
+ return newContent;
1013
+ },
1014
+ (childItem, path) => {
1015
+ const pathIds = path.map((p) => p.split(":")[0]);
1016
+ if (childItem.props.id === data.props.id) {
1017
+ return data;
1018
+ } else if (childItem.props.id === parentId) {
1019
+ return childItem;
1020
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
1021
+ return childItem;
1022
+ } else if (pathIds.indexOf(data.props.id) > -1) {
1023
+ return childItem;
1024
+ }
1025
+ return null;
1026
+ }
1027
+ );
1028
+ };
1029
+
1030
+ // ../core/reducer/actions/replace-root.ts
1031
+ init_react_import();
1032
+ var replaceRootAction = (state, action, appStore) => {
1033
+ return walkAppState(
1034
+ state,
1035
+ appStore.config,
1036
+ (content) => content,
1037
+ (childItem) => {
1038
+ if (childItem.props.id === "root") {
1039
+ return __spreadProps(__spreadValues({}, childItem), {
1040
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
1041
+ readOnly: action.root.readOnly
1042
+ });
1043
+ }
1044
+ return childItem;
1045
+ }
1046
+ );
1047
+ };
1048
+
1049
+ // ../core/reducer/actions/duplicate.ts
1050
+ init_react_import();
1051
+
1052
+ // ../core/lib/data/get-item.ts
1053
+ init_react_import();
1054
+ function getItem(selector, state) {
1055
+ var _a, _b;
1056
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
1057
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
1058
+ }
1059
+
1060
+ // ../core/reducer/actions/duplicate.ts
1061
+ function duplicateAction(state, action, appStore) {
1062
+ const item = getItem(
1063
+ { index: action.sourceIndex, zone: action.sourceZone },
1064
+ state
1065
+ );
1066
+ const idsInPath = getIdsForParent(action.sourceZone, state);
1067
+ const newItem = __spreadProps(__spreadValues({}, item), {
1068
+ props: __spreadProps(__spreadValues({}, item.props), {
1069
+ id: generateId(item.type)
1070
+ })
1071
+ });
1072
+ const modified = walkAppState(
1073
+ state,
1074
+ appStore.config,
1075
+ (content, zoneCompound) => {
1076
+ if (zoneCompound === action.sourceZone) {
1077
+ return insert(content, action.sourceIndex + 1, item);
1078
+ }
1079
+ return content;
1080
+ },
1081
+ (childItem, path, index) => {
1082
+ const zoneCompound = path[path.length - 1];
1083
+ const parents = path.map((p) => p.split(":")[0]);
1084
+ if (parents.indexOf(newItem.props.id) > -1) {
1085
+ return __spreadProps(__spreadValues({}, childItem), {
1086
+ props: __spreadProps(__spreadValues({}, childItem.props), {
1087
+ id: generateId(childItem.type)
1088
+ })
1089
+ });
1090
+ }
1091
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
1092
+ return newItem;
1093
+ }
1094
+ const [sourceZoneParent] = action.sourceZone.split(":");
1095
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
1096
+ return childItem;
1097
+ }
1098
+ return null;
1099
+ }
1100
+ );
1101
+ return __spreadProps(__spreadValues({}, modified), {
1102
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
1103
+ itemSelector: {
1104
+ index: action.sourceIndex + 1,
1105
+ zone: action.sourceZone
1106
+ }
1107
+ })
1108
+ });
1109
+ }
1110
+
1111
+ // ../core/reducer/actions/reorder.ts
1112
+ init_react_import();
1113
+
1114
+ // ../core/reducer/actions/move.ts
1115
+ init_react_import();
1116
+
1117
+ // ../core/lib/data/remove.ts
1118
+ init_react_import();
1119
+ var remove = (list, index) => {
1120
+ const result = Array.from(list);
1121
+ result.splice(index, 1);
1122
+ return result;
1123
+ };
1124
+
1125
+ // ../core/reducer/actions/move.ts
1126
+ var moveAction = (state, action, appStore) => {
1127
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
1128
+ return state;
1129
+ }
1130
+ const item = getItem(
1131
+ { zone: action.sourceZone, index: action.sourceIndex },
1132
+ state
1133
+ );
1134
+ if (!item) return state;
1135
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
1136
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
1137
+ return walkAppState(
766
1138
  state,
767
1139
  appStore.config,
768
1140
  (content, zoneCompound) => {
769
- if (zoneCompound === action.destinationZone) {
770
- return reorder(
771
- content || [],
772
- action.sourceIndex,
773
- action.destinationIndex
1141
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
1142
+ return insert(
1143
+ remove(content, action.sourceIndex),
1144
+ action.destinationIndex,
1145
+ item
774
1146
  );
1147
+ } else if (zoneCompound === action.sourceZone) {
1148
+ return remove(content, action.sourceIndex);
1149
+ } else if (zoneCompound === action.destinationZone) {
1150
+ return insert(content, action.destinationIndex, item);
775
1151
  }
776
1152
  return content;
777
1153
  },
778
- (childItem, path, index) => {
779
- const zoneCompound = path[path.length - 1];
780
- if (index === action.sourceIndex && zoneCompound === action.destinationZone || index === action.destinationIndex && zoneCompound === action.destinationZone) {
1154
+ (childItem, path) => {
1155
+ const [sourceZoneParent] = action.sourceZone.split(":");
1156
+ const [destinationZoneParent] = action.destinationZone.split(":");
1157
+ const childId = childItem.props.id;
1158
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
781
1159
  return childItem;
782
1160
  }
783
1161
  return null;
784
1162
  }
785
1163
  );
786
1164
  };
787
- var replaceAction = (state, action, appStore) => {
788
- const [parentId] = action.destinationZone.split(":");
789
- const idsInPath = getIdsForParent(action.destinationZone, state);
790
- return walkTree(
1165
+
1166
+ // ../core/reducer/actions/reorder.ts
1167
+ var reorderAction = (state, action, appStore) => {
1168
+ return moveAction(
791
1169
  state,
792
- appStore.config,
793
- (content) => content,
794
- (childItem, path) => {
795
- const pathIds = path.map((p) => p.split(":")[0]);
796
- if (childItem.props.id === action.data.props.id) {
797
- return action.data;
798
- } else if (childItem.props.id === parentId) {
799
- return childItem;
800
- } else if (idsInPath.indexOf(childItem.props.id) > -1) {
801
- return childItem;
802
- } else if (pathIds.indexOf(action.data.props.id) > -1) {
803
- return childItem;
804
- }
805
- return null;
806
- }
1170
+ {
1171
+ type: "move",
1172
+ sourceIndex: action.sourceIndex,
1173
+ sourceZone: action.destinationZone,
1174
+ destinationIndex: action.destinationIndex,
1175
+ destinationZone: action.destinationZone
1176
+ },
1177
+ appStore
807
1178
  );
808
1179
  };
809
- var setAction = (state, action, appStore) => {
810
- if (typeof action.state === "object") {
811
- const newState = __spreadValues(__spreadValues({}, state), action.state);
812
- if (action.state.indexes) {
813
- console.warn(
814
- "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
815
- );
816
- return newState;
817
- }
818
- return walkTree(newState, appStore.config);
819
- }
820
- return __spreadValues(__spreadValues({}, state), action.state(state));
821
- };
822
- function reduce(state, action, appStore) {
823
- if (action.type === "set") {
824
- return setAction(state, action, appStore);
825
- }
826
- if (action.type === "insert") {
827
- return insertAction(state, action, appStore.config);
828
- }
829
- if (action.type === "replace") {
830
- return replaceAction(state, action, appStore);
831
- }
832
- if (action.type === "replaceRoot") {
833
- return walkTree(
834
- state,
835
- appStore.config,
836
- (content) => content,
837
- (childItem) => {
838
- if (childItem.props.id === "root") {
839
- return __spreadProps(__spreadValues({}, childItem), {
840
- props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
841
- readOnly: action.root.readOnly
842
- });
843
- }
844
- return childItem;
1180
+
1181
+ // ../core/reducer/actions/remove.ts
1182
+ init_react_import();
1183
+ var removeAction = (state, action, appStore) => {
1184
+ const item = getItem({ index: action.index, zone: action.zone }, state);
1185
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
1186
+ (acc, [nodeId, nodeData]) => {
1187
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
1188
+ if (pathIds.includes(item.props.id)) {
1189
+ return [...acc, nodeId];
845
1190
  }
846
- );
847
- }
848
- if (action.type === "duplicate") {
849
- const item = getItem(
850
- { index: action.sourceIndex, zone: action.sourceZone },
851
- state
852
- );
853
- const idsInPath = getIdsForParent(action.sourceZone, state);
854
- const newItem = __spreadProps(__spreadValues({}, item), {
855
- props: __spreadProps(__spreadValues({}, item.props), {
856
- id: generateId(item.type)
857
- })
858
- });
859
- const modified = walkTree(
860
- state,
861
- appStore.config,
862
- (content, zoneCompound) => {
863
- if (zoneCompound === action.sourceZone) {
864
- return insert(content, action.sourceIndex + 1, item);
865
- }
866
- return content;
867
- },
868
- (childItem, path, index) => {
869
- const zoneCompound = path[path.length - 1];
870
- const parents = path.map((p) => p.split(":")[0]);
871
- if (parents.indexOf(newItem.props.id) > -1) {
872
- return __spreadProps(__spreadValues({}, childItem), {
873
- props: __spreadProps(__spreadValues({}, childItem.props), {
874
- id: generateId(childItem.type)
875
- })
876
- });
877
- }
878
- if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
879
- return newItem;
880
- }
881
- const [sourceZoneParent] = action.sourceZone.split(":");
882
- if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
883
- return childItem;
884
- }
885
- return null;
1191
+ return acc;
1192
+ },
1193
+ [item.props.id]
1194
+ );
1195
+ const newState = walkAppState(
1196
+ state,
1197
+ appStore.config,
1198
+ (content, zoneCompound) => {
1199
+ if (zoneCompound === action.zone) {
1200
+ return remove(content, action.index);
886
1201
  }
887
- );
888
- return __spreadProps(__spreadValues({}, modified), {
889
- ui: __spreadProps(__spreadValues({}, modified.ui), {
890
- itemSelector: {
891
- index: action.sourceIndex + 1,
892
- zone: action.sourceZone
893
- }
894
- })
895
- });
896
- }
897
- if (action.type === "reorder") {
898
- return reorderAction(state, action, appStore);
899
- }
900
- if (action.type === "move") {
901
- if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
902
- return state;
1202
+ return content;
903
1203
  }
904
- const item = getItem(
905
- { zone: action.sourceZone, index: action.sourceIndex },
906
- state
907
- );
908
- if (!item) return state;
909
- const idsInSourcePath = getIdsForParent(action.sourceZone, state);
910
- const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
911
- return walkTree(
912
- state,
913
- appStore.config,
914
- (content, zoneCompound) => {
915
- if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
916
- return insert(
917
- remove(content, action.sourceIndex),
918
- action.destinationIndex,
919
- item
920
- );
921
- } else if (zoneCompound === action.sourceZone) {
922
- return remove(content, action.sourceIndex);
923
- } else if (zoneCompound === action.destinationZone) {
924
- return insert(content, action.destinationIndex, item);
925
- }
926
- return content;
927
- },
928
- (childItem) => {
929
- const [sourceZoneParent] = action.sourceZone.split(":");
930
- const [destinationZoneParent] = action.destinationZone.split(":");
931
- const childId = childItem.props.id;
932
- if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1) {
933
- return childItem;
934
- }
935
- return null;
936
- }
937
- );
938
- }
939
- if (action.type === "remove") {
940
- const item = getItem({ index: action.index, zone: action.zone }, state);
941
- let deindexed = deindex(state, item);
942
- const [parentId] = action.zone.split(":");
943
- return walkTree(
944
- __spreadProps(__spreadValues({}, state), { indexes: deindexed }),
945
- appStore.config,
946
- (content, zoneCompound) => {
947
- if (zoneCompound === action.zone) {
948
- return remove(content, action.index);
949
- }
950
- return content;
951
- },
952
- (childItem, path) => {
953
- const parentIds = path.map((p) => p.split(":")[0]);
954
- if (childItem.props.id === parentId || childItem.props.id === item.props.id || parentIds.indexOf(item.props.id) > -1) {
955
- return childItem;
956
- }
957
- return null;
958
- }
959
- );
960
- }
961
- if (action.type === "registerZone") {
962
- if (zoneCache[action.zone]) {
963
- return __spreadProps(__spreadValues({}, state), {
964
- data: __spreadProps(__spreadValues({}, state.data), {
965
- zones: __spreadProps(__spreadValues({}, state.data.zones), {
966
- [action.zone]: zoneCache[action.zone]
967
- })
968
- }),
969
- indexes: __spreadProps(__spreadValues({}, state.indexes), {
970
- zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
971
- [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
972
- contentIds: zoneCache[action.zone].map((item) => item.props.id),
973
- type: "dropzone"
974
- })
975
- })
976
- })
977
- });
1204
+ );
1205
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
1206
+ const parentId = zoneCompound.split(":")[0];
1207
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
1208
+ delete newState.data.zones[zoneCompound];
978
1209
  }
979
- return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
980
- }
981
- if (action.type === "unregisterZone") {
982
- const _zones = __spreadValues({}, state.data.zones || {});
983
- const zoneIndex = __spreadValues({}, state.indexes.zones || {});
984
- if (_zones[action.zone]) {
985
- zoneCache[action.zone] = _zones[action.zone];
986
- delete _zones[action.zone];
1210
+ });
1211
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
1212
+ const parentId = zoneCompound.split(":")[0];
1213
+ if (nodesToDelete.includes(parentId)) {
1214
+ delete newState.indexes.zones[zoneCompound];
987
1215
  }
988
- delete zoneIndex[action.zone];
1216
+ });
1217
+ nodesToDelete.forEach((id) => {
1218
+ delete newState.indexes.nodes[id];
1219
+ });
1220
+ return newState;
1221
+ };
1222
+
1223
+ // ../core/reducer/actions/register-zone.ts
1224
+ init_react_import();
1225
+
1226
+ // ../core/lib/data/setup-zone.ts
1227
+ init_react_import();
1228
+ var setupZone = (data, zoneKey) => {
1229
+ if (zoneKey === rootDroppableId) {
1230
+ return data;
1231
+ }
1232
+ const newData = __spreadProps(__spreadValues({}, data), {
1233
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
1234
+ });
1235
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
1236
+ return newData;
1237
+ };
1238
+
1239
+ // ../core/reducer/actions/register-zone.ts
1240
+ var zoneCache = {};
1241
+ function registerZoneAction(state, action) {
1242
+ if (zoneCache[action.zone]) {
989
1243
  return __spreadProps(__spreadValues({}, state), {
990
1244
  data: __spreadProps(__spreadValues({}, state.data), {
991
- zones: _zones
1245
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
1246
+ [action.zone]: zoneCache[action.zone]
1247
+ })
992
1248
  }),
993
1249
  indexes: __spreadProps(__spreadValues({}, state.indexes), {
994
- zones: zoneIndex
1250
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
1251
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
1252
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
1253
+ type: "dropzone"
1254
+ })
1255
+ })
995
1256
  })
996
1257
  });
997
1258
  }
998
- if (action.type === "setData") {
999
- if (typeof action.data === "object") {
1000
- console.warn(
1001
- "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
1002
- );
1003
- return walkTree(
1004
- __spreadProps(__spreadValues({}, state), {
1005
- data: __spreadValues(__spreadValues({}, state.data), action.data)
1006
- }),
1007
- appStore.config
1008
- );
1009
- }
1010
- return walkTree(
1259
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
1260
+ }
1261
+ function unregisterZoneAction(state, action) {
1262
+ const _zones = __spreadValues({}, state.data.zones || {});
1263
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
1264
+ if (_zones[action.zone]) {
1265
+ zoneCache[action.zone] = _zones[action.zone];
1266
+ delete _zones[action.zone];
1267
+ }
1268
+ delete zoneIndex[action.zone];
1269
+ return __spreadProps(__spreadValues({}, state), {
1270
+ data: __spreadProps(__spreadValues({}, state.data), {
1271
+ zones: _zones
1272
+ }),
1273
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
1274
+ zones: zoneIndex
1275
+ })
1276
+ });
1277
+ }
1278
+
1279
+ // ../core/reducer/actions/set-data.ts
1280
+ init_react_import();
1281
+ var setDataAction = (state, action, appStore) => {
1282
+ if (typeof action.data === "object") {
1283
+ console.warn(
1284
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
1285
+ );
1286
+ return walkAppState(
1011
1287
  __spreadProps(__spreadValues({}, state), {
1012
- data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
1288
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
1013
1289
  }),
1014
1290
  appStore.config
1015
1291
  );
1016
1292
  }
1017
- if (action.type === "setUi") {
1018
- if (typeof action.ui === "object") {
1019
- return __spreadProps(__spreadValues({}, state), {
1020
- ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
1021
- });
1022
- }
1293
+ return walkAppState(
1294
+ __spreadProps(__spreadValues({}, state), {
1295
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
1296
+ }),
1297
+ appStore.config
1298
+ );
1299
+ };
1300
+
1301
+ // ../core/reducer/actions/set-ui.ts
1302
+ init_react_import();
1303
+ var setUiAction = (state, action) => {
1304
+ if (typeof action.ui === "object") {
1023
1305
  return __spreadProps(__spreadValues({}, state), {
1024
- ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
1306
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
1025
1307
  });
1026
1308
  }
1027
- return state;
1028
- }
1309
+ return __spreadProps(__spreadValues({}, state), {
1310
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
1311
+ });
1312
+ };
1313
+
1314
+ // ../core/lib/data/make-state-public.ts
1315
+ init_react_import();
1316
+ var makeStatePublic = (state) => {
1317
+ const { data, ui } = state;
1318
+ return { data, ui };
1319
+ };
1029
1320
 
1030
1321
  // ../core/reducer/actions.tsx
1031
1322
  init_react_import();
@@ -1044,21 +1335,54 @@ function storeInterceptor(reducer, record, onAction) {
1044
1335
  if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
1045
1336
  if (record) record(newAppState);
1046
1337
  }
1047
- onAction == null ? void 0 : onAction(action, newAppState, state);
1338
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
1048
1339
  return newAppState;
1049
1340
  };
1050
1341
  }
1051
1342
  function createReducer({
1052
- config,
1053
1343
  record,
1054
1344
  onAction,
1055
1345
  appStore
1056
1346
  }) {
1057
1347
  return storeInterceptor(
1058
1348
  (state, action) => {
1059
- const result = reduce(state, action, appStore);
1060
- console.log(action.type, state, result);
1061
- return result;
1349
+ if (action.type === "set") {
1350
+ return setAction(state, action, appStore);
1351
+ }
1352
+ if (action.type === "insert") {
1353
+ return insertAction(state, action, appStore);
1354
+ }
1355
+ if (action.type === "replace") {
1356
+ return replaceAction(state, action, appStore);
1357
+ }
1358
+ if (action.type === "replaceRoot") {
1359
+ return replaceRootAction(state, action, appStore);
1360
+ }
1361
+ if (action.type === "duplicate") {
1362
+ return duplicateAction(state, action, appStore);
1363
+ }
1364
+ if (action.type === "reorder") {
1365
+ return reorderAction(state, action, appStore);
1366
+ }
1367
+ if (action.type === "move") {
1368
+ return moveAction(state, action, appStore);
1369
+ }
1370
+ if (action.type === "remove") {
1371
+ return removeAction(state, action, appStore);
1372
+ }
1373
+ if (action.type === "registerZone") {
1374
+ return registerZoneAction(state, action);
1375
+ }
1376
+ if (action.type === "unregisterZone") {
1377
+ return unregisterZoneAction(state, action);
1378
+ }
1379
+ if (action.type === "setData") {
1380
+ return setDataAction(state, action, appStore);
1381
+ }
1382
+ if (action.type === "setUi") {
1383
+ return setUiAction(state, action);
1384
+ }
1385
+ return state;
1062
1386
  },
1063
1387
  record,
1064
1388
  onAction
@@ -1253,7 +1577,7 @@ var createHistorySlice = (set, get) => {
1253
1577
  const { dispatch, history } = get();
1254
1578
  dispatch({
1255
1579
  type: "set",
1256
- state: ((_a = history.histories[history.index]) == null ? void 0 : _a.state) || history.initialAppState
1580
+ state: ((_a = history.histories[index]) == null ? void 0 : _a.state) || history.initialAppState
1257
1581
  });
1258
1582
  set({ history: __spreadProps(__spreadValues({}, history), { index }) });
1259
1583
  },
@@ -1269,7 +1593,11 @@ var createNodesSlice = (set, get) => ({
1269
1593
  const s = get().nodes;
1270
1594
  const emptyNode = {
1271
1595
  id,
1272
- methods: { sync: () => null },
1596
+ methods: {
1597
+ sync: () => null,
1598
+ hideOverlay: () => null,
1599
+ showOverlay: () => null
1600
+ },
1273
1601
  element: null
1274
1602
  };
1275
1603
  const existingNode = s.nodes[id];
@@ -1302,11 +1630,11 @@ var createNodesSlice = (set, get) => ({
1302
1630
  init_react_import();
1303
1631
  var import_react7 = require("react");
1304
1632
 
1305
- // ../core/lib/flatten-data.ts
1633
+ // ../core/lib/data/flatten-data.ts
1306
1634
  init_react_import();
1307
1635
  var flattenData = (state, config) => {
1308
1636
  const data = [];
1309
- walkTree(
1637
+ walkAppState(
1310
1638
  state,
1311
1639
  config,
1312
1640
  (content) => content,
@@ -1320,12 +1648,13 @@ var flattenData = (state, config) => {
1320
1648
 
1321
1649
  // ../core/lib/get-changed.ts
1322
1650
  init_react_import();
1651
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1323
1652
  var getChanged = (newItem, oldItem) => {
1324
1653
  return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
1325
1654
  const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
1326
1655
  const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
1327
1656
  return __spreadProps(__spreadValues({}, acc), {
1328
- [item]: oldItemProps[item] !== newItemProps[item]
1657
+ [item]: !(0, import_fast_deep_equal.default)(oldItemProps[item], newItemProps[item])
1329
1658
  });
1330
1659
  }, {}) : {};
1331
1660
  };
@@ -1337,12 +1666,7 @@ var createPermissionsSlice = (set, get) => {
1337
1666
  const { cache: cache2, globalPermissions } = permissions;
1338
1667
  const resolveDataForItem = (item2, force2 = false) => __async(void 0, null, function* () {
1339
1668
  var _a, _b, _c;
1340
- const {
1341
- config: config2,
1342
- state: appState,
1343
- setComponentLoading,
1344
- unsetComponentLoading
1345
- } = get();
1669
+ const { config: config2, state: appState, setComponentLoading } = get();
1346
1670
  const componentConfig = item2.type === "root" ? config2.root : config2.components[item2.type];
1347
1671
  if (!componentConfig) {
1348
1672
  return;
@@ -1351,14 +1675,14 @@ var createPermissionsSlice = (set, get) => {
1351
1675
  if (componentConfig.resolvePermissions) {
1352
1676
  const changed = getChanged(item2, (_a = cache2[item2.props.id]) == null ? void 0 : _a.lastData);
1353
1677
  if (Object.values(changed).some((el) => el === true) || force2) {
1354
- setComponentLoading(item2.props.id);
1678
+ const clearTimeout2 = setComponentLoading(item2.props.id, true, 50);
1355
1679
  const resolvedPermissions = yield componentConfig.resolvePermissions(
1356
1680
  item2,
1357
1681
  {
1358
1682
  changed,
1359
1683
  lastPermissions: ((_b = cache2[item2.props.id]) == null ? void 0 : _b.lastPermissions) || null,
1360
1684
  permissions: initialPermissions,
1361
- appState,
1685
+ appState: makeStatePublic(appState),
1362
1686
  lastData: ((_c = cache2[item2.props.id]) == null ? void 0 : _c.lastData) || null
1363
1687
  }
1364
1688
  );
@@ -1376,7 +1700,7 @@ var createPermissionsSlice = (set, get) => {
1376
1700
  })
1377
1701
  })
1378
1702
  });
1379
- unsetComponentLoading(item2.props.id);
1703
+ clearTimeout2();
1380
1704
  }
1381
1705
  }
1382
1706
  });
@@ -1386,7 +1710,7 @@ var createPermissionsSlice = (set, get) => {
1386
1710
  // Shim the root data in by conforming to component data shape
1387
1711
  {
1388
1712
  type: "root",
1389
- props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "puck-root" })
1713
+ props: __spreadProps(__spreadValues({}, appState.data.root.props), { id: "root" })
1390
1714
  },
1391
1715
  force2
1392
1716
  );
@@ -1401,7 +1725,6 @@ var createPermissionsSlice = (set, get) => {
1401
1725
  } else if (root) {
1402
1726
  resolveDataForRoot(force);
1403
1727
  } else {
1404
- resolveDataForRoot(force);
1405
1728
  flattenData(state, config).map((item2) => __async(void 0, null, function* () {
1406
1729
  yield resolveDataForItem(item2, force);
1407
1730
  }));
@@ -1432,7 +1755,7 @@ var createPermissionsSlice = (set, get) => {
1432
1755
  } else if (root) {
1433
1756
  const rootConfig = config.root;
1434
1757
  const initialPermissions = __spreadValues(__spreadValues({}, globalPermissions), rootConfig == null ? void 0 : rootConfig.permissions);
1435
- const resolvedForItem = resolvedPermissions["puck-root"];
1758
+ const resolvedForItem = resolvedPermissions["root"];
1436
1759
  return resolvedForItem ? __spreadValues(__spreadValues({}, globalPermissions), resolvedForItem) : initialPermissions;
1437
1760
  }
1438
1761
  return globalPermissions;
@@ -1449,45 +1772,24 @@ var createFieldsSlice = (_set, _get) => {
1449
1772
  return {
1450
1773
  fields: {},
1451
1774
  loading: false,
1452
- lastResolvedData: {}
1775
+ lastResolvedData: {},
1776
+ id: void 0
1453
1777
  };
1454
1778
  };
1455
1779
 
1456
1780
  // ../core/lib/resolve-component-data.ts
1457
1781
  init_react_import();
1458
-
1459
- // ../core/lib/map-slots.ts
1460
- init_react_import();
1461
- function mapSlots(item, map, recursive = true, isSlot2) {
1462
- return __async(this, null, function* () {
1463
- const props = __spreadValues({}, item.props);
1464
- yield forEachSlot(
1465
- item,
1466
- (_parentId, propName, content) => __async(this, null, function* () {
1467
- const mappedContent = recursive ? yield Promise.all(
1468
- content.map((item2) => __async(this, null, function* () {
1469
- return yield mapSlots(item2, map, recursive, isSlot2);
1470
- }))
1471
- ) : content;
1472
- props[propName] = yield map(mappedContent, propName);
1473
- }),
1474
- false,
1475
- isSlot2
1476
- );
1477
- return __spreadProps(__spreadValues({}, item), { props });
1478
- });
1479
- }
1480
-
1481
- // ../core/lib/resolve-component-data.ts
1482
- var import_fast_deep_equal = __toESM(require_fast_deep_equal());
1782
+ var import_fast_deep_equal2 = __toESM(require_fast_deep_equal());
1483
1783
  var cache = { lastChange: {} };
1484
- var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace", recursive = true) {
1485
- const configForItem = "type" in item ? config.components[item.type] : config.root;
1486
- if ((configForItem == null ? void 0 : configForItem.resolveData) && item.props) {
1487
- const id = "id" in item.props ? item.props.id : "root";
1784
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace") {
1785
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
1786
+ const resolvedItem = __spreadValues({}, item);
1787
+ const shouldRunResolver = (configForItem == null ? void 0 : configForItem.resolveData) && item.props;
1788
+ const id = "id" in item.props ? item.props.id : "root";
1789
+ if (shouldRunResolver) {
1488
1790
  const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
1489
- if (item && item === oldItem) {
1490
- return resolved;
1791
+ if (item && (0, import_fast_deep_equal2.default)(item, oldItem)) {
1792
+ return { node: resolved, didChange: false };
1491
1793
  }
1492
1794
  const changed = getChanged(item, oldItem);
1493
1795
  if (onResolveStart) {
@@ -1496,47 +1798,48 @@ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], f
1496
1798
  const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
1497
1799
  changed,
1498
1800
  lastData: oldItem,
1499
- metadata,
1801
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
1500
1802
  trigger
1501
1803
  });
1502
- let resolvedItem = __spreadProps(__spreadValues({}, item), {
1503
- props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
1504
- });
1505
- if (recursive) {
1506
- resolvedItem = yield mapSlots(resolvedItem, (content) => __async(void 0, null, function* () {
1507
- return Promise.all(
1508
- content.map(
1509
- (childItem) => __async(void 0, null, function* () {
1510
- return (yield resolveComponentData(
1511
- childItem,
1512
- config,
1513
- metadata,
1514
- onResolveStart,
1515
- onResolveEnd,
1516
- trigger,
1517
- false
1518
- )).node;
1519
- })
1520
- )
1521
- );
1522
- }));
1523
- }
1804
+ resolvedItem.props = __spreadValues(__spreadValues({}, item.props), resolvedProps);
1524
1805
  if (Object.keys(readOnly).length) {
1525
1806
  resolvedItem.readOnly = readOnly;
1526
1807
  }
1527
- cache.lastChange[id] = {
1528
- item,
1529
- resolved: resolvedItem
1530
- };
1531
- if (onResolveEnd) {
1532
- onResolveEnd(resolvedItem);
1533
- }
1534
- return { node: resolvedItem, didChange: !(0, import_fast_deep_equal.default)(item, resolvedItem) };
1535
1808
  }
1536
- return { node: item, didChange: false };
1809
+ let itemWithResolvedChildren = yield mapSlots(
1810
+ resolvedItem,
1811
+ (content) => __async(void 0, null, function* () {
1812
+ return yield Promise.all(
1813
+ content.map(
1814
+ (childItem) => __async(void 0, null, function* () {
1815
+ return (yield resolveComponentData(
1816
+ childItem,
1817
+ config,
1818
+ metadata,
1819
+ onResolveStart,
1820
+ onResolveEnd,
1821
+ trigger
1822
+ )).node;
1823
+ })
1824
+ )
1825
+ );
1826
+ }),
1827
+ config
1828
+ );
1829
+ if (shouldRunResolver && onResolveEnd) {
1830
+ onResolveEnd(resolvedItem);
1831
+ }
1832
+ cache.lastChange[id] = {
1833
+ item,
1834
+ resolved: itemWithResolvedChildren
1835
+ };
1836
+ return {
1837
+ node: itemWithResolvedChildren,
1838
+ didChange: !(0, import_fast_deep_equal2.default)(item, itemWithResolvedChildren)
1839
+ };
1537
1840
  });
1538
1841
 
1539
- // ../core/lib/to-root.ts
1842
+ // ../core/lib/data/to-root.ts
1540
1843
  init_react_import();
1541
1844
  var toRoot = (item) => {
1542
1845
  if ("type" in item && item.type !== "root") {
@@ -1553,7 +1856,8 @@ var toRoot = (item) => {
1553
1856
  return { props: {}, readOnly };
1554
1857
  };
1555
1858
 
1556
- // ../core/store/index.ts
1859
+ // ../core/store/default-app-state.ts
1860
+ init_react_import();
1557
1861
  var defaultAppState = {
1558
1862
  data: { content: [], root: {}, zones: {} },
1559
1863
  ui: {
@@ -1579,178 +1883,182 @@ var defaultAppState = {
1579
1883
  zones: {}
1580
1884
  }
1581
1885
  };
1886
+
1887
+ // ../core/store/index.ts
1582
1888
  var defaultPageFields = {
1583
1889
  title: { type: "text" }
1584
1890
  };
1585
1891
  var createAppStore = (initialAppStore) => create()(
1586
- subscribeWithSelector((set, get) => __spreadProps(__spreadValues({
1587
- state: defaultAppState,
1588
- config: { components: {} },
1589
- componentState: {},
1590
- plugins: [],
1591
- overrides: {},
1592
- viewports: defaultViewports,
1593
- zoomConfig: {
1594
- autoZoom: 1,
1595
- rootHeight: 0,
1596
- zoom: 1
1597
- },
1598
- status: "LOADING",
1599
- iframe: {},
1600
- metadata: {}
1601
- }, initialAppStore), {
1602
- fields: createFieldsSlice(set, get),
1603
- history: createHistorySlice(set, get),
1604
- nodes: createNodesSlice(set, get),
1605
- permissions: createPermissionsSlice(set, get),
1606
- getComponentConfig: (type) => {
1607
- var _a;
1608
- const { config, selectedItem } = get();
1609
- const rootFields = ((_a = config.root) == null ? void 0 : _a.fields) || defaultPageFields;
1610
- return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1611
- },
1612
- dispatch: (action) => set((s) => {
1613
- var _a, _b;
1614
- const { record } = get().history;
1615
- const dispatch = createReducer({
1616
- config: s.config,
1617
- record,
1618
- appStore: s
1619
- });
1620
- const state = dispatch(s.state, action);
1621
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1622
- (_b = (_a = get()).onAction) == null ? void 0 : _b.call(_a, action, state, get().state);
1623
- return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1624
- }),
1625
- setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1626
- setStatus: (status) => set({ status }),
1627
- setComponentState: (componentState) => set({ componentState }),
1628
- pendingComponentLoads: {},
1629
- setComponentLoading: (id, loading = true, defer = 0) => {
1630
- const { setComponentState, pendingComponentLoads } = get();
1631
- const thisPendingComponentLoads = __spreadValues({}, pendingComponentLoads);
1632
- const setLoading = () => {
1633
- var _a;
1634
- const { componentState } = get();
1635
- setComponentState(__spreadProps(__spreadValues({}, componentState), {
1636
- [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1637
- loadingCount: (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) + 1
1638
- })
1639
- }));
1640
- };
1641
- const unsetLoading = () => {
1642
- var _a;
1643
- const { componentState } = get();
1644
- setComponentState(__spreadProps(__spreadValues({}, componentState), {
1645
- [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1646
- loadingCount: Math.max(
1647
- (((_a = componentState[id]) == null ? void 0 : _a.loadingCount) || 0) - 1,
1648
- 0
1649
- )
1892
+ subscribeWithSelector((set, get) => {
1893
+ var _a, _b;
1894
+ return __spreadProps(__spreadValues({
1895
+ state: defaultAppState,
1896
+ config: { components: {} },
1897
+ componentState: {},
1898
+ plugins: [],
1899
+ overrides: {},
1900
+ viewports: defaultViewports,
1901
+ zoomConfig: {
1902
+ autoZoom: 1,
1903
+ rootHeight: 0,
1904
+ zoom: 1
1905
+ },
1906
+ status: "LOADING",
1907
+ iframe: {},
1908
+ metadata: {}
1909
+ }, initialAppStore), {
1910
+ fields: createFieldsSlice(set, get),
1911
+ history: createHistorySlice(set, get),
1912
+ nodes: createNodesSlice(set, get),
1913
+ permissions: createPermissionsSlice(set, get),
1914
+ getComponentConfig: (type) => {
1915
+ var _a2;
1916
+ const { config, selectedItem } = get();
1917
+ const rootFields = ((_a2 = config.root) == null ? void 0 : _a2.fields) || defaultPageFields;
1918
+ return type && type !== "root" ? config.components[type] : selectedItem ? config.components[selectedItem.type] : __spreadProps(__spreadValues({}, config.root), { fields: rootFields });
1919
+ },
1920
+ selectedItem: ((_a = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _a.ui.itemSelector) ? getItem(
1921
+ (_b = initialAppStore == null ? void 0 : initialAppStore.state) == null ? void 0 : _b.ui.itemSelector,
1922
+ initialAppStore.state
1923
+ ) : null,
1924
+ dispatch: (action) => set((s) => {
1925
+ var _a2, _b2;
1926
+ const { record } = get().history;
1927
+ const dispatch = createReducer({
1928
+ record,
1929
+ appStore: s
1930
+ });
1931
+ const state = dispatch(s.state, action);
1932
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1933
+ (_b2 = (_a2 = get()).onAction) == null ? void 0 : _b2.call(_a2, action, state, get().state);
1934
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1935
+ }),
1936
+ setZoomConfig: (zoomConfig) => set({ zoomConfig }),
1937
+ setStatus: (status) => set({ status }),
1938
+ setComponentState: (componentState) => set({ componentState }),
1939
+ pendingLoadTimeouts: {},
1940
+ setComponentLoading: (id, loading = true, defer = 0) => {
1941
+ const { setComponentState, pendingLoadTimeouts } = get();
1942
+ const loadId = generateId();
1943
+ const setLoading = () => {
1944
+ var _a2;
1945
+ const { componentState } = get();
1946
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1947
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1948
+ loadingCount: (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) + 1
1949
+ })
1950
+ }));
1951
+ };
1952
+ const unsetLoading = () => {
1953
+ var _a2;
1954
+ const { componentState } = get();
1955
+ clearTimeout(timeout);
1956
+ delete pendingLoadTimeouts[loadId];
1957
+ set({ pendingLoadTimeouts });
1958
+ setComponentState(__spreadProps(__spreadValues({}, componentState), {
1959
+ [id]: __spreadProps(__spreadValues({}, componentState[id]), {
1960
+ loadingCount: Math.max(
1961
+ (((_a2 = componentState[id]) == null ? void 0 : _a2.loadingCount) || 0) - 1,
1962
+ 0
1963
+ )
1964
+ })
1965
+ }));
1966
+ };
1967
+ const timeout = setTimeout(() => {
1968
+ if (loading) {
1969
+ setLoading();
1970
+ } else {
1971
+ unsetLoading();
1972
+ }
1973
+ delete pendingLoadTimeouts[loadId];
1974
+ set({ pendingLoadTimeouts });
1975
+ }, defer);
1976
+ set({
1977
+ pendingLoadTimeouts: __spreadProps(__spreadValues({}, pendingLoadTimeouts), {
1978
+ [id]: timeout
1650
1979
  })
1651
- }));
1652
- };
1653
- if (thisPendingComponentLoads[id]) {
1654
- clearTimeout(thisPendingComponentLoads[id]);
1655
- delete thisPendingComponentLoads[id];
1656
- set({ pendingComponentLoads: thisPendingComponentLoads });
1657
- }
1658
- const timeout = setTimeout(() => {
1659
- if (loading) {
1660
- setLoading();
1661
- } else {
1662
- unsetLoading();
1663
- }
1664
- delete thisPendingComponentLoads[id];
1665
- set({ pendingComponentLoads: thisPendingComponentLoads });
1666
- }, defer);
1667
- set({
1668
- pendingComponentLoads: __spreadProps(__spreadValues({}, thisPendingComponentLoads), {
1669
- [id]: timeout
1670
- })
1671
- });
1672
- },
1673
- unsetComponentLoading: (id) => {
1674
- const { setComponentLoading } = get();
1675
- setComponentLoading(id, false);
1676
- },
1677
- // Helper
1678
- setUi: (ui, recordHistory) => set((s) => {
1679
- const dispatch = createReducer({
1680
- config: s.config,
1681
- record: () => {
1682
- },
1683
- appStore: s
1684
- });
1685
- const state = dispatch(s.state, {
1686
- type: "setUi",
1687
- ui,
1688
- recordHistory
1689
- });
1690
- const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
1691
- return __spreadProps(__spreadValues({}, s), { state, selectedItem });
1692
- }),
1693
- // resolveDataRuns: 0,
1694
- // resolveData: (newAppState) =>
1695
- // set((s) => {
1696
- // resolveData(newAppState, get);
1697
- // return { ...s, resolveDataRuns: s.resolveDataRuns + 1 };
1698
- // }),
1699
- resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
1700
- const { config, metadata, setComponentLoading } = get();
1701
- return yield resolveComponentData(
1702
- componentData,
1703
- config,
1704
- metadata,
1705
- (item) => setComponentLoading(
1706
- "id" in item.props ? item.props.id : "root",
1707
- true,
1708
- 50
1709
- ),
1710
- (item) => setComponentLoading(
1711
- "id" in item.props ? item.props.id : "root",
1712
- false,
1713
- 0
1714
- ),
1715
- trigger
1716
- );
1717
- }),
1718
- resolveAndCommitData: () => __async(void 0, null, function* () {
1719
- const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
1720
- walkTree(
1721
- state,
1722
- config,
1723
- (content) => content,
1724
- (childItem) => {
1725
- resolveComponentData2(childItem, "load").then((resolved) => {
1726
- const { state: state2 } = get();
1727
- const node = state2.indexes.nodes[resolved.node.props.id];
1728
- if (node && resolved.didChange) {
1729
- if (resolved.node.props.id === "root") {
1730
- dispatch({
1731
- type: "replaceRoot",
1732
- root: toRoot(resolved.node)
1733
- });
1734
- } else {
1735
- const zoneCompound = `${node.parentId}:${node.zone}`;
1736
- const parentZone = state2.indexes.zones[zoneCompound];
1737
- const index = parentZone.contentIds.indexOf(
1738
- resolved.node.props.id
1739
- );
1740
- dispatch({
1741
- type: "replace",
1742
- data: resolved.node,
1743
- destinationIndex: index,
1744
- destinationZone: zoneCompound
1745
- });
1746
- }
1980
+ });
1981
+ return unsetLoading;
1982
+ },
1983
+ unsetComponentLoading: (id) => {
1984
+ const { setComponentLoading } = get();
1985
+ setComponentLoading(id, false);
1986
+ },
1987
+ // Helper
1988
+ setUi: (ui, recordHistory) => set((s) => {
1989
+ const dispatch = createReducer({
1990
+ record: () => {
1991
+ },
1992
+ appStore: s
1993
+ });
1994
+ const state = dispatch(s.state, {
1995
+ type: "setUi",
1996
+ ui,
1997
+ recordHistory
1998
+ });
1999
+ const selectedItem = state.ui.itemSelector ? getItem(state.ui.itemSelector, state) : null;
2000
+ return __spreadProps(__spreadValues({}, s), { state, selectedItem });
2001
+ }),
2002
+ resolveComponentData: (componentData, trigger) => __async(void 0, null, function* () {
2003
+ const { config, metadata, setComponentLoading, permissions } = get();
2004
+ const timeouts = {};
2005
+ return yield resolveComponentData(
2006
+ componentData,
2007
+ config,
2008
+ metadata,
2009
+ (item) => {
2010
+ const id = "id" in item.props ? item.props.id : "root";
2011
+ timeouts[id] = setComponentLoading(id, true, 50);
2012
+ },
2013
+ (item) => __async(void 0, null, function* () {
2014
+ const id = "id" in item.props ? item.props.id : "root";
2015
+ if ("type" in item) {
2016
+ yield permissions.refreshPermissions({ item });
2017
+ } else {
2018
+ yield permissions.refreshPermissions({ root: true });
1747
2019
  }
1748
- });
1749
- return childItem;
1750
- }
1751
- );
1752
- })
1753
- }))
2020
+ timeouts[id]();
2021
+ }),
2022
+ trigger
2023
+ );
2024
+ }),
2025
+ resolveAndCommitData: () => __async(void 0, null, function* () {
2026
+ const { config, state, dispatch, resolveComponentData: resolveComponentData2 } = get();
2027
+ walkAppState(
2028
+ state,
2029
+ config,
2030
+ (content) => content,
2031
+ (childItem) => {
2032
+ resolveComponentData2(childItem, "load").then((resolved) => {
2033
+ const { state: state2 } = get();
2034
+ const node = state2.indexes.nodes[resolved.node.props.id];
2035
+ if (node && resolved.didChange) {
2036
+ if (resolved.node.props.id === "root") {
2037
+ dispatch({
2038
+ type: "replaceRoot",
2039
+ root: toRoot(resolved.node)
2040
+ });
2041
+ } else {
2042
+ const zoneCompound = `${node.parentId}:${node.zone}`;
2043
+ const parentZone = state2.indexes.zones[zoneCompound];
2044
+ const index = parentZone.contentIds.indexOf(
2045
+ resolved.node.props.id
2046
+ );
2047
+ dispatch({
2048
+ type: "replace",
2049
+ data: resolved.node,
2050
+ destinationIndex: index,
2051
+ destinationZone: zoneCompound
2052
+ });
2053
+ }
2054
+ }
2055
+ });
2056
+ return childItem;
2057
+ }
2058
+ );
2059
+ })
2060
+ });
2061
+ })
1754
2062
  );
1755
2063
  var appStoreContext = (0, import_react9.createContext)(createAppStore());
1756
2064
  function useAppStore(selector) {
@@ -1812,7 +2120,10 @@ init_react_import();
1812
2120
  // ../core/lib/filter.ts
1813
2121
  init_react_import();
1814
2122
 
1815
- // ../core/lib/replace.ts
2123
+ // ../core/lib/data/reorder.ts
2124
+ init_react_import();
2125
+
2126
+ // ../core/lib/data/replace.ts
1816
2127
  init_react_import();
1817
2128
 
1818
2129
  // css-module:/home/runner/work/puck/puck/packages/core/components/Loader/styles.module.css#css-module
@@ -1998,15 +2309,37 @@ var HeadingAnalyzer = () => {
1998
2309
  const [hierarchy, setHierarchy] = (0, import_react11.useState)([]);
1999
2310
  (0, import_react11.useEffect)(() => {
2000
2311
  const frame = getFrame();
2001
- const entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2002
- if (!entry) return;
2003
- setHierarchy(buildHierarchy(entry));
2004
- const observer = new MutationObserver(() => {
2312
+ let entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2313
+ const createHierarchy = () => {
2005
2314
  setHierarchy(buildHierarchy(entry));
2315
+ };
2316
+ const entryObserver = new MutationObserver(() => {
2317
+ createHierarchy();
2318
+ });
2319
+ const frameObserver = new MutationObserver(() => {
2320
+ entry = frame == null ? void 0 : frame.querySelector(`[data-puck-entry]`);
2321
+ if (entry) {
2322
+ registerEntryObserver();
2323
+ frameObserver.disconnect();
2324
+ }
2006
2325
  });
2007
- observer.observe(entry, { subtree: true, childList: true });
2326
+ const registerEntryObserver = () => {
2327
+ if (!entry) return;
2328
+ entryObserver.observe(entry, { subtree: true, childList: true });
2329
+ };
2330
+ const registerFrameObserver = () => {
2331
+ if (!frame) return;
2332
+ frameObserver.observe(frame, { subtree: true, childList: true });
2333
+ };
2334
+ if (entry) {
2335
+ createHierarchy();
2336
+ registerEntryObserver();
2337
+ } else {
2338
+ registerFrameObserver();
2339
+ }
2008
2340
  return () => {
2009
- observer.disconnect();
2341
+ entryObserver.disconnect();
2342
+ frameObserver.disconnect();
2010
2343
  };
2011
2344
  }, [data]);
2012
2345
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: getClassName5(), children: [