@measured/puck 0.21.0-canary.bd7b613d → 0.21.0-canary.c0db75c1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,477 @@
1
+ import {
2
+ __async,
3
+ __objRest,
4
+ __spreadProps,
5
+ __spreadValues,
6
+ init_react_import,
7
+ mapFields,
8
+ walkAppState,
9
+ walkTree
10
+ } from "./chunk-S3RM5GHZ.mjs";
11
+
12
+ // lib/migrate.ts
13
+ init_react_import();
14
+
15
+ // store/default-app-state.ts
16
+ init_react_import();
17
+
18
+ // components/ViewportControls/default-viewports.ts
19
+ init_react_import();
20
+ var defaultViewports = [
21
+ { width: 360, height: "auto", icon: "Smartphone", label: "Small" },
22
+ { width: 768, height: "auto", icon: "Tablet", label: "Medium" },
23
+ { width: 1280, height: "auto", icon: "Monitor", label: "Large" },
24
+ { width: "100%", height: "auto", icon: "FullWidth", label: "Full-width" }
25
+ ];
26
+
27
+ // store/default-app-state.ts
28
+ var defaultAppState = {
29
+ data: { content: [], root: {}, zones: {} },
30
+ ui: {
31
+ leftSideBarVisible: true,
32
+ rightSideBarVisible: true,
33
+ arrayState: {},
34
+ itemSelector: null,
35
+ componentList: {},
36
+ isDragging: false,
37
+ previewMode: "edit",
38
+ viewports: {
39
+ current: {
40
+ width: defaultViewports[0].width,
41
+ height: defaultViewports[0].height || "auto"
42
+ },
43
+ options: [],
44
+ controlsVisible: true
45
+ },
46
+ field: { focus: null },
47
+ plugin: { current: null }
48
+ },
49
+ indexes: {
50
+ nodes: {},
51
+ zones: {}
52
+ }
53
+ };
54
+
55
+ // lib/migrate.ts
56
+ var migrations = [
57
+ // Migrate root to root.props
58
+ (data) => {
59
+ const rootProps = data.root.props || data.root;
60
+ if (Object.keys(data.root).length > 0 && !data.root.props) {
61
+ console.warn(
62
+ "Migration applied: Root props moved from `root` to `root.props`."
63
+ );
64
+ return __spreadProps(__spreadValues({}, data), {
65
+ root: {
66
+ props: __spreadValues({}, rootProps)
67
+ }
68
+ });
69
+ }
70
+ return data;
71
+ },
72
+ // Migrate zones to slots
73
+ (data, config, migrationOptions) => {
74
+ var _a, _b;
75
+ if (!config) return data;
76
+ console.log("Migrating DropZones to slots...");
77
+ const updatedItems = {};
78
+ const appState = __spreadProps(__spreadValues({}, defaultAppState), { data });
79
+ const { indexes } = walkAppState(appState, config);
80
+ const deletedCompounds = [];
81
+ walkAppState(appState, config, (content, zoneCompound, zoneType) => {
82
+ var _a2, _b2, _c;
83
+ if (zoneType === "dropzone") {
84
+ const [id, slotName] = zoneCompound.split(":");
85
+ const nodeData = indexes.nodes[id].data;
86
+ const componentType = nodeData.type;
87
+ const configForComponent = id === "root" ? config.root : config.components[componentType];
88
+ if (((_b2 = (_a2 = configForComponent == null ? void 0 : configForComponent.fields) == null ? void 0 : _a2[slotName]) == null ? void 0 : _b2.type) === "slot") {
89
+ updatedItems[id] = __spreadProps(__spreadValues({}, nodeData), {
90
+ props: __spreadProps(__spreadValues(__spreadValues({}, nodeData.props), (_c = updatedItems[id]) == null ? void 0 : _c.props), {
91
+ [slotName]: content
92
+ })
93
+ });
94
+ deletedCompounds.push(zoneCompound);
95
+ }
96
+ return content;
97
+ }
98
+ return content;
99
+ });
100
+ const updated = walkAppState(
101
+ appState,
102
+ config,
103
+ (content) => content,
104
+ (item) => {
105
+ var _a2;
106
+ return (_a2 = updatedItems[item.props.id]) != null ? _a2 : item;
107
+ }
108
+ );
109
+ deletedCompounds.forEach((zoneCompound) => {
110
+ var _a2;
111
+ const [_, propName] = zoneCompound.split(":");
112
+ console.log(
113
+ `\u2713 Success: Migrated "${zoneCompound}" from DropZone to slot field "${propName}"`
114
+ );
115
+ (_a2 = updated.data.zones) == null ? true : delete _a2[zoneCompound];
116
+ });
117
+ if (migrationOptions == null ? void 0 : migrationOptions.migrateDynamicZonesForComponent) {
118
+ const unmigratedZonesGrouped = {};
119
+ Object.keys((_a = updated.data.zones) != null ? _a : {}).forEach((zoneCompound) => {
120
+ var _a2;
121
+ const [componentId, propName] = zoneCompound.split(":");
122
+ const content = (_a2 = updated.data.zones) == null ? void 0 : _a2[zoneCompound];
123
+ if (!content) {
124
+ return;
125
+ }
126
+ if (!unmigratedZonesGrouped[componentId]) {
127
+ unmigratedZonesGrouped[componentId] = {};
128
+ }
129
+ if (!unmigratedZonesGrouped[componentId][propName]) {
130
+ unmigratedZonesGrouped[componentId][propName] = content;
131
+ }
132
+ });
133
+ Object.keys(unmigratedZonesGrouped).forEach((componentId) => {
134
+ updated.data = walkTree(updated.data, config, (content) => {
135
+ return content.map((child) => {
136
+ var _a2;
137
+ if (child.props.id !== componentId) {
138
+ return child;
139
+ }
140
+ const migrateFn = (_a2 = migrationOptions == null ? void 0 : migrationOptions.migrateDynamicZonesForComponent) == null ? void 0 : _a2[child.type];
141
+ if (!migrateFn) {
142
+ return child;
143
+ }
144
+ const zones = unmigratedZonesGrouped[componentId];
145
+ const migratedProps = migrateFn(child.props, zones);
146
+ Object.keys(zones).forEach((propName) => {
147
+ var _a3;
148
+ const zoneCompound = `${componentId}:${propName}`;
149
+ console.log(`\u2713 Success: Migrated "${zoneCompound}" DropZone`);
150
+ (_a3 = updated.data.zones) == null ? true : delete _a3[zoneCompound];
151
+ });
152
+ return __spreadProps(__spreadValues({}, child), {
153
+ props: migratedProps
154
+ });
155
+ });
156
+ });
157
+ });
158
+ }
159
+ Object.keys((_b = updated.data.zones) != null ? _b : {}).forEach((zoneCompound) => {
160
+ const [_, propName] = zoneCompound.split(":");
161
+ throw new Error(
162
+ `Could not migrate DropZone "${zoneCompound}" to slot field. No slot exists with the name "${propName}".`
163
+ );
164
+ });
165
+ delete updated.data.zones;
166
+ return updated.data;
167
+ }
168
+ ];
169
+ function migrate(data, config, migrationOptions) {
170
+ return migrations == null ? void 0 : migrations.reduce(
171
+ (acc, migration) => migration(acc, config, migrationOptions),
172
+ data
173
+ );
174
+ }
175
+
176
+ // lib/transform-props.ts
177
+ init_react_import();
178
+
179
+ // lib/data/default-data.ts
180
+ init_react_import();
181
+ var defaultData = (data) => __spreadProps(__spreadValues({}, data), {
182
+ root: data.root || {},
183
+ content: data.content || []
184
+ });
185
+
186
+ // lib/transform-props.ts
187
+ function transformProps(data, propTransforms, config = { components: {} }) {
188
+ const mapItem = (item) => {
189
+ if (propTransforms[item.type]) {
190
+ return __spreadProps(__spreadValues({}, item), {
191
+ props: __spreadValues({
192
+ id: item.props.id
193
+ }, propTransforms[item.type](item.props))
194
+ });
195
+ }
196
+ return item;
197
+ };
198
+ const defaultedData = defaultData(data);
199
+ const rootProps = defaultedData.root.props || defaultedData.root;
200
+ let newRoot = __spreadValues({}, defaultedData.root);
201
+ if (propTransforms["root"]) {
202
+ newRoot.props = propTransforms["root"](rootProps);
203
+ }
204
+ const dataWithUpdatedRoot = __spreadProps(__spreadValues({}, defaultedData), { root: newRoot });
205
+ const updatedData = walkTree(
206
+ dataWithUpdatedRoot,
207
+ config,
208
+ (content) => content.map(mapItem)
209
+ );
210
+ if (!defaultedData.root.props) {
211
+ updatedData.root = updatedData.root.props;
212
+ }
213
+ return updatedData;
214
+ }
215
+
216
+ // lib/resolve-all-data.ts
217
+ init_react_import();
218
+
219
+ // lib/resolve-component-data.ts
220
+ init_react_import();
221
+
222
+ // lib/get-changed.ts
223
+ init_react_import();
224
+ import { deepEqual } from "fast-equals";
225
+ var getChanged = (newItem, oldItem) => {
226
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
227
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
228
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
229
+ return __spreadProps(__spreadValues({}, acc), {
230
+ [item]: !deepEqual(oldItemProps[item], newItemProps[item])
231
+ });
232
+ }, {}) : {};
233
+ };
234
+
235
+ // lib/resolve-component-data.ts
236
+ import { deepEqual as deepEqual2 } from "fast-equals";
237
+ var cache = { lastChange: {} };
238
+ var resolveComponentData = (_0, _1, ..._2) => __async(null, [_0, _1, ..._2], function* (item, config, metadata = {}, onResolveStart, onResolveEnd, trigger = "replace") {
239
+ const configForItem = "type" in item && item.type !== "root" ? config.components[item.type] : config.root;
240
+ const resolvedItem = __spreadValues({}, item);
241
+ const shouldRunResolver = (configForItem == null ? void 0 : configForItem.resolveData) && item.props;
242
+ const id = "id" in item.props ? item.props.id : "root";
243
+ if (shouldRunResolver) {
244
+ const { item: oldItem = null, resolved = {} } = cache.lastChange[id] || {};
245
+ if (trigger !== "force" && item && deepEqual2(item, oldItem)) {
246
+ return { node: resolved, didChange: false };
247
+ }
248
+ const changed = getChanged(item, oldItem);
249
+ if (onResolveStart) {
250
+ onResolveStart(item);
251
+ }
252
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, {
253
+ changed,
254
+ lastData: oldItem,
255
+ metadata: __spreadValues(__spreadValues({}, metadata), configForItem.metadata),
256
+ trigger
257
+ });
258
+ resolvedItem.props = __spreadValues(__spreadValues({}, item.props), resolvedProps);
259
+ if (Object.keys(readOnly).length) {
260
+ resolvedItem.readOnly = readOnly;
261
+ }
262
+ }
263
+ let itemWithResolvedChildren = yield mapFields(
264
+ resolvedItem,
265
+ {
266
+ slot: (_02) => __async(null, [_02], function* ({ value }) {
267
+ const content = value;
268
+ return yield Promise.all(
269
+ content.map(
270
+ (childItem) => __async(null, null, function* () {
271
+ return (yield resolveComponentData(
272
+ childItem,
273
+ config,
274
+ metadata,
275
+ onResolveStart,
276
+ onResolveEnd,
277
+ trigger
278
+ )).node;
279
+ })
280
+ )
281
+ );
282
+ })
283
+ },
284
+ config
285
+ );
286
+ if (shouldRunResolver && onResolveEnd) {
287
+ onResolveEnd(resolvedItem);
288
+ }
289
+ cache.lastChange[id] = {
290
+ item,
291
+ resolved: itemWithResolvedChildren
292
+ };
293
+ return {
294
+ node: itemWithResolvedChildren,
295
+ didChange: !deepEqual2(item, itemWithResolvedChildren)
296
+ };
297
+ });
298
+
299
+ // lib/data/to-component.ts
300
+ init_react_import();
301
+ var toComponent = (item) => {
302
+ return "type" in item ? item : __spreadProps(__spreadValues({}, item), {
303
+ props: __spreadProps(__spreadValues({}, item.props), { id: "root" }),
304
+ type: "root"
305
+ });
306
+ };
307
+
308
+ // lib/resolve-all-data.ts
309
+ function resolveAllData(_0, _1) {
310
+ return __async(this, arguments, function* (data, config, metadata = {}, onResolveStart, onResolveEnd) {
311
+ var _a;
312
+ const defaultedData = defaultData(data);
313
+ const resolveNode = (_node) => __async(null, null, function* () {
314
+ const node = toComponent(_node);
315
+ onResolveStart == null ? void 0 : onResolveStart(node);
316
+ const resolved = (yield resolveComponentData(
317
+ node,
318
+ config,
319
+ metadata,
320
+ () => {
321
+ },
322
+ () => {
323
+ },
324
+ "force"
325
+ )).node;
326
+ const resolvedDeep = yield mapFields(
327
+ resolved,
328
+ { slot: ({ value }) => processContent(value) },
329
+ config
330
+ );
331
+ onResolveEnd == null ? void 0 : onResolveEnd(toComponent(resolvedDeep));
332
+ return resolvedDeep;
333
+ });
334
+ const processContent = (content) => __async(null, null, function* () {
335
+ return Promise.all(content.map(resolveNode));
336
+ });
337
+ const processZones = () => __async(null, null, function* () {
338
+ var _a2;
339
+ const zones = (_a2 = data.zones) != null ? _a2 : {};
340
+ Object.entries(zones).forEach((_02) => __async(null, [_02], function* ([zoneKey, content]) {
341
+ zones[zoneKey] = yield Promise.all(content.map(resolveNode));
342
+ }));
343
+ return zones;
344
+ });
345
+ const dynamic = {
346
+ root: yield resolveNode(defaultedData.root),
347
+ content: yield processContent(defaultedData.content),
348
+ zones: yield processZones()
349
+ };
350
+ Object.keys((_a = defaultedData.zones) != null ? _a : {}).forEach((zoneKey) => __async(null, null, function* () {
351
+ const content = defaultedData.zones[zoneKey];
352
+ dynamic.zones[zoneKey] = yield processContent(content);
353
+ }), {});
354
+ return dynamic;
355
+ });
356
+ }
357
+
358
+ // lib/field-transforms/use-field-transforms.tsx
359
+ init_react_import();
360
+ import { useMemo } from "react";
361
+ function useFieldTransforms(config, item, transforms, readOnly, forceReadOnly) {
362
+ const mappers = useMemo(() => {
363
+ return Object.keys(transforms).reduce((acc, _fieldType) => {
364
+ const fieldType = _fieldType;
365
+ return __spreadProps(__spreadValues({}, acc), {
366
+ [fieldType]: (_a) => {
367
+ var _b = _a, {
368
+ parentId
369
+ } = _b, params = __objRest(_b, [
370
+ "parentId"
371
+ ]);
372
+ const wildcardPath = params.propPath.replace(/\[\d+\]/g, "[*]");
373
+ const isReadOnly = (readOnly == null ? void 0 : readOnly[params.propPath]) || (readOnly == null ? void 0 : readOnly[wildcardPath]) || forceReadOnly || false;
374
+ const fn = transforms[fieldType];
375
+ return fn == null ? void 0 : fn(__spreadProps(__spreadValues({}, params), {
376
+ isReadOnly,
377
+ componentId: parentId
378
+ }));
379
+ }
380
+ });
381
+ }, {});
382
+ }, [transforms, readOnly, forceReadOnly]);
383
+ const transformedProps = useMemo(() => {
384
+ const mapped = mapFields(item, mappers, config).props;
385
+ return mapped;
386
+ }, [config, item, mappers]);
387
+ const mergedProps = useMemo(
388
+ () => __spreadValues(__spreadValues({}, item.props), transformedProps),
389
+ [item.props, transformedProps]
390
+ );
391
+ return mergedProps;
392
+ }
393
+
394
+ // lib/field-transforms/default-transforms/slot-transform.tsx
395
+ init_react_import();
396
+ var getSlotTransform = (renderSlotEdit, renderSlotRender = renderSlotEdit) => ({
397
+ slot: ({ value: content, propName, field, isReadOnly }) => {
398
+ const render = isReadOnly ? renderSlotRender : renderSlotEdit;
399
+ const Slot = (dzProps) => render(__spreadProps(__spreadValues({
400
+ allow: (field == null ? void 0 : field.type) === "slot" ? field.allow : [],
401
+ disallow: (field == null ? void 0 : field.type) === "slot" ? field.disallow : []
402
+ }, dzProps), {
403
+ zone: propName,
404
+ content
405
+ }));
406
+ return Slot;
407
+ }
408
+ });
409
+
410
+ // lib/use-slots.tsx
411
+ init_react_import();
412
+ function useSlots(config, item, renderSlotEdit, renderSlotRender = renderSlotEdit, readOnly, forceReadOnly) {
413
+ return useFieldTransforms(
414
+ config,
415
+ item,
416
+ getSlotTransform(renderSlotEdit, renderSlotRender),
417
+ readOnly,
418
+ forceReadOnly
419
+ );
420
+ }
421
+
422
+ // components/SlotRender/server.tsx
423
+ init_react_import();
424
+ import { forwardRef } from "react";
425
+ import { jsx } from "react/jsx-runtime";
426
+ var SlotRenderPure = (props) => /* @__PURE__ */ jsx(SlotRender, __spreadValues({}, props));
427
+ var Item = ({
428
+ config,
429
+ item,
430
+ metadata
431
+ }) => {
432
+ const Component = config.components[item.type];
433
+ const props = useSlots(config, item, (slotProps) => /* @__PURE__ */ jsx(SlotRenderPure, __spreadProps(__spreadValues({}, slotProps), { config, metadata })));
434
+ return /* @__PURE__ */ jsx(
435
+ Component.render,
436
+ __spreadProps(__spreadValues({}, props), {
437
+ puck: __spreadProps(__spreadValues({}, props.puck), {
438
+ metadata: metadata || {}
439
+ })
440
+ })
441
+ );
442
+ };
443
+ var SlotRender = forwardRef(
444
+ function SlotRenderInternal({ className, style, content, config, metadata, as }, ref) {
445
+ const El = as != null ? as : "div";
446
+ return /* @__PURE__ */ jsx(El, { className, style, ref, children: content.map((item) => {
447
+ if (!config.components[item.type]) {
448
+ return null;
449
+ }
450
+ return /* @__PURE__ */ jsx(
451
+ Item,
452
+ {
453
+ config,
454
+ item,
455
+ metadata
456
+ },
457
+ item.props.id
458
+ );
459
+ }) });
460
+ }
461
+ );
462
+
463
+ export {
464
+ defaultViewports,
465
+ getChanged,
466
+ resolveComponentData,
467
+ defaultAppState,
468
+ useFieldTransforms,
469
+ getSlotTransform,
470
+ useSlots,
471
+ SlotRenderPure,
472
+ SlotRender,
473
+ toComponent,
474
+ migrate,
475
+ transformProps,
476
+ resolveAllData
477
+ };