@3dsource/metabox-front-api 1.2.1-dev.0 → 1.2.1-dev.3

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.
Files changed (36) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +218 -216
  3. package/{dist/index.esm.js → fesm2022/3dsource-metabox-front-api.mjs} +47 -46
  4. package/fesm2022/3dsource-metabox-front-api.mjs.map +1 -0
  5. package/index.d.ts +860 -0
  6. package/package.json +39 -53
  7. package/dist/actions/CommandBase.d.ts +0 -7
  8. package/dist/actions/GetFastRender.d.ts +0 -2
  9. package/dist/actions/GetPdf.d.ts +0 -24
  10. package/dist/actions/GetScreenshot.d.ts +0 -34
  11. package/dist/actions/SetEnvironment.d.ts +0 -21
  12. package/dist/actions/SetEnvironmentMaterial.d.ts +0 -24
  13. package/dist/actions/SetMaterial.d.ts +0 -24
  14. package/dist/actions/SetProduct.d.ts +0 -25
  15. package/dist/actions/ShowEmbeddedMenu.d.ts +0 -20
  16. package/dist/actions/ShowOverlayInterface.d.ts +0 -23
  17. package/dist/actions/UnrealCommand.d.ts +0 -14
  18. package/dist/actions/WatchCallbacks.d.ts +0 -18
  19. package/dist/const/FromMetaboxApiAction.d.ts +0 -58
  20. package/dist/index.d.ts +0 -23
  21. package/dist/index.esm.js.map +0 -1
  22. package/dist/index.umd.js +0 -604
  23. package/dist/index.umd.js.map +0 -1
  24. package/dist/interfaces/FromMetaboxMessagePayloads.d.ts +0 -134
  25. package/dist/interfaces/ToMetaboxMessagePayloads.d.ts +0 -72
  26. package/dist/interfaces/UniversalConfigurator.d.ts +0 -213
  27. package/dist/interfaces/application-device-types.d.ts +0 -1
  28. package/dist/lib/Communicator.d.ts +0 -57
  29. package/dist/lib/EventDispatcher.d.ts +0 -39
  30. package/dist/lib/UrlUtils.d.ts +0 -18
  31. package/dist/lib/fromCommunicatorEvent.d.ts +0 -9
  32. package/dist/lib/integrateMetaBox.d.ts +0 -31
  33. package/dist/lib/saveImage.d.ts +0 -11
  34. package/dist/lib/sleep.d.ts +0 -10
  35. package/dist/lib/test/Communicator.spec.d.ts +0 -1
  36. package/dist/lib/test/integrateMetabox.spec.d.ts +0 -9
package/dist/index.umd.js DELETED
@@ -1,604 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'rxjs'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["metabox-front-api"] = {}, global.rxjs));
5
- })(this, (function (exports, rxjs) { 'use strict';
6
-
7
- /**
8
- * Base class for commands sent to the MetaBox.
9
- */
10
- class CommandBase {
11
- }
12
-
13
- /**
14
- * Represents a command to toggle the Unreal Overlay Interface Menu.
15
- *
16
- * @remarks
17
- * This action sends a message to the MetaBox API to toggle the visibility of the Unreal overlay UI.
18
- *
19
- * @example
20
- * import { ShowOverlayInterface,Communicator } from '@3dsource/metabox-front-api';
21
- *
22
- * //...Assume that the integration is already implemented
23
- * window.env3DSource.apiReady = (api:Communicator) => {
24
- * api.setCommandToMetaBox(new ShowOverlayInterface(true));
25
- * };
26
- */
27
- class ShowOverlayInterface extends CommandBase {
28
- /**
29
- * Creates an instance of ShowOverlayInterface.
30
- *
31
- * @param visible - A flag indicating whether the Unreal overlay UI should be visible.
32
- */
33
- constructor(visible) {
34
- super();
35
- this.data = {
36
- action: 'showOverlayInterface',
37
- payload: {
38
- visible,
39
- },
40
- };
41
- }
42
- }
43
-
44
- /**
45
- * Represents a command to toggle the Embedded MetaBox Menu.
46
- *
47
- * @example
48
- * import { ShowEmbeddedMenu,Communicator } from '@3dsource/metabox-front-api';
49
- *
50
- * //...Assume that the integration is already implemented
51
- * window.env3DSource.apiReady = (api:Communicator) => {
52
- * api.setCommandToMetaBox(new ShowEmbeddedMenu(true));
53
- * };
54
- */
55
- class ShowEmbeddedMenu extends CommandBase {
56
- /**
57
- * Creates an instance of ShowEmbeddedMenu.
58
- *
59
- * @param visible - A flag indicating whether the embedded menu should be visible.
60
- */
61
- constructor(visible) {
62
- super();
63
- this.data = {
64
- action: 'showEmbeddedMenu',
65
- payload: {
66
- visible,
67
- },
68
- };
69
- }
70
- }
71
-
72
- /**
73
- * Represents a command to set the environment by its ID.
74
- *
75
- * @example
76
- * import { SetEnvironment,Communicator } from '@3dsource/metabox-front-api';
77
- *
78
- * //...Assume that the integration is already implemented
79
- * window.env3DSource.apiReady = (api:Communicator) => {
80
- * // Change the environment to '55555555-1234-1234-1234-01234567890'
81
- * api.sendCommandToMetaBox(new SetEnvironment('55555555-1234-1234-1234-01234567890'));
82
- * };
83
- */
84
- class SetEnvironment extends CommandBase {
85
- /**
86
- * Creates an instance of SetEnvironment.
87
- *
88
- * @param environmentId - The environment ID.
89
- */
90
- constructor(environmentId) {
91
- super();
92
- this.data = {
93
- payload: {
94
- id: environmentId,
95
- },
96
- action: 'loadLevel',
97
- };
98
- }
99
- }
100
-
101
- /**
102
- * Represents a command to set a product by its ID.
103
- *
104
- * @remarks
105
- * This action sends a message to the MetaBox API to set a product using the provided product ID.
106
- *
107
- * @example
108
- * import { SetProduct,Communicator } from '@3dsource/metabox-front-api';
109
- *
110
- * //...Assume that the integration is already implemented
111
- * window.env3DSource.apiReady = (api:Communicator) => {
112
- * api.setCommandToMetaBox(new SetProduct(
113
- * 'ffea6b5c-3a8a-4f56-9417-e605acb5cca3'
114
- * ));
115
- * };
116
- */
117
- class SetProduct extends CommandBase {
118
- /**
119
- * Creates an instance of SetProduct.
120
- *
121
- * @param productId - The product ID.
122
- */
123
- constructor(productId) {
124
- super();
125
- this.data = {
126
- payload: {
127
- productId,
128
- },
129
- action: 'setProduct',
130
- };
131
- }
132
- }
133
-
134
- /**
135
- * Represents a command to get a PDF from Metabox.
136
- * @remarks
137
- * This class sends a message to the MetaBox API to generate a PDF based on the current configuration.
138
- * This action does not require any parameters.
139
- *
140
- * **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
141
- *
142
- * @example
143
- * import { GetPdf,Communicator } from '@3dsource/metabox-front-api';
144
- * window.env3DSource.apiReady = (api:Communicator) => {
145
- * api.sendCommandToMetaBox(new GetPdf());
146
- * };
147
- */
148
- class GetPdf extends CommandBase {
149
- /**
150
- * Creates an instance of GetPdf.
151
- *
152
- * @remarks
153
- * This constructor does not require any parameters.
154
- */
155
- constructor() {
156
- super();
157
- this.data = {
158
- action: 'getPdf',
159
- };
160
- }
161
- }
162
-
163
- /**
164
- * Represents a command to set a material by its ID.
165
- *
166
- * @example
167
- * import { SetMaterial,Communicator } from '@3dsource/metabox-front-api';
168
- *
169
- * //...Assume that the integration is already implemented
170
- * window.env3DSource.apiReady = (api:Communicator) => {
171
- * api.sendCommandToMetaBox(new SetMaterial(
172
- * 'carpaint',
173
- * 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
174
- * ));
175
- * };
176
- */
177
- class SetMaterial extends CommandBase {
178
- /**
179
- * Creates an instance of SetMaterial.
180
- *
181
- * @param slotId - The slot ID.
182
- * @param materialId - The material ID.
183
- */
184
- constructor(slotId, materialId) {
185
- super();
186
- this.data = {
187
- payload: {
188
- slotId,
189
- materialId,
190
- },
191
- action: 'setMaterialById',
192
- };
193
- }
194
- }
195
-
196
- /**
197
- * Represents a command to set an environment material by its ID.
198
- *
199
- * @example
200
- * import { SetEnvironmentMaterial,Communicator } from '@3dsource/metabox-front-api';
201
- *
202
- * //...Assume that the integration is already implemented
203
- * window.env3DSource.apiReady = (api:Communicator) => {
204
- * api.sendCommandToMetaBox(new SetEnvironmentMaterial(
205
- * 'carpaint',
206
- * 'dd829d6e-9200-47a7-8d5b-af5df89b7e91',
207
- * ));
208
- * };
209
- */
210
- class SetEnvironmentMaterial extends CommandBase {
211
- /**
212
- * Creates an instance of SetEnvironmentMaterial.
213
- *
214
- * @param slotId - The slot ID.
215
- * @param materialId - The material ID.
216
- */
217
- constructor(slotId, materialId) {
218
- super();
219
- this.data = {
220
- payload: {
221
- slotId,
222
- materialId,
223
- },
224
- action: 'setEnvironmentMaterialById',
225
- };
226
- }
227
- }
228
-
229
- /**
230
- * Represents a command to get fast render.
231
- *
232
- * **Important**: Invoking this command will buffer it until the viewport is ready. Only then will the request be processed.
233
- *
234
- * @example
235
- * import { GetScreenshot,Communicator,saveImage } from '@3dsource/metabox-front-api';
236
- * window.env3DSource.apiReady = (api:Communicator) => {
237
- * // Listen for events from the MetaBox API
238
- * api.addEventListener('screenshot', (data) => {
239
- * // Process the fast render response
240
- * saveImage(data, 'Render.png');
241
- * });
242
- * api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1024, y: 1024 }));
243
- * };
244
- */
245
- class GetScreenshot extends CommandBase {
246
- /**
247
- * Constructs an instance of GetScreenshot.
248
- * @param mimeType - The output format.
249
- * @param size - Optional size in pixels.
250
- */
251
- constructor(mimeType, size) {
252
- super();
253
- this.data = {
254
- payload: {
255
- format: mimeType,
256
- size,
257
- },
258
- action: 'getScreenshot',
259
- };
260
- }
261
- }
262
-
263
- /**
264
- * @internal
265
- * @hidden
266
- * Represents a command to send Unreal command.
267
- */
268
- class UnrealCommand extends CommandBase {
269
- constructor(payload) {
270
- super();
271
- this.data = {
272
- payload,
273
- action: 'sendCommandToUnreal',
274
- };
275
- }
276
- }
277
- /**
278
- * @deprecated, use `UnrealCommand` instead.
279
- */
280
- const SendCommandToUnreal = UnrealCommand;
281
-
282
- /**
283
- * @experimental
284
- * @internal
285
- * @hidden
286
- * @remarks
287
- * Represents a command to watch Unreal callbacks from Unreal engine.
288
- * Can be useful for custom solutions.
289
- */
290
- class WatchCallbacks extends CommandBase {
291
- /**
292
- * Creates an instance of WatchCallbacks.
293
- *
294
- * @remarks
295
- * This constructor does not require any parameters.
296
- */
297
- constructor() {
298
- super();
299
- this.data = {
300
- action: 'watchCallbacks',
301
- };
302
- }
303
- }
304
-
305
- /**
306
- * EventDispatcher is a class that manages event listeners and dispatches events to them.
307
- */
308
- class EventDispatcher {
309
- constructor() {
310
- /**
311
- * Storage for callback listeners by message type.
312
- */
313
- this.listeners = [];
314
- }
315
- destroy() {
316
- this.listeners = [];
317
- }
318
- /**
319
- * Adds an event listener for receiving specific types of messages.
320
- *
321
- * @param messageType - The message type to listen for.
322
- * @param callback - The callback function to execute when a message is received.
323
- */
324
- addEventListener(messageType, callback) {
325
- this.listeners.push({ messageType, callback });
326
- return this;
327
- }
328
- /**
329
- * Dispatches an event to all listeners of a specific message type.
330
- *
331
- * @param messageType - The message type.
332
- * @param data - The data associated with the event.
333
- */
334
- dispatchEvent(messageType, data) {
335
- this.listeners
336
- .filter((listener) => listener.messageType === messageType)
337
- .forEach((listener) => listener.callback(data));
338
- return this;
339
- }
340
- /**
341
- * Removes an event listener for a specific type of message.
342
- *
343
- * @param messageType - The message type.
344
- * @param callback - The callback function to remove.
345
- */
346
- removeEventListener(messageType, callback) {
347
- this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
348
- return this;
349
- }
350
- }
351
-
352
- /**
353
- * Handles messaging between the host page and embedded MetaBox content.
354
- * @internal Use {@link Communicator.createInstance} or the {@link integrateMetaBox} helper to instantiate.
355
- */
356
- class Communicator extends EventDispatcher {
357
- /**
358
- * Constructs a Communicator, replacing any existing instance, and begins listening for messages.
359
- * @internal
360
- */
361
- constructor() {
362
- var _a;
363
- super();
364
- /**
365
- * Bound handler for incoming postMessage events.
366
- */
367
- this.binder = this.handleMessageReceived.bind(this, 'metaboxData');
368
- (_a = Communicator.instance) === null || _a === void 0 ? void 0 : _a.destroy();
369
- Communicator.instance = this;
370
- window.addEventListener('message', this.binder);
371
- }
372
- /**
373
- * Cleans up resources and stops listening for messages.
374
- */
375
- destroy() {
376
- super.destroy();
377
- window.removeEventListener('message', this.binder);
378
- }
379
- /**
380
- * Posts a command to the MetaBox iframe.
381
- * @param command - An action command containing data to send.
382
- */
383
- sendCommandToMetaBox(command) {
384
- const { data } = command;
385
- const iframe = document.getElementById('embeddedContent');
386
- if (!(iframe === null || iframe === void 0 ? void 0 : iframe.contentWindow)) {
387
- console.warn('MetaBox IFrame not found or not ready.');
388
- return;
389
- }
390
- const message = {
391
- host: 'metaBoxHost',
392
- payload: Object.assign({}, data),
393
- };
394
- // Replace '*' with a specific origin in production for better security.
395
- iframe.contentWindow.postMessage(message, '*');
396
- }
397
- /**
398
- * Filters and dispatches incoming messages from MetaBox.
399
- * @param messageType - Expected message type for filtering ('metaboxData').
400
- * @param event - The postMessage event received on window.
401
- */
402
- handleMessageReceived(messageType, event) {
403
- var _a;
404
- // Optionally validate event.origin for security.
405
- if (messageType !== 'metaboxData' || ((_a = event.data) === null || _a === void 0 ? void 0 : _a.host) !== 'metabox') {
406
- return;
407
- }
408
- const { eventType, payload } = event.data.payload;
409
- this.dispatchEvent(eventType, payload);
410
- }
411
- /**
412
- * Listens for MetaBox to signal readiness, then initializes communicator.
413
- * @param apiReadyCallback - Called with the new Communicator once MetaBox is loaded.
414
- */
415
- static createInstance(apiReadyCallback) {
416
- const startHandler = (event) => {
417
- var _a;
418
- const message = event.data;
419
- if (((_a = message === null || message === void 0 ? void 0 : message.payload) === null || _a === void 0 ? void 0 : _a.action) === 'appLoaded') {
420
- apiReadyCallback(new Communicator());
421
- window.removeEventListener('message', startHandler);
422
- }
423
- };
424
- window.addEventListener('message', startHandler);
425
- }
426
- /**
427
- * Registers an event listener for messages dispatched by MetaBox.
428
- * @override
429
- */
430
- addEventListener(messageType, callback) {
431
- return super.addEventListener(messageType, callback);
432
- }
433
- /**
434
- * Dispatches a typed event to all registered listeners.
435
- * @override
436
- */
437
- dispatchEvent(messageType, data) {
438
- return super.dispatchEvent(messageType, data);
439
- }
440
- /**
441
- * Removes a previously registered event listener.
442
- * @override
443
- */
444
- removeEventListener(messageType, callback) {
445
- return super.removeEventListener(messageType, callback);
446
- }
447
- }
448
- /**
449
- * Singleton reference to the current communicator instance.
450
- */
451
- Communicator.instance = null;
452
-
453
- /**
454
- * Integrates the MetaBox into the provided layout.
455
- *
456
- * @remarks
457
- * This function injects an iframe into a specified container element in the DOM and initializes
458
- * the communicator for interacting with the MetaBox embedded content.
459
- *
460
- * @param iframeSrc - The source URL of the MetaBox iframe.
461
- * Additional url parameters:
462
- * ```javascript
463
- * sidebar=false - disables the internal MetaBox menu
464
- * introImage=https://example.com/image.png - sets the intro image
465
- * ```
466
- * 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
467
- *
468
- * @param containerId - The ID of the layout element. Defaults to 'embed3DSource'.
469
- * @param apiReadyCallback
470
- * @throws Error if the container element with the specified ID is not found.
471
- *
472
- * @example
473
- * import {
474
- * integrateMetaBox,
475
- * } from '@3dsource/metabox-front-api';
476
- *
477
- * integrateMetaBox(
478
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
479
- * 'embed3DSource'
480
- * );
481
- */
482
- function integrateMetaBox(iframeSrc, containerId = 'embed3DSource', apiReadyCallback) {
483
- const container = document.getElementById(containerId);
484
- if (!container) {
485
- throw new Error(`Container element with id ${containerId} not found`);
486
- }
487
- Communicator.createInstance(apiReadyCallback);
488
- const style = document.createElement('style');
489
- style.innerHTML = `
490
- #${containerId} {
491
- width: 100%;
492
- height: 100%;
493
- overflow: hidden;
494
- position: relative;
495
- }
496
- `;
497
- document.head.appendChild(style);
498
- const iframe = document.createElement('iframe');
499
- iframe.setAttribute('referrerPolicy', 'no-referrer-when-downgrade');
500
- iframe.setAttribute('id', 'embeddedContent');
501
- iframe.style.border = '0';
502
- iframe.style.width = '100%';
503
- iframe.style.height = '100%';
504
- iframe.style.overflow = 'hidden';
505
- iframe.src = iframeSrc;
506
- container.appendChild(iframe);
507
- }
508
-
509
- /**
510
- * Wrapper helper function to use it with rxJs same as `fromEvent` to listen to events from the Communicator.
511
- * @param target
512
- * @param eventName
513
- */
514
- function fromCommunicatorEvent(target, eventName) {
515
- return rxjs.fromEvent(target, eventName);
516
- }
517
-
518
- /**
519
- * Saves an image by triggering a download.
520
- *
521
- * @remarks
522
- * This function creates an anchor element, sets its `href` attribute to the provided image URL,
523
- * and triggers a click event to initiate a download with the specified filename.
524
- *
525
- * @param imageUrl - The URL of the image to save.
526
- * @param filename - The name of the file to save.
527
- */
528
- function saveImage(imageUrl, filename) {
529
- const a = document.createElement('a');
530
- a.href = imageUrl;
531
- a.download = filename;
532
- document.body.appendChild(a);
533
- a.click();
534
- document.body.removeChild(a);
535
- }
536
-
537
- /**
538
- * Pauses execution for a specified duration.
539
- *
540
- * @remarks
541
- * This function returns a promise that resolves after the given number of milliseconds.
542
- *
543
- * @param time - The time to sleep in milliseconds.
544
- * @returns A promise that resolves after the specified duration.
545
- */
546
- function sleep(time) {
547
- return new Promise((resolve) => setTimeout(resolve, time));
548
- }
549
-
550
- /**
551
- * Sets the URL parameters based on the provided state.
552
- *
553
- * @remarks
554
- * This function updates the current URL by replacing its search parameters with the key-value pairs from the provided state object.
555
- *
556
- * @param state - An object containing key-value pairs to set as URL parameters.
557
- */
558
- function setUrlParams(state) {
559
- const url = new URL(window.location.href);
560
- url.search = '';
561
- Object.entries(state).forEach(([key, value]) => {
562
- url.searchParams.set(key, value);
563
- });
564
- history.replaceState(null, '', url.toString());
565
- }
566
- /**
567
- * Retrieves the URL parameters as an object.
568
- *
569
- * @remarks
570
- * This function parses the current URL's search parameters and returns them as a key-value object.
571
- *
572
- * @returns An object containing the URL parameters.
573
- */
574
- function getUrlParams() {
575
- const urlParams = new URLSearchParams(window.location.search);
576
- const selections = {};
577
- urlParams.forEach((value, key) => {
578
- selections[key] = value;
579
- });
580
- return selections;
581
- }
582
-
583
- exports.CommandBase = CommandBase;
584
- exports.Communicator = Communicator;
585
- exports.GetPdf = GetPdf;
586
- exports.GetScreenshot = GetScreenshot;
587
- exports.SendCommandToUnreal = SendCommandToUnreal;
588
- exports.SetEnvironment = SetEnvironment;
589
- exports.SetEnvironmentMaterial = SetEnvironmentMaterial;
590
- exports.SetMaterial = SetMaterial;
591
- exports.SetProduct = SetProduct;
592
- exports.ShowEmbeddedMenu = ShowEmbeddedMenu;
593
- exports.ShowOverlayInterface = ShowOverlayInterface;
594
- exports.UnrealCommand = UnrealCommand;
595
- exports.WatchCallbacks = WatchCallbacks;
596
- exports.fromCommunicatorEvent = fromCommunicatorEvent;
597
- exports.getUrlParams = getUrlParams;
598
- exports.integrateMetaBox = integrateMetaBox;
599
- exports.saveImage = saveImage;
600
- exports.setUrlParams = setUrlParams;
601
- exports.sleep = sleep;
602
-
603
- }));
604
- //# sourceMappingURL=index.umd.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.umd.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}