@measured/puck 0.21.0-canary.b818cb1f → 0.21.0-canary.c0db75c1

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.
@@ -0,0 +1,906 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __export = (target, all) => {
26
+ for (var name in all)
27
+ __defProp(target, name, { get: all[name], enumerable: true });
28
+ };
29
+ var __copyProps = (to, from, except, desc) => {
30
+ if (from && typeof from === "object" || typeof from === "function") {
31
+ for (let key of __getOwnPropNames(from))
32
+ if (!__hasOwnProp.call(to, key) && key !== except)
33
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
34
+ }
35
+ return to;
36
+ };
37
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
+ // If the importer is in node compatibility mode or this is not an ESM
39
+ // file that has been converted to a CommonJS file using a Babel-
40
+ // compatible transform (i.e. "__esModule" has not been set), then set
41
+ // "default" to the CommonJS "module.exports" for node compatibility.
42
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
+ mod
44
+ ));
45
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
+
47
+ // bundle/internal.ts
48
+ var internal_exports = {};
49
+ __export(internal_exports, {
50
+ createReducer: () => createReducer
51
+ });
52
+ module.exports = __toCommonJS(internal_exports);
53
+
54
+ // ../tsup-config/react-import.js
55
+ var import_react = __toESM(require("react"));
56
+
57
+ // lib/root-droppable-id.ts
58
+ var rootAreaId = "root";
59
+ var rootZone = "default-zone";
60
+ var rootDroppableId = `${rootAreaId}:${rootZone}`;
61
+
62
+ // lib/get-zone-id.ts
63
+ var getZoneId = (zoneCompound) => {
64
+ if (!zoneCompound) {
65
+ return [];
66
+ }
67
+ if (zoneCompound && zoneCompound.indexOf(":") > -1) {
68
+ return zoneCompound.split(":");
69
+ }
70
+ return [rootDroppableId, zoneCompound];
71
+ };
72
+
73
+ // lib/data/for-related-zones.ts
74
+ function forRelatedZones(item, data, cb, path = []) {
75
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
76
+ const [parentId] = getZoneId(zoneCompound);
77
+ if (parentId === item.props.id) {
78
+ cb(path, zoneCompound, content);
79
+ }
80
+ });
81
+ }
82
+
83
+ // lib/data/default-slots.ts
84
+ var defaultSlots = (value, fields) => Object.keys(fields).reduce(
85
+ (acc, fieldName) => fields[fieldName].type === "slot" ? __spreadValues({ [fieldName]: [] }, acc) : acc,
86
+ value
87
+ );
88
+
89
+ // lib/data/map-fields.ts
90
+ var isPromise = (v) => !!v && typeof v.then === "function";
91
+ var flatten = (values) => values.reduce((acc, item) => __spreadValues(__spreadValues({}, acc), item), {});
92
+ var containsPromise = (arr) => arr.some(isPromise);
93
+ var walkField = ({
94
+ value,
95
+ fields,
96
+ mappers,
97
+ propKey = "",
98
+ propPath = "",
99
+ id = "",
100
+ config,
101
+ recurseSlots = false
102
+ }) => {
103
+ var _a, _b, _c;
104
+ const fieldType = (_a = fields[propKey]) == null ? void 0 : _a.type;
105
+ const map = mappers[fieldType];
106
+ if (map && fieldType === "slot") {
107
+ const content = value || [];
108
+ const mappedContent = recurseSlots ? content.map((el) => {
109
+ var _a2;
110
+ const componentConfig = config.components[el.type];
111
+ if (!componentConfig) {
112
+ throw new Error(`Could not find component config for ${el.type}`);
113
+ }
114
+ const fields2 = (_a2 = componentConfig.fields) != null ? _a2 : {};
115
+ return walkField({
116
+ value: __spreadProps(__spreadValues({}, el), { props: defaultSlots(el.props, fields2) }),
117
+ fields: fields2,
118
+ mappers,
119
+ id: el.props.id,
120
+ config,
121
+ recurseSlots
122
+ });
123
+ }) : content;
124
+ if (containsPromise(mappedContent)) {
125
+ return Promise.all(mappedContent);
126
+ }
127
+ return map({
128
+ value: mappedContent,
129
+ parentId: id,
130
+ propName: propPath,
131
+ field: fields[propKey],
132
+ propPath
133
+ });
134
+ } else if (map && fields[propKey]) {
135
+ return map({
136
+ value,
137
+ parentId: id,
138
+ propName: propKey,
139
+ field: fields[propKey],
140
+ propPath
141
+ });
142
+ }
143
+ if (value && typeof value === "object") {
144
+ if (Array.isArray(value)) {
145
+ const arrayFields = ((_b = fields[propKey]) == null ? void 0 : _b.type) === "array" ? fields[propKey].arrayFields : null;
146
+ if (!arrayFields) return value;
147
+ const newValue = value.map(
148
+ (el, idx) => walkField({
149
+ value: el,
150
+ fields: arrayFields,
151
+ mappers,
152
+ propKey,
153
+ propPath: `${propPath}[${idx}]`,
154
+ id,
155
+ config,
156
+ recurseSlots
157
+ })
158
+ );
159
+ if (containsPromise(newValue)) {
160
+ return Promise.all(newValue);
161
+ }
162
+ return newValue;
163
+ } else if ("$$typeof" in value) {
164
+ return value;
165
+ } else {
166
+ const objectFields = ((_c = fields[propKey]) == null ? void 0 : _c.type) === "object" ? fields[propKey].objectFields : fields;
167
+ return walkObject({
168
+ value,
169
+ fields: objectFields,
170
+ mappers,
171
+ id,
172
+ getPropPath: (k) => `${propPath}.${k}`,
173
+ config,
174
+ recurseSlots
175
+ });
176
+ }
177
+ }
178
+ return value;
179
+ };
180
+ var walkObject = ({
181
+ value,
182
+ fields,
183
+ mappers,
184
+ id,
185
+ getPropPath,
186
+ config,
187
+ recurseSlots
188
+ }) => {
189
+ const newProps = Object.entries(value).map(([k, v]) => {
190
+ const opts = {
191
+ value: v,
192
+ fields,
193
+ mappers,
194
+ propKey: k,
195
+ propPath: getPropPath(k),
196
+ id,
197
+ config,
198
+ recurseSlots
199
+ };
200
+ const newValue = walkField(opts);
201
+ if (isPromise(newValue)) {
202
+ return newValue.then((resolvedValue) => ({
203
+ [k]: resolvedValue
204
+ }));
205
+ }
206
+ return {
207
+ [k]: newValue
208
+ };
209
+ }, {});
210
+ if (containsPromise(newProps)) {
211
+ return Promise.all(newProps).then(flatten);
212
+ }
213
+ return flatten(newProps);
214
+ };
215
+ function mapFields(item, mappers, config, recurseSlots = false) {
216
+ var _a, _b, _c, _d, _e;
217
+ const itemType = "type" in item ? item.type : "root";
218
+ const componentConfig = itemType === "root" ? config.root : (_a = config.components) == null ? void 0 : _a[itemType];
219
+ const newProps = walkObject({
220
+ value: defaultSlots((_b = item.props) != null ? _b : {}, (_c = componentConfig == null ? void 0 : componentConfig.fields) != null ? _c : {}),
221
+ fields: (_d = componentConfig == null ? void 0 : componentConfig.fields) != null ? _d : {},
222
+ mappers,
223
+ id: item.props ? (_e = item.props.id) != null ? _e : "root" : "root",
224
+ getPropPath: (k) => k,
225
+ config,
226
+ recurseSlots
227
+ });
228
+ if (isPromise(newProps)) {
229
+ return newProps.then((resolvedProps) => __spreadProps(__spreadValues({}, item), {
230
+ props: resolvedProps
231
+ }));
232
+ }
233
+ return __spreadProps(__spreadValues({}, item), {
234
+ props: newProps
235
+ });
236
+ }
237
+
238
+ // lib/data/flatten-node.ts
239
+ var import_flat = __toESM(require("flat"));
240
+
241
+ // lib/data/strip-slots.ts
242
+ var stripSlots = (data, config) => {
243
+ return mapFields(data, { slot: () => null }, config);
244
+ };
245
+
246
+ // lib/data/flatten-node.ts
247
+ var { flatten: flatten2, unflatten } = import_flat.default;
248
+ var flattenNode = (node, config) => {
249
+ return __spreadProps(__spreadValues({}, node), {
250
+ props: flatten2(stripSlots(node, config).props)
251
+ });
252
+ };
253
+
254
+ // lib/data/walk-app-state.ts
255
+ function walkAppState(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
256
+ var _a;
257
+ let newZones = {};
258
+ const newZoneIndex = {};
259
+ const newNodeIndex = {};
260
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
261
+ var _a2;
262
+ const [parentId] = zoneCompound.split(":");
263
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
264
+ const [_2, zone] = zoneCompound.split(":");
265
+ const newZoneCompound = `${newId || parentId}:${zone}`;
266
+ const newContent2 = mappedContent.map(
267
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
268
+ );
269
+ newZoneIndex[newZoneCompound] = {
270
+ contentIds: newContent2.map((item) => item.props.id),
271
+ type: zoneType
272
+ };
273
+ return [newZoneCompound, newContent2];
274
+ };
275
+ const processRelatedZones = (item, newId, initialPath) => {
276
+ forRelatedZones(
277
+ item,
278
+ state.data,
279
+ (relatedPath, relatedZoneCompound, relatedContent) => {
280
+ const [zoneCompound, newContent2] = processContent(
281
+ relatedPath,
282
+ relatedZoneCompound,
283
+ relatedContent,
284
+ "dropzone",
285
+ newId
286
+ );
287
+ newZones[zoneCompound] = newContent2;
288
+ },
289
+ initialPath
290
+ );
291
+ };
292
+ const processItem = (item, path, index) => {
293
+ const mappedItem = mapNodeOrSkip(item, path, index);
294
+ if (!mappedItem) return item;
295
+ const id = mappedItem.props.id;
296
+ const newProps = __spreadProps(__spreadValues({}, mapFields(
297
+ mappedItem,
298
+ {
299
+ slot: ({ value, parentId: parentId2, propPath }) => {
300
+ const content = value;
301
+ const zoneCompound = `${parentId2}:${propPath}`;
302
+ const [_2, newContent2] = processContent(
303
+ path,
304
+ zoneCompound,
305
+ content,
306
+ "slot",
307
+ parentId2
308
+ );
309
+ return newContent2;
310
+ }
311
+ },
312
+ config
313
+ ).props), {
314
+ id
315
+ });
316
+ processRelatedZones(item, id, path);
317
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
318
+ const thisZoneCompound = path[path.length - 1];
319
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
320
+ newNodeIndex[id] = {
321
+ data: newItem,
322
+ flatData: flattenNode(newItem, config),
323
+ path,
324
+ parentId,
325
+ zone
326
+ };
327
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
328
+ if (newProps.id === "root") {
329
+ delete finalData["type"];
330
+ delete finalData.props["id"];
331
+ }
332
+ return finalData;
333
+ };
334
+ const zones = state.data.zones || {};
335
+ const [_, newContent] = processContent(
336
+ [],
337
+ rootDroppableId,
338
+ state.data.content,
339
+ "root"
340
+ );
341
+ const processedContent = newContent;
342
+ const zonesAlreadyProcessed = Object.keys(newZones);
343
+ Object.keys(zones || {}).forEach((zoneCompound) => {
344
+ const [parentId] = zoneCompound.split(":");
345
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
346
+ return;
347
+ }
348
+ const [_2, newContent2] = processContent(
349
+ [rootDroppableId],
350
+ zoneCompound,
351
+ zones[zoneCompound],
352
+ "dropzone",
353
+ parentId
354
+ );
355
+ newZones[zoneCompound] = newContent2;
356
+ }, newZones);
357
+ const processedRoot = processItem(
358
+ {
359
+ type: "root",
360
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
361
+ },
362
+ [],
363
+ -1
364
+ );
365
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
366
+ props: processedRoot.props
367
+ });
368
+ return __spreadProps(__spreadValues({}, state), {
369
+ data: {
370
+ root,
371
+ content: processedContent,
372
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
373
+ },
374
+ indexes: {
375
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
376
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
377
+ }
378
+ });
379
+ }
380
+
381
+ // reducer/actions/set.ts
382
+ var setAction = (state, action, appStore) => {
383
+ if (typeof action.state === "object") {
384
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
385
+ if (action.state.indexes) {
386
+ return newState;
387
+ }
388
+ console.warn(
389
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
390
+ );
391
+ return walkAppState(newState, appStore.config);
392
+ }
393
+ return __spreadValues(__spreadValues({}, state), action.state(state));
394
+ };
395
+
396
+ // lib/data/insert.ts
397
+ var insert = (list, index, item) => {
398
+ const result = Array.from(list || []);
399
+ result.splice(index, 0, item);
400
+ return result;
401
+ };
402
+
403
+ // lib/generate-id.ts
404
+ var import_uuid = require("uuid");
405
+ var generateId = (type) => type ? `${type}-${(0, import_uuid.v4)()}` : (0, import_uuid.v4)();
406
+
407
+ // lib/data/get-ids-for-parent.ts
408
+ var getIdsForParent = (zoneCompound, state) => {
409
+ const [parentId] = zoneCompound.split(":");
410
+ const node = state.indexes.nodes[parentId];
411
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
412
+ };
413
+
414
+ // lib/data/walk-tree.ts
415
+ function walkTree(data, config, callbackFn) {
416
+ var _a, _b;
417
+ const walkItem = (item) => {
418
+ return mapFields(
419
+ item,
420
+ {
421
+ slot: ({ value, parentId, propName }) => {
422
+ var _a2;
423
+ const content = value;
424
+ return (_a2 = callbackFn(content, { parentId, propName })) != null ? _a2 : content;
425
+ }
426
+ },
427
+ config,
428
+ true
429
+ );
430
+ };
431
+ if ("props" in data) {
432
+ return walkItem(data);
433
+ }
434
+ const _data = data;
435
+ const zones = (_a = _data.zones) != null ? _a : {};
436
+ const mappedContent = _data.content.map(walkItem);
437
+ return {
438
+ root: walkItem(_data.root),
439
+ content: (_b = callbackFn(mappedContent, {
440
+ parentId: "root",
441
+ propName: "default-zone"
442
+ })) != null ? _b : mappedContent,
443
+ zones: Object.keys(zones).reduce(
444
+ (acc, zoneCompound) => __spreadProps(__spreadValues({}, acc), {
445
+ [zoneCompound]: zones[zoneCompound].map(walkItem)
446
+ }),
447
+ {}
448
+ )
449
+ };
450
+ }
451
+
452
+ // lib/data/populate-ids.ts
453
+ var populateIds = (data, config, override = false) => {
454
+ const id = generateId(data.type);
455
+ return walkTree(
456
+ __spreadProps(__spreadValues({}, data), {
457
+ props: override ? __spreadProps(__spreadValues({}, data.props), { id }) : __spreadValues({}, data.props)
458
+ }),
459
+ config,
460
+ (contents) => contents.map((item) => {
461
+ const id2 = generateId(item.type);
462
+ return __spreadProps(__spreadValues({}, item), {
463
+ props: override ? __spreadProps(__spreadValues({}, item.props), { id: id2 }) : __spreadValues({ id: id2 }, item.props)
464
+ });
465
+ })
466
+ );
467
+ };
468
+
469
+ // reducer/actions/insert.ts
470
+ function insertAction(state, action, appStore) {
471
+ const id = action.id || generateId(action.componentType);
472
+ const emptyComponentData = populateIds(
473
+ {
474
+ type: action.componentType,
475
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
476
+ id
477
+ })
478
+ },
479
+ appStore.config
480
+ );
481
+ const [parentId] = action.destinationZone.split(":");
482
+ const idsInPath = getIdsForParent(action.destinationZone, state);
483
+ return walkAppState(
484
+ state,
485
+ appStore.config,
486
+ (content, zoneCompound) => {
487
+ if (zoneCompound === action.destinationZone) {
488
+ return insert(
489
+ content || [],
490
+ action.destinationIndex,
491
+ emptyComponentData
492
+ );
493
+ }
494
+ return content;
495
+ },
496
+ (childItem, path) => {
497
+ if (childItem.props.id === id || childItem.props.id === parentId) {
498
+ return childItem;
499
+ } else if (idsInPath.includes(childItem.props.id)) {
500
+ return childItem;
501
+ } else if (path.includes(action.destinationZone)) {
502
+ return childItem;
503
+ }
504
+ return null;
505
+ }
506
+ );
507
+ }
508
+
509
+ // reducer/actions/replace.ts
510
+ var replaceAction = (state, action, appStore) => {
511
+ const [parentId] = action.destinationZone.split(":");
512
+ const idsInPath = getIdsForParent(action.destinationZone, state);
513
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
514
+ const idChanged = originalId !== action.data.props.id;
515
+ if (idChanged) {
516
+ throw new Error(
517
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
518
+ );
519
+ }
520
+ const newSlotIds = [];
521
+ const data = walkTree(action.data, appStore.config, (contents, opts) => {
522
+ newSlotIds.push(`${opts.parentId}:${opts.propName}`);
523
+ return contents.map((item) => {
524
+ const id = generateId(item.type);
525
+ return __spreadProps(__spreadValues({}, item), {
526
+ props: __spreadValues({ id }, item.props)
527
+ });
528
+ });
529
+ });
530
+ const stateWithDeepSlotsRemoved = __spreadValues({}, state);
531
+ Object.keys(state.indexes.zones).forEach((zoneCompound) => {
532
+ const id = zoneCompound.split(":")[0];
533
+ if (id === originalId) {
534
+ if (!newSlotIds.includes(zoneCompound)) {
535
+ delete stateWithDeepSlotsRemoved.indexes.zones[zoneCompound];
536
+ }
537
+ }
538
+ });
539
+ return walkAppState(
540
+ stateWithDeepSlotsRemoved,
541
+ appStore.config,
542
+ (content, zoneCompound) => {
543
+ const newContent = [...content];
544
+ if (zoneCompound === action.destinationZone) {
545
+ newContent[action.destinationIndex] = data;
546
+ }
547
+ return newContent;
548
+ },
549
+ (childItem, path) => {
550
+ const pathIds = path.map((p) => p.split(":")[0]);
551
+ if (childItem.props.id === data.props.id) {
552
+ return data;
553
+ } else if (childItem.props.id === parentId) {
554
+ return childItem;
555
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
556
+ return childItem;
557
+ } else if (pathIds.indexOf(data.props.id) > -1) {
558
+ return childItem;
559
+ }
560
+ return null;
561
+ }
562
+ );
563
+ };
564
+
565
+ // reducer/actions/replace-root.ts
566
+ var replaceRootAction = (state, action, appStore) => {
567
+ return walkAppState(
568
+ state,
569
+ appStore.config,
570
+ (content) => content,
571
+ (childItem) => {
572
+ if (childItem.props.id === "root") {
573
+ return __spreadProps(__spreadValues({}, childItem), {
574
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
575
+ readOnly: action.root.readOnly
576
+ });
577
+ }
578
+ return childItem;
579
+ }
580
+ );
581
+ };
582
+
583
+ // lib/data/get-item.ts
584
+ function getItem(selector, state) {
585
+ var _a, _b;
586
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
587
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
588
+ }
589
+
590
+ // reducer/actions/duplicate.ts
591
+ function duplicateAction(state, action, appStore) {
592
+ const item = getItem(
593
+ { index: action.sourceIndex, zone: action.sourceZone },
594
+ state
595
+ );
596
+ const idsInPath = getIdsForParent(action.sourceZone, state);
597
+ const newItem = __spreadProps(__spreadValues({}, item), {
598
+ props: __spreadProps(__spreadValues({}, item.props), {
599
+ id: generateId(item.type)
600
+ })
601
+ });
602
+ const modified = walkAppState(
603
+ state,
604
+ appStore.config,
605
+ (content, zoneCompound) => {
606
+ if (zoneCompound === action.sourceZone) {
607
+ return insert(content, action.sourceIndex + 1, item);
608
+ }
609
+ return content;
610
+ },
611
+ (childItem, path, index) => {
612
+ const zoneCompound = path[path.length - 1];
613
+ const parents = path.map((p) => p.split(":")[0]);
614
+ if (parents.indexOf(newItem.props.id) > -1) {
615
+ return __spreadProps(__spreadValues({}, childItem), {
616
+ props: __spreadProps(__spreadValues({}, childItem.props), {
617
+ id: generateId(childItem.type)
618
+ })
619
+ });
620
+ }
621
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
622
+ return newItem;
623
+ }
624
+ const [sourceZoneParent] = action.sourceZone.split(":");
625
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
626
+ return childItem;
627
+ }
628
+ return null;
629
+ }
630
+ );
631
+ return __spreadProps(__spreadValues({}, modified), {
632
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
633
+ itemSelector: {
634
+ index: action.sourceIndex + 1,
635
+ zone: action.sourceZone
636
+ }
637
+ })
638
+ });
639
+ }
640
+
641
+ // lib/data/remove.ts
642
+ var remove = (list, index) => {
643
+ const result = Array.from(list);
644
+ result.splice(index, 1);
645
+ return result;
646
+ };
647
+
648
+ // reducer/actions/move.ts
649
+ var moveAction = (state, action, appStore) => {
650
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
651
+ return state;
652
+ }
653
+ const item = getItem(
654
+ { zone: action.sourceZone, index: action.sourceIndex },
655
+ state
656
+ );
657
+ if (!item) return state;
658
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
659
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
660
+ return walkAppState(
661
+ state,
662
+ appStore.config,
663
+ (content, zoneCompound) => {
664
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
665
+ return insert(
666
+ remove(content, action.sourceIndex),
667
+ action.destinationIndex,
668
+ item
669
+ );
670
+ } else if (zoneCompound === action.sourceZone) {
671
+ return remove(content, action.sourceIndex);
672
+ } else if (zoneCompound === action.destinationZone) {
673
+ return insert(content, action.destinationIndex, item);
674
+ }
675
+ return content;
676
+ },
677
+ (childItem, path) => {
678
+ const [sourceZoneParent] = action.sourceZone.split(":");
679
+ const [destinationZoneParent] = action.destinationZone.split(":");
680
+ const childId = childItem.props.id;
681
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
682
+ return childItem;
683
+ }
684
+ return null;
685
+ }
686
+ );
687
+ };
688
+
689
+ // reducer/actions/reorder.ts
690
+ var reorderAction = (state, action, appStore) => {
691
+ return moveAction(
692
+ state,
693
+ {
694
+ type: "move",
695
+ sourceIndex: action.sourceIndex,
696
+ sourceZone: action.destinationZone,
697
+ destinationIndex: action.destinationIndex,
698
+ destinationZone: action.destinationZone
699
+ },
700
+ appStore
701
+ );
702
+ };
703
+
704
+ // reducer/actions/remove.ts
705
+ var removeAction = (state, action, appStore) => {
706
+ const item = getItem({ index: action.index, zone: action.zone }, state);
707
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
708
+ (acc, [nodeId, nodeData]) => {
709
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
710
+ if (pathIds.includes(item.props.id)) {
711
+ return [...acc, nodeId];
712
+ }
713
+ return acc;
714
+ },
715
+ [item.props.id]
716
+ );
717
+ const newState = walkAppState(
718
+ state,
719
+ appStore.config,
720
+ (content, zoneCompound) => {
721
+ if (zoneCompound === action.zone) {
722
+ return remove(content, action.index);
723
+ }
724
+ return content;
725
+ }
726
+ );
727
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
728
+ const parentId = zoneCompound.split(":")[0];
729
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
730
+ delete newState.data.zones[zoneCompound];
731
+ }
732
+ });
733
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
734
+ const parentId = zoneCompound.split(":")[0];
735
+ if (nodesToDelete.includes(parentId)) {
736
+ delete newState.indexes.zones[zoneCompound];
737
+ }
738
+ });
739
+ nodesToDelete.forEach((id) => {
740
+ delete newState.indexes.nodes[id];
741
+ });
742
+ return newState;
743
+ };
744
+
745
+ // lib/data/setup-zone.ts
746
+ var setupZone = (data, zoneKey) => {
747
+ if (zoneKey === rootDroppableId) {
748
+ return data;
749
+ }
750
+ const newData = __spreadProps(__spreadValues({}, data), {
751
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
752
+ });
753
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
754
+ return newData;
755
+ };
756
+
757
+ // reducer/actions/register-zone.ts
758
+ var zoneCache = {};
759
+ function registerZoneAction(state, action) {
760
+ if (zoneCache[action.zone]) {
761
+ return __spreadProps(__spreadValues({}, state), {
762
+ data: __spreadProps(__spreadValues({}, state.data), {
763
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
764
+ [action.zone]: zoneCache[action.zone]
765
+ })
766
+ }),
767
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
768
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
769
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
770
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
771
+ type: "dropzone"
772
+ })
773
+ })
774
+ })
775
+ });
776
+ }
777
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
778
+ }
779
+ function unregisterZoneAction(state, action) {
780
+ const _zones = __spreadValues({}, state.data.zones || {});
781
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
782
+ if (_zones[action.zone]) {
783
+ zoneCache[action.zone] = _zones[action.zone];
784
+ delete _zones[action.zone];
785
+ }
786
+ delete zoneIndex[action.zone];
787
+ return __spreadProps(__spreadValues({}, state), {
788
+ data: __spreadProps(__spreadValues({}, state.data), {
789
+ zones: _zones
790
+ }),
791
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
792
+ zones: zoneIndex
793
+ })
794
+ });
795
+ }
796
+
797
+ // reducer/actions/set-data.ts
798
+ var setDataAction = (state, action, appStore) => {
799
+ if (typeof action.data === "object") {
800
+ console.warn(
801
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
802
+ );
803
+ return walkAppState(
804
+ __spreadProps(__spreadValues({}, state), {
805
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
806
+ }),
807
+ appStore.config
808
+ );
809
+ }
810
+ return walkAppState(
811
+ __spreadProps(__spreadValues({}, state), {
812
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
813
+ }),
814
+ appStore.config
815
+ );
816
+ };
817
+
818
+ // reducer/actions/set-ui.ts
819
+ var setUiAction = (state, action) => {
820
+ if (typeof action.ui === "object") {
821
+ return __spreadProps(__spreadValues({}, state), {
822
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
823
+ });
824
+ }
825
+ return __spreadProps(__spreadValues({}, state), {
826
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
827
+ });
828
+ };
829
+
830
+ // lib/data/make-state-public.ts
831
+ var makeStatePublic = (state) => {
832
+ const { data, ui } = state;
833
+ return { data, ui };
834
+ };
835
+
836
+ // reducer/index.ts
837
+ function storeInterceptor(reducer, record, onAction) {
838
+ return (state, action) => {
839
+ const newAppState = reducer(state, action);
840
+ const isValidType = ![
841
+ "registerZone",
842
+ "unregisterZone",
843
+ "setData",
844
+ "setUi",
845
+ "set"
846
+ ].includes(action.type);
847
+ if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
848
+ if (record) record(newAppState);
849
+ }
850
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
851
+ return newAppState;
852
+ };
853
+ }
854
+ function createReducer({
855
+ record,
856
+ onAction,
857
+ appStore
858
+ }) {
859
+ return storeInterceptor(
860
+ (state, action) => {
861
+ if (action.type === "set") {
862
+ return setAction(state, action, appStore);
863
+ }
864
+ if (action.type === "insert") {
865
+ return insertAction(state, action, appStore);
866
+ }
867
+ if (action.type === "replace") {
868
+ return replaceAction(state, action, appStore);
869
+ }
870
+ if (action.type === "replaceRoot") {
871
+ return replaceRootAction(state, action, appStore);
872
+ }
873
+ if (action.type === "duplicate") {
874
+ return duplicateAction(state, action, appStore);
875
+ }
876
+ if (action.type === "reorder") {
877
+ return reorderAction(state, action, appStore);
878
+ }
879
+ if (action.type === "move") {
880
+ return moveAction(state, action, appStore);
881
+ }
882
+ if (action.type === "remove") {
883
+ return removeAction(state, action, appStore);
884
+ }
885
+ if (action.type === "registerZone") {
886
+ return registerZoneAction(state, action);
887
+ }
888
+ if (action.type === "unregisterZone") {
889
+ return unregisterZoneAction(state, action);
890
+ }
891
+ if (action.type === "setData") {
892
+ return setDataAction(state, action, appStore);
893
+ }
894
+ if (action.type === "setUi") {
895
+ return setUiAction(state, action);
896
+ }
897
+ return state;
898
+ },
899
+ record,
900
+ onAction
901
+ );
902
+ }
903
+ // Annotate the CommonJS export names for ESM import in node:
904
+ 0 && (module.exports = {
905
+ createReducer
906
+ });