@3dsource/metabox-modular-configurator-api 0.0.1 → 0.0.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.
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Metabox Modular Configurator API
|
|
2
2
|
|
|
3
|
-
A powerful
|
|
3
|
+
A powerful TypeScript API for integrating and controlling the Metabox Modular Configurator in web
|
|
4
4
|
applications. This package provides seamless communication with 3D configurators, allowing you to control environments,
|
|
5
|
-
components, materials, and capture screenshots or PDFs.
|
|
5
|
+
components, component types, materials, and capture screenshots or PDFs.
|
|
6
6
|
|
|
7
7
|
## Table of Contents
|
|
8
8
|
|
|
@@ -11,8 +11,12 @@ components, materials, and capture screenshots or PDFs.
|
|
|
11
11
|
- [Installation](#installation)
|
|
12
12
|
- [Quick Start](#quick-start)
|
|
13
13
|
- [API Reference](#api-reference)
|
|
14
|
+
- [Core Integration](#core-integration)
|
|
15
|
+
- [Type Definitions](#type-definitions)
|
|
16
|
+
- [Command Classes](#command-classes)
|
|
14
17
|
- [Examples](#examples)
|
|
15
18
|
- [Event Handling](#event-handling)
|
|
19
|
+
- [Troubleshooting](#troubleshooting)
|
|
16
20
|
- [Contributing](#contributing)
|
|
17
21
|
|
|
18
22
|
## Features
|
|
@@ -51,7 +55,7 @@ import { integrateMetabox, Communicator, SetEnvironment, SetComponent } from 'ht
|
|
|
51
55
|
|
|
52
56
|
Alternative CDN options:
|
|
53
57
|
|
|
54
|
-
- **jsDelivr**: `https://cdn.jsdelivr.net/npm/@3dsource/metabox-modular-configurator-api@0.0.
|
|
58
|
+
- **jsDelivr**: `https://cdn.jsdelivr.net/npm/@3dsource/metabox-modular-configurator-api@0.0.3/+esm`
|
|
55
59
|
|
|
56
60
|
## Quick Start
|
|
57
61
|
|
|
@@ -92,7 +96,7 @@ Create a container element where the configurator will be embedded:
|
|
|
92
96
|
|
|
93
97
|
```html
|
|
94
98
|
<script type="module">
|
|
95
|
-
import {
|
|
99
|
+
import { GetPdf, GetScreenshot, integrateMetabox, saveImage, SetComponent, SetComponentMaterial, SetEnvironment, SetEnvironmentMaterial, ShowEmbeddedMenu, ShowOverlayInterface } from 'https://cdn.jsdelivr.net/npm/@3dsource/metabox-modular-configurator-api';
|
|
96
100
|
|
|
97
101
|
// Replace it with your actual configurator URL
|
|
98
102
|
const configuratorUrl = 'https://metabox.3dsource.com/metabox-configurator/modular/your-configurator-id';
|
|
@@ -101,12 +105,12 @@ Create a container element where the configurator will be embedded:
|
|
|
101
105
|
console.log('Configurator API ready!');
|
|
102
106
|
|
|
103
107
|
// Listen for configurator data updates
|
|
104
|
-
api.addEventListener('
|
|
108
|
+
api.addEventListener('configuratorDataUpdated', (data) => {
|
|
105
109
|
console.log('Configurator data:', data);
|
|
106
110
|
});
|
|
107
111
|
|
|
108
112
|
// Listen for screenshot events
|
|
109
|
-
api.addEventListener('
|
|
113
|
+
api.addEventListener('screenshotReady', (data) => {
|
|
110
114
|
saveImage(data, 'configurator-screenshot.png');
|
|
111
115
|
});
|
|
112
116
|
|
|
@@ -126,19 +130,19 @@ Create a container element where the configurator will be embedded:
|
|
|
126
130
|
#### TypeScript/Angular/React
|
|
127
131
|
|
|
128
132
|
```typescript
|
|
129
|
-
import { integrateMetabox, Communicator, ModularConfiguratorEnvelope, SetEnvironment, ShowOverlayInterface, GetScreenshot, saveImage } from '@3dsource/metabox-modular-configurator-api';
|
|
133
|
+
import { integrateMetabox, Communicator, ModularConfiguratorEnvelope, SetEnvironment, ShowOverlayInterface, GetScreenshot, saveImage, SetComponent, SetComponentMaterial, type MimeType, ShowEmbeddedMenu } from '@3dsource/metabox-modular-configurator-api';
|
|
130
134
|
|
|
131
135
|
class ConfiguratorIntegration {
|
|
132
136
|
private api: Communicator | null = null;
|
|
137
|
+
private readonly configuratorUrl: string;
|
|
133
138
|
|
|
134
|
-
constructor() {
|
|
139
|
+
constructor(configuratorUrl: string) {
|
|
140
|
+
this.configuratorUrl = configuratorUrl;
|
|
135
141
|
this.initialize();
|
|
136
142
|
}
|
|
137
143
|
|
|
138
144
|
private initialize(): void {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
integrateMetabox(configuratorUrl, 'embed3DSource', (api) => {
|
|
145
|
+
integrateMetabox(this.configuratorUrl, 'embed3DSource', (api: Communicator) => {
|
|
142
146
|
this.api = api;
|
|
143
147
|
this.setupEventListeners();
|
|
144
148
|
this.setupInitialState();
|
|
@@ -148,37 +152,89 @@ class ConfiguratorIntegration {
|
|
|
148
152
|
private setupEventListeners(): void {
|
|
149
153
|
if (!this.api) return;
|
|
150
154
|
|
|
151
|
-
|
|
155
|
+
// Listen for configuration data changes
|
|
156
|
+
this.api.addEventListener('configuratorDataUpdated', (data: ModularConfiguratorEnvelope) => {
|
|
152
157
|
console.log('Configurator data updated:', data);
|
|
153
158
|
this.handleConfiguratorData(data);
|
|
154
159
|
});
|
|
155
160
|
|
|
156
|
-
|
|
157
|
-
|
|
161
|
+
// Handle screenshot events
|
|
162
|
+
this.api.addEventListener('screenshotReady', (imageData: string | null) => {
|
|
163
|
+
saveImage(imageData ?? '', 'configurator-render.png');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Handle viewport ready events
|
|
167
|
+
this.api.addEventListener('viewportReady', (isReady: boolean) => {
|
|
168
|
+
console.log('Viewport ready:', isReady);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Handle status messages
|
|
172
|
+
this.api.addEventListener('statusMessageChanged', (message: string | null) => {
|
|
173
|
+
console.log('Status message:', message);
|
|
158
174
|
});
|
|
159
175
|
}
|
|
160
176
|
|
|
161
177
|
private setupInitialState(): void {
|
|
162
178
|
if (!this.api) return;
|
|
163
179
|
|
|
180
|
+
// Configure initial environment and components
|
|
181
|
+
this.api.sendCommandToMetaBox(new SetEnvironment('default-environment'));
|
|
182
|
+
this.api.sendCommandToMetaBox(new SetComponent('main-product', 'chair', true));
|
|
164
183
|
this.api.sendCommandToMetaBox(new ShowOverlayInterface(true));
|
|
165
|
-
this.api.sendCommandToMetaBox(new
|
|
184
|
+
this.api.sendCommandToMetaBox(new ShowEmbeddedMenu(true));
|
|
166
185
|
}
|
|
167
186
|
|
|
168
187
|
private handleConfiguratorData(data: ModularConfiguratorEnvelope): void {
|
|
188
|
+
// Access typed data properties according to actual interface
|
|
189
|
+
console.log('Configurator:', data.configurator);
|
|
190
|
+
console.log('Configuration tree:', data.configurationTree);
|
|
191
|
+
console.log('Components by type:', data.componentsByComponentType);
|
|
192
|
+
console.log('Component materials:', data.componentMaterialsIds);
|
|
193
|
+
console.log('Environment ID:', data.environmentId);
|
|
194
|
+
console.log('Environment materials:', data.environmentMaterialsIds);
|
|
195
|
+
|
|
169
196
|
// Process configurator data according to your needs
|
|
170
|
-
|
|
197
|
+
this.updateUI(data);
|
|
171
198
|
}
|
|
172
199
|
|
|
173
|
-
|
|
200
|
+
private updateUI(data: ModularConfiguratorEnvelope): void {
|
|
201
|
+
// Update your application UI based on configurator state
|
|
202
|
+
// This method would contain your specific UI update logic
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Public API methods
|
|
206
|
+
public takeScreenshot(format: MimeType = 'image/png', size?: { x: number; y: number }): void {
|
|
174
207
|
if (this.api) {
|
|
175
|
-
this.api.sendCommandToMetaBox(new GetScreenshot(
|
|
208
|
+
this.api.sendCommandToMetaBox(new GetScreenshot(format, size));
|
|
176
209
|
}
|
|
177
210
|
}
|
|
211
|
+
|
|
212
|
+
public changeMaterial(slotId: string, materialId: string): void {
|
|
213
|
+
if (this.api) {
|
|
214
|
+
this.api.sendCommandToMetaBox(new SetComponentMaterialById(slotId, materialId));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public changeEnvironment(environmentId: string): void {
|
|
219
|
+
if (this.api) {
|
|
220
|
+
this.api.sendCommandToMetaBox(new SetEnvironment(environmentId));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
public destroy(): void {
|
|
225
|
+
// Clean up resources when component is destroyed
|
|
226
|
+
this.api = null;
|
|
227
|
+
}
|
|
178
228
|
}
|
|
179
229
|
|
|
180
|
-
//
|
|
181
|
-
new ConfiguratorIntegration();
|
|
230
|
+
// Usage example
|
|
231
|
+
const configurator = new ConfiguratorIntegration('https://metabox.3dsource.com/metabox-configurator/modular/your-configurator-id');
|
|
232
|
+
|
|
233
|
+
// Take a high-resolution screenshot
|
|
234
|
+
configurator.takeScreenshot('image/png', { x: 1920, y: 1080 });
|
|
235
|
+
|
|
236
|
+
// Change material
|
|
237
|
+
configurator.changeMaterial('seat-fabric', 'leather-brown');
|
|
182
238
|
```
|
|
183
239
|
|
|
184
240
|
## API Reference
|
|
@@ -191,9 +247,15 @@ Integrates the Metabox modular configurator into the specified container.
|
|
|
191
247
|
|
|
192
248
|
**Parameters:**
|
|
193
249
|
|
|
194
|
-
- `configuratorUrl` (string): The URL of your Metabox configurator
|
|
195
|
-
- `containerId` (string): The ID of the HTML element where the configurator will be embedded
|
|
196
|
-
- `apiReadyCallback` (
|
|
250
|
+
- `configuratorUrl` (`string`): The URL of your Metabox configurator
|
|
251
|
+
- `containerId` (`string`): The ID of the HTML element where the configurator will be embedded
|
|
252
|
+
- `apiReadyCallback` (`(api: Communicator) => void`): Callback function that receives the API communicator instance
|
|
253
|
+
|
|
254
|
+
**TypeScript Signature:**
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
declare function integrateMetabox(configuratorUrl: string, containerId: string, apiReadyCallback: (api: Communicator) => void): void;
|
|
258
|
+
```
|
|
197
259
|
|
|
198
260
|
**Example:**
|
|
199
261
|
|
|
@@ -203,6 +265,178 @@ integrateMetabox('https://metabox.3dsource.com/metabox-configurator/modular/your
|
|
|
203
265
|
});
|
|
204
266
|
```
|
|
205
267
|
|
|
268
|
+
### Type Definitions
|
|
269
|
+
|
|
270
|
+
The API provides comprehensive TypeScript support with the following key interfaces and types:
|
|
271
|
+
|
|
272
|
+
#### Core Types
|
|
273
|
+
|
|
274
|
+
##### `Communicator`
|
|
275
|
+
|
|
276
|
+
The main API interface for sending commands and listening to events.
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
interface Communicator {
|
|
280
|
+
sendCommandToMetaBox<T extends keyof ToMetaboxMessagePayloads>(command: CommandBase<T>): void;
|
|
281
|
+
|
|
282
|
+
addEventListener<K extends keyof FromMetaboxMessagePayloads>(eventType: K, listener: (data: FromMetaboxMessagePayloads[K]) => void): void;
|
|
283
|
+
|
|
284
|
+
removeEventListener<K extends keyof FromMetaboxMessagePayloads>(eventType: K, listener: (data: FromMetaboxMessagePayloads[K]) => void): void;
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
##### `ModularConfiguratorEnvelope`
|
|
289
|
+
|
|
290
|
+
The main data structure contains the complete configurator state.
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
interface ModularConfiguratorEnvelope {
|
|
294
|
+
/** Represents the modular configurator api with its components, component types, and environments */
|
|
295
|
+
configurator: InitialModularConfigurator;
|
|
296
|
+
/** The unique tree of components and parent and component type id */
|
|
297
|
+
configurationTree: ComponentWithParent[];
|
|
298
|
+
/** A record mapping all components by current component type */
|
|
299
|
+
componentsByComponentType: Record<string, string>;
|
|
300
|
+
/** A record mapping component material selection */
|
|
301
|
+
componentMaterialsIds: SelectedIds;
|
|
302
|
+
/** The unique environment identifier */
|
|
303
|
+
environmentId: string;
|
|
304
|
+
/** A record mapping environment material selections */
|
|
305
|
+
environmentMaterialsIds: SelectedIds;
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
##### `ModularConfiguratorComponent`
|
|
310
|
+
|
|
311
|
+
Represents a component in the configurator.
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
interface ModularConfiguratorComponent {
|
|
315
|
+
/** The id unique identifier. */
|
|
316
|
+
id: string;
|
|
317
|
+
/** The modular configurator component type identifier. */
|
|
318
|
+
componentType: ModularConfiguratorComponentType;
|
|
319
|
+
/** Display the name for the product. */
|
|
320
|
+
name: string;
|
|
321
|
+
/** Position index of the product. */
|
|
322
|
+
position: number;
|
|
323
|
+
/** The detailed product information. */
|
|
324
|
+
product: Product;
|
|
325
|
+
/** Connectors between connectors and virtual sockets. */
|
|
326
|
+
virtualSocketToConnectorsMap?: ModularConfiguratorVirtualSocketToConnector[];
|
|
327
|
+
/** GraphQL typename for the component. */
|
|
328
|
+
__typename: 'ModularConfiguratorComponent';
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
##### `Material`
|
|
333
|
+
|
|
334
|
+
Represents a material that can be applied to components or environments.
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
interface Material {
|
|
338
|
+
/** Unique identifier for the material. */
|
|
339
|
+
id: string;
|
|
340
|
+
/** Title or name of the material. */
|
|
341
|
+
title: string;
|
|
342
|
+
/** Unique external id for the material. */
|
|
343
|
+
externalId: string;
|
|
344
|
+
/** List of thumbnails for the material. */
|
|
345
|
+
thumbnailList: Thumbnail[];
|
|
346
|
+
/** GraphQL typename for the material. */
|
|
347
|
+
__typename: 'Material';
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
##### `Environment`
|
|
352
|
+
|
|
353
|
+
Represents an environment/scene configuration.
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
interface Environment {
|
|
357
|
+
/** Unique identifier for the environment. */
|
|
358
|
+
id: string;
|
|
359
|
+
/** Title or name of the environment. */
|
|
360
|
+
title: string;
|
|
361
|
+
/** Flag indicating if UDS is enabled. */
|
|
362
|
+
udsEnabled: boolean;
|
|
363
|
+
/** The north yaw of the sun in the environment. */
|
|
364
|
+
sunNorthYaw: number;
|
|
365
|
+
/** The UDS hour setting for the environment. */
|
|
366
|
+
udsHour: number;
|
|
367
|
+
/** List of thumbnails for the environment (optional). */
|
|
368
|
+
thumbnailList: Thumbnail[];
|
|
369
|
+
/** List of slots for the environment (optional). */
|
|
370
|
+
slots: Slot[];
|
|
371
|
+
/** GraphQL typename for the environment. */
|
|
372
|
+
__typename: 'Environment';
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
#### Command Payload Types
|
|
377
|
+
|
|
378
|
+
All commands use strongly typed payloads defined in `ToMetaboxMessagePayloads`:
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
interface ToMetaboxMessagePayloads {
|
|
382
|
+
setEnvironment: { id: string };
|
|
383
|
+
setEnvironmentMaterialById: { slotId: string; materialId: string };
|
|
384
|
+
setComponent: { id: string; typeId: string; isRoot: boolean };
|
|
385
|
+
setComponentType: { typeId: string };
|
|
386
|
+
setComponentMaterialById: { slotId: string; materialId: string };
|
|
387
|
+
getScreenshot: { format: MimeType; size?: { x: number; y: number } };
|
|
388
|
+
getPdf: void;
|
|
389
|
+
showEmbeddedMenu: { visible: boolean };
|
|
390
|
+
showOverlayInterface: { visible: boolean };
|
|
391
|
+
forceSetDeviceType: { deviceType: ApplicationDeviceTypes };
|
|
392
|
+
resetCamera: void;
|
|
393
|
+
applyZoom: { zoom: number };
|
|
394
|
+
initShowcase: { productId: string };
|
|
395
|
+
playShowcase: void;
|
|
396
|
+
pauseShowcase: void;
|
|
397
|
+
stopShowcase: void;
|
|
398
|
+
sendCommandToUnreal: object;
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
#### Utility Types
|
|
403
|
+
|
|
404
|
+
##### `MimeType`
|
|
405
|
+
|
|
406
|
+
Supported image formats for screenshots.
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
type MimeType = 'image/png' | 'image/jpeg' | 'image/webp';
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
##### `ApplicationDeviceTypes`
|
|
413
|
+
|
|
414
|
+
Device type options for responsive behavior.
|
|
415
|
+
|
|
416
|
+
```typescript
|
|
417
|
+
type ApplicationDeviceTypes = 'desktop' | 'tablet' | 'mobile';
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
##### `Position` and `Rotation`
|
|
421
|
+
|
|
422
|
+
3D coordinate and rotation structures.
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
interface Position {
|
|
426
|
+
x: number;
|
|
427
|
+
y: number;
|
|
428
|
+
z: number;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
interface Rotation {
|
|
432
|
+
x: number;
|
|
433
|
+
y: number;
|
|
434
|
+
z: number;
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### Command Classes
|
|
439
|
+
|
|
206
440
|
### Environment & Scene Control
|
|
207
441
|
|
|
208
442
|
#### `SetEnvironment(environmentId)`
|
|
@@ -346,8 +580,17 @@ Captures a screenshot of the current configurator view.
|
|
|
346
580
|
|
|
347
581
|
**Parameters:**
|
|
348
582
|
|
|
349
|
-
- `mimeType` (string): Image format ('image/png', 'image/jpeg')
|
|
350
|
-
- `size` (
|
|
583
|
+
- `mimeType` (`string`): Image format ('image/png', 'image/jpeg')
|
|
584
|
+
- `size` (`{ x: number; y: number }`, optional): Screenshot dimensions in pixels
|
|
585
|
+
|
|
586
|
+
**TypeScript Interface:**
|
|
587
|
+
|
|
588
|
+
```typescript
|
|
589
|
+
interface ScreenshotSize {
|
|
590
|
+
x: number; // Width in pixels
|
|
591
|
+
y: number; // Height in pixels
|
|
592
|
+
}
|
|
593
|
+
```
|
|
351
594
|
|
|
352
595
|
**Example:**
|
|
353
596
|
|
|
@@ -377,9 +620,7 @@ Utility function to save image data to a file.
|
|
|
377
620
|
**Example:**
|
|
378
621
|
|
|
379
622
|
```typescript
|
|
380
|
-
|
|
381
|
-
saveImage(data, 'my-configuration.png');
|
|
382
|
-
});
|
|
623
|
+
saveImage(data, 'my-configuration.png');
|
|
383
624
|
```
|
|
384
625
|
|
|
385
626
|
### Showcase Control
|
|
@@ -426,16 +667,6 @@ Sends custom commands directly to the Unreal Engine.
|
|
|
426
667
|
api.sendCommandToMetaBox(new UnrealCommand('SetLightIntensity 2.5'));
|
|
427
668
|
```
|
|
428
669
|
|
|
429
|
-
#### `WatchCallbacks()`
|
|
430
|
-
|
|
431
|
-
Enables watching for callbacks from the Unreal Engine.
|
|
432
|
-
|
|
433
|
-
**Example:**
|
|
434
|
-
|
|
435
|
-
```typescript
|
|
436
|
-
api.sendCommandToMetaBox(new WatchCallbacks());
|
|
437
|
-
```
|
|
438
|
-
|
|
439
670
|
## Examples
|
|
440
671
|
|
|
441
672
|
### Complete Product Configurator
|
|
@@ -445,7 +676,7 @@ import { integrateMetabox, Communicator, ModularConfiguratorEnvelope, SetEnviron
|
|
|
445
676
|
|
|
446
677
|
class ProductConfigurator {
|
|
447
678
|
private api: Communicator | null = null;
|
|
448
|
-
private currentConfig:
|
|
679
|
+
private currentConfig: ModularConfiguratorEnvelope | null = null;
|
|
449
680
|
|
|
450
681
|
constructor(configuratorUrl: string) {
|
|
451
682
|
integrateMetabox(configuratorUrl, 'embed3DSource', (api) => {
|
|
@@ -509,66 +740,206 @@ class ProductConfigurator {
|
|
|
509
740
|
link.click();
|
|
510
741
|
}
|
|
511
742
|
}
|
|
512
|
-
|
|
513
|
-
// Usage
|
|
514
|
-
const configurator = new ProductConfigurator('https://metabox.3dsource.com/metabox-configurator/modular/your-product-id');
|
|
515
743
|
```
|
|
516
744
|
|
|
517
745
|
### React Hook Example
|
|
518
746
|
|
|
519
747
|
```typescript
|
|
520
|
-
import { useEffect, useRef, useState } from 'react';
|
|
521
|
-
import {
|
|
748
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
749
|
+
import {
|
|
750
|
+
Communicator,
|
|
751
|
+
GetScreenshot,
|
|
752
|
+
integrateMetabox,
|
|
753
|
+
ModularConfiguratorEnvelope,
|
|
754
|
+
SetComponent,
|
|
755
|
+
SetComponentMaterial,
|
|
756
|
+
SetEnvironment,
|
|
757
|
+
} from '@3dsource/metabox-modular-configurator-api';
|
|
758
|
+
|
|
759
|
+
interface UseMetaboxConfiguratorOptions {
|
|
760
|
+
configuratorUrl: string;
|
|
761
|
+
autoInitialize?: boolean;
|
|
762
|
+
}
|
|
522
763
|
|
|
523
|
-
|
|
764
|
+
interface UseMetaboxConfiguratorReturn {
|
|
765
|
+
containerRef: React.RefObject<HTMLDivElement>;
|
|
766
|
+
api: Communicator | null;
|
|
767
|
+
isReady: boolean;
|
|
768
|
+
configData: ModularConfiguratorEnvelope | null;
|
|
769
|
+
isLoading: boolean;
|
|
770
|
+
error: string | null;
|
|
771
|
+
// Actions
|
|
772
|
+
changeEnvironment: (environmentId: string) => void;
|
|
773
|
+
takeScreenshot: (format?: MimeType, size?: { x: number; y: number }) => void;
|
|
774
|
+
changeMaterial: (slotId: string, materialId: string) => void;
|
|
775
|
+
addComponent: (id: string, typeId: string, isRoot?: boolean) => void;
|
|
776
|
+
initialize: () => void;
|
|
777
|
+
destroy: () => void;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
export const useMetaboxConfigurator = ({
|
|
781
|
+
configuratorUrl,
|
|
782
|
+
autoInitialize = true,
|
|
783
|
+
}: UseMetaboxConfiguratorOptions): UseMetaboxConfiguratorReturn => {
|
|
524
784
|
const [api, setApi] = useState<Communicator | null>(null);
|
|
525
|
-
const [isReady, setIsReady] = useState(false);
|
|
526
|
-
const [
|
|
785
|
+
const [isReady, setIsReady] = useState<boolean>(false);
|
|
786
|
+
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
787
|
+
const [error, setError] = useState<string | null>(null);
|
|
788
|
+
const [configData, setConfigData] = useState<ModularConfiguratorEnvelope | null>(null);
|
|
527
789
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
528
790
|
|
|
529
|
-
|
|
530
|
-
if (!containerRef.current || !configuratorUrl) return;
|
|
791
|
+
const initialize = useCallback(() => {
|
|
792
|
+
if (!containerRef.current || !configuratorUrl || isLoading) return;
|
|
793
|
+
|
|
794
|
+
setIsLoading(true);
|
|
795
|
+
setError(null);
|
|
796
|
+
|
|
797
|
+
try {
|
|
798
|
+
const containerId = 'metabox-container-' + Date.now();
|
|
799
|
+
containerRef.current.id = containerId;
|
|
531
800
|
|
|
532
|
-
|
|
533
|
-
|
|
801
|
+
integrateMetabox(configuratorUrl, containerId, (apiInstance: Communicator) => {
|
|
802
|
+
setApi(apiInstance);
|
|
803
|
+
setIsReady(true);
|
|
804
|
+
setIsLoading(false);
|
|
534
805
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
806
|
+
// Set up event listeners
|
|
807
|
+
apiInstance.addEventListener('configuratorData', (data: ModularConfiguratorEnvelope) => {
|
|
808
|
+
setConfigData(data);
|
|
809
|
+
});
|
|
538
810
|
|
|
539
|
-
|
|
540
|
-
|
|
811
|
+
apiInstance.addEventListener('screenshot', (imageData: string) => {
|
|
812
|
+
// Handle screenshot data - could emit custom event or callback
|
|
813
|
+
console.log('Screenshot ready:', imageData);
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
apiInstance.addEventListener('pdf', (pdfData: string) => {
|
|
817
|
+
// Handle PDF data
|
|
818
|
+
console.log('PDF ready:', pdfData);
|
|
819
|
+
});
|
|
541
820
|
});
|
|
542
|
-
})
|
|
821
|
+
} catch (err) {
|
|
822
|
+
setError(err instanceof Error ? err.message : 'Failed to initialize configurator');
|
|
823
|
+
setIsLoading(false);
|
|
824
|
+
}
|
|
825
|
+
}, [configuratorUrl, isLoading]);
|
|
826
|
+
|
|
827
|
+
const destroy = useCallback(() => {
|
|
828
|
+
setApi(null);
|
|
829
|
+
setIsReady(false);
|
|
830
|
+
setIsLoading(false);
|
|
831
|
+
setConfigData(null);
|
|
832
|
+
setError(null);
|
|
833
|
+
}, []);
|
|
543
834
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
835
|
+
useEffect(() => {
|
|
836
|
+
if (autoInitialize) {
|
|
837
|
+
initialize();
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
return destroy;
|
|
841
|
+
}, [initialize, destroy, autoInitialize]);
|
|
550
842
|
|
|
551
|
-
|
|
552
|
-
|
|
843
|
+
// Action methods
|
|
844
|
+
const changeEnvironment = useCallback((environmentId: string) => {
|
|
845
|
+
if (api && isReady) {
|
|
553
846
|
api.sendCommandToMetaBox(new SetEnvironment(environmentId));
|
|
554
847
|
}
|
|
555
|
-
};
|
|
848
|
+
}, [api, isReady]);
|
|
849
|
+
|
|
850
|
+
const takeScreenshot = useCallback((
|
|
851
|
+
format: MimeType = 'image/png',
|
|
852
|
+
size?: { x: number; y: number },
|
|
853
|
+
) => {
|
|
854
|
+
if (api && isReady) {
|
|
855
|
+
api.sendCommandToMetaBox(new GetScreenshot(format, size));
|
|
856
|
+
}
|
|
857
|
+
}, [api, isReady]);
|
|
556
858
|
|
|
557
|
-
const
|
|
558
|
-
if (api) {
|
|
559
|
-
api.sendCommandToMetaBox(new
|
|
859
|
+
const changeMaterial = useCallback((slotId: string, materialId: string) => {
|
|
860
|
+
if (api && isReady) {
|
|
861
|
+
api.sendCommandToMetaBox(new SetComponentMaterial(slotId, materialId));
|
|
560
862
|
}
|
|
561
|
-
};
|
|
863
|
+
}, [api, isReady]);
|
|
864
|
+
|
|
865
|
+
const addComponent = useCallback((id: string, typeId: string, isRoot: boolean = false) => {
|
|
866
|
+
if (api && isReady) {
|
|
867
|
+
api.sendCommandToMetaBox(new SetComponent(id, typeId, isRoot));
|
|
868
|
+
}
|
|
869
|
+
}, [api, isReady]);
|
|
562
870
|
|
|
563
871
|
return {
|
|
564
872
|
containerRef,
|
|
565
873
|
api,
|
|
566
874
|
isReady,
|
|
567
875
|
configData,
|
|
876
|
+
isLoading,
|
|
877
|
+
error,
|
|
568
878
|
changeEnvironment,
|
|
569
879
|
takeScreenshot,
|
|
880
|
+
changeMaterial,
|
|
881
|
+
addComponent,
|
|
882
|
+
initialize,
|
|
883
|
+
destroy,
|
|
570
884
|
};
|
|
571
885
|
};
|
|
886
|
+
|
|
887
|
+
// Usage example in a React component
|
|
888
|
+
const ConfiguratorComponent: React.FC = () => {
|
|
889
|
+
const {
|
|
890
|
+
containerRef,
|
|
891
|
+
isReady,
|
|
892
|
+
isLoading,
|
|
893
|
+
error,
|
|
894
|
+
configData,
|
|
895
|
+
changeEnvironment,
|
|
896
|
+
takeScreenshot,
|
|
897
|
+
changeMaterial,
|
|
898
|
+
} = useMetaboxConfigurator({
|
|
899
|
+
configuratorUrl: 'https://metabox.3dsource.com/metabox-configurator/modular/your-id',
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
const handleEnvironmentChange = (envId: string) => {
|
|
903
|
+
changeEnvironment(envId);
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
const handleTakeScreenshot = () => {
|
|
907
|
+
takeScreenshot('image/png', { x: 1920, y: 1080 });
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
if (error) {
|
|
911
|
+
return <div className="error">Error loading configurator: {error}</div>;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
return (
|
|
915
|
+
<div className="configurator-wrapper">
|
|
916
|
+
{isLoading && <div className="loading">Loading configurator...</div>}
|
|
917
|
+
|
|
918
|
+
<div ref={containerRef} className="configurator-container" />
|
|
919
|
+
|
|
920
|
+
{isReady && (
|
|
921
|
+
<div className="controls">
|
|
922
|
+
<button onClick={() => handleEnvironmentChange('showroom-1')}>
|
|
923
|
+
Showroom Environment
|
|
924
|
+
</button>
|
|
925
|
+
<button onClick={() => changeMaterial('seat', 'leather-brown')}>
|
|
926
|
+
Brown Leather
|
|
927
|
+
</button>
|
|
928
|
+
<button onClick={handleTakeScreenshot}>
|
|
929
|
+
Take Screenshot
|
|
930
|
+
</button>
|
|
931
|
+
</div>
|
|
932
|
+
)}
|
|
933
|
+
|
|
934
|
+
{configData && (
|
|
935
|
+
<div className="config-info">
|
|
936
|
+
<p>Components: {configData.componentsByComponentType ? Object.keys(configData.componentsByComponentType).length : 0}</p>
|
|
937
|
+
<p>Environment: {configData.environmentId}</p>
|
|
938
|
+
</div>
|
|
939
|
+
)}
|
|
940
|
+
</div>
|
|
941
|
+
);
|
|
942
|
+
};
|
|
572
943
|
```
|
|
573
944
|
|
|
574
945
|
## Event Handling
|
|
@@ -577,28 +948,30 @@ The API provides several events that you can listen to for real-time updates:
|
|
|
577
948
|
|
|
578
949
|
### Available Events
|
|
579
950
|
|
|
580
|
-
| Event Name
|
|
581
|
-
|
|
|
582
|
-
| `
|
|
583
|
-
| `
|
|
584
|
-
| `
|
|
585
|
-
| `
|
|
951
|
+
| Event Name | Description | Data Type |
|
|
952
|
+
| ------------------------- | ------------------------------------- | ----------------------------- |
|
|
953
|
+
| `configuratorDataUpdated` | Fired when configuration data changes | `ModularConfiguratorEnvelope` |
|
|
954
|
+
| `screenshotReady` | Fired when a screenshot is ready | `string` (base64 image data) |
|
|
955
|
+
| `showcaseStatusChanged` | Fired when showcase status changes | `ShowCaseStatus` |
|
|
956
|
+
| `viewportReady` | Fired when viewport is ready | `boolean` |
|
|
957
|
+
| `statusMessageChanged` | Fired when status message changes | `string \| null` |
|
|
586
958
|
|
|
587
959
|
### Event Listener Examples
|
|
588
960
|
|
|
589
961
|
```typescript
|
|
590
962
|
// Listen for configuration changes
|
|
591
|
-
api.addEventListener('
|
|
963
|
+
api.addEventListener('configuratorDataUpdated', (data: ModularConfiguratorEnvelope) => {
|
|
592
964
|
console.log('Configuration updated:', data);
|
|
593
965
|
|
|
594
966
|
// Access specific data
|
|
595
|
-
console.log('
|
|
596
|
-
console.log('
|
|
597
|
-
console.log('
|
|
967
|
+
console.log('Configurator:', data.configurator);
|
|
968
|
+
console.log('Configuration tree:', data.configurationTree);
|
|
969
|
+
console.log('Components by type:', data.componentsByComponentType);
|
|
970
|
+
console.log('Environment ID:', data.environmentId);
|
|
598
971
|
});
|
|
599
972
|
|
|
600
973
|
// Handle screenshot events
|
|
601
|
-
api.addEventListener('
|
|
974
|
+
api.addEventListener('screenshotReady', (imageData: string) => {
|
|
602
975
|
// imageData is a base64 encoded image
|
|
603
976
|
const img = document.createElement('img');
|
|
604
977
|
img.src = imageData;
|
|
@@ -606,18 +979,19 @@ api.addEventListener('screenshot', (imageData: string) => {
|
|
|
606
979
|
});
|
|
607
980
|
|
|
608
981
|
// Monitor showcase playback
|
|
609
|
-
api.addEventListener('
|
|
982
|
+
api.addEventListener('showcaseStatusChanged', (status: ShowCaseStatus) => {
|
|
610
983
|
console.log('Showcase status:', status);
|
|
611
984
|
// Handle play/pause/stop states
|
|
612
985
|
});
|
|
613
986
|
|
|
614
|
-
// Handle
|
|
615
|
-
api.addEventListener('
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
987
|
+
// Handle viewport ready events
|
|
988
|
+
api.addEventListener('viewportReady', (isReady: boolean) => {
|
|
989
|
+
console.log('Viewport ready:', isReady);
|
|
990
|
+
});
|
|
991
|
+
|
|
992
|
+
// Handle status messages
|
|
993
|
+
api.addEventListener('statusMessageChanged', (message: string | null) => {
|
|
994
|
+
console.log('Status message:', message);
|
|
621
995
|
});
|
|
622
996
|
```
|
|
623
997
|
|