@measured/puck 0.21.0-canary.e491598a → 0.21.0-canary.eb8ea5ce

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,537 @@
1
+ import {
2
+ __async,
3
+ __spreadProps,
4
+ __spreadValues,
5
+ init_react_import
6
+ } from "./chunk-GQ457KMA.mjs";
7
+
8
+ // lib/data/walk-tree.ts
9
+ init_react_import();
10
+
11
+ // lib/data/map-fields.ts
12
+ init_react_import();
13
+
14
+ // lib/data/default-slots.ts
15
+ init_react_import();
16
+ var defaultSlots = (value, fields) => Object.keys(fields).reduce(
17
+ (acc, fieldName) => fields[fieldName].type === "slot" ? __spreadValues({ [fieldName]: [] }, acc) : acc,
18
+ value
19
+ );
20
+
21
+ // lib/data/map-fields.ts
22
+ var isPromise = (v) => !!v && typeof v.then === "function";
23
+ var flatten = (values) => values.reduce((acc, item) => __spreadValues(__spreadValues({}, acc), item), {});
24
+ var containsPromise = (arr) => arr.some(isPromise);
25
+ var walkField = ({
26
+ value,
27
+ fields,
28
+ mappers,
29
+ propKey = "",
30
+ propPath = "",
31
+ id = "",
32
+ config,
33
+ recurseSlots = false
34
+ }) => {
35
+ var _a, _b, _c;
36
+ const fieldType = (_a = fields[propKey]) == null ? void 0 : _a.type;
37
+ const map = mappers[fieldType];
38
+ if (map && fieldType === "slot") {
39
+ const content = value || [];
40
+ const mappedContent = recurseSlots ? content.map((el) => {
41
+ var _a2;
42
+ const componentConfig = config.components[el.type];
43
+ if (!componentConfig) {
44
+ throw new Error(`Could not find component config for ${el.type}`);
45
+ }
46
+ const fields2 = (_a2 = componentConfig.fields) != null ? _a2 : {};
47
+ return walkField({
48
+ value: __spreadProps(__spreadValues({}, el), { props: defaultSlots(el.props, fields2) }),
49
+ fields: fields2,
50
+ mappers,
51
+ id: el.props.id,
52
+ config,
53
+ recurseSlots
54
+ });
55
+ }) : content;
56
+ if (containsPromise(mappedContent)) {
57
+ return Promise.all(mappedContent);
58
+ }
59
+ return map({
60
+ value: mappedContent,
61
+ parentId: id,
62
+ propName: propPath,
63
+ field: fields[propKey],
64
+ propPath
65
+ });
66
+ } else if (map && fields[propKey]) {
67
+ return map({
68
+ value,
69
+ parentId: id,
70
+ propName: propKey,
71
+ field: fields[propKey],
72
+ propPath
73
+ });
74
+ }
75
+ if (value && typeof value === "object") {
76
+ if (Array.isArray(value)) {
77
+ const arrayFields = ((_b = fields[propKey]) == null ? void 0 : _b.type) === "array" ? fields[propKey].arrayFields : null;
78
+ if (!arrayFields) return value;
79
+ const newValue = value.map(
80
+ (el, idx) => walkField({
81
+ value: el,
82
+ fields: arrayFields,
83
+ mappers,
84
+ propKey,
85
+ propPath: `${propPath}[${idx}]`,
86
+ id,
87
+ config,
88
+ recurseSlots
89
+ })
90
+ );
91
+ if (containsPromise(newValue)) {
92
+ return Promise.all(newValue);
93
+ }
94
+ return newValue;
95
+ } else if ("$$typeof" in value) {
96
+ return value;
97
+ } else {
98
+ const objectFields = ((_c = fields[propKey]) == null ? void 0 : _c.type) === "object" ? fields[propKey].objectFields : fields;
99
+ return walkObject({
100
+ value,
101
+ fields: objectFields,
102
+ mappers,
103
+ id,
104
+ getPropPath: (k) => `${propPath}.${k}`,
105
+ config,
106
+ recurseSlots
107
+ });
108
+ }
109
+ }
110
+ return value;
111
+ };
112
+ var walkObject = ({
113
+ value,
114
+ fields,
115
+ mappers,
116
+ id,
117
+ getPropPath,
118
+ config,
119
+ recurseSlots
120
+ }) => {
121
+ const newProps = Object.entries(value).map(([k, v]) => {
122
+ const opts = {
123
+ value: v,
124
+ fields,
125
+ mappers,
126
+ propKey: k,
127
+ propPath: getPropPath(k),
128
+ id,
129
+ config,
130
+ recurseSlots
131
+ };
132
+ const newValue = walkField(opts);
133
+ if (isPromise(newValue)) {
134
+ return newValue.then((resolvedValue) => ({
135
+ [k]: resolvedValue
136
+ }));
137
+ }
138
+ return {
139
+ [k]: newValue
140
+ };
141
+ }, {});
142
+ if (containsPromise(newProps)) {
143
+ return Promise.all(newProps).then(flatten);
144
+ }
145
+ return flatten(newProps);
146
+ };
147
+ function mapFields(item, mappers, config, recurseSlots = false) {
148
+ var _a, _b, _c, _d, _e;
149
+ const itemType = "type" in item ? item.type : "root";
150
+ const componentConfig = itemType === "root" ? config.root : (_a = config.components) == null ? void 0 : _a[itemType];
151
+ const newProps = walkObject({
152
+ value: defaultSlots((_b = item.props) != null ? _b : {}, (_c = componentConfig == null ? void 0 : componentConfig.fields) != null ? _c : {}),
153
+ fields: (_d = componentConfig == null ? void 0 : componentConfig.fields) != null ? _d : {},
154
+ mappers,
155
+ id: item.props ? (_e = item.props.id) != null ? _e : "root" : "root",
156
+ getPropPath: (k) => k,
157
+ config,
158
+ recurseSlots
159
+ });
160
+ if (isPromise(newProps)) {
161
+ return newProps.then((resolvedProps) => __spreadProps(__spreadValues({}, item), {
162
+ props: resolvedProps
163
+ }));
164
+ }
165
+ return __spreadProps(__spreadValues({}, item), {
166
+ props: newProps
167
+ });
168
+ }
169
+
170
+ // lib/data/walk-tree.ts
171
+ function walkTree(data, config, callbackFn) {
172
+ var _a, _b;
173
+ const walkItem = (item) => {
174
+ return mapFields(
175
+ item,
176
+ {
177
+ slot: ({ value, parentId, propName }) => {
178
+ var _a2;
179
+ const content = value;
180
+ return (_a2 = callbackFn(content, { parentId, propName })) != null ? _a2 : content;
181
+ }
182
+ },
183
+ config,
184
+ true
185
+ );
186
+ };
187
+ if ("props" in data) {
188
+ return walkItem(data);
189
+ }
190
+ const _data = data;
191
+ const zones = (_a = _data.zones) != null ? _a : {};
192
+ const mappedContent = _data.content.map(walkItem);
193
+ return {
194
+ root: walkItem(_data.root),
195
+ content: (_b = callbackFn(mappedContent, {
196
+ parentId: "root",
197
+ propName: "default-zone"
198
+ })) != null ? _b : mappedContent,
199
+ zones: Object.keys(zones).reduce(
200
+ (acc, zoneCompound) => __spreadProps(__spreadValues({}, acc), {
201
+ [zoneCompound]: zones[zoneCompound].map(walkItem)
202
+ }),
203
+ {}
204
+ )
205
+ };
206
+ }
207
+
208
+ // lib/root-droppable-id.ts
209
+ init_react_import();
210
+ var rootAreaId = "root";
211
+ var rootZone = "default-zone";
212
+ var rootDroppableId = `${rootAreaId}:${rootZone}`;
213
+
214
+ // lib/data/flatten-node.ts
215
+ init_react_import();
216
+ import flat from "flat";
217
+
218
+ // lib/data/strip-slots.ts
219
+ init_react_import();
220
+ var stripSlots = (data, config) => {
221
+ return mapFields(data, { slot: () => null }, config);
222
+ };
223
+
224
+ // lib/data/flatten-node.ts
225
+ var { flatten: flatten2, unflatten } = flat;
226
+ var flattenNode = (node, config) => {
227
+ return __spreadProps(__spreadValues({}, node), {
228
+ props: flatten2(stripSlots(node, config).props)
229
+ });
230
+ };
231
+ var expandNode = (node) => {
232
+ const props = unflatten(node.props);
233
+ return __spreadProps(__spreadValues({}, node), {
234
+ props
235
+ });
236
+ };
237
+
238
+ // lib/data/walk-app-state.ts
239
+ init_react_import();
240
+
241
+ // lib/data/for-related-zones.ts
242
+ init_react_import();
243
+
244
+ // lib/get-zone-id.ts
245
+ init_react_import();
246
+ var getZoneId = (zoneCompound) => {
247
+ if (!zoneCompound) {
248
+ return [];
249
+ }
250
+ if (zoneCompound && zoneCompound.indexOf(":") > -1) {
251
+ return zoneCompound.split(":");
252
+ }
253
+ return [rootDroppableId, zoneCompound];
254
+ };
255
+
256
+ // lib/data/for-related-zones.ts
257
+ function forRelatedZones(item, data, cb, path = []) {
258
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
259
+ const [parentId] = getZoneId(zoneCompound);
260
+ if (parentId === item.props.id) {
261
+ cb(path, zoneCompound, content);
262
+ }
263
+ });
264
+ }
265
+
266
+ // lib/data/walk-app-state.ts
267
+ function walkAppState(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
268
+ var _a;
269
+ let newZones = {};
270
+ const newZoneIndex = {};
271
+ const newNodeIndex = {};
272
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
273
+ var _a2;
274
+ const [parentId] = zoneCompound.split(":");
275
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
276
+ const [_2, zone] = zoneCompound.split(":");
277
+ const newZoneCompound = `${newId || parentId}:${zone}`;
278
+ const newContent2 = mappedContent.map(
279
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
280
+ );
281
+ newZoneIndex[newZoneCompound] = {
282
+ contentIds: newContent2.map((item) => item.props.id),
283
+ type: zoneType
284
+ };
285
+ return [newZoneCompound, newContent2];
286
+ };
287
+ const processRelatedZones = (item, newId, initialPath) => {
288
+ forRelatedZones(
289
+ item,
290
+ state.data,
291
+ (relatedPath, relatedZoneCompound, relatedContent) => {
292
+ const [zoneCompound, newContent2] = processContent(
293
+ relatedPath,
294
+ relatedZoneCompound,
295
+ relatedContent,
296
+ "dropzone",
297
+ newId
298
+ );
299
+ newZones[zoneCompound] = newContent2;
300
+ },
301
+ initialPath
302
+ );
303
+ };
304
+ const processItem = (item, path, index) => {
305
+ const mappedItem = mapNodeOrSkip(item, path, index);
306
+ if (!mappedItem) return item;
307
+ const id = mappedItem.props.id;
308
+ const newProps = __spreadProps(__spreadValues({}, mapFields(
309
+ mappedItem,
310
+ {
311
+ slot: ({ value, parentId: parentId2, propPath }) => {
312
+ const content = value;
313
+ const zoneCompound = `${parentId2}:${propPath}`;
314
+ const [_2, newContent2] = processContent(
315
+ path,
316
+ zoneCompound,
317
+ content,
318
+ "slot",
319
+ parentId2
320
+ );
321
+ return newContent2;
322
+ }
323
+ },
324
+ config
325
+ ).props), {
326
+ id
327
+ });
328
+ processRelatedZones(item, id, path);
329
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
330
+ const thisZoneCompound = path[path.length - 1];
331
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
332
+ newNodeIndex[id] = {
333
+ data: newItem,
334
+ flatData: flattenNode(newItem, config),
335
+ path,
336
+ parentId,
337
+ zone
338
+ };
339
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
340
+ if (newProps.id === "root") {
341
+ delete finalData["type"];
342
+ delete finalData.props["id"];
343
+ }
344
+ return finalData;
345
+ };
346
+ const zones = state.data.zones || {};
347
+ const [_, newContent] = processContent(
348
+ [],
349
+ rootDroppableId,
350
+ state.data.content,
351
+ "root"
352
+ );
353
+ const processedContent = newContent;
354
+ const zonesAlreadyProcessed = Object.keys(newZones);
355
+ Object.keys(zones || {}).forEach((zoneCompound) => {
356
+ const [parentId] = zoneCompound.split(":");
357
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
358
+ return;
359
+ }
360
+ const [_2, newContent2] = processContent(
361
+ [rootDroppableId],
362
+ zoneCompound,
363
+ zones[zoneCompound],
364
+ "dropzone",
365
+ parentId
366
+ );
367
+ newZones[zoneCompound] = newContent2;
368
+ }, newZones);
369
+ const processedRoot = processItem(
370
+ {
371
+ type: "root",
372
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
373
+ },
374
+ [],
375
+ -1
376
+ );
377
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
378
+ props: processedRoot.props
379
+ });
380
+ return __spreadProps(__spreadValues({}, state), {
381
+ data: {
382
+ root,
383
+ content: processedContent,
384
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
385
+ },
386
+ indexes: {
387
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
388
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
389
+ }
390
+ });
391
+ }
392
+
393
+ // lib/data/setup-zone.ts
394
+ init_react_import();
395
+ var setupZone = (data, zoneKey) => {
396
+ if (zoneKey === rootDroppableId) {
397
+ return data;
398
+ }
399
+ const newData = __spreadProps(__spreadValues({}, data), {
400
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
401
+ });
402
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
403
+ return newData;
404
+ };
405
+
406
+ // components/ViewportControls/default-viewports.ts
407
+ init_react_import();
408
+ var defaultViewports = [
409
+ { width: 360, height: "auto", icon: "Smartphone", label: "Small" },
410
+ { width: 768, height: "auto", icon: "Tablet", label: "Medium" },
411
+ { width: 1280, height: "auto", icon: "Monitor", label: "Large" }
412
+ ];
413
+
414
+ // lib/resolve-component-data.ts
415
+ init_react_import();
416
+
417
+ // lib/get-changed.ts
418
+ init_react_import();
419
+ import { deepEqual } from "fast-equals";
420
+ var getChanged = (newItem, oldItem) => {
421
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
422
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
423
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
424
+ return __spreadProps(__spreadValues({}, acc), {
425
+ [item]: !deepEqual(oldItemProps[item], newItemProps[item])
426
+ });
427
+ }, {}) : {};
428
+ };
429
+
430
+ // lib/resolve-component-data.ts
431
+ import { deepEqual as deepEqual2 } from "fast-equals";
432
+ var cache = { lastChange: {} };
433
+ var resolveComponentData = (_0, _1, ..._2) => __async(null, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace") {
434
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
435
+ const resolvedItem = __spreadValues({}, item);
436
+ const shouldRunResolver = (configForItem == null ? void 0 : configForItem.resolveData) && item.props;
437
+ const id = "id" in item.props ? item.props.id : "root";
438
+ if (shouldRunResolver) {
439
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
440
+ if (trigger !== "force" && item && deepEqual2(item, oldItem)) {
441
+ return { node: resolved, didChange: false };
442
+ }
443
+ const changed = getChanged(item, oldItem);
444
+ if (onResolveStart) {
445
+ onResolveStart(item);
446
+ }
447
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
448
+ changed,
449
+ lastData: oldItem,
450
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
451
+ trigger
452
+ });
453
+ resolvedItem.props = __spreadValues(__spreadValues({}, item.props), resolvedProps);
454
+ if (Object.keys(readOnly).length) {
455
+ resolvedItem.readOnly = readOnly;
456
+ }
457
+ }
458
+ let itemWithResolvedChildren = yield mapFields(
459
+ resolvedItem,
460
+ {
461
+ slot: (_02) => __async(null, [_02], function* ({ value }) {
462
+ const content = value;
463
+ return yield Promise.all(
464
+ content.map(
465
+ (childItem) => __async(null, null, function* () {
466
+ return (yield resolveComponentData(
467
+ childItem,
468
+ config,
469
+ metadata,
470
+ onResolveStart,
471
+ onResolveEnd,
472
+ trigger
473
+ )).node;
474
+ })
475
+ )
476
+ );
477
+ })
478
+ },
479
+ config
480
+ );
481
+ if (shouldRunResolver && onResolveEnd) {
482
+ onResolveEnd(resolvedItem);
483
+ }
484
+ cache.lastChange[id] = {
485
+ item,
486
+ resolved: itemWithResolvedChildren
487
+ };
488
+ return {
489
+ node: itemWithResolvedChildren,
490
+ didChange: !deepEqual2(item, itemWithResolvedChildren)
491
+ };
492
+ });
493
+
494
+ // store/default-app-state.ts
495
+ init_react_import();
496
+ var defaultAppState = {
497
+ data: { content: [], root: {}, zones: {} },
498
+ ui: {
499
+ leftSideBarVisible: true,
500
+ rightSideBarVisible: true,
501
+ arrayState: {},
502
+ itemSelector: null,
503
+ componentList: {},
504
+ isDragging: false,
505
+ previewMode: "edit",
506
+ viewports: {
507
+ current: {
508
+ width: defaultViewports[0].width,
509
+ height: defaultViewports[0].height || "auto"
510
+ },
511
+ options: [],
512
+ controlsVisible: true
513
+ },
514
+ field: { focus: null }
515
+ },
516
+ indexes: {
517
+ nodes: {},
518
+ zones: {}
519
+ }
520
+ };
521
+
522
+ export {
523
+ rootAreaId,
524
+ rootZone,
525
+ rootDroppableId,
526
+ defaultSlots,
527
+ walkField,
528
+ mapFields,
529
+ expandNode,
530
+ walkAppState,
531
+ walkTree,
532
+ setupZone,
533
+ defaultViewports,
534
+ getChanged,
535
+ resolveComponentData,
536
+ defaultAppState
537
+ };