@measured/puck 0.21.0-canary.bd7b613d → 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,524 @@
1
+ import {
2
+ __spreadProps,
3
+ __spreadValues,
4
+ init_react_import,
5
+ rootDroppableId,
6
+ setupZone,
7
+ walkAppState,
8
+ walkTree
9
+ } from "./chunk-S3RM5GHZ.mjs";
10
+
11
+ // reducer/index.ts
12
+ init_react_import();
13
+
14
+ // reducer/actions/set.ts
15
+ init_react_import();
16
+ var setAction = (state, action, appStore) => {
17
+ if (typeof action.state === "object") {
18
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
19
+ if (action.state.indexes) {
20
+ return newState;
21
+ }
22
+ console.warn(
23
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
24
+ );
25
+ return walkAppState(newState, appStore.config);
26
+ }
27
+ return __spreadValues(__spreadValues({}, state), action.state(state));
28
+ };
29
+
30
+ // reducer/actions/insert.ts
31
+ init_react_import();
32
+
33
+ // lib/data/insert.ts
34
+ init_react_import();
35
+ var insert = (list, index, item) => {
36
+ const result = Array.from(list || []);
37
+ result.splice(index, 0, item);
38
+ return result;
39
+ };
40
+
41
+ // lib/generate-id.ts
42
+ init_react_import();
43
+ import { v4 as uuidv4 } from "uuid";
44
+ var generateId = (type) => type ? `${type}-${uuidv4()}` : uuidv4();
45
+
46
+ // lib/data/get-ids-for-parent.ts
47
+ init_react_import();
48
+ var getIdsForParent = (zoneCompound, state) => {
49
+ const [parentId] = zoneCompound.split(":");
50
+ const node = state.indexes.nodes[parentId];
51
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
52
+ };
53
+
54
+ // lib/data/populate-ids.ts
55
+ init_react_import();
56
+ var populateIds = (data, config, override = false) => {
57
+ const id = generateId(data.type);
58
+ return walkTree(
59
+ __spreadProps(__spreadValues({}, data), {
60
+ props: override ? __spreadProps(__spreadValues({}, data.props), { id }) : __spreadValues({}, data.props)
61
+ }),
62
+ config,
63
+ (contents) => contents.map((item) => {
64
+ const id2 = generateId(item.type);
65
+ return __spreadProps(__spreadValues({}, item), {
66
+ props: override ? __spreadProps(__spreadValues({}, item.props), { id: id2 }) : __spreadValues({ id: id2 }, item.props)
67
+ });
68
+ })
69
+ );
70
+ };
71
+
72
+ // reducer/actions/insert.ts
73
+ function insertAction(state, action, appStore) {
74
+ const id = action.id || generateId(action.componentType);
75
+ const emptyComponentData = populateIds(
76
+ {
77
+ type: action.componentType,
78
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
79
+ id
80
+ })
81
+ },
82
+ appStore.config
83
+ );
84
+ const [parentId] = action.destinationZone.split(":");
85
+ const idsInPath = getIdsForParent(action.destinationZone, state);
86
+ return walkAppState(
87
+ state,
88
+ appStore.config,
89
+ (content, zoneCompound) => {
90
+ if (zoneCompound === action.destinationZone) {
91
+ return insert(
92
+ content || [],
93
+ action.destinationIndex,
94
+ emptyComponentData
95
+ );
96
+ }
97
+ return content;
98
+ },
99
+ (childItem, path) => {
100
+ if (childItem.props.id === id || childItem.props.id === parentId) {
101
+ return childItem;
102
+ } else if (idsInPath.includes(childItem.props.id)) {
103
+ return childItem;
104
+ } else if (path.includes(action.destinationZone)) {
105
+ return childItem;
106
+ }
107
+ return null;
108
+ }
109
+ );
110
+ }
111
+
112
+ // reducer/actions/replace.ts
113
+ init_react_import();
114
+ var replaceAction = (state, action, appStore) => {
115
+ const [parentId] = action.destinationZone.split(":");
116
+ const idsInPath = getIdsForParent(action.destinationZone, state);
117
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
118
+ const idChanged = originalId !== action.data.props.id;
119
+ if (idChanged) {
120
+ throw new Error(
121
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
122
+ );
123
+ }
124
+ const newSlotIds = [];
125
+ const data = walkTree(action.data, appStore.config, (contents, opts) => {
126
+ newSlotIds.push(`${opts.parentId}:${opts.propName}`);
127
+ return contents.map((item) => {
128
+ const id = generateId(item.type);
129
+ return __spreadProps(__spreadValues({}, item), {
130
+ props: __spreadValues({ id }, item.props)
131
+ });
132
+ });
133
+ });
134
+ const stateWithDeepSlotsRemoved = __spreadValues({}, state);
135
+ Object.keys(state.indexes.zones).forEach((zoneCompound) => {
136
+ const id = zoneCompound.split(":")[0];
137
+ if (id === originalId) {
138
+ if (!newSlotIds.includes(zoneCompound)) {
139
+ delete stateWithDeepSlotsRemoved.indexes.zones[zoneCompound];
140
+ }
141
+ }
142
+ });
143
+ return walkAppState(
144
+ stateWithDeepSlotsRemoved,
145
+ appStore.config,
146
+ (content, zoneCompound) => {
147
+ const newContent = [...content];
148
+ if (zoneCompound === action.destinationZone) {
149
+ newContent[action.destinationIndex] = data;
150
+ }
151
+ return newContent;
152
+ },
153
+ (childItem, path) => {
154
+ const pathIds = path.map((p) => p.split(":")[0]);
155
+ if (childItem.props.id === data.props.id) {
156
+ return data;
157
+ } else if (childItem.props.id === parentId) {
158
+ return childItem;
159
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
160
+ return childItem;
161
+ } else if (pathIds.indexOf(data.props.id) > -1) {
162
+ return childItem;
163
+ }
164
+ return null;
165
+ }
166
+ );
167
+ };
168
+
169
+ // reducer/actions/replace-root.ts
170
+ init_react_import();
171
+ var replaceRootAction = (state, action, appStore) => {
172
+ return walkAppState(
173
+ state,
174
+ appStore.config,
175
+ (content) => content,
176
+ (childItem) => {
177
+ if (childItem.props.id === "root") {
178
+ return __spreadProps(__spreadValues({}, childItem), {
179
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
180
+ readOnly: action.root.readOnly
181
+ });
182
+ }
183
+ return childItem;
184
+ }
185
+ );
186
+ };
187
+
188
+ // reducer/actions/duplicate.ts
189
+ init_react_import();
190
+
191
+ // lib/data/get-item.ts
192
+ init_react_import();
193
+ function getItem(selector, state) {
194
+ var _a, _b;
195
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
196
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
197
+ }
198
+
199
+ // reducer/actions/duplicate.ts
200
+ function duplicateAction(state, action, appStore) {
201
+ const item = getItem(
202
+ { index: action.sourceIndex, zone: action.sourceZone },
203
+ state
204
+ );
205
+ const idsInPath = getIdsForParent(action.sourceZone, state);
206
+ const newItem = __spreadProps(__spreadValues({}, item), {
207
+ props: __spreadProps(__spreadValues({}, item.props), {
208
+ id: generateId(item.type)
209
+ })
210
+ });
211
+ const modified = walkAppState(
212
+ state,
213
+ appStore.config,
214
+ (content, zoneCompound) => {
215
+ if (zoneCompound === action.sourceZone) {
216
+ return insert(content, action.sourceIndex + 1, item);
217
+ }
218
+ return content;
219
+ },
220
+ (childItem, path, index) => {
221
+ const zoneCompound = path[path.length - 1];
222
+ const parents = path.map((p) => p.split(":")[0]);
223
+ if (parents.indexOf(newItem.props.id) > -1) {
224
+ return __spreadProps(__spreadValues({}, childItem), {
225
+ props: __spreadProps(__spreadValues({}, childItem.props), {
226
+ id: generateId(childItem.type)
227
+ })
228
+ });
229
+ }
230
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
231
+ return newItem;
232
+ }
233
+ const [sourceZoneParent] = action.sourceZone.split(":");
234
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
235
+ return childItem;
236
+ }
237
+ return null;
238
+ }
239
+ );
240
+ return __spreadProps(__spreadValues({}, modified), {
241
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
242
+ itemSelector: {
243
+ index: action.sourceIndex + 1,
244
+ zone: action.sourceZone
245
+ }
246
+ })
247
+ });
248
+ }
249
+
250
+ // reducer/actions/reorder.ts
251
+ init_react_import();
252
+
253
+ // reducer/actions/move.ts
254
+ init_react_import();
255
+
256
+ // lib/data/remove.ts
257
+ init_react_import();
258
+ var remove = (list, index) => {
259
+ const result = Array.from(list);
260
+ result.splice(index, 1);
261
+ return result;
262
+ };
263
+
264
+ // reducer/actions/move.ts
265
+ var moveAction = (state, action, appStore) => {
266
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
267
+ return state;
268
+ }
269
+ const item = getItem(
270
+ { zone: action.sourceZone, index: action.sourceIndex },
271
+ state
272
+ );
273
+ if (!item) return state;
274
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
275
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
276
+ return walkAppState(
277
+ state,
278
+ appStore.config,
279
+ (content, zoneCompound) => {
280
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
281
+ return insert(
282
+ remove(content, action.sourceIndex),
283
+ action.destinationIndex,
284
+ item
285
+ );
286
+ } else if (zoneCompound === action.sourceZone) {
287
+ return remove(content, action.sourceIndex);
288
+ } else if (zoneCompound === action.destinationZone) {
289
+ return insert(content, action.destinationIndex, item);
290
+ }
291
+ return content;
292
+ },
293
+ (childItem, path) => {
294
+ const [sourceZoneParent] = action.sourceZone.split(":");
295
+ const [destinationZoneParent] = action.destinationZone.split(":");
296
+ const childId = childItem.props.id;
297
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
298
+ return childItem;
299
+ }
300
+ return null;
301
+ }
302
+ );
303
+ };
304
+
305
+ // reducer/actions/reorder.ts
306
+ var reorderAction = (state, action, appStore) => {
307
+ return moveAction(
308
+ state,
309
+ {
310
+ type: "move",
311
+ sourceIndex: action.sourceIndex,
312
+ sourceZone: action.destinationZone,
313
+ destinationIndex: action.destinationIndex,
314
+ destinationZone: action.destinationZone
315
+ },
316
+ appStore
317
+ );
318
+ };
319
+
320
+ // reducer/actions/remove.ts
321
+ init_react_import();
322
+ var removeAction = (state, action, appStore) => {
323
+ const item = getItem({ index: action.index, zone: action.zone }, state);
324
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
325
+ (acc, [nodeId, nodeData]) => {
326
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
327
+ if (pathIds.includes(item.props.id)) {
328
+ return [...acc, nodeId];
329
+ }
330
+ return acc;
331
+ },
332
+ [item.props.id]
333
+ );
334
+ const newState = walkAppState(
335
+ state,
336
+ appStore.config,
337
+ (content, zoneCompound) => {
338
+ if (zoneCompound === action.zone) {
339
+ return remove(content, action.index);
340
+ }
341
+ return content;
342
+ }
343
+ );
344
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
345
+ const parentId = zoneCompound.split(":")[0];
346
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
347
+ delete newState.data.zones[zoneCompound];
348
+ }
349
+ });
350
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
351
+ const parentId = zoneCompound.split(":")[0];
352
+ if (nodesToDelete.includes(parentId)) {
353
+ delete newState.indexes.zones[zoneCompound];
354
+ }
355
+ });
356
+ nodesToDelete.forEach((id) => {
357
+ delete newState.indexes.nodes[id];
358
+ });
359
+ return newState;
360
+ };
361
+
362
+ // reducer/actions/register-zone.ts
363
+ init_react_import();
364
+ var zoneCache = {};
365
+ function registerZoneAction(state, action) {
366
+ if (zoneCache[action.zone]) {
367
+ return __spreadProps(__spreadValues({}, state), {
368
+ data: __spreadProps(__spreadValues({}, state.data), {
369
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
370
+ [action.zone]: zoneCache[action.zone]
371
+ })
372
+ }),
373
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
374
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
375
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
376
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
377
+ type: "dropzone"
378
+ })
379
+ })
380
+ })
381
+ });
382
+ }
383
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
384
+ }
385
+ function unregisterZoneAction(state, action) {
386
+ const _zones = __spreadValues({}, state.data.zones || {});
387
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
388
+ if (_zones[action.zone]) {
389
+ zoneCache[action.zone] = _zones[action.zone];
390
+ delete _zones[action.zone];
391
+ }
392
+ delete zoneIndex[action.zone];
393
+ return __spreadProps(__spreadValues({}, state), {
394
+ data: __spreadProps(__spreadValues({}, state.data), {
395
+ zones: _zones
396
+ }),
397
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
398
+ zones: zoneIndex
399
+ })
400
+ });
401
+ }
402
+
403
+ // reducer/actions/set-data.ts
404
+ init_react_import();
405
+ var setDataAction = (state, action, appStore) => {
406
+ if (typeof action.data === "object") {
407
+ console.warn(
408
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
409
+ );
410
+ return walkAppState(
411
+ __spreadProps(__spreadValues({}, state), {
412
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
413
+ }),
414
+ appStore.config
415
+ );
416
+ }
417
+ return walkAppState(
418
+ __spreadProps(__spreadValues({}, state), {
419
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
420
+ }),
421
+ appStore.config
422
+ );
423
+ };
424
+
425
+ // reducer/actions/set-ui.ts
426
+ init_react_import();
427
+ var setUiAction = (state, action) => {
428
+ if (typeof action.ui === "object") {
429
+ return __spreadProps(__spreadValues({}, state), {
430
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
431
+ });
432
+ }
433
+ return __spreadProps(__spreadValues({}, state), {
434
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
435
+ });
436
+ };
437
+
438
+ // lib/data/make-state-public.ts
439
+ init_react_import();
440
+ var makeStatePublic = (state) => {
441
+ const { data, ui } = state;
442
+ return { data, ui };
443
+ };
444
+
445
+ // reducer/actions.tsx
446
+ init_react_import();
447
+
448
+ // reducer/index.ts
449
+ function storeInterceptor(reducer, record, onAction) {
450
+ return (state, action) => {
451
+ const newAppState = reducer(state, action);
452
+ const isValidType = ![
453
+ "registerZone",
454
+ "unregisterZone",
455
+ "setData",
456
+ "setUi",
457
+ "set"
458
+ ].includes(action.type);
459
+ if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
460
+ if (record) record(newAppState);
461
+ }
462
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
463
+ return newAppState;
464
+ };
465
+ }
466
+ function createReducer({
467
+ record,
468
+ onAction,
469
+ appStore
470
+ }) {
471
+ return storeInterceptor(
472
+ (state, action) => {
473
+ if (action.type === "set") {
474
+ return setAction(state, action, appStore);
475
+ }
476
+ if (action.type === "insert") {
477
+ return insertAction(state, action, appStore);
478
+ }
479
+ if (action.type === "replace") {
480
+ return replaceAction(state, action, appStore);
481
+ }
482
+ if (action.type === "replaceRoot") {
483
+ return replaceRootAction(state, action, appStore);
484
+ }
485
+ if (action.type === "duplicate") {
486
+ return duplicateAction(state, action, appStore);
487
+ }
488
+ if (action.type === "reorder") {
489
+ return reorderAction(state, action, appStore);
490
+ }
491
+ if (action.type === "move") {
492
+ return moveAction(state, action, appStore);
493
+ }
494
+ if (action.type === "remove") {
495
+ return removeAction(state, action, appStore);
496
+ }
497
+ if (action.type === "registerZone") {
498
+ return registerZoneAction(state, action);
499
+ }
500
+ if (action.type === "unregisterZone") {
501
+ return unregisterZoneAction(state, action);
502
+ }
503
+ if (action.type === "setData") {
504
+ return setDataAction(state, action, appStore);
505
+ }
506
+ if (action.type === "setUi") {
507
+ return setUiAction(state, action);
508
+ }
509
+ return state;
510
+ },
511
+ record,
512
+ onAction
513
+ );
514
+ }
515
+
516
+ export {
517
+ insert,
518
+ generateId,
519
+ populateIds,
520
+ insertAction,
521
+ getItem,
522
+ makeStatePublic,
523
+ createReducer
524
+ };