@3dsource/metabox-modular-configurator-api 0.0.1-dev.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.
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
import { fromEvent } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base class for commands sent to the Metabox.
|
|
5
|
+
*/
|
|
6
|
+
class CommandBase {
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Wrapper helper function to use it with rxJs the same as `fromEvent` to listen to events from the Communicator.
|
|
11
|
+
* @param target
|
|
12
|
+
* @param eventName
|
|
13
|
+
*/
|
|
14
|
+
function fromCommunicatorEvent(target, eventName) {
|
|
15
|
+
return fromEvent(target, eventName);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const MetaboxModularConfiguratorActions = {
|
|
19
|
+
setEnvironment: 'setEnvironment',
|
|
20
|
+
setEnvironmentMaterialById: 'setEnvironmentMaterialById',
|
|
21
|
+
setComponent: 'setComponent',
|
|
22
|
+
setComponentType: 'setComponentType',
|
|
23
|
+
setComponentMaterialById: 'setComponentMaterialById',
|
|
24
|
+
sendCommandToUnreal: 'sendCommandToUnreal',
|
|
25
|
+
getPdf: 'getPdf',
|
|
26
|
+
getScreenshot: 'getScreenshot',
|
|
27
|
+
watchCallbacks: 'watchCallbacks',
|
|
28
|
+
showEmbeddedMenu: 'showEmbeddedMenu',
|
|
29
|
+
forceSetDeviceType: 'forceSetDeviceType',
|
|
30
|
+
showOverlayInterface: 'showOverlayInterface',
|
|
31
|
+
resetCamera: 'resetCamera',
|
|
32
|
+
applyZoom: 'applyZoom',
|
|
33
|
+
initShowcase: 'initShowcase',
|
|
34
|
+
playShowcase: 'playShowcase',
|
|
35
|
+
pauseShowcase: 'pauseShowcase',
|
|
36
|
+
stopShowcase: 'stopShowcase',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Represents a command to get a PDF from Metabox.
|
|
41
|
+
* @remarks
|
|
42
|
+
* This class sends a message to the Metabox API to generate a PDF based on the current configuration.
|
|
43
|
+
* This action does not require any parameters.
|
|
44
|
+
*
|
|
45
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* import { GetPdf, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
49
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
50
|
+
* api.sendCommandToMetaBox(new GetPdf());
|
|
51
|
+
* };
|
|
52
|
+
*/
|
|
53
|
+
class GetPdf extends CommandBase {
|
|
54
|
+
/**
|
|
55
|
+
* Creates an instance of GetPdf.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* This constructor does not require any parameters.
|
|
59
|
+
*/
|
|
60
|
+
constructor() {
|
|
61
|
+
super();
|
|
62
|
+
this.data = { action: MetaboxModularConfiguratorActions.getPdf };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Represents a command to send to unreal and reset camera to initial.
|
|
68
|
+
* @remarks
|
|
69
|
+
* This class sends a message to the Metabox API to reset the camera on a scene.
|
|
70
|
+
* This action does not require any parameters.
|
|
71
|
+
*
|
|
72
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* import { ResetCamera, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
76
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
77
|
+
* api.sendCommandToMetaBox(new ResetCamera());
|
|
78
|
+
* };
|
|
79
|
+
*/
|
|
80
|
+
class ResetCamera extends CommandBase {
|
|
81
|
+
/**
|
|
82
|
+
* Creates an instance of ResetCamera.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* This constructor does not require any parameters.
|
|
86
|
+
*/
|
|
87
|
+
constructor() {
|
|
88
|
+
super();
|
|
89
|
+
this.data = { action: MetaboxModularConfiguratorActions.resetCamera };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Represents a command to ApplyZoom and change zoom camera on a scene.
|
|
95
|
+
* @remarks
|
|
96
|
+
* This class sends a command to the Metabox API to change zoom on a scene.
|
|
97
|
+
*
|
|
98
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* import { ApplyZoom, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
102
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
103
|
+
* api.sendCommandToMetaBox(new ApplyZoom(10));
|
|
104
|
+
* api.sendCommandToMetaBox(new ApplyZoom(-10));
|
|
105
|
+
* };
|
|
106
|
+
*/
|
|
107
|
+
class ApplyZoom extends CommandBase {
|
|
108
|
+
/**
|
|
109
|
+
* Creates an instance of ApplyZoom.
|
|
110
|
+
*
|
|
111
|
+
* @param {...number} zoom
|
|
112
|
+
*/
|
|
113
|
+
constructor(zoom) {
|
|
114
|
+
super();
|
|
115
|
+
this.data = {
|
|
116
|
+
action: MetaboxModularConfiguratorActions.applyZoom,
|
|
117
|
+
payload: { zoom },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Represents a command to Init Showcase for a product on a scene and start a sequence for the product.
|
|
124
|
+
* @remarks
|
|
125
|
+
* This class sends a command to the Metabox API to init showcase if the product has a sequence.
|
|
126
|
+
*
|
|
127
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* import { InitShowcase, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
131
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
132
|
+
* api.sendCommandToMetaBox(new InitShowcase('ffea6b5c-3a8a-4f56-9417-e605acb5cca3');
|
|
133
|
+
* };
|
|
134
|
+
*/
|
|
135
|
+
class InitShowcase extends CommandBase {
|
|
136
|
+
/**
|
|
137
|
+
* Creates an instance of InitShowcase.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} productId
|
|
140
|
+
*/
|
|
141
|
+
constructor(productId) {
|
|
142
|
+
super();
|
|
143
|
+
this.data = {
|
|
144
|
+
action: MetaboxModularConfiguratorActions.initShowcase,
|
|
145
|
+
payload: { productId },
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Represents a command to Play Showcase for a product when it already init, and you call pause, for example, before it.
|
|
152
|
+
* @remarks
|
|
153
|
+
* This class sends a command to the Metabox API to play showcase for a product if it is already initialized and pause.
|
|
154
|
+
*
|
|
155
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* import { PlayShowcase, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
159
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
160
|
+
* api.sendCommandToMetaBox(new PlayShowcase());
|
|
161
|
+
* };
|
|
162
|
+
*/
|
|
163
|
+
class PlayShowcase extends CommandBase {
|
|
164
|
+
/**
|
|
165
|
+
* Creates an instance of PlayShowcase.
|
|
166
|
+
*
|
|
167
|
+
* @remarks
|
|
168
|
+
* This constructor does not require any parameters.
|
|
169
|
+
*/
|
|
170
|
+
constructor() {
|
|
171
|
+
super();
|
|
172
|
+
this.data = { action: MetaboxModularConfiguratorActions.playShowcase };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Represents a command to Pause Showcase for a product when it already init and play, and you call pause.
|
|
178
|
+
* @remarks
|
|
179
|
+
* This class sends a command to the Metabox API to pause showcase for a product if it is already initialized and play.
|
|
180
|
+
*
|
|
181
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* import { PauseShowcase, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
185
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
186
|
+
* api.sendCommandToMetaBox(new PauseShowcase());
|
|
187
|
+
* };
|
|
188
|
+
*/
|
|
189
|
+
class PauseShowcase extends CommandBase {
|
|
190
|
+
/**
|
|
191
|
+
* Creates an instance of PauseShowcase.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* This constructor does not require any parameters.
|
|
195
|
+
*/
|
|
196
|
+
constructor() {
|
|
197
|
+
super();
|
|
198
|
+
this.data = { action: MetaboxModularConfiguratorActions.pauseShowcase };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Represents a command to Stop Showcase for a product when it already init, and you want to destroy it.
|
|
204
|
+
* @remarks
|
|
205
|
+
* This class sends a command to the Metabox API to Stop showcase for a product if it is already initialized.
|
|
206
|
+
*
|
|
207
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* import { StopShowcase, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
211
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
212
|
+
* api.sendCommandToMetaBox(new StopShowcase());
|
|
213
|
+
* };
|
|
214
|
+
*/
|
|
215
|
+
class StopShowcase extends CommandBase {
|
|
216
|
+
/**
|
|
217
|
+
* Creates an instance of StopShowcase.
|
|
218
|
+
*
|
|
219
|
+
* @remarks
|
|
220
|
+
* This constructor does not require any parameters.
|
|
221
|
+
*/
|
|
222
|
+
constructor() {
|
|
223
|
+
super();
|
|
224
|
+
this.data = { action: MetaboxModularConfiguratorActions.stopShowcase };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Represents a command to get a screenshot.
|
|
230
|
+
*
|
|
231
|
+
* **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* import { GetScreenshot, Communicator, saveImage } from '@3dsource/metabox-modular-configurator-api';
|
|
235
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
236
|
+
* // Listen for events from to the Metabox API
|
|
237
|
+
* api.addEventListener('screenshot', (data) => {
|
|
238
|
+
* // Process the get screenshot response
|
|
239
|
+
* saveImage(data, 'Render.png');
|
|
240
|
+
* });
|
|
241
|
+
* api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1024, y: 1024 }));
|
|
242
|
+
* };
|
|
243
|
+
*/
|
|
244
|
+
class GetScreenshot extends CommandBase {
|
|
245
|
+
/**
|
|
246
|
+
* Constructs an instance of GetScreenshot.
|
|
247
|
+
* @param {MimeType} mimeType - The output format.
|
|
248
|
+
* @param {{ x: number; y: number }} [size] - Optional size in pixels.
|
|
249
|
+
*/
|
|
250
|
+
constructor(mimeType, size) {
|
|
251
|
+
super();
|
|
252
|
+
this.data = {
|
|
253
|
+
payload: { format: mimeType, size },
|
|
254
|
+
action: MetaboxModularConfiguratorActions.getScreenshot,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Represents a command to set a component by its data (ID, typeId, isRoot).
|
|
261
|
+
*
|
|
262
|
+
* @remarks
|
|
263
|
+
* This action sends a message to the Metabox API to set a component using the provided component ID and its component type data.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* import { SetComponent, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
267
|
+
*
|
|
268
|
+
* //...Assume that the integration is already implemented
|
|
269
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
270
|
+
* api.setCommandToMetaBox(new SetComponent(
|
|
271
|
+
* 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3', 'typeId', true
|
|
272
|
+
* ));
|
|
273
|
+
* };
|
|
274
|
+
*/
|
|
275
|
+
class SetComponent extends CommandBase {
|
|
276
|
+
/**
|
|
277
|
+
* Creates an instance of SetComponent.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} id
|
|
280
|
+
* @param {string} typeId
|
|
281
|
+
* @param {boolean} isRoot
|
|
282
|
+
*/
|
|
283
|
+
constructor(id, typeId, isRoot) {
|
|
284
|
+
super();
|
|
285
|
+
this.data = {
|
|
286
|
+
payload: { id, typeId, isRoot },
|
|
287
|
+
action: MetaboxModularConfiguratorActions.setComponent,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Represents a command to set component material by its slot ID and Material ID.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* import { SetComponentMaterial, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
297
|
+
*
|
|
298
|
+
* //...Assume that the integration is already implemented
|
|
299
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
300
|
+
* api.sendCommandToMetaBox(new SetComponentMaterial(
|
|
301
|
+
* 'carpaint',
|
|
302
|
+
* 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
|
|
303
|
+
* ));
|
|
304
|
+
* };
|
|
305
|
+
*/
|
|
306
|
+
class SetComponentMaterial extends CommandBase {
|
|
307
|
+
/**
|
|
308
|
+
* Creates an instance of SetComponentMaterial.
|
|
309
|
+
*
|
|
310
|
+
* @param {string} slotId - The slot ID.
|
|
311
|
+
* @param {string} materialId - The material ID.
|
|
312
|
+
*/
|
|
313
|
+
constructor(slotId, materialId) {
|
|
314
|
+
super();
|
|
315
|
+
this.data = {
|
|
316
|
+
payload: { slotId, materialId },
|
|
317
|
+
action: MetaboxModularConfiguratorActions.setComponentMaterialById,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Represents a command to set a component type by its ID.
|
|
324
|
+
*
|
|
325
|
+
* @remarks
|
|
326
|
+
* This action sends a message to the Metabox API to set a component type using the provided component type ID.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* import { SetComponentType, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
330
|
+
*
|
|
331
|
+
* //...Assume that the integration is already implemented
|
|
332
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
333
|
+
* api.setCommandToMetaBox(new SetComponentType(
|
|
334
|
+
* 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3'
|
|
335
|
+
* ));
|
|
336
|
+
* };
|
|
337
|
+
*/
|
|
338
|
+
class SetComponentType extends CommandBase {
|
|
339
|
+
/**
|
|
340
|
+
* Creates an instance of SetComponentType.
|
|
341
|
+
*
|
|
342
|
+
* @param {string} typeId
|
|
343
|
+
*/
|
|
344
|
+
constructor(typeId) {
|
|
345
|
+
super();
|
|
346
|
+
this.data = {
|
|
347
|
+
payload: { typeId },
|
|
348
|
+
action: MetaboxModularConfiguratorActions.setComponentType,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Represents a command to set the environment by its ID.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* import { SetEnvironment, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
358
|
+
*
|
|
359
|
+
* //...Assume that the integration is already implemented
|
|
360
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
361
|
+
* // Change the environment to '55555555-1234-1234-1234-01234567890'
|
|
362
|
+
* api.sendCommandToMetaBox(new SetEnvironment('55555555-1234-1234-1234-01234567890'));
|
|
363
|
+
* };
|
|
364
|
+
*/
|
|
365
|
+
class SetEnvironment extends CommandBase {
|
|
366
|
+
/**
|
|
367
|
+
* Creates an instance of SetEnvironment.
|
|
368
|
+
*
|
|
369
|
+
* @param {string} environmentId - The environment ID.
|
|
370
|
+
*/
|
|
371
|
+
constructor(environmentId) {
|
|
372
|
+
super();
|
|
373
|
+
this.data = {
|
|
374
|
+
payload: { id: environmentId },
|
|
375
|
+
action: MetaboxModularConfiguratorActions.setEnvironment,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Represents a command to set an environment material by its slot ID and Material ID.
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* import { SetEnvironmentMaterial, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
385
|
+
*
|
|
386
|
+
* //...Assume that the integration is already implemented
|
|
387
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
388
|
+
* api.sendCommandToMetaBox(new SetEnvironmentMaterial(
|
|
389
|
+
* 'carpaint',
|
|
390
|
+
* 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
|
|
391
|
+
* ));
|
|
392
|
+
* };
|
|
393
|
+
*/
|
|
394
|
+
class SetEnvironmentMaterial extends CommandBase {
|
|
395
|
+
/**
|
|
396
|
+
* Creates an instance of SetEnvironmentMaterial.
|
|
397
|
+
*
|
|
398
|
+
* @param {string} slotId - The slot ID.
|
|
399
|
+
* @param {string} materialId - The material ID.
|
|
400
|
+
*/
|
|
401
|
+
constructor(slotId, materialId) {
|
|
402
|
+
super();
|
|
403
|
+
this.data = {
|
|
404
|
+
payload: { slotId, materialId },
|
|
405
|
+
action: MetaboxModularConfiguratorActions.setEnvironmentMaterialById,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Represents a command to toggle the Embedded to the Metabox Menu.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* import { ShowEmbeddedMenu, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
415
|
+
*
|
|
416
|
+
* //...Assume that the integration is already implemented
|
|
417
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
418
|
+
* api.setCommandToMetaBox(new ShowEmbeddedMenu(true));
|
|
419
|
+
* };
|
|
420
|
+
*/
|
|
421
|
+
class ShowEmbeddedMenu extends CommandBase {
|
|
422
|
+
/**
|
|
423
|
+
* Creates an instance of ShowEmbeddedMenu.
|
|
424
|
+
*
|
|
425
|
+
* @param {boolean} visible - A flag indicating whether the embedded menu should be visible.
|
|
426
|
+
*/
|
|
427
|
+
constructor(visible) {
|
|
428
|
+
super();
|
|
429
|
+
this.data = {
|
|
430
|
+
action: MetaboxModularConfiguratorActions.showEmbeddedMenu,
|
|
431
|
+
payload: { visible },
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Represents a command to toggle the Unreal Overlay Interface Menu.
|
|
438
|
+
*
|
|
439
|
+
* @remarks
|
|
440
|
+
* This action sends a message to the Metabox API to toggle the visibility of the Unreal overlay UI.
|
|
441
|
+
*
|
|
442
|
+
* @example
|
|
443
|
+
* import { ShowOverlayInterface, Communicator } from '@3dsource/metabox-modular-configurator-api';
|
|
444
|
+
*
|
|
445
|
+
* //...Assume that the integration is already implemented
|
|
446
|
+
* window.env3DSource.apiReady = (api:Communicator) => {
|
|
447
|
+
* api.setCommandToMetaBox(new ShowOverlayInterface(true));
|
|
448
|
+
* };
|
|
449
|
+
*/
|
|
450
|
+
class ShowOverlayInterface extends CommandBase {
|
|
451
|
+
/**
|
|
452
|
+
* Creates an instance of ShowOverlayInterface.
|
|
453
|
+
*
|
|
454
|
+
* @param {boolean} visible - A flag indicating whether the Unreal overlay UI should be visible.
|
|
455
|
+
*/
|
|
456
|
+
constructor(visible) {
|
|
457
|
+
super();
|
|
458
|
+
this.data = {
|
|
459
|
+
action: MetaboxModularConfiguratorActions.showOverlayInterface,
|
|
460
|
+
payload: { visible },
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* @internal
|
|
467
|
+
* @hidden
|
|
468
|
+
* Represents a command to send Unreal command.
|
|
469
|
+
*/
|
|
470
|
+
class UnrealCommand extends CommandBase {
|
|
471
|
+
constructor(payload) {
|
|
472
|
+
super();
|
|
473
|
+
this.data = {
|
|
474
|
+
payload,
|
|
475
|
+
action: MetaboxModularConfiguratorActions.sendCommandToUnreal,
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* @experimental
|
|
482
|
+
* @internal
|
|
483
|
+
* @hidden
|
|
484
|
+
* @remarks
|
|
485
|
+
* Represents a command to watch Unreal callbacks from Unreal engine.
|
|
486
|
+
* Can be useful for custom solutions.
|
|
487
|
+
*/
|
|
488
|
+
class WatchCallbacks extends CommandBase {
|
|
489
|
+
/**
|
|
490
|
+
* Creates an instance of WatchCallbacks.
|
|
491
|
+
*
|
|
492
|
+
* @remarks
|
|
493
|
+
* This constructor does not require any parameters.
|
|
494
|
+
*/
|
|
495
|
+
constructor() {
|
|
496
|
+
super();
|
|
497
|
+
this.data = { action: MetaboxModularConfiguratorActions.watchCallbacks };
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* EventDispatcher is a class that manages event listeners and dispatches events to them.
|
|
503
|
+
*/
|
|
504
|
+
class EventDispatcher {
|
|
505
|
+
constructor() {
|
|
506
|
+
/**
|
|
507
|
+
* Storage for callback listeners by message type.
|
|
508
|
+
*/
|
|
509
|
+
this.listeners = [];
|
|
510
|
+
}
|
|
511
|
+
destroy() {
|
|
512
|
+
this.listeners = [];
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Adds an event listener for receiving specific types of messages.
|
|
516
|
+
*
|
|
517
|
+
* @param messageType - The message type to listen for.
|
|
518
|
+
* @param callback - The callback function to execute when a message is received.
|
|
519
|
+
*/
|
|
520
|
+
addEventListener(messageType, callback) {
|
|
521
|
+
this.listeners.push({ messageType, callback });
|
|
522
|
+
return this;
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Dispatches an event to all listeners of a specific message type.
|
|
526
|
+
*
|
|
527
|
+
* @param messageType - The message type.
|
|
528
|
+
* @param data - The data associated with the event.
|
|
529
|
+
*/
|
|
530
|
+
dispatchEvent(messageType, data) {
|
|
531
|
+
this.listeners
|
|
532
|
+
.filter((listener) => listener.messageType === messageType)
|
|
533
|
+
.forEach((listener) => listener.callback(data));
|
|
534
|
+
return this;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Removes an event listener for a specific type of message.
|
|
538
|
+
*
|
|
539
|
+
* @param messageType - The message type.
|
|
540
|
+
* @param callback - The callback function to remove.
|
|
541
|
+
*/
|
|
542
|
+
removeEventListener(messageType, callback) {
|
|
543
|
+
this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
|
|
544
|
+
return this;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Handles messaging between the host page and embedded to the Metabox content.
|
|
550
|
+
* @internal Use {@link Communicator.createInstance} or the {@link integrateMetaBox} helper to instantiate.
|
|
551
|
+
*/
|
|
552
|
+
class Communicator extends EventDispatcher {
|
|
553
|
+
/**
|
|
554
|
+
* Singleton reference to the current communicator instance.
|
|
555
|
+
*/
|
|
556
|
+
static { this.instance = null; }
|
|
557
|
+
/**
|
|
558
|
+
* Constructs a Communicator, replacing any existing instance, and begins listening for messages.
|
|
559
|
+
* @internal
|
|
560
|
+
*/
|
|
561
|
+
constructor() {
|
|
562
|
+
super();
|
|
563
|
+
/**
|
|
564
|
+
* Bound handler for incoming postMessage events.
|
|
565
|
+
*/
|
|
566
|
+
this.binder = this.handleMessageReceived.bind(this, 'metaboxData');
|
|
567
|
+
Communicator.instance?.destroy();
|
|
568
|
+
Communicator.instance = this;
|
|
569
|
+
window.addEventListener('message', this.binder);
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Listens for to the Metabox to signal readiness, then initializes communicator.
|
|
573
|
+
* @param apiReadyCallback - Called with the new Communicator once to the Metabox is loaded.
|
|
574
|
+
*/
|
|
575
|
+
static createInstance(apiReadyCallback) {
|
|
576
|
+
const startHandler = (event) => {
|
|
577
|
+
const message = event.data;
|
|
578
|
+
if (message?.envelope?.action === 'appLoaded') {
|
|
579
|
+
apiReadyCallback(new Communicator());
|
|
580
|
+
window.removeEventListener('message', startHandler);
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
window.addEventListener('message', startHandler);
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Cleans up resources and stops listening for messages.
|
|
587
|
+
*/
|
|
588
|
+
destroy() {
|
|
589
|
+
super.destroy();
|
|
590
|
+
window.removeEventListener('message', this.binder);
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Posts a command to the Metabox iframe.
|
|
594
|
+
* @param command - An action command containing data to send.
|
|
595
|
+
*/
|
|
596
|
+
sendCommandToMetaBox(command) {
|
|
597
|
+
const { data } = command;
|
|
598
|
+
const iframe = document.getElementById('embeddedContent');
|
|
599
|
+
if (!iframe?.contentWindow) {
|
|
600
|
+
console.warn('to the Metabox IFrame not found or not ready.');
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const message = {
|
|
604
|
+
host: 'metaBoxHost',
|
|
605
|
+
payload: { ...data },
|
|
606
|
+
};
|
|
607
|
+
// Replace '*' with a specific origin in production for better security.
|
|
608
|
+
iframe.contentWindow.postMessage(message, '*');
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Registers an event listener for messages dispatched by Metabox.
|
|
612
|
+
* @override
|
|
613
|
+
*/
|
|
614
|
+
addEventListener(messageType, callback) {
|
|
615
|
+
return super.addEventListener(messageType, callback);
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Dispatches a typed event to all registered listeners.
|
|
619
|
+
* @override
|
|
620
|
+
*/
|
|
621
|
+
dispatchEvent(messageType, data) {
|
|
622
|
+
return super.dispatchEvent(messageType, data);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Removes a previously registered event listener.
|
|
626
|
+
* @override
|
|
627
|
+
*/
|
|
628
|
+
removeEventListener(messageType, callback) {
|
|
629
|
+
return super.removeEventListener(messageType, callback);
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Filters and dispatches incoming messages from to the Metabox.
|
|
633
|
+
* @param messageType - Expected message type for filtering ('metaboxData').
|
|
634
|
+
* @param event - The postMessage event received on window.
|
|
635
|
+
*/
|
|
636
|
+
handleMessageReceived(messageType, event) {
|
|
637
|
+
// Optionally validate event.origin for security.
|
|
638
|
+
if (messageType !== 'metaboxData' || event.data?.host !== 'metabox') {
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const { eventType, payload } = event.data.payload;
|
|
642
|
+
this.dispatchEvent(eventType, payload);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Integrates Metabox Modular Configurator into the provided layout.
|
|
648
|
+
*
|
|
649
|
+
* @remarks
|
|
650
|
+
* This function injects an iframe into a specified container element in the DOM and initializes
|
|
651
|
+
* the communicator for interacting with the Metabox embedded content.
|
|
652
|
+
*
|
|
653
|
+
* @param iframeSrc - The source URL of the Metabox iframe.
|
|
654
|
+
* Additional url parameters:
|
|
655
|
+
* ```javascript
|
|
656
|
+
* sidebar=false - disables the internal Metabox menu
|
|
657
|
+
* introImage=https://example.com/image.png - sets the intro image
|
|
658
|
+
* ```
|
|
659
|
+
* i.e., a final link will look like https://metabox.3dsource.com/metabox-configurator/modular/your-configurator-id?sidebar=false&introImage=https://example.com/image.png
|
|
660
|
+
*
|
|
661
|
+
* @param containerId - The ID of the layout element. Defaults to 'embed3DSource'.
|
|
662
|
+
* @param apiReadyCallback
|
|
663
|
+
* @throws Error if the container element with the specified ID is not found.
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* import {
|
|
667
|
+
* integrateMetaBox,
|
|
668
|
+
* } from '@3dsource/metabox-modular-configurator-api';
|
|
669
|
+
*
|
|
670
|
+
* integrateMetaBox(
|
|
671
|
+
* 'https://metabox.3dsource.com/metabox-configurator/modular/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
|
|
672
|
+
* 'embed3DSource'
|
|
673
|
+
* );
|
|
674
|
+
*/
|
|
675
|
+
function integrateMetabox(iframeSrc, containerId = 'embed3DSource', apiReadyCallback) {
|
|
676
|
+
const container = document.getElementById(containerId);
|
|
677
|
+
if (!container) {
|
|
678
|
+
throw new Error(`Container element with id ${containerId} not found`);
|
|
679
|
+
}
|
|
680
|
+
Communicator.createInstance(apiReadyCallback);
|
|
681
|
+
const style = document.createElement('style');
|
|
682
|
+
style.innerHTML = `
|
|
683
|
+
#${containerId} {
|
|
684
|
+
width: 100%;
|
|
685
|
+
height: 100%;
|
|
686
|
+
overflow: hidden;
|
|
687
|
+
position: relative;
|
|
688
|
+
}
|
|
689
|
+
`;
|
|
690
|
+
document.head.appendChild(style);
|
|
691
|
+
const iframe = document.createElement('iframe');
|
|
692
|
+
iframe.setAttribute('referrerPolicy', 'no-referrer-when-downgrade');
|
|
693
|
+
iframe.setAttribute('id', 'embeddedContent');
|
|
694
|
+
iframe.style.border = '0';
|
|
695
|
+
iframe.style.width = '100%';
|
|
696
|
+
iframe.style.height = '100%';
|
|
697
|
+
iframe.style.overflow = 'hidden';
|
|
698
|
+
iframe.src = iframeSrc;
|
|
699
|
+
container.appendChild(iframe);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Saves an image by triggering a download.
|
|
704
|
+
*
|
|
705
|
+
* @remarks
|
|
706
|
+
* This function creates an anchor element, sets its `href` attribute to the provided image URL,
|
|
707
|
+
* and triggers a click event to initiate a download with the specified filename.
|
|
708
|
+
*
|
|
709
|
+
* @param imageUrl - The URL of the image to save.
|
|
710
|
+
* @param filename - The name of the file to save.
|
|
711
|
+
*/
|
|
712
|
+
function saveImage(imageUrl, filename) {
|
|
713
|
+
const a = document.createElement('a');
|
|
714
|
+
a.href = imageUrl;
|
|
715
|
+
a.download = filename;
|
|
716
|
+
document.body.appendChild(a);
|
|
717
|
+
a.click();
|
|
718
|
+
document.body.removeChild(a);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Pauses execution for a specified duration.
|
|
723
|
+
*
|
|
724
|
+
* @remarks
|
|
725
|
+
* This function returns a promise that resolves after the given number of milliseconds.
|
|
726
|
+
*
|
|
727
|
+
* @param time - The time to sleep in milliseconds.
|
|
728
|
+
* @returns A promise that resolves after the specified duration.
|
|
729
|
+
*/
|
|
730
|
+
function sleep(time) {
|
|
731
|
+
return new Promise((resolve) => setTimeout(resolve, time));
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Sets the URL parameters based on the provided state.
|
|
736
|
+
*
|
|
737
|
+
* @remarks
|
|
738
|
+
* This function updates the current URL by replacing its search parameters with the key-value pairs from the provided state object.
|
|
739
|
+
*
|
|
740
|
+
* @param state - An object containing key-value pairs to set as URL parameters.
|
|
741
|
+
*/
|
|
742
|
+
function setUrlParams(state) {
|
|
743
|
+
const url = new URL(window.location.href);
|
|
744
|
+
url.search = '';
|
|
745
|
+
Object.entries(state).forEach(([key, value]) => {
|
|
746
|
+
url.searchParams.set(key, value);
|
|
747
|
+
});
|
|
748
|
+
history.replaceState(null, '', url.toString());
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Retrieves the URL parameters as an object.
|
|
752
|
+
*
|
|
753
|
+
* @remarks
|
|
754
|
+
* This function parses the current URL's search parameters and returns them as a key-value object.
|
|
755
|
+
*
|
|
756
|
+
* @returns An object containing the URL parameters.
|
|
757
|
+
*/
|
|
758
|
+
function getUrlParams() {
|
|
759
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
760
|
+
const selections = {};
|
|
761
|
+
urlParams.forEach((value, key) => {
|
|
762
|
+
selections[key] = value;
|
|
763
|
+
});
|
|
764
|
+
return selections;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Generated bundle index. Do not edit.
|
|
769
|
+
*/
|
|
770
|
+
|
|
771
|
+
export { ApplyZoom, CommandBase, Communicator, EventDispatcher, GetPdf, GetScreenshot, InitShowcase, MetaboxModularConfiguratorActions, PauseShowcase, PlayShowcase, ResetCamera, SetComponent, SetComponentMaterial, SetComponentType, SetEnvironment, SetEnvironmentMaterial, ShowEmbeddedMenu, ShowOverlayInterface, StopShowcase, UnrealCommand, WatchCallbacks, fromCommunicatorEvent, getUrlParams, integrateMetabox, saveImage, setUrlParams, sleep };
|
|
772
|
+
//# sourceMappingURL=3dsource-metabox-modular-configurator-api.mjs.map
|