@uniformdev/canvas-react 18.33.0 → 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 +3 -12
- package/dist/index.esm.js +202 -155
- package/dist/index.js +203 -159
- package/dist/index.mjs +202 -155
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -42,7 +42,7 @@ __export(src_exports, {
|
|
|
42
42
|
createApiEnhancer: () => createApiEnhancer,
|
|
43
43
|
createComponentStore: () => createComponentStore,
|
|
44
44
|
createComponentStoreResolver: () => createComponentStoreResolver,
|
|
45
|
-
createUniformApiEnhancer: () =>
|
|
45
|
+
createUniformApiEnhancer: () => import_canvas3.createUniformApiEnhancer,
|
|
46
46
|
registerUniformComponent: () => registerUniformComponent,
|
|
47
47
|
useComposition: () => useComposition,
|
|
48
48
|
useCompositionEventEffect: () => useCompositionEventEffect,
|
|
@@ -87,9 +87,9 @@ function DefaultNotImplementedComponent(props) {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
// src/components/UniformComponent.tsx
|
|
90
|
-
var
|
|
90
|
+
var import_canvas6 = require("@uniformdev/canvas");
|
|
91
91
|
var import_context_react2 = require("@uniformdev/context-react");
|
|
92
|
-
var
|
|
92
|
+
var import_react6 = __toESM(require("react"));
|
|
93
93
|
|
|
94
94
|
// src/convertComponentToProps.ts
|
|
95
95
|
function convertComponentToProps(component) {
|
|
@@ -123,7 +123,8 @@ var createComponentStore = () => {
|
|
|
123
123
|
};
|
|
124
124
|
var createComponentStoreResolver = ({ store, defaultComponent = DefaultNotImplementedComponent }) => {
|
|
125
125
|
return (component) => {
|
|
126
|
-
|
|
126
|
+
var _a;
|
|
127
|
+
const resolved = (_a = store.get(getTypeWithVariant(component.type, component.variant))) != null ? _a : store.get(getTypeWithVariant(component.type));
|
|
127
128
|
return resolved || defaultComponent;
|
|
128
129
|
};
|
|
129
130
|
};
|
|
@@ -146,8 +147,8 @@ var componentStoreResolver = createComponentStoreResolver({
|
|
|
146
147
|
});
|
|
147
148
|
|
|
148
149
|
// src/components/UniformSlot.tsx
|
|
149
|
-
var
|
|
150
|
-
var
|
|
150
|
+
var import_canvas5 = require("@uniformdev/canvas");
|
|
151
|
+
var import_react5 = __toESM(require("react"));
|
|
151
152
|
|
|
152
153
|
// src/defaultSystemComponentResolver.tsx
|
|
153
154
|
var import_canvas2 = require("@uniformdev/canvas");
|
|
@@ -187,6 +188,166 @@ var defaultSystemComponentResolver = {
|
|
|
187
188
|
}
|
|
188
189
|
};
|
|
189
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
|
+
|
|
190
351
|
// src/components/UniformSlot.tsx
|
|
191
352
|
function UniformSlot({
|
|
192
353
|
name,
|
|
@@ -224,9 +385,9 @@ function UniformSlot({
|
|
|
224
385
|
emptyPlaceholder
|
|
225
386
|
});
|
|
226
387
|
const elements = children ? children({ child, component, key: `wrapped-inner-${index}` }) : child;
|
|
227
|
-
return
|
|
388
|
+
return import_react5.default.createElement(import_react5.default.Fragment, { key: index }, elements);
|
|
228
389
|
});
|
|
229
|
-
return
|
|
390
|
+
return import_react5.default.createElement(import_react5.default.Fragment, void 0, finalChildren);
|
|
230
391
|
}
|
|
231
392
|
function renderComponent({
|
|
232
393
|
component,
|
|
@@ -239,43 +400,42 @@ function renderComponent({
|
|
|
239
400
|
slotChildrenCount,
|
|
240
401
|
emptyPlaceholder
|
|
241
402
|
}) {
|
|
242
|
-
var _a, _b, _c, _d;
|
|
243
403
|
const RenderComponent = resolveRenderer == null ? void 0 : resolveRenderer(component);
|
|
244
|
-
if (component.type ===
|
|
404
|
+
if (component.type === import_canvas5.CANVAS_TEST_TYPE) {
|
|
245
405
|
return resolveSystem.test(
|
|
246
406
|
component,
|
|
247
407
|
key,
|
|
248
408
|
(variantComponent, key2) => {
|
|
249
|
-
var
|
|
409
|
+
var _a, _b;
|
|
250
410
|
return renderComponent({
|
|
251
411
|
component: variantComponent,
|
|
252
412
|
resolveRenderer,
|
|
253
413
|
resolveSystem,
|
|
254
414
|
key: key2,
|
|
255
415
|
parentComponent: component,
|
|
256
|
-
slotName:
|
|
257
|
-
slotChildrenCount: (
|
|
258
|
-
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(
|
|
259
419
|
({ _id }) => variantComponent._id === _id
|
|
260
420
|
)
|
|
261
421
|
});
|
|
262
422
|
}
|
|
263
423
|
);
|
|
264
|
-
} else if (component.type ===
|
|
424
|
+
} else if (component.type === import_canvas5.CANVAS_PERSONALIZE_TYPE) {
|
|
265
425
|
return resolveSystem.personalization(
|
|
266
426
|
component,
|
|
267
427
|
key,
|
|
268
428
|
(variantComponent, key2) => {
|
|
269
|
-
var
|
|
429
|
+
var _a, _b;
|
|
270
430
|
return renderComponent({
|
|
271
431
|
component: variantComponent,
|
|
272
432
|
resolveRenderer,
|
|
273
433
|
resolveSystem,
|
|
274
434
|
key: key2,
|
|
275
435
|
parentComponent: component,
|
|
276
|
-
slotName:
|
|
277
|
-
slotChildrenCount: (
|
|
278
|
-
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(
|
|
279
439
|
({ _id }) => variantComponent._id === _id
|
|
280
440
|
)
|
|
281
441
|
});
|
|
@@ -283,25 +443,18 @@ function renderComponent({
|
|
|
283
443
|
);
|
|
284
444
|
} else if (RenderComponent) {
|
|
285
445
|
const props = convertComponentToProps(component);
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
return /* @__PURE__ */ import_react2.default.createElement(UniformComponent, { key, data: component, resolveRenderer }, !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ import_react2.default.createElement(
|
|
289
|
-
"script",
|
|
446
|
+
return /* @__PURE__ */ import_react5.default.createElement(UniformComponent, { key, data: component, resolveRenderer }, /* @__PURE__ */ import_react5.default.createElement(
|
|
447
|
+
ContextualEditingComponentWrapper,
|
|
290
448
|
{
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
"data-is-placeholder": isPlaceholder ? "true" : void 0,
|
|
301
|
-
"data-is-localized": ((_a = component.parameters) == null ? void 0 : _a[import_canvas3.CANVAS_LOCALE_TAG_PARAM]) ? "true" : void 0,
|
|
302
|
-
"data-component-title": (_d = (_c = (_b = component.parameters) == null ? void 0 : _b.title) == null ? void 0 : _c.value) != null ? _d : ""
|
|
303
|
-
}
|
|
304
|
-
), 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
|
+
));
|
|
305
458
|
} else if (process.env.NODE_ENV !== "production") {
|
|
306
459
|
console.warn(
|
|
307
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.
|
|
@@ -314,9 +467,9 @@ function renderComponent({
|
|
|
314
467
|
var Slot = UniformSlot;
|
|
315
468
|
|
|
316
469
|
// src/components/UniformComponent.tsx
|
|
317
|
-
var UniformComponentContext = (0,
|
|
470
|
+
var UniformComponentContext = (0, import_react6.createContext)({});
|
|
318
471
|
function useUniformCurrentComponent() {
|
|
319
|
-
return (0,
|
|
472
|
+
return (0, import_react6.useContext)(UniformComponentContext);
|
|
320
473
|
}
|
|
321
474
|
function UniformComponent({
|
|
322
475
|
data,
|
|
@@ -338,7 +491,7 @@ function UniformComponent({
|
|
|
338
491
|
resolveRenderer: resolveRenderer || parentData.resolveRenderer || componentStoreResolver,
|
|
339
492
|
behaviorTracking: (_a = behaviorTracking != null ? behaviorTracking : parentData == null ? void 0 : parentData.behaviorTracking) != null ? _a : "onView"
|
|
340
493
|
};
|
|
341
|
-
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;
|
|
342
495
|
const TrackComponent = contextValue.behaviorTracking === "onLoad" ? import_context_react2.TrackFragment : import_context_react2.Track;
|
|
343
496
|
const resolvedChildren = resolveChildren({
|
|
344
497
|
children,
|
|
@@ -346,9 +499,9 @@ function UniformComponent({
|
|
|
346
499
|
hasParentLayout: Boolean(parentData.data),
|
|
347
500
|
resolveRenderer: contextValue.resolveRenderer
|
|
348
501
|
});
|
|
349
|
-
return /* @__PURE__ */
|
|
502
|
+
return /* @__PURE__ */ import_react6.default.createElement(UniformComponentContext.Provider, { value: contextValue }, contextContextProviderPresent ? (
|
|
350
503
|
/* auto-track behavior signals when in a Canvas context */
|
|
351
|
-
/* @__PURE__ */
|
|
504
|
+
/* @__PURE__ */ import_react6.default.createElement(TrackComponent, { behavior: enrichmentTags }, resolvedChildren)
|
|
352
505
|
) : resolvedChildren);
|
|
353
506
|
}
|
|
354
507
|
function resolveChildren({
|
|
@@ -361,150 +514,41 @@ function resolveChildren({
|
|
|
361
514
|
if (!children && !hasParentLayout) {
|
|
362
515
|
const topLevelComponent = resolveRenderer({ type: data.type });
|
|
363
516
|
if (topLevelComponent) {
|
|
364
|
-
children =
|
|
517
|
+
children = import_react6.default.createElement(topLevelComponent, convertComponentToProps(data));
|
|
365
518
|
} else {
|
|
366
519
|
if (Object.keys((_a = data.slots) != null ? _a : {}).length > 1 && process.env.NODE_ENV === "development") {
|
|
367
520
|
console.warn(
|
|
368
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.`
|
|
369
522
|
);
|
|
370
523
|
}
|
|
371
|
-
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 }));
|
|
372
525
|
}
|
|
373
526
|
}
|
|
374
527
|
const renderChildren = typeof children === "function" ? children(convertComponentToProps(data)) : children;
|
|
375
528
|
return renderChildren;
|
|
376
529
|
}
|
|
377
530
|
|
|
378
|
-
// src/components/UniformComposition.tsx
|
|
379
|
-
var import_react5 = __toESM(require("react"));
|
|
380
|
-
|
|
381
|
-
// src/hooks/useUniformContextualEditing.ts
|
|
382
|
-
var import_canvas5 = require("@uniformdev/canvas");
|
|
383
|
-
var import_react4 = require("react");
|
|
384
|
-
var createApiEnhancer = import_canvas5.createUniformApiEnhancer;
|
|
385
|
-
var registeredCompositionIds = /* @__PURE__ */ new Set();
|
|
386
|
-
var useUniformContextualEditing = ({
|
|
387
|
-
initialCompositionValue,
|
|
388
|
-
enhance = (message) => message.composition
|
|
389
|
-
}) => {
|
|
390
|
-
const [contextualComposition, setContextualComposition] = (0, import_react4.useState)();
|
|
391
|
-
const channel = (0, import_react4.useMemo)(() => {
|
|
392
|
-
var _a;
|
|
393
|
-
if (!isInContextEditingMode()) {
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
const channel2 = (0, import_canvas5.createCanvasChannel)({
|
|
397
|
-
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
398
|
-
listenTo: [window]
|
|
399
|
-
});
|
|
400
|
-
return channel2;
|
|
401
|
-
}, []);
|
|
402
|
-
(0, import_react4.useEffect)(() => {
|
|
403
|
-
if (!channel || registeredCompositionIds.has(initialCompositionValue == null ? void 0 : initialCompositionValue._id)) {
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
const unsubscribe = channel.on("update-composition", async (message) => {
|
|
407
|
-
if (!(0, import_canvas5.isUpdateCompositionMessage)(message)) {
|
|
408
|
-
return;
|
|
409
|
-
}
|
|
410
|
-
const enhancedComposition = await enhance(message);
|
|
411
|
-
setContextualComposition(enhancedComposition);
|
|
412
|
-
});
|
|
413
|
-
registeredCompositionIds.add(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
414
|
-
return () => {
|
|
415
|
-
unsubscribe();
|
|
416
|
-
registeredCompositionIds.delete(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
417
|
-
};
|
|
418
|
-
}, [channel, enhance, initialCompositionValue == null ? void 0 : initialCompositionValue._id]);
|
|
419
|
-
(0, import_react4.useEffect)(() => {
|
|
420
|
-
if (!isInContextEditingMode()) {
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
const existingScript = document.getElementById(import_canvas5.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID);
|
|
424
|
-
if (existingScript) {
|
|
425
|
-
return;
|
|
426
|
-
}
|
|
427
|
-
window.__UNIFORM_CONTEXTUAL_EDITING__ = {
|
|
428
|
-
framework: "React"
|
|
429
|
-
};
|
|
430
|
-
const script = document.createElement("script");
|
|
431
|
-
script.id = import_canvas5.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID;
|
|
432
|
-
script.src = getCanvasInContextEmbedScriptUrl();
|
|
433
|
-
script.async = true;
|
|
434
|
-
document.head.appendChild(script);
|
|
435
|
-
}, []);
|
|
436
|
-
return {
|
|
437
|
-
composition: contextualComposition != null ? contextualComposition : initialCompositionValue
|
|
438
|
-
};
|
|
439
|
-
};
|
|
440
|
-
function getCanvasInContextEmbedScriptUrl() {
|
|
441
|
-
const scriptUrl = `${window.document.referrer}files/canvas-in-context-embed/index.js`;
|
|
442
|
-
return scriptUrl;
|
|
443
|
-
}
|
|
444
|
-
function isInContextEditingMode() {
|
|
445
|
-
if (typeof window === "undefined") {
|
|
446
|
-
return false;
|
|
447
|
-
}
|
|
448
|
-
const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
|
|
449
|
-
import_canvas5.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
|
|
450
|
-
);
|
|
451
|
-
const isAllowlistedReferrer = Boolean(
|
|
452
|
-
window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
|
|
453
|
-
);
|
|
454
|
-
return isOpenedByInContextEditor && isAllowlistedReferrer;
|
|
455
|
-
}
|
|
456
|
-
var useContextualEditing = useUniformContextualEditing;
|
|
457
|
-
|
|
458
|
-
// src/components/UniformComposition.tsx
|
|
459
|
-
var UniformCompositionContext = (0, import_react5.createContext)({ data: void 0 });
|
|
460
|
-
function useUniformCurrentComposition() {
|
|
461
|
-
return (0, import_react5.useContext)(UniformCompositionContext);
|
|
462
|
-
}
|
|
463
|
-
function UniformComposition({
|
|
464
|
-
data,
|
|
465
|
-
behaviorTracking = "onView",
|
|
466
|
-
children,
|
|
467
|
-
resolveRenderer,
|
|
468
|
-
contextualEditingEnhancer
|
|
469
|
-
}) {
|
|
470
|
-
const { composition } = useUniformContextualEditing({
|
|
471
|
-
initialCompositionValue: data,
|
|
472
|
-
enhance: contextualEditingEnhancer
|
|
473
|
-
});
|
|
474
|
-
return /* @__PURE__ */ import_react5.default.createElement(UniformCompositionContext.Provider, { value: { data: composition, behaviorTracking, resolveRenderer } }, /* @__PURE__ */ import_react5.default.createElement(
|
|
475
|
-
UniformComponent,
|
|
476
|
-
{
|
|
477
|
-
data: composition,
|
|
478
|
-
behaviorTracking,
|
|
479
|
-
resolveRenderer
|
|
480
|
-
},
|
|
481
|
-
children
|
|
482
|
-
));
|
|
483
|
-
}
|
|
484
|
-
var useComposition = useUniformCurrentComposition;
|
|
485
|
-
var Composition = UniformComposition;
|
|
486
|
-
|
|
487
531
|
// src/hooks/useCompositionEventEffect.ts
|
|
488
|
-
var
|
|
489
|
-
var
|
|
532
|
+
var import_canvas7 = require("@uniformdev/canvas");
|
|
533
|
+
var import_react7 = require("react");
|
|
490
534
|
function useCompositionEventEffect({
|
|
491
535
|
enabled,
|
|
492
536
|
projectId,
|
|
493
537
|
compositionId,
|
|
494
538
|
effect
|
|
495
539
|
}) {
|
|
496
|
-
(0,
|
|
540
|
+
(0, import_react7.useEffect)(() => {
|
|
497
541
|
if (!enabled || !compositionId || !projectId) {
|
|
498
542
|
return;
|
|
499
543
|
}
|
|
500
544
|
let goodbye = void 0;
|
|
501
545
|
const loadEffect = async () => {
|
|
502
|
-
const eventBus = await (0,
|
|
546
|
+
const eventBus = await (0, import_canvas7.createEventBus)();
|
|
503
547
|
if (eventBus) {
|
|
504
|
-
goodbye = (0,
|
|
548
|
+
goodbye = (0, import_canvas7.subscribeToComposition)({
|
|
505
549
|
eventBus,
|
|
506
550
|
compositionId,
|
|
507
|
-
compositionState:
|
|
551
|
+
compositionState: import_canvas7.CANVAS_DRAFT_STATE,
|
|
508
552
|
projectId,
|
|
509
553
|
callback: effect,
|
|
510
554
|
event: "updated"
|