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

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.
@@ -1,134 +0,0 @@
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
- * @hidden
15
- */
16
- export class Communicator {
17
- /**
18
- * Creates an instance of Communicator.
19
- * @internal
20
- * @remarks
21
- * If an instance already exists, it will be destroyed before creating a new one.
22
- * The instance listens for messages from the iframe.
23
- */
24
- constructor() {
25
- var _a;
26
- /**
27
- * Storage for callback listeners by message type.
28
- */
29
- this.listeners = [];
30
- /**
31
- * Binder function for handling incoming messages.
32
- */
33
- this.binder = this.handleMessageReceived.bind(this, messageType);
34
- (_a = Communicator.instance) === null || _a === void 0 ? void 0 : _a.destroy();
35
- Communicator.instance = this;
36
- window.addEventListener('message', this.binder);
37
- }
38
- /**
39
- * Destroys the communicator instance by clearing listeners and removing event listeners.
40
- */
41
- destroy() {
42
- this.listeners = [];
43
- window.removeEventListener('message', this.binder);
44
- }
45
- /**
46
- * Sends a message to the embedded content (MetaBox).
47
- *
48
- * @param data - The data to send.
49
- */
50
- sendMessageToMetaBox(data) {
51
- var _a;
52
- const iframe = document.getElementById('embeddedContent');
53
- const dataToSend = {
54
- host: 'metaBoxHost',
55
- payload: Object.assign({}, data),
56
- };
57
- // In production, replace "*" with the actual origin for added security.
58
- (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage(dataToSend, '*');
59
- }
60
- /**
61
- * Adds an event listener for receiving specific types of messages.
62
- *
63
- * @param messageType - The message type to listen for.
64
- * @param callback - The callback function to execute when a message is received.
65
- */
66
- addEventListener(messageType, callback) {
67
- this.listeners.push({ messageType, callback });
68
- }
69
- /**
70
- * Dispatches an event to all listeners of a specific message type.
71
- *
72
- * @param messageType - The message type.
73
- * @param data - The data associated with the event.
74
- */
75
- dispatchEvent(messageType, data) {
76
- this.listeners
77
- .filter((listener) => listener.messageType === messageType)
78
- .forEach((listener) => listener.callback(data));
79
- }
80
- /**
81
- * Handles incoming messages by invoking the appropriate callbacks.
82
- *
83
- * @param messageType - The message type to filter for.
84
- * @param event - The incoming message event.
85
- *
86
- * @remarks
87
- * Optionally, you can perform an origin check for added security.
88
- */
89
- handleMessageReceived(messageType, event) {
90
- var _a;
91
- // Optional: Perform origin check here for added security
92
- // if (event.origin !== "EXPECTED_ORIGIN") return;
93
- if (((_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.host) !== 'metabox') {
94
- return;
95
- }
96
- this.listeners
97
- .filter((listener) => listener.messageType === messageType)
98
- .forEach((listener) => {
99
- listener.callback(event.data.payload);
100
- });
101
- }
102
- /**
103
- * Removes an event listener for a specific type of message.
104
- *
105
- * @param messageType - The message type.
106
- * @param callback - The callback function to remove.
107
- */
108
- removeEventListener(messageType, callback) {
109
- this.listeners = this.listeners.filter((listener) => !(listener.messageType === messageType && listener.callback === callback));
110
- }
111
- /**
112
- * Initializes the communicator when the embedded content is ready.
113
- * @internal
114
- * @remarks
115
- * This function sets up a listener that waits for the 'appLoaded' action and then initializes the Communicator.
116
- */
117
- static createInstance() {
118
- window.env3DSource = window.env3DSource || {};
119
- const startHandler = (event) => {
120
- var _a, _b;
121
- 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' &&
122
- 'apiReady' in window.env3DSource) {
123
- window.env3DSource.api = new Communicator();
124
- window.env3DSource.apiReady(window.env3DSource.api);
125
- window.removeEventListener('message', startHandler);
126
- }
127
- };
128
- window.addEventListener('message', startHandler);
129
- }
130
- }
131
- /**
132
- * The singleton instance of the communicator.
133
- */
134
- Communicator.instance = null;
@@ -1,61 +0,0 @@
1
- /*
2
- * Copyright (c) 2025.
3
- * 3DSource.com, Sergii Karanda steve@3dsource.com
4
- * All Rights Reserved
5
- */
6
- import { Communicator } from './initializeCommunicator';
7
- /**
8
- * Integrates the MetaBox into the provided layout.
9
- *
10
- * @remarks
11
- * This function injects an iframe into a specified container element in the DOM and initializes
12
- * the communicator for interacting with the MetaBox embedded content.
13
- *
14
- * @param iframeSrc - The source URL of the MetaBox iframe.
15
- * Additional url parameters:
16
- * ```javascript
17
- * sidebar=false - disables the internal MetaBox menu
18
- * introImage=https://example.com/image.png - sets the intro image
19
- * ```
20
- * 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
21
- *
22
- * @param containerId - The ID of the layout element. Defaults to 'embed3DSource'.
23
- *
24
- * @throws Error if the container element with the specified ID is not found.
25
- *
26
- * @example
27
- * import {
28
- * integrateMetaBox,
29
- * } from 'https://unpkg.com/@3dsource/metabox-front-api@latest/dist/index.js';
30
- *
31
- * integrateMetaBox(
32
- * 'https://metabox.3dsource.com/metabox-configurator/basic/12345678-1234-1234-1234-01234567890?introImage=https://example.com/intro.webp&sidebar=false',
33
- * 'embed3DSource'
34
- * );
35
- */
36
- export function integrateMetaBox(iframeSrc, containerId = 'embed3DSource') {
37
- const container = document.getElementById(containerId);
38
- if (!container) {
39
- throw new Error(`Container element with id ${containerId} not found`);
40
- }
41
- Communicator.createInstance();
42
- const style = document.createElement('style');
43
- style.innerHTML = `
44
- #${containerId} {
45
- width: 100%;
46
- height: 100%;
47
- overflow: hidden;
48
- position: relative;
49
- }
50
- `;
51
- document.head.appendChild(style);
52
- const iframe = document.createElement('iframe');
53
- iframe.setAttribute('referrerPolicy', 'no-referrer-when-downgrade');
54
- iframe.setAttribute('id', 'embeddedContent');
55
- iframe.style.border = '0';
56
- iframe.style.width = '100%';
57
- iframe.style.height = '100%';
58
- iframe.style.overflow = 'hidden';
59
- iframe.src = iframeSrc;
60
- container.appendChild(iframe);
61
- }
@@ -1,23 +0,0 @@
1
- /*
2
- * Copyright (c) 2025.
3
- * 3DSource.com, Sergii Karanda steve@3dsource.com
4
- * All Rights Reserved
5
- */
6
- /**
7
- * Saves an image by triggering a download.
8
- *
9
- * @remarks
10
- * This function creates an anchor element, sets its `href` attribute to the provided image URL,
11
- * and triggers a click event to initiate a download with the specified filename.
12
- *
13
- * @param image - The URL of the image to save.
14
- * @param filename - The name of the file to save.
15
- */
16
- export function saveImage(image, filename) {
17
- const a = document.createElement('a');
18
- a.href = image;
19
- a.download = filename;
20
- document.body.appendChild(a);
21
- a.click();
22
- document.body.removeChild(a);
23
- }
package/dist/lib/sleep.js DELETED
@@ -1,17 +0,0 @@
1
- /*
2
- * Copyright (c) 2025.
3
- * 3DSource.com, Sergii Karanda steve@3dsource.com
4
- * All Rights Reserved
5
- */
6
- /**
7
- * Pauses execution for a specified duration.
8
- *
9
- * @remarks
10
- * This function returns a promise that resolves after the given number of milliseconds.
11
- *
12
- * @param time - The time to sleep in milliseconds.
13
- * @returns A promise that resolves after the specified duration.
14
- */
15
- export function sleep(time) {
16
- return new Promise((resolve) => setTimeout(resolve, time));
17
- }