@uniformdev/canvas-react 18.0.1-alpha.7 → 18.1.2-alpha.4
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 +105 -62
- package/dist/index.esm.js +171 -149
- package/dist/index.js +183 -159
- package/dist/index.mjs +171 -149
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -29,22 +29,61 @@ __export(src_exports, {
|
|
|
29
29
|
Composition: () => Composition,
|
|
30
30
|
DefaultNotImplementedComponent: () => DefaultNotImplementedComponent,
|
|
31
31
|
Slot: () => Slot,
|
|
32
|
+
UniformComponent: () => UniformComponent,
|
|
33
|
+
UniformComposition: () => UniformComposition,
|
|
34
|
+
UniformSlot: () => UniformSlot,
|
|
32
35
|
componentStore: () => componentStore,
|
|
33
36
|
componentStoreResolver: () => componentStoreResolver,
|
|
34
37
|
createApiEnhancer: () => createApiEnhancer,
|
|
35
38
|
createComponentStore: () => createComponentStore,
|
|
36
39
|
createComponentStoreResolver: () => createComponentStoreResolver,
|
|
40
|
+
createUniformApiEnhancer: () => import_canvas5.createUniformApiEnhancer,
|
|
37
41
|
registerUniformComponent: () => registerUniformComponent,
|
|
38
42
|
useComposition: () => useComposition,
|
|
39
43
|
useCompositionEventEffect: () => useCompositionEventEffect,
|
|
40
|
-
|
|
44
|
+
useUniformContextualEditing: () => useUniformContextualEditing,
|
|
45
|
+
useUniformCurrentComponent: () => useUniformCurrentComponent,
|
|
46
|
+
useUniformCurrentComposition: () => useUniformCurrentComposition
|
|
41
47
|
});
|
|
42
48
|
module.exports = __toCommonJS(src_exports);
|
|
43
49
|
|
|
44
|
-
// src/components/
|
|
45
|
-
var
|
|
50
|
+
// src/components/DefaultNotImplementedComponent.tsx
|
|
51
|
+
var import_canvas = require("@uniformdev/canvas");
|
|
52
|
+
var import_react = __toESM(require("react"));
|
|
53
|
+
var wrapperStyles = {
|
|
54
|
+
borderLeft: "4px solid #e42535",
|
|
55
|
+
padding: "16px",
|
|
56
|
+
fontSize: "16px",
|
|
57
|
+
borderRadius: "0 8px 8px 0",
|
|
58
|
+
margin: "8px",
|
|
59
|
+
backgroundColor: "rgba(255, 255, 255, 0.45)",
|
|
60
|
+
color: "#1d3557"
|
|
61
|
+
};
|
|
62
|
+
function DefaultNotImplementedComponent(props) {
|
|
63
|
+
var _a;
|
|
64
|
+
const componentType = (_a = props.component) == null ? void 0 : _a.type;
|
|
65
|
+
if (!componentType) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (componentType === import_canvas.CANVAS_LOCALIZATION_TYPE) {
|
|
69
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { style: wrapperStyles }, /* @__PURE__ */ import_react.default.createElement("p", null, "Seems like localization is not enabled in your application. Please read our documentation on how to", " ", /* @__PURE__ */ import_react.default.createElement(
|
|
70
|
+
"a",
|
|
71
|
+
{
|
|
72
|
+
href: "https://docs.uniform.app/guides/composition/localization#activate-front-end",
|
|
73
|
+
target: "_blank",
|
|
74
|
+
style: { fontWeight: "bolder", textDecoration: "underline" },
|
|
75
|
+
rel: "noreferrer"
|
|
76
|
+
},
|
|
77
|
+
"enable localization in your front-end application."
|
|
78
|
+
)));
|
|
79
|
+
}
|
|
80
|
+
return /* @__PURE__ */ import_react.default.createElement("div", { style: wrapperStyles }, /* @__PURE__ */ import_react.default.createElement("h2", null, "Component: ", /* @__PURE__ */ import_react.default.createElement("code", null, componentType)), /* @__PURE__ */ import_react.default.createElement("p", null, /* @__PURE__ */ import_react.default.createElement("code", null, /* @__PURE__ */ import_react.default.createElement("strong", null, componentType)), " ", "has no React implementation. It may need to be added to your ", /* @__PURE__ */ import_react.default.createElement("code", null, "resolveRenderer()"), " function."), /* @__PURE__ */ import_react.default.createElement("details", null, /* @__PURE__ */ import_react.default.createElement("summary", { style: { cursor: "pointer" } }, "Props"), /* @__PURE__ */ import_react.default.createElement("pre", null, JSON.stringify(props, null, 2))));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/components/UniformComponent.tsx
|
|
84
|
+
var import_canvas4 = require("@uniformdev/canvas");
|
|
46
85
|
var import_context_react2 = require("@uniformdev/context-react");
|
|
47
|
-
var
|
|
86
|
+
var import_react3 = __toESM(require("react"));
|
|
48
87
|
|
|
49
88
|
// src/convertComponentToProps.ts
|
|
50
89
|
function convertComponentToProps(component) {
|
|
@@ -104,22 +143,22 @@ var componentStoreResolver = createComponentStoreResolver({
|
|
|
104
143
|
store: componentStore
|
|
105
144
|
});
|
|
106
145
|
|
|
107
|
-
// src/components/
|
|
108
|
-
var
|
|
109
|
-
var
|
|
146
|
+
// src/components/UniformSlot.tsx
|
|
147
|
+
var import_canvas3 = require("@uniformdev/canvas");
|
|
148
|
+
var import_react2 = __toESM(require("react"));
|
|
110
149
|
|
|
111
150
|
// src/defaultSystemComponentResolver.tsx
|
|
112
|
-
var
|
|
151
|
+
var import_canvas2 = require("@uniformdev/canvas");
|
|
113
152
|
var import_context_react = require("@uniformdev/context-react");
|
|
114
|
-
var
|
|
153
|
+
var React2 = __toESM(require("react"));
|
|
115
154
|
var defaultSystemComponentResolver = {
|
|
116
155
|
test: (component, key, renderChild) => {
|
|
117
156
|
var _a, _b, _c, _d, _e;
|
|
118
157
|
const testComponent = component;
|
|
119
158
|
const variants = (_b = (_a = testComponent.slots) == null ? void 0 : _a.test) != null ? _b : [];
|
|
120
159
|
const testName = (_e = (_d = (_c = testComponent.parameters) == null ? void 0 : _c.test) == null ? void 0 : _d.value) != null ? _e : "Untitled Test";
|
|
121
|
-
const finalVariants = (0,
|
|
122
|
-
return /* @__PURE__ */
|
|
160
|
+
const finalVariants = (0, import_canvas2.mapSlotToTestVariations)(variants);
|
|
161
|
+
return /* @__PURE__ */ React2.createElement(
|
|
123
162
|
import_context_react.Test,
|
|
124
163
|
{
|
|
125
164
|
key,
|
|
@@ -132,8 +171,8 @@ var defaultSystemComponentResolver = {
|
|
|
132
171
|
personalization: (component, key, renderChild) => {
|
|
133
172
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
134
173
|
const pzComponent = component;
|
|
135
|
-
const processedVariants = (0,
|
|
136
|
-
return /* @__PURE__ */
|
|
174
|
+
const processedVariants = (0, import_canvas2.mapSlotToPersonalizedVariations)((_a = pzComponent == null ? void 0 : pzComponent.slots) == null ? void 0 : _a.pz);
|
|
175
|
+
return /* @__PURE__ */ React2.createElement(
|
|
137
176
|
import_context_react.Personalize,
|
|
138
177
|
{
|
|
139
178
|
key,
|
|
@@ -146,28 +185,24 @@ var defaultSystemComponentResolver = {
|
|
|
146
185
|
}
|
|
147
186
|
};
|
|
148
187
|
|
|
149
|
-
// src/components/
|
|
150
|
-
function
|
|
188
|
+
// src/components/UniformSlot.tsx
|
|
189
|
+
function UniformSlot({
|
|
151
190
|
name,
|
|
152
191
|
resolveRenderer,
|
|
153
192
|
children,
|
|
154
193
|
emptyPlaceholder
|
|
155
194
|
}) {
|
|
156
195
|
var _a;
|
|
157
|
-
const
|
|
158
|
-
if (!
|
|
159
|
-
throw new Error("Slot
|
|
160
|
-
}
|
|
161
|
-
const { composition: layout, resolveRenderer: parentResolveRenderer } = compositionContext;
|
|
162
|
-
if (!layout) {
|
|
163
|
-
throw new Error("Cannot use Slot without a Composition wrapper.");
|
|
196
|
+
const { data: parentData, resolveRenderer: parentResolveRenderer } = useUniformCurrentComponent();
|
|
197
|
+
if (!parentData) {
|
|
198
|
+
throw new Error("Cannot use Slot without a <UniformComponent /> wrapper.");
|
|
164
199
|
}
|
|
165
|
-
const slot = (_a =
|
|
200
|
+
const slot = (_a = parentData.slots) == null ? void 0 : _a[name];
|
|
166
201
|
if (!slot || !Array.isArray(slot)) {
|
|
167
202
|
if (process.env.NODE_ENV === "development") {
|
|
168
203
|
console.warn(
|
|
169
204
|
`[canvas-dev] slot '${name}' was rendered, but it was not defined on its component. This is expected if the slot is optional, otherwise it may indicate a typo. Component:`,
|
|
170
|
-
|
|
205
|
+
parentData
|
|
171
206
|
);
|
|
172
207
|
}
|
|
173
208
|
return null;
|
|
@@ -182,14 +217,14 @@ function Slot({
|
|
|
182
217
|
key: `inner-${index}`,
|
|
183
218
|
indexInSlot: index,
|
|
184
219
|
slotName: name,
|
|
185
|
-
parentComponent:
|
|
220
|
+
parentComponent: parentData,
|
|
186
221
|
slotChildrenCount: slot.length,
|
|
187
222
|
emptyPlaceholder
|
|
188
223
|
});
|
|
189
224
|
const elements = children ? children({ child, component, key: `wrapped-inner-${index}` }) : child;
|
|
190
|
-
return
|
|
225
|
+
return import_react2.default.createElement(import_react2.default.Fragment, { key: index }, elements);
|
|
191
226
|
});
|
|
192
|
-
return
|
|
227
|
+
return import_react2.default.createElement(import_react2.default.Fragment, void 0, finalChildren);
|
|
193
228
|
}
|
|
194
229
|
function renderComponent({
|
|
195
230
|
component,
|
|
@@ -204,13 +239,13 @@ function renderComponent({
|
|
|
204
239
|
}) {
|
|
205
240
|
var _a, _b, _c, _d;
|
|
206
241
|
const RenderComponent = resolveRenderer == null ? void 0 : resolveRenderer(component);
|
|
207
|
-
if (component.type ===
|
|
242
|
+
if (component.type === import_canvas3.CANVAS_TEST_TYPE) {
|
|
208
243
|
return resolveSystem.test(
|
|
209
244
|
component,
|
|
210
245
|
key,
|
|
211
246
|
(component2, key2) => renderComponent({ component: component2, resolveRenderer, resolveSystem, key: key2 })
|
|
212
247
|
);
|
|
213
|
-
} else if (component.type ===
|
|
248
|
+
} else if (component.type === import_canvas3.CANVAS_PERSONALIZE_TYPE) {
|
|
214
249
|
return resolveSystem.personalization(
|
|
215
250
|
component,
|
|
216
251
|
key,
|
|
@@ -219,12 +254,12 @@ function renderComponent({
|
|
|
219
254
|
} else if (RenderComponent) {
|
|
220
255
|
const props = convertComponentToProps(component);
|
|
221
256
|
const shouldRenderContextualEditingTags = Boolean(component._id);
|
|
222
|
-
const isPlaceholder = component._id ===
|
|
223
|
-
return /* @__PURE__ */
|
|
257
|
+
const isPlaceholder = component._id === import_canvas3.PLACEHOLDER_ID;
|
|
258
|
+
return /* @__PURE__ */ import_react2.default.createElement(UniformComponent, { key, data: component, resolveRenderer }, !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ import_react2.default.createElement(
|
|
224
259
|
"script",
|
|
225
260
|
{
|
|
226
261
|
key,
|
|
227
|
-
"data-role":
|
|
262
|
+
"data-role": import_canvas3.IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,
|
|
228
263
|
"data-parent-id": parentComponent == null ? void 0 : parentComponent._id,
|
|
229
264
|
"data-parent-type": parentComponent == null ? void 0 : parentComponent.type,
|
|
230
265
|
"data-component-id": component._id,
|
|
@@ -233,10 +268,10 @@ function renderComponent({
|
|
|
233
268
|
"data-total-components": slotChildrenCount != null ? slotChildrenCount : "",
|
|
234
269
|
"data-component-name": component.type,
|
|
235
270
|
"data-is-placeholder": isPlaceholder ? "true" : void 0,
|
|
236
|
-
"data-is-localized": ((_a = component.parameters) == null ? void 0 : _a[
|
|
271
|
+
"data-is-localized": ((_a = component.parameters) == null ? void 0 : _a[import_canvas3.CANVAS_LOCALE_TAG_PARAM]) ? "true" : void 0,
|
|
237
272
|
"data-component-title": (_d = (_c = (_b = component.parameters) == null ? void 0 : _b.title) == null ? void 0 : _c.value) != null ? _d : ""
|
|
238
273
|
}
|
|
239
|
-
), isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder : /* @__PURE__ */
|
|
274
|
+
), isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder : /* @__PURE__ */ import_react2.default.createElement(RenderComponent, { ...props }), !shouldRenderContextualEditingTags ? null : /* @__PURE__ */ import_react2.default.createElement("script", { "data-role": "component-end" }));
|
|
240
275
|
} else if (process.env.NODE_ENV !== "production") {
|
|
241
276
|
console.warn(
|
|
242
277
|
`[canvas] found component of type '${component.type}' which the resolveRenderer prop returned no component for. Nothing will be rendered. The resolveRenderer function may need to be extended to handle the new type.`,
|
|
@@ -245,44 +280,45 @@ function renderComponent({
|
|
|
245
280
|
}
|
|
246
281
|
return null;
|
|
247
282
|
}
|
|
283
|
+
var Slot = UniformSlot;
|
|
248
284
|
|
|
249
|
-
// src/components/
|
|
250
|
-
var
|
|
251
|
-
function
|
|
252
|
-
return (0,
|
|
285
|
+
// src/components/UniformComponent.tsx
|
|
286
|
+
var UniformComponentContext = (0, import_react3.createContext)({});
|
|
287
|
+
function useUniformCurrentComponent() {
|
|
288
|
+
return (0, import_react3.useContext)(UniformComponentContext);
|
|
253
289
|
}
|
|
254
290
|
var componentStoreResolver2 = (component) => {
|
|
255
291
|
const resolved = componentStore.get(component.type);
|
|
256
292
|
return resolved || null;
|
|
257
293
|
};
|
|
258
|
-
function
|
|
294
|
+
function UniformComponent({
|
|
259
295
|
data,
|
|
260
296
|
resolveRenderer,
|
|
261
297
|
children,
|
|
262
298
|
behaviorTracking
|
|
263
299
|
}) {
|
|
264
300
|
var _a, _b, _c;
|
|
265
|
-
const
|
|
301
|
+
const parentData = useUniformCurrentComponent();
|
|
266
302
|
const contextContextProviderPresent = (0, import_context_react2.useUniformContext)({ throwOnMissingProvider: false }) !== void 0;
|
|
267
303
|
if (!data) {
|
|
268
304
|
if (process.env.NODE_ENV === "development") {
|
|
269
|
-
console.warn(`[canvas-dev]
|
|
305
|
+
console.warn(`[canvas-dev] UniformComponent was rendered with no data, nothing will be output.`);
|
|
270
306
|
}
|
|
271
307
|
return null;
|
|
272
308
|
}
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
resolveRenderer: resolveRenderer || (
|
|
276
|
-
behaviorTracking: (_a = behaviorTracking != null ? behaviorTracking :
|
|
309
|
+
const contextValue = {
|
|
310
|
+
data,
|
|
311
|
+
resolveRenderer: resolveRenderer || (parentData == null ? void 0 : parentData.resolveRenderer) || componentStoreResolver2,
|
|
312
|
+
behaviorTracking: (_a = behaviorTracking != null ? behaviorTracking : parentData == null ? void 0 : parentData.behaviorTracking) != null ? _a : "onView"
|
|
277
313
|
};
|
|
278
|
-
const enrichmentTags = (_c = (_b = data.parameters) == null ? void 0 : _b[
|
|
279
|
-
const TrackComponent =
|
|
314
|
+
const enrichmentTags = (_c = (_b = data.parameters) == null ? void 0 : _b[import_canvas4.CANVAS_ENRICHMENT_TAG_PARAM]) == null ? void 0 : _c.value;
|
|
315
|
+
const TrackComponent = contextValue.behaviorTracking === "onLoad" ? import_context_react2.TrackFragment : import_context_react2.Track;
|
|
280
316
|
const resolvedChildren = resolveChildren({
|
|
281
317
|
children,
|
|
282
318
|
data,
|
|
283
|
-
hasParentLayout: Boolean(
|
|
319
|
+
hasParentLayout: Boolean(parentData)
|
|
284
320
|
});
|
|
285
|
-
return /* @__PURE__ */
|
|
321
|
+
return /* @__PURE__ */ import_react3.default.createElement(UniformComponentContext.Provider, { value: contextValue }, contextContextProviderPresent ? /* @__PURE__ */ import_react3.default.createElement(TrackComponent, { behavior: enrichmentTags }, resolvedChildren) : resolvedChildren);
|
|
286
322
|
}
|
|
287
323
|
function resolveChildren({
|
|
288
324
|
children,
|
|
@@ -290,157 +326,74 @@ function resolveChildren({
|
|
|
290
326
|
hasParentLayout
|
|
291
327
|
}) {
|
|
292
328
|
var _a;
|
|
293
|
-
|
|
294
|
-
if (!compositionChildren && !hasParentLayout) {
|
|
329
|
+
if (!children && !hasParentLayout) {
|
|
295
330
|
const rootComponent = componentStore.get(data.type);
|
|
296
331
|
if (rootComponent) {
|
|
297
|
-
|
|
332
|
+
children = import_react3.default.createElement(rootComponent, convertComponentToProps(data));
|
|
298
333
|
} else {
|
|
299
334
|
if (Object.keys((_a = data.slots) != null ? _a : {}).length > 1 && process.env.NODE_ENV === "development") {
|
|
300
335
|
console.warn(
|
|
301
336
|
`[canvas-dev] All the slots in component '${data.type}' are rendered in no particular order. Use '<Slot name={slotName} />' to reliably render the slots.`
|
|
302
337
|
);
|
|
303
338
|
}
|
|
304
|
-
|
|
339
|
+
children = Object.keys(data.slots || {}).map((slotName) => /* @__PURE__ */ import_react3.default.createElement(UniformSlot, { key: slotName, name: slotName }));
|
|
305
340
|
}
|
|
306
341
|
}
|
|
307
|
-
const renderChildren = typeof
|
|
342
|
+
const renderChildren = typeof children === "function" ? children(convertComponentToProps(data)) : children;
|
|
308
343
|
return renderChildren;
|
|
309
344
|
}
|
|
310
345
|
|
|
311
|
-
// src/components/
|
|
312
|
-
var
|
|
313
|
-
var import_react3 = __toESM(require("react"));
|
|
314
|
-
var wrapperStyles = {
|
|
315
|
-
borderLeft: "4px solid #e42535",
|
|
316
|
-
padding: "16px",
|
|
317
|
-
fontSize: "16px",
|
|
318
|
-
borderRadius: "0 8px 8px 0",
|
|
319
|
-
margin: "8px",
|
|
320
|
-
backgroundColor: "rgba(255, 255, 255, 0.45)",
|
|
321
|
-
color: "#1d3557"
|
|
322
|
-
};
|
|
323
|
-
function DefaultNotImplementedComponent(props) {
|
|
324
|
-
var _a;
|
|
325
|
-
const componentType = (_a = props.component) == null ? void 0 : _a.type;
|
|
326
|
-
if (!componentType) {
|
|
327
|
-
return null;
|
|
328
|
-
}
|
|
329
|
-
if (componentType === import_canvas4.CANVAS_LOCALIZATION_TYPE) {
|
|
330
|
-
return /* @__PURE__ */ import_react3.default.createElement("div", { style: wrapperStyles }, /* @__PURE__ */ import_react3.default.createElement("p", null, "Seems like localization is not enabled in your application. Please read our documentation on how to", " ", /* @__PURE__ */ import_react3.default.createElement(
|
|
331
|
-
"a",
|
|
332
|
-
{
|
|
333
|
-
href: "https://docs.uniform.app/guides/composition/localization#activate-front-end",
|
|
334
|
-
target: "_blank",
|
|
335
|
-
style: { fontWeight: "bolder", textDecoration: "underline" },
|
|
336
|
-
rel: "noreferrer"
|
|
337
|
-
},
|
|
338
|
-
"enable localization in your front-end application."
|
|
339
|
-
)));
|
|
340
|
-
}
|
|
341
|
-
return /* @__PURE__ */ import_react3.default.createElement("div", { style: wrapperStyles }, /* @__PURE__ */ import_react3.default.createElement("h2", null, "Component: ", /* @__PURE__ */ import_react3.default.createElement("code", null, componentType)), /* @__PURE__ */ import_react3.default.createElement("p", null, /* @__PURE__ */ import_react3.default.createElement("code", null, /* @__PURE__ */ import_react3.default.createElement("strong", null, componentType)), " ", "has no React implementation. It may need to be added to your ", /* @__PURE__ */ import_react3.default.createElement("code", null, "resolveRenderer()"), " function."), /* @__PURE__ */ import_react3.default.createElement("details", null, /* @__PURE__ */ import_react3.default.createElement("summary", { style: { cursor: "pointer" } }, "Props"), /* @__PURE__ */ import_react3.default.createElement("pre", null, JSON.stringify(props, null, 2))));
|
|
342
|
-
}
|
|
346
|
+
// src/components/UniformComposition.tsx
|
|
347
|
+
var import_react5 = __toESM(require("react"));
|
|
343
348
|
|
|
344
|
-
// src/hooks/
|
|
349
|
+
// src/hooks/useUniformContextualEditing.ts
|
|
345
350
|
var import_canvas5 = require("@uniformdev/canvas");
|
|
346
351
|
var import_react4 = require("react");
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
compositionId,
|
|
351
|
-
effect
|
|
352
|
-
}) {
|
|
353
|
-
(0, import_react4.useEffect)(() => {
|
|
354
|
-
if (!enabled || !compositionId || !projectId) {
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
let goodbye = void 0;
|
|
358
|
-
const loadEffect = async () => {
|
|
359
|
-
const eventBus = await (0, import_canvas5.createEventBus)();
|
|
360
|
-
if (eventBus) {
|
|
361
|
-
goodbye = (0, import_canvas5.subscribeToComposition)({
|
|
362
|
-
eventBus,
|
|
363
|
-
compositionId,
|
|
364
|
-
compositionState: import_canvas5.CANVAS_DRAFT_STATE,
|
|
365
|
-
projectId,
|
|
366
|
-
callback: effect,
|
|
367
|
-
event: "updated"
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
loadEffect();
|
|
372
|
-
return () => {
|
|
373
|
-
if (goodbye) {
|
|
374
|
-
goodbye();
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
}, [compositionId, enabled, projectId, effect]);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// src/hooks/useContextualEditing.ts
|
|
381
|
-
var import_canvas6 = require("@uniformdev/canvas");
|
|
382
|
-
var import_react5 = require("react");
|
|
383
|
-
var previewScriptId = "uniform-canvas-preview-script";
|
|
384
|
-
var createApiEnhancer = ({ apiUrl }) => {
|
|
385
|
-
return async (message) => {
|
|
386
|
-
const response = await fetch(apiUrl, {
|
|
387
|
-
method: "post",
|
|
388
|
-
body: JSON.stringify({
|
|
389
|
-
composition: message.composition,
|
|
390
|
-
hash: message.hash
|
|
391
|
-
}),
|
|
392
|
-
headers: {
|
|
393
|
-
"Content-Type": "application/json"
|
|
394
|
-
}
|
|
395
|
-
});
|
|
396
|
-
const json = await response.json();
|
|
397
|
-
if (!response.ok) {
|
|
398
|
-
throw new Error("Error reading enhanced composition");
|
|
399
|
-
}
|
|
400
|
-
const body = json;
|
|
401
|
-
return body.composition;
|
|
402
|
-
};
|
|
403
|
-
};
|
|
404
|
-
var useContextualEditing = ({
|
|
352
|
+
var createApiEnhancer = import_canvas5.createUniformApiEnhancer;
|
|
353
|
+
var registeredCompositionIds = /* @__PURE__ */ new Set();
|
|
354
|
+
var useUniformContextualEditing = ({
|
|
405
355
|
initialCompositionValue,
|
|
406
356
|
enhance = (message) => message.composition
|
|
407
357
|
}) => {
|
|
408
|
-
const [contextualComposition, setContextualComposition] = (0,
|
|
409
|
-
const channel = (0,
|
|
358
|
+
const [contextualComposition, setContextualComposition] = (0, import_react4.useState)();
|
|
359
|
+
const channel = (0, import_react4.useMemo)(() => {
|
|
410
360
|
var _a;
|
|
411
361
|
if (!isInContextEditingMode()) {
|
|
412
362
|
return;
|
|
413
363
|
}
|
|
414
|
-
const channel2 = (0,
|
|
364
|
+
const channel2 = (0, import_canvas5.createCanvasChannel)({
|
|
415
365
|
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
416
366
|
listenTo: [window]
|
|
417
367
|
});
|
|
418
368
|
return channel2;
|
|
419
369
|
}, []);
|
|
420
|
-
(0,
|
|
421
|
-
if (!channel) {
|
|
370
|
+
(0, import_react4.useEffect)(() => {
|
|
371
|
+
if (!channel || registeredCompositionIds.has(initialCompositionValue._id)) {
|
|
422
372
|
return;
|
|
423
373
|
}
|
|
424
374
|
const unsubscribe = channel.on("update-composition", async (message) => {
|
|
425
|
-
if ((0,
|
|
426
|
-
|
|
427
|
-
setContextualComposition(composition);
|
|
375
|
+
if (!(0, import_canvas5.isUpdateCompositionMessage)(message)) {
|
|
376
|
+
return;
|
|
428
377
|
}
|
|
378
|
+
const enhancedComposition = await enhance(message);
|
|
379
|
+
setContextualComposition(enhancedComposition);
|
|
429
380
|
});
|
|
381
|
+
registeredCompositionIds.add(initialCompositionValue._id);
|
|
430
382
|
return () => {
|
|
431
383
|
unsubscribe();
|
|
384
|
+
registeredCompositionIds.delete(initialCompositionValue._id);
|
|
432
385
|
};
|
|
433
|
-
}, [channel, enhance]);
|
|
434
|
-
(0,
|
|
386
|
+
}, [channel, enhance, initialCompositionValue._id]);
|
|
387
|
+
(0, import_react4.useEffect)(() => {
|
|
435
388
|
if (!isInContextEditingMode()) {
|
|
436
389
|
return;
|
|
437
390
|
}
|
|
438
|
-
const existingScript = document.getElementById(
|
|
391
|
+
const existingScript = document.getElementById(import_canvas5.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID);
|
|
439
392
|
if (existingScript) {
|
|
440
393
|
return;
|
|
441
394
|
}
|
|
442
395
|
const script = document.createElement("script");
|
|
443
|
-
script.id =
|
|
396
|
+
script.id = import_canvas5.IN_CONTEXT_EDITOR_EMBED_SCRIPT_ID;
|
|
444
397
|
script.src = getCanvasInContextEmbedScriptUrl();
|
|
445
398
|
script.async = true;
|
|
446
399
|
document.head.appendChild(script);
|
|
@@ -458,25 +411,96 @@ function isInContextEditingMode() {
|
|
|
458
411
|
return false;
|
|
459
412
|
}
|
|
460
413
|
const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
|
|
461
|
-
|
|
414
|
+
import_canvas5.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
|
|
462
415
|
);
|
|
463
416
|
const isAllowlistedReferrer = Boolean(
|
|
464
417
|
window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
|
|
465
418
|
);
|
|
466
419
|
return isOpenedByInContextEditor && isAllowlistedReferrer;
|
|
467
420
|
}
|
|
421
|
+
|
|
422
|
+
// src/components/UniformComposition.tsx
|
|
423
|
+
var UniformCompositionContext = (0, import_react5.createContext)({});
|
|
424
|
+
function useUniformCurrentComposition() {
|
|
425
|
+
return (0, import_react5.useContext)(UniformCompositionContext);
|
|
426
|
+
}
|
|
427
|
+
function UniformComposition({
|
|
428
|
+
data,
|
|
429
|
+
behaviorTracking = "onView",
|
|
430
|
+
children,
|
|
431
|
+
resolveRenderer,
|
|
432
|
+
contextualEditingEnhancer
|
|
433
|
+
}) {
|
|
434
|
+
const { composition } = useUniformContextualEditing({
|
|
435
|
+
initialCompositionValue: data,
|
|
436
|
+
enhance: contextualEditingEnhancer
|
|
437
|
+
});
|
|
438
|
+
return /* @__PURE__ */ import_react5.default.createElement(UniformCompositionContext.Provider, { value: { data: composition, behaviorTracking, resolveRenderer } }, /* @__PURE__ */ import_react5.default.createElement(
|
|
439
|
+
UniformComponent,
|
|
440
|
+
{
|
|
441
|
+
data: composition,
|
|
442
|
+
behaviorTracking,
|
|
443
|
+
resolveRenderer
|
|
444
|
+
},
|
|
445
|
+
children
|
|
446
|
+
));
|
|
447
|
+
}
|
|
448
|
+
var useComposition = useUniformCurrentComposition;
|
|
449
|
+
var Composition = UniformComposition;
|
|
450
|
+
|
|
451
|
+
// src/hooks/useCompositionEventEffect.ts
|
|
452
|
+
var import_canvas6 = require("@uniformdev/canvas");
|
|
453
|
+
var import_react6 = require("react");
|
|
454
|
+
function useCompositionEventEffect({
|
|
455
|
+
enabled,
|
|
456
|
+
projectId,
|
|
457
|
+
compositionId,
|
|
458
|
+
effect
|
|
459
|
+
}) {
|
|
460
|
+
(0, import_react6.useEffect)(() => {
|
|
461
|
+
if (!enabled || !compositionId || !projectId) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
let goodbye = void 0;
|
|
465
|
+
const loadEffect = async () => {
|
|
466
|
+
const eventBus = await (0, import_canvas6.createEventBus)();
|
|
467
|
+
if (eventBus) {
|
|
468
|
+
goodbye = (0, import_canvas6.subscribeToComposition)({
|
|
469
|
+
eventBus,
|
|
470
|
+
compositionId,
|
|
471
|
+
compositionState: import_canvas6.CANVAS_DRAFT_STATE,
|
|
472
|
+
projectId,
|
|
473
|
+
callback: effect,
|
|
474
|
+
event: "updated"
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
loadEffect();
|
|
479
|
+
return () => {
|
|
480
|
+
if (goodbye) {
|
|
481
|
+
goodbye();
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
}, [compositionId, enabled, projectId, effect]);
|
|
485
|
+
}
|
|
468
486
|
// Annotate the CommonJS export names for ESM import in node:
|
|
469
487
|
0 && (module.exports = {
|
|
470
488
|
Composition,
|
|
471
489
|
DefaultNotImplementedComponent,
|
|
472
490
|
Slot,
|
|
491
|
+
UniformComponent,
|
|
492
|
+
UniformComposition,
|
|
493
|
+
UniformSlot,
|
|
473
494
|
componentStore,
|
|
474
495
|
componentStoreResolver,
|
|
475
496
|
createApiEnhancer,
|
|
476
497
|
createComponentStore,
|
|
477
498
|
createComponentStoreResolver,
|
|
499
|
+
createUniformApiEnhancer,
|
|
478
500
|
registerUniformComponent,
|
|
479
501
|
useComposition,
|
|
480
502
|
useCompositionEventEffect,
|
|
481
|
-
|
|
503
|
+
useUniformContextualEditing,
|
|
504
|
+
useUniformCurrentComponent,
|
|
505
|
+
useUniformCurrentComposition
|
|
482
506
|
});
|