@measured/puck 0.19.0-canary.e62832e → 0.19.0-canary.e7d23713

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,8 +68,10 @@ var __async = (__this, __arguments, generator) => {
68
68
  var rsc_exports = {};
69
69
  __export(rsc_exports, {
70
70
  Render: () => Render,
71
+ migrate: () => migrate,
71
72
  resolveAllData: () => resolveAllData,
72
- transformProps: () => transformProps
73
+ transformProps: () => transformProps,
74
+ walkTree: () => walkTree
73
75
  });
74
76
  module.exports = __toCommonJS(rsc_exports);
75
77
 
@@ -81,7 +83,7 @@ var rootAreaId = "root";
81
83
  var rootZone = "default-zone";
82
84
  var rootDroppableId = `${rootAreaId}:${rootZone}`;
83
85
 
84
- // lib/setup-zone.ts
86
+ // lib/data/setup-zone.ts
85
87
  var setupZone = (data, zoneKey) => {
86
88
  if (zoneKey === rootDroppableId) {
87
89
  return data;
@@ -93,8 +95,219 @@ var setupZone = (data, zoneKey) => {
93
95
  return newData;
94
96
  };
95
97
 
96
- // 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");
97
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");
98
311
  function DropZoneRender({
99
312
  zone,
100
313
  data,
@@ -111,28 +324,29 @@ function DropZoneRender({
111
324
  zoneCompound = `${areaId}:${zone}`;
112
325
  content = setupZone(data, zoneCompound).zones[zoneCompound];
113
326
  }
114
- 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) => {
115
328
  const Component = config.components[item.type];
116
- if (Component) {
117
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
118
- Component.render,
119
- __spreadProps(__spreadValues({}, item.props), {
120
- puck: {
121
- renderDropZone: ({ zone: zone2 }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
122
- DropZoneRender,
123
- {
124
- zone: zone2,
125
- data,
126
- areaId: item.props.id,
127
- config,
128
- metadata
129
- }
130
- ),
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,
131
338
  metadata
132
339
  }
133
- }),
134
- item.props.id
135
- );
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);
136
350
  }
137
351
  return null;
138
352
  }) });
@@ -140,44 +354,43 @@ function DropZoneRender({
140
354
  function Render({
141
355
  config,
142
356
  data,
143
- metadata
357
+ metadata = {}
144
358
  }) {
145
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 })));
146
382
  if ((_a = config.root) == null ? void 0 : _a.render) {
147
- const rootProps = data.root.props || data.root;
148
- const title = rootProps.title || "";
149
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
150
- config.root.render,
151
- __spreadProps(__spreadValues({}, rootProps), {
152
- puck: {
153
- renderDropZone: ({ zone }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
154
- DropZoneRender,
155
- {
156
- zone,
157
- data,
158
- config,
159
- metadata
160
- }
161
- ),
162
- isEditing: false,
163
- dragRef: null
164
- },
165
- title,
166
- editMode: false,
167
- id: "puck-root",
168
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
169
- DropZoneRender,
170
- {
171
- config,
172
- data,
173
- zone: rootZone,
174
- metadata
175
- }
176
- )
177
- })
178
- );
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
+ ) }));
179
392
  }
180
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
393
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
181
394
  DropZoneRender,
182
395
  {
183
396
  config,
@@ -189,37 +402,29 @@ function Render({
189
402
  }
190
403
 
191
404
  // lib/get-changed.ts
405
+ var import_fast_deep_equal = __toESM(require("fast-deep-equal"));
192
406
  var getChanged = (newItem, oldItem) => {
193
407
  return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
194
408
  const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
195
409
  const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
196
410
  return __spreadProps(__spreadValues({}, acc), {
197
- [item]: oldItemProps[item] !== newItemProps[item]
411
+ [item]: !(0, import_fast_deep_equal.default)(oldItemProps[item], newItemProps[item])
198
412
  });
199
413
  }, {}) : {};
200
414
  };
201
415
 
202
416
  // lib/resolve-component-data.ts
417
+ var import_fast_deep_equal2 = __toESM(require("fast-deep-equal"));
203
418
  var cache = { lastChange: {} };
204
- var resolveAllComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (content, config, metadata = {}, onResolveStart, onResolveEnd) {
205
- return yield Promise.all(
206
- content.map((item) => __async(void 0, null, function* () {
207
- return yield resolveComponentData(
208
- item,
209
- config,
210
- metadata,
211
- onResolveStart,
212
- onResolveEnd
213
- );
214
- }))
215
- );
216
- });
217
- var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd) {
218
- const configForItem = config.components[item.type];
219
- if (configForItem.resolveData) {
220
- const { item: oldItem = null, resolved = {} } = cache.lastChange[item.props.id] || {};
221
- if (item && item === oldItem) {
222
- 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 };
223
428
  }
224
429
  const changed = getChanged(item, oldItem);
225
430
  if (onResolveStart) {
@@ -228,102 +433,154 @@ var resolveComponentData = (_0, _1, ..._2) => __async(void 0, [_0, _1, ..._2], f
228
433
  const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
229
434
  changed,
230
435
  lastData: oldItem,
231
- metadata
232
- });
233
- const resolvedItem = __spreadProps(__spreadValues({}, item), {
234
- props: __spreadValues(__spreadValues({}, item.props), resolvedProps)
436
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
437
+ trigger
235
438
  });
439
+ resolvedItem.props = __spreadValues(__spreadValues({}, item.props), resolvedProps);
236
440
  if (Object.keys(readOnly).length) {
237
441
  resolvedItem.readOnly = readOnly;
238
442
  }
239
- cache.lastChange[item.props.id] = {
240
- item,
241
- resolved: resolvedItem
242
- };
243
- if (onResolveEnd) {
244
- onResolveEnd(resolvedItem);
245
- }
246
- return resolvedItem;
247
443
  }
248
- 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
+ };
249
475
  });
250
476
 
251
- // lib/resolve-root-data.ts
252
- var cache2 = {};
253
- function resolveRootData(data, config, metadata) {
254
- return __async(this, null, function* () {
255
- var _a, _b, _c, _d, _e;
256
- if (((_a = config.root) == null ? void 0 : _a.resolveData) && data.root.props) {
257
- if (((_b = cache2.lastChange) == null ? void 0 : _b.original) === data.root) {
258
- return cache2.lastChange.resolved;
259
- }
260
- const changed = getChanged(data.root, (_c = cache2.lastChange) == null ? void 0 : _c.original);
261
- const rootWithProps = data.root;
262
- const resolvedRoot = yield (_e = config.root) == null ? void 0 : _e.resolveData(rootWithProps, {
263
- changed,
264
- lastData: ((_d = cache2.lastChange) == null ? void 0 : _d.original) || {},
265
- metadata: metadata || {}
266
- });
267
- cache2.lastChange = {
268
- original: data.root,
269
- resolved: resolvedRoot
270
- };
271
- return __spreadProps(__spreadValues(__spreadValues({}, data.root), resolvedRoot), {
272
- props: __spreadValues(__spreadValues({}, data.root.props), resolvedRoot.props)
273
- });
274
- }
275
- return data.root;
276
- });
277
- }
278
-
279
- // lib/default-data.ts
477
+ // lib/data/default-data.ts
280
478
  var defaultData = (data) => __spreadProps(__spreadValues({}, data), {
281
479
  root: data.root || {},
282
480
  content: data.content || []
283
481
  });
284
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
+
285
491
  // lib/resolve-all-data.ts
286
492
  function resolveAllData(_0, _1) {
287
493
  return __async(this, arguments, function* (data, config, metadata = {}, onResolveStart, onResolveEnd) {
494
+ var _a;
288
495
  const defaultedData = defaultData(data);
289
- const dynamicRoot = yield resolveRootData(
290
- defaultedData,
291
- config,
292
- metadata
293
- );
294
- const { zones = {} } = data;
295
- const zoneKeys = Object.keys(zones);
296
- const resolvedZones = {};
297
- for (let i = 0; i < zoneKeys.length; i++) {
298
- const zoneKey = zoneKeys[i];
299
- resolvedZones[zoneKey] = yield resolveAllComponentData(
300
- 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,
301
501
  config,
302
502
  metadata,
303
- onResolveStart,
304
- onResolveEnd
503
+ () => {
504
+ },
505
+ () => {
506
+ },
507
+ "force"
508
+ )).node;
509
+ const resolvedDeep = yield mapSlots(
510
+ resolved,
511
+ processContent,
512
+ config
305
513
  );
306
- }
307
- return __spreadProps(__spreadValues({}, defaultedData), {
308
- root: dynamicRoot,
309
- content: yield resolveAllComponentData(
310
- defaultedData.content,
311
- config,
312
- metadata,
313
- onResolveStart,
314
- onResolveEnd
315
- ),
316
- zones: resolvedZones
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;
317
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;
318
538
  });
319
539
  }
320
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
+
321
576
  // lib/transform-props.ts
322
- function transformProps(data, propTransforms) {
577
+ function transformProps(data, propTransforms, config = { components: {} }) {
323
578
  const mapItem = (item) => {
324
579
  if (propTransforms[item.type]) {
325
580
  return __spreadProps(__spreadValues({}, item), {
326
- props: propTransforms[item.type](item.props)
581
+ props: __spreadValues({
582
+ id: item.props.id
583
+ }, propTransforms[item.type](item.props))
327
584
  });
328
585
  }
329
586
  return item;
@@ -332,27 +589,297 @@ function transformProps(data, propTransforms) {
332
589
  const rootProps = defaultedData.root.props || defaultedData.root;
333
590
  let newRoot = __spreadValues({}, defaultedData.root);
334
591
  if (propTransforms["root"]) {
335
- if (defaultedData.root.props) {
336
- newRoot.props = propTransforms["root"](rootProps);
337
- } else {
338
- newRoot = propTransforms["root"](rootProps);
339
- }
592
+ newRoot.props = propTransforms["root"](rootProps);
340
593
  }
341
- const afterPropTransforms = __spreadProps(__spreadValues({}, defaultedData), {
342
- root: newRoot,
343
- content: defaultedData.content.map(mapItem),
344
- zones: Object.keys(data.zones || {}).reduce(
345
- (acc, zoneKey) => __spreadProps(__spreadValues({}, acc), {
346
- [zoneKey]: data.zones[zoneKey].map(mapItem)
347
- }),
348
- {}
349
- )
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
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
+ }
350
797
  });
351
- return afterPropTransforms;
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
+ );
352
877
  }
353
878
  // Annotate the CommonJS export names for ESM import in node:
354
879
  0 && (module.exports = {
355
880
  Render,
881
+ migrate,
356
882
  resolveAllData,
357
- transformProps
883
+ transformProps,
884
+ walkTree
358
885
  });