@measured/puck-plugin-heading-analyzer 0.19.0-canary.1918ad70 → 0.19.0-canary.1e8a3ae2

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