@3dsource/metabox-front-api 0.0.11 → 0.0.12
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.esm.js +752 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +777 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +12 -9
- package/dist/actions/ApiAction.js +0 -14
- package/dist/actions/GetFastRender.js +0 -61
- package/dist/actions/GetPdf.js +0 -51
- package/dist/actions/SetEnvironment.js +0 -45
- package/dist/actions/SetEnvironmentMaterial.js +0 -65
- package/dist/actions/SetMaterial.js +0 -65
- package/dist/actions/SetProduct.js +0 -29
- package/dist/actions/ShowEmbeddedMenu.js +0 -29
- package/dist/actions/WatchCallbacks.js +0 -27
- package/dist/index.js +0 -23
- package/dist/interfaces/FromMetaboxMessagePayloads.js +0 -60
- package/dist/interfaces/ToMetaboxMessagePayloads.js +0 -22
- package/dist/interfaces/UniversalConfigurator.js +0 -6
- package/dist/interfaces/application-device-types.js +0 -10
- package/dist/lib/UrlUtils.js +0 -37
- package/dist/lib/initializeCommunicator.js +0 -134
- package/dist/lib/integrateMetaBox.js +0 -61
- package/dist/lib/saveImage.js +0 -23
- package/dist/lib/sleep.js +0 -17
|
@@ -0,0 +1,752 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025.
|
|
3
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
4
|
+
* All Rights Reserved
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Base class for all API actions.
|
|
8
|
+
* @hidden
|
|
9
|
+
*/
|
|
10
|
+
class ApiAction {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.api = window.env3DSource.api;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Copyright (c) 2025.
|
|
18
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
19
|
+
* All Rights Reserved
|
|
20
|
+
*/
|
|
21
|
+
const ToMetaBoxApiAction = {
|
|
22
|
+
getPdf: 'getPdf',
|
|
23
|
+
loadLevel: 'loadLevel',
|
|
24
|
+
setProduct: 'setProduct',
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated, use 'watchCallbacks'"
|
|
27
|
+
* use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
|
|
28
|
+
* @hidden
|
|
29
|
+
*/
|
|
30
|
+
addResponse: 'addResponse',
|
|
31
|
+
getFastRender: 'getFastRender',
|
|
32
|
+
watchCallbacks: 'watchCallbacks',
|
|
33
|
+
setMaterialById: 'setMaterialById',
|
|
34
|
+
showEmbeddedMenu: 'showEmbeddedMenu',
|
|
35
|
+
forceSetDeviceType: 'forceSetDeviceType',
|
|
36
|
+
setEnvironmentMaterialById: 'setEnvironmentMaterialById',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/*
|
|
40
|
+
* Copyright (c) 2025.
|
|
41
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
42
|
+
* All Rights Reserved
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Represents an action to toggle the Embedded Menu in MetaBox.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* This action sends a message to the MetaBox API to toggle the visibility of the embedded menu.
|
|
49
|
+
*/
|
|
50
|
+
class ShowEmbeddedMenu extends ApiAction {
|
|
51
|
+
/**
|
|
52
|
+
* Creates an instance of ShowEmbeddedMenu.
|
|
53
|
+
*
|
|
54
|
+
* @param visible - A flag indicating whether the embedded menu should be visible.
|
|
55
|
+
*/
|
|
56
|
+
constructor(visible) {
|
|
57
|
+
super();
|
|
58
|
+
this.api.sendMessageToMetaBox({
|
|
59
|
+
action: ToMetaBoxApiAction.showEmbeddedMenu,
|
|
60
|
+
payload: {
|
|
61
|
+
visible,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Copyright (c) 2025.
|
|
69
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
70
|
+
* All Rights Reserved
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Represents an action to set the environment by its ID.
|
|
74
|
+
* @remarks
|
|
75
|
+
* This action sends a message to the MetaBox API to load the environment level using the specified environment ID.
|
|
76
|
+
* It requires the MetaBox API to be properly initialized and an event listener to be registered.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* import { integrateMetaBox, SetEnvironment } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
|
|
80
|
+
*
|
|
81
|
+
* // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
|
|
82
|
+
* integrateMetaBox(
|
|
83
|
+
* 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
84
|
+
* 'embed3DSource'
|
|
85
|
+
* );
|
|
86
|
+
*
|
|
87
|
+
* // Setup API ready callback
|
|
88
|
+
* window.env3DSource = window.env3DSource || {};
|
|
89
|
+
* window.env3DSource.apiReady = (api) => {
|
|
90
|
+
* // Change the environment to '55555555-1234-1234-1234-01234567890'
|
|
91
|
+
* new SetEnvironment('55555555-1234-1234-1234-01234567890');
|
|
92
|
+
* };
|
|
93
|
+
*/
|
|
94
|
+
class SetEnvironment extends ApiAction {
|
|
95
|
+
/**
|
|
96
|
+
* Creates an instance of SetEnvironment.
|
|
97
|
+
*
|
|
98
|
+
* @param environmentId - The environment ID.
|
|
99
|
+
*/
|
|
100
|
+
constructor(environmentId) {
|
|
101
|
+
super();
|
|
102
|
+
this.api.sendMessageToMetaBox({
|
|
103
|
+
payload: {
|
|
104
|
+
id: environmentId,
|
|
105
|
+
},
|
|
106
|
+
action: ToMetaBoxApiAction.loadLevel,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/*
|
|
112
|
+
* Copyright (c) 2025.
|
|
113
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
114
|
+
* All Rights Reserved
|
|
115
|
+
*/
|
|
116
|
+
/**
|
|
117
|
+
* Represents an action to set a product by its ID.
|
|
118
|
+
*
|
|
119
|
+
* @remarks
|
|
120
|
+
* This action sends a message to the MetaBox API to set a product using the provided product ID.
|
|
121
|
+
*/
|
|
122
|
+
class SetProduct extends ApiAction {
|
|
123
|
+
/**
|
|
124
|
+
* Creates an instance of SetProduct.
|
|
125
|
+
*
|
|
126
|
+
* @param productId - The product ID.
|
|
127
|
+
*/
|
|
128
|
+
constructor(productId) {
|
|
129
|
+
super();
|
|
130
|
+
this.api.sendMessageToMetaBox({
|
|
131
|
+
payload: {
|
|
132
|
+
productId,
|
|
133
|
+
},
|
|
134
|
+
action: ToMetaBoxApiAction.setProduct,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
* Copyright (c) 2025.
|
|
141
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
142
|
+
* All Rights Reserved
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* Represents an action to get a PDF from Metabox.
|
|
146
|
+
* @remarks
|
|
147
|
+
* This class sends a message to the MetaBox API to generate a PDF based on the current configuration.
|
|
148
|
+
* This action does not require any parameters.
|
|
149
|
+
*
|
|
150
|
+
* **Important:** Before invoking this action, ensure that the MetaBox viewport is fully loaded. This is indicated by the event:
|
|
151
|
+
*```javascript
|
|
152
|
+
* data.eventType === 'viewportReady' && data.payload === true.
|
|
153
|
+
* ```
|
|
154
|
+
* Only then request be processed.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* import { integrateMetaBox, GetPdf } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
|
|
158
|
+
*
|
|
159
|
+
* integrateMetaBox(
|
|
160
|
+
* 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
161
|
+
* 'embed3DSource'
|
|
162
|
+
* );
|
|
163
|
+
*
|
|
164
|
+
* window.env3DSource = window.env3DSource || {};
|
|
165
|
+
* window.env3DSource.apiReady = (api) => {
|
|
166
|
+
* api.addEventListener('metaboxData', (data) => {
|
|
167
|
+
* if (data.eventType === 'viewportReady' && data.payload === true) {
|
|
168
|
+
* // Request a PDF of the current configuration
|
|
169
|
+
* new GetPdf();
|
|
170
|
+
* }
|
|
171
|
+
* });
|
|
172
|
+
* };
|
|
173
|
+
*/
|
|
174
|
+
class GetPdf extends ApiAction {
|
|
175
|
+
/**
|
|
176
|
+
* Creates an instance of GetPdf.
|
|
177
|
+
*
|
|
178
|
+
* @remarks
|
|
179
|
+
* This constructor does not require any parameters.
|
|
180
|
+
*/
|
|
181
|
+
constructor() {
|
|
182
|
+
super();
|
|
183
|
+
this.api.sendMessageToMetaBox({
|
|
184
|
+
action: ToMetaBoxApiAction.getPdf,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/*
|
|
190
|
+
* Copyright (c) 2025.
|
|
191
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
192
|
+
* All Rights Reserved
|
|
193
|
+
*/
|
|
194
|
+
/**
|
|
195
|
+
* Represents an action to set a material by its ID.
|
|
196
|
+
*
|
|
197
|
+
* @remarks
|
|
198
|
+
* This action sends a message to the MetaBox API to update the material on a specific product slot using the provided identifiers.
|
|
199
|
+
|
|
200
|
+
* **Important:** Before invoking this action, ensure that the MetaBox already dispatched
|
|
201
|
+
* as minimum one eventType:"basicConfiguratorDataWithState":
|
|
202
|
+
* ```javascript
|
|
203
|
+
* data.eventType === 'basicConfiguratorDataWithState'.
|
|
204
|
+
* ```
|
|
205
|
+
* Only then request be processed.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* import { SetMaterial } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
|
|
209
|
+
*
|
|
210
|
+
* // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
|
|
211
|
+
* integrateMetaBox(
|
|
212
|
+
* 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
213
|
+
* 'embed3DSource'
|
|
214
|
+
* );
|
|
215
|
+
*
|
|
216
|
+
* // Set up the global API environment
|
|
217
|
+
* window.env3DSource = window.env3DSource || {};
|
|
218
|
+
* window.env3DSource.apiReady = (api) => {
|
|
219
|
+
* // Listen for events from the MetaBox API
|
|
220
|
+
* api.addEventListener('metaboxData', (data) => {
|
|
221
|
+
* // Process SetMaterial
|
|
222
|
+
* if (data.eventType === 'basicConfiguratorDataWithState') {
|
|
223
|
+
* new SetMaterial(
|
|
224
|
+
* 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3',
|
|
225
|
+
* 'carpaint',
|
|
226
|
+
* 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
|
|
227
|
+
* );
|
|
228
|
+
* }
|
|
229
|
+
* });
|
|
230
|
+
* };
|
|
231
|
+
*/
|
|
232
|
+
class SetMaterial extends ApiAction {
|
|
233
|
+
/**
|
|
234
|
+
* Creates an instance of SetMaterial.
|
|
235
|
+
*
|
|
236
|
+
* @param productId - The product ID.
|
|
237
|
+
* @param slotId - The slot ID.
|
|
238
|
+
* @param materialId - The material ID.
|
|
239
|
+
*/
|
|
240
|
+
constructor(productId, slotId, materialId) {
|
|
241
|
+
super();
|
|
242
|
+
this.api.sendMessageToMetaBox({
|
|
243
|
+
payload: {
|
|
244
|
+
productId,
|
|
245
|
+
slotId,
|
|
246
|
+
materialId,
|
|
247
|
+
},
|
|
248
|
+
action: ToMetaBoxApiAction.setMaterialById,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/*
|
|
254
|
+
* Copyright (c) 2025.
|
|
255
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
256
|
+
* All Rights Reserved
|
|
257
|
+
*/
|
|
258
|
+
/**
|
|
259
|
+
* Represents an action to set a material by its ID.
|
|
260
|
+
*
|
|
261
|
+
* @remarks
|
|
262
|
+
* This action sends a message to the MetaBox API to update the material on a specific product slot using the provided identifiers.
|
|
263
|
+
|
|
264
|
+
* **Important:** Before invoking this action, ensure that the MetaBox already dispatched
|
|
265
|
+
* as minimum one eventType:"basicConfiguratorDataWithState":
|
|
266
|
+
* ```javascript
|
|
267
|
+
* data.eventType === 'basicConfiguratorDataWithState'.
|
|
268
|
+
* ```
|
|
269
|
+
* Only then request be processed.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* import { SetMaterial } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
|
|
273
|
+
*
|
|
274
|
+
* // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
|
|
275
|
+
* integrateMetaBox(
|
|
276
|
+
* 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
277
|
+
* 'embed3DSource'
|
|
278
|
+
* );
|
|
279
|
+
*
|
|
280
|
+
* // Set up the global API environment
|
|
281
|
+
* window.env3DSource = window.env3DSource || {};
|
|
282
|
+
* window.env3DSource.apiReady = (api) => {
|
|
283
|
+
* // Listen for events from the MetaBox API
|
|
284
|
+
* api.addEventListener('metaboxData', (data) => {
|
|
285
|
+
* // Process SetMaterial
|
|
286
|
+
* if (data.eventType === 'basicConfiguratorDataWithState') {
|
|
287
|
+
* new SetEnvironmentMaterial(
|
|
288
|
+
* 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3',
|
|
289
|
+
* 'carpaint',
|
|
290
|
+
* 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
|
|
291
|
+
* );
|
|
292
|
+
* }
|
|
293
|
+
* });
|
|
294
|
+
* };
|
|
295
|
+
*/
|
|
296
|
+
class SetEnvironmentMaterial extends ApiAction {
|
|
297
|
+
/**
|
|
298
|
+
* Creates an instance of SetMaterial.
|
|
299
|
+
*
|
|
300
|
+
* @param productId - The product ID.
|
|
301
|
+
* @param slotId - The slot ID.
|
|
302
|
+
* @param materialId - The material ID.
|
|
303
|
+
*/
|
|
304
|
+
constructor(productId, slotId, materialId) {
|
|
305
|
+
super();
|
|
306
|
+
this.api.sendMessageToMetaBox({
|
|
307
|
+
payload: {
|
|
308
|
+
productId,
|
|
309
|
+
slotId,
|
|
310
|
+
materialId,
|
|
311
|
+
},
|
|
312
|
+
action: ToMetaBoxApiAction.setEnvironmentMaterialById,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/*
|
|
318
|
+
* Copyright (c) 2025.
|
|
319
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
320
|
+
* All Rights Reserved
|
|
321
|
+
*/
|
|
322
|
+
/**
|
|
323
|
+
* Represents a fast render action.
|
|
324
|
+
* @remarks
|
|
325
|
+
* This class sends a message to the MetaBox API to obtain a fast render based on the provided format and optional size.
|
|
326
|
+
*
|
|
327
|
+
* **Important:** Before invoking this action, ensure that the MetaBox viewport is fully loaded. This is indicated by the event:
|
|
328
|
+
* ```javascript
|
|
329
|
+
* data.eventType === 'viewportReady' && data.payload === true.
|
|
330
|
+
* ```
|
|
331
|
+
* Only then request be processed.
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* import { integrateMetaBox, GetFastRender, saveImage } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
|
|
335
|
+
*
|
|
336
|
+
* // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
|
|
337
|
+
* integrateMetaBox(
|
|
338
|
+
* 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
339
|
+
* 'embed3DSource'
|
|
340
|
+
* );
|
|
341
|
+
*
|
|
342
|
+
* // Set up the global API environment
|
|
343
|
+
* window.env3DSource = window.env3DSource || {};
|
|
344
|
+
* window.env3DSource.apiReady = (api) => {
|
|
345
|
+
* // Listen for events from the MetaBox API
|
|
346
|
+
* api.addEventListener('metaboxData', (data) => {
|
|
347
|
+
* // Ensure the viewport is ready before sending render requests
|
|
348
|
+
* if (data.eventType === 'viewportReady' && data.payload === true) {
|
|
349
|
+
* // Request a fast render (PNG image with 1024x1024 dimensions)
|
|
350
|
+
* new GetFastRender('image/png', { x: 1024, y: 1024 });
|
|
351
|
+
* }
|
|
352
|
+
* // Process the fast render response
|
|
353
|
+
* if (data.eventType === 'fastRender') {
|
|
354
|
+
* saveImage(data.payload, 'Render.png');
|
|
355
|
+
* }
|
|
356
|
+
* });
|
|
357
|
+
* };
|
|
358
|
+
*/
|
|
359
|
+
class GetFastRender extends ApiAction {
|
|
360
|
+
/**
|
|
361
|
+
* Constructs an instance of GetFastRender.
|
|
362
|
+
* @param mimeType - The output format.
|
|
363
|
+
* @param size - Optional size in pixels.
|
|
364
|
+
*/
|
|
365
|
+
constructor(mimeType, size) {
|
|
366
|
+
super();
|
|
367
|
+
this.api.sendMessageToMetaBox({
|
|
368
|
+
payload: {
|
|
369
|
+
format: mimeType,
|
|
370
|
+
size,
|
|
371
|
+
},
|
|
372
|
+
action: ToMetaBoxApiAction.getFastRender,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/*
|
|
378
|
+
* Copyright (c) 2025.
|
|
379
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
380
|
+
* All Rights Reserved
|
|
381
|
+
*/
|
|
382
|
+
/**
|
|
383
|
+
* @experimental
|
|
384
|
+
* @remarks
|
|
385
|
+
* Represents an action to watch Unreal callbacks from Unreal engine.
|
|
386
|
+
* Can be useful for custom solutions.
|
|
387
|
+
*/
|
|
388
|
+
class WatchCallbacks extends ApiAction {
|
|
389
|
+
/**
|
|
390
|
+
* Creates an instance of WatchCallbacks.
|
|
391
|
+
*
|
|
392
|
+
* @remarks
|
|
393
|
+
* This constructor does not require any parameters.
|
|
394
|
+
*/
|
|
395
|
+
constructor() {
|
|
396
|
+
super();
|
|
397
|
+
this.api.sendMessageToMetaBox({
|
|
398
|
+
action: ToMetaBoxApiAction.watchCallbacks,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/*
|
|
404
|
+
* Copyright (c) 2025.
|
|
405
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
406
|
+
* All Rights Reserved
|
|
407
|
+
*/
|
|
408
|
+
const messageType = 'metaboxData';
|
|
409
|
+
/**
|
|
410
|
+
* API class to handle communication with the embedded content.
|
|
411
|
+
* @internal Do not Create an instance of this class directly.
|
|
412
|
+
* integrateMetabox will create an instance of this class.
|
|
413
|
+
*
|
|
414
|
+
* @remarks
|
|
415
|
+
* This class sets up a communication channel with the embedded content via window messages.
|
|
416
|
+
* @hidden
|
|
417
|
+
*/
|
|
418
|
+
class Communicator {
|
|
419
|
+
/**
|
|
420
|
+
* Creates an instance of Communicator.
|
|
421
|
+
* @internal
|
|
422
|
+
* @remarks
|
|
423
|
+
* If an instance already exists, it will be destroyed before creating a new one.
|
|
424
|
+
* The instance listens for messages from the iframe.
|
|
425
|
+
*/
|
|
426
|
+
constructor() {
|
|
427
|
+
var _a;
|
|
428
|
+
/**
|
|
429
|
+
* Storage for callback listeners by message type.
|
|
430
|
+
*/
|
|
431
|
+
this.listeners = [];
|
|
432
|
+
/**
|
|
433
|
+
* Binder function for handling incoming messages.
|
|
434
|
+
*/
|
|
435
|
+
this.binder = this.handleMessageReceived.bind(this, messageType);
|
|
436
|
+
(_a = Communicator.instance) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
437
|
+
Communicator.instance = this;
|
|
438
|
+
window.addEventListener('message', this.binder);
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Destroys the communicator instance by clearing listeners and removing event listeners.
|
|
442
|
+
*/
|
|
443
|
+
destroy() {
|
|
444
|
+
this.listeners = [];
|
|
445
|
+
window.removeEventListener('message', this.binder);
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Sends a message to the embedded content (MetaBox).
|
|
449
|
+
*
|
|
450
|
+
* @param data - The data to send.
|
|
451
|
+
*/
|
|
452
|
+
sendMessageToMetaBox(data) {
|
|
453
|
+
var _a;
|
|
454
|
+
const iframe = document.getElementById('embeddedContent');
|
|
455
|
+
const dataToSend = {
|
|
456
|
+
host: 'metaBoxHost',
|
|
457
|
+
payload: Object.assign({}, data),
|
|
458
|
+
};
|
|
459
|
+
// In production, replace "*" with the actual origin for added security.
|
|
460
|
+
(_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage(dataToSend, '*');
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Adds an event listener for receiving specific types of messages.
|
|
464
|
+
*
|
|
465
|
+
* @param messageType - The message type to listen for.
|
|
466
|
+
* @param callback - The callback function to execute when a message is received.
|
|
467
|
+
*/
|
|
468
|
+
addEventListener(messageType, callback) {
|
|
469
|
+
this.listeners.push({ messageType, callback });
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Dispatches an event to all listeners of a specific message type.
|
|
473
|
+
*
|
|
474
|
+
* @param messageType - The message type.
|
|
475
|
+
* @param data - The data associated with the event.
|
|
476
|
+
*/
|
|
477
|
+
dispatchEvent(messageType, data) {
|
|
478
|
+
this.listeners
|
|
479
|
+
.filter((listener) => listener.messageType === messageType)
|
|
480
|
+
.forEach((listener) => listener.callback(data));
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Handles incoming messages by invoking the appropriate callbacks.
|
|
484
|
+
*
|
|
485
|
+
* @param messageType - The message type to filter for.
|
|
486
|
+
* @param event - The incoming message event.
|
|
487
|
+
*
|
|
488
|
+
* @remarks
|
|
489
|
+
* Optionally, you can perform an origin check for added security.
|
|
490
|
+
*/
|
|
491
|
+
handleMessageReceived(messageType, event) {
|
|
492
|
+
var _a;
|
|
493
|
+
// Optional: Perform origin check here for added security
|
|
494
|
+
// if (event.origin !== "EXPECTED_ORIGIN") return;
|
|
495
|
+
if (((_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.host) !== 'metabox') {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
this.listeners
|
|
499
|
+
.filter((listener) => listener.messageType === messageType)
|
|
500
|
+
.forEach((listener) => {
|
|
501
|
+
listener.callback(event.data.payload);
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Removes an event listener for a specific type of message.
|
|
506
|
+
*
|
|
507
|
+
* @param messageType - The message type.
|
|
508
|
+
* @param callback - The callback function to remove.
|
|
509
|
+
*/
|
|
510
|
+
removeEventListener(messageType, callback) {
|
|
511
|
+
this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Initializes the communicator when the embedded content is ready.
|
|
515
|
+
* @internal
|
|
516
|
+
* @remarks
|
|
517
|
+
* This function sets up a listener that waits for the 'appLoaded' action and then initializes the Communicator.
|
|
518
|
+
*/
|
|
519
|
+
static createInstance() {
|
|
520
|
+
window.env3DSource = window.env3DSource || {};
|
|
521
|
+
const startHandler = (event) => {
|
|
522
|
+
var _a, _b;
|
|
523
|
+
if (((_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.action) === 'appLoaded' &&
|
|
524
|
+
'apiReady' in window.env3DSource) {
|
|
525
|
+
window.env3DSource.api = new Communicator();
|
|
526
|
+
window.env3DSource.apiReady(window.env3DSource.api);
|
|
527
|
+
window.removeEventListener('message', startHandler);
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
window.addEventListener('message', startHandler);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* The singleton instance of the communicator.
|
|
535
|
+
*/
|
|
536
|
+
Communicator.instance = null;
|
|
537
|
+
|
|
538
|
+
/*
|
|
539
|
+
* Copyright (c) 2025.
|
|
540
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
541
|
+
* All Rights Reserved
|
|
542
|
+
*/
|
|
543
|
+
/**
|
|
544
|
+
* Integrates the MetaBox into the provided layout.
|
|
545
|
+
*
|
|
546
|
+
* @remarks
|
|
547
|
+
* This function injects an iframe into a specified container element in the DOM and initializes
|
|
548
|
+
* the communicator for interacting with the MetaBox embedded content.
|
|
549
|
+
*
|
|
550
|
+
* @param iframeSrc - The source URL of the MetaBox iframe.
|
|
551
|
+
* Additional url parameters:
|
|
552
|
+
* ```javascript
|
|
553
|
+
* sidebar=false - disables the internal MetaBox menu
|
|
554
|
+
* introImage=https://example.com/image.png - sets the intro image
|
|
555
|
+
* ```
|
|
556
|
+
* i.e. final link will look like https://metabox.3dsource.com/metabox-configurator/basic/your-configurator-id?sidebar=false&introImage=https://example.com/image.png
|
|
557
|
+
*
|
|
558
|
+
* @param containerId - The ID of the layout element. Defaults to 'embed3DSource'.
|
|
559
|
+
*
|
|
560
|
+
* @throws Error if the container element with the specified ID is not found.
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* import {
|
|
564
|
+
* integrateMetaBox,
|
|
565
|
+
* } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
|
|
566
|
+
*
|
|
567
|
+
* integrateMetaBox(
|
|
568
|
+
* 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
569
|
+
* 'embed3DSource'
|
|
570
|
+
* );
|
|
571
|
+
*/
|
|
572
|
+
function integrateMetaBox(iframeSrc, containerId = 'embed3DSource') {
|
|
573
|
+
const container = document.getElementById(containerId);
|
|
574
|
+
if (!container) {
|
|
575
|
+
throw new Error(`Container element with id ${containerId} not found`);
|
|
576
|
+
}
|
|
577
|
+
Communicator.createInstance();
|
|
578
|
+
const style = document.createElement('style');
|
|
579
|
+
style.innerHTML = `
|
|
580
|
+
#${containerId} {
|
|
581
|
+
width: 100%;
|
|
582
|
+
height: 100%;
|
|
583
|
+
overflow: hidden;
|
|
584
|
+
position: relative;
|
|
585
|
+
}
|
|
586
|
+
`;
|
|
587
|
+
document.head.appendChild(style);
|
|
588
|
+
const iframe = document.createElement('iframe');
|
|
589
|
+
iframe.setAttribute('referrerPolicy', 'no-referrer-when-downgrade');
|
|
590
|
+
iframe.setAttribute('id', 'embeddedContent');
|
|
591
|
+
iframe.style.border = '0';
|
|
592
|
+
iframe.style.width = '100%';
|
|
593
|
+
iframe.style.height = '100%';
|
|
594
|
+
iframe.style.overflow = 'hidden';
|
|
595
|
+
iframe.src = iframeSrc;
|
|
596
|
+
container.appendChild(iframe);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/*
|
|
600
|
+
* Copyright (c) 2025.
|
|
601
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
602
|
+
* All Rights Reserved
|
|
603
|
+
*/
|
|
604
|
+
/**
|
|
605
|
+
* Saves an image by triggering a download.
|
|
606
|
+
*
|
|
607
|
+
* @remarks
|
|
608
|
+
* This function creates an anchor element, sets its `href` attribute to the provided image URL,
|
|
609
|
+
* and triggers a click event to initiate a download with the specified filename.
|
|
610
|
+
*
|
|
611
|
+
* @param image - The URL of the image to save.
|
|
612
|
+
* @param filename - The name of the file to save.
|
|
613
|
+
*/
|
|
614
|
+
function saveImage(image, filename) {
|
|
615
|
+
const a = document.createElement('a');
|
|
616
|
+
a.href = image;
|
|
617
|
+
a.download = filename;
|
|
618
|
+
document.body.appendChild(a);
|
|
619
|
+
a.click();
|
|
620
|
+
document.body.removeChild(a);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/*
|
|
624
|
+
* Copyright (c) 2025.
|
|
625
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
626
|
+
* All Rights Reserved
|
|
627
|
+
*/
|
|
628
|
+
/**
|
|
629
|
+
* Pauses execution for a specified duration.
|
|
630
|
+
*
|
|
631
|
+
* @remarks
|
|
632
|
+
* This function returns a promise that resolves after the given number of milliseconds.
|
|
633
|
+
*
|
|
634
|
+
* @param time - The time to sleep in milliseconds.
|
|
635
|
+
* @returns A promise that resolves after the specified duration.
|
|
636
|
+
*/
|
|
637
|
+
function sleep(time) {
|
|
638
|
+
return new Promise((resolve) => setTimeout(resolve, time));
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/*
|
|
642
|
+
* Copyright (c) 2025.
|
|
643
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
644
|
+
* All Rights Reserved
|
|
645
|
+
*/
|
|
646
|
+
/**
|
|
647
|
+
* Sets the URL parameters based on the provided state.
|
|
648
|
+
*
|
|
649
|
+
* @remarks
|
|
650
|
+
* This function updates the current URL by replacing its search parameters with the key-value pairs from the provided state object.
|
|
651
|
+
*
|
|
652
|
+
* @param state - An object containing key-value pairs to set as URL parameters.
|
|
653
|
+
*/
|
|
654
|
+
function setUrlParams(state) {
|
|
655
|
+
const url = new URL(window.location.href);
|
|
656
|
+
url.search = '';
|
|
657
|
+
Object.entries(state).forEach(([key, value]) => {
|
|
658
|
+
url.searchParams.set(key, value);
|
|
659
|
+
});
|
|
660
|
+
history.replaceState(null, '', url.toString());
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Retrieves the URL parameters as an object.
|
|
664
|
+
*
|
|
665
|
+
* @remarks
|
|
666
|
+
* This function parses the current URL's search parameters and returns them as a key-value object.
|
|
667
|
+
*
|
|
668
|
+
* @returns An object containing the URL parameters.
|
|
669
|
+
*/
|
|
670
|
+
function getUrlParams() {
|
|
671
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
672
|
+
const selections = {};
|
|
673
|
+
urlParams.forEach((value, key) => {
|
|
674
|
+
selections[key] = value;
|
|
675
|
+
});
|
|
676
|
+
return selections;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/*
|
|
680
|
+
* Copyright (c) 2025.
|
|
681
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
682
|
+
* All Rights Reserved
|
|
683
|
+
*/
|
|
684
|
+
const FromMetaboxApiAction = {
|
|
685
|
+
bridge: 'bridge',
|
|
686
|
+
appLoaded: 'appLoaded',
|
|
687
|
+
/**
|
|
688
|
+
* @hidden
|
|
689
|
+
* @deprecated, use MetaBoxApiAction.Bridge with eventType: "unrealFeature"
|
|
690
|
+
*/
|
|
691
|
+
unrealFeatureBridge: 'unrealFeatureBridge',
|
|
692
|
+
/**
|
|
693
|
+
* @hidden
|
|
694
|
+
* @deprecated, use MetaBoxApiAction.Bridge with eventType: "unrealCallback"
|
|
695
|
+
*/
|
|
696
|
+
unrealCallback: 'unrealCallback',
|
|
697
|
+
/**
|
|
698
|
+
* @hidden
|
|
699
|
+
* @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
|
|
700
|
+
*/
|
|
701
|
+
configuratorDataWithStateBridge: 'configuratorDataWithStateBridge',
|
|
702
|
+
/**
|
|
703
|
+
* @hidden
|
|
704
|
+
* @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
|
|
705
|
+
*/
|
|
706
|
+
configuratorDataBridge: 'configuratorDataBridge',
|
|
707
|
+
/**
|
|
708
|
+
* @hidden
|
|
709
|
+
* @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
|
|
710
|
+
*/
|
|
711
|
+
menuDataBridge: 'menuDataBridge',
|
|
712
|
+
/**
|
|
713
|
+
* @hidden
|
|
714
|
+
* @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
|
|
715
|
+
*/
|
|
716
|
+
flatMenuDataBridge: 'flatMenuDataBridge',
|
|
717
|
+
/**
|
|
718
|
+
* @hidden
|
|
719
|
+
* @deprecated, use Bridge with isFullscreen
|
|
720
|
+
*/
|
|
721
|
+
fullScreenBridge: 'fullScreenBridge',
|
|
722
|
+
/**
|
|
723
|
+
* @hidden
|
|
724
|
+
* @deprecated, use Bridge with streamResolution
|
|
725
|
+
*/
|
|
726
|
+
videoSize: 'videoSize',
|
|
727
|
+
/**
|
|
728
|
+
* @hidden
|
|
729
|
+
* Used in platform, must be removed in future
|
|
730
|
+
* @deprecated
|
|
731
|
+
*/
|
|
732
|
+
unrealResponse: 'unrealResponse',
|
|
733
|
+
/**
|
|
734
|
+
* @hidden
|
|
735
|
+
* @deprecated\
|
|
736
|
+
*/
|
|
737
|
+
getFastRender: 'getFastRender',
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
/*
|
|
741
|
+
* Copyright (c) 2025.
|
|
742
|
+
* 3DSource.com, Sergii Karanda steve@3dsource.com
|
|
743
|
+
* All Rights Reserved
|
|
744
|
+
*/
|
|
745
|
+
const APPLICATION_DEVICE_TYPES = {
|
|
746
|
+
deviceTypeAuto: 'deviceTypeAuto',
|
|
747
|
+
deviceTypeMobile: 'deviceTypeMobile',
|
|
748
|
+
deviceTypeDesktop: 'deviceTypeDesktop',
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
export { APPLICATION_DEVICE_TYPES, ApiAction, Communicator, FromMetaboxApiAction, GetFastRender, GetPdf, SetEnvironment, SetEnvironmentMaterial, SetMaterial, SetProduct, ShowEmbeddedMenu, ToMetaBoxApiAction, WatchCallbacks, getUrlParams, integrateMetaBox, saveImage, setUrlParams, sleep };
|
|
752
|
+
//# sourceMappingURL=index.esm.js.map
|