@3dsource/metabox-front-api 0.0.3 → 0.0.4

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/README.md +1 -1
  2. package/dist/actions/ApiAction.d.ts +4 -0
  3. package/dist/actions/ApiAction.js +10 -0
  4. package/dist/actions/GetFastRender.d.ts +55 -0
  5. package/dist/actions/GetFastRender.js +61 -0
  6. package/dist/actions/GetPdf.d.ts +40 -0
  7. package/dist/actions/GetPdf.js +51 -0
  8. package/dist/actions/SetEnvironment.d.ts +31 -0
  9. package/dist/actions/SetEnvironment.js +45 -0
  10. package/dist/actions/SetMaterial.d.ts +49 -0
  11. package/dist/actions/SetMaterial.js +65 -0
  12. package/dist/actions/SetProduct.d.ts +15 -0
  13. package/dist/actions/SetProduct.js +29 -0
  14. package/dist/actions/ShowEmbeddedMenu.d.ts +15 -0
  15. package/dist/actions/ShowEmbeddedMenu.js +29 -0
  16. package/dist/actions/WatchCallbacks.d.ts +16 -0
  17. package/dist/actions/WatchCallbacks.js +27 -0
  18. package/dist/index.d.ts +18 -0
  19. package/dist/index.js +22 -0
  20. package/dist/interfaces/FromMetaboxMessagePayloads.d.ts +130 -0
  21. package/dist/interfaces/FromMetaboxMessagePayloads.js +41 -0
  22. package/dist/interfaces/ToMetaboxMessagePayloads.d.ts +53 -0
  23. package/dist/interfaces/ToMetaboxMessagePayloads.js +12 -0
  24. package/dist/interfaces/UniversalConfigurator.d.ts +152 -0
  25. package/dist/interfaces/UniversalConfigurator.js +1 -0
  26. package/dist/lib/UrlUtils.d.ts +18 -0
  27. package/dist/lib/UrlUtils.js +37 -0
  28. package/dist/lib/initializeCommunicator.d.ts +87 -0
  29. package/dist/lib/initializeCommunicator.js +133 -0
  30. package/dist/lib/integrateMetaBox.d.ts +24 -0
  31. package/dist/lib/integrateMetaBox.js +55 -0
  32. package/dist/lib/saveImage.d.ts +11 -0
  33. package/dist/lib/saveImage.js +23 -0
  34. package/dist/lib/sleep.d.ts +10 -0
  35. package/dist/lib/sleep.js +17 -0
  36. package/package.json +2 -2
@@ -0,0 +1,130 @@
1
+ import type { ConfiguratorEnvelope } from './UniversalConfigurator';
2
+ export declare const FromMetaboxApiActions: {
3
+ readonly bridge: "bridge";
4
+ readonly unrealCallback: "unrealCallback";
5
+ readonly unrealFeatureBridge: "unrealFeatureBridge";
6
+ /**
7
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
8
+ */
9
+ readonly configuratorDataWithStateBridge: "configuratorDataWithStateBridge";
10
+ /**
11
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
12
+ */
13
+ readonly configuratorDataBridge: "configuratorDataBridge";
14
+ /**
15
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
16
+ */
17
+ readonly menuDataBridge: "menuDataBridge";
18
+ /**
19
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
20
+ */
21
+ readonly flatMenuDataBridge: "flatMenuDataBridge";
22
+ /**
23
+ * @deprecated, use Bridge with isFullscreen
24
+ */
25
+ readonly fullScreenBridge: "fullScreenBridge";
26
+ /**
27
+ * @deprecated, use Bridge with streamResolution
28
+ */
29
+ readonly videoSize: "videoSize";
30
+ /**
31
+ * Used in platform, must be removed in future
32
+ */
33
+ /**
34
+ * @deprecated\
35
+ */
36
+ readonly unrealResponse: "unrealResponse";
37
+ /**
38
+ * @deprecated\
39
+ */
40
+ readonly getFastRender: "getFastRender";
41
+ readonly appLoaded: "appLoaded";
42
+ };
43
+ export type FromMetaBoxAction = (typeof FromMetaboxApiActions)[keyof typeof FromMetaboxApiActions];
44
+ /**
45
+ * @internal
46
+ * Used for signaling that the app is loaded and ready to open api.
47
+ */
48
+ interface MCAppLoaded {
49
+ action: string;
50
+ eventType?: unknown;
51
+ payload?: unknown;
52
+ }
53
+ export interface FromMetaboxMessagePayloads {
54
+ /**
55
+ * Dispatches Every time the configurator data is updated.
56
+ * I.e. when the user changes the configuration. (Material, Product, Environment)
57
+ */
58
+ basicConfiguratorDataWithState: ConfiguratorEnvelope;
59
+ /**
60
+ * @internal
61
+ * Dispatches Every time when configurator connected/disconnected with Unreal Engine.
62
+ */
63
+ dataChannelConnected: boolean;
64
+ /**
65
+ * Dispatches Every time when Unreal Viewport is visible/hidden.
66
+ */
67
+ viewportReady: boolean;
68
+ /**
69
+ * Dispatches Every time viewport shows/hides its internal menu .
70
+ */
71
+ isFullscreen: boolean;
72
+ /**
73
+ * Dispatches at the moment while configurator loads. Shows status information.
74
+ */
75
+ statusMessage: string | null;
76
+ /**
77
+ * @internal
78
+ * @experimental
79
+ * Dispatches every time when freeze-frame is arrived or updated.
80
+ */
81
+ freezeFrame: {
82
+ dataUrl: string | null;
83
+ progress: number | null;
84
+ };
85
+ /**
86
+ * @internal
87
+ * Dispatches every Unreal is sending command callback.
88
+ * Works only after new WatchCallbacks() command is sent.
89
+ */
90
+ unrealCallback: object;
91
+ /**
92
+ * Dispatches every time when Unreal is sending ordered render with new GetFastRender() command.
93
+ */
94
+ getFastRender: string | null;
95
+ /**
96
+ * @internal
97
+ * @callback
98
+ * Dispatches every Unreal state is changing, including all unreal module state.
99
+ */
100
+ unrealFeature: object;
101
+ /**
102
+ * Dispatches every time when Unreal is changing its video stream size.
103
+ */
104
+ streamResolution: {
105
+ width: number;
106
+ number: number;
107
+ };
108
+ }
109
+ export type FromMetaBoxApiEvents = keyof FromMetaboxMessagePayloads;
110
+ /**
111
+ * @internal
112
+ * This creates an all event envelopes defined in MetaBoxAPIPayloads.
113
+ */
114
+ export interface MetaBoxApiEnvelope<T extends FromMetaBoxApiEvents> {
115
+ action: 'bridge';
116
+ eventType: T;
117
+ payload: FromMetaboxMessagePayloads[T];
118
+ }
119
+ /**
120
+ * @internal
121
+ * This creates a union of all event envelopes defined in MetaBoxAPIPayloads.
122
+ */
123
+ export type FromMetaBoxAPIEnvelope = {
124
+ [K in keyof FromMetaboxMessagePayloads]: MetaBoxApiEnvelope<K>;
125
+ }[keyof FromMetaboxMessagePayloads] | MCAppLoaded;
126
+ export interface FromMetaBoxMessage {
127
+ host: 'metabox';
128
+ payload: FromMetaBoxAPIEnvelope;
129
+ }
130
+ export {};
@@ -0,0 +1,41 @@
1
+ export const FromMetaboxApiActions = {
2
+ bridge: 'bridge',
3
+ unrealCallback: 'unrealCallback',
4
+ unrealFeatureBridge: 'unrealFeatureBridge',
5
+ /**
6
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
7
+ */
8
+ configuratorDataWithStateBridge: 'configuratorDataWithStateBridge',
9
+ /**
10
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
11
+ */
12
+ configuratorDataBridge: 'configuratorDataBridge',
13
+ /**
14
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
15
+ */
16
+ menuDataBridge: 'menuDataBridge',
17
+ /**
18
+ * @deprecated, use MetaBoxApiAction.Bridge with eventType: "configuratorDataWithState"
19
+ */
20
+ flatMenuDataBridge: 'flatMenuDataBridge',
21
+ /**
22
+ * @deprecated, use Bridge with isFullscreen
23
+ */
24
+ fullScreenBridge: 'fullScreenBridge',
25
+ /**
26
+ * @deprecated, use Bridge with streamResolution
27
+ */
28
+ videoSize: 'videoSize',
29
+ /**
30
+ * Used in platform, must be removed in future
31
+ */
32
+ /**
33
+ * @deprecated\
34
+ */
35
+ unrealResponse: 'unrealResponse',
36
+ /**
37
+ * @deprecated\
38
+ */
39
+ getFastRender: 'getFastRender',
40
+ appLoaded: 'appLoaded',
41
+ };
@@ -0,0 +1,53 @@
1
+ import type { MimeType } from '../actions/GetFastRender';
2
+ export declare const ToMetaBoxApiActions: {
3
+ readonly getPdf: "getPdf";
4
+ readonly loadLevel: "loadLevel";
5
+ readonly setProduct: "setProduct";
6
+ readonly addResponse: "addResponse";
7
+ readonly getFastRender: "getFastRender";
8
+ readonly watchCallbacks: "watchCallbacks";
9
+ readonly setExternalMode: "setExternalMode";
10
+ readonly setMaterialById: "setMaterialById";
11
+ readonly showEmbeddedMenu: "showEmbeddedMenu";
12
+ readonly forceSetDeviceType: "forceSetDeviceType";
13
+ };
14
+ export type ToMetaBoxAction = (typeof ToMetaBoxApiActions)[keyof typeof ToMetaBoxApiActions];
15
+ export type ToMetaboxMessagePayload = ToMetaboxMessagePayloads[keyof ToMetaboxMessagePayloads];
16
+ export interface ToMetaboxMessagePayloads {
17
+ getPdf: void;
18
+ loadLevel: {
19
+ id: string;
20
+ };
21
+ setProduct: {
22
+ productId: string;
23
+ };
24
+ addResponse: void;
25
+ getFastRender: {
26
+ format: MimeType;
27
+ size?: {
28
+ x: number;
29
+ y: number;
30
+ };
31
+ };
32
+ watchCallbacks: void;
33
+ setExternalMode: void;
34
+ setMaterialById: {
35
+ productId: string;
36
+ slotId: string;
37
+ materialId: string;
38
+ };
39
+ showEmbeddedMenu: {
40
+ visible: boolean;
41
+ };
42
+ forceSetDeviceType: {
43
+ deviceType: 'deviceTypeAuto' | 'deviceTypeMobile' | 'deviceTypeDesktop';
44
+ };
45
+ }
46
+ export interface ToMetaBoxMessageEnvelope {
47
+ action: ToMetaBoxAction;
48
+ payload?: ToMetaboxMessagePayload | never;
49
+ }
50
+ export interface ToMetaBoxMessage {
51
+ host: 'metaBoxHost';
52
+ payload: ToMetaBoxMessageEnvelope;
53
+ }
@@ -0,0 +1,12 @@
1
+ export const ToMetaBoxApiActions = {
2
+ getPdf: 'getPdf',
3
+ loadLevel: 'loadLevel',
4
+ setProduct: 'setProduct',
5
+ addResponse: 'addResponse',
6
+ getFastRender: 'getFastRender',
7
+ watchCallbacks: 'watchCallbacks',
8
+ setExternalMode: 'setExternalMode',
9
+ setMaterialById: 'setMaterialById',
10
+ showEmbeddedMenu: 'showEmbeddedMenu',
11
+ forceSetDeviceType: 'forceSetDeviceType',
12
+ };
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Represents a configurator object with its configuration, selected materials,
3
+ * product identifier, and associated environment.
4
+ */
5
+ export interface ConfiguratorEnvelope {
6
+ /**
7
+ * A reference to the universal configurator.
8
+ */
9
+ configurator: UniversalConfigurator | null;
10
+ /**
11
+ * A record mapping material selections.
12
+ */
13
+ selectedMaterials: SelectedMaterials;
14
+ /**
15
+ * The unique product identifier.
16
+ */
17
+ productId: string;
18
+ /**
19
+ * The environment configuration for the universal configurator.
20
+ */
21
+ environment: UniversalConfiguratorEnvironment;
22
+ }
23
+ /**
24
+ * Represents the universal configurator with its basic properties and components.
25
+ */
26
+ export interface UniversalConfigurator {
27
+ /** Unique identifier for the configurator. */
28
+ id: string;
29
+ /** Indicates if the configurator is active. */
30
+ /** Label for the product collection. */
31
+ productCollectionLabel: string;
32
+ /** The name of the configurator. */
33
+ name: string;
34
+ /** The camera settings used in the configurator. */
35
+ /** Client information associated with the configurator. */
36
+ /** A list of products for the configurator. */
37
+ products: UniversalConfiguratorProduct[];
38
+ /** A list of available environments. */
39
+ environments: UniversalConfiguratorEnvironment[];
40
+ /** GraphQL typename for the configurator. */
41
+ __typename: 'UniversalConfigurator';
42
+ }
43
+ /**
44
+ * Represents a product within the universal configurator.
45
+ */
46
+ export interface UniversalConfiguratorProduct {
47
+ /** The product's unique identifier. */
48
+ productId: string;
49
+ /** Position index of the product. */
50
+ position: number;
51
+ /** Display label for the product. */
52
+ label: string;
53
+ /** Optional camera settings for the product. */
54
+ /** The detailed product information. */
55
+ product: Product;
56
+ /** GraphQL typename for the product. */
57
+ __typename: 'UniversalConfiguratorProduct';
58
+ }
59
+ /**
60
+ * Represents product details including visual assets and configuration.
61
+ */
62
+ export interface Product {
63
+ /** The title of the product. */
64
+ title: string;
65
+ /** List of thumbnails for the product. */
66
+ thumbnailList: Thumbnail[];
67
+ /** Available slots for the product. */
68
+ slots: Slot[];
69
+ /** The model information associated with the product. */
70
+ /** Unique identifier for the product. */
71
+ id: string;
72
+ /** Showcase details for the product. */
73
+ /** GraphQL typename for the product. */
74
+ __typename: 'Product';
75
+ }
76
+ /**
77
+ * Represents a thumbnail image entity with dimensions and URL path.
78
+ */
79
+ export interface Thumbnail {
80
+ /** Width of the thumbnail. */
81
+ width: number;
82
+ /** Height of the thumbnail. */
83
+ height: number;
84
+ /** URL path to the thumbnail image. */
85
+ urlPath: string;
86
+ /** GraphQL typename (as a string, flexible type). */
87
+ __typename: 'Thumbnail';
88
+ }
89
+ /**
90
+ * Represents a slot within a product that may allow material customization.
91
+ */
92
+ export interface Slot {
93
+ /** Unique identifier for the slot. */
94
+ id: string;
95
+ /** Display label for the slot. */
96
+ label: string;
97
+ /** A list of enabled materials for the slot. */
98
+ enabledMaterials: Material[];
99
+ /** GraphQL typename for the slot. */
100
+ __typename: 'Slot';
101
+ }
102
+ /**
103
+ * Represents a material that can be applied to a product slot.
104
+ */
105
+ export interface Material {
106
+ /** Unique identifier for the material. */
107
+ id: string;
108
+ /** Title or name of the material. */
109
+ title: string;
110
+ /** List of thumbnails for the material. */
111
+ thumbnailList: Thumbnail[];
112
+ /** The model information associated with the material. */
113
+ /** GraphQL typename for the material. */
114
+ __typename: 'Material';
115
+ }
116
+ /**
117
+ * Represents the environment settings for a universal configurator.
118
+ */
119
+ export interface UniversalConfiguratorEnvironment {
120
+ /** Unique identifier for the environment. */
121
+ environmentId: string;
122
+ /** Position index of the environment. */
123
+ position: number;
124
+ /** Display label for the environment. */
125
+ label: string;
126
+ /** Detailed environment configuration. */
127
+ environment: Environment;
128
+ /** GraphQL typename for the environment. */
129
+ __typename: 'UniversalConfiguratorEnvironment';
130
+ }
131
+ /**
132
+ * Represents an environment with associated media and configuration.
133
+ */
134
+ export interface Environment {
135
+ /** Unique identifier for the environment. */
136
+ id: string;
137
+ /** Title or name of the environment. */
138
+ title: string;
139
+ /** Flag indicating if UDS is enabled. */
140
+ /** The north yaw of the sun in the environment. */
141
+ /** The UDS hour setting for the environment. */
142
+ /** GraphQL typename for the environment. */
143
+ __typename: 'Environment';
144
+ /** List of thumbnails for the environment (optional). */
145
+ thumbnailList: Thumbnail[];
146
+ /** List of slots for the environment (optional). */
147
+ slots: Slot[];
148
+ }
149
+ /**
150
+ * Defines the structure for a mapping of selected materials.
151
+ */
152
+ export type SelectedMaterials = Record<string, Record<string, string>>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Sets the URL parameters based on the provided state.
3
+ *
4
+ * @remarks
5
+ * This function updates the current URL by replacing its search parameters with the key-value pairs from the provided state object.
6
+ *
7
+ * @param state - An object containing key-value pairs to set as URL parameters.
8
+ */
9
+ export declare function setUrlParams(state: Record<string, string>): void;
10
+ /**
11
+ * Retrieves the URL parameters as an object.
12
+ *
13
+ * @remarks
14
+ * This function parses the current URL's search parameters and returns them as a key-value object.
15
+ *
16
+ * @returns An object containing the URL parameters.
17
+ */
18
+ export declare function getUrlParams(): Record<string, string>;
@@ -0,0 +1,37 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * 3DSource.com, Sergii Karanda steve@3dsource.com
4
+ * All Rights Reserved
5
+ */
6
+ /**
7
+ * Sets the URL parameters based on the provided state.
8
+ *
9
+ * @remarks
10
+ * This function updates the current URL by replacing its search parameters with the key-value pairs from the provided state object.
11
+ *
12
+ * @param state - An object containing key-value pairs to set as URL parameters.
13
+ */
14
+ export function setUrlParams(state) {
15
+ const url = new URL(window.location.href);
16
+ url.search = '';
17
+ Object.entries(state).forEach(([key, value]) => {
18
+ url.searchParams.set(key, value);
19
+ });
20
+ history.replaceState(null, '', url.toString());
21
+ }
22
+ /**
23
+ * Retrieves the URL parameters as an object.
24
+ *
25
+ * @remarks
26
+ * This function parses the current URL's search parameters and returns them as a key-value object.
27
+ *
28
+ * @returns An object containing the URL parameters.
29
+ */
30
+ export function getUrlParams() {
31
+ const urlParams = new URLSearchParams(window.location.search);
32
+ const selections = {};
33
+ urlParams.forEach((value, key) => {
34
+ selections[key] = value;
35
+ });
36
+ return selections;
37
+ }
@@ -0,0 +1,87 @@
1
+ import { ToMetaBoxMessageEnvelope } from '../interfaces/ToMetaboxMessagePayloads';
2
+ declare global {
3
+ interface Window {
4
+ env3DSource: {
5
+ api: Communicator;
6
+ apiReady: (api: Communicator) => void;
7
+ };
8
+ }
9
+ }
10
+ /**
11
+ * API class to handle communication with the embedded content.
12
+ * @internal Do not Create an instance of this class directly.
13
+ * integrateMetabox will create an instance of this class.
14
+ *
15
+ * @remarks
16
+ * This class sets up a communication channel with the embedded content via window messages.
17
+ */
18
+ export declare class Communicator {
19
+ /**
20
+ * Storage for callback listeners by message type.
21
+ */
22
+ private listeners;
23
+ /**
24
+ * Binder function for handling incoming messages.
25
+ */
26
+ private binder;
27
+ /**
28
+ * The singleton instance of the communicator.
29
+ */
30
+ static instance: Communicator | null;
31
+ /**
32
+ * Creates an instance of Communicator.
33
+ * @internal
34
+ * @remarks
35
+ * If an instance already exists, it will be destroyed before creating a new one.
36
+ * The instance listens for messages from the iframe.
37
+ */
38
+ constructor();
39
+ /**
40
+ * Destroys the communicator instance by clearing listeners and removing event listeners.
41
+ */
42
+ destroy(): void;
43
+ /**
44
+ * Sends a message to the embedded content (MetaBox).
45
+ *
46
+ * @param data - The data to send.
47
+ */
48
+ sendMessageToMetaBox(data: ToMetaBoxMessageEnvelope): void;
49
+ /**
50
+ * Adds an event listener for receiving specific types of messages.
51
+ *
52
+ * @param messageType - The message type to listen for.
53
+ * @param callback - The callback function to execute when a message is received.
54
+ */
55
+ addEventListener(messageType: string, callback: (data: unknown) => void): void;
56
+ /**
57
+ * Dispatches an event to all listeners of a specific message type.
58
+ *
59
+ * @param messageType - The message type.
60
+ * @param data - The data associated with the event.
61
+ */
62
+ dispatchEvent(messageType: string, data: unknown): void;
63
+ /**
64
+ * Handles incoming messages by invoking the appropriate callbacks.
65
+ *
66
+ * @param messageType - The message type to filter for.
67
+ * @param event - The incoming message event.
68
+ *
69
+ * @remarks
70
+ * Optionally, you can perform an origin check for added security.
71
+ */
72
+ private handleMessageReceived;
73
+ /**
74
+ * Removes an event listener for a specific type of message.
75
+ *
76
+ * @param messageType - The message type.
77
+ * @param callback - The callback function to remove.
78
+ */
79
+ removeEventListener(messageType: string, callback: (data: unknown) => void): void;
80
+ /**
81
+ * Initializes the communicator when the embedded content is ready.
82
+ * @internal
83
+ * @remarks
84
+ * This function sets up a listener that waits for the 'appLoaded' action and then initializes the Communicator.
85
+ */
86
+ static createInstance(): void;
87
+ }
@@ -0,0 +1,133 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * 3DSource.com, Sergii Karanda steve@3dsource.com
4
+ * All Rights Reserved
5
+ */
6
+ const messageType = 'metaboxData';
7
+ /**
8
+ * API class to handle communication with the embedded content.
9
+ * @internal Do not Create an instance of this class directly.
10
+ * integrateMetabox will create an instance of this class.
11
+ *
12
+ * @remarks
13
+ * This class sets up a communication channel with the embedded content via window messages.
14
+ */
15
+ export class Communicator {
16
+ /**
17
+ * Creates an instance of Communicator.
18
+ * @internal
19
+ * @remarks
20
+ * If an instance already exists, it will be destroyed before creating a new one.
21
+ * The instance listens for messages from the iframe.
22
+ */
23
+ constructor() {
24
+ var _a;
25
+ /**
26
+ * Storage for callback listeners by message type.
27
+ */
28
+ this.listeners = [];
29
+ /**
30
+ * Binder function for handling incoming messages.
31
+ */
32
+ this.binder = this.handleMessageReceived.bind(this, messageType);
33
+ (_a = Communicator.instance) === null || _a === void 0 ? void 0 : _a.destroy();
34
+ Communicator.instance = this;
35
+ window.addEventListener('message', this.binder);
36
+ }
37
+ /**
38
+ * Destroys the communicator instance by clearing listeners and removing event listeners.
39
+ */
40
+ destroy() {
41
+ this.listeners = [];
42
+ window.removeEventListener('message', this.binder);
43
+ }
44
+ /**
45
+ * Sends a message to the embedded content (MetaBox).
46
+ *
47
+ * @param data - The data to send.
48
+ */
49
+ sendMessageToMetaBox(data) {
50
+ var _a;
51
+ const iframe = document.getElementById('embeddedContent');
52
+ const dataToSend = {
53
+ host: 'metaBoxHost',
54
+ payload: Object.assign({}, data),
55
+ };
56
+ // In production, replace "*" with the actual origin for added security.
57
+ (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage(dataToSend, '*');
58
+ }
59
+ /**
60
+ * Adds an event listener for receiving specific types of messages.
61
+ *
62
+ * @param messageType - The message type to listen for.
63
+ * @param callback - The callback function to execute when a message is received.
64
+ */
65
+ addEventListener(messageType, callback) {
66
+ this.listeners.push({ messageType, callback });
67
+ }
68
+ /**
69
+ * Dispatches an event to all listeners of a specific message type.
70
+ *
71
+ * @param messageType - The message type.
72
+ * @param data - The data associated with the event.
73
+ */
74
+ dispatchEvent(messageType, data) {
75
+ this.listeners
76
+ .filter((listener) => listener.messageType === messageType)
77
+ .forEach((listener) => listener.callback(data));
78
+ }
79
+ /**
80
+ * Handles incoming messages by invoking the appropriate callbacks.
81
+ *
82
+ * @param messageType - The message type to filter for.
83
+ * @param event - The incoming message event.
84
+ *
85
+ * @remarks
86
+ * Optionally, you can perform an origin check for added security.
87
+ */
88
+ handleMessageReceived(messageType, event) {
89
+ var _a;
90
+ // Optional: Perform origin check here for added security
91
+ // if (event.origin !== "EXPECTED_ORIGIN") return;
92
+ if (((_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.host) !== 'metabox') {
93
+ return;
94
+ }
95
+ this.listeners
96
+ .filter((listener) => listener.messageType === messageType)
97
+ .forEach((listener) => {
98
+ listener.callback(event.data.payload);
99
+ });
100
+ }
101
+ /**
102
+ * Removes an event listener for a specific type of message.
103
+ *
104
+ * @param messageType - The message type.
105
+ * @param callback - The callback function to remove.
106
+ */
107
+ removeEventListener(messageType, callback) {
108
+ this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
109
+ }
110
+ /**
111
+ * Initializes the communicator when the embedded content is ready.
112
+ * @internal
113
+ * @remarks
114
+ * This function sets up a listener that waits for the 'appLoaded' action and then initializes the Communicator.
115
+ */
116
+ static createInstance() {
117
+ window.env3DSource = window.env3DSource || {};
118
+ const startHandler = (event) => {
119
+ var _a, _b;
120
+ 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' &&
121
+ 'apiReady' in window.env3DSource) {
122
+ window.env3DSource.api = new Communicator();
123
+ window.env3DSource.apiReady(window.env3DSource.api);
124
+ window.removeEventListener('message', startHandler);
125
+ }
126
+ };
127
+ window.addEventListener('message', startHandler);
128
+ }
129
+ }
130
+ /**
131
+ * The singleton instance of the communicator.
132
+ */
133
+ Communicator.instance = null;