@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.
@@ -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,8 @@ 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
+ const cssUrl = new URL("./css/pref-viewer-2d.css", import.meta.url);
148
+ style.textContent = `@import "${cssUrl}";`;
138
149
  this.append(style);
139
150
  }
140
151
 
@@ -203,20 +214,21 @@ export class PrefViewer2D extends HTMLElement {
203
214
  }
204
215
 
205
216
  /**
206
- * Handle invalid or unsupported SVG input. Emits a custom error event.
217
+ * Handles invalid or unsupported SVG input by emitting a custom error event.
207
218
  * @private
208
- * @returns {void}
219
+ * @returns {object} Detail object with an Error describing the SVG input issue.
209
220
  */
210
221
  #onError() {
211
222
  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
- );
223
+ const detail = { error: new Error(error) };
224
+ const customEventOptions = {
225
+ bubbles: true, // indicates whether the event bubbles up through the DOM tree or not
226
+ cancelable: true, // indicates whether the event can be canceled, and therefore prevented as if the event never happened
227
+ composed: false, // indicates whether or not the event will propagate across the shadow DOM boundary into the standard DOM
228
+ detail: detail,
229
+ };
230
+ this.dispatchEvent(new CustomEvent("drawing-error", customEventOptions));
231
+ return detail;
220
232
  }
221
233
 
222
234
  /**
@@ -225,13 +237,12 @@ export class PrefViewer2D extends HTMLElement {
225
237
  * @returns {void}
226
238
  */
227
239
  #onLoaded() {
228
- this.dispatchEvent(
229
- new CustomEvent("prefviewer2d-loaded", {
230
- bubbles: true,
231
- cancelable: false,
232
- composed: true,
233
- })
234
- );
240
+ const customEventOptions = {
241
+ bubbles: true,
242
+ cancelable: true,
243
+ composed: false,
244
+ };
245
+ this.dispatchEvent(new CustomEvent("drawing-loaded", customEventOptions));
235
246
 
236
247
  this.removeAttribute("loading");
237
248
  this.setAttribute("loaded", "");
@@ -246,13 +257,12 @@ export class PrefViewer2D extends HTMLElement {
246
257
  * @returns {void}
247
258
  */
248
259
  #onLoading() {
249
- this.dispatchEvent(
250
- new CustomEvent("prefviewer2d-loading", {
251
- bubbles: true,
252
- cancelable: false,
253
- composed: true,
254
- })
255
- );
260
+ const customEventOptions = {
261
+ bubbles: true,
262
+ cancelable: true,
263
+ composed: false,
264
+ };
265
+ this.dispatchEvent(new CustomEvent("drawing-loading", customEventOptions));
256
266
 
257
267
  this.removeAttribute("loaded");
258
268
  this.setAttribute("loading", "");
@@ -274,10 +284,20 @@ export class PrefViewer2D extends HTMLElement {
274
284
  * the UI with the enabled/disabled status of the zoomIn, zoomOut, center, and zoomExtentsAll buttons.
275
285
  */
276
286
  #onPanzoomChanged(state) {
277
- this.dispatchEvent(
278
- new CustomEvent("prefviewer2d-zoomchanged", {
279
- bubbles: true,
280
- cancelable: false,
287
+ const customEventOptions = {
288
+ bubbles: true,
289
+ cancelable: true,
290
+ composed: false,
291
+ detail: state,
292
+ };
293
+ customEventOptions.detail.desde2d = "true";
294
+ this.dispatchEvent(new CustomEvent("drawing-zoom-changed", customEventOptions));
295
+ }
296
+
297
+ /**
298
+ * Subscribe DOM events required by Panzoom and keyboard interactions.
299
+ * @private
300
+ * @returns {void}
281
301
  composed: true,
282
302
  detail: state,
283
303
  })
@@ -325,17 +345,17 @@ export class PrefViewer2D extends HTMLElement {
325
345
  * @param {object} svgConfig - SVG configuration. An object with a `storage` property describing the source:
326
346
  * { storage: { url: "<url>" } }
327
347
  * 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.
348
+ * @returns {Promise<{success: boolean, error: Error|null}>}
349
+ * - 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).
350
+ * - Resolves to { success: true, error: null } when the SVG was accepted and the component has started rendering.
331
351
  */
332
352
  async load(svgConfig) {
333
353
  if (!svgConfig || !(await this.#svgResolver.getSVG(svgConfig))) {
334
- this.#onError();
335
- return false;
354
+ const errorDetail = this.#onError();
355
+ return { success: false, ...errorDetail };
336
356
  }
337
- this.#isInitialized && this.#loadSVG();
338
- return true;
357
+ const success = this.#isInitialized && this.#loadSVG();
358
+ return { success: success, error: null };
339
359
  }
340
360
 
341
361
  /**
@@ -417,7 +437,7 @@ export class PrefViewer2D extends HTMLElement {
417
437
  * @public
418
438
  * @returns {boolean} True if the component is initialized; otherwise, false.
419
439
  */
420
- get initialized() {
440
+ get isInitialized() {
421
441
  return this.#isInitialized;
422
442
  }
423
443
 
@@ -426,7 +446,7 @@ export class PrefViewer2D extends HTMLElement {
426
446
  * @public
427
447
  * @returns {boolean} True if the SVG is loaded; otherwise, false.
428
448
  */
429
- get loaded() {
449
+ get isLoaded() {
430
450
  return this.#isLoaded;
431
451
  }
432
452
 
@@ -435,7 +455,7 @@ export class PrefViewer2D extends HTMLElement {
435
455
  * @public
436
456
  * @returns {boolean} True if the component is visible; otherwise, false.
437
457
  */
438
- get visible() {
458
+ get isVisible() {
439
459
  return this.#isVisible;
440
460
  }
441
461
  }