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

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: false,
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,55 @@ 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: false,
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: false,
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
+ const customEventOptions = {
337
+ bubbles: true,
338
+ cancelable: false,
339
+ composed: true,
340
+ detail: event.detail,
341
+ };
342
+ this.dispatchEvent(new CustomEvent("drawing-zoom-changed", customEventOptions));
320
343
  }
321
344
 
322
345
  /**
@@ -351,8 +374,8 @@ class PrefViewer extends HTMLElement {
351
374
  }
352
375
 
353
376
  this.#on2DLoading();
354
- this.#component2D.load(drawing).then(() => {
355
- this.#on2DLoaded();
377
+ this.#component2D.load(drawing).then((detail) => {
378
+ this.#on2DLoaded(detail);
356
379
  this.#processNextTask();
357
380
  });
358
381
  }
@@ -438,7 +461,7 @@ class PrefViewer extends HTMLElement {
438
461
  * Public methods
439
462
  * ---------------------------
440
463
  */
441
-
464
+
442
465
  /**
443
466
  * Sets the viewer mode to "2d" or "3d" and updates component visibility accordingly.
444
467
  * @public
@@ -448,7 +471,7 @@ class PrefViewer extends HTMLElement {
448
471
  setMode(mode = this.#mode) {
449
472
  if (mode !== "2d" && mode !== "3d") {
450
473
  console.warn(`PrefViewer: invalid mode "${mode}". Allowed modes are "2d" and "3d".`);
451
- return;
474
+ mode = this.#mode;
452
475
  }
453
476
  this.#mode = mode;
454
477
  if (mode === "2d") {
@@ -595,6 +618,61 @@ class PrefViewer extends HTMLElement {
595
618
  this.#addTaskToQueue(config, "visibility");
596
619
  }
597
620
 
621
+ /**
622
+ * Centers the 2D drawing view in the viewer.
623
+ * @public
624
+ * @returns {void}
625
+ * @description
626
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
627
+ */
628
+ zoomCenter() {
629
+ if (!this.#component2D || this.#mode !== "2d") {
630
+ return;
631
+ }
632
+ this.#component2D.zoomCenter();
633
+ }
634
+
635
+ /**
636
+ * Zooms the 2D drawing to fit all content within the viewer.
637
+ * @public
638
+ * @returns {void}
639
+ * @description
640
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
641
+ */
642
+ zoomExtentsAll() {
643
+ if (!this.#component2D || this.#mode !== "2d") {
644
+ return;
645
+ }
646
+ this.#component2D.zoomExtentsAll();
647
+ }
648
+
649
+ /**
650
+ * Zooms in on the 2D drawing.
651
+ * @public
652
+ * @returns {void}
653
+ * @description
654
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
655
+ */
656
+ zoomIn() {
657
+ if (!this.#component2D || this.#mode !== "2d") {
658
+ return;
659
+ }
660
+ this.#component2D.zoomIn();
661
+ }
662
+
663
+ /**
664
+ * Zooms out of the 2D drawing.
665
+ * @public
666
+ * @returns {void}
667
+ * @description
668
+ * Only works when the viewer is in 2D mode. Pending implementation for 3D mode when active camera is not blocked.
669
+ */
670
+ zoomOut() {
671
+ if (!this.#component2D || this.#mode !== "2d") {
672
+ return;
673
+ }
674
+ this.#component2D.zoomOut();
675
+ }
598
676
 
599
677
  /**
600
678
  * Initiates download of the current 3D model in GLB format.
@@ -653,7 +731,7 @@ class PrefViewer extends HTMLElement {
653
731
  * @public
654
732
  * @returns {boolean} True if initialized; otherwise, false.
655
733
  */
656
- get initialized() {
734
+ get isInitialized() {
657
735
  return this.#isInitialized;
658
736
  }
659
737
 
@@ -662,7 +740,7 @@ class PrefViewer extends HTMLElement {
662
740
  * @public
663
741
  * @returns {boolean} True if loaded; otherwise, false.
664
742
  */
665
- get loaded() {
743
+ get isLoaded() {
666
744
  return this.#isLoaded;
667
745
  }
668
746
 
@@ -671,9 +749,27 @@ class PrefViewer extends HTMLElement {
671
749
  * @public
672
750
  * @returns {boolean} True if loading; otherwise, false.
673
751
  */
674
- get loading() {
752
+ get isLoading() {
675
753
  return this.#isLoading;
676
754
  }
755
+
756
+ /**
757
+ * Indicates whether the viewer is currently in 2D mode.
758
+ * @public
759
+ * @returns {boolean} True if the viewer is in 2D mode; otherwise, false.
760
+ */
761
+ get isMode2D() {
762
+ return this.#mode === "2d";
763
+ }
764
+
765
+ /**
766
+ * Indicates whether the viewer is currently in 3D mode.
767
+ * @public
768
+ * @returns {boolean} True if the viewer is in 3D mode; otherwise, false.
769
+ */
770
+ get isMode3D() {
771
+ return this.#mode === "3d";
772
+ }
677
773
  }
678
774
 
679
775
  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,
225
+ cancelable: false,
226
+ composed: true,
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: false,
242
+ composed: true,
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: false,
262
+ composed: true,
263
+ };
264
+ this.dispatchEvent(new CustomEvent("drawing-loading", customEventOptions));
256
265
 
257
266
  this.removeAttribute("loaded");
258
267
  this.setAttribute("loading", "");
@@ -274,10 +283,19 @@ 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: false,
289
+ composed: true,
290
+ detail: state,
291
+ };
292
+ this.dispatchEvent(new CustomEvent("drawing-zoom-changed", customEventOptions));
293
+ }
294
+
295
+ /**
296
+ * Subscribe DOM events required by Panzoom and keyboard interactions.
297
+ * @private
298
+ * @returns {void}
281
299
  composed: true,
282
300
  detail: state,
283
301
  })
@@ -325,17 +343,17 @@ export class PrefViewer2D extends HTMLElement {
325
343
  * @param {object} svgConfig - SVG configuration. An object with a `storage` property describing the source:
326
344
  * { storage: { url: "<url>" } }
327
345
  * 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.
346
+ * @returns {Promise<{success: boolean, error: Error|null}>}
347
+ * - 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).
348
+ * - Resolves to { success: true, error: null } when the SVG was accepted and the component has started rendering.
331
349
  */
332
350
  async load(svgConfig) {
333
351
  if (!svgConfig || !(await this.#svgResolver.getSVG(svgConfig))) {
334
- this.#onError();
335
- return false;
352
+ const errorDetail = this.#onError();
353
+ return { success: false, ...errorDetail };
336
354
  }
337
- this.#isInitialized && this.#loadSVG();
338
- return true;
355
+ const success = this.#isInitialized && this.#loadSVG();
356
+ return { success: success, error: null };
339
357
  }
340
358
 
341
359
  /**
@@ -417,7 +435,7 @@ export class PrefViewer2D extends HTMLElement {
417
435
  * @public
418
436
  * @returns {boolean} True if the component is initialized; otherwise, false.
419
437
  */
420
- get initialized() {
438
+ get isInitialized() {
421
439
  return this.#isInitialized;
422
440
  }
423
441
 
@@ -426,7 +444,7 @@ export class PrefViewer2D extends HTMLElement {
426
444
  * @public
427
445
  * @returns {boolean} True if the SVG is loaded; otherwise, false.
428
446
  */
429
- get loaded() {
447
+ get isLoaded() {
430
448
  return this.#isLoaded;
431
449
  }
432
450
 
@@ -435,7 +453,7 @@ export class PrefViewer2D extends HTMLElement {
435
453
  * @public
436
454
  * @returns {boolean} True if the component is visible; otherwise, false.
437
455
  */
438
- get visible() {
456
+ get isVisible() {
439
457
  return this.#isVisible;
440
458
  }
441
459
  }