@preference-sl/pref-viewer 2.11.0-beta.1 → 2.11.0-beta.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/src/index.js CHANGED
@@ -1,21 +1,32 @@
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 { PrefViewerTask } from "./pref-viewer-task.js";
4
4
 
5
5
  /**
6
- * PrefViewer - Custom Web Component for rendering and managing 2D and 3D product visualizations.
6
+ * PrefViewer - Custom Web Component for advanced 2D and 3D product visualization and configuration.
7
7
  *
8
8
  * 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.
9
+ * - Encapsulates both 2D (SVG) and 3D (Babylon.js) viewers, supporting glTF/GLB models, environments, and drawings.
10
+ * - Loads assets from remote URLs, Base64 data URIs, and IndexedDB sources.
11
11
  * - Provides a unified API for loading models, scenes, drawings, materials, and configuration via attributes or methods.
12
12
  * - Manages an internal task queue for sequential processing of viewer operations.
13
13
  * - Emits custom events for loading, errors, and state changes to facilitate integration.
14
+ * - Supports downloading models and scenes in GLB and USDZ formats.
15
+ * - Automatically updates the viewer when reactive attributes change.
14
16
  *
15
17
  * Usage:
16
18
  * - 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.
19
+ * - Configure via attributes (config, model, scene, materials, drawing, options, mode).
20
+ * - Control viewer mode, visibility, and downloads via public methods.
21
+ *
22
+ * Reactive Attributes:
23
+ * - config: URL or Base64 for configuration file.
24
+ * - model: URL or Base64 for 3D model (glTF/GLB).
25
+ * - scene: URL or Base64 for environment/scene (glTF/GLB).
26
+ * - materials: URL or Base64 for materials definition.
27
+ * - drawing: URL or Base64 for SVG drawing.
28
+ * - options: JSON string for viewer options.
29
+ * - mode: Viewer mode ("2d" or "3d").
19
30
  *
20
31
  * Public Methods:
21
32
  * - loadConfig(config), loadModel(model), loadScene(scene), loadMaterials(materials), loadDrawing(drawing)
@@ -23,16 +34,21 @@ import {PrefViewerTask} from "./pref-viewer-task.js";
23
34
  * - setMode(mode): Sets the viewer mode to "2d" or "3d" and updates component visibility.
24
35
  * - showModel(), hideModel(), showScene(), hideScene()
25
36
  * - downloadModelGLB(), downloadModelUSDZ(), downloadModelAndSceneGLB(), downloadModelAndSceneUSDZ()
37
+ * - zoomCenter(), zoomExtentsAll(), zoomIn(), zoomOut()
26
38
  *
27
39
  * 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.
40
+ * - isInitialized: Indicates if the viewer is initialized.
41
+ * - isLoaded: Indicates if the viewer has finished loading.
42
+ * - isLoading: Indicates if the viewer is currently loading.
43
+ * - isMode2D: Indicates if the viewer is in 2D mode.
44
+ * - isMode3D: Indicates if the viewer is in 3D mode.
31
45
  *
32
46
  * 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.
47
+ * - "scene-loading": Dispatched when a 3D loading operation starts.
48
+ * - "scene-loaded": Dispatched when a 3D loading operation completes.
49
+ * - "drawing-loading": Dispatched when a 2D drawing loading operation starts.
50
+ * - "drawing-loaded": Dispatched when a 2D drawing loading operation completes.
51
+ * - "drawing-zoom-changed": Dispatched when the 2D drawing zoom/pan state changes.
36
52
  *
37
53
  * Notes:
38
54
  * - Automatically creates and manages 2D and 3D viewer components in its shadow DOM.
@@ -125,7 +141,6 @@ class PrefViewer extends HTMLElement {
125
141
  /**
126
142
  * Called when the element is inserted into the DOM.
127
143
  * 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
144
  * @public
130
145
  * @returns {void|boolean} Returns false if initialization fails; otherwise void.
131
146
  */
@@ -137,20 +152,6 @@ class PrefViewer extends HTMLElement {
137
152
  this.setMode();
138
153
  }
139
154
 
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
155
  this.#isInitialized = true;
155
156
  this.#processNextTask();
156
157
  }
@@ -164,6 +165,7 @@ class PrefViewer extends HTMLElement {
164
165
  #createComponent2D() {
165
166
  this.#component2D = document.createElement("pref-viewer-2d");
166
167
  this.#component2D.setAttribute("visible", "false");
168
+ this.#component2D.addEventListener("drawing-zoom-changed", this.#on2DZoomChanged.bind(this));
167
169
  this.shadowRoot.appendChild(this.#component2D);
168
170
  }
169
171
 
@@ -243,37 +245,38 @@ class PrefViewer extends HTMLElement {
243
245
 
244
246
  this.removeAttribute("loaded-3d");
245
247
  this.setAttribute("loading-3d", "");
246
- this.dispatchEvent(
247
- new CustomEvent("scene-loading", {
248
- bubbles: true,
249
- cancelable: false,
250
- composed: true,
251
- })
252
- );
248
+
249
+ const customEventOptions = {
250
+ bubbles: true,
251
+ cancelable: false,
252
+ composed: true,
253
+ };
254
+ this.dispatchEvent(new CustomEvent("scene-loading", customEventOptions));
253
255
  }
254
256
 
255
257
  /**
256
258
  * Handles the completion of a 3D loading operation.
257
259
  * Updates loading state, sets attributes, dispatches a "scene-loaded" event, and processes the next task.
258
260
  * @private
259
- * @param {object} [detail={}] - Optional details to include in the event.
261
+ * @param {object} [detail] - Optional details to include in the event.
260
262
  * @returns {void}
261
263
  */
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
- );
264
+ #on3DLoaded(detail) {
265
+ this.#isLoaded = true;
266
+ this.#isLoading = false;
271
267
 
272
268
  this.removeAttribute("loading-3d");
273
269
  this.setAttribute("loaded-3d", "");
274
270
 
275
- this.#isLoaded = true;
276
- this.#isLoading = false;
271
+ const customEventOptions = {
272
+ bubbles: true,
273
+ cancelable: true,
274
+ composed: true,
275
+ };
276
+ if (detail) {
277
+ customEventOptions.detail = detail;
278
+ }
279
+ this.dispatchEvent(new CustomEvent("scene-loaded", customEventOptions));
277
280
  }
278
281
 
279
282
  /**
@@ -288,35 +291,57 @@ class PrefViewer extends HTMLElement {
288
291
 
289
292
  this.removeAttribute("loaded-2d");
290
293
  this.setAttribute("loading-2d", "");
291
- this.dispatchEvent(
292
- new CustomEvent("drawing-loading", {
293
- bubbles: true,
294
- cancelable: false,
295
- composed: true,
296
- })
297
- );
294
+
295
+ const customEventOptions = {
296
+ bubbles: true,
297
+ cancelable: true,
298
+ composed: true,
299
+ };
300
+ this.dispatchEvent(new CustomEvent("drawing-loading", customEventOptions));
298
301
  }
299
302
 
300
303
  /**
301
304
  * Handles the completion of a 2D loading operation.
302
305
  * Updates loading state, sets attributes, dispatches a "drawing-loaded" event, and processes the next task.
303
306
  * @private
307
+ * @param {object} [detail] - Optional details to include in the event.
304
308
  * @returns {void}
305
309
  */
306
- #on2DLoaded() {
307
- this.dispatchEvent(
308
- new CustomEvent("drawing-loaded", {
309
- bubbles: true,
310
- cancelable: false,
311
- composed: true,
312
- })
313
- );
310
+ #on2DLoaded(detail) {
311
+ this.#isLoaded = true;
312
+ this.#isLoading = false;
314
313
 
315
314
  this.removeAttribute("loading-2d");
316
315
  this.setAttribute("loaded-2d", "");
317
316
 
318
- this.#isLoaded = true;
319
- this.#isLoading = false;
317
+ const customEventOptions = {
318
+ bubbles: true,
319
+ cancelable: true,
320
+ composed: true,
321
+ };
322
+ if (detail) {
323
+ customEventOptions.detail = detail;
324
+ }
325
+ this.dispatchEvent(new CustomEvent("drawing-loaded", customEventOptions));
326
+ }
327
+
328
+ /**
329
+ * Handles the "drawing-zoom-changed" event from the 2D viewer component.
330
+ * Dispatches a custom "drawing-zoom-changed" event from the PrefViewer element, forwarding the event detail to external listeners.
331
+ * @private
332
+ * @param {CustomEvent} event - The original zoom change event from the 2D viewer.
333
+ * @returns {void}
334
+ */
335
+ #on2DZoomChanged(event) {
336
+ event.stopPropagation();
337
+ event.preventDefault();
338
+ const customEventOptions = {
339
+ bubbles: true,
340
+ cancelable: true,
341
+ composed: true,
342
+ detail: event.detail,
343
+ };
344
+ this.dispatchEvent(new CustomEvent("drawing-zoom-changed", customEventOptions));
320
345
  }
321
346
 
322
347
  /**
@@ -351,8 +376,8 @@ class PrefViewer extends HTMLElement {
351
376
  }
352
377
 
353
378
  this.#on2DLoading();
354
- this.#component2D.load(drawing).then(() => {
355
- this.#on2DLoaded();
379
+ this.#component2D.load(drawing).then((detail) => {
380
+ this.#on2DLoaded(detail);
356
381
  this.#processNextTask();
357
382
  });
358
383
  }
@@ -438,7 +463,7 @@ class PrefViewer extends HTMLElement {
438
463
  * Public methods
439
464
  * ---------------------------
440
465
  */
441
-
466
+
442
467
  /**
443
468
  * Sets the viewer mode to "2d" or "3d" and updates component visibility accordingly.
444
469
  * @public
@@ -448,7 +473,7 @@ class PrefViewer extends HTMLElement {
448
473
  setMode(mode = this.#mode) {
449
474
  if (mode !== "2d" && mode !== "3d") {
450
475
  console.warn(`PrefViewer: invalid mode "${mode}". Allowed modes are "2d" and "3d".`);
451
- return;
476
+ mode = this.#mode;
452
477
  }
453
478
  this.#mode = mode;
454
479
  if (mode === "2d") {
@@ -595,6 +620,61 @@ class PrefViewer extends HTMLElement {
595
620
  this.#addTaskToQueue(config, "visibility");
596
621
  }
597
622
 
623
+ /**
624
+ * Centers the 2D drawing view in the viewer.
625
+ * @public
626
+ * @returns {void}
627
+ * @description
628
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
629
+ */
630
+ zoomCenter() {
631
+ if (!this.#component2D || this.#mode !== "2d") {
632
+ return;
633
+ }
634
+ this.#component2D.zoomCenter();
635
+ }
636
+
637
+ /**
638
+ * Zooms the 2D drawing to fit all content within the viewer.
639
+ * @public
640
+ * @returns {void}
641
+ * @description
642
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
643
+ */
644
+ zoomExtentsAll() {
645
+ if (!this.#component2D || this.#mode !== "2d") {
646
+ return;
647
+ }
648
+ this.#component2D.zoomExtentsAll();
649
+ }
650
+
651
+ /**
652
+ * Zooms in on the 2D drawing.
653
+ * @public
654
+ * @returns {void}
655
+ * @description
656
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
657
+ */
658
+ zoomIn() {
659
+ if (!this.#component2D || this.#mode !== "2d") {
660
+ return;
661
+ }
662
+ this.#component2D.zoomIn();
663
+ }
664
+
665
+ /**
666
+ * Zooms out of the 2D drawing.
667
+ * @public
668
+ * @returns {void}
669
+ * @description
670
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
671
+ */
672
+ zoomOut() {
673
+ if (!this.#component2D || this.#mode !== "2d") {
674
+ return;
675
+ }
676
+ this.#component2D.zoomOut();
677
+ }
598
678
 
599
679
  /**
600
680
  * Initiates download of the current 3D model in GLB format.
@@ -653,7 +733,7 @@ class PrefViewer extends HTMLElement {
653
733
  * @public
654
734
  * @returns {boolean} True if initialized; otherwise, false.
655
735
  */
656
- get initialized() {
736
+ get isInitialized() {
657
737
  return this.#isInitialized;
658
738
  }
659
739
 
@@ -662,7 +742,7 @@ class PrefViewer extends HTMLElement {
662
742
  * @public
663
743
  * @returns {boolean} True if loaded; otherwise, false.
664
744
  */
665
- get loaded() {
745
+ get isLoaded() {
666
746
  return this.#isLoaded;
667
747
  }
668
748
 
@@ -671,9 +751,27 @@ class PrefViewer extends HTMLElement {
671
751
  * @public
672
752
  * @returns {boolean} True if loading; otherwise, false.
673
753
  */
674
- get loading() {
754
+ get isLoading() {
675
755
  return this.#isLoading;
676
756
  }
757
+
758
+ /**
759
+ * Indicates whether the viewer is currently in 2D mode.
760
+ * @public
761
+ * @returns {boolean} True if the viewer is in 2D mode; otherwise, false.
762
+ */
763
+ get isMode2D() {
764
+ return this.#mode === "2d";
765
+ }
766
+
767
+ /**
768
+ * Indicates whether the viewer is currently in 3D mode.
769
+ * @public
770
+ * @returns {boolean} True if the viewer is in 3D mode; otherwise, false.
771
+ */
772
+ get isMode3D() {
773
+ return this.#mode === "3d";
774
+ }
677
775
  }
678
776
 
679
777
  customElements.define("pref-viewer-2d", PrefViewer2D);
@@ -21,12 +21,17 @@ import PanzoomController from "./panzoom-controller.js";
21
21
  * - async load(svgConfig) : Promise<boolean> — accept SVG input/config and prepare for render
22
22
  * - show(), hide() — toggle visibility and event subscriptions
23
23
  * - zoomIn(), zoomOut(), zoomCenter(), zoomExtentsAll() — pan/zoom controls
24
+ *
25
+ * * Public Properties:
26
+ * - isInitialized: Indicates whether the component has completed initialization.
27
+ * - isLoaded: Indicates whether the SVG content is loaded and ready.
28
+ * - isVisible: Indicates whether the component is currently visible.
24
29
  *
25
30
  * Events
26
- * - "prefviewer2d-loading": emitted before loading starts
27
- * - "prefviewer2d-loaded": emitted after content and Panzoom are ready
28
- * - "prefviewer2d-error": emitted on validation/fetch errors (detail.error: Error)
29
- * - "prefviewer2d-zoomchanged": emitted when pan/zoom state changes (detail: { moved, scaled, maximized, minimized })
31
+ * - "drawing-loading": emitted before loading starts
32
+ * - "drawing-loaded": emitted after content and Panzoom are ready
33
+ * - "drawing-error": emitted on validation/fetch errors (detail.error: Error)
34
+ * - "drawing-zoom-changed": emitted when pan/zoom state changes (detail: { moved, scaled, maximized, minimized })
30
35
  *
31
36
  * Implementation notes
32
37
  * - Keeps internal state in private fields (#svg, #panzoom, #panzoomOptions, ...).
@@ -80,10 +85,10 @@ export class PrefViewer2D extends HTMLElement {
80
85
  }
81
86
 
82
87
  /**
83
- * Handle attribute changes.
84
- * @param {string} name - Attribute name.
85
- * @param {string|null} _old - Previous attribute value (unused).
86
- * @param {string|null} value -New attribute value.
88
+ * Handles changes to observed attributes and updates the component state accordingly.
89
+ * @param {string} name - The name of the changed attribute.
90
+ * @param {string|null} _old - The previous value of the attribute.
91
+ * @param {string|null} value - The new value of the attribute.
87
92
  * @returns {void}
88
93
  * @description
89
94
  * - "svg": triggers load of new SVG content.
@@ -119,6 +124,11 @@ export class PrefViewer2D extends HTMLElement {
119
124
  this.#loadSVG();
120
125
  }
121
126
 
127
+ /**
128
+ * Called when the element is removed from the DOM.
129
+ * Disables the PanzoomController to clean up event listeners and resources.
130
+ * @returns {void}
131
+ */
122
132
  disconnectedCallback() {
123
133
  if (this.#panzoomController) {
124
134
  this.#panzoomController.disable();
@@ -203,20 +213,21 @@ export class PrefViewer2D extends HTMLElement {
203
213
  }
204
214
 
205
215
  /**
206
- * Handle invalid or unsupported SVG input. Emits a custom error event.
216
+ * Handles invalid or unsupported SVG input by emitting a custom error event.
207
217
  * @private
208
- * @returns {void}
218
+ * @returns {object} Detail object with an Error describing the SVG input issue.
209
219
  */
210
220
  #onError() {
211
221
  const error = "PrefViewer2D: Error in SVG provided for loading. Ensure the SVG is a URL to an SVG file, a string containing a SVG, or a string containing a base64-encoded SVG.";
212
- this.dispatchEvent(
213
- new CustomEvent("prefviewer2d-error", {
214
- bubbles: true,
215
- cancelable: false,
216
- composed: true,
217
- detail: { error: new Error(error) },
218
- })
219
- );
222
+ const detail = { error: new Error(error) };
223
+ const customEventOptions = {
224
+ bubbles: true, // indicates whether the event bubbles up through the DOM tree or not
225
+ cancelable: true, // indicates whether the event can be canceled, and therefore prevented as if the event never happened
226
+ composed: false, // indicates whether or not the event will propagate across the shadow DOM boundary into the standard DOM
227
+ detail: detail,
228
+ };
229
+ this.dispatchEvent(new CustomEvent("drawing-error", customEventOptions));
230
+ return detail;
220
231
  }
221
232
 
222
233
  /**
@@ -225,13 +236,12 @@ export class PrefViewer2D extends HTMLElement {
225
236
  * @returns {void}
226
237
  */
227
238
  #onLoaded() {
228
- this.dispatchEvent(
229
- new CustomEvent("prefviewer2d-loaded", {
230
- bubbles: true,
231
- cancelable: false,
232
- composed: true,
233
- })
234
- );
239
+ const customEventOptions = {
240
+ bubbles: true,
241
+ cancelable: true,
242
+ composed: false,
243
+ };
244
+ this.dispatchEvent(new CustomEvent("drawing-loaded", customEventOptions));
235
245
 
236
246
  this.removeAttribute("loading");
237
247
  this.setAttribute("loaded", "");
@@ -246,13 +256,12 @@ export class PrefViewer2D extends HTMLElement {
246
256
  * @returns {void}
247
257
  */
248
258
  #onLoading() {
249
- this.dispatchEvent(
250
- new CustomEvent("prefviewer2d-loading", {
251
- bubbles: true,
252
- cancelable: false,
253
- composed: true,
254
- })
255
- );
259
+ const customEventOptions = {
260
+ bubbles: true,
261
+ cancelable: true,
262
+ composed: false,
263
+ };
264
+ this.dispatchEvent(new CustomEvent("drawing-loading", customEventOptions));
256
265
 
257
266
  this.removeAttribute("loaded");
258
267
  this.setAttribute("loading", "");
@@ -274,10 +283,20 @@ export class PrefViewer2D extends HTMLElement {
274
283
  * the UI with the enabled/disabled status of the zoomIn, zoomOut, center, and zoomExtentsAll buttons.
275
284
  */
276
285
  #onPanzoomChanged(state) {
277
- this.dispatchEvent(
278
- new CustomEvent("prefviewer2d-zoomchanged", {
279
- bubbles: true,
280
- cancelable: false,
286
+ const customEventOptions = {
287
+ bubbles: true,
288
+ cancelable: true,
289
+ composed: false,
290
+ detail: state,
291
+ };
292
+ customEventOptions.detail.desde2d = "true";
293
+ this.dispatchEvent(new CustomEvent("drawing-zoom-changed", customEventOptions));
294
+ }
295
+
296
+ /**
297
+ * Subscribe DOM events required by Panzoom and keyboard interactions.
298
+ * @private
299
+ * @returns {void}
281
300
  composed: true,
282
301
  detail: state,
283
302
  })
@@ -325,17 +344,17 @@ export class PrefViewer2D extends HTMLElement {
325
344
  * @param {object} svgConfig - SVG configuration. An object with a `storage` property describing the source:
326
345
  * { storage: { url: "<url>" } }
327
346
  * or { storage: { db: "<db>", table: "<table>", id: "<id>" } }
328
- * @returns {Promise<boolean|void>}
329
- * - Resolves to false when the input is invalid or the SVG could not be retrieved/decoded, or when there is no update required (cached copy unchanged).
330
- * - Resolves to true when the SVG was accepted and the component has started rendering.
347
+ * @returns {Promise<{success: boolean, error: Error|null}>}
348
+ * - Resolves to { success: false, error: <Error> } when the input is invalid or the SVG could not be retrieved/decoded, or when there is no update required (cached copy unchanged).
349
+ * - Resolves to { success: true, error: null } when the SVG was accepted and the component has started rendering.
331
350
  */
332
351
  async load(svgConfig) {
333
352
  if (!svgConfig || !(await this.#svgResolver.getSVG(svgConfig))) {
334
- this.#onError();
335
- return false;
353
+ const errorDetail = this.#onError();
354
+ return { success: false, ...errorDetail };
336
355
  }
337
- this.#isInitialized && this.#loadSVG();
338
- return true;
356
+ const success = this.#isInitialized && this.#loadSVG();
357
+ return { success: success, error: null };
339
358
  }
340
359
 
341
360
  /**
@@ -417,7 +436,7 @@ export class PrefViewer2D extends HTMLElement {
417
436
  * @public
418
437
  * @returns {boolean} True if the component is initialized; otherwise, false.
419
438
  */
420
- get initialized() {
439
+ get isInitialized() {
421
440
  return this.#isInitialized;
422
441
  }
423
442
 
@@ -426,7 +445,7 @@ export class PrefViewer2D extends HTMLElement {
426
445
  * @public
427
446
  * @returns {boolean} True if the SVG is loaded; otherwise, false.
428
447
  */
429
- get loaded() {
448
+ get isLoaded() {
430
449
  return this.#isLoaded;
431
450
  }
432
451
 
@@ -435,7 +454,7 @@ export class PrefViewer2D extends HTMLElement {
435
454
  * @public
436
455
  * @returns {boolean} True if the component is visible; otherwise, false.
437
456
  */
438
- get visible() {
457
+ get isVisible() {
439
458
  return this.#isVisible;
440
459
  }
441
460
  }