@uniformdev/canvas-react 18.33.1-alpha.7 → 18.34.0
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/index.d.ts +255 -6
- package/dist/index.esm.js +209 -152
- package/dist/index.js +215 -156
- package/dist/index.mjs +209 -152
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -30,18 +30,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
+
Composition: () => Composition,
|
|
33
34
|
DefaultNotImplementedComponent: () => DefaultNotImplementedComponent,
|
|
34
35
|
NOT_IMPLEMENTED_COMPONENT: () => NOT_IMPLEMENTED_COMPONENT,
|
|
36
|
+
Slot: () => Slot,
|
|
35
37
|
UniformComponent: () => UniformComponent,
|
|
36
38
|
UniformComposition: () => UniformComposition,
|
|
37
39
|
UniformSlot: () => UniformSlot,
|
|
38
40
|
componentStore: () => componentStore,
|
|
39
41
|
componentStoreResolver: () => componentStoreResolver,
|
|
42
|
+
createApiEnhancer: () => createApiEnhancer,
|
|
40
43
|
createComponentStore: () => createComponentStore,
|
|
41
44
|
createComponentStoreResolver: () => createComponentStoreResolver,
|
|
42
|
-
createUniformApiEnhancer: () =>
|
|
45
|
+
createUniformApiEnhancer: () => import_canvas3.createUniformApiEnhancer,
|
|
43
46
|
registerUniformComponent: () => registerUniformComponent,
|
|
47
|
+
useComposition: () => useComposition,
|
|
44
48
|
useCompositionEventEffect: () => useCompositionEventEffect,
|
|
49
|
+
useContextualEditing: () => useContextualEditing,
|
|
45
50
|
useUniformContextualEditing: () => useUniformContextualEditing,
|
|
46
51
|
useUniformCurrentComponent: () => useUniformCurrentComponent,
|
|
47
52
|
useUniformCurrentComposition: () => useUniformCurrentComposition
|
|
@@ -82,9 +87,9 @@ function DefaultNotImplementedComponent(props) {
|
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
// src/components/UniformComponent.tsx
|
|
85
|
-
var
|
|
90
|
+
var import_canvas6 = require("@uniformdev/canvas");
|
|
86
91
|
var import_context_react2 = require("@uniformdev/context-react");
|
|
87
|
-
var
|
|
92
|
+
var import_react6 = __toESM(require("react"));
|
|
88
93
|
|
|
89
94
|
// src/convertComponentToProps.ts
|
|
90
95
|
function convertComponentToProps(component) {
|
|
@@ -118,7 +123,8 @@ var createComponentStore = () => {
|
|
|
118
123
|
};
|
|
119
124
|
var createComponentStoreResolver = ({ store, defaultComponent = DefaultNotImplementedComponent }) => {
|
|
120
125
|
return (component) => {
|
|
121
|
-
|
|
126
|
+
var _a;
|
|
127
|
+
const resolved = (_a = store.get(getTypeWithVariant(component.type, component.variant))) != null ? _a : store.get(getTypeWithVariant(component.type));
|
|
122
128
|
return resolved || defaultComponent;
|
|
123
129
|
};
|
|
124
130
|
};
|
|
@@ -141,8 +147,8 @@ var componentStoreResolver = createComponentStoreResolver({
|
|
|
141
147
|
});
|
|
142
148
|
|
|
143
149
|
// src/components/UniformSlot.tsx
|
|
144
|
-
var
|
|
145
|
-
var
|
|
150
|
+
var import_canvas5 = require("@uniformdev/canvas");
|
|
151
|
+
var import_react5 = __toESM(require("react"));
|
|
146
152
|
|
|
147
153
|
// src/defaultSystemComponentResolver.tsx
|
|
148
154
|
var import_canvas2 = require("@uniformdev/canvas");
|
|
@@ -182,6 +188,166 @@ var defaultSystemComponentResolver = {
|
|
|
182
188
|
}
|
|
183
189
|
};
|
|
184
190
|
|
|
191
|
+
// src/components/ContextualEditingComponentWrapper.tsx
|
|
192
|
+
var import_canvas4 = require("@uniformdev/canvas");
|
|
193
|
+
var import_react4 = __toESM(require("react"));
|
|
194
|
+
|
|
195
|
+
// src/components/UniformComposition.tsx
|
|
196
|
+
var import_react3 = __toESM(require("react"));
|
|
197
|
+
|
|
198
|
+
// src/hooks/useUniformContextualEditing.ts
|
|
199
|
+
var import_canvas3 = require("@uniformdev/canvas");
|
|
200
|
+
var import_react2 = require("react");
|
|
201
|
+
var createApiEnhancer = import_canvas3.createUniformApiEnhancer;
|
|
202
|
+
var registeredCompositionIds = /* @__PURE__ */ new Set();
|
|
203
|
+
var useUniformContextualEditing = ({
|
|
204
|
+
initialCompositionValue,
|
|
205
|
+
enhance = (message) => message.composition
|
|
206
|
+
}) => {
|
|
207
|
+
const [contextualComposition, setContextualComposition] = (0, import_react2.useState)();
|
|
208
|
+
const channel = (0, import_react2.useMemo)(() => {
|
|
209
|
+
var _a;
|
|
210
|
+
if (!isInContextEditingMode()) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const channel2 = (0, import_canvas3.createCanvasChannel)({
|
|
214
|
+
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
215
|
+
listenTo: [window]
|
|
216
|
+
});
|
|
217
|
+
return channel2;
|
|
218
|
+
}, []);
|
|
219
|
+
(0, import_react2.useEffect)(() => {
|
|
220
|
+
if ((contextualComposition == null ? void 0 : contextualComposition._id) && (initialCompositionValue == null ? void 0 : initialCompositionValue._id) !== import_canvas3.EMPTY_COMPOSITION._id && (initialCompositionValue == null ? void 0 : initialCompositionValue._id) !== (contextualComposition == null ? void 0 : contextualComposition._id)) {
|
|
221
|
+
setContextualComposition(void 0);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (!channel || registeredCompositionIds.has(initialCompositionValue == null ? void 0 : initialCompositionValue._id)) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const unsubscribeFromCompositionUpdates = channel.on("update-composition", async (message) => {
|
|
228
|
+
if (!(0, import_canvas3.isUpdateCompositionMessage)(message)) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const enhancedComposition = await enhance(message);
|
|
232
|
+
setContextualComposition(enhancedComposition);
|
|
233
|
+
});
|
|
234
|
+
registeredCompositionIds.add(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
235
|
+
return () => {
|
|
236
|
+
unsubscribeFromCompositionUpdates();
|
|
237
|
+
registeredCompositionIds.delete(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
238
|
+
};
|
|
239
|
+
}, [channel, enhance, initialCompositionValue == null ? void 0 : initialCompositionValue._id, contextualComposition == null ? void 0 : contextualComposition._id]);
|
|
240
|
+
(0, import_react2.useEffect)(() => {
|
|
241
|
+
if (!isInContextEditingMode()) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const existingScript = document.getElementById(import_canvas3.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID);
|
|
245
|
+
if (existingScript) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
window.__UNIFORM_CONTEXTUAL_EDITING__ = {
|
|
249
|
+
framework: "React",
|
|
250
|
+
// Make sure to also update the value in canvas-vue
|
|
251
|
+
version: 1
|
|
252
|
+
};
|
|
253
|
+
const script = document.createElement("script");
|
|
254
|
+
script.id = import_canvas3.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID;
|
|
255
|
+
script.src = getCanvasInContextEmbedScriptUrl();
|
|
256
|
+
script.async = true;
|
|
257
|
+
document.head.appendChild(script);
|
|
258
|
+
}, [initialCompositionValue == null ? void 0 : initialCompositionValue._id]);
|
|
259
|
+
return {
|
|
260
|
+
composition: contextualComposition != null ? contextualComposition : initialCompositionValue,
|
|
261
|
+
isContextualEditing: Boolean(contextualComposition)
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
function getCanvasInContextEmbedScriptUrl() {
|
|
265
|
+
const scriptUrl = `${window.document.referrer}files/canvas-in-context-embed/index.js`;
|
|
266
|
+
return scriptUrl;
|
|
267
|
+
}
|
|
268
|
+
function isInContextEditingMode() {
|
|
269
|
+
if (typeof window === "undefined") {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
|
|
273
|
+
import_canvas3.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
|
|
274
|
+
);
|
|
275
|
+
const isAllowlistedReferrer = Boolean(
|
|
276
|
+
window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
|
|
277
|
+
);
|
|
278
|
+
return isOpenedByInContextEditor && isAllowlistedReferrer;
|
|
279
|
+
}
|
|
280
|
+
var useContextualEditing = useUniformContextualEditing;
|
|
281
|
+
|
|
282
|
+
// src/components/UniformComposition.tsx
|
|
283
|
+
var UniformCompositionContext = (0, import_react3.createContext)({
|
|
284
|
+
data: void 0,
|
|
285
|
+
isContextualEditing: false
|
|
286
|
+
});
|
|
287
|
+
function useUniformCurrentComposition() {
|
|
288
|
+
return (0, import_react3.useContext)(UniformCompositionContext);
|
|
289
|
+
}
|
|
290
|
+
function UniformComposition({
|
|
291
|
+
data,
|
|
292
|
+
behaviorTracking = "onView",
|
|
293
|
+
children,
|
|
294
|
+
resolveRenderer,
|
|
295
|
+
contextualEditingEnhancer
|
|
296
|
+
}) {
|
|
297
|
+
const { composition, isContextualEditing } = useUniformContextualEditing({
|
|
298
|
+
initialCompositionValue: data,
|
|
299
|
+
enhance: contextualEditingEnhancer
|
|
300
|
+
});
|
|
301
|
+
return /* @__PURE__ */ import_react3.default.createElement(
|
|
302
|
+
UniformCompositionContext.Provider,
|
|
303
|
+
{
|
|
304
|
+
value: { data: composition, behaviorTracking, resolveRenderer, isContextualEditing }
|
|
305
|
+
},
|
|
306
|
+
/* @__PURE__ */ import_react3.default.createElement(ContextualEditingComponentWrapper, { component: composition }, /* @__PURE__ */ import_react3.default.createElement(
|
|
307
|
+
UniformComponent,
|
|
308
|
+
{
|
|
309
|
+
data: composition,
|
|
310
|
+
behaviorTracking,
|
|
311
|
+
resolveRenderer
|
|
312
|
+
},
|
|
313
|
+
children
|
|
314
|
+
))
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
var useComposition = useUniformCurrentComposition;
|
|
318
|
+
var Composition = UniformComposition;
|
|
319
|
+
|
|
320
|
+
// src/components/ContextualEditingComponentWrapper.tsx
|
|
321
|
+
function ContextualEditingComponentWrapper({
|
|
322
|
+
component,
|
|
323
|
+
parentComponent,
|
|
324
|
+
slotName,
|
|
325
|
+
indexInSlot,
|
|
326
|
+
slotChildrenCount,
|
|
327
|
+
emptyPlaceholder,
|
|
328
|
+
children
|
|
329
|
+
}) {
|
|
330
|
+
var _a, _b, _c, _d;
|
|
331
|
+
const isPlaceholder = (component == null ? void 0 : component._id) === import_canvas4.PLACEHOLDER_ID;
|
|
332
|
+
const { isContextualEditing } = useUniformCurrentComposition();
|
|
333
|
+
return !isContextualEditing ? /* @__PURE__ */ import_react4.default.createElement(import_react4.default.Fragment, null, children) : /* @__PURE__ */ import_react4.default.createElement(import_react4.default.Fragment, null, /* @__PURE__ */ import_react4.default.createElement(
|
|
334
|
+
"script",
|
|
335
|
+
{
|
|
336
|
+
"data-role": import_canvas4.IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,
|
|
337
|
+
"data-parent-id": parentComponent == null ? void 0 : parentComponent._id,
|
|
338
|
+
"data-parent-type": parentComponent == null ? void 0 : parentComponent.type,
|
|
339
|
+
"data-component-id": component == null ? void 0 : component._id,
|
|
340
|
+
"data-slot-name": slotName != null ? slotName : "",
|
|
341
|
+
"data-component-index": indexInSlot != null ? indexInSlot : "",
|
|
342
|
+
"data-total-components": slotChildrenCount != null ? slotChildrenCount : "",
|
|
343
|
+
"data-component-name": component == null ? void 0 : component.type,
|
|
344
|
+
"data-is-placeholder": isPlaceholder ? "true" : void 0,
|
|
345
|
+
"data-is-localized": ((_a = component == null ? void 0 : component.parameters) == null ? void 0 : _a[import_canvas4.CANVAS_LOCALE_TAG_PARAM]) ? "true" : void 0,
|
|
346
|
+
"data-component-title": (_d = (_c = (_b = component == null ? void 0 : component.parameters) == null ? void 0 : _b.title) == null ? void 0 : _c.value) != null ? _d : ""
|
|
347
|
+
}
|
|
348
|
+
), isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder : children, /* @__PURE__ */ import_react4.default.createElement("script", { "data-role": import_canvas4.IN_CONTEXT_EDITOR_COMPONENT_END_ROLE }));
|
|
349
|
+
}
|
|
350
|
+
|
|
185
351
|
// src/components/UniformSlot.tsx
|
|
186
352
|
function UniformSlot({
|
|
187
353
|
name,
|
|
@@ -219,9 +385,9 @@ function UniformSlot({
|
|
|
219
385
|
emptyPlaceholder
|
|
220
386
|
});
|
|
221
387
|
const elements = children ? children({ child, component, key: `wrapped-inner-${index}` }) : child;
|
|
222
|
-
return
|
|
388
|
+
return import_react5.default.createElement(import_react5.default.Fragment, { key: index }, elements);
|
|
223
389
|
});
|
|
224
|
-
return
|
|
390
|
+
return import_react5.default.createElement(import_react5.default.Fragment, void 0, finalChildren);
|
|
225
391
|
}
|
|
226
392
|
function renderComponent({
|
|
227
393
|
component,
|
|
@@ -234,43 +400,42 @@ function renderComponent({
|
|
|
234
400
|
slotChildrenCount,
|
|
235
401
|
emptyPlaceholder
|
|
236
402
|
}) {
|
|
237
|
-
var _a, _b, _c, _d;
|
|
238
403
|
const RenderComponent = resolveRenderer == null ? void 0 : resolveRenderer(component);
|
|
239
|
-
if (component.type ===
|
|
404
|
+
if (component.type === import_canvas5.CANVAS_TEST_TYPE) {
|
|
240
405
|
return resolveSystem.test(
|
|
241
406
|
component,
|
|
242
407
|
key,
|
|
243
408
|
(variantComponent, key2) => {
|
|
244
|
-
var
|
|
409
|
+
var _a, _b;
|
|
245
410
|
return renderComponent({
|
|
246
411
|
component: variantComponent,
|
|
247
412
|
resolveRenderer,
|
|
248
413
|
resolveSystem,
|
|
249
414
|
key: key2,
|
|
250
415
|
parentComponent: component,
|
|
251
|
-
slotName:
|
|
252
|
-
slotChildrenCount: (
|
|
253
|
-
indexInSlot: (
|
|
416
|
+
slotName: import_canvas5.CANVAS_TEST_SLOT,
|
|
417
|
+
slotChildrenCount: (_a = component == null ? void 0 : component.slots) == null ? void 0 : _a[import_canvas5.CANVAS_TEST_SLOT].length,
|
|
418
|
+
indexInSlot: (_b = component == null ? void 0 : component.slots) == null ? void 0 : _b[import_canvas5.CANVAS_TEST_SLOT].findIndex(
|
|
254
419
|
({ _id }) => variantComponent._id === _id
|
|
255
420
|
)
|
|
256
421
|
});
|
|
257
422
|
}
|
|
258
423
|
);
|
|
259
|
-
} else if (component.type ===
|
|
424
|
+
} else if (component.type === import_canvas5.CANVAS_PERSONALIZE_TYPE) {
|
|
260
425
|
return resolveSystem.personalization(
|
|
261
426
|
component,
|
|
262
427
|
key,
|
|
263
428
|
(variantComponent, key2) => {
|
|
264
|
-
var
|
|
429
|
+
var _a, _b;
|
|
265
430
|
return renderComponent({
|
|
266
431
|
component: variantComponent,
|
|
267
432
|
resolveRenderer,
|
|
268
433
|
resolveSystem,
|
|
269
434
|
key: key2,
|
|
270
435
|
parentComponent: component,
|
|
271
|
-
slotName:
|
|
272
|
-
slotChildrenCount: (
|
|
273
|
-
indexInSlot: (
|
|
436
|
+
slotName: import_canvas5.CANVAS_PERSONALIZE_SLOT,
|
|
437
|
+
slotChildrenCount: (_a = component == null ? void 0 : component.slots) == null ? void 0 : _a[import_canvas5.CANVAS_PERSONALIZE_SLOT].length,
|
|
438
|
+
indexInSlot: (_b = component == null ? void 0 : component.slots) == null ? void 0 : _b[import_canvas5.CANVAS_PERSONALIZE_SLOT].findIndex(
|
|
274
439
|
({ _id }) => variantComponent._id === _id
|
|
275
440
|
)
|
|
276
441
|
});
|
|
@@ -278,25 +443,18 @@ function renderComponent({
|
|
|
278
443
|
);
|
|
279
444
|
} else if (RenderComponent) {
|
|
280
445
|
const props = convertComponentToProps(component);
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
return /* @__PURE__ */ import_react2.default.createElement(UniformComponent, { key, data: component, resolveRenderer }, !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ import_react2.default.createElement(
|
|
284
|
-
"script",
|
|
446
|
+
return /* @__PURE__ */ import_react5.default.createElement(UniformComponent, { key, data: component, resolveRenderer }, /* @__PURE__ */ import_react5.default.createElement(
|
|
447
|
+
ContextualEditingComponentWrapper,
|
|
285
448
|
{
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
"data-is-placeholder": isPlaceholder ? "true" : void 0,
|
|
296
|
-
"data-is-localized": ((_a = component.parameters) == null ? void 0 : _a[import_canvas3.CANVAS_LOCALE_TAG_PARAM]) ? "true" : void 0,
|
|
297
|
-
"data-component-title": (_d = (_c = (_b = component.parameters) == null ? void 0 : _b.title) == null ? void 0 : _c.value) != null ? _d : ""
|
|
298
|
-
}
|
|
299
|
-
), isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder : /* @__PURE__ */ import_react2.default.createElement(RenderComponent, { ...props }), !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ import_react2.default.createElement("script", { "data-role": "component-end" }));
|
|
449
|
+
component,
|
|
450
|
+
parentComponent,
|
|
451
|
+
slotName,
|
|
452
|
+
indexInSlot,
|
|
453
|
+
slotChildrenCount,
|
|
454
|
+
emptyPlaceholder
|
|
455
|
+
},
|
|
456
|
+
/* @__PURE__ */ import_react5.default.createElement(RenderComponent, { ...props })
|
|
457
|
+
));
|
|
300
458
|
} else if (process.env.NODE_ENV !== "production") {
|
|
301
459
|
console.warn(
|
|
302
460
|
`[canvas] found component of type '${component.type}'${component.variant ? ` with variant '${component.variant}'` : ""}. Nothing will be rendered. Check your resolveRenderer function or add registerUniformComponent to your component.
|
|
@@ -306,11 +464,12 @@ function renderComponent({
|
|
|
306
464
|
}
|
|
307
465
|
return null;
|
|
308
466
|
}
|
|
467
|
+
var Slot = UniformSlot;
|
|
309
468
|
|
|
310
469
|
// src/components/UniformComponent.tsx
|
|
311
|
-
var UniformComponentContext = (0,
|
|
470
|
+
var UniformComponentContext = (0, import_react6.createContext)({});
|
|
312
471
|
function useUniformCurrentComponent() {
|
|
313
|
-
return (0,
|
|
472
|
+
return (0, import_react6.useContext)(UniformComponentContext);
|
|
314
473
|
}
|
|
315
474
|
function UniformComponent({
|
|
316
475
|
data,
|
|
@@ -332,7 +491,7 @@ function UniformComponent({
|
|
|
332
491
|
resolveRenderer: resolveRenderer || parentData.resolveRenderer || componentStoreResolver,
|
|
333
492
|
behaviorTracking: (_a = behaviorTracking != null ? behaviorTracking : parentData == null ? void 0 : parentData.behaviorTracking) != null ? _a : "onView"
|
|
334
493
|
};
|
|
335
|
-
const enrichmentTags = (_c = (_b = data.parameters) == null ? void 0 : _b[
|
|
494
|
+
const enrichmentTags = (_c = (_b = data.parameters) == null ? void 0 : _b[import_canvas6.CANVAS_ENRICHMENT_TAG_PARAM]) == null ? void 0 : _c.value;
|
|
336
495
|
const TrackComponent = contextValue.behaviorTracking === "onLoad" ? import_context_react2.TrackFragment : import_context_react2.Track;
|
|
337
496
|
const resolvedChildren = resolveChildren({
|
|
338
497
|
children,
|
|
@@ -340,9 +499,9 @@ function UniformComponent({
|
|
|
340
499
|
hasParentLayout: Boolean(parentData.data),
|
|
341
500
|
resolveRenderer: contextValue.resolveRenderer
|
|
342
501
|
});
|
|
343
|
-
return /* @__PURE__ */
|
|
502
|
+
return /* @__PURE__ */ import_react6.default.createElement(UniformComponentContext.Provider, { value: contextValue }, contextContextProviderPresent ? (
|
|
344
503
|
/* auto-track behavior signals when in a Canvas context */
|
|
345
|
-
/* @__PURE__ */
|
|
504
|
+
/* @__PURE__ */ import_react6.default.createElement(TrackComponent, { behavior: enrichmentTags }, resolvedChildren)
|
|
346
505
|
) : resolvedChildren);
|
|
347
506
|
}
|
|
348
507
|
function resolveChildren({
|
|
@@ -355,146 +514,41 @@ function resolveChildren({
|
|
|
355
514
|
if (!children && !hasParentLayout) {
|
|
356
515
|
const topLevelComponent = resolveRenderer({ type: data.type });
|
|
357
516
|
if (topLevelComponent) {
|
|
358
|
-
children =
|
|
517
|
+
children = import_react6.default.createElement(topLevelComponent, convertComponentToProps(data));
|
|
359
518
|
} else {
|
|
360
519
|
if (Object.keys((_a = data.slots) != null ? _a : {}).length > 1 && process.env.NODE_ENV === "development") {
|
|
361
520
|
console.warn(
|
|
362
|
-
`[canvas-dev] All the slots in component '${data.type}' are rendered in no particular order. Use '<
|
|
521
|
+
`[canvas-dev] All the slots in component '${data.type}' are rendered in no particular order. Use '<Slot name={slotName} />' to reliably render the slots.`
|
|
363
522
|
);
|
|
364
523
|
}
|
|
365
|
-
children = Object.keys(data.slots || {}).map((slotName) => /* @__PURE__ */
|
|
524
|
+
children = Object.keys(data.slots || {}).map((slotName) => /* @__PURE__ */ import_react6.default.createElement(UniformSlot, { key: slotName, name: slotName }));
|
|
366
525
|
}
|
|
367
526
|
}
|
|
368
527
|
const renderChildren = typeof children === "function" ? children(convertComponentToProps(data)) : children;
|
|
369
528
|
return renderChildren;
|
|
370
529
|
}
|
|
371
530
|
|
|
372
|
-
// src/components/UniformComposition.tsx
|
|
373
|
-
var import_react5 = __toESM(require("react"));
|
|
374
|
-
|
|
375
|
-
// src/hooks/useUniformContextualEditing.ts
|
|
376
|
-
var import_canvas5 = require("@uniformdev/canvas");
|
|
377
|
-
var import_react4 = require("react");
|
|
378
|
-
var registeredCompositionIds = /* @__PURE__ */ new Set();
|
|
379
|
-
var useUniformContextualEditing = ({
|
|
380
|
-
initialCompositionValue,
|
|
381
|
-
enhance = (message) => message.composition
|
|
382
|
-
}) => {
|
|
383
|
-
const [contextualComposition, setContextualComposition] = (0, import_react4.useState)();
|
|
384
|
-
const channel = (0, import_react4.useMemo)(() => {
|
|
385
|
-
var _a;
|
|
386
|
-
if (!isInContextEditingMode()) {
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
const channel2 = (0, import_canvas5.createCanvasChannel)({
|
|
390
|
-
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
391
|
-
listenTo: [window]
|
|
392
|
-
});
|
|
393
|
-
return channel2;
|
|
394
|
-
}, []);
|
|
395
|
-
(0, import_react4.useEffect)(() => {
|
|
396
|
-
if (!channel || registeredCompositionIds.has(initialCompositionValue == null ? void 0 : initialCompositionValue._id)) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
const unsubscribe = channel.on("update-composition", async (message) => {
|
|
400
|
-
if (!(0, import_canvas5.isUpdateCompositionMessage)(message)) {
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
const enhancedComposition = await enhance(message);
|
|
404
|
-
setContextualComposition(enhancedComposition);
|
|
405
|
-
});
|
|
406
|
-
registeredCompositionIds.add(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
407
|
-
return () => {
|
|
408
|
-
unsubscribe();
|
|
409
|
-
registeredCompositionIds.delete(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
410
|
-
};
|
|
411
|
-
}, [channel, enhance, initialCompositionValue == null ? void 0 : initialCompositionValue._id]);
|
|
412
|
-
(0, import_react4.useEffect)(() => {
|
|
413
|
-
if (!isInContextEditingMode()) {
|
|
414
|
-
return;
|
|
415
|
-
}
|
|
416
|
-
const existingScript = document.getElementById(import_canvas5.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID);
|
|
417
|
-
if (existingScript) {
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
window.__UNIFORM_CONTEXTUAL_EDITING__ = {
|
|
421
|
-
framework: "React"
|
|
422
|
-
};
|
|
423
|
-
const script = document.createElement("script");
|
|
424
|
-
script.id = import_canvas5.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID;
|
|
425
|
-
script.src = getCanvasInContextEmbedScriptUrl();
|
|
426
|
-
script.async = true;
|
|
427
|
-
document.head.appendChild(script);
|
|
428
|
-
}, []);
|
|
429
|
-
return {
|
|
430
|
-
composition: contextualComposition != null ? contextualComposition : initialCompositionValue
|
|
431
|
-
};
|
|
432
|
-
};
|
|
433
|
-
function getCanvasInContextEmbedScriptUrl() {
|
|
434
|
-
const scriptUrl = `${window.document.referrer}files/canvas-in-context-embed/index.js`;
|
|
435
|
-
return scriptUrl;
|
|
436
|
-
}
|
|
437
|
-
function isInContextEditingMode() {
|
|
438
|
-
if (typeof window === "undefined") {
|
|
439
|
-
return false;
|
|
440
|
-
}
|
|
441
|
-
const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
|
|
442
|
-
import_canvas5.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
|
|
443
|
-
);
|
|
444
|
-
const isAllowlistedReferrer = Boolean(
|
|
445
|
-
window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
|
|
446
|
-
);
|
|
447
|
-
return isOpenedByInContextEditor && isAllowlistedReferrer;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// src/components/UniformComposition.tsx
|
|
451
|
-
var UniformCompositionContext = (0, import_react5.createContext)({ data: void 0 });
|
|
452
|
-
function useUniformCurrentComposition() {
|
|
453
|
-
return (0, import_react5.useContext)(UniformCompositionContext);
|
|
454
|
-
}
|
|
455
|
-
function UniformComposition({
|
|
456
|
-
data,
|
|
457
|
-
behaviorTracking = "onView",
|
|
458
|
-
children,
|
|
459
|
-
resolveRenderer,
|
|
460
|
-
contextualEditingEnhancer
|
|
461
|
-
}) {
|
|
462
|
-
const { composition } = useUniformContextualEditing({
|
|
463
|
-
initialCompositionValue: data,
|
|
464
|
-
enhance: contextualEditingEnhancer
|
|
465
|
-
});
|
|
466
|
-
return /* @__PURE__ */ import_react5.default.createElement(UniformCompositionContext.Provider, { value: { data: composition, behaviorTracking, resolveRenderer } }, /* @__PURE__ */ import_react5.default.createElement(
|
|
467
|
-
UniformComponent,
|
|
468
|
-
{
|
|
469
|
-
data: composition,
|
|
470
|
-
behaviorTracking,
|
|
471
|
-
resolveRenderer
|
|
472
|
-
},
|
|
473
|
-
children
|
|
474
|
-
));
|
|
475
|
-
}
|
|
476
|
-
|
|
477
531
|
// src/hooks/useCompositionEventEffect.ts
|
|
478
|
-
var
|
|
479
|
-
var
|
|
532
|
+
var import_canvas7 = require("@uniformdev/canvas");
|
|
533
|
+
var import_react7 = require("react");
|
|
480
534
|
function useCompositionEventEffect({
|
|
481
535
|
enabled,
|
|
482
536
|
projectId,
|
|
483
537
|
compositionId,
|
|
484
538
|
effect
|
|
485
539
|
}) {
|
|
486
|
-
(0,
|
|
540
|
+
(0, import_react7.useEffect)(() => {
|
|
487
541
|
if (!enabled || !compositionId || !projectId) {
|
|
488
542
|
return;
|
|
489
543
|
}
|
|
490
544
|
let goodbye = void 0;
|
|
491
545
|
const loadEffect = async () => {
|
|
492
|
-
const eventBus = await (0,
|
|
546
|
+
const eventBus = await (0, import_canvas7.createEventBus)();
|
|
493
547
|
if (eventBus) {
|
|
494
|
-
goodbye = (0,
|
|
548
|
+
goodbye = (0, import_canvas7.subscribeToComposition)({
|
|
495
549
|
eventBus,
|
|
496
550
|
compositionId,
|
|
497
|
-
compositionState:
|
|
551
|
+
compositionState: import_canvas7.CANVAS_DRAFT_STATE,
|
|
498
552
|
projectId,
|
|
499
553
|
callback: effect,
|
|
500
554
|
event: "updated"
|
|
@@ -511,18 +565,23 @@ function useCompositionEventEffect({
|
|
|
511
565
|
}
|
|
512
566
|
// Annotate the CommonJS export names for ESM import in node:
|
|
513
567
|
0 && (module.exports = {
|
|
568
|
+
Composition,
|
|
514
569
|
DefaultNotImplementedComponent,
|
|
515
570
|
NOT_IMPLEMENTED_COMPONENT,
|
|
571
|
+
Slot,
|
|
516
572
|
UniformComponent,
|
|
517
573
|
UniformComposition,
|
|
518
574
|
UniformSlot,
|
|
519
575
|
componentStore,
|
|
520
576
|
componentStoreResolver,
|
|
577
|
+
createApiEnhancer,
|
|
521
578
|
createComponentStore,
|
|
522
579
|
createComponentStoreResolver,
|
|
523
580
|
createUniformApiEnhancer,
|
|
524
581
|
registerUniformComponent,
|
|
582
|
+
useComposition,
|
|
525
583
|
useCompositionEventEffect,
|
|
584
|
+
useContextualEditing,
|
|
526
585
|
useUniformContextualEditing,
|
|
527
586
|
useUniformCurrentComponent,
|
|
528
587
|
useUniformCurrentComposition
|