@measured/puck 0.19.0-canary.af4f756 → 0.19.0-canary.b22833ee

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/rsc.js CHANGED
@@ -68,7 +68,10 @@ var __async = (__this, __arguments, generator) => {
68
68
  var rsc_exports = {};
69
69
  __export(rsc_exports, {
70
70
  Render: () => Render,
71
- resolveAllData: () => resolveAllData
71
+ migrate: () => migrate,
72
+ resolveAllData: () => resolveAllData,
73
+ transformProps: () => transformProps,
74
+ walkTree: () => walkTree
72
75
  });
73
76
  module.exports = __toCommonJS(rsc_exports);
74
77
 
@@ -80,7 +83,7 @@ var rootAreaId = "root";
80
83
  var rootZone = "default-zone";
81
84
  var rootDroppableId = `${rootAreaId}:${rootZone}`;
82
85
 
83
- // lib/setup-zone.ts
86
+ // lib/data/setup-zone.ts
84
87
  var setupZone = (data, zoneKey) => {
85
88
  if (zoneKey === rootDroppableId) {
86
89
  return data;
@@ -92,8 +95,219 @@ var setupZone = (data, zoneKey) => {
92
95
  return newData;
93
96
  };
94
97
 
95
- // components/ServerRender/index.tsx
98
+ // lib/use-slots.tsx
99
+ var import_react2 = require("react");
100
+
101
+ // lib/data/default-slots.ts
102
+ var defaultSlots = (value, fields) => Object.keys(fields).reduce(
103
+ (acc, fieldName) => fields[fieldName].type === "slot" ? __spreadValues({ [fieldName]: [] }, acc) : acc,
104
+ value
105
+ );
106
+
107
+ // lib/data/map-slots.ts
108
+ var isPromise = (v) => !!v && typeof v.then === "function";
109
+ var flatten = (values) => values.reduce((acc, item) => __spreadValues(__spreadValues({}, acc), item), {});
110
+ var containsPromise = (arr) => arr.some(isPromise);
111
+ var walkField = ({
112
+ value,
113
+ fields,
114
+ map,
115
+ propKey = "",
116
+ propPath = "",
117
+ id = "",
118
+ config,
119
+ recurseSlots = false
120
+ }) => {
121
+ var _a, _b, _c;
122
+ if (((_a = fields[propKey]) == null ? void 0 : _a.type) === "slot") {
123
+ const content = value || [];
124
+ const mappedContent = recurseSlots ? content.map((el) => {
125
+ var _a2;
126
+ const componentConfig = config.components[el.type];
127
+ if (!componentConfig) {
128
+ throw new Error(`Could not find component config for ${el.type}`);
129
+ }
130
+ const fields2 = (_a2 = componentConfig.fields) != null ? _a2 : {};
131
+ return walkField({
132
+ value: __spreadProps(__spreadValues({}, el), { props: defaultSlots(el.props, fields2) }),
133
+ fields: fields2,
134
+ map,
135
+ id: el.props.id,
136
+ config,
137
+ recurseSlots
138
+ });
139
+ }) : content;
140
+ if (containsPromise(mappedContent)) {
141
+ return Promise.all(mappedContent);
142
+ }
143
+ return map(mappedContent, id, propPath, fields[propKey], propPath);
144
+ }
145
+ if (value && typeof value === "object") {
146
+ if (Array.isArray(value)) {
147
+ const arrayFields = ((_b = fields[propKey]) == null ? void 0 : _b.type) === "array" ? fields[propKey].arrayFields : null;
148
+ if (!arrayFields) return value;
149
+ const newValue = value.map(
150
+ (el, idx) => walkField({
151
+ value: el,
152
+ fields: arrayFields,
153
+ map,
154
+ propKey,
155
+ propPath: `${propPath}[${idx}]`,
156
+ id,
157
+ config,
158
+ recurseSlots
159
+ })
160
+ );
161
+ if (containsPromise(newValue)) {
162
+ return Promise.all(newValue);
163
+ }
164
+ return newValue;
165
+ } else if ("$$typeof" in value) {
166
+ return value;
167
+ } else {
168
+ const objectFields = ((_c = fields[propKey]) == null ? void 0 : _c.type) === "object" ? fields[propKey].objectFields : fields;
169
+ return walkObject({
170
+ value,
171
+ fields: objectFields,
172
+ map,
173
+ id,
174
+ getPropPath: (k) => `${propPath}.${k}`,
175
+ config,
176
+ recurseSlots
177
+ });
178
+ }
179
+ }
180
+ return value;
181
+ };
182
+ var walkObject = ({
183
+ value,
184
+ fields,
185
+ map,
186
+ id,
187
+ getPropPath,
188
+ config,
189
+ recurseSlots
190
+ }) => {
191
+ const newProps = Object.entries(value).map(([k, v]) => {
192
+ const opts = {
193
+ value: v,
194
+ fields,
195
+ map,
196
+ propKey: k,
197
+ propPath: getPropPath(k),
198
+ id,
199
+ config,
200
+ recurseSlots
201
+ };
202
+ const newValue = walkField(opts);
203
+ if (isPromise(newValue)) {
204
+ return newValue.then((resolvedValue) => ({
205
+ [k]: resolvedValue
206
+ }));
207
+ }
208
+ return {
209
+ [k]: newValue
210
+ };
211
+ }, {});
212
+ if (containsPromise(newProps)) {
213
+ return Promise.all(newProps).then(flatten);
214
+ }
215
+ return flatten(newProps);
216
+ };
217
+ function mapSlots(item, map, config, recurseSlots = false) {
218
+ var _a, _b, _c, _d, _e;
219
+ const itemType = "type" in item ? item.type : "root";
220
+ const componentConfig = itemType === "root" ? config.root : (_a = config.components) == null ? void 0 : _a[itemType];
221
+ const newProps = walkObject({
222
+ value: defaultSlots((_b = item.props) != null ? _b : {}, (_c = componentConfig == null ? void 0 : componentConfig.fields) != null ? _c : {}),
223
+ fields: (_d = componentConfig == null ? void 0 : componentConfig.fields) != null ? _d : {},
224
+ map,
225
+ id: item.props ? (_e = item.props.id) != null ? _e : "root" : "root",
226
+ getPropPath: (k) => k,
227
+ config,
228
+ recurseSlots
229
+ });
230
+ if (isPromise(newProps)) {
231
+ return newProps.then((resolvedProps) => __spreadProps(__spreadValues({}, item), {
232
+ props: resolvedProps
233
+ }));
234
+ }
235
+ return __spreadProps(__spreadValues({}, item), {
236
+ props: newProps
237
+ });
238
+ }
239
+
240
+ // lib/use-slots.tsx
241
+ function useSlots(config, item, renderSlotEdit, renderSlotRender = renderSlotEdit, readOnly, forceReadOnly) {
242
+ const slotProps = (0, import_react2.useMemo)(() => {
243
+ const mapped = mapSlots(
244
+ item,
245
+ (content, _parentId, propName, field, propPath) => {
246
+ const wildcardPath = propPath.replace(/\[\d+\]/g, "[*]");
247
+ const isReadOnly = (readOnly == null ? void 0 : readOnly[propPath]) || (readOnly == null ? void 0 : readOnly[wildcardPath]) || forceReadOnly;
248
+ const render = isReadOnly ? renderSlotRender : renderSlotEdit;
249
+ const Slot = (dzProps) => render(__spreadProps(__spreadValues({
250
+ allow: (field == null ? void 0 : field.type) === "slot" ? field.allow : [],
251
+ disallow: (field == null ? void 0 : field.type) === "slot" ? field.disallow : []
252
+ }, dzProps), {
253
+ zone: propName,
254
+ content
255
+ }));
256
+ return Slot;
257
+ },
258
+ config
259
+ ).props;
260
+ return mapped;
261
+ }, [config, item, readOnly, forceReadOnly]);
262
+ const mergedProps = (0, import_react2.useMemo)(
263
+ () => __spreadValues(__spreadValues({}, item.props), slotProps),
264
+ [item.props, slotProps]
265
+ );
266
+ return mergedProps;
267
+ }
268
+
269
+ // components/SlotRender/server.tsx
270
+ var import_react3 = require("react");
96
271
  var import_jsx_runtime = require("react/jsx-runtime");
272
+ var SlotRenderPure = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotRender, __spreadValues({}, props));
273
+ var Item = ({
274
+ config,
275
+ item,
276
+ metadata
277
+ }) => {
278
+ const Component = config.components[item.type];
279
+ const props = useSlots(config, item, (slotProps) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SlotRenderPure, __spreadProps(__spreadValues({}, slotProps), { config, metadata })));
280
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
281
+ Component.render,
282
+ __spreadProps(__spreadValues({}, props), {
283
+ puck: __spreadProps(__spreadValues({}, props.puck), {
284
+ renderDropZone: DropZoneRender,
285
+ metadata: metadata || {}
286
+ })
287
+ })
288
+ );
289
+ };
290
+ var SlotRender = (0, import_react3.forwardRef)(
291
+ function SlotRenderInternal({ className, style, content, config, metadata }, ref) {
292
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className, style, ref, children: content.map((item) => {
293
+ if (!config.components[item.type]) {
294
+ return null;
295
+ }
296
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
297
+ Item,
298
+ {
299
+ config,
300
+ item,
301
+ metadata
302
+ },
303
+ item.props.id
304
+ );
305
+ }) });
306
+ }
307
+ );
308
+
309
+ // components/ServerRender/index.tsx
310
+ var import_jsx_runtime2 = require("react/jsx-runtime");
97
311
  function DropZoneRender({
98
312
  zone,
99
313
  data,
@@ -110,28 +324,29 @@ function DropZoneRender({
110
324
  zoneCompound = `${areaId}:${zone}`;
111
325
  content = setupZone(data, zoneCompound).zones[zoneCompound];
112
326
  }
113
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: content.map((item) => {
327
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: content.map((item) => {
114
328
  const Component = config.components[item.type];
115
- if (Component) {
116
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
117
- Component.render,
118
- __spreadProps(__spreadValues({}, item.props), {
119
- puck: {
120
- renderDropZone: ({ zone: zone2 }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
121
- DropZoneRender,
122
- {
123
- zone: zone2,
124
- data,
125
- areaId: item.props.id,
126
- config,
127
- metadata
128
- }
129
- ),
329
+ const props = __spreadProps(__spreadValues({}, item.props), {
330
+ puck: {
331
+ renderDropZone: ({ zone: zone2 }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
332
+ DropZoneRender,
333
+ {
334
+ zone: zone2,
335
+ data,
336
+ areaId: item.props.id,
337
+ config,
130
338
  metadata
131
339
  }
132
- }),
133
- item.props.id
134
- );
340
+ ),
341
+ metadata,
342
+ dragRef: null,
343
+ isEditing: false
344
+ }
345
+ });
346
+ const renderItem = __spreadProps(__spreadValues({}, item), { props });
347
+ const propsWithSlots = useSlots(config, renderItem, (props2) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SlotRenderPure, __spreadProps(__spreadValues({}, props2), { config, metadata })));
348
+ if (Component) {
349
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component.render, __spreadValues({}, propsWithSlots), renderItem.props.id);
135
350
  }
136
351
  return null;
137
352
  }) });
@@ -139,44 +354,43 @@ function DropZoneRender({
139
354
  function Render({
140
355
  config,
141
356
  data,
142
- metadata
357
+ metadata = {}
143
358
  }) {
144
359
  var _a;
360
+ const rootProps = "props" in data.root ? data.root.props : data.root;
361
+ const title = rootProps.title || "";
362
+ const props = __spreadProps(__spreadValues({}, rootProps), {
363
+ puck: {
364
+ renderDropZone: ({ zone }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
365
+ DropZoneRender,
366
+ {
367
+ zone,
368
+ data,
369
+ config,
370
+ metadata
371
+ }
372
+ ),
373
+ isEditing: false,
374
+ dragRef: null,
375
+ metadata
376
+ },
377
+ title,
378
+ editMode: false,
379
+ id: "puck-root"
380
+ });
381
+ const propsWithSlots = useSlots(config, { type: "root", props }, (props2) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SlotRenderPure, __spreadProps(__spreadValues({}, props2), { config, metadata })));
145
382
  if ((_a = config.root) == null ? void 0 : _a.render) {
146
- const rootProps = data.root.props || data.root;
147
- const title = rootProps.title || "";
148
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
149
- config.root.render,
150
- __spreadProps(__spreadValues({}, rootProps), {
151
- puck: {
152
- renderDropZone: ({ zone }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
153
- DropZoneRender,
154
- {
155
- zone,
156
- data,
157
- config,
158
- metadata
159
- }
160
- ),
161
- isEditing: false,
162
- dragRef: null
163
- },
164
- title,
165
- editMode: false,
166
- id: "puck-root",
167
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
168
- DropZoneRender,
169
- {
170
- config,
171
- data,
172
- zone: rootZone,
173
- metadata
174
- }
175
- )
176
- })
177
- );
383
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(config.root.render, __spreadProps(__spreadValues({}, propsWithSlots), { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
384
+ DropZoneRender,
385
+ {
386
+ config,
387
+ data,
388
+ zone: rootZone,
389
+ metadata
390
+ }
391
+ ) }));
178
392
  }
179
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
393
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
180
394
  DropZoneRender,
181
395
  {
182
396
  config,
@@ -188,37 +402,29 @@ function Render({
188
402
  }
189
403
 
190
404
  // lib/get-changed.ts
405
+ var import_fast_deep_equal = __toESM(require("fast-deep-equal"));
191
406
  var getChanged = (newItem, oldItem) => {
192
407
  return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
193
408
  const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
194
409
  const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
195
410
  return __spreadProps(__spreadValues({}, acc), {
196
- [item]: oldItemProps[item] !== newItemProps[item]
411
+ [item]: !(0, import_fast_deep_equal.default)(oldItemProps[item], newItemProps[item])
197
412
  });
198
413
  }, {}) : {};
199
414
  };
200
415
 
201
416
  // lib/resolve-component-data.ts
417
+ var import_fast_deep_equal2 = __toESM(require("fast-deep-equal"));
202
418
  var cache = { lastChange: {} };
203
- var resolveAllComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (content, config, metadata = {}, onResolveStart, onResolveEnd) {
204
- return yield Promise.all(
205
- content.map((item) => __async(void 0, null, function* () {
206
- return yield resolveComponentData(
207
- item,
208
- config,
209
- metadata,
210
- onResolveStart,
211
- onResolveEnd
212
- );
213
- }))
214
- );
215
- });
216
- var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd) {
217
- const configForItem = config.components[item.type];
218
- if (configForItem.resolveData) {
219
- const { item: oldItem = null, resolved = {} } = cache.lastChange[item.props.id] || {};
220
- if (item && item === oldItem) {
221
- return resolved;
419
+ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace") {
420
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
421
+ const resolvedItem = __spreadValues({}, item);
422
+ const shouldRunResolver = (configForItem == null ? void 0 : configForItem.resolveData) && item.props;
423
+ const id = "id" in item.props ? item.props.id : "root";
424
+ if (shouldRunResolver) {
425
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
426
+ if (item && (0, import_fast_deep_equal2.default)(item, oldItem)) {
427
+ return { node: resolved, didChange: false };
222
428
  }
223
429
  const changed = getChanged(item, oldItem);
224
430
  if (onResolveStart) {
@@ -227,97 +433,453 @@ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], f
227
433
  const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
228
434
  changed,
229
435
  lastData: oldItem,
230
- metadata
231
- });
232
- const resolvedItem = __spreadProps(__spreadValues({}, item), {
233
- props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
436
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
437
+ trigger
234
438
  });
439
+ resolvedItem.props = __spreadValues(__spreadValues({}, item.props), resolvedProps);
235
440
  if (Object.keys(readOnly).length) {
236
441
  resolvedItem.readOnly = readOnly;
237
442
  }
238
- cache.lastChange[item.props.id] = {
239
- item,
240
- resolved: resolvedItem
241
- };
242
- if (onResolveEnd) {
243
- onResolveEnd(resolvedItem);
244
- }
245
- return resolvedItem;
246
443
  }
247
- return item;
444
+ let itemWithResolvedChildren = yield mapSlots(
445
+ resolvedItem,
446
+ (content) => __async(void 0, null, function* () {
447
+ return yield Promise.all(
448
+ content.map(
449
+ (childItem) => __async(void 0, null, function* () {
450
+ return (yield resolveComponentData(
451
+ childItem,
452
+ config,
453
+ metadata,
454
+ onResolveStart,
455
+ onResolveEnd,
456
+ trigger
457
+ )).node;
458
+ })
459
+ )
460
+ );
461
+ }),
462
+ config
463
+ );
464
+ if (shouldRunResolver && onResolveEnd) {
465
+ onResolveEnd(resolvedItem);
466
+ }
467
+ cache.lastChange[id] = {
468
+ item,
469
+ resolved: itemWithResolvedChildren
470
+ };
471
+ return {
472
+ node: itemWithResolvedChildren,
473
+ didChange: !(0, import_fast_deep_equal2.default)(item, itemWithResolvedChildren)
474
+ };
248
475
  });
249
476
 
250
- // lib/resolve-root-data.ts
251
- var cache2 = {};
252
- function resolveRootData(data, config, metadata) {
253
- return __async(this, null, function* () {
254
- var _a, _b, _c, _d, _e;
255
- if (((_a = config.root) == null ? void 0 : _a.resolveData) && data.root.props) {
256
- if (((_b = cache2.lastChange) == null ? void 0 : _b.original) === data.root) {
257
- return cache2.lastChange.resolved;
258
- }
259
- const changed = getChanged(data.root, (_c = cache2.lastChange) == null ? void 0 : _c.original);
260
- const rootWithProps = data.root;
261
- const resolvedRoot = yield (_e = config.root) == null ? void 0 : _e.resolveData(rootWithProps, {
262
- changed,
263
- lastData: ((_d = cache2.lastChange) == null ? void 0 : _d.original) || {},
264
- metadata: metadata || {}
265
- });
266
- cache2.lastChange = {
267
- original: data.root,
268
- resolved: resolvedRoot
269
- };
270
- return __spreadProps(__spreadValues(__spreadValues({}, data.root), resolvedRoot), {
271
- props: __spreadValues(__spreadValues({}, data.root.props), resolvedRoot.props)
272
- });
273
- }
274
- return data.root;
275
- });
276
- }
277
-
278
- // lib/default-data.ts
477
+ // lib/data/default-data.ts
279
478
  var defaultData = (data) => __spreadProps(__spreadValues({}, data), {
280
479
  root: data.root || {},
281
480
  content: data.content || []
282
481
  });
283
482
 
483
+ // lib/data/to-component.ts
484
+ var toComponent = (item) => {
485
+ return "type" in item ? item : __spreadProps(__spreadValues({}, item), {
486
+ props: __spreadProps(__spreadValues({}, item.props), { id: "root" }),
487
+ type: "root"
488
+ });
489
+ };
490
+
284
491
  // lib/resolve-all-data.ts
285
492
  function resolveAllData(_0, _1) {
286
493
  return __async(this, arguments, function* (data, config, metadata = {}, onResolveStart, onResolveEnd) {
494
+ var _a;
287
495
  const defaultedData = defaultData(data);
288
- const dynamicRoot = yield resolveRootData(
289
- defaultedData,
290
- config,
291
- metadata
292
- );
293
- const { zones = {} } = data;
294
- const zoneKeys = Object.keys(zones);
295
- const resolvedZones = {};
296
- for (let i = 0; i < zoneKeys.length; i++) {
297
- const zoneKey = zoneKeys[i];
298
- resolvedZones[zoneKey] = yield resolveAllComponentData(
299
- zones[zoneKey],
496
+ const resolveNode = (_node) => __async(this, null, function* () {
497
+ const node = toComponent(_node);
498
+ onResolveStart == null ? void 0 : onResolveStart(node);
499
+ const resolved = (yield resolveComponentData(
500
+ node,
300
501
  config,
301
502
  metadata,
302
- onResolveStart,
303
- onResolveEnd
503
+ () => {
504
+ },
505
+ () => {
506
+ },
507
+ "force"
508
+ )).node;
509
+ const resolvedDeep = yield mapSlots(
510
+ resolved,
511
+ processContent,
512
+ config
304
513
  );
514
+ onResolveEnd == null ? void 0 : onResolveEnd(toComponent(resolvedDeep));
515
+ return resolvedDeep;
516
+ });
517
+ const processContent = (content) => __async(this, null, function* () {
518
+ return Promise.all(content.map(resolveNode));
519
+ });
520
+ const processZones = () => __async(this, null, function* () {
521
+ var _a2;
522
+ const zones = (_a2 = data.zones) != null ? _a2 : {};
523
+ Object.entries(zones).forEach((_02) => __async(this, [_02], function* ([zoneKey, content]) {
524
+ zones[zoneKey] = yield Promise.all(content.map(resolveNode));
525
+ }));
526
+ return zones;
527
+ });
528
+ const dynamic = {
529
+ root: yield resolveNode(defaultedData.root),
530
+ content: yield processContent(defaultedData.content),
531
+ zones: yield processZones()
532
+ };
533
+ Object.keys((_a = defaultedData.zones) != null ? _a : {}).forEach((zoneKey) => __async(this, null, function* () {
534
+ const content = defaultedData.zones[zoneKey];
535
+ dynamic.zones[zoneKey] = yield processContent(content);
536
+ }), {});
537
+ return dynamic;
538
+ });
539
+ }
540
+
541
+ // lib/data/walk-tree.ts
542
+ function walkTree(data, config, callbackFn) {
543
+ var _a, _b;
544
+ const walkItem = (item) => {
545
+ return mapSlots(
546
+ item,
547
+ (content, parentId, propName) => {
548
+ var _a2;
549
+ return (_a2 = callbackFn(content, { parentId, propName })) != null ? _a2 : content;
550
+ },
551
+ config,
552
+ true
553
+ );
554
+ };
555
+ if ("props" in data) {
556
+ return walkItem(data);
557
+ }
558
+ const _data = data;
559
+ const zones = (_a = _data.zones) != null ? _a : {};
560
+ const mappedContent = _data.content.map(walkItem);
561
+ return {
562
+ root: walkItem(_data.root),
563
+ content: (_b = callbackFn(mappedContent, {
564
+ parentId: "root",
565
+ propName: "default-zone"
566
+ })) != null ? _b : mappedContent,
567
+ zones: Object.keys(zones).reduce(
568
+ (acc, zoneCompound) => __spreadProps(__spreadValues({}, acc), {
569
+ [zoneCompound]: zones[zoneCompound].map(walkItem)
570
+ }),
571
+ {}
572
+ )
573
+ };
574
+ }
575
+
576
+ // lib/transform-props.ts
577
+ function transformProps(data, propTransforms, config = { components: {} }) {
578
+ const mapItem = (item) => {
579
+ if (propTransforms[item.type]) {
580
+ return __spreadProps(__spreadValues({}, item), {
581
+ props: __spreadValues({
582
+ id: item.props.id
583
+ }, propTransforms[item.type](item.props))
584
+ });
305
585
  }
306
- return __spreadProps(__spreadValues({}, defaultedData), {
307
- root: dynamicRoot,
308
- content: yield resolveAllComponentData(
309
- defaultedData.content,
310
- config,
311
- metadata,
312
- onResolveStart,
313
- onResolveEnd
314
- ),
315
- zones: resolvedZones
586
+ return item;
587
+ };
588
+ const defaultedData = defaultData(data);
589
+ const rootProps = defaultedData.root.props || defaultedData.root;
590
+ let newRoot = __spreadValues({}, defaultedData.root);
591
+ if (propTransforms["root"]) {
592
+ newRoot.props = propTransforms["root"](rootProps);
593
+ }
594
+ const dataWithUpdatedRoot = __spreadProps(__spreadValues({}, defaultedData), { root: newRoot });
595
+ const updatedData = walkTree(
596
+ dataWithUpdatedRoot,
597
+ config,
598
+ (content) => content.map(mapItem)
599
+ );
600
+ if (!defaultedData.root.props) {
601
+ updatedData.root = updatedData.root.props;
602
+ }
603
+ return updatedData;
604
+ }
605
+
606
+ // components/ViewportControls/default-viewports.ts
607
+ var defaultViewports = [
608
+ { width: 360, height: "auto", icon: "Smartphone", label: "Small" },
609
+ { width: 768, height: "auto", icon: "Tablet", label: "Medium" },
610
+ { width: 1280, height: "auto", icon: "Monitor", label: "Large" }
611
+ ];
612
+
613
+ // store/default-app-state.ts
614
+ var defaultAppState = {
615
+ data: { content: [], root: {}, zones: {} },
616
+ ui: {
617
+ leftSideBarVisible: true,
618
+ rightSideBarVisible: true,
619
+ arrayState: {},
620
+ itemSelector: null,
621
+ componentList: {},
622
+ isDragging: false,
623
+ previewMode: "edit",
624
+ viewports: {
625
+ current: {
626
+ width: defaultViewports[0].width,
627
+ height: defaultViewports[0].height || "auto"
628
+ },
629
+ options: [],
630
+ controlsVisible: true
631
+ },
632
+ field: { focus: null }
633
+ },
634
+ indexes: {
635
+ nodes: {},
636
+ zones: {}
637
+ }
638
+ };
639
+
640
+ // lib/get-zone-id.ts
641
+ var getZoneId = (zoneCompound) => {
642
+ if (!zoneCompound) {
643
+ return [];
644
+ }
645
+ if (zoneCompound && zoneCompound.indexOf(":") > -1) {
646
+ return zoneCompound.split(":");
647
+ }
648
+ return [rootDroppableId, zoneCompound];
649
+ };
650
+
651
+ // lib/data/for-related-zones.ts
652
+ function forRelatedZones(item, data, cb, path = []) {
653
+ Object.entries(data.zones || {}).forEach(([zoneCompound, content]) => {
654
+ const [parentId] = getZoneId(zoneCompound);
655
+ if (parentId === item.props.id) {
656
+ cb(path, zoneCompound, content);
657
+ }
658
+ });
659
+ }
660
+
661
+ // lib/data/flatten-node.ts
662
+ var import_flat = require("flat");
663
+
664
+ // lib/data/strip-slots.ts
665
+ var stripSlots = (data, config) => {
666
+ return mapSlots(data, () => null, config);
667
+ };
668
+
669
+ // lib/data/flatten-node.ts
670
+ var flattenNode = (node, config) => {
671
+ return __spreadProps(__spreadValues({}, node), {
672
+ props: (0, import_flat.flatten)(stripSlots(node, config).props)
673
+ });
674
+ };
675
+
676
+ // lib/data/walk-app-state.ts
677
+ function walkAppState(state, config, mapContent = (content) => content, mapNodeOrSkip = (item) => item) {
678
+ var _a;
679
+ let newZones = {};
680
+ const newZoneIndex = {};
681
+ const newNodeIndex = {};
682
+ const processContent = (path, zoneCompound, content, zoneType, newId) => {
683
+ var _a2;
684
+ const [parentId] = zoneCompound.split(":");
685
+ const mappedContent = ((_a2 = mapContent(content, zoneCompound, zoneType)) != null ? _a2 : content) || [];
686
+ const [_2, zone] = zoneCompound.split(":");
687
+ const newZoneCompound = `${newId || parentId}:${zone}`;
688
+ const newContent2 = mappedContent.map(
689
+ (zoneChild, index) => processItem(zoneChild, [...path, newZoneCompound], index)
690
+ );
691
+ newZoneIndex[newZoneCompound] = {
692
+ contentIds: newContent2.map((item) => item.props.id),
693
+ type: zoneType
694
+ };
695
+ return [newZoneCompound, newContent2];
696
+ };
697
+ const processRelatedZones = (item, newId, initialPath) => {
698
+ forRelatedZones(
699
+ item,
700
+ state.data,
701
+ (relatedPath, relatedZoneCompound, relatedContent) => {
702
+ const [zoneCompound, newContent2] = processContent(
703
+ relatedPath,
704
+ relatedZoneCompound,
705
+ relatedContent,
706
+ "dropzone",
707
+ newId
708
+ );
709
+ newZones[zoneCompound] = newContent2;
710
+ },
711
+ initialPath
712
+ );
713
+ };
714
+ const processItem = (item, path, index) => {
715
+ const mappedItem = mapNodeOrSkip(item, path, index);
716
+ if (!mappedItem) return item;
717
+ const id = mappedItem.props.id;
718
+ const newProps = __spreadProps(__spreadValues({}, mapSlots(
719
+ mappedItem,
720
+ (content, parentId2, slotId) => {
721
+ const zoneCompound = `${parentId2}:${slotId}`;
722
+ const [_2, newContent2] = processContent(
723
+ path,
724
+ zoneCompound,
725
+ content,
726
+ "slot",
727
+ parentId2
728
+ );
729
+ return newContent2;
730
+ },
731
+ config
732
+ ).props), {
733
+ id
316
734
  });
735
+ processRelatedZones(item, id, path);
736
+ const newItem = __spreadProps(__spreadValues({}, item), { props: newProps });
737
+ const thisZoneCompound = path[path.length - 1];
738
+ const [parentId, zone] = thisZoneCompound ? thisZoneCompound.split(":") : [null, ""];
739
+ newNodeIndex[id] = {
740
+ data: newItem,
741
+ flatData: flattenNode(newItem, config),
742
+ path,
743
+ parentId,
744
+ zone
745
+ };
746
+ const finalData = __spreadProps(__spreadValues({}, newItem), { props: __spreadValues({}, newItem.props) });
747
+ if (newProps.id === "root") {
748
+ delete finalData["type"];
749
+ delete finalData.props["id"];
750
+ }
751
+ return finalData;
752
+ };
753
+ const zones = state.data.zones || {};
754
+ const [_, newContent] = processContent(
755
+ [],
756
+ rootDroppableId,
757
+ state.data.content,
758
+ "root"
759
+ );
760
+ const processedContent = newContent;
761
+ const zonesAlreadyProcessed = Object.keys(newZones);
762
+ Object.keys(zones || {}).forEach((zoneCompound) => {
763
+ const [parentId] = zoneCompound.split(":");
764
+ if (zonesAlreadyProcessed.includes(zoneCompound)) {
765
+ return;
766
+ }
767
+ const [_2, newContent2] = processContent(
768
+ [rootDroppableId],
769
+ zoneCompound,
770
+ zones[zoneCompound],
771
+ "dropzone",
772
+ parentId
773
+ );
774
+ newZones[zoneCompound] = newContent2;
775
+ }, newZones);
776
+ const processedRoot = processItem(
777
+ {
778
+ type: "root",
779
+ props: __spreadProps(__spreadValues({}, (_a = state.data.root.props) != null ? _a : state.data.root), { id: "root" })
780
+ },
781
+ [],
782
+ -1
783
+ );
784
+ const root = __spreadProps(__spreadValues({}, state.data.root), {
785
+ props: processedRoot.props
786
+ });
787
+ return __spreadProps(__spreadValues({}, state), {
788
+ data: {
789
+ root,
790
+ content: processedContent,
791
+ zones: __spreadValues(__spreadValues({}, state.data.zones), newZones)
792
+ },
793
+ indexes: {
794
+ nodes: __spreadValues(__spreadValues({}, state.indexes.nodes), newNodeIndex),
795
+ zones: __spreadValues(__spreadValues({}, state.indexes.zones), newZoneIndex)
796
+ }
317
797
  });
318
798
  }
799
+
800
+ // lib/migrate.ts
801
+ var migrations = [
802
+ // Migrate root to root.props
803
+ (data) => {
804
+ const rootProps = data.root.props || data.root;
805
+ if (Object.keys(data.root).length > 0 && !data.root.props) {
806
+ console.warn(
807
+ "Migration applied: Root props moved from `root` to `root.props`."
808
+ );
809
+ return __spreadProps(__spreadValues({}, data), {
810
+ root: {
811
+ props: __spreadValues({}, rootProps)
812
+ }
813
+ });
814
+ }
815
+ return data;
816
+ },
817
+ // Migrate zones to slots
818
+ (data, config) => {
819
+ var _a;
820
+ if (!config) return data;
821
+ console.log("Migrating DropZones to slots...");
822
+ const updatedItems = {};
823
+ const appState = __spreadProps(__spreadValues({}, defaultAppState), { data });
824
+ const { indexes } = walkAppState(appState, config);
825
+ const deletedCompounds = [];
826
+ walkAppState(appState, config, (content, zoneCompound, zoneType) => {
827
+ var _a2, _b;
828
+ if (zoneType === "dropzone") {
829
+ const [id, slotName] = zoneCompound.split(":");
830
+ const nodeData = indexes.nodes[id].data;
831
+ const componentType = nodeData.type;
832
+ const configForComponent = id === "root" ? config.root : config.components[componentType];
833
+ if (((_b = (_a2 = configForComponent == null ? void 0 : configForComponent.fields) == null ? void 0 : _a2[slotName]) == null ? void 0 : _b.type) === "slot") {
834
+ updatedItems[id] = __spreadProps(__spreadValues({}, nodeData), {
835
+ props: __spreadProps(__spreadValues({}, nodeData.props), {
836
+ [slotName]: content
837
+ })
838
+ });
839
+ deletedCompounds.push(zoneCompound);
840
+ }
841
+ return content;
842
+ }
843
+ return content;
844
+ });
845
+ const updated = walkAppState(
846
+ appState,
847
+ config,
848
+ (content) => content,
849
+ (item) => {
850
+ var _a2;
851
+ return (_a2 = updatedItems[item.props.id]) != null ? _a2 : item;
852
+ }
853
+ );
854
+ deletedCompounds.forEach((zoneCompound) => {
855
+ var _a2;
856
+ const [_, propName] = zoneCompound.split(":");
857
+ console.log(
858
+ `\u2713 Success: Migrated "${zoneCompound}" from DropZone to slot field "${propName}"`
859
+ );
860
+ (_a2 = updated.data.zones) == null ? true : delete _a2[zoneCompound];
861
+ });
862
+ Object.keys((_a = updated.data.zones) != null ? _a : {}).forEach((zoneCompound) => {
863
+ const [_, propName] = zoneCompound.split(":");
864
+ throw new Error(
865
+ `Could not migrate DropZone "${zoneCompound}" to slot field. No slot exists with the name "${propName}".`
866
+ );
867
+ });
868
+ delete updated.data.zones;
869
+ return updated.data;
870
+ }
871
+ ];
872
+ function migrate(data, config) {
873
+ return migrations == null ? void 0 : migrations.reduce(
874
+ (acc, migration) => migration(acc, config),
875
+ data
876
+ );
877
+ }
319
878
  // Annotate the CommonJS export names for ESM import in node:
320
879
  0 && (module.exports = {
321
880
  Render,
322
- resolveAllData
881
+ migrate,
882
+ resolveAllData,
883
+ transformProps,
884
+ walkTree
323
885
  });