calmlens 0.19.0 → 0.21.0

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.
Files changed (108) hide show
  1. package/README.md +13 -9
  2. package/bin/calmlens.js +1646 -706
  3. package/cjs/{BrowserUpload-CJ4Xc9bR.js → BrowserUpload-CeSwiMo5.js} +1 -1
  4. package/{esm/BrowserUpload-RkxV_Vv7.d.ts → cjs/BrowserUpload-D-NnAN7g.d.ts} +2 -2
  5. package/cjs/{CalmLensAppClient-LHMEQ-UZ.js → CalmLensAppClient-0UVCuNqW.js} +30 -10
  6. package/cjs/{CalmLensAppClient-B5gfOB6J.d.ts → CalmLensAppClient-gG16T2py.d.ts} +13 -3
  7. package/cjs/{CalmLensOptionKeys-BUj7v5AG.js → CalmLensOptionKeys-CYY6elxg.js} +242 -345
  8. package/cjs/{CalmLensServerClient-DCSbwIBs.d.ts → CalmLensServerClient-BHx3x_nZ.d.ts} +107 -49
  9. package/cjs/{CalmLensServerClient-BVOEUT_v.js → CalmLensServerClient-nfu6_YEW.js} +297 -92
  10. package/cjs/{CalmLensTypes-DpU-hXq2.d.ts → CalmLensTypes-CUB0YYVz.d.ts} +26601 -27354
  11. package/cjs/{Classification-D3SkprrQ-CMnQKW13.d.ts → Classification-Q5zGpR8l-CEWjnE39.d.ts} +731 -607
  12. package/cjs/Core-C82YmjjX.d.ts +823 -0
  13. package/cjs/Core-Dc3cteU7.js +998 -0
  14. package/cjs/FileIcons-4qSX7DW2.d.ts +67 -0
  15. package/cjs/{ModerationContextLimits-FDpQmw_T.js → ModerationContextLimits-BxOmeSqq.js} +970 -278
  16. package/cjs/{ReactUpload-BvpW4hJi.js → ReactUpload-BK2O3dnK.js} +1 -1
  17. package/cjs/{ReactUpload-huIMnOJ4.d.ts → ReactUpload-DiiXfKvj.d.ts} +1 -1
  18. package/cjs/{TestTriggers-CMwMrbjy-K0YBzu2a.js → TestTriggers-BTyXtX0Q-ATuFDhLX.js} +343 -35
  19. package/{esm/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → cjs/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
  20. package/cjs/{UploadController-D4qOZZc3.js → UploadController-DLyiLDTB.js} +34 -14
  21. package/cjs/{WebhookPayload-CQg8BBZQ.d.ts → WebhookPayload-BLT2y_Jy.d.ts} +93 -86
  22. package/cjs/app.d.ts +6 -7
  23. package/cjs/app.js +4 -3
  24. package/cjs/elements.d.ts +123 -22
  25. package/cjs/elements.js +553 -156
  26. package/cjs/file-icons.d.ts +2 -67
  27. package/cjs/index-CKRgjV16.d.ts +16 -0
  28. package/cjs/index.d.ts +5 -6
  29. package/cjs/index.js +3 -2
  30. package/cjs/react-native.d.ts +52 -11
  31. package/cjs/react-native.js +231 -99
  32. package/cjs/react.d.ts +42 -9
  33. package/cjs/react.js +381 -118
  34. package/cjs/server.d.ts +48 -7
  35. package/cjs/server.js +7 -3
  36. package/cjs/test-inputs.d.ts +2 -2
  37. package/cjs/test-inputs.js +13 -9
  38. package/cjs/testing.d.ts +29 -16
  39. package/cjs/testing.js +1511 -397
  40. package/cjs/ui-CTBUxnVp.d.ts +139 -0
  41. package/cjs/ui-CZ8exACq.js +1359 -0
  42. package/cjs/ui.d.ts +3 -2
  43. package/cjs/ui.js +45 -22
  44. package/cjs/vue.d.ts +802 -199
  45. package/cjs/vue.js +359 -127
  46. package/{cjs/BrowserUpload-RkxV_Vv7.d.ts → esm/BrowserUpload-D-NnAN7g.d.ts} +2 -2
  47. package/esm/{BrowserUpload-DpzS2bbj.js → BrowserUpload-i9OhCzMJ.js} +1 -1
  48. package/esm/{CalmLensAppClient-BjlvBweS.js → CalmLensAppClient-CK4_Py0c.js} +30 -10
  49. package/esm/{CalmLensAppClient-B5gfOB6J.d.ts → CalmLensAppClient-gG16T2py.d.ts} +13 -3
  50. package/esm/{CalmLensOptionKeys-BKez0Wiw.js → CalmLensOptionKeys-BGZlIsT2.js} +235 -338
  51. package/esm/{CalmLensServerClient-DCSbwIBs.d.ts → CalmLensServerClient-BHx3x_nZ.d.ts} +107 -49
  52. package/esm/{CalmLensServerClient-DemxbNMn.js → CalmLensServerClient-dAQUcDWb.js} +274 -93
  53. package/esm/{CalmLensTypes-DpU-hXq2.d.ts → CalmLensTypes-CUB0YYVz.d.ts} +26601 -27354
  54. package/esm/{Classification-D3SkprrQ-CMnQKW13.d.ts → Classification-Q5zGpR8l-CEWjnE39.d.ts} +731 -607
  55. package/esm/Core-BxeIud_S.js +843 -0
  56. package/esm/Core-C82YmjjX.d.ts +823 -0
  57. package/esm/FileIcons-4qSX7DW2.d.ts +67 -0
  58. package/esm/{ModerationContextLimits-lLfmyLni.js → ModerationContextLimits-B6_RalCQ.js} +911 -261
  59. package/esm/{ReactUpload-huIMnOJ4.d.ts → ReactUpload-DiiXfKvj.d.ts} +1 -1
  60. package/esm/{ReactUpload-6klucehl.js → ReactUpload-DxwVqfX1.js} +1 -1
  61. package/esm/{TestTriggers-CMwMrbjy-bdowBBKw.js → TestTriggers-BTyXtX0Q-GRB9jQwo.js} +320 -36
  62. package/{cjs/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → esm/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
  63. package/esm/{UploadController-UxP6gj7A.js → UploadController-D9Z-h00Q.js} +34 -14
  64. package/esm/{WebhookPayload-CQg8BBZQ.d.ts → WebhookPayload-BLT2y_Jy.d.ts} +93 -86
  65. package/esm/app.d.ts +6 -7
  66. package/esm/app.js +3 -3
  67. package/esm/elements.d.ts +123 -22
  68. package/esm/elements.js +497 -128
  69. package/esm/file-icons.d.ts +2 -67
  70. package/esm/index-CKRgjV16.d.ts +16 -0
  71. package/esm/index.d.ts +5 -6
  72. package/esm/index.js +1 -1
  73. package/esm/react-native.d.ts +52 -11
  74. package/esm/react-native.js +205 -75
  75. package/esm/react.d.ts +42 -9
  76. package/esm/react.js +327 -91
  77. package/esm/server.d.ts +48 -7
  78. package/esm/server.js +3 -3
  79. package/esm/styles.css +142 -22
  80. package/esm/svelte/AssetViewer.svelte +25 -0
  81. package/esm/svelte/CalmLensAsset.svelte +318 -0
  82. package/esm/svelte/CalmLensDocumentViewer.svelte +7 -0
  83. package/esm/svelte/CalmLensImageViewer.svelte +7 -0
  84. package/esm/svelte/CalmLensSpreadsheetViewer.svelte +7 -0
  85. package/esm/svelte/CalmLensTextViewer.svelte +7 -0
  86. package/esm/svelte/CalmLensWebsiteViewer.svelte +7 -0
  87. package/esm/svelte/index.d.ts +29 -6
  88. package/esm/svelte/index.js +7 -2
  89. package/esm/test-inputs.d.ts +2 -2
  90. package/esm/test-inputs.js +1 -1
  91. package/esm/testing.d.ts +29 -16
  92. package/esm/testing.js +1507 -393
  93. package/esm/ui-CTBUxnVp.d.ts +139 -0
  94. package/esm/ui-Cr7s6JHm.js +1258 -0
  95. package/esm/ui.d.ts +3 -2
  96. package/esm/ui.js +4 -3
  97. package/esm/vue.d.ts +802 -199
  98. package/esm/vue.js +308 -103
  99. package/package.json +3 -1
  100. package/cjs/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
  101. package/cjs/index-BcDDQJF2.d.ts +0 -253
  102. package/cjs/ui-B23r8BvW.d.ts +0 -539
  103. package/cjs/ui-Dq-cm6Rx.js +0 -586
  104. package/esm/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
  105. package/esm/index-BcDDQJF2.d.ts +0 -253
  106. package/esm/svelte/CalmLensMedia.svelte +0 -134
  107. package/esm/ui-B23r8BvW.d.ts +0 -539
  108. package/esm/ui-Bn29rfAi.js +0 -461
package/esm/elements.js CHANGED
@@ -1,6 +1,180 @@
1
- import { _ as getMediaState, a as getMediaAspectRatio, b as ICONS, c as describeUploadItem, d as formatMessage, f as getDropzoneHint, g as resolveMessages, h as getUploadItemOutcome, i as resolveTheme, l as describeUploader, m as getUploadItemMessage, o as getMediaLoadKey, p as getMediaUnavailableMessage, r as CALMLENS_THEME, s as DEFAULT_MESSAGES, t as CALMLENS_STYLES, u as formatBytes, v as pickMediaSource, x as ICON_ATTRIBUTES, y as resolveMediaProps } from "./ui-Bn29rfAi.js";
2
- import { i as createProgressFetch, n as createUploadController$1, r as isFileAccepted, t as UploadController } from "./UploadController-UxP6gj7A.js";
3
- import { t as createUploadController } from "./BrowserUpload-DpzS2bbj.js";
1
+ import { _ as loadPdfJs, a as ASSET_VIEWERS, c as getSpreadsheetPage, d as createImageViewer, f as createWebsiteViewer, g as resolveViewerMessages, h as formatViewerMessage, i as isMountedViewerKind, l as parseSpreadsheetText, m as DEFAULT_VIEWER_MESSAGES, n as AssetViewerMount, o as createSpreadsheetViewer, p as createDocumentViewer, r as getMountedViewerOptions, s as createTextViewer, t as MediaStreamPlayer, u as spreadsheetColumnIndex } from "./ui-Cr7s6JHm.js";
2
+ import { i as createProgressFetch, n as createUploadController$1, r as isFileAccepted, t as UploadController } from "./UploadController-D9Z-h00Q.js";
3
+ import { C as CALMLENS_THEME, E as isSpreadsheetFormat, S as VIEWER_STYLES, T as getAssetViewerKind, _ as getAssetView, a as describeUploadItem, b as getPlayedPercent, c as formatMessage, d as getUploadItemOutcome, f as resolveMessages, g as getAssetAspectRatio, h as AssetSession, i as DEFAULT_MESSAGES, l as getDropzoneHint, m as ICON_ATTRIBUTES, o as describeUploader, p as ICONS, r as ASSET_FILE_ICON_SIZE, s as formatBytes, t as CALMLENS_STYLES, u as getUploadItemMessage, v as getAssetViewKey, w as resolveTheme, x as pickAssetSource, y as getPeakHeights } from "./Core-BxeIud_S.js";
4
+ import { c as renderFileIconSvg } from "./FileIcons-M6dzjFxd.js";
5
+ import { t as createUploadController } from "./BrowserUpload-i9OhCzMJ.js";
6
+ // src/viewers/Elements.ts
7
+ const BaseElement$1 = typeof HTMLElement === "undefined" ? class {} : HTMLElement;
8
+ const PROPERTIES = [
9
+ "asset",
10
+ "onExpired",
11
+ "getText",
12
+ "text",
13
+ "messages",
14
+ "pdf"
15
+ ];
16
+ /**
17
+ * An asset viewer element. Set the `src` attribute to the asset URL (public or signed) and the
18
+ * `asset` property to its display info or the asset from the API, and `onExpired` to refresh a
19
+ * signed URL; other attributes: `label`, `viewer-claims` (JSON), `sizes` (images), `page-size`
20
+ * (spreadsheets) and `initial-page` (documents). The `text` and `getText` properties supply text
21
+ * and spreadsheet viewers' text, `messages` replaces their text and `pdf` sets where pdf.js loads
22
+ * its files from.
23
+ */
24
+ var ViewerElement = class extends BaseElement$1 {
25
+ static {
26
+ this.observedAttributes = [
27
+ "src",
28
+ "label",
29
+ "viewer-claims",
30
+ "sizes",
31
+ "page-size",
32
+ "initial-page"
33
+ ];
34
+ }
35
+ #viewer;
36
+ #asset;
37
+ #onExpired;
38
+ #getText;
39
+ #text;
40
+ #messages;
41
+ #pdf;
42
+ /** The asset to show, from the API. */
43
+ get asset() {
44
+ return this.#asset;
45
+ }
46
+ set asset(value) {
47
+ this.#asset = value;
48
+ this.#render();
49
+ }
50
+ /** Returns a fresh asset URL from your server once a signed one expires. */
51
+ get onExpired() {
52
+ return this.#onExpired;
53
+ }
54
+ set onExpired(value) {
55
+ this.#onExpired = value;
56
+ this.#render();
57
+ }
58
+ /** Reads a text or spreadsheet viewer's text yourself. */
59
+ get getText() {
60
+ return this.#getText;
61
+ }
62
+ set getText(value) {
63
+ this.#getText = value;
64
+ this.#render();
65
+ }
66
+ /** Text your app already has, for a text or spreadsheet viewer. */
67
+ get text() {
68
+ return this.#text;
69
+ }
70
+ set text(value) {
71
+ this.#text = value;
72
+ this.#render();
73
+ }
74
+ get messages() {
75
+ return this.#messages;
76
+ }
77
+ set messages(value) {
78
+ this.#messages = value;
79
+ this.#render();
80
+ }
81
+ /** Where a document viewer's pdf.js loads its worker, fonts and decoders from. */
82
+ get pdf() {
83
+ return this.#pdf;
84
+ }
85
+ set pdf(value) {
86
+ this.#pdf = value;
87
+ this.#render();
88
+ }
89
+ connectedCallback() {
90
+ const record = this;
91
+ for (const name of PROPERTIES) {
92
+ if (!Object.hasOwn(this, name)) continue;
93
+ const value = record[name];
94
+ delete record[name];
95
+ record[name] = value;
96
+ }
97
+ this.#render();
98
+ }
99
+ attributeChangedCallback() {
100
+ this.#render();
101
+ }
102
+ disconnectedCallback() {
103
+ this.#viewer?.destroy();
104
+ this.#viewer = void 0;
105
+ }
106
+ #render() {
107
+ const src = this.getAttribute("src");
108
+ if (!this.isConnected || !this.#asset || !src) return;
109
+ const pageSize = this.getAttribute("page-size");
110
+ const initialPage = this.getAttribute("initial-page");
111
+ const claims = this.getAttribute("viewer-claims");
112
+ const options = {
113
+ src,
114
+ asset: this.#asset,
115
+ onExpired: this.#onExpired,
116
+ getText: this.#getText,
117
+ text: this.#text,
118
+ messages: this.#messages,
119
+ pdf: this.#pdf,
120
+ label: this.getAttribute("label") ?? void 0,
121
+ viewerClaims: claims ? JSON.parse(claims) : void 0,
122
+ sizes: this.getAttribute("sizes") ?? void 0,
123
+ pageSize: pageSize ? Number(pageSize) : void 0,
124
+ initialPage: initialPage ? Number(initialPage) : void 0
125
+ };
126
+ if (this.#viewer) this.#viewer.update(options);
127
+ else this.#viewer = ASSET_VIEWERS[this.kind](this, options);
128
+ }
129
+ };
130
+ /** `<calmlens-image-viewer>`: an image asset's rendered file, fitted to the element. */
131
+ var CalmLensImageViewerElement = class extends ViewerElement {
132
+ constructor(..._args) {
133
+ super(..._args);
134
+ this.kind = "image";
135
+ }
136
+ };
137
+ /** `<calmlens-website-viewer>`: a website's screenshot and a link to the page. */
138
+ var CalmLensWebsiteViewerElement = class extends ViewerElement {
139
+ constructor(..._args2) {
140
+ super(..._args2);
141
+ this.kind = "website";
142
+ }
143
+ };
144
+ /** `<calmlens-text-viewer>`: text, escaped, in a scrolling region. */
145
+ var CalmLensTextViewerElement = class extends ViewerElement {
146
+ constructor(..._args3) {
147
+ super(..._args3);
148
+ this.kind = "text";
149
+ }
150
+ };
151
+ /** `<calmlens-document-viewer>`: a PDF a page at a time, with pdf.js loaded lazily. */
152
+ var CalmLensDocumentViewerElement = class extends ViewerElement {
153
+ constructor(..._args4) {
154
+ super(..._args4);
155
+ this.kind = "document";
156
+ }
157
+ };
158
+ /** `<calmlens-spreadsheet-viewer>`: a spreadsheet's sheets as tabs and paged tables. */
159
+ var CalmLensSpreadsheetViewerElement = class extends ViewerElement {
160
+ constructor(..._args5) {
161
+ super(..._args5);
162
+ this.kind = "spreadsheet";
163
+ }
164
+ };
165
+ const ELEMENTS = {
166
+ "calmlens-image-viewer": CalmLensImageViewerElement,
167
+ "calmlens-website-viewer": CalmLensWebsiteViewerElement,
168
+ "calmlens-text-viewer": CalmLensTextViewerElement,
169
+ "calmlens-document-viewer": CalmLensDocumentViewerElement,
170
+ "calmlens-spreadsheet-viewer": CalmLensSpreadsheetViewerElement
171
+ };
172
+ /** Defines the asset viewer elements, if they aren't defined yet. */
173
+ const defineCalmLensViewerElements = () => {
174
+ if (typeof customElements === "undefined") return;
175
+ for (const [name, element] of Object.entries(ELEMENTS)) if (!customElements.get(name)) customElements.define(name, element);
176
+ };
177
+ defineCalmLensViewerElements();
4
178
  // src/elements.ts
5
179
  const el = (tag, attributes = {}, children = []) => {
6
180
  const node = document.createElement(tag);
@@ -342,187 +516,382 @@ var CalmLensUploaderElement = class extends BaseElement {
342
516
  }
343
517
  };
344
518
  /**
345
- * `<calmlens-media project-id="…" asset-id="…" status="allowed" alt="…">` shows an asset from the
346
- * CalmLens CDN with a blurred placeholder while it loads. Age-restricted assets show the
347
- * placeholder and a reveal button, which dispatches `calmlens-reveal`, until `age-confirmed` is set.
348
- * Attributes: `project-id`, `asset-id`, `status`, `age-confirmed`, `type`, `resource`, `lens`,
349
- * `transform`, `widths` (comma-separated), `url`, `no-placeholder`, `placeholder-hash`,
350
- * `placeholder-url`, `average-color`, `cdn-base-url`, `alt`, `aspect-ratio`, `width`, `height`,
351
- * `sizes`, `fit`, `position`, `eager`, `fetch-priority` and `no-controls`. Set the
352
- * `asset` property to an API asset instead of `project-id` and `asset-id`.
519
+ * `<calmlens-asset src="…" alt="…">` shows any asset from its asset URL (public, or signed by your
520
+ * server), natively in the page, the same way `CalmLensAsset` does in React, Vue and Svelte. It
521
+ * reads `{src}/info` once connected, unless the `info` property is set. Attributes: `src`,
522
+ * `display` (`full` or `preview`), `viewer-claims` (JSON), `alt`, `fit`, `aspect-ratio`, `eager`,
523
+ * `fetch-priority`, `lens`, `transform`, `widths` (comma-separated), `sizes`, `no-controls`,
524
+ * `autoplay`, `muted`, `loop`, `no-stream`, `initial-page` and `page-size`. Properties: `info`,
525
+ * `onExpired` (returns a fresh signed asset URL), `messages`, `pdf`, `getText` (a spreadsheet's
526
+ * sheets), and `renderUnavailable` / `renderAgeGate` (return your own notice's node). Events:
527
+ * `calmlens-load`, `calmlens-error`, `calmlens-availability-change` (detail: the availability),
528
+ * `calmlens-age-confirm-request`, `calmlens-play`, `calmlens-ended` and `calmlens-timeupdate`
529
+ * (detail: the position in seconds).
353
530
  */
354
- var CalmLensMediaElement = class extends BaseElement {
531
+ var CalmLensAssetElement = class extends BaseElement {
355
532
  static {
356
533
  this.observedAttributes = [
357
- "project-id",
358
- "asset-id",
359
- "status",
360
- "content-removed",
361
- "type",
362
- "resource",
363
- "lens",
364
- "transform",
365
- "widths",
366
- "url",
367
- "no-placeholder",
368
- "placeholder-hash",
369
- "placeholder-url",
370
- "average-color",
371
- "cdn-base-url",
534
+ "src",
535
+ "display",
372
536
  "viewer-claims",
373
537
  "alt",
374
- "aspect-ratio",
375
- "width",
376
- "height",
377
- "sizes",
378
538
  "fit",
379
- "position",
539
+ "aspect-ratio",
380
540
  "eager",
381
541
  "fetch-priority",
382
- "no-controls"
542
+ "lens",
543
+ "transform",
544
+ "widths",
545
+ "sizes",
546
+ "no-controls",
547
+ "autoplay",
548
+ "muted",
549
+ "loop",
550
+ "no-stream",
551
+ "initial-page",
552
+ "page-size"
383
553
  ];
384
554
  }
385
555
  #messages;
386
- #asset;
556
+ #info;
557
+ #onExpired;
558
+ #pdf;
559
+ #getText;
560
+ #renderUnavailable;
561
+ #renderAgeGate;
387
562
  #loadKey;
563
+ #availability;
564
+ #session = new AssetSession(() => this.#render());
565
+ #streams = new MediaStreamPlayer((media) => {
566
+ this.#session.mediaFailed({
567
+ position: media.currentTime || 0,
568
+ playing: !media.paused && !media.ended
569
+ });
570
+ return true;
571
+ });
572
+ #viewers = new AssetViewerMount();
388
573
  get messages() {
389
574
  return this.#messages;
390
575
  }
391
576
  set messages(value) {
392
577
  this.#messages = value;
393
- this.#loadKey = void 0;
578
+ this.#rerender();
579
+ }
580
+ /** The asset's display info, prefetched with `getAssetInfo`, so nothing waits for `/info`. */
581
+ get info() {
582
+ return this.#info;
583
+ }
584
+ set info(value) {
585
+ this.#info = value;
394
586
  this.#render();
395
587
  }
396
- /**
397
- * An asset from the API, in place of `project-id` and `asset-id`. It supplies the status, type
398
- * and preview too; attributes you set win over it.
399
- */
400
- get asset() {
401
- return this.#asset;
588
+ /** Returns a fresh asset URL from your server once a signed one expires. */
589
+ get onExpired() {
590
+ return this.#onExpired;
402
591
  }
403
- set asset(value) {
404
- this.#asset = value;
592
+ set onExpired(value) {
593
+ this.#onExpired = value;
594
+ this.#render();
595
+ }
596
+ /** Where a document's pdf.js loads its worker, fonts and decoders from. */
597
+ get pdf() {
598
+ return this.#pdf;
599
+ }
600
+ set pdf(value) {
601
+ this.#pdf = value;
405
602
  this.#render();
406
603
  }
604
+ /** Reads a spreadsheet's sheets yourself, such as from its text child asset. */
605
+ get getText() {
606
+ return this.#getText;
607
+ }
608
+ set getText(value) {
609
+ this.#getText = value;
610
+ this.#render();
611
+ }
612
+ /** Returns your own unavailable notice. */
613
+ get renderUnavailable() {
614
+ return this.#renderUnavailable;
615
+ }
616
+ set renderUnavailable(value) {
617
+ this.#renderUnavailable = value;
618
+ this.#rerender();
619
+ }
620
+ /** Returns your own age notice; call `confirm` from its reveal button. */
621
+ get renderAgeGate() {
622
+ return this.#renderAgeGate;
623
+ }
624
+ set renderAgeGate(value) {
625
+ this.#renderAgeGate = value;
626
+ this.#rerender();
627
+ }
407
628
  connectedCallback() {
408
- upgradeProperties(this, ["messages", "asset"]);
409
- this.classList.add("calmlens-media");
629
+ upgradeProperties(this, [
630
+ "messages",
631
+ "info",
632
+ "onExpired",
633
+ "pdf",
634
+ "getText",
635
+ "renderUnavailable",
636
+ "renderAgeGate"
637
+ ]);
638
+ this.classList.add("calmlens-asset");
410
639
  this.setAttribute("role", "figure");
411
640
  this.#render();
412
641
  }
413
642
  attributeChangedCallback() {
414
643
  this.#render();
415
644
  }
416
- #mediaOptions() {
645
+ disconnectedCallback() {
646
+ this.#streams.stop();
647
+ this.#viewers.destroy();
648
+ this.#loadKey = void 0;
649
+ }
650
+ #rerender() {
651
+ this.#loadKey = void 0;
652
+ this.#render();
653
+ }
654
+ #emit(name, detail) {
655
+ this.dispatchEvent(new CustomEvent(name, {
656
+ bubbles: true,
657
+ detail
658
+ }));
659
+ }
660
+ #options() {
417
661
  const list = (name) => this.getAttribute(name)?.split(",").map((value) => value.trim()).filter(Boolean);
418
- return resolveMediaProps({
419
- asset: this.#asset,
420
- projectId: this.getAttribute("project-id") ?? void 0,
421
- assetId: this.getAttribute("asset-id") ?? void 0,
422
- status: this.getAttribute("status") ?? void 0,
423
- contentRemoved: this.hasAttribute("content-removed") ? true : void 0,
424
- type: this.getAttribute("type") ?? void 0,
425
- resource: this.getAttribute("resource") ?? void 0,
426
- lens: this.getAttribute("lens") ?? void 0,
427
- transform: this.getAttribute("transform") ?? void 0,
428
- widths: list("widths")?.map(Number),
429
- url: this.getAttribute("url") ?? void 0,
430
- placeholder: !this.hasAttribute("no-placeholder"),
431
- placeholderHash: this.getAttribute("placeholder-hash") ?? void 0,
432
- placeholderUrl: this.getAttribute("placeholder-url") ?? void 0,
433
- viewerClaims: this.hasAttribute("viewer-claims") ? JSON.parse(this.getAttribute("viewer-claims")) : void 0,
434
- cdnBaseUrl: this.getAttribute("cdn-base-url") ?? void 0,
435
- width: numberAttribute(this, "width"),
436
- height: numberAttribute(this, "height"),
437
- averageColor: this.getAttribute("average-color") ?? void 0
438
- });
662
+ let viewerClaims;
663
+ try {
664
+ viewerClaims = this.hasAttribute("viewer-claims") ? JSON.parse(this.getAttribute("viewer-claims")) : void 0;
665
+ } catch {}
666
+ return {
667
+ src: this.getAttribute("src") || void 0,
668
+ display: this.getAttribute("display") ?? void 0,
669
+ viewerClaims,
670
+ info: this.#info,
671
+ alt: this.getAttribute("alt") ?? "",
672
+ image: {
673
+ lens: this.getAttribute("lens") ?? void 0,
674
+ transform: this.getAttribute("transform") ?? void 0,
675
+ widths: list("widths")?.map(Number),
676
+ sizes: this.getAttribute("sizes") ?? void 0
677
+ },
678
+ video: {
679
+ controls: !this.hasAttribute("no-controls"),
680
+ autoPlay: this.hasAttribute("autoplay"),
681
+ muted: this.hasAttribute("muted"),
682
+ loop: this.hasAttribute("loop"),
683
+ stream: !this.hasAttribute("no-stream")
684
+ },
685
+ document: {
686
+ initialPage: numberAttribute(this, "initial-page"),
687
+ pdf: this.#pdf
688
+ },
689
+ spreadsheet: {
690
+ pageSize: numberAttribute(this, "page-size"),
691
+ getText: this.#getText
692
+ },
693
+ onExpired: this.#onExpired
694
+ };
439
695
  }
440
696
  #render() {
441
697
  if (!this.isConnected) return;
442
- const options = this.#mediaOptions();
443
- if (!options.projectId || !options.assetId) return;
444
- const state = getMediaState(options);
445
- const loadKey = `${getMediaLoadKey(state)} ${this.getAttribute("alt")} ${this.getAttribute("sizes")} ${options.viewerClaims?.ageConfirmed}`;
446
- const ratio = getMediaAspectRatio({
447
- aspectRatio: numberAttribute(this, "aspect-ratio"),
448
- width: options.width,
449
- height: options.height
450
- });
698
+ const options = this.#options();
699
+ const state = this.#session.update(options);
700
+ this.#session.load();
701
+ const view = getAssetView(options, state);
702
+ const ratio = getAssetAspectRatio(view, state.info, numberAttribute(this, "aspect-ratio"));
451
703
  this.style.aspectRatio = ratio ? String(ratio) : "";
452
704
  this.toggleAttribute("data-sized", Boolean(ratio));
453
- this.style.backgroundColor = options.averageColor ?? "";
705
+ this.style.backgroundColor = state.info?.content?.preview?.averageColor ?? "";
454
706
  this.style.setProperty("--calmlens-fit", this.getAttribute("fit"));
455
- this.style.setProperty("--calmlens-position", this.getAttribute("position"));
456
- this.dataset.kind = state.kind;
457
- if (loadKey === this.#loadKey) return;
707
+ this.dataset.kind = view.kind;
708
+ this.toggleAttribute("aria-busy", view.kind === "pending");
709
+ const availability = view.kind === "unavailable" ? "unavailable" : view.kind === "age-restricted" ? "age-restricted" : view.kind === "pending" ? void 0 : "available";
710
+ if (availability && availability !== this.#availability) {
711
+ this.#availability = availability;
712
+ this.#emit("calmlens-availability-change", availability);
713
+ }
714
+ const viewerOptions = getMountedViewerOptions(options, this.#session);
715
+ const loadKey = `${getAssetViewKey(view)} ${options.alt} ${options.image.sizes} ${options.video.controls}`;
716
+ if (loadKey === this.#loadKey) {
717
+ if (isMountedViewerKind(view.kind)) this.#viewers.update(this.querySelector(".calmlens-asset__viewer"), view.kind, viewerOptions);
718
+ return;
719
+ }
458
720
  this.#loadKey = loadKey;
459
721
  this.dataset.state = "loading";
722
+ this.#viewers.destroy();
460
723
  const messages = resolveMessages(this.#messages);
461
- const alt = this.getAttribute("alt") ?? "";
462
- const sizes = this.getAttribute("sizes") ?? void 0;
463
- const placeholder = state.kind !== "audio" && state.kind !== "unavailable" && state.placeholderUrl ? el("img", {
464
- class: "calmlens-media__placeholder",
465
- src: state.placeholderUrl,
724
+ const { alt } = options;
725
+ const loaded = () => {
726
+ this.#session.loaded();
727
+ this.dataset.state = "loaded";
728
+ this.querySelector(":scope > .calmlens-asset__placeholder")?.remove();
729
+ this.#emit("calmlens-load");
730
+ };
731
+ const placeholderUrl = "placeholderUrl" in view ? view.placeholderUrl : void 0;
732
+ const placeholder = placeholderUrl ? el("img", {
733
+ class: "calmlens-asset__placeholder",
734
+ src: placeholderUrl,
466
735
  alt: "",
467
736
  "aria-hidden": "true"
468
737
  }) : void 0;
469
- const notice = (children) => el("div", { class: "calmlens-media__notice" }, children);
470
- const unavailable = () => notice([el("p", {}, [icon("unavailable"), el("span", { class: "calmlens-media__label" }, [getMediaUnavailableMessage(state, messages)])])]);
471
- const fail = () => {
472
- this.dataset.state = "error";
473
- this.replaceChildren(unavailable());
738
+ const notice = (children) => el("div", { class: "calmlens-asset__notice" }, children);
739
+ const playable = (media) => {
740
+ media.addEventListener("loadedmetadata", () => {
741
+ this.#session.mediaLoaded(media);
742
+ this.dataset.state = "loaded";
743
+ this.#emit("calmlens-load");
744
+ });
745
+ media.addEventListener("error", () => {
746
+ if (this.#streams.recover(media)) return;
747
+ this.#session.mediaFailed({
748
+ position: media.currentTime || 0,
749
+ playing: !media.paused && !media.ended
750
+ });
751
+ });
752
+ media.addEventListener("play", () => this.#emit("calmlens-play"));
753
+ media.addEventListener("ended", () => this.#emit("calmlens-ended"));
754
+ media.addEventListener("timeupdate", () => this.#emit("calmlens-timeupdate", media.currentTime));
755
+ return media;
474
756
  };
475
- const controls = !this.hasAttribute("no-controls");
476
- if (state.kind === "unavailable") this.replaceChildren(unavailable());
477
- else if (state.kind === "gated") {
478
- const reveal = el("button", {
479
- type: "button",
480
- class: "calmlens-button"
481
- }, [messages.reveal]);
482
- reveal.addEventListener("click", () => this.dispatchEvent(new CustomEvent("calmlens-reveal", { bubbles: true })));
483
- this.replaceChildren(...[placeholder].filter((node) => node !== void 0), notice([
484
- el("strong", {}, [messages.sensitiveTitle]),
485
- el("p", {}, [messages.sensitiveBody]),
486
- reveal
487
- ]));
488
- } else if (state.kind === "image") {
489
- const image = el("img", {
490
- class: "calmlens-media__content",
491
- src: state.src,
492
- srcset: state.srcSet,
493
- sizes,
757
+ const image = (shown) => {
758
+ const content = el("img", {
759
+ class: "calmlens-asset__content",
760
+ src: shown.src,
761
+ srcset: shown.srcSet,
762
+ sizes: shown.srcSet ? options.image.sizes : void 0,
494
763
  alt,
495
- width: options.width,
496
- height: options.height,
764
+ width: shown.width,
765
+ height: shown.height,
497
766
  loading: this.hasAttribute("eager") ? void 0 : "lazy",
498
767
  decoding: "async",
499
768
  fetchpriority: this.getAttribute("fetch-priority") ?? void 0
500
769
  });
501
- image.addEventListener("load", () => {
502
- this.dataset.state = "loaded";
503
- placeholder?.remove();
504
- });
505
- image.addEventListener("error", fail);
506
- this.replaceChildren(...[placeholder, image].filter((node) => node !== void 0));
507
- } else {
508
- const media = el(state.kind, {
509
- class: "calmlens-media__content",
510
- src: state.src,
511
- controls,
512
- playsinline: state.kind === "video",
770
+ content.addEventListener("load", loaded);
771
+ content.addEventListener("error", () => this.#session.failed());
772
+ return content;
773
+ };
774
+ const present = (...nodes) => this.replaceChildren(...nodes.filter((node) => node !== void 0));
775
+ if (view.kind === "unavailable") {
776
+ this.dataset.state = "error";
777
+ this.#emit("calmlens-error");
778
+ present(this.#renderUnavailable?.() ?? notice([el("p", {}, [icon("unavailable"), el("span", { class: "calmlens-asset__label" }, [messages.unavailable])])]));
779
+ } else if (view.kind === "age-restricted") {
780
+ const confirm = () => this.#emit("calmlens-age-confirm-request");
781
+ if (this.#renderAgeGate) present(placeholder, this.#renderAgeGate({
782
+ placeholderUrl,
783
+ confirm
784
+ }));
785
+ else {
786
+ const reveal = el("button", {
787
+ type: "button",
788
+ class: "calmlens-button"
789
+ }, [messages.reveal]);
790
+ reveal.addEventListener("click", confirm);
791
+ present(placeholder, notice([
792
+ el("strong", {}, [messages.sensitiveTitle]),
793
+ el("p", {}, [messages.sensitiveBody]),
794
+ reveal
795
+ ]));
796
+ }
797
+ } else if (view.kind === "pending") present(placeholder);
798
+ else if (view.kind === "image") present(placeholder, image(view));
799
+ else if (view.kind === "website") present(placeholder, view.link ? el("div", { class: "calmlens-asset__bar" }, [el("a", {
800
+ class: "calmlens-button",
801
+ href: view.link,
802
+ target: "_blank",
803
+ rel: "noopener noreferrer nofollow"
804
+ }, [formatMessage(messages.openWebsite, { host: new URL(view.link).host })])]) : void 0, el("div", {
805
+ class: "calmlens-asset__scroll",
806
+ role: "region",
807
+ "aria-label": alt || void 0,
808
+ tabindex: 0
809
+ }, [image(view)]));
810
+ else if (view.kind === "video") present(playable(el("video", {
811
+ class: "calmlens-asset__content",
812
+ src: view.stream ? void 0 : view.src,
813
+ poster: view.poster,
814
+ controls: options.video.controls,
815
+ autoplay: options.video.autoPlay,
816
+ muted: options.video.muted,
817
+ loop: options.video.loop,
818
+ playsinline: true,
819
+ preload: "metadata",
820
+ "aria-label": alt || void 0
821
+ })));
822
+ else if (view.kind === "audio") {
823
+ const audio = playable(el("audio", {
824
+ class: "calmlens-asset__content",
825
+ src: view.stream ? void 0 : view.src,
826
+ controls: options.video.controls,
827
+ autoplay: options.video.autoPlay,
828
+ muted: options.video.muted,
829
+ loop: options.video.loop,
513
830
  preload: "metadata",
514
831
  "aria-label": alt || void 0
832
+ }));
833
+ const progress = view.peaks && waveformProgress(view.peaks);
834
+ if (progress) audio.addEventListener("timeupdate", () => {
835
+ const played = getPlayedPercent(audio);
836
+ progress.setAttribute("aria-valuenow", String(played));
837
+ progress.style.setProperty("--calmlens-played", `${played}%`);
838
+ });
839
+ present(view.waveformUrl || progress ? el("div", { class: "calmlens-asset__waveform" }, [view.waveformUrl && el("img", {
840
+ class: "calmlens-asset__wave",
841
+ src: view.waveformUrl,
842
+ alt: "",
843
+ "aria-hidden": "true"
844
+ }), progress]) : void 0, audio);
845
+ } else if (isMountedViewerKind(view.kind)) {
846
+ const root = el("div", { class: "calmlens-asset__viewer" });
847
+ present(root);
848
+ this.#viewers.update(root, view.kind, viewerOptions, (viewerState) => {
849
+ if (viewerState === "loading") return;
850
+ this.dataset.state = viewerState;
851
+ this.#emit(viewerState === "loaded" ? "calmlens-load" : "calmlens-error");
515
852
  });
516
- media.addEventListener("error", fail);
517
- this.replaceChildren(media);
853
+ } else if (view.kind === "file") {
854
+ const file = el("span", {
855
+ class: "calmlens-asset__file",
856
+ role: "img",
857
+ "aria-label": alt || void 0,
858
+ "aria-hidden": alt ? void 0 : "true"
859
+ });
860
+ file.innerHTML = renderFileIconSvg({
861
+ family: view.icon,
862
+ extension: ""
863
+ }, 64);
864
+ present(file);
518
865
  }
866
+ this.#streams.update(this.querySelector("video, audio"), view);
519
867
  }
520
868
  };
521
- /** Defines `<calmlens-uploader>` and `<calmlens-media>`, if they aren't defined yet. */
869
+ /** Audio's progress drawn over its waveform; decorative, as the audio's own controls report it. */
870
+ const waveformProgress = (peaks) => {
871
+ const bars = (played) => el("div", {
872
+ class: "calmlens-asset__peaks",
873
+ "data-played": played
874
+ }, getPeakHeights(peaks).map((height) => {
875
+ const bar = el("span");
876
+ bar.style.height = `${height}%`;
877
+ return bar;
878
+ }));
879
+ const progress = el("div", {
880
+ class: "calmlens-asset__progress",
881
+ role: "progressbar",
882
+ "aria-hidden": "true",
883
+ "aria-valuemin": 0,
884
+ "aria-valuemax": 100,
885
+ "aria-valuenow": 0
886
+ }, [bars(false), bars(true)]);
887
+ progress.style.setProperty("--calmlens-played", "0%");
888
+ return progress;
889
+ };
890
+ /** Defines `<calmlens-uploader>` and `<calmlens-asset>`, if they aren't defined yet. */
522
891
  const defineCalmLensElements = () => {
523
892
  if (typeof customElements === "undefined") return;
524
893
  if (!customElements.get("calmlens-uploader")) customElements.define("calmlens-uploader", CalmLensUploaderElement);
525
- if (!customElements.get("calmlens-media")) customElements.define("calmlens-media", CalmLensMediaElement);
894
+ if (!customElements.get("calmlens-asset")) customElements.define("calmlens-asset", CalmLensAssetElement);
526
895
  };
527
896
  defineCalmLensElements();
528
- export { CALMLENS_STYLES, CALMLENS_THEME, CalmLensMediaElement, CalmLensUploaderElement, DEFAULT_MESSAGES, ICONS, ICON_ATTRIBUTES, UploadController, createProgressFetch, createUploadController, defineCalmLensElements, describeUploadItem, describeUploader, formatBytes, formatMessage, getDropzoneHint, getMediaAspectRatio, getMediaLoadKey, getMediaState, getMediaUnavailableMessage, getUploadItemMessage, getUploadItemOutcome, isFileAccepted, pickMediaSource, resolveMediaProps, resolveMessages, resolveTheme };
897
+ export { ASSET_FILE_ICON_SIZE, ASSET_VIEWERS, AssetSession, AssetViewerMount, CALMLENS_STYLES, CALMLENS_THEME, CalmLensAssetElement, CalmLensDocumentViewerElement, CalmLensImageViewerElement, CalmLensSpreadsheetViewerElement, CalmLensTextViewerElement, CalmLensUploaderElement, CalmLensWebsiteViewerElement, DEFAULT_MESSAGES, DEFAULT_VIEWER_MESSAGES, ICONS, ICON_ATTRIBUTES, MediaStreamPlayer, UploadController, VIEWER_STYLES, createDocumentViewer, createImageViewer, createProgressFetch, createSpreadsheetViewer, createTextViewer, createUploadController, createWebsiteViewer, defineCalmLensElements, defineCalmLensViewerElements, describeUploadItem, describeUploader, formatBytes, formatMessage, formatViewerMessage, getAssetAspectRatio, getAssetView, getAssetViewKey, getAssetViewerKind, getDropzoneHint, getMountedViewerOptions, getPeakHeights, getPlayedPercent, getSpreadsheetPage, getUploadItemMessage, getUploadItemOutcome, isFileAccepted, isMountedViewerKind, isSpreadsheetFormat, loadPdfJs, parseSpreadsheetText, pickAssetSource, resolveMessages, resolveTheme, resolveViewerMessages, spreadsheetColumnIndex };