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

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