@preference-sl/pref-viewer 2.11.0-beta.11 → 2.11.0-beta.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.
package/src/index.js CHANGED
@@ -1,918 +1,23 @@
1
- import { PrefViewer2D } from "./pref-viewer-2d.js";
2
- import { PrefViewer3D } from "./pref-viewer-3d.js";
3
- import { PrefViewerDialog } from "./pref-viewer-dialog.js";
4
- import { PrefViewerTask } from "./pref-viewer-task.js";
5
-
6
- /**
7
- * PrefViewer - Custom Web Component for advanced 2D and 3D product visualization and configuration.
8
- *
9
- * Overview:
10
- * - Encapsulates both 2D (SVG) and 3D (Babylon.js) viewers, supporting glTF/GLB models, environments, and drawings.
11
- * - Loads assets from remote URLs, Base64 data URIs, and IndexedDB sources.
12
- * - Provides a unified API for loading models, scenes, drawings, materials, and configuration via attributes or methods.
13
- * - Manages an internal task queue for sequential processing of viewer operations.
14
- * - Emits custom events for loading, errors, and state changes to facilitate integration.
15
- * - Supports downloading models and scenes in GLB and USDZ formats.
16
- * - Automatically updates the viewer when reactive attributes change.
17
- *
18
- * Usage:
19
- * - Use as a custom HTML element: <pref-viewer ...>
20
- * - Configure via attributes (config, model, scene, materials, drawing, options, mode).
21
- * - Control viewer mode, visibility, and downloads via public methods.
22
- *
23
- * Reactive Attributes:
24
- * - config: URL or Base64 for configuration file.
25
- * - model: URL or Base64 for 3D model (glTF/GLB).
26
- * - scene: URL or Base64 for environment/scene (glTF/GLB).
27
- * - materials: URL or Base64 for materials definition.
28
- * - drawing: URL or Base64 for SVG drawing.
29
- * - options: JSON string for viewer options.
30
- * - mode: Viewer mode ("2d" or "3d").
31
- *
32
- * Public Methods:
33
- * - loadConfig(config): Loads a configuration object or JSON string.
34
- * - loadModel(model): Loads a model object or JSON string.
35
- * - loadScene(scene): Loads a scene/environment object or JSON string.
36
- * - loadMaterials(materials): Loads materials object or JSON string.
37
- * - loadDrawing(drawing): Loads a drawing object or JSON string.
38
- * - setOptions(options): Sets viewer options from an object or JSON string.
39
- * - setMode(mode): Sets the viewer mode to "2d" or "3d" and updates component visibility.
40
- * - showModel(): Shows the 3D model.
41
- * - hideModel(): Hides the 3D model.
42
- * - showScene(): Shows the 3D environment/scene.
43
- * - hideScene(): Hides the 3D environment/scene.
44
- * - zoomCenter(): Centers the 2D drawing view.
45
- * - zoomExtentsAll(): Zooms the 2D drawing to fit all content.
46
- * - zoomIn(): Zooms in on the 2D drawing.
47
- * - zoomOut(): Zooms out of the 2D drawing.
48
- * - downloadModelGLB(): Downloads the current 3D model as a GLB file.
49
- * - downloadModelGLTF(): Downloads the current 3D model as a glTF ZIP file.
50
- * - downloadModelUSDZ(): Downloads the current 3D model as a USDZ file.
51
- * - downloadModelAndSceneGLB(): Downloads both the model and scene as a GLB file.
52
- * - downloadModelAndSceneGLTF(): Downloads both the model and scene as a glTF ZIP file.
53
- * - downloadModelAndSceneUSDZ(): Downloads both the model and scene as a USDZ file.
54
- * - downloadSceneGLB(): Downloads the environment as a GLB file.
55
- * - downloadSceneGLTF(): Downloads the environment as a glTF ZIP file.
56
- * - downloadSceneUSDZ(): Downloads the environment as a USDZ file.
57
- * - openDialog(title, content, footer): Opens a modal dialog with the specified title, content, and footer.
58
- * - closeDialog(): Closes the currently open dialog, if any, and removes it from the DOM.
59
- *
60
- * Public Properties:
61
- * - isInitialized: Indicates if the viewer is initialized.
62
- * - isLoaded: Indicates if the viewer has finished loading.
63
- * - isLoading: Indicates if the viewer is currently loading.
64
- * - isMode2D: Indicates if the viewer is in 2D mode.
65
- * - isMode3D: Indicates if the viewer is in 3D mode.
66
- *
67
- * Events:
68
- * - "scene-loading": Dispatched when a 3D loading operation starts.
69
- * - "scene-loaded": Dispatched when a 3D loading operation completes.
70
- * - "drawing-loading": Dispatched when a 2D drawing loading operation starts.
71
- * - "drawing-loaded": Dispatched when a 2D drawing loading operation completes.
72
- * - "drawing-zoom-changed": Dispatched when the 2D drawing zoom/pan state changes.
73
- *
74
- * Notes:
75
- * - Automatically creates and manages 2D and 3D viewer components in its shadow DOM.
76
- * - Processes tasks sequentially to ensure consistent state.
77
- * - Designed for extensibility and integration in product configurators and visualization tools.
78
- */
79
- class PrefViewer extends HTMLElement {
80
- #isInitialized = false;
81
- #isLoaded = false;
82
- #isLoading = false;
83
- #mode = "3d";
84
-
85
- #taskQueue = [];
86
-
87
- #wrapper = null;
88
- #component2D = null;
89
- #component3D = null;
90
- #dialog = null;
91
-
92
- /**
93
- * Creates a new PrefViewer instance and attaches a shadow DOM.
94
- * Initializes internal state and component references.
95
- * @public
96
- */
97
- constructor() {
98
- super();
99
- this.attachShadow({ mode: "open" });
100
- }
101
-
102
- /**
103
- * Returns the list of attributes to observe for changes.
104
- * @public
105
- * @returns {string[]} Array of attribute names to observe.
106
- */
107
- static get observedAttributes() {
108
- return ["config", "drawing", "materials", "mode", "model", "scene", "options", "show-model", "show-scene"];
109
- }
110
-
111
- /**
112
- * Observes changes to specific attributes and triggers corresponding actions.
113
- * Loads configuration, drawing, model, scene, materials, or options when their attributes change.
114
- * Toggles model or scene visibility when "show-model" or "show-scene" attributes change.
115
- * @public
116
- * @param {string} name - The name of the changed attribute.
117
- * @param {*} _old - The previous value of the attribute (unused).
118
- * @param {*} value - The new value of the attribute.
119
- * @returns {void}
120
- */
121
- attributeChangedCallback(name, _old, value) {
122
- switch (name) {
123
- case "config":
124
- this.loadConfig(value);
125
- break;
126
- case "drawing":
127
- this.loadDrawing(value);
128
- break;
129
- case "materials":
130
- this.loadMaterials(value);
131
- break;
132
- case "mode":
133
- if (_old === value || value.toLowerCase() === this.#mode) {
134
- return;
135
- }
136
- this.setMode(value.toLowerCase());
137
- break;
138
- case "model":
139
- this.loadModel(value);
140
- break;
141
- case "scene":
142
- this.loadScene(value);
143
- break;
144
- case "options":
145
- this.setOptions(value);
146
- break;
147
- case "show-model":
148
- if (_old === value) {
149
- return;
150
- }
151
- const showModel = value.toLowerCase() === "true";
152
- showModel ? this.showModel() : this.hideModel();
153
- break;
154
- case "show-scene":
155
- if (_old === value) {
156
- return;
157
- }
158
- const showScene = value.toLowerCase() === "true";
159
- showScene ? this.showScene() : this.hideScene();
160
- break;
161
- }
162
- }
163
-
164
- /**
165
- * Called when the element is inserted into the DOM.
166
- * Initializes the 3D and 2D viewer components and starts processing tasks.
167
- * @public
168
- * @returns {void|boolean} Returns false if initialization fails; otherwise void.
169
- */
170
- connectedCallback() {
171
- this.#wrapper = document.createElement("div");
172
- this.#wrapper.classList.add("pref-viewer-wrapper");
173
- this.shadowRoot.append(this.#wrapper);
174
-
175
- const style = document.createElement("style");
176
- const cssUrl = new URL("./css/pref-viewer.css", import.meta.url);
177
- style.textContent = `@import "${cssUrl}";`;
178
- this.shadowRoot.append(style);
179
-
180
- this.#createComponent3D();
181
- this.#createComponent2D();
182
-
183
- if (!this.hasAttribute("mode")) {
184
- this.setMode();
185
- }
186
-
187
- this.#isInitialized = true;
188
- this.#processNextTask();
189
- }
190
-
191
- /**
192
- * Creates and appends the 2D viewer component to the shadow DOM.
193
- * Sets the "visible" attribute to true by default.
194
- * @private
195
- * @returns {void}
196
- */
197
- #createComponent2D() {
198
- this.#component2D = document.createElement("pref-viewer-2d");
199
- this.#component2D.setAttribute("visible", "false");
200
- this.#component2D.addEventListener("drawing-zoom-changed", this.#on2DZoomChanged.bind(this));
201
- this.#wrapper.appendChild(this.#component2D);
202
- }
203
-
204
- /**
205
- * Creates and appends the 3D viewer component to the shadow DOM.
206
- * Sets the "visible" attribute to true by default.
207
- * @private
208
- * @returns {void}
209
- */
210
- #createComponent3D() {
211
- this.#component3D = document.createElement("pref-viewer-3d");
212
- this.#component3D.setAttribute("visible", "false");
213
- this.#wrapper.appendChild(this.#component3D);
214
- }
215
-
216
- /**
217
- * Adds a new task to the internal queue for processing.
218
- * If the viewer is initialized and not currently loading, immediately processes the next task.
219
- * @private
220
- * @param {*} value - The payload or data for the task.
221
- * @param {string} type - The type of task (see PrefViewerTask.Types).
222
- * @returns {void}
223
- */
224
- #addTaskToQueue(value, type) {
225
- this.#taskQueue.push(new PrefViewerTask(value, type));
226
- if (this.#isInitialized && !this.#isLoading) {
227
- this.#processNextTask();
228
- }
229
- }
230
-
231
- /**
232
- * Processes the next task in the queue, if any.
233
- * Dispatches the task to the appropriate handler based on its type.
234
- * @private
235
- * @returns {boolean|void} Returns false if the queue is empty; otherwise void.
236
- */
237
- #processNextTask() {
238
- if (!this.#taskQueue.length) {
239
- return false;
240
- }
241
- const task = this.#taskQueue[0];
242
- this.#taskQueue.shift();
243
- switch (task.type) {
244
- case PrefViewerTask.Types.Config:
245
- this.#processConfig(task.value);
246
- break;
247
- case PrefViewerTask.Types.Drawing:
248
- this.#processDrawing(task.value);
249
- break;
250
- case PrefViewerTask.Types.Environment:
251
- this.#processEnvironment(task.value);
252
- break;
253
- case PrefViewerTask.Types.Materials:
254
- this.#processMaterials(task.value);
255
- break;
256
- case PrefViewerTask.Types.Model:
257
- this.#processModel(task.value);
258
- break;
259
- case PrefViewerTask.Types.Options:
260
- this.#processOptions(task.value);
261
- break;
262
- case PrefViewerTask.Types.Visibility:
263
- this.#processVisibility(task.value);
264
- break;
265
- }
266
- }
267
-
268
- /**
269
- * Handles the start of a 3D loading operation.
270
- * Updates loading state, sets attributes, and dispatches a "scene-loading" event.
271
- * @private
272
- * @returns {void}
273
- */
274
- #on3DLoading() {
275
- this.#isLoaded = false;
276
- this.#isLoading = true;
277
-
278
- this.removeAttribute("loaded-3d");
279
- this.setAttribute("loading-3d", "");
280
-
281
- const customEventOptions = {
282
- bubbles: true,
283
- cancelable: false,
284
- composed: true,
285
- };
286
- this.dispatchEvent(new CustomEvent("scene-loading", customEventOptions));
287
- }
288
-
289
- /**
290
- * Handles the completion of a 3D loading operation.
291
- * Updates loading state, sets attributes, dispatches a "scene-loaded" event, and processes the next task.
292
- * @private
293
- * @param {object} [detail] - Optional details to include in the event.
294
- * @returns {void}
295
- */
296
- #on3DLoaded(detail) {
297
- this.#isLoaded = true;
298
- this.#isLoading = false;
299
-
300
- this.removeAttribute("loading-3d");
301
- this.setAttribute("loaded-3d", "");
302
-
303
- const customEventOptions = {
304
- bubbles: true,
305
- cancelable: true,
306
- composed: true,
307
- };
308
- if (detail) {
309
- customEventOptions.detail = detail;
310
- }
311
- this.dispatchEvent(new CustomEvent("scene-loaded", customEventOptions));
312
- }
313
-
314
- /**
315
- * Handles the start of a 2D loading operation.
316
- * Updates loading state, sets attributes, and dispatches a "drawing-loading" event.
317
- * @private
318
- * @returns {void}
319
- */
320
- #on2DLoading() {
321
- this.#isLoaded = false;
322
- this.#isLoading = true;
323
-
324
- this.removeAttribute("loaded-2d");
325
- this.setAttribute("loading-2d", "");
326
-
327
- const customEventOptions = {
328
- bubbles: true,
329
- cancelable: true,
330
- composed: true,
331
- };
332
- this.dispatchEvent(new CustomEvent("drawing-loading", customEventOptions));
333
- }
334
-
335
- /**
336
- * Handles the completion of a 2D loading operation.
337
- * Updates loading state, sets attributes, dispatches a "drawing-loaded" event, and processes the next task.
338
- * @private
339
- * @param {object} [detail] - Optional details to include in the event.
340
- * @returns {void}
341
- */
342
- #on2DLoaded(detail) {
343
- this.#isLoaded = true;
344
- this.#isLoading = false;
345
-
346
- this.removeAttribute("loading-2d");
347
- this.setAttribute("loaded-2d", "");
348
-
349
- const customEventOptions = {
350
- bubbles: true,
351
- cancelable: true,
352
- composed: true,
353
- };
354
- if (detail) {
355
- customEventOptions.detail = detail;
356
- }
357
- this.dispatchEvent(new CustomEvent("drawing-loaded", customEventOptions));
358
- }
359
-
360
- /**
361
- * Handles the "drawing-zoom-changed" event from the 2D viewer component.
362
- * Dispatches a custom "drawing-zoom-changed" event from the PrefViewer element, forwarding the event detail to external listeners.
363
- * @private
364
- * @param {CustomEvent} event - The original zoom change event from the 2D viewer.
365
- * @returns {void}
366
- */
367
- #on2DZoomChanged(event) {
368
- event.stopPropagation();
369
- event.preventDefault();
370
- const customEventOptions = {
371
- bubbles: true,
372
- cancelable: true,
373
- composed: true,
374
- detail: event.detail,
375
- };
376
- this.dispatchEvent(new CustomEvent("drawing-zoom-changed", customEventOptions));
377
- }
378
-
379
- /**
380
- * Processes a configuration object by loading it into the 3D component.
381
- * Dispatches loading events and processes the next task when finished.
382
- * @private
383
- * @param {object} config - The configuration object to process.
384
- * @returns {void}
385
- */
386
- #processConfig(config) {
387
- if (!this.#component3D) {
388
- return;
389
- }
390
-
391
- this.#on3DLoading();
392
- this.#component3D.load(config).then((detail) => {
393
- this.#on3DLoaded(detail);
394
- this.#processNextTask();
395
- });
396
- }
397
-
398
- /**
399
- * Processes a drawing object by loading it into the 2D component.
400
- * Processes the next task when finished.
401
- * @private
402
- * @param {object} drawing - The drawing object to process.
403
- * @returns {void}
404
- */
405
- #processDrawing(drawing) {
406
- if (!this.#component2D) {
407
- return;
408
- }
409
-
410
- this.#on2DLoading();
411
- this.#component2D.load(drawing).then((detail) => {
412
- this.#on2DLoaded(detail);
413
- this.#processNextTask();
414
- });
415
- }
416
-
417
- /**
418
- * Processes an environment (scene) object by wrapping it in a config and loading it.
419
- * @private
420
- * @param {object} environment - The environment/scene object to process.
421
- * @returns {void}
422
- */
423
- #processEnvironment(environment) {
424
- const config = {};
425
- config.scene = environment;
426
- this.#processConfig(config);
427
- }
428
-
429
- /**
430
- * Processes a materials object by wrapping it in a config and loading it.
431
- * @private
432
- * @param {object} materials - The materials object to process.
433
- * @returns {void}
434
- */
435
- #processMaterials(materials) {
436
- const config = {};
437
- config.materials = materials;
438
- this.#processConfig(config);
439
- }
440
-
441
- /**
442
- * Processes a model object by wrapping it in a config and loading it.
443
- * @private
444
- * @param {object} model - The model object to process.
445
- * @returns {void}
446
- */
447
- #processModel(model) {
448
- const config = {};
449
- config.model = model;
450
- this.#processConfig(config);
451
- }
452
-
453
- /**
454
- * Processes viewer options by loading them into the 3D component.
455
- * Dispatches loading events and processes the next task.
456
- * @private
457
- * @param {object} options - The options object to process.
458
- * @returns {void}
459
- */
460
- #processOptions(options) {
461
- if (!this.#component3D) {
462
- return;
463
- }
464
-
465
- this.#on3DLoading();
466
- const detail = this.#component3D.setOptions(options);
467
- this.#on3DLoaded(detail);
468
- this.#processNextTask();
469
- }
470
-
471
- /**
472
- * Processes visibility configuration for the model and scene.
473
- * Shows or hides the model and/or scene based on the config, then processes the next task.
474
- * @private
475
- * @param {object} config - The visibility configuration object.
476
- * @returns {void}
477
- */
478
- #processVisibility(config) {
479
- if (!this.#component3D) {
480
- return;
481
- }
482
- const showModel = config.model?.visible;
483
- const showScene = config.scene?.visible;
484
- if (showModel !== undefined) {
485
- showModel ? this.#component3D.showModel() : this.#component3D.hideModel();
486
- }
487
- if (showScene !== undefined) {
488
- showScene ? this.#component3D.showEnvironment() : this.#component3D.hideEnvironment();
489
- }
490
- this.#processNextTask();
491
- }
492
-
493
- /**
494
- * ---------------------------
495
- * Public methods
496
- * ---------------------------
497
- */
498
-
499
- /**
500
- * Sets the viewer mode to "2d" or "3d" and updates component visibility accordingly.
501
- * @public
502
- * @param {string} [mode=this.#mode] - The mode to set ("2d" or "3d").
503
- * @returns {void}
504
- */
505
- setMode(mode = this.#mode) {
506
- if (mode !== "2d" && mode !== "3d") {
507
- console.warn(`PrefViewer: invalid mode "${mode}". Allowed modes are "2d" and "3d".`);
508
- mode = this.#mode;
509
- }
510
- this.#mode = mode;
511
- if (mode === "2d") {
512
- this.#component3D?.hide();
513
- this.#component2D?.show();
514
- } else {
515
- this.#component2D?.hide();
516
- this.#component3D?.show();
517
- }
518
- if (this.getAttribute("mode") !== mode) {
519
- this.setAttribute("mode", mode);
520
- if (this.#dialog) {
521
- this.closeDialog();
522
- }
523
- }
524
- }
525
-
526
- /**
527
- * Loads a configuration object or JSON string and adds it to the task queue.
528
- * If the config contains a drawing, adds a drawing task as well.
529
- * @public
530
- * @param {object|string} config - Configuration object or JSON string.
531
- * @returns {boolean|void} Returns false if config is invalid; otherwise void.
532
- */
533
- loadConfig(config) {
534
- config = typeof config === "string" ? JSON.parse(config) : config;
535
- if (!config) {
536
- return false;
537
- }
538
- if (config.drawing) {
539
- this.#addTaskToQueue(config.drawing, "drawing");
540
- }
541
- this.#addTaskToQueue(config, "config");
542
- }
543
-
544
- /**
545
- * Loads a model object or JSON string and adds it to the task queue.
546
- * @public
547
- * @param {object|string} model - Model object or JSON string.
548
- * @returns {boolean|void} Returns false if model is invalid; otherwise void.
549
- */
550
- loadModel(model) {
551
- model = typeof model === "string" ? JSON.parse(model) : model;
552
- if (!model) {
553
- return false;
554
- }
555
- this.#addTaskToQueue(model, "model");
556
- }
557
-
558
- /**
559
- * Loads a scene/environment object or JSON string and adds it to the task queue.
560
- * @public
561
- * @param {object|string} scene - Scene object or JSON string.
562
- * @returns {boolean|void} Returns false if scene is invalid; otherwise void.
563
- */
564
- loadScene(scene) {
565
- scene = typeof scene === "string" ? JSON.parse(scene) : scene;
566
- if (!scene) {
567
- return false;
568
- }
569
- this.#addTaskToQueue(scene, "environment");
570
- }
571
-
572
- /**
573
- * Loads materials object or JSON string and adds it to the task queue.
574
- * @public
575
- * @param {object|string} materials - Materials object or JSON string.
576
- * @returns {boolean|void} Returns false if materials are invalid; otherwise void.
577
- */
578
- loadMaterials(materials) {
579
- materials = typeof materials === "string" ? JSON.parse(materials) : materials;
580
- if (!materials) {
581
- return false;
582
- }
583
- this.#addTaskToQueue(materials, "materials");
584
- }
585
-
586
- /**
587
- * Loads a drawing object or JSON string and adds it to the task queue.
588
- * @public
589
- * @param {object|string} drawing - Drawing object or JSON string.
590
- * @returns {boolean|void} Returns false if drawing is invalid; otherwise void.
591
- */
592
- loadDrawing(drawing) {
593
- drawing = typeof drawing === "string" ? JSON.parse(drawing) : drawing;
594
- if (!drawing) {
595
- return false;
596
- }
597
- this.#addTaskToQueue(drawing, "drawing");
598
- }
599
-
600
- /**
601
- * Sets viewer options from an object or JSON string and adds them to the task queue.
602
- * @public
603
- * @param {object|string} options - Options object or JSON string.
604
- * @returns {boolean|void} Returns false if options are invalid; otherwise void.
605
- */
606
- setOptions(options) {
607
- options = typeof options === "string" ? JSON.parse(options) : options;
608
- if (!options) {
609
- return false;
610
- }
611
- this.#addTaskToQueue(options, "options");
612
- }
613
-
614
- /**
615
- * Shows the 3D model by setting its visibility to true.
616
- * Adds a visibility task to the queue for processing.
617
- * @public
618
- * @returns {void}
619
- */
620
- showModel() {
621
- const config = { model: { visible: true } };
622
- this.#addTaskToQueue(config, "visibility");
623
- }
624
-
625
- /**
626
- * Hides the 3D model by setting its visibility to false.
627
- * Adds a visibility task to the queue for processing.
628
- * @public
629
- * @returns {void}
630
- */
631
- hideModel() {
632
- const config = { model: { visible: false } };
633
- this.#addTaskToQueue(config, "visibility");
634
- }
635
-
636
- /**
637
- * Shows the scene/environment by setting its visibility to true.
638
- * Adds a visibility task to the queue for processing.
639
- * @public
640
- * @returns {void}
641
- */
642
- showScene() {
643
- const config = { scene: { visible: true } };
644
- this.#addTaskToQueue(config, "visibility");
645
- }
646
-
647
- /**
648
- * Hides the scene/environment by setting its visibility to false.
649
- * Adds a visibility task to the queue for processing.
650
- * @public
651
- * @returns {void}
652
- */
653
- hideScene() {
654
- const config = { scene: { visible: false } };
655
- this.#addTaskToQueue(config, "visibility");
656
- }
657
-
658
- /**
659
- * Centers the 2D drawing view in the viewer.
660
- * @public
661
- * @returns {void}
662
- * @description
663
- * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
664
- */
665
- zoomCenter() {
666
- if (!this.#component2D || this.#mode !== "2d") {
667
- return;
668
- }
669
- this.#component2D.zoomCenter();
670
- }
671
-
672
- /**
673
- * Zooms the 2D drawing to fit all content within the viewer.
674
- * @public
675
- * @returns {void}
676
- * @description
677
- * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
678
- */
679
- zoomExtentsAll() {
680
- if (!this.#component2D || this.#mode !== "2d") {
681
- return;
682
- }
683
- this.#component2D.zoomExtentsAll();
684
- }
685
-
686
- /**
687
- * Zooms in on the 2D drawing.
688
- * @public
689
- * @returns {void}
690
- * @description
691
- * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
692
- */
693
- zoomIn() {
694
- if (!this.#component2D || this.#mode !== "2d") {
695
- return;
696
- }
697
- this.#component2D.zoomIn();
698
- }
699
-
700
- /**
701
- * Zooms out of the 2D drawing.
702
- * @public
703
- * @returns {void}
704
- * @description
705
- * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
706
- */
707
- zoomOut() {
708
- if (!this.#component2D || this.#mode !== "2d") {
709
- return;
710
- }
711
- this.#component2D.zoomOut();
712
- }
713
-
714
- /**
715
- * Initiates download of the current 3D model in GLB format.
716
- * @public
717
- * @returns {void}
718
- */
719
- downloadModelGLB() {
720
- if (!this.#component3D) {
721
- return;
722
- }
723
-
724
- this.#component3D.downloadModelGLB();
725
- }
726
-
727
- /**
728
- * Initiates download of the current 3D model in GLTF format.
729
- * @public
730
- * @returns {void}
731
- */
732
- downloadModelGLTF() {
733
- if (!this.#component3D) {
734
- return;
735
- }
736
-
737
- this.#component3D.downloadModelGLTF();
738
- }
739
-
740
- /**
741
- * Initiates download of the current 3D model in USDZ format.
742
- * @public
743
- * @returns {void}
744
- */
745
- downloadModelUSDZ() {
746
- if (!this.#component3D) {
747
- return;
748
- }
749
-
750
- this.#component3D.downloadModelUSDZ();
751
- }
752
-
753
- /**
754
- * Initiates download of the current complete scene (3D model and environment) in GLB format.
755
- * @public
756
- * @returns {void}
757
- */
758
- downloadModelAndSceneGLB() {
759
- if (!this.#component3D) {
760
- return;
761
- }
762
-
763
- this.#component3D.downloadModelAndSceneGLB();
764
- }
765
-
766
- /**
767
- * Initiates download of the current complete scene (3D model and environment) in GLTF format.
768
- * @public
769
- * @returns {void}
770
- */
771
- downloadModelAndSceneGLTF() {
772
- if (!this.#component3D) {
773
- return;
774
- }
775
-
776
- this.#component3D.downloadModelAndSceneGLTF();
777
- }
778
-
779
- /**
780
- * Initiates download of the current complete scene (3D model and environment) in USDZ format.
781
- * @public
782
- * @returns {void}
783
- */
784
- downloadModelAndSceneUSDZ() {
785
- if (!this.#component3D) {
786
- return;
787
- }
788
-
789
- this.#component3D.downloadModelAndSceneUSDZ();
790
- }
791
-
792
- /**
793
- * Initiates download of the current 3D environment in GLB format.
794
- * @public
795
- * @returns {void}
796
- */
797
- downloadSceneGLB() {
798
- if (!this.#component3D) {
799
- return;
800
- }
801
-
802
- this.#component3D.downloadSceneGLB();
803
- }
804
-
805
- /**
806
- * Initiates download of the current 3D environment in GLTF format.
807
- * @public
808
- * @returns {void}
809
- */
810
- downloadSceneGLTF() {
811
- if (!this.#component3D) {
812
- return;
813
- }
814
-
815
- this.#component3D.downloadSceneGLTF();
816
- }
817
-
818
- /**
819
- * Initiates download of the current 3D environment in USDZ format.
820
- * @public
821
- * @returns {void}
822
- */
823
- downloadSceneUSDZ() {
824
- if (!this.#component3D) {
825
- return;
826
- }
827
- this.#component3D.downloadSceneUSDZ();
828
- }
829
-
830
- /**
831
- * Opens a modal dialog with the specified title, content, and footer.
832
- * @public
833
- * @param {string} title - The dialog title to display in the header.
834
- * @param {string} content - The HTML content to display in the dialog body.
835
- * @param {string} footer - The HTML content to display in the dialog footer (e.g., action buttons).
836
- * @returns {HTMLElement} The created dialog element.
837
- * @description
838
- * If a dialog is already open, it is closed before opening the new one.
839
- * The dialog is appended to the viewer's shadow DOM and returned for further manipulation.
840
- */
841
- openDialog(title, content, footer) {
842
- if (this.#dialog && this.#dialog.hasAttribute("open")) {
843
- this.#dialog.close();
844
- }
845
- this.#dialog = document.createElement("pref-viewer-dialog");
846
- this.shadowRoot.querySelector(".pref-viewer-wrapper").appendChild(this.#dialog);
847
- const opened = this.#dialog.open(title, content, footer);
848
- return opened ? this.#dialog : null;
849
- }
850
-
851
- /**
852
- * Closes the currently open dialog, if any, and removes it from the DOM.
853
- * @public
854
- * @returns {void}
855
- */
856
- closeDialog() {
857
- if (this.#dialog) {
858
- this.#dialog.close();
859
- this.#dialog = null;
860
- }
861
- }
862
-
863
- /**
864
- * ---------------------------
865
- * Public properties
866
- * ---------------------------
867
- */
868
-
869
- /**
870
- * Indicates whether the viewer has been initialized.
871
- * @public
872
- * @returns {boolean} True if initialized; otherwise, false.
873
- */
874
- get isInitialized() {
875
- return this.#isInitialized;
876
- }
877
-
878
- /**
879
- * Indicates whether the viewer has finished loading.
880
- * @public
881
- * @returns {boolean} True if loaded; otherwise, false.
882
- */
883
- get isLoaded() {
884
- return this.#isLoaded;
885
- }
886
-
887
- /**
888
- * Indicates whether the viewer is currently loading.
889
- * @public
890
- * @returns {boolean} True if loading; otherwise, false.
891
- */
892
- get isLoading() {
893
- return this.#isLoading;
894
- }
895
-
896
- /**
897
- * Indicates whether the viewer is currently in 2D mode.
898
- * @public
899
- * @returns {boolean} True if the viewer is in 2D mode; otherwise, false.
900
- */
901
- get isMode2D() {
902
- return this.#mode === "2d";
903
- }
904
-
905
- /**
906
- * Indicates whether the viewer is currently in 3D mode.
907
- * @public
908
- * @returns {boolean} True if the viewer is in 3D mode; otherwise, false.
909
- */
910
- get isMode3D() {
911
- return this.#mode === "3d";
912
- }
913
- }
1
+ // Imports all main PrefViewer components (2D, 3D, dialog, and root).
2
+ import PrefViewer from "./pref-viewer.js";
3
+ import PrefViewer2D from "./pref-viewer-2d.js";
4
+ import PrefViewer3D from "./pref-viewer-3d.js";
5
+ import PrefViewerDialog from "./pref-viewer-dialog.js";
914
6
 
7
+ // Defines custom elements for use as HTML tags in the application.
915
8
  customElements.define("pref-viewer-2d", PrefViewer2D);
916
9
  customElements.define("pref-viewer-3d", PrefViewer3D);
917
10
  customElements.define("pref-viewer-dialog", PrefViewerDialog);
918
11
  customElements.define("pref-viewer", PrefViewer);
12
+
13
+ // Exposes selected PrefViewer classes globally for external JavaScript use.
14
+ import BabylonJSAnimationController from "./babylonjs-animation-controller.js";
15
+ const PrefViewerClasses = {
16
+ BabylonJSAnimationController
17
+ };
18
+
19
+ if (typeof window !== 'undefined') {
20
+ window.PrefViewerClasses = PrefViewerClasses;
21
+ }
22
+
23
+ export default PrefViewerClasses;