@uniformdev/canvas-vue 17.7.0 → 17.7.1-alpha.167

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.js CHANGED
@@ -1 +1,435 @@
1
- "use strict";var b=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var L=(e,t)=>{for(var o in t)b(e,o,{get:t[o],enumerable:!0})},z=(e,t,o,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of j(t))!M.call(e,n)&&n!==o&&b(e,n,{get:()=>t[n],enumerable:!(s=V(t,n))||s.enumerable});return e};var D=e=>z(b({},"__esModule",{value:!0}),e);var Z={};L(Z,{CANVAS_COMPOSITION_TYPE:()=>w,CANVAS_SLOT_CONTENT_TYPE:()=>g,Composition:()=>x,DefaultNotImplementedComponent:()=>B,SlotContent:()=>F,compositionInjectionKey:()=>R,convertComponentToProps:()=>N,createApiEnhancer:()=>G,resolveRendererInjectionKey:()=>h,useCompositionEventEffect:()=>q,useContextualEditing:()=>H});module.exports=D(Z);var O=require("@uniformdev/canvas"),P=require("@uniformdev/context-vue"),p=require("vue-demi");var w="Composition",g="SlotContent",R="uniformComposition",h="uniformResolveRenderer";var x=(0,p.defineComponent)({name:w,inheritAttrs:!1,props:{data:{type:Object,required:!0},resolveRenderer:{type:Function,default:()=>({type:e})=>(0,p.resolveComponent)(e)},behaviorTracking:{type:String,default:"onView"}},setup(e,t){var l,i,T;let o=(0,p.computed)(()=>{var d,f;return(f=e.data)!=null?f:(d=(0,p.inject)(R))==null?void 0:d.value}),s=(l=e.resolveRenderer)!=null?l:(0,p.inject)(h);(0,p.provide)(R,o),(0,p.provide)(h,s);let n=(0,p.computed)(()=>{var d,f;return(f=(d=o.value)==null?void 0:d.slots)!=null?f:{}}),r=(0,p.computed)(()=>{var d,f;return(f=(d=o.value)==null?void 0:d.parameters)!=null?f:{}}),m=(0,p.computed)(()=>JSON.stringify(o.value)),c=(T=(i=o.value.parameters)==null?void 0:i[O.CANVAS_ENRICHMENT_TAG_PARAM])==null?void 0:T.value,a=()=>{var d,f;return(f=(d=t.slots).default)==null?void 0:f.call(d,{slots:n.value,parameters:r.value})};if((0,P.isUsingUniformContext)()){let d=e.behaviorTracking==="onLoad"?P.TrackSlot:P.Track;return()=>(0,p.h)(d,{behavior:c,key:m.value},a)}return a}});var C=require("vue-demi"),K={borderLeft:"10px solid #e42535",background:"rgba(122, 215, 218, 0.3)",color:"#1d3557",padding:"1rem",textAlign:"left",margin:"1rem 5vw"},Y={margin:"0 0 1rem"},B=(0,C.defineComponent)({name:"DefaultNotImplementedComponent",props:{component:{type:Object,required:!0}},setup(e,{attrs:t}){var s;let o=(s=e.component)==null?void 0:s.type;return()=>(0,C.h)("div",{key:"content",style:{...K}},[(0,C.h)("h2",{style:{...Y}},`Component: ${o}`),(0,C.h)("p",[(0,C.h)("strong",`${o} has no Vue implementation. It may need to be added to your `),(0,C.h)("code",{},"resolveRenderer()"),(0,C.h)("strong",{}," function.")]),(0,C.h)("details",{},[(0,C.h)("summary",{},"props/attributes"),(0,C.h)("pre",{style:{overflowX:"auto"}},`${JSON.stringify(t)}`)])])}});var y=require("@uniformdev/canvas"),E=require("@uniformdev/context-vue"),u=require("vue-demi");function N(e){var n;let t=(n=e.parameters)!=null?n:{};return{...Object.entries(t).reduce((r,[m,{value:c}])=>({...r,[$(m)]:c}),{}),...e.data,component:e}}function $(e){return e.replace("$","")}var F=(0,u.defineComponent)({name:g,inheritAttrs:!1,props:{name:{type:String},resolveRenderer:{type:Function}},setup(e,t){var c;let o=(0,u.inject)(R),s=(c=e.resolveRenderer)!=null?c:(0,u.inject)(h),n=t.slots.emptyPlaceholder;if(!o||!s)throw new Error("<SlotContent /> can only be used inside a <Composition />");let r=(0,u.computed)(()=>{var a,l,i;return e.name?(l=(a=o==null?void 0:o.value.slots)==null?void 0:a[e.name])!=null?l:[]:Object.values((i=o==null?void 0:o.value.slots)!=null?i:{}).flat()}),m=r.value.map((a,l)=>{let i=_({component:a,resolveRenderer:s,indexInSlot:l,slotName:e.name,parentComponent:o.value,slotChildrenCount:r.value.length,emptyPlaceholder:n});return t.slots.default?t.slots.default({child:i,component:a}):i});return()=>m}});function J(e,t){var r,m,c,a,l,i;let o=e==null?void 0:e.parameters,s=(0,y.mapSlotToPersonalizedVariations)((r=e.slots)==null?void 0:r.pz),n=(a=(c=(m=e.parameters)==null?void 0:m.trackingEventName)==null?void 0:c.value)!=null?a:"Untitled Personalization";return(0,u.h)(E.Personalize,{name:n,component:T=>t(T),variations:s,count:Number((i=(l=o==null?void 0:o.count)==null?void 0:l.value)!=null?i:1)})}function X(e,t){var r,m,c,a,l;let o=(m=(r=e==null?void 0:e.slots)==null?void 0:r.test)!=null?m:[],s=(l=(a=(c=e==null?void 0:e.parameters)==null?void 0:c.test)==null?void 0:a.value)!=null?l:"Untitled Test",n=(0,y.mapSlotToTestVariations)(o);return(0,u.h)(E.Test,{variations:n,name:s,component:i=>t(i)})}function _({component:e,resolveRenderer:t,indexInSlot:o,slotName:s,parentComponent:n,slotChildrenCount:r,emptyPlaceholder:m}){var a,l;if(e.type===y.CANVAS_TEST_TYPE)return X(e,i=>_({component:i,resolveRenderer:t}));if(e.type===y.CANVAS_PERSONALIZE_TYPE)return J(e,i=>_({component:i,resolveRenderer:t}));let c=t==null?void 0:t(e);if(c){let i=N(e),T=Boolean(e._id),d=e._id===y.PLACEHOLDER_ID,f=T?[(0,u.h)("script",{"data-role":y.IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,"data-parent-id":n==null?void 0:n._id,"data-parent-type":n==null?void 0:n.type,"data-component-id":e._id,"data-slot-name":s,"data-component-index":o,"data-total-components":r,"data-component-name":e.type,"data-is-placeholder":d?"true":void 0,"data-component-title":(l=(a=e.parameters)==null?void 0:a.title)==null?void 0:l.value}),d&&m!==void 0?m():(0,u.h)(c,i),(0,u.h)("script",{"data-role":"component-end"})]:[(0,u.h)(c,i)];return(0,u.h)(x,{data:e,resolveRenderer:t},()=>f)}return console.warn(`[canvas] found component of type '${e.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.`,e),(0,u.h)("")}var I=require("@uniformdev/canvas"),A=require("vue-demi");async function q({enabled:e,projectId:t,compositionIdRef:o,effect:s}){let n;(0,A.watch)([()=>e,()=>o.value,()=>t],async()=>{if(n==null||n(),!e||!o.value||!t)return;let r=await(0,I.createEventBus)();r&&(n=(0,I.subscribeToComposition)({eventBus:r,compositionId:o.value,compositionState:I.CANVAS_DRAFT_STATE,projectId:t,callback:s,event:"updated"}))},{immediate:!0})}var S=require("@uniformdev/canvas"),v=require("vue-demi"),k="uniform-canvas-preview-script",G=({apiUrl:e})=>async t=>{let o=await fetch(e,{method:"post",body:JSON.stringify({composition:t.composition,hash:t.hash}),headers:{"Content-Type":"application/json"}}),s=await o.json();if(!o.ok)throw new Error("Error reading enhanced composition");return s.composition},H=({initialCompositionValue:e,enhance:t=o=>o.composition})=>{let o=(0,v.ref)(),s=(0,v.computed)(()=>{var r;return U()?(0,S.createCanvasChannel)({broadcastTo:[(r=window.opener)!=null?r:window.top],listenTo:[window]}):void 0});return(0,v.watch)([s,()=>t],()=>{if(!s.value)return;let n=s.value.on("update-composition",async r=>{if((0,S.isUpdateCompositionMessage)(r)){let m=await t(r);o.value=m}});return()=>{n()}},{immediate:!0}),(0,v.onMounted)(()=>{if(!U()||document.getElementById(k))return;let r=document.createElement("script");r.id=k,r.src=Q(),r.async=!0,document.head.appendChild(r)}),{composition:o!=null?o:e}};function Q(){return`${window.document.referrer}files/canvas-in-context-embed/index.js`}function U(){if(typeof window=="undefined")return!1;let e=new URLSearchParams(window.location.search).has(S.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM),t=Boolean(window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//));return e&&t}0&&(module.exports={CANVAS_COMPOSITION_TYPE,CANVAS_SLOT_CONTENT_TYPE,Composition,DefaultNotImplementedComponent,SlotContent,compositionInjectionKey,convertComponentToProps,createApiEnhancer,resolveRendererInjectionKey,useCompositionEventEffect,useContextualEditing});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ CANVAS_COMPOSITION_TYPE: () => CANVAS_COMPOSITION_TYPE,
24
+ CANVAS_SLOT_CONTENT_TYPE: () => CANVAS_SLOT_CONTENT_TYPE,
25
+ Composition: () => Composition,
26
+ DefaultNotImplementedComponent: () => DefaultNotImplementedComponent,
27
+ SlotContent: () => SlotContent,
28
+ compositionInjectionKey: () => compositionInjectionKey,
29
+ convertComponentToProps: () => convertComponentToProps,
30
+ createApiEnhancer: () => createApiEnhancer,
31
+ resolveRendererInjectionKey: () => resolveRendererInjectionKey,
32
+ useCompositionEventEffect: () => useCompositionEventEffect,
33
+ useContextualEditing: () => useContextualEditing
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+
37
+ // src/components/Composition.ts
38
+ var import_canvas = require("@uniformdev/canvas");
39
+ var import_context_vue = require("@uniformdev/context-vue");
40
+ var import_vue_demi = require("vue-demi");
41
+
42
+ // src/utils/constants.ts
43
+ var CANVAS_COMPOSITION_TYPE = "Composition";
44
+ var CANVAS_SLOT_CONTENT_TYPE = "SlotContent";
45
+ var compositionInjectionKey = "uniformComposition";
46
+ var resolveRendererInjectionKey = "uniformResolveRenderer";
47
+
48
+ // src/components/Composition.ts
49
+ var Composition = (0, import_vue_demi.defineComponent)({
50
+ name: CANVAS_COMPOSITION_TYPE,
51
+ inheritAttrs: false,
52
+ props: {
53
+ data: {
54
+ type: Object,
55
+ required: true
56
+ },
57
+ resolveRenderer: {
58
+ type: Function,
59
+ default: () => ({ type }) => (0, import_vue_demi.resolveComponent)(type)
60
+ },
61
+ behaviorTracking: {
62
+ type: String,
63
+ default: "onView"
64
+ }
65
+ },
66
+ setup(props, context) {
67
+ var _a, _b, _c;
68
+ const data = (0, import_vue_demi.computed)(() => {
69
+ var _a2, _b2;
70
+ return (_b2 = props.data) != null ? _b2 : (_a2 = (0, import_vue_demi.inject)(compositionInjectionKey)) == null ? void 0 : _a2.value;
71
+ });
72
+ const resolveRenderer = (_a = props.resolveRenderer) != null ? _a : (0, import_vue_demi.inject)(resolveRendererInjectionKey);
73
+ (0, import_vue_demi.provide)(compositionInjectionKey, data);
74
+ (0, import_vue_demi.provide)(resolveRendererInjectionKey, resolveRenderer);
75
+ const slots = (0, import_vue_demi.computed)(() => {
76
+ var _a2, _b2;
77
+ return (_b2 = (_a2 = data.value) == null ? void 0 : _a2.slots) != null ? _b2 : {};
78
+ });
79
+ const parameters = (0, import_vue_demi.computed)(() => {
80
+ var _a2, _b2;
81
+ return (_b2 = (_a2 = data.value) == null ? void 0 : _a2.parameters) != null ? _b2 : {};
82
+ });
83
+ const componentKey = (0, import_vue_demi.computed)(() => {
84
+ return JSON.stringify(data.value);
85
+ });
86
+ const enrichmentTags = (_c = (_b = data.value.parameters) == null ? void 0 : _b[import_canvas.CANVAS_ENRICHMENT_TAG_PARAM]) == null ? void 0 : _c.value;
87
+ const renderChildren = () => {
88
+ var _a2, _b2;
89
+ return (_b2 = (_a2 = context.slots).default) == null ? void 0 : _b2.call(_a2, {
90
+ slots: slots.value,
91
+ parameters: parameters.value
92
+ });
93
+ };
94
+ if ((0, import_context_vue.isUsingUniformContext)()) {
95
+ const TrackComponent = props.behaviorTracking === "onLoad" ? import_context_vue.TrackSlot : import_context_vue.Track;
96
+ return () => (0, import_vue_demi.h)(
97
+ TrackComponent,
98
+ { behavior: enrichmentTags, key: componentKey.value },
99
+ renderChildren
100
+ );
101
+ }
102
+ return renderChildren;
103
+ }
104
+ });
105
+
106
+ // src/components/DefaultNotImplementedComponent.ts
107
+ var import_vue_demi2 = require("vue-demi");
108
+ var inlineStyles = {
109
+ borderLeft: "10px solid #e42535",
110
+ background: "rgba(122, 215, 218, 0.3)",
111
+ color: "#1d3557",
112
+ padding: "1rem",
113
+ textAlign: "left",
114
+ margin: "1rem 5vw"
115
+ };
116
+ var headingStyle = {
117
+ margin: "0 0 1rem"
118
+ };
119
+ var DefaultNotImplementedComponent = (0, import_vue_demi2.defineComponent)({
120
+ name: "DefaultNotImplementedComponent",
121
+ props: {
122
+ component: {
123
+ type: Object,
124
+ required: true
125
+ }
126
+ },
127
+ setup(props, { attrs }) {
128
+ var _a;
129
+ const componentType = (_a = props.component) == null ? void 0 : _a.type;
130
+ return () => (0, import_vue_demi2.h)("div", { key: "content", style: { ...inlineStyles } }, [
131
+ (0, import_vue_demi2.h)("h2", { style: { ...headingStyle } }, `Component: ${componentType}`),
132
+ (0, import_vue_demi2.h)("p", [
133
+ (0, import_vue_demi2.h)("strong", `${componentType} has no Vue implementation. It may need to be added to your `),
134
+ (0, import_vue_demi2.h)("code", {}, "resolveRenderer()"),
135
+ (0, import_vue_demi2.h)("strong", {}, " function.")
136
+ ]),
137
+ (0, import_vue_demi2.h)("details", {}, [
138
+ (0, import_vue_demi2.h)("summary", {}, "props/attributes"),
139
+ (0, import_vue_demi2.h)(
140
+ "pre",
141
+ {
142
+ style: {
143
+ overflowX: "auto"
144
+ }
145
+ },
146
+ `${JSON.stringify(attrs)}`
147
+ )
148
+ ])
149
+ ]);
150
+ }
151
+ });
152
+
153
+ // src/components/SlotContent.ts
154
+ var import_canvas2 = require("@uniformdev/canvas");
155
+ var import_context_vue2 = require("@uniformdev/context-vue");
156
+ var import_vue_demi3 = require("vue-demi");
157
+
158
+ // src/utils/convertComponentToProps.ts
159
+ function convertComponentToProps(component) {
160
+ var _a;
161
+ const parameters = (_a = component.parameters) != null ? _a : {};
162
+ const flatParams = Object.entries(parameters).reduce(
163
+ (props, [key, { value }]) => ({
164
+ ...props,
165
+ [normalizePropName(key)]: value
166
+ }),
167
+ {}
168
+ );
169
+ const renderComponentProps = {
170
+ ...flatParams,
171
+ ...component.data,
172
+ component
173
+ };
174
+ return renderComponentProps;
175
+ }
176
+ function normalizePropName(name) {
177
+ return name.replace("$", "");
178
+ }
179
+
180
+ // src/components/SlotContent.ts
181
+ var SlotContent = (0, import_vue_demi3.defineComponent)({
182
+ name: CANVAS_SLOT_CONTENT_TYPE,
183
+ inheritAttrs: false,
184
+ props: {
185
+ name: {
186
+ type: String
187
+ },
188
+ resolveRenderer: {
189
+ type: Function
190
+ }
191
+ },
192
+ setup(props, context) {
193
+ var _a;
194
+ const componentData = (0, import_vue_demi3.inject)(compositionInjectionKey);
195
+ const resolveRenderer = (_a = props.resolveRenderer) != null ? _a : (0, import_vue_demi3.inject)(resolveRendererInjectionKey);
196
+ const emptyPlaceholder = context.slots.emptyPlaceholder;
197
+ if (!componentData || !resolveRenderer) {
198
+ throw new Error("<SlotContent /> can only be used inside a <Composition />");
199
+ }
200
+ const slotItems = (0, import_vue_demi3.computed)(() => {
201
+ var _a2, _b, _c;
202
+ if (props.name) {
203
+ return (_b = (_a2 = componentData == null ? void 0 : componentData.value.slots) == null ? void 0 : _a2[props.name]) != null ? _b : [];
204
+ }
205
+ return Object.values((_c = componentData == null ? void 0 : componentData.value.slots) != null ? _c : {}).flat();
206
+ });
207
+ const childrenToRender = slotItems.value.map((component, index) => {
208
+ const child = renderComponent({
209
+ component,
210
+ resolveRenderer,
211
+ indexInSlot: index,
212
+ slotName: props.name,
213
+ parentComponent: componentData.value,
214
+ slotChildrenCount: slotItems.value.length,
215
+ emptyPlaceholder
216
+ });
217
+ return context.slots.default ? context.slots.default({ child, component }) : child;
218
+ });
219
+ return () => childrenToRender;
220
+ }
221
+ });
222
+ function renderPersonalizeComponent(component, resolveRenderer) {
223
+ var _a, _b, _c, _d, _e, _f;
224
+ const parameters = component == null ? void 0 : component.parameters;
225
+ const processedVariants = (0, import_canvas2.mapSlotToPersonalizedVariations)((_a = component.slots) == null ? void 0 : _a.pz);
226
+ const name = (_d = (_c = (_b = component.parameters) == null ? void 0 : _b.trackingEventName) == null ? void 0 : _c.value) != null ? _d : "Untitled Personalization";
227
+ return (0, import_vue_demi3.h)(import_context_vue2.Personalize, {
228
+ name,
229
+ component: (variant) => resolveRenderer(variant),
230
+ variations: processedVariants,
231
+ count: Number((_f = (_e = parameters == null ? void 0 : parameters.count) == null ? void 0 : _e.value) != null ? _f : 1)
232
+ });
233
+ }
234
+ function renderTestComponent(component, resolveRenderer) {
235
+ var _a, _b, _c, _d, _e;
236
+ const variants = (_b = (_a = component == null ? void 0 : component.slots) == null ? void 0 : _a.test) != null ? _b : [];
237
+ const testName = (_e = (_d = (_c = component == null ? void 0 : component.parameters) == null ? void 0 : _c.test) == null ? void 0 : _d.value) != null ? _e : "Untitled Test";
238
+ const finalVariants = (0, import_canvas2.mapSlotToTestVariations)(variants);
239
+ return (0, import_vue_demi3.h)(import_context_vue2.Test, {
240
+ variations: finalVariants,
241
+ name: testName,
242
+ component: (variant) => resolveRenderer(variant)
243
+ });
244
+ }
245
+ function renderComponent({
246
+ component,
247
+ resolveRenderer,
248
+ indexInSlot,
249
+ slotName,
250
+ parentComponent,
251
+ slotChildrenCount,
252
+ emptyPlaceholder
253
+ }) {
254
+ var _a, _b;
255
+ if (component.type === import_canvas2.CANVAS_TEST_TYPE) {
256
+ return renderTestComponent(
257
+ component,
258
+ (component2) => renderComponent({ component: component2, resolveRenderer })
259
+ );
260
+ }
261
+ if (component.type === import_canvas2.CANVAS_PERSONALIZE_TYPE) {
262
+ return renderPersonalizeComponent(
263
+ component,
264
+ (component2) => renderComponent({ component: component2, resolveRenderer })
265
+ );
266
+ }
267
+ const resolvedComponent = resolveRenderer == null ? void 0 : resolveRenderer(component);
268
+ if (resolvedComponent) {
269
+ const props = convertComponentToProps(component);
270
+ const shouldRenderContextualEditingTags = Boolean(component._id);
271
+ const isPlaceholder = component._id === import_canvas2.PLACEHOLDER_ID;
272
+ const elementsToRender = shouldRenderContextualEditingTags ? [
273
+ (0, import_vue_demi3.h)("script", {
274
+ "data-role": import_canvas2.IN_CONTEXT_EDITOR_COMPONENT_START_ROLE,
275
+ "data-parent-id": parentComponent == null ? void 0 : parentComponent._id,
276
+ "data-parent-type": parentComponent == null ? void 0 : parentComponent.type,
277
+ "data-component-id": component._id,
278
+ "data-slot-name": slotName,
279
+ "data-component-index": indexInSlot,
280
+ "data-total-components": slotChildrenCount,
281
+ "data-component-name": component.type,
282
+ "data-is-placeholder": isPlaceholder ? "true" : void 0,
283
+ "data-component-title": (_b = (_a = component.parameters) == null ? void 0 : _a.title) == null ? void 0 : _b.value
284
+ }),
285
+ isPlaceholder && emptyPlaceholder !== void 0 ? emptyPlaceholder() : (0, import_vue_demi3.h)(resolvedComponent, props),
286
+ (0, import_vue_demi3.h)("script", { "data-role": "component-end" })
287
+ ] : [(0, import_vue_demi3.h)(resolvedComponent, props)];
288
+ return (0, import_vue_demi3.h)(Composition, { data: component, resolveRenderer }, () => elementsToRender);
289
+ }
290
+ console.warn(
291
+ `[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.`,
292
+ component
293
+ );
294
+ return (0, import_vue_demi3.h)("");
295
+ }
296
+
297
+ // src/composables/useCompositionEventEffect.ts
298
+ var import_canvas3 = require("@uniformdev/canvas");
299
+ var import_vue_demi4 = require("vue-demi");
300
+ async function useCompositionEventEffect({
301
+ enabled,
302
+ projectId,
303
+ compositionIdRef,
304
+ effect
305
+ }) {
306
+ let unsubscribe;
307
+ (0, import_vue_demi4.watch)(
308
+ [() => enabled, () => compositionIdRef.value, () => projectId],
309
+ async () => {
310
+ unsubscribe == null ? void 0 : unsubscribe();
311
+ if (!enabled || !compositionIdRef.value || !projectId) {
312
+ return;
313
+ }
314
+ const eventBus = await (0, import_canvas3.createEventBus)();
315
+ if (eventBus) {
316
+ unsubscribe = (0, import_canvas3.subscribeToComposition)({
317
+ eventBus,
318
+ compositionId: compositionIdRef.value,
319
+ compositionState: import_canvas3.CANVAS_DRAFT_STATE,
320
+ projectId,
321
+ callback: effect,
322
+ event: "updated"
323
+ });
324
+ }
325
+ },
326
+ { immediate: true }
327
+ );
328
+ }
329
+
330
+ // src/composables/useContextualEditing.ts
331
+ var import_canvas4 = require("@uniformdev/canvas");
332
+ var import_vue_demi5 = require("vue-demi");
333
+ var previewScriptId = "uniform-canvas-preview-script";
334
+ var createApiEnhancer = ({ apiUrl }) => {
335
+ return async (message) => {
336
+ const response = await fetch(apiUrl, {
337
+ method: "post",
338
+ body: JSON.stringify({
339
+ composition: message.composition,
340
+ hash: message.hash
341
+ }),
342
+ headers: {
343
+ "Content-Type": "application/json"
344
+ }
345
+ });
346
+ const json = await response.json();
347
+ if (!response.ok) {
348
+ throw new Error("Error reading enhanced composition");
349
+ }
350
+ const body = json;
351
+ return body.composition;
352
+ };
353
+ };
354
+ var useContextualEditing = ({
355
+ initialCompositionValue,
356
+ enhance = (message) => message.composition
357
+ }) => {
358
+ const contextualComposition = (0, import_vue_demi5.ref)();
359
+ const channel = (0, import_vue_demi5.computed)(() => {
360
+ var _a;
361
+ if (!isInContextEditingMode()) {
362
+ return;
363
+ }
364
+ const channel2 = (0, import_canvas4.createCanvasChannel)({
365
+ broadcastTo: [(_a = window.opener) != null ? _a : window.top],
366
+ listenTo: [window]
367
+ });
368
+ return channel2;
369
+ });
370
+ (0, import_vue_demi5.watch)(
371
+ [channel, () => enhance],
372
+ () => {
373
+ if (!channel.value) {
374
+ return;
375
+ }
376
+ const unsubscribe = channel.value.on("update-composition", async (message) => {
377
+ if ((0, import_canvas4.isUpdateCompositionMessage)(message)) {
378
+ const composition = await enhance(message);
379
+ contextualComposition.value = composition;
380
+ }
381
+ });
382
+ return () => {
383
+ unsubscribe();
384
+ };
385
+ },
386
+ { immediate: true }
387
+ );
388
+ (0, import_vue_demi5.onMounted)(() => {
389
+ if (!isInContextEditingMode()) {
390
+ return;
391
+ }
392
+ const existingScript = document.getElementById(previewScriptId);
393
+ if (existingScript) {
394
+ return;
395
+ }
396
+ const script = document.createElement("script");
397
+ script.id = previewScriptId;
398
+ script.src = getCanvasInContextEmbedScriptUrl();
399
+ script.async = true;
400
+ document.head.appendChild(script);
401
+ });
402
+ return {
403
+ composition: contextualComposition != null ? contextualComposition : initialCompositionValue
404
+ };
405
+ };
406
+ function getCanvasInContextEmbedScriptUrl() {
407
+ const scriptUrl = `${window.document.referrer}files/canvas-in-context-embed/index.js`;
408
+ return scriptUrl;
409
+ }
410
+ function isInContextEditingMode() {
411
+ if (typeof window === "undefined") {
412
+ return false;
413
+ }
414
+ const isOpenedByInContextEditor = new URLSearchParams(window.location.search).has(
415
+ import_canvas4.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM
416
+ );
417
+ const isAllowlistedReferrer = Boolean(
418
+ window.document.referrer.match(/(^https:\/\/|\.)(uniform.app|uniform.wtf|localhost:\d{4})\//)
419
+ );
420
+ return isOpenedByInContextEditor && isAllowlistedReferrer;
421
+ }
422
+ // Annotate the CommonJS export names for ESM import in node:
423
+ 0 && (module.exports = {
424
+ CANVAS_COMPOSITION_TYPE,
425
+ CANVAS_SLOT_CONTENT_TYPE,
426
+ Composition,
427
+ DefaultNotImplementedComponent,
428
+ SlotContent,
429
+ compositionInjectionKey,
430
+ convertComponentToProps,
431
+ createApiEnhancer,
432
+ resolveRendererInjectionKey,
433
+ useCompositionEventEffect,
434
+ useContextualEditing
435
+ });