@measured/puck 0.19.0-canary.0ea6ce4 → 0.19.0-canary.15d05558

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