@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.mjs
CHANGED
|
@@ -36,7 +36,7 @@ import {
|
|
|
36
36
|
CANVAS_ENRICHMENT_TAG_PARAM
|
|
37
37
|
} from "@uniformdev/canvas";
|
|
38
38
|
import { Track, TrackFragment, useUniformContext } from "@uniformdev/context-react";
|
|
39
|
-
import
|
|
39
|
+
import React6, { createContext as createContext2, useContext as useContext2 } from "react";
|
|
40
40
|
|
|
41
41
|
// src/convertComponentToProps.ts
|
|
42
42
|
function convertComponentToProps(component) {
|
|
@@ -70,7 +70,8 @@ var createComponentStore = () => {
|
|
|
70
70
|
};
|
|
71
71
|
var createComponentStoreResolver = ({ store, defaultComponent = DefaultNotImplementedComponent }) => {
|
|
72
72
|
return (component) => {
|
|
73
|
-
|
|
73
|
+
var _a;
|
|
74
|
+
const resolved = (_a = store.get(getTypeWithVariant(component.type, component.variant))) != null ? _a : store.get(getTypeWithVariant(component.type));
|
|
74
75
|
return resolved || defaultComponent;
|
|
75
76
|
};
|
|
76
77
|
};
|
|
@@ -94,15 +95,12 @@ var componentStoreResolver = createComponentStoreResolver({
|
|
|
94
95
|
|
|
95
96
|
// src/components/UniformSlot.tsx
|
|
96
97
|
import {
|
|
97
|
-
CANVAS_LOCALE_TAG_PARAM,
|
|
98
98
|
CANVAS_PERSONALIZE_SLOT,
|
|
99
99
|
CANVAS_PERSONALIZE_TYPE,
|
|
100
100
|
CANVAS_TEST_SLOT,
|
|
101
|
-
CANVAS_TEST_TYPE
|
|
102
|
-
IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,
|
|
103
|
-
PLACEHOLDER_ID
|
|
101
|
+
CANVAS_TEST_TYPE
|
|
104
102
|
} from "@uniformdev/canvas";
|
|
105
|
-
import
|
|
103
|
+
import React5 from "react";
|
|
106
104
|
|
|
107
105
|
// src/defaultSystemComponentResolver.tsx
|
|
108
106
|
import {
|
|
@@ -145,6 +143,178 @@ var defaultSystemComponentResolver = {
|
|
|
145
143
|
}
|
|
146
144
|
};
|
|
147
145
|
|
|
146
|
+
// src/components/ContextualEditingComponentWrapper.tsx
|
|
147
|
+
import {
|
|
148
|
+
CANVAS_LOCALE_TAG_PARAM,
|
|
149
|
+
IN_CONTEXT_EDITOR_COMPONENT_END_ROLE,
|
|
150
|
+
IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,
|
|
151
|
+
PLACEHOLDER_ID
|
|
152
|
+
} from "@uniformdev/canvas";
|
|
153
|
+
import React4 from "react";
|
|
154
|
+
|
|
155
|
+
// src/components/UniformComposition.tsx
|
|
156
|
+
import React3, { createContext, useContext } from "react";
|
|
157
|
+
|
|
158
|
+
// src/hooks/useUniformContextualEditing.ts
|
|
159
|
+
import {
|
|
160
|
+
createCanvasChannel,
|
|
161
|
+
createUniformApiEnhancer,
|
|
162
|
+
EMPTY_COMPOSITION,
|
|
163
|
+
IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID,
|
|
164
|
+
IN_CONTEXT_EDITOR_QUERY_STRING_PARAM,
|
|
165
|
+
isUpdateCompositionMessage
|
|
166
|
+
} from "@uniformdev/canvas";
|
|
167
|
+
import { useEffect, useMemo, useState } from "react";
|
|
168
|
+
var createApiEnhancer = createUniformApiEnhancer;
|
|
169
|
+
var registeredCompositionIds = /* @__PURE__ */ new Set();
|
|
170
|
+
var useUniformContextualEditing = ({
|
|
171
|
+
initialCompositionValue,
|
|
172
|
+
enhance = (message) => message.composition
|
|
173
|
+
}) => {
|
|
174
|
+
const [contextualComposition, setContextualComposition] = useState();
|
|
175
|
+
const channel = useMemo(() => {
|
|
176
|
+
var _a;
|
|
177
|
+
if (!isInContextEditingMode()) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const channel2 = createCanvasChannel({
|
|
181
|
+
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
182
|
+
listenTo: [window]
|
|
183
|
+
});
|
|
184
|
+
return channel2;
|
|
185
|
+
}, []);
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
if ((contextualComposition == null ? void 0 : contextualComposition._id) && (initialCompositionValue == null ? void 0 : initialCompositionValue._id) !== EMPTY_COMPOSITION._id && (initialCompositionValue == null ? void 0 : initialCompositionValue._id) !== (contextualComposition == null ? void 0 : contextualComposition._id)) {
|
|
188
|
+
setContextualComposition(void 0);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (!channel || registeredCompositionIds.has(initialCompositionValue == null ? void 0 : initialCompositionValue._id)) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const unsubscribeFromCompositionUpdates = channel.on("update-composition", async (message) => {
|
|
195
|
+
if (!isUpdateCompositionMessage(message)) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const enhancedComposition = await enhance(message);
|
|
199
|
+
setContextualComposition(enhancedComposition);
|
|
200
|
+
});
|
|
201
|
+
registeredCompositionIds.add(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
202
|
+
return () => {
|
|
203
|
+
unsubscribeFromCompositionUpdates();
|
|
204
|
+
registeredCompositionIds.delete(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
205
|
+
};
|
|
206
|
+
}, [channel, enhance, initialCompositionValue == null ? void 0 : initialCompositionValue._id, contextualComposition == null ? void 0 : contextualComposition._id]);
|
|
207
|
+
useEffect(() => {
|
|
208
|
+
if (!isInContextEditingMode()) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const existingScript = document.getElementById(IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID);
|
|
212
|
+
if (existingScript) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
window.__UNIFORM_CONTEXTUAL_EDITING__ = {
|
|
216
|
+
framework: "React",
|
|
217
|
+
// Make sure to also update the value in canvas-vue
|
|
218
|
+
version: 1
|
|
219
|
+
};
|
|
220
|
+
const script = document.createElement("script");
|
|
221
|
+
script.id = IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID;
|
|
222
|
+
script.src = getCanvasInContextEmbedScriptUrl();
|
|
223
|
+
script.async = true;
|
|
224
|
+
document.head.appendChild(script);
|
|
225
|
+
}, [initialCompositionValue == null ? void 0 : initialCompositionValue._id]);
|
|
226
|
+
return {
|
|
227
|
+
composition: contextualComposition != null ? contextualComposition : initialCompositionValue,
|
|
228
|
+
isContextualEditing: Boolean(contextualComposition)
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
function getCanvasInContextEmbedScriptUrl() {
|
|
232
|
+
const scriptUrl = `${window.document.referrer}files/canvas-in-context-embed/index.js`;
|
|
233
|
+
return scriptUrl;
|
|
234
|
+
}
|
|
235
|
+
function isInContextEditingMode() {
|
|
236
|
+
if (typeof window === "undefined") {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
|
|
240
|
+
IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
|
|
241
|
+
);
|
|
242
|
+
const isAllowlistedReferrer = Boolean(
|
|
243
|
+
window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
|
|
244
|
+
);
|
|
245
|
+
return isOpenedByInContextEditor && isAllowlistedReferrer;
|
|
246
|
+
}
|
|
247
|
+
var useContextualEditing = useUniformContextualEditing;
|
|
248
|
+
|
|
249
|
+
// src/components/UniformComposition.tsx
|
|
250
|
+
var UniformCompositionContext = createContext({
|
|
251
|
+
data: void 0,
|
|
252
|
+
isContextualEditing: false
|
|
253
|
+
});
|
|
254
|
+
function useUniformCurrentComposition() {
|
|
255
|
+
return useContext(UniformCompositionContext);
|
|
256
|
+
}
|
|
257
|
+
function UniformComposition({
|
|
258
|
+
data,
|
|
259
|
+
behaviorTracking = "onView",
|
|
260
|
+
children,
|
|
261
|
+
resolveRenderer,
|
|
262
|
+
contextualEditingEnhancer
|
|
263
|
+
}) {
|
|
264
|
+
const { composition, isContextualEditing } = useUniformContextualEditing({
|
|
265
|
+
initialCompositionValue: data,
|
|
266
|
+
enhance: contextualEditingEnhancer
|
|
267
|
+
});
|
|
268
|
+
return /* @__PURE__ */ React3.createElement(
|
|
269
|
+
UniformCompositionContext.Provider,
|
|
270
|
+
{
|
|
271
|
+
value: { data: composition, behaviorTracking, resolveRenderer, isContextualEditing }
|
|
272
|
+
},
|
|
273
|
+
/* @__PURE__ */ React3.createElement(ContextualEditingComponentWrapper, { component: composition }, /* @__PURE__ */ React3.createElement(
|
|
274
|
+
UniformComponent,
|
|
275
|
+
{
|
|
276
|
+
data: composition,
|
|
277
|
+
behaviorTracking,
|
|
278
|
+
resolveRenderer
|
|
279
|
+
},
|
|
280
|
+
children
|
|
281
|
+
))
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
var useComposition = useUniformCurrentComposition;
|
|
285
|
+
var Composition = UniformComposition;
|
|
286
|
+
|
|
287
|
+
// src/components/ContextualEditingComponentWrapper.tsx
|
|
288
|
+
function ContextualEditingComponentWrapper({
|
|
289
|
+
component,
|
|
290
|
+
parentComponent,
|
|
291
|
+
slotName,
|
|
292
|
+
indexInSlot,
|
|
293
|
+
slotChildrenCount,
|
|
294
|
+
emptyPlaceholder,
|
|
295
|
+
children
|
|
296
|
+
}) {
|
|
297
|
+
var _a, _b, _c, _d;
|
|
298
|
+
const isPlaceholder = (component == null ? void 0 : component._id) === PLACEHOLDER_ID;
|
|
299
|
+
const { isContextualEditing } = useUniformCurrentComposition();
|
|
300
|
+
return !isContextualEditing ? /* @__PURE__ */ React4.createElement(React4.Fragment, null, children) : /* @__PURE__ */ React4.createElement(React4.Fragment, null, /* @__PURE__ */ React4.createElement(
|
|
301
|
+
"script",
|
|
302
|
+
{
|
|
303
|
+
"data-role": IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,
|
|
304
|
+
"data-parent-id": parentComponent == null ? void 0 : parentComponent._id,
|
|
305
|
+
"data-parent-type": parentComponent == null ? void 0 : parentComponent.type,
|
|
306
|
+
"data-component-id": component == null ? void 0 : component._id,
|
|
307
|
+
"data-slot-name": slotName != null ? slotName : "",
|
|
308
|
+
"data-component-index": indexInSlot != null ? indexInSlot : "",
|
|
309
|
+
"data-total-components": slotChildrenCount != null ? slotChildrenCount : "",
|
|
310
|
+
"data-component-name": component == null ? void 0 : component.type,
|
|
311
|
+
"data-is-placeholder": isPlaceholder ? "true" : void 0,
|
|
312
|
+
"data-is-localized": ((_a = component == null ? void 0 : component.parameters) == null ? void 0 : _a[CANVAS_LOCALE_TAG_PARAM]) ? "true" : void 0,
|
|
313
|
+
"data-component-title": (_d = (_c = (_b = component == null ? void 0 : component.parameters) == null ? void 0 : _b.title) == null ? void 0 : _c.value) != null ? _d : ""
|
|
314
|
+
}
|
|
315
|
+
), isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder : children, /* @__PURE__ */ React4.createElement("script", { "data-role": IN_CONTEXT_EDITOR_COMPONENT_END_ROLE }));
|
|
316
|
+
}
|
|
317
|
+
|
|
148
318
|
// src/components/UniformSlot.tsx
|
|
149
319
|
function UniformSlot({
|
|
150
320
|
name,
|
|
@@ -182,9 +352,9 @@ function UniformSlot({
|
|
|
182
352
|
emptyPlaceholder
|
|
183
353
|
});
|
|
184
354
|
const elements = children ? children({ child, component, key: `wrapped-inner-${index}` }) : child;
|
|
185
|
-
return
|
|
355
|
+
return React5.createElement(React5.Fragment, { key: index }, elements);
|
|
186
356
|
});
|
|
187
|
-
return
|
|
357
|
+
return React5.createElement(React5.Fragment, void 0, finalChildren);
|
|
188
358
|
}
|
|
189
359
|
function renderComponent({
|
|
190
360
|
component,
|
|
@@ -197,14 +367,13 @@ function renderComponent({
|
|
|
197
367
|
slotChildrenCount,
|
|
198
368
|
emptyPlaceholder
|
|
199
369
|
}) {
|
|
200
|
-
var _a, _b, _c, _d;
|
|
201
370
|
const RenderComponent = resolveRenderer == null ? void 0 : resolveRenderer(component);
|
|
202
371
|
if (component.type === CANVAS_TEST_TYPE) {
|
|
203
372
|
return resolveSystem.test(
|
|
204
373
|
component,
|
|
205
374
|
key,
|
|
206
375
|
(variantComponent, key2) => {
|
|
207
|
-
var
|
|
376
|
+
var _a, _b;
|
|
208
377
|
return renderComponent({
|
|
209
378
|
component: variantComponent,
|
|
210
379
|
resolveRenderer,
|
|
@@ -212,8 +381,8 @@ function renderComponent({
|
|
|
212
381
|
key: key2,
|
|
213
382
|
parentComponent: component,
|
|
214
383
|
slotName: CANVAS_TEST_SLOT,
|
|
215
|
-
slotChildrenCount: (
|
|
216
|
-
indexInSlot: (
|
|
384
|
+
slotChildrenCount: (_a = component == null ? void 0 : component.slots) == null ? void 0 : _a[CANVAS_TEST_SLOT].length,
|
|
385
|
+
indexInSlot: (_b = component == null ? void 0 : component.slots) == null ? void 0 : _b[CANVAS_TEST_SLOT].findIndex(
|
|
217
386
|
({ _id }) => variantComponent._id === _id
|
|
218
387
|
)
|
|
219
388
|
});
|
|
@@ -224,7 +393,7 @@ function renderComponent({
|
|
|
224
393
|
component,
|
|
225
394
|
key,
|
|
226
395
|
(variantComponent, key2) => {
|
|
227
|
-
var
|
|
396
|
+
var _a, _b;
|
|
228
397
|
return renderComponent({
|
|
229
398
|
component: variantComponent,
|
|
230
399
|
resolveRenderer,
|
|
@@ -232,8 +401,8 @@ function renderComponent({
|
|
|
232
401
|
key: key2,
|
|
233
402
|
parentComponent: component,
|
|
234
403
|
slotName: CANVAS_PERSONALIZE_SLOT,
|
|
235
|
-
slotChildrenCount: (
|
|
236
|
-
indexInSlot: (
|
|
404
|
+
slotChildrenCount: (_a = component == null ? void 0 : component.slots) == null ? void 0 : _a[CANVAS_PERSONALIZE_SLOT].length,
|
|
405
|
+
indexInSlot: (_b = component == null ? void 0 : component.slots) == null ? void 0 : _b[CANVAS_PERSONALIZE_SLOT].findIndex(
|
|
237
406
|
({ _id }) => variantComponent._id === _id
|
|
238
407
|
)
|
|
239
408
|
});
|
|
@@ -241,25 +410,18 @@ function renderComponent({
|
|
|
241
410
|
);
|
|
242
411
|
} else if (RenderComponent) {
|
|
243
412
|
const props = convertComponentToProps(component);
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
return /* @__PURE__ */ React3.createElement(UniformComponent, { key, data: component, resolveRenderer }, !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ React3.createElement(
|
|
247
|
-
"script",
|
|
413
|
+
return /* @__PURE__ */ React5.createElement(UniformComponent, { key, data: component, resolveRenderer }, /* @__PURE__ */ React5.createElement(
|
|
414
|
+
ContextualEditingComponentWrapper,
|
|
248
415
|
{
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
"data-is-placeholder": isPlaceholder ? "true" : void 0,
|
|
259
|
-
"data-is-localized": ((_a = component.parameters) == null ? void 0 : _a[CANVAS_LOCALE_TAG_PARAM]) ? "true" : void 0,
|
|
260
|
-
"data-component-title": (_d = (_c = (_b = component.parameters) == null ? void 0 : _b.title) == null ? void 0 : _c.value) != null ? _d : ""
|
|
261
|
-
}
|
|
262
|
-
), isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder : /* @__PURE__ */ React3.createElement(RenderComponent, { ...props }), !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ React3.createElement("script", { "data-role": "component-end" }));
|
|
416
|
+
component,
|
|
417
|
+
parentComponent,
|
|
418
|
+
slotName,
|
|
419
|
+
indexInSlot,
|
|
420
|
+
slotChildrenCount,
|
|
421
|
+
emptyPlaceholder
|
|
422
|
+
},
|
|
423
|
+
/* @__PURE__ */ React5.createElement(RenderComponent, { ...props })
|
|
424
|
+
));
|
|
263
425
|
} else if (process.env.NODE_ENV !== "production") {
|
|
264
426
|
console.warn(
|
|
265
427
|
`[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.
|
|
@@ -269,11 +431,12 @@ function renderComponent({
|
|
|
269
431
|
}
|
|
270
432
|
return null;
|
|
271
433
|
}
|
|
434
|
+
var Slot = UniformSlot;
|
|
272
435
|
|
|
273
436
|
// src/components/UniformComponent.tsx
|
|
274
|
-
var UniformComponentContext =
|
|
437
|
+
var UniformComponentContext = createContext2({});
|
|
275
438
|
function useUniformCurrentComponent() {
|
|
276
|
-
return
|
|
439
|
+
return useContext2(UniformComponentContext);
|
|
277
440
|
}
|
|
278
441
|
function UniformComponent({
|
|
279
442
|
data,
|
|
@@ -303,9 +466,9 @@ function UniformComponent({
|
|
|
303
466
|
hasParentLayout: Boolean(parentData.data),
|
|
304
467
|
resolveRenderer: contextValue.resolveRenderer
|
|
305
468
|
});
|
|
306
|
-
return /* @__PURE__ */
|
|
469
|
+
return /* @__PURE__ */ React6.createElement(UniformComponentContext.Provider, { value: contextValue }, contextContextProviderPresent ? (
|
|
307
470
|
/* auto-track behavior signals when in a Canvas context */
|
|
308
|
-
/* @__PURE__ */
|
|
471
|
+
/* @__PURE__ */ React6.createElement(TrackComponent, { behavior: enrichmentTags }, resolvedChildren)
|
|
309
472
|
) : resolvedChildren);
|
|
310
473
|
}
|
|
311
474
|
function resolveChildren({
|
|
@@ -318,131 +481,20 @@ function resolveChildren({
|
|
|
318
481
|
if (!children && !hasParentLayout) {
|
|
319
482
|
const topLevelComponent = resolveRenderer({ type: data.type });
|
|
320
483
|
if (topLevelComponent) {
|
|
321
|
-
children =
|
|
484
|
+
children = React6.createElement(topLevelComponent, convertComponentToProps(data));
|
|
322
485
|
} else {
|
|
323
486
|
if (Object.keys((_a = data.slots) != null ? _a : {}).length > 1 && process.env.NODE_ENV === "development") {
|
|
324
487
|
console.warn(
|
|
325
|
-
`[canvas-dev] All the slots in component '${data.type}' are rendered in no particular order. Use '<
|
|
488
|
+
`[canvas-dev] All the slots in component '${data.type}' are rendered in no particular order. Use '<Slot name={slotName} />' to reliably render the slots.`
|
|
326
489
|
);
|
|
327
490
|
}
|
|
328
|
-
children = Object.keys(data.slots || {}).map((slotName) => /* @__PURE__ */
|
|
491
|
+
children = Object.keys(data.slots || {}).map((slotName) => /* @__PURE__ */ React6.createElement(UniformSlot, { key: slotName, name: slotName }));
|
|
329
492
|
}
|
|
330
493
|
}
|
|
331
494
|
const renderChildren = typeof children === "function" ? children(convertComponentToProps(data)) : children;
|
|
332
495
|
return renderChildren;
|
|
333
496
|
}
|
|
334
497
|
|
|
335
|
-
// src/components/UniformComposition.tsx
|
|
336
|
-
import React5, { createContext as createContext2, useContext as useContext2 } from "react";
|
|
337
|
-
|
|
338
|
-
// src/hooks/useUniformContextualEditing.ts
|
|
339
|
-
import {
|
|
340
|
-
createCanvasChannel,
|
|
341
|
-
createUniformApiEnhancer,
|
|
342
|
-
IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID,
|
|
343
|
-
IN_CONTEXT_EDITOR_QUERY_STRING_PARAM,
|
|
344
|
-
isUpdateCompositionMessage
|
|
345
|
-
} from "@uniformdev/canvas";
|
|
346
|
-
import { useEffect, useMemo, useState } from "react";
|
|
347
|
-
var registeredCompositionIds = /* @__PURE__ */ new Set();
|
|
348
|
-
var useUniformContextualEditing = ({
|
|
349
|
-
initialCompositionValue,
|
|
350
|
-
enhance = (message) => message.composition
|
|
351
|
-
}) => {
|
|
352
|
-
const [contextualComposition, setContextualComposition] = useState();
|
|
353
|
-
const channel = useMemo(() => {
|
|
354
|
-
var _a;
|
|
355
|
-
if (!isInContextEditingMode()) {
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
const channel2 = createCanvasChannel({
|
|
359
|
-
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
360
|
-
listenTo: [window]
|
|
361
|
-
});
|
|
362
|
-
return channel2;
|
|
363
|
-
}, []);
|
|
364
|
-
useEffect(() => {
|
|
365
|
-
if (!channel || registeredCompositionIds.has(initialCompositionValue == null ? void 0 : initialCompositionValue._id)) {
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
const unsubscribe = channel.on("update-composition", async (message) => {
|
|
369
|
-
if (!isUpdateCompositionMessage(message)) {
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
const enhancedComposition = await enhance(message);
|
|
373
|
-
setContextualComposition(enhancedComposition);
|
|
374
|
-
});
|
|
375
|
-
registeredCompositionIds.add(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
376
|
-
return () => {
|
|
377
|
-
unsubscribe();
|
|
378
|
-
registeredCompositionIds.delete(initialCompositionValue == null ? void 0 : initialCompositionValue._id);
|
|
379
|
-
};
|
|
380
|
-
}, [channel, enhance, initialCompositionValue == null ? void 0 : initialCompositionValue._id]);
|
|
381
|
-
useEffect(() => {
|
|
382
|
-
if (!isInContextEditingMode()) {
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
const existingScript = document.getElementById(IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID);
|
|
386
|
-
if (existingScript) {
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
window.__UNIFORM_CONTEXTUAL_EDITING__ = {
|
|
390
|
-
framework: "React"
|
|
391
|
-
};
|
|
392
|
-
const script = document.createElement("script");
|
|
393
|
-
script.id = IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID;
|
|
394
|
-
script.src = getCanvasInContextEmbedScriptUrl();
|
|
395
|
-
script.async = true;
|
|
396
|
-
document.head.appendChild(script);
|
|
397
|
-
}, []);
|
|
398
|
-
return {
|
|
399
|
-
composition: contextualComposition != null ? contextualComposition : initialCompositionValue
|
|
400
|
-
};
|
|
401
|
-
};
|
|
402
|
-
function getCanvasInContextEmbedScriptUrl() {
|
|
403
|
-
const scriptUrl = `${window.document.referrer}files/canvas-in-context-embed/index.js`;
|
|
404
|
-
return scriptUrl;
|
|
405
|
-
}
|
|
406
|
-
function isInContextEditingMode() {
|
|
407
|
-
if (typeof window === "undefined") {
|
|
408
|
-
return false;
|
|
409
|
-
}
|
|
410
|
-
const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
|
|
411
|
-
IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
|
|
412
|
-
);
|
|
413
|
-
const isAllowlistedReferrer = Boolean(
|
|
414
|
-
window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
|
|
415
|
-
);
|
|
416
|
-
return isOpenedByInContextEditor && isAllowlistedReferrer;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// src/components/UniformComposition.tsx
|
|
420
|
-
var UniformCompositionContext = createContext2({ data: void 0 });
|
|
421
|
-
function useUniformCurrentComposition() {
|
|
422
|
-
return useContext2(UniformCompositionContext);
|
|
423
|
-
}
|
|
424
|
-
function UniformComposition({
|
|
425
|
-
data,
|
|
426
|
-
behaviorTracking = "onView",
|
|
427
|
-
children,
|
|
428
|
-
resolveRenderer,
|
|
429
|
-
contextualEditingEnhancer
|
|
430
|
-
}) {
|
|
431
|
-
const { composition } = useUniformContextualEditing({
|
|
432
|
-
initialCompositionValue: data,
|
|
433
|
-
enhance: contextualEditingEnhancer
|
|
434
|
-
});
|
|
435
|
-
return /* @__PURE__ */ React5.createElement(UniformCompositionContext.Provider, { value: { data: composition, behaviorTracking, resolveRenderer } }, /* @__PURE__ */ React5.createElement(
|
|
436
|
-
UniformComponent,
|
|
437
|
-
{
|
|
438
|
-
data: composition,
|
|
439
|
-
behaviorTracking,
|
|
440
|
-
resolveRenderer
|
|
441
|
-
},
|
|
442
|
-
children
|
|
443
|
-
));
|
|
444
|
-
}
|
|
445
|
-
|
|
446
498
|
// src/hooks/useCompositionEventEffect.ts
|
|
447
499
|
import {
|
|
448
500
|
CANVAS_DRAFT_STATE,
|
|
@@ -483,18 +535,23 @@ function useCompositionEventEffect({
|
|
|
483
535
|
}, [compositionId, enabled, projectId, effect]);
|
|
484
536
|
}
|
|
485
537
|
export {
|
|
538
|
+
Composition,
|
|
486
539
|
DefaultNotImplementedComponent,
|
|
487
540
|
NOT_IMPLEMENTED_COMPONENT,
|
|
541
|
+
Slot,
|
|
488
542
|
UniformComponent,
|
|
489
543
|
UniformComposition,
|
|
490
544
|
UniformSlot,
|
|
491
545
|
componentStore,
|
|
492
546
|
componentStoreResolver,
|
|
547
|
+
createApiEnhancer,
|
|
493
548
|
createComponentStore,
|
|
494
549
|
createComponentStoreResolver,
|
|
495
550
|
createUniformApiEnhancer,
|
|
496
551
|
registerUniformComponent,
|
|
552
|
+
useComposition,
|
|
497
553
|
useCompositionEventEffect,
|
|
554
|
+
useContextualEditing,
|
|
498
555
|
useUniformContextualEditing,
|
|
499
556
|
useUniformCurrentComponent,
|
|
500
557
|
useUniformCurrentComposition
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniformdev/canvas-react",
|
|
3
|
-
"version": "18.
|
|
3
|
+
"version": "18.34.0",
|
|
4
4
|
"description": "React SDK for Uniform Canvas",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"document": "api-extractor run --local"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@uniformdev/canvas": "18.
|
|
28
|
-
"@uniformdev/context": "18.
|
|
29
|
-
"@uniformdev/context-react": "18.
|
|
27
|
+
"@uniformdev/canvas": "18.34.0",
|
|
28
|
+
"@uniformdev/context": "18.34.0",
|
|
29
|
+
"@uniformdev/context-react": "18.34.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": ">= 16 || 17 || 18",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "563ab31aa4308102c988bea321028e73efe836d0"
|
|
47
47
|
}
|