@preference-sl/pref-viewer 2.11.0-beta.2 → 2.11.0-beta.20

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