@3dsource/metabox-front-api 0.0.13 → 0.0.15

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 CHANGED
@@ -4,8 +4,26 @@
4
4
  * All Rights Reserved
5
5
  */
6
6
  /**
7
- * Base class for all API actions.
8
- * @hidden
7
+ * To integrate the MetaBox configurator into an HTML element, use the following example:
8
+ *
9
+ * **We strongly recommend using the option with npm and locking it to the latest version at the time of integration, as the option with cdn can change rapidly and may cause unexpected behavior in your existing code. See root documentation.**
10
+ *
11
+ * ```typescript
12
+ * import { integrateMetaBox } from 'https://cdn.jsdelivr.net/npm/@3dsource/metabox-front-api@latest/dist/index.js';
13
+ *
14
+ * integrateMetaBox(
15
+ * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
16
+ * 'embed3DSource'
17
+ * );
18
+ *
19
+ * // Set up the global API environment
20
+ * window.env3DSource = window.env3DSource || {};
21
+ *
22
+ * //Listen for the API ready event
23
+ * window.env3DSource.apiReady = (api) => {
24
+ * // Use the API actions here
25
+ * };
26
+ * ```
9
27
  */
10
28
  class ApiAction {
11
29
  constructor() {
@@ -43,10 +61,18 @@ const ToMetaBoxApiAction = {
43
61
  * All Rights Reserved
44
62
  */
45
63
  /**
46
- * Represents an action to toggle the Embedded Menu in MetaBox.
64
+ * {@inheritDoc ApiAction}
65
+ *
66
+ * Represents an action to toggle the Embedded MetaBox Menu.
47
67
  *
48
68
  * @remarks
49
69
  * This action sends a message to the MetaBox API to toggle the visibility of the embedded menu.
70
+ *
71
+ * @example
72
+ * import { ShowEmbeddedMenu } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
73
+ * window.env3DSource.apiReady = (api) => {
74
+ * new ShowEmbeddedMenu(true);
75
+ * };
50
76
  */
51
77
  class ShowEmbeddedMenu extends ApiAction {
52
78
  /**
@@ -71,22 +97,14 @@ class ShowEmbeddedMenu extends ApiAction {
71
97
  * All Rights Reserved
72
98
  */
73
99
  /**
100
+ * {@inheritDoc ApiAction}
101
+ *
74
102
  * Represents an action to set the environment by its ID.
75
103
  * @remarks
76
104
  * This action sends a message to the MetaBox API to load the environment level using the specified environment ID.
77
- * It requires the MetaBox API to be properly initialized and an event listener to be registered.
78
105
  *
79
106
  * @example
80
- * import { integrateMetaBox, SetEnvironment } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
81
- *
82
- * // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
83
- * integrateMetaBox(
84
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
85
- * 'embed3DSource'
86
- * );
87
- *
88
- * // Setup API ready callback
89
- * window.env3DSource = window.env3DSource || {};
107
+ * import { SetEnvironment } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
90
108
  * window.env3DSource.apiReady = (api) => {
91
109
  * // Change the environment to '55555555-1234-1234-1234-01234567890'
92
110
  * new SetEnvironment('55555555-1234-1234-1234-01234567890');
@@ -115,10 +133,20 @@ class SetEnvironment extends ApiAction {
115
133
  * All Rights Reserved
116
134
  */
117
135
  /**
136
+ * {@inheritDoc ApiAction}
137
+ *
118
138
  * Represents an action to set a product by its ID.
119
139
  *
120
140
  * @remarks
121
141
  * This action sends a message to the MetaBox API to set a product using the provided product ID.
142
+ *
143
+ * @example
144
+ * import { SetProduct } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
145
+ * window.env3DSource.apiReady = (api) => {
146
+ * new SetProduct(
147
+ * 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3'
148
+ * );
149
+ * };
122
150
  */
123
151
  class SetProduct extends ApiAction {
124
152
  /**
@@ -143,33 +171,19 @@ class SetProduct extends ApiAction {
143
171
  * All Rights Reserved
144
172
  */
145
173
  /**
174
+ * {@inheritDoc ApiAction}
175
+ *
146
176
  * Represents an action to get a PDF from Metabox.
147
177
  * @remarks
148
178
  * This class sends a message to the MetaBox API to generate a PDF based on the current configuration.
149
179
  * This action does not require any parameters.
150
180
  *
151
- * **Important:** Before invoking this action, ensure that the MetaBox viewport is fully loaded. This is indicated by the event:
152
- *```javascript
153
- * data.eventType === 'viewportReady' && data.payload === true.
154
- * ```
155
- * Only then request be processed.
181
+ * **Important**: Invoking this action will buffer it until the viewport is ready. Only then will the request be processed.
156
182
  *
157
183
  * @example
158
- * import { integrateMetaBox, GetPdf } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
159
- *
160
- * integrateMetaBox(
161
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
162
- * 'embed3DSource'
163
- * );
164
- *
165
- * window.env3DSource = window.env3DSource || {};
184
+ * import { GetPdf } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
166
185
  * window.env3DSource.apiReady = (api) => {
167
- * api.addEventListener('metaboxData', (data) => {
168
- * if (data.eventType === 'viewportReady' && data.payload === true) {
169
- * // Request a PDF of the current configuration
170
- * new GetPdf();
171
- * }
172
- * });
186
+ * new GetPdf();
173
187
  * };
174
188
  */
175
189
  class GetPdf extends ApiAction {
@@ -193,41 +207,21 @@ class GetPdf extends ApiAction {
193
207
  * All Rights Reserved
194
208
  */
195
209
  /**
210
+ * {@inheritDoc ApiAction}
211
+ *
196
212
  * Represents an action to set a material by its ID.
197
213
  *
198
214
  * @remarks
199
215
  * This action sends a message to the MetaBox API to update the material on a specific product slot using the provided identifiers.
200
-
201
- * **Important:** Before invoking this action, ensure that the MetaBox already dispatched
202
- * as minimum one eventType:"basicConfiguratorDataWithState":
203
- * ```javascript
204
- * data.eventType === 'basicConfiguratorDataWithState'.
205
- * ```
206
- * Only then request be processed.
207
216
  *
208
217
  * @example
209
218
  * import { SetMaterial } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
210
- *
211
- * // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
212
- * integrateMetaBox(
213
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
214
- * 'embed3DSource'
215
- * );
216
- *
217
- * // Set up the global API environment
218
- * window.env3DSource = window.env3DSource || {};
219
219
  * window.env3DSource.apiReady = (api) => {
220
- * // Listen for events from the MetaBox API
221
- * api.addEventListener('metaboxData', (data) => {
222
- * // Process SetMaterial
223
- * if (data.eventType === 'basicConfiguratorDataWithState') {
224
- * new SetMaterial(
225
- * 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3',
226
- * 'carpaint',
227
- * 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
228
- * );
229
- * }
230
- * });
220
+ * new SetMaterial(
221
+ * 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3',
222
+ * 'carpaint',
223
+ * 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
224
+ * );
231
225
  * };
232
226
  */
233
227
  class SetMaterial extends ApiAction {
@@ -257,46 +251,26 @@ class SetMaterial extends ApiAction {
257
251
  * All Rights Reserved
258
252
  */
259
253
  /**
260
- * Represents an action to set a material by its ID.
254
+ * {@inheritDoc ApiAction}
255
+ *
256
+ * Represents an action to set a environment material by its ID.
261
257
  *
262
258
  * @remarks
263
- * This action sends a message to the MetaBox API to update the material on a specific product slot using the provided identifiers.
264
-
265
- * **Important:** Before invoking this action, ensure that the MetaBox already dispatched
266
- * as minimum one eventType:"basicConfiguratorDataWithState":
267
- * ```javascript
268
- * data.eventType === 'basicConfiguratorDataWithState'.
269
- * ```
270
- * Only then request be processed.
259
+ * This action sends a message to the MetaBox API to update the material on a specific environment slot using the provided identifiers.
271
260
  *
272
261
  * @example
273
- * import { SetMaterial } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
274
- *
275
- * // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
276
- * integrateMetaBox(
277
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
278
- * 'embed3DSource'
279
- * );
280
- *
281
- * // Set up the global API environment
282
- * window.env3DSource = window.env3DSource || {};
262
+ * import { SetEnvironmentMaterial } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
283
263
  * window.env3DSource.apiReady = (api) => {
284
- * // Listen for events from the MetaBox API
285
- * api.addEventListener('metaboxData', (data) => {
286
- * // Process SetMaterial
287
- * if (data.eventType === 'basicConfiguratorDataWithState') {
288
- * new SetEnvironmentMaterial(
289
- * 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3',
290
- * 'carpaint',
291
- * 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
292
- * );
293
- * }
294
- * });
264
+ * new SetEnvironmentMaterial(
265
+ * 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3',
266
+ * 'carpaint',
267
+ * 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
268
+ * );
295
269
  * };
296
270
  */
297
271
  class SetEnvironmentMaterial extends ApiAction {
298
272
  /**
299
- * Creates an instance of SetMaterial.
273
+ * Creates an instance of SetEnvironmentMaterial.
300
274
  *
301
275
  * @param productId - The product ID.
302
276
  * @param slotId - The slot ID.
@@ -321,40 +295,25 @@ class SetEnvironmentMaterial extends ApiAction {
321
295
  * All Rights Reserved
322
296
  */
323
297
  /**
298
+ * {@inheritDoc ApiAction}
299
+ *
324
300
  * Represents a fast render action.
325
301
  * @remarks
326
302
  * This class sends a message to the MetaBox API to obtain a fast render based on the provided format and optional size.
327
303
  *
328
- * **Important:** Before invoking this action, ensure that the MetaBox viewport is fully loaded. This is indicated by the event:
329
- * ```javascript
330
- * data.eventType === 'viewportReady' && data.payload === true.
331
- * ```
332
- * Only then request be processed.
304
+ * **Important**: Invoking this action will buffer it until the viewport is ready. Only then will the request be processed.
333
305
  *
334
306
  * @example
335
- * import { integrateMetaBox, GetFastRender, saveImage } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
336
- *
337
- * // Integrate the MetaBox configurator into an HTML element with the ID 'embed3DSource'
338
- * integrateMetaBox(
339
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
340
- * 'embed3DSource'
341
- * );
342
- *
343
- * // Set up the global API environment
344
- * window.env3DSource = window.env3DSource || {};
307
+ * import { GetFastRender,saveImage } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
345
308
  * window.env3DSource.apiReady = (api) => {
346
309
  * // Listen for events from the MetaBox API
347
310
  * api.addEventListener('metaboxData', (data) => {
348
- * // Ensure the viewport is ready before sending render requests
349
- * if (data.eventType === 'viewportReady' && data.payload === true) {
350
- * // Request a fast render (PNG image with 1024x1024 dimensions)
351
- * new GetFastRender('image/png', { x: 1024, y: 1024 });
352
- * }
353
311
  * // Process the fast render response
354
312
  * if (data.eventType === 'fastRender') {
355
313
  * saveImage(data.payload, 'Render.png');
356
314
  * }
357
315
  * });
316
+ * new GetFastRender('image/png', { x: 1024, y: 1024 });
358
317
  * };
359
318
  */
360
319
  class GetFastRender extends ApiAction {
@@ -381,6 +340,7 @@ class GetFastRender extends ApiAction {
381
340
  * All Rights Reserved
382
341
  */
383
342
  /**
343
+ * {@inheritDoc ApiAction}
384
344
  * @internal
385
345
  * @hidden
386
346
  * Represents an action to send Unreal command.
@@ -402,6 +362,8 @@ class SendCommandToUnreal extends ApiAction {
402
362
  */
403
363
  /**
404
364
  * @experimental
365
+ * @internal
366
+ * @hidden
405
367
  * @remarks
406
368
  * Represents an action to watch Unreal callbacks from Unreal engine.
407
369
  * Can be useful for custom solutions.
@@ -426,7 +388,56 @@ class WatchCallbacks extends ApiAction {
426
388
  * 3DSource.com, Sergii Karanda steve@3dsource.com
427
389
  * All Rights Reserved
428
390
  */
429
- const messageType = 'metaboxData';
391
+ /**
392
+ * EventDispatcher is a class that manages event listeners and dispatches events to them.
393
+ */
394
+ class EventDispatcher {
395
+ constructor() {
396
+ /**
397
+ * Storage for callback listeners by message type.
398
+ */
399
+ this.listeners = [];
400
+ }
401
+ destroy() {
402
+ this.listeners = [];
403
+ }
404
+ /**
405
+ * Adds an event listener for receiving specific types of messages.
406
+ *
407
+ * @param messageType - The message type to listen for.
408
+ * @param callback - The callback function to execute when a message is received.
409
+ */
410
+ addEventListener(messageType, callback) {
411
+ this.listeners.push({ messageType, callback });
412
+ }
413
+ /**
414
+ * Dispatches an event to all listeners of a specific message type.
415
+ *
416
+ * @param messageType - The message type.
417
+ * @param data - The data associated with the event.
418
+ */
419
+ dispatchEvent(messageType, data) {
420
+ this.listeners
421
+ .filter((listener) => listener.messageType === messageType)
422
+ .forEach((listener) => listener.callback(data));
423
+ }
424
+ /**
425
+ * Removes an event listener for a specific type of message.
426
+ *
427
+ * @param messageType - The message type.
428
+ * @param callback - The callback function to remove.
429
+ */
430
+ removeEventListener(messageType, callback) {
431
+ this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
432
+ }
433
+ }
434
+
435
+ /*
436
+ * Copyright (c) 2025.
437
+ * 3DSource.com, Sergii Karanda steve@3dsource.com
438
+ * All Rights Reserved
439
+ */
440
+ const binderMessageType = 'metaboxData';
430
441
  /**
431
442
  * API class to handle communication with the embedded content.
432
443
  * @internal Do not Create an instance of this class directly.
@@ -436,7 +447,7 @@ const messageType = 'metaboxData';
436
447
  * This class sets up a communication channel with the embedded content via window messages.
437
448
  * @hidden
438
449
  */
439
- class Communicator {
450
+ class Communicator extends EventDispatcher {
440
451
  /**
441
452
  * Creates an instance of Communicator.
442
453
  * @internal
@@ -446,14 +457,11 @@ class Communicator {
446
457
  */
447
458
  constructor() {
448
459
  var _a;
449
- /**
450
- * Storage for callback listeners by message type.
451
- */
452
- this.listeners = [];
460
+ super();
453
461
  /**
454
462
  * Binder function for handling incoming messages.
455
463
  */
456
- this.binder = this.handleMessageReceived.bind(this, messageType);
464
+ this.binder = this.handleMessageReceived.bind(this, binderMessageType);
457
465
  (_a = Communicator.instance) === null || _a === void 0 ? void 0 : _a.destroy();
458
466
  Communicator.instance = this;
459
467
  window.addEventListener('message', this.binder);
@@ -462,7 +470,7 @@ class Communicator {
462
470
  * Destroys the communicator instance by clearing listeners and removing event listeners.
463
471
  */
464
472
  destroy() {
465
- this.listeners = [];
473
+ super.destroy();
466
474
  window.removeEventListener('message', this.binder);
467
475
  }
468
476
  /**
@@ -480,26 +488,6 @@ class Communicator {
480
488
  // In production, replace "*" with the actual origin for added security.
481
489
  (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage(dataToSend, '*');
482
490
  }
483
- /**
484
- * Adds an event listener for receiving specific types of messages.
485
- *
486
- * @param messageType - The message type to listen for.
487
- * @param callback - The callback function to execute when a message is received.
488
- */
489
- addEventListener(messageType, callback) {
490
- this.listeners.push({ messageType, callback });
491
- }
492
- /**
493
- * Dispatches an event to all listeners of a specific message type.
494
- *
495
- * @param messageType - The message type.
496
- * @param data - The data associated with the event.
497
- */
498
- dispatchEvent(messageType, data) {
499
- this.listeners
500
- .filter((listener) => listener.messageType === messageType)
501
- .forEach((listener) => listener.callback(data));
502
- }
503
491
  /**
504
492
  * Handles incoming messages by invoking the appropriate callbacks.
505
493
  *
@@ -516,20 +504,7 @@ class Communicator {
516
504
  if (((_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.host) !== 'metabox') {
517
505
  return;
518
506
  }
519
- this.listeners
520
- .filter((listener) => listener.messageType === messageType)
521
- .forEach((listener) => {
522
- listener.callback(event.data.payload);
523
- });
524
- }
525
- /**
526
- * Removes an event listener for a specific type of message.
527
- *
528
- * @param messageType - The message type.
529
- * @param callback - The callback function to remove.
530
- */
531
- removeEventListener(messageType, callback) {
532
- this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
507
+ this.dispatchEvent(messageType, event.data.payload);
533
508
  }
534
509
  /**
535
510
  * Initializes the communicator when the embedded content is ready.
@@ -538,7 +513,7 @@ class Communicator {
538
513
  * This function sets up a listener that waits for the 'appLoaded' action and then initializes the Communicator.
539
514
  */
540
515
  static createInstance() {
541
- window.env3DSource = window.env3DSource || {};
516
+ window.env3DSource = Object.assign({}, window.env3DSource);
542
517
  const startHandler = (event) => {
543
518
  var _a, _b;
544
519
  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' &&
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}