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

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.
@@ -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();
@@ -134,7 +144,7 @@ export class PrefViewer2D extends HTMLElement {
134
144
  this.#wrapper = document.createElement("div");
135
145
  this.append(this.#wrapper);
136
146
  const style = document.createElement("style");
137
- style.textContent = `pref-viewer-2d[visible="true"] { display: block; } pref-viewer-2d[visible="false"] { display: none; } pref-viewer-2d, pref-viewer-2d > div, pref-viewer-2d > div > svg { width: 100%; height: 100%; display: block; position: relative; outline: none; box-sizing: border-box; } pref-viewer-2d > div > svg { padding: 10px; }`;
147
+ style.textContent = `@import "/dist/css/pref-viewer-2d.css";`;
138
148
  this.append(style);
139
149
  }
140
150
 
@@ -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
  }