@preference-sl/pref-viewer 2.11.0-beta.1 → 2.11.0-beta.11

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,38 +1,75 @@
1
1
  import { PrefViewer2D } from "./pref-viewer-2d.js";
2
2
  import { PrefViewer3D } from "./pref-viewer-3d.js";
3
- import {PrefViewerTask} from "./pref-viewer-task.js";
3
+ import { PrefViewerDialog } from "./pref-viewer-dialog.js";
4
+ import { PrefViewerTask } from "./pref-viewer-task.js";
4
5
 
5
6
  /**
6
- * PrefViewer - Custom Web Component for rendering and managing 2D and 3D product visualizations.
7
+ * PrefViewer - Custom Web Component for advanced 2D and 3D product visualization and configuration.
7
8
  *
8
9
  * Overview:
9
- * - Encapsulates both 2D and 3D viewers using Babylon.js, supporting glTF/GLB models and environments.
10
- * - Handles loading from remote URLs, Base64 data URIs, and IndexedDB sources.
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.
11
12
  * - Provides a unified API for loading models, scenes, drawings, materials, and configuration via attributes or methods.
12
13
  * - Manages an internal task queue for sequential processing of viewer operations.
13
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.
14
17
  *
15
18
  * Usage:
16
19
  * - Use as a custom HTML element: <pref-viewer ...>
17
- * - Configure via attributes (e.g., config, model, scene, materials, drawing, options).
18
- * - Control visibility, downloads, and viewer mode via public methods.
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").
19
31
  *
20
32
  * Public Methods:
21
- * - loadConfig(config), loadModel(model), loadScene(scene), loadMaterials(materials), loadDrawing(drawing)
22
- * - setOptions(options)
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.
23
39
  * - setMode(mode): Sets the viewer mode to "2d" or "3d" and updates component visibility.
24
- * - showModel(), hideModel(), showScene(), hideScene()
25
- * - downloadModelGLB(), downloadModelUSDZ(), downloadModelAndSceneGLB(), downloadModelAndSceneUSDZ()
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.
26
59
  *
27
60
  * Public Properties:
28
- * - initialized: Indicates if the viewer is initialized.
29
- * - loaded: Indicates if the viewer has finished loading.
30
- * - loading: Indicates if the viewer is currently loading.
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.
31
66
  *
32
67
  * Events:
33
- * - "scene-loading": Dispatched when a loading operation starts.
34
- * - "scene-loaded": Dispatched when a loading operation completes.
35
- * - "scene-error": Dispatched when initialization fails.
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.
36
73
  *
37
74
  * Notes:
38
75
  * - Automatically creates and manages 2D and 3D viewer components in its shadow DOM.
@@ -47,8 +84,10 @@ class PrefViewer extends HTMLElement {
47
84
 
48
85
  #taskQueue = [];
49
86
 
87
+ #wrapper = null;
50
88
  #component2D = null;
51
89
  #component3D = null;
90
+ #dialog = null;
52
91
 
53
92
  /**
54
93
  * Creates a new PrefViewer instance and attaches a shadow DOM.
@@ -125,11 +164,19 @@ class PrefViewer extends HTMLElement {
125
164
  /**
126
165
  * Called when the element is inserted into the DOM.
127
166
  * Initializes the 3D and 2D viewer components and starts processing tasks.
128
- * If the "config" attribute is missing, dispatches a "scene-error" event and stops initialization.
129
167
  * @public
130
168
  * @returns {void|boolean} Returns false if initialization fails; otherwise void.
131
169
  */
132
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
+
133
180
  this.#createComponent3D();
134
181
  this.#createComponent2D();
135
182
 
@@ -137,20 +184,6 @@ class PrefViewer extends HTMLElement {
137
184
  this.setMode();
138
185
  }
139
186
 
140
- if (!this.hasAttribute("config")) {
141
- const error = 'PrefViewer: provide "config" as a configuration object to initialize the viewer.';
142
- console.error(error);
143
- this.dispatchEvent(
144
- new CustomEvent("scene-error", {
145
- bubbles: true,
146
- cancelable: false,
147
- composed: true,
148
- detail: { error: new Error(error) },
149
- })
150
- );
151
- return false;
152
- }
153
-
154
187
  this.#isInitialized = true;
155
188
  this.#processNextTask();
156
189
  }
@@ -164,7 +197,8 @@ class PrefViewer extends HTMLElement {
164
197
  #createComponent2D() {
165
198
  this.#component2D = document.createElement("pref-viewer-2d");
166
199
  this.#component2D.setAttribute("visible", "false");
167
- this.shadowRoot.appendChild(this.#component2D);
200
+ this.#component2D.addEventListener("drawing-zoom-changed", this.#on2DZoomChanged.bind(this));
201
+ this.#wrapper.appendChild(this.#component2D);
168
202
  }
169
203
 
170
204
  /**
@@ -176,7 +210,7 @@ class PrefViewer extends HTMLElement {
176
210
  #createComponent3D() {
177
211
  this.#component3D = document.createElement("pref-viewer-3d");
178
212
  this.#component3D.setAttribute("visible", "false");
179
- this.shadowRoot.appendChild(this.#component3D);
213
+ this.#wrapper.appendChild(this.#component3D);
180
214
  }
181
215
 
182
216
  /**
@@ -243,37 +277,38 @@ class PrefViewer extends HTMLElement {
243
277
 
244
278
  this.removeAttribute("loaded-3d");
245
279
  this.setAttribute("loading-3d", "");
246
- this.dispatchEvent(
247
- new CustomEvent("scene-loading", {
248
- bubbles: true,
249
- cancelable: false,
250
- composed: true,
251
- })
252
- );
280
+
281
+ const customEventOptions = {
282
+ bubbles: true,
283
+ cancelable: false,
284
+ composed: true,
285
+ };
286
+ this.dispatchEvent(new CustomEvent("scene-loading", customEventOptions));
253
287
  }
254
288
 
255
289
  /**
256
290
  * Handles the completion of a 3D loading operation.
257
291
  * Updates loading state, sets attributes, dispatches a "scene-loaded" event, and processes the next task.
258
292
  * @private
259
- * @param {object} [detail={}] - Optional details to include in the event.
293
+ * @param {object} [detail] - Optional details to include in the event.
260
294
  * @returns {void}
261
295
  */
262
- #on3DLoaded(detail = {}) {
263
- this.dispatchEvent(
264
- new CustomEvent("scene-loaded", {
265
- bubbles: true,
266
- cancelable: false,
267
- composed: true,
268
- detail: detail,
269
- })
270
- );
296
+ #on3DLoaded(detail) {
297
+ this.#isLoaded = true;
298
+ this.#isLoading = false;
271
299
 
272
300
  this.removeAttribute("loading-3d");
273
301
  this.setAttribute("loaded-3d", "");
274
302
 
275
- this.#isLoaded = true;
276
- this.#isLoading = false;
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));
277
312
  }
278
313
 
279
314
  /**
@@ -288,35 +323,57 @@ class PrefViewer extends HTMLElement {
288
323
 
289
324
  this.removeAttribute("loaded-2d");
290
325
  this.setAttribute("loading-2d", "");
291
- this.dispatchEvent(
292
- new CustomEvent("drawing-loading", {
293
- bubbles: true,
294
- cancelable: false,
295
- composed: true,
296
- })
297
- );
326
+
327
+ const customEventOptions = {
328
+ bubbles: true,
329
+ cancelable: true,
330
+ composed: true,
331
+ };
332
+ this.dispatchEvent(new CustomEvent("drawing-loading", customEventOptions));
298
333
  }
299
334
 
300
335
  /**
301
336
  * Handles the completion of a 2D loading operation.
302
337
  * Updates loading state, sets attributes, dispatches a "drawing-loaded" event, and processes the next task.
303
338
  * @private
339
+ * @param {object} [detail] - Optional details to include in the event.
304
340
  * @returns {void}
305
341
  */
306
- #on2DLoaded() {
307
- this.dispatchEvent(
308
- new CustomEvent("drawing-loaded", {
309
- bubbles: true,
310
- cancelable: false,
311
- composed: true,
312
- })
313
- );
342
+ #on2DLoaded(detail) {
343
+ this.#isLoaded = true;
344
+ this.#isLoading = false;
314
345
 
315
346
  this.removeAttribute("loading-2d");
316
347
  this.setAttribute("loaded-2d", "");
317
348
 
318
- this.#isLoaded = true;
319
- this.#isLoading = false;
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));
320
377
  }
321
378
 
322
379
  /**
@@ -351,8 +408,8 @@ class PrefViewer extends HTMLElement {
351
408
  }
352
409
 
353
410
  this.#on2DLoading();
354
- this.#component2D.load(drawing).then(() => {
355
- this.#on2DLoaded();
411
+ this.#component2D.load(drawing).then((detail) => {
412
+ this.#on2DLoaded(detail);
356
413
  this.#processNextTask();
357
414
  });
358
415
  }
@@ -438,7 +495,7 @@ class PrefViewer extends HTMLElement {
438
495
  * Public methods
439
496
  * ---------------------------
440
497
  */
441
-
498
+
442
499
  /**
443
500
  * Sets the viewer mode to "2d" or "3d" and updates component visibility accordingly.
444
501
  * @public
@@ -448,18 +505,21 @@ class PrefViewer extends HTMLElement {
448
505
  setMode(mode = this.#mode) {
449
506
  if (mode !== "2d" && mode !== "3d") {
450
507
  console.warn(`PrefViewer: invalid mode "${mode}". Allowed modes are "2d" and "3d".`);
451
- return;
508
+ mode = this.#mode;
452
509
  }
453
510
  this.#mode = mode;
454
511
  if (mode === "2d") {
455
512
  this.#component3D?.hide();
456
513
  this.#component2D?.show();
457
514
  } else {
458
- this.#component3D?.show();
459
515
  this.#component2D?.hide();
516
+ this.#component3D?.show();
460
517
  }
461
518
  if (this.getAttribute("mode") !== mode) {
462
519
  this.setAttribute("mode", mode);
520
+ if (this.#dialog) {
521
+ this.closeDialog();
522
+ }
463
523
  }
464
524
  }
465
525
 
@@ -595,6 +655,61 @@ class PrefViewer extends HTMLElement {
595
655
  this.#addTaskToQueue(config, "visibility");
596
656
  }
597
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
+ }
598
713
 
599
714
  /**
600
715
  * Initiates download of the current 3D model in GLB format.
@@ -609,6 +724,19 @@ class PrefViewer extends HTMLElement {
609
724
  this.#component3D.downloadModelGLB();
610
725
  }
611
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
+
612
740
  /**
613
741
  * Initiates download of the current 3D model in USDZ format.
614
742
  * @public
@@ -623,7 +751,33 @@ class PrefViewer extends HTMLElement {
623
751
  }
624
752
 
625
753
  /**
626
- * Initiates download of both the 3D model and scene in USDZ format.
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.
627
781
  * @public
628
782
  * @returns {void}
629
783
  */
@@ -636,24 +790,88 @@ class PrefViewer extends HTMLElement {
636
790
  }
637
791
 
638
792
  /**
639
- * Initiates download of both the 3D model and scene in GLB format.
793
+ * Initiates download of the current 3D environment in GLB format.
640
794
  * @public
641
795
  * @returns {void}
642
796
  */
643
- downloadModelAndSceneGLB() {
797
+ downloadSceneGLB() {
644
798
  if (!this.#component3D) {
645
799
  return;
646
800
  }
647
801
 
648
- this.#component3D.downloadModelAndSceneGLB();
802
+ this.#component3D.downloadSceneGLB();
649
803
  }
650
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
+
651
869
  /**
652
870
  * Indicates whether the viewer has been initialized.
653
871
  * @public
654
872
  * @returns {boolean} True if initialized; otherwise, false.
655
873
  */
656
- get initialized() {
874
+ get isInitialized() {
657
875
  return this.#isInitialized;
658
876
  }
659
877
 
@@ -662,7 +880,7 @@ class PrefViewer extends HTMLElement {
662
880
  * @public
663
881
  * @returns {boolean} True if loaded; otherwise, false.
664
882
  */
665
- get loaded() {
883
+ get isLoaded() {
666
884
  return this.#isLoaded;
667
885
  }
668
886
 
@@ -671,11 +889,30 @@ class PrefViewer extends HTMLElement {
671
889
  * @public
672
890
  * @returns {boolean} True if loading; otherwise, false.
673
891
  */
674
- get loading() {
892
+ get isLoading() {
675
893
  return this.#isLoading;
676
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
+ }
677
913
  }
678
914
 
679
915
  customElements.define("pref-viewer-2d", PrefViewer2D);
680
916
  customElements.define("pref-viewer-3d", PrefViewer3D);
917
+ customElements.define("pref-viewer-dialog", PrefViewerDialog);
681
918
  customElements.define("pref-viewer", PrefViewer);