calmlens 0.20.0 → 0.22.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.
- package/README.md +15 -10
- package/bin/calmlens.js +1848 -679
- package/cjs/{BrowserUpload-C07aL9Vq.d.ts → BrowserUpload-BWjZ9ntu.d.ts} +2 -2
- package/cjs/{BrowserUpload-CjSHjx3C.js → BrowserUpload-Bwlib4Zc.js} +1 -1
- package/cjs/{CalmLensAppClient-DDLojj0P.d.ts → CalmLensAppClient-BaW3ddUR.d.ts} +13 -3
- package/cjs/{CalmLensAppClient-NaCxOVlM.js → CalmLensAppClient-DUYMo9PZ.js} +30 -10
- package/cjs/{CalmLensOptionKeys-p2QK687A.js → CalmLensOptionKeys-YvgV995V.js} +318 -374
- package/cjs/{CalmLensServerClient-BDzTthxE.js → CalmLensServerClient-3N-gdsxC.js} +346 -89
- package/cjs/CalmLensServerClient-BKzn8zmp.d.ts +353 -0
- package/cjs/{CalmLensTypes-COJbOuiv.d.ts → CalmLensTypes-BDKwThbO.d.ts} +26993 -27187
- package/cjs/{Classification-D3SkprrQ-CMnQKW13.d.ts → Classification-Q5zGpR8l-CEWjnE39.d.ts} +731 -607
- package/cjs/Core-C33pkBmf.d.ts +826 -0
- package/cjs/Core-Cm5Z1oJ9.js +1001 -0
- package/cjs/FileIcons-4qSX7DW2.d.ts +67 -0
- package/cjs/{ModerationContextLimits-2nIv7Zh3.js → ModerationContextLimits-ud1xpZRK.js} +727 -268
- package/cjs/{ReactUpload-BdTRCTGA.js → ReactUpload-DQ0BYISX.js} +1 -1
- package/cjs/{ReactUpload-CRJNPB5T.d.ts → ReactUpload-DhF8mm51.d.ts} +1 -1
- package/{esm/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → cjs/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
- package/cjs/{TestTriggers-DbhsNPdg-DjvVuc2h.js → TestTriggers-D29T_R0M-D6Bu-Mln.js} +355 -47
- package/cjs/{UploadController-MLVs97Zt.js → UploadController-l8fZM2qT.js} +34 -14
- package/{esm/WebhookPayload-DkukERKE.d.ts → cjs/WebhookPayload-Dt6DttS0.d.ts} +120 -86
- package/cjs/app.d.ts +6 -7
- package/cjs/app.js +4 -3
- package/cjs/elements.d.ts +123 -22
- package/cjs/elements.js +553 -156
- package/cjs/file-icons.d.ts +2 -67
- package/cjs/index-Bde1wjiw.d.ts +16 -0
- package/cjs/index.d.ts +5 -6
- package/cjs/index.js +3 -2
- package/cjs/react-native.d.ts +52 -11
- package/cjs/react-native.js +231 -99
- package/cjs/react.d.ts +42 -9
- package/cjs/react.js +381 -118
- package/cjs/server.d.ts +48 -7
- package/cjs/server.js +7 -3
- package/cjs/test-inputs.d.ts +2 -2
- package/cjs/test-inputs.js +13 -9
- package/cjs/testing.d.ts +29 -15
- package/cjs/testing.js +1722 -433
- package/cjs/ui-BRA4HQ8r.js +1359 -0
- package/cjs/ui-YQuvbidU.d.ts +139 -0
- package/cjs/ui.d.ts +3 -2
- package/cjs/ui.js +45 -22
- package/cjs/vue.d.ts +802 -199
- package/cjs/vue.js +359 -127
- package/esm/{BrowserUpload-C07aL9Vq.d.ts → BrowserUpload-BWjZ9ntu.d.ts} +2 -2
- package/esm/{BrowserUpload-CAVkRotw.js → BrowserUpload-GRZVB0G_.js} +1 -1
- package/esm/{CalmLensAppClient-PQ9VBvlW.js → CalmLensAppClient-B5QIJ6KS.js} +30 -10
- package/esm/{CalmLensAppClient-DDLojj0P.d.ts → CalmLensAppClient-BaW3ddUR.d.ts} +13 -3
- package/esm/{CalmLensOptionKeys-Do2mPP6w.js → CalmLensOptionKeys-BG7Qrh8-.js} +317 -367
- package/esm/CalmLensServerClient-BKzn8zmp.d.ts +353 -0
- package/esm/{CalmLensServerClient-C_H5aAsV.js → CalmLensServerClient-bRUAebi-.js} +323 -90
- package/esm/{CalmLensTypes-COJbOuiv.d.ts → CalmLensTypes-BDKwThbO.d.ts} +26993 -27187
- package/esm/{Classification-D3SkprrQ-CMnQKW13.d.ts → Classification-Q5zGpR8l-CEWjnE39.d.ts} +731 -607
- package/esm/Core-BvQe4moS.js +846 -0
- package/esm/Core-C33pkBmf.d.ts +826 -0
- package/esm/FileIcons-4qSX7DW2.d.ts +67 -0
- package/esm/{ModerationContextLimits-Chw-AzgL.js → ModerationContextLimits-CrxSnVZ3.js} +682 -253
- package/esm/{ReactUpload-CMmL0R5Q.js → ReactUpload-BMO8saxT.js} +1 -1
- package/esm/{ReactUpload-CRJNPB5T.d.ts → ReactUpload-DhF8mm51.d.ts} +1 -1
- package/{cjs/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → esm/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
- package/esm/{TestTriggers-DbhsNPdg-CUdrv8Nm.js → TestTriggers-D29T_R0M-Ztc74ucs.js} +332 -48
- package/esm/{UploadController-BJZghJpg.js → UploadController-DnJrWiE4.js} +34 -14
- package/{cjs/WebhookPayload-DkukERKE.d.ts → esm/WebhookPayload-Dt6DttS0.d.ts} +120 -86
- package/esm/app.d.ts +6 -7
- package/esm/app.js +3 -3
- package/esm/elements.d.ts +123 -22
- package/esm/elements.js +497 -128
- package/esm/file-icons.d.ts +2 -67
- package/esm/index-Bde1wjiw.d.ts +16 -0
- package/esm/index.d.ts +5 -6
- package/esm/index.js +1 -1
- package/esm/react-native.d.ts +52 -11
- package/esm/react-native.js +205 -75
- package/esm/react.d.ts +42 -9
- package/esm/react.js +327 -91
- package/esm/server.d.ts +48 -7
- package/esm/server.js +3 -3
- package/esm/styles.css +142 -22
- package/esm/svelte/AssetViewer.svelte +25 -0
- package/esm/svelte/CalmLensAsset.svelte +318 -0
- package/esm/svelte/CalmLensDocumentViewer.svelte +7 -0
- package/esm/svelte/CalmLensImageViewer.svelte +7 -0
- package/esm/svelte/CalmLensSpreadsheetViewer.svelte +7 -0
- package/esm/svelte/CalmLensTextViewer.svelte +7 -0
- package/esm/svelte/CalmLensWebsiteViewer.svelte +7 -0
- package/esm/svelte/index.d.ts +29 -6
- package/esm/svelte/index.js +7 -2
- package/esm/test-inputs.d.ts +2 -2
- package/esm/test-inputs.js +1 -1
- package/esm/testing.d.ts +29 -15
- package/esm/testing.js +1718 -429
- package/esm/ui-YQuvbidU.d.ts +139 -0
- package/esm/ui-heryDd6g.js +1258 -0
- package/esm/ui.d.ts +3 -2
- package/esm/ui.js +4 -3
- package/esm/vue.d.ts +802 -199
- package/esm/vue.js +308 -103
- package/package.json +4 -2
- package/cjs/CalmLensServerClient-C-jsjn8m.d.ts +0 -259
- package/cjs/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
- package/cjs/index-DxbJ_Xaa.d.ts +0 -253
- package/cjs/ui-BSqPGKrZ.d.ts +0 -539
- package/cjs/ui-CX1z2pD9.js +0 -586
- package/esm/CalmLensServerClient-C-jsjn8m.d.ts +0 -259
- package/esm/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
- package/esm/index-DxbJ_Xaa.d.ts +0 -253
- package/esm/svelte/CalmLensMedia.svelte +0 -134
- package/esm/ui-BSqPGKrZ.d.ts +0 -539
- package/esm/ui-CXH7xJpb.js +0 -461
|
@@ -0,0 +1,846 @@
|
|
|
1
|
+
import { R as CalmLensError, a as appendAssetLocation, c as toUrlViewerClaims, s as getAssetInfo, v as serializeTransform, x as transformSelector } from "./ModerationContextLimits-CrxSnVZ3.js";
|
|
2
|
+
import { a as placeholderHashToDataURL } from "./UploadController-DnJrWiE4.js";
|
|
3
|
+
import { a as getFileIcon, c as renderFileIconSvg } from "./FileIcons-M6dzjFxd.js";
|
|
4
|
+
// src/ui/viewers/ViewerAsset.ts
|
|
5
|
+
/** Spreadsheet MIME types, as `content.format` reports them (compared lower-cased). */
|
|
6
|
+
const SPREADSHEET_FORMATS = /* @__PURE__ */ new Set([
|
|
7
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
8
|
+
"application/vnd.ms-excel.sheet.macroenabled.12",
|
|
9
|
+
"application/vnd.ms-excel",
|
|
10
|
+
"application/vnd.oasis.opendocument.spreadsheet"
|
|
11
|
+
]);
|
|
12
|
+
/** Whether a MIME type is a spreadsheet's. */
|
|
13
|
+
const isSpreadsheetFormat = (format) => Boolean(format && SPREADSHEET_FORMATS.has(format.split(";")[0].trim().toLowerCase()));
|
|
14
|
+
/** The viewer that shows an asset, from its type and `content.rendered.kind`. */
|
|
15
|
+
const getAssetViewerKind = (asset) => {
|
|
16
|
+
const kind = asset.content?.rendered?.kind;
|
|
17
|
+
if (asset.type === "website") return "website";
|
|
18
|
+
if (kind === "video" || kind === "audio") return "media";
|
|
19
|
+
if (kind === "pdf") return "document";
|
|
20
|
+
if (kind === "image") return "image";
|
|
21
|
+
if (asset.type === "multimedia-document" && isSpreadsheetFormat(asset.content?.format)) return "spreadsheet";
|
|
22
|
+
if (asset.type === "plaintext-document") return "text";
|
|
23
|
+
return "none";
|
|
24
|
+
};
|
|
25
|
+
// src/ui/Theme.ts
|
|
26
|
+
const CALMLENS_THEME = {
|
|
27
|
+
accent: "#635bff",
|
|
28
|
+
accentText: "#fff",
|
|
29
|
+
radius: 12,
|
|
30
|
+
text: "#1f2328",
|
|
31
|
+
background: "transparent",
|
|
32
|
+
success: "#12a26f",
|
|
33
|
+
warning: "#e8a317",
|
|
34
|
+
danger: "#e5484d"
|
|
35
|
+
};
|
|
36
|
+
/** `color` at `alpha` as `rgba()`, for a hex colour; other colours are returned as they are. */
|
|
37
|
+
const withAlpha = (color, alpha) => {
|
|
38
|
+
const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color)?.[1];
|
|
39
|
+
if (!hex) return color;
|
|
40
|
+
const full = hex.length === 3 ? [...hex].map((digit) => digit + digit).join("") : hex;
|
|
41
|
+
const [red, green, blue] = [
|
|
42
|
+
0,
|
|
43
|
+
2,
|
|
44
|
+
4
|
|
45
|
+
].map((start) => Number.parseInt(full.slice(start, start + 2), 16));
|
|
46
|
+
return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The full theme from your overrides, mixing the muted, border and surface colours from `text`
|
|
50
|
+
* as the stylesheet mixes them from the page's text colour.
|
|
51
|
+
*/
|
|
52
|
+
const resolveTheme = (overrides = {}) => {
|
|
53
|
+
const text = overrides.text ?? CALMLENS_THEME.text;
|
|
54
|
+
return {
|
|
55
|
+
...CALMLENS_THEME,
|
|
56
|
+
muted: withAlpha(text, .72),
|
|
57
|
+
border: withAlpha(text, .18),
|
|
58
|
+
surface: withAlpha(text, .04),
|
|
59
|
+
...overrides,
|
|
60
|
+
text
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
// src/ui/viewers/ViewerStyles.ts
|
|
64
|
+
/**
|
|
65
|
+
* The asset viewers' styles, part of the components' stylesheet (`calmlens/styles.css`). A viewer
|
|
66
|
+
* fills its element; give the element a height (an embed page's frame, or a box in your page),
|
|
67
|
+
* or it takes the image's shape, or a default height for documents, text and spreadsheets. Set
|
|
68
|
+
* `--calmlens-viewer-background` and `--calmlens-viewer-text` to match a page's own colours.
|
|
69
|
+
*/
|
|
70
|
+
const VIEWER_STYLES = `
|
|
71
|
+
:where(.calmlens-viewer) {
|
|
72
|
+
--calmlens-accent: ${CALMLENS_THEME.accent};
|
|
73
|
+
--calmlens-accent-text: ${CALMLENS_THEME.accentText};
|
|
74
|
+
--calmlens-radius: ${CALMLENS_THEME.radius}px;
|
|
75
|
+
--calmlens-focus-width: 2px;
|
|
76
|
+
--calmlens-focus-offset: 2px;
|
|
77
|
+
--calmlens-border: color-mix(in srgb, currentColor 18%, transparent);
|
|
78
|
+
--calmlens-surface: color-mix(in srgb, currentColor 4%, transparent);
|
|
79
|
+
--calmlens-muted: color-mix(in srgb, currentColor 72%, transparent);
|
|
80
|
+
--calmlens-focus-ring: var(--calmlens-accent);
|
|
81
|
+
box-sizing: border-box;
|
|
82
|
+
font: inherit;
|
|
83
|
+
}
|
|
84
|
+
.calmlens-viewer *, .calmlens-viewer *::before, .calmlens-viewer *::after { box-sizing: border-box; }
|
|
85
|
+
.calmlens-viewer { display: block; position: relative; width: 100%; height: 100%; }
|
|
86
|
+
.calmlens-viewer__frame {
|
|
87
|
+
position: relative; display: flex; flex-direction: column; width: 100%; height: 100%;
|
|
88
|
+
min-height: 0; overflow: hidden;
|
|
89
|
+
/* An opaque surface of its own, so sticky headers and the toolbar cover what scrolls under them. */
|
|
90
|
+
background: var(--calmlens-viewer-background, Canvas); color: var(--calmlens-viewer-text, CanvasText);
|
|
91
|
+
}
|
|
92
|
+
.calmlens-viewer:is([data-kind="text"], [data-kind="document"], [data-kind="spreadsheet"], [data-kind="website"]) > .calmlens-viewer__frame { min-height: 320px; }
|
|
93
|
+
.calmlens-viewer :focus-visible {
|
|
94
|
+
outline: 2px solid var(--calmlens-focus-ring); outline-offset: -2px;
|
|
95
|
+
}
|
|
96
|
+
.calmlens-viewer__sr {
|
|
97
|
+
position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden;
|
|
98
|
+
clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
|
|
99
|
+
}
|
|
100
|
+
.calmlens-viewer__notice {
|
|
101
|
+
display: grid; place-content: center; justify-items: center; gap: 12px; flex: 1;
|
|
102
|
+
padding: 24px 16px; text-align: center; color: var(--calmlens-muted);
|
|
103
|
+
}
|
|
104
|
+
.calmlens-viewer__notice p { margin: 0; max-width: 40ch; }
|
|
105
|
+
.calmlens-viewer__bar {
|
|
106
|
+
display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 8px;
|
|
107
|
+
border-bottom: 1px solid var(--calmlens-border); background: var(--calmlens-viewer-background, Canvas);
|
|
108
|
+
}
|
|
109
|
+
.calmlens-viewer__bar .calmlens-button { min-width: 36px; min-height: 36px; }
|
|
110
|
+
.calmlens-viewer__spacer { flex: 1; }
|
|
111
|
+
.calmlens-viewer__link { text-decoration: none; overflow-wrap: anywhere; }
|
|
112
|
+
.calmlens-viewer__scroll { flex: 1; min-height: 0; overflow: auto; position: relative; }
|
|
113
|
+
/* Images fit inside the frame, keeping their shape; without a frame height, the box takes it. */
|
|
114
|
+
.calmlens-viewer[data-kind="image"] > .calmlens-viewer__frame { aspect-ratio: var(--calmlens-viewer-ratio, auto); max-height: 100%; }
|
|
115
|
+
.calmlens-viewer[data-kind="image"] > .calmlens-viewer__frame > img {
|
|
116
|
+
position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
|
|
117
|
+
}
|
|
118
|
+
.calmlens-viewer:not([data-kind="image"]) .calmlens-viewer__placeholder {
|
|
119
|
+
position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: top;
|
|
120
|
+
}
|
|
121
|
+
.calmlens-viewer__placeholder { filter: blur(12px); transform: scale(1.05); }
|
|
122
|
+
.calmlens-viewer__image { opacity: 0; transition: opacity 250ms; }
|
|
123
|
+
.calmlens-viewer[data-state="loaded"] .calmlens-viewer__image { opacity: 1; }
|
|
124
|
+
.calmlens-viewer[data-kind="website"] .calmlens-viewer__image { position: relative; display: block; width: 100%; height: auto; }
|
|
125
|
+
.calmlens-viewer__text {
|
|
126
|
+
margin: 0 auto; padding: 20px 24px; max-width: 75ch; white-space: pre-wrap; overflow-wrap: anywhere;
|
|
127
|
+
font-family: inherit; font-size: 1rem; line-height: 1.6;
|
|
128
|
+
}
|
|
129
|
+
.calmlens-viewer__pages { flex: 1; min-height: 0; overflow: auto; padding: 16px; text-align: center; }
|
|
130
|
+
.calmlens-viewer__page { display: inline-block; position: relative; vertical-align: top; }
|
|
131
|
+
.calmlens-viewer__page canvas { display: block; background: #fff; box-shadow: 0 1px 4px rgb(0 0 0 / 0.2); }
|
|
132
|
+
.calmlens-viewer__page-field { width: 4.5em; min-height: 36px; padding: 4px 6px; font: inherit; border: 1px solid var(--calmlens-border); border-radius: calc(var(--calmlens-radius) / 2); background: transparent; color: inherit; }
|
|
133
|
+
.calmlens-viewer__zoom { min-width: 4ch; text-align: center; font-variant-numeric: tabular-nums; }
|
|
134
|
+
.calmlens-viewer__sheets { display: flex; flex-direction: column; flex: 1; min-height: 0; }
|
|
135
|
+
.calmlens-viewer__tabs {
|
|
136
|
+
display: flex; gap: 2px; overflow-x: auto; padding: 6px 8px 0; border-bottom: 1px solid var(--calmlens-border);
|
|
137
|
+
}
|
|
138
|
+
.calmlens-viewer__tab {
|
|
139
|
+
padding: 6px 12px; border: 1px solid transparent; border-bottom: 0; border-radius: 6px 6px 0 0;
|
|
140
|
+
background: none; color: inherit; font: inherit; white-space: nowrap; cursor: pointer;
|
|
141
|
+
}
|
|
142
|
+
.calmlens-viewer__tab[aria-selected="true"] { border-color: var(--calmlens-border); background: var(--calmlens-viewer-background, Canvas); font-weight: 600; }
|
|
143
|
+
.calmlens-viewer__tab[data-hidden], .calmlens-viewer__table [data-hidden] { font-style: italic; }
|
|
144
|
+
.calmlens-viewer__panel { display: flex; flex-direction: column; flex: 1; min-height: 0; }
|
|
145
|
+
.calmlens-viewer__table { flex: 1; min-height: 0; overflow: auto; }
|
|
146
|
+
.calmlens-viewer__table table { border-collapse: collapse; font-size: 0.875rem; font-variant-numeric: tabular-nums; }
|
|
147
|
+
.calmlens-viewer__table th, .calmlens-viewer__table td {
|
|
148
|
+
padding: 4px 8px; border: 1px solid var(--calmlens-border); text-align: left; vertical-align: top;
|
|
149
|
+
max-width: 40ch; overflow-wrap: anywhere; white-space: pre-wrap;
|
|
150
|
+
}
|
|
151
|
+
.calmlens-viewer__table thead th { position: sticky; top: 0; z-index: 1; }
|
|
152
|
+
.calmlens-viewer__table th { background: var(--calmlens-viewer-background, Canvas); font-weight: 600; color: var(--calmlens-muted); }
|
|
153
|
+
.calmlens-viewer__table tbody th { position: sticky; left: 0; text-align: right; }
|
|
154
|
+
.calmlens-viewer__corner { background: var(--calmlens-viewer-background, Canvas); }
|
|
155
|
+
.calmlens-viewer__tag { margin-left: 6px; font-size: 0.75em; font-weight: 400; }
|
|
156
|
+
.calmlens-viewer__pager {
|
|
157
|
+
display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px;
|
|
158
|
+
border-top: 1px solid var(--calmlens-border);
|
|
159
|
+
}
|
|
160
|
+
.calmlens-viewer__pager p { margin: 0; }
|
|
161
|
+
.calmlens-viewer__empty { margin: 0; padding: 24px 16px; color: var(--calmlens-muted); }
|
|
162
|
+
.calmlens-viewer__notes { padding: 8px 16px; border-top: 1px solid var(--calmlens-border); font-size: 0.875rem; }
|
|
163
|
+
.calmlens-viewer__notes ul { margin: 0; padding-left: 1.25em; }
|
|
164
|
+
`;
|
|
165
|
+
// src/ui/Asset.ts
|
|
166
|
+
const decodedPlaceholders = /* @__PURE__ */ new Map();
|
|
167
|
+
/** Renders a ThumbHash as a PNG data URL, once per hash; `undefined` for an invalid hash. */
|
|
168
|
+
const decodePlaceholderHash = (hash) => {
|
|
169
|
+
if (!hash) return void 0;
|
|
170
|
+
if (!decodedPlaceholders.has(hash)) {
|
|
171
|
+
let decoded;
|
|
172
|
+
try {
|
|
173
|
+
decoded = placeholderHashToDataURL(hash);
|
|
174
|
+
} catch {}
|
|
175
|
+
if (decodedPlaceholders.size > 500) decodedPlaceholders.clear();
|
|
176
|
+
decodedPlaceholders.set(hash, decoded);
|
|
177
|
+
}
|
|
178
|
+
return decodedPlaceholders.get(hash);
|
|
179
|
+
};
|
|
180
|
+
const withWidth = (base, width) => {
|
|
181
|
+
if (width === void 0) return base;
|
|
182
|
+
if (base === void 0 || typeof base !== "string") return {
|
|
183
|
+
...base,
|
|
184
|
+
width
|
|
185
|
+
};
|
|
186
|
+
return [transformSelector(base), serializeTransform({ width })].filter(Boolean).join(",");
|
|
187
|
+
};
|
|
188
|
+
/** The file-type icon for an asset with nothing to show, from its type. */
|
|
189
|
+
const FILE_ICONS = {
|
|
190
|
+
image: "images",
|
|
191
|
+
video: "video",
|
|
192
|
+
audio: "audio",
|
|
193
|
+
"plaintext-document": "documents",
|
|
194
|
+
"multimedia-document": "documents",
|
|
195
|
+
website: "documents"
|
|
196
|
+
};
|
|
197
|
+
/** A web page's address, if the viewer may link to it. */
|
|
198
|
+
const pageLink = (address) => {
|
|
199
|
+
if (!address) return void 0;
|
|
200
|
+
try {
|
|
201
|
+
const url = new URL(address);
|
|
202
|
+
return url.protocol === "https:" || url.protocol === "http:" ? url.href : void 0;
|
|
203
|
+
} catch {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
/** The asset URL without any query; claims are added per resource. */
|
|
208
|
+
const baseOf = (src) => src.split("?", 1)[0].replace(/\/+$/, "");
|
|
209
|
+
/** The viewer claims an asset URL carries in its query, if any. */
|
|
210
|
+
const claimsOf = (src) => {
|
|
211
|
+
const query = src.split("?", 2)[1];
|
|
212
|
+
const raw = query ? new URLSearchParams(query).get("viewerClaims") : null;
|
|
213
|
+
if (!raw) return void 0;
|
|
214
|
+
try {
|
|
215
|
+
return JSON.parse(raw);
|
|
216
|
+
} catch {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
/** The claims requests carry: the options', else the URL's. Invalid claims are left out. */
|
|
221
|
+
const effectiveClaims = (src, claims) => {
|
|
222
|
+
try {
|
|
223
|
+
return toUrlViewerClaims(claims ?? (src ? claimsOf(src) : void 0));
|
|
224
|
+
} catch {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* What `CalmLensAsset` shows for an asset URL and its display info: the image with its
|
|
230
|
+
* responsive widths, a website's screenshot and link, video with its poster and stream, audio with
|
|
231
|
+
* its waveform, a document, text or spreadsheet viewer, a preview still, a file icon, the age
|
|
232
|
+
* notice or the unavailable notice. `unavailable` is the session's verdict after the CDN refused
|
|
233
|
+
* the asset.
|
|
234
|
+
*/
|
|
235
|
+
const getAssetView = (options, state) => {
|
|
236
|
+
const { src, info } = state;
|
|
237
|
+
if (state.unavailable) return { kind: "unavailable" };
|
|
238
|
+
if (!src) return { kind: "pending" };
|
|
239
|
+
const base = baseOf(src);
|
|
240
|
+
const claims = effectiveClaims(src, options.viewerClaims);
|
|
241
|
+
const url = (resource, location = {}) => appendAssetLocation(base, {
|
|
242
|
+
resource,
|
|
243
|
+
...location,
|
|
244
|
+
viewerClaims: resource === "placeholder" ? claims?.region ? { region: claims.region } : void 0 : claims
|
|
245
|
+
});
|
|
246
|
+
if (!info) return { kind: "pending" };
|
|
247
|
+
const content = info.content ?? {};
|
|
248
|
+
const { rendered, preview } = content;
|
|
249
|
+
const placeholderUrl = decodePlaceholderHash(preview?.thumbhash) ?? (preview || rendered?.kind === "image" ? url("placeholder") : void 0);
|
|
250
|
+
if (info.availability === "unavailable") return { kind: "unavailable" };
|
|
251
|
+
if (info.availability === "age-restricted") return {
|
|
252
|
+
kind: "age-restricted",
|
|
253
|
+
placeholderUrl
|
|
254
|
+
};
|
|
255
|
+
const image = options.image ?? {};
|
|
256
|
+
const still = (resource, size) => {
|
|
257
|
+
const widths = [...new Set(image.widths ?? [])].filter((width) => Number.isSafeInteger(width) && width > 0).sort((a, b) => a - b);
|
|
258
|
+
const build = (width) => url(resource, {
|
|
259
|
+
lens: image.lens,
|
|
260
|
+
transform: withWidth(image.transform, width)
|
|
261
|
+
});
|
|
262
|
+
const sources = widths.map((width) => ({
|
|
263
|
+
src: build(width),
|
|
264
|
+
width
|
|
265
|
+
}));
|
|
266
|
+
return {
|
|
267
|
+
src: sources.at(-1)?.src ?? build(),
|
|
268
|
+
srcSet: sources.length ? sources.map(({ src: source, width }) => `${source} ${width}w`).join(", ") : void 0,
|
|
269
|
+
sources: sources.length ? sources : void 0,
|
|
270
|
+
width: size?.width ?? void 0,
|
|
271
|
+
height: size?.height ?? void 0,
|
|
272
|
+
placeholderUrl
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
const icon = () => ({
|
|
276
|
+
kind: "file",
|
|
277
|
+
icon: FILE_ICONS[info.type ?? ""] ?? "unknown"
|
|
278
|
+
});
|
|
279
|
+
if (options.display === "preview") {
|
|
280
|
+
if (preview) return {
|
|
281
|
+
kind: "image",
|
|
282
|
+
...still("preview", preview)
|
|
283
|
+
};
|
|
284
|
+
if (rendered?.kind === "image") return {
|
|
285
|
+
kind: "image",
|
|
286
|
+
...still("rendered", rendered)
|
|
287
|
+
};
|
|
288
|
+
return icon();
|
|
289
|
+
}
|
|
290
|
+
switch (getAssetViewerKind(info)) {
|
|
291
|
+
case "image": return {
|
|
292
|
+
kind: "image",
|
|
293
|
+
...still("rendered", rendered)
|
|
294
|
+
};
|
|
295
|
+
case "website": return rendered ? {
|
|
296
|
+
kind: "website",
|
|
297
|
+
...still("rendered", rendered),
|
|
298
|
+
link: pageLink(info.externalUrl)
|
|
299
|
+
} : icon();
|
|
300
|
+
case "media": {
|
|
301
|
+
const streams = options.video?.stream !== false && Boolean(content.hls);
|
|
302
|
+
if (rendered?.kind === "audio") return {
|
|
303
|
+
kind: "audio",
|
|
304
|
+
src: url("rendered"),
|
|
305
|
+
stream: streams ? url("hls") : void 0,
|
|
306
|
+
waveformUrl: preview ? url("preview") : void 0,
|
|
307
|
+
peaks: rendered.peaks?.length ? rendered.peaks : void 0
|
|
308
|
+
};
|
|
309
|
+
return {
|
|
310
|
+
kind: "video",
|
|
311
|
+
src: url("rendered"),
|
|
312
|
+
stream: streams ? url("hls") : void 0,
|
|
313
|
+
poster: preview ? url("preview") : void 0,
|
|
314
|
+
width: rendered?.width ?? void 0,
|
|
315
|
+
height: rendered?.height ?? void 0,
|
|
316
|
+
placeholderUrl
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
case "document": return {
|
|
320
|
+
kind: "document",
|
|
321
|
+
src: url("rendered"),
|
|
322
|
+
still: preview ? url("preview") : void 0
|
|
323
|
+
};
|
|
324
|
+
case "text": return { kind: "text" };
|
|
325
|
+
case "spreadsheet": return { kind: "spreadsheet" };
|
|
326
|
+
default: return preview ? {
|
|
327
|
+
kind: "image",
|
|
328
|
+
...still("preview", preview)
|
|
329
|
+
} : icon();
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
/** Width divided by height of what the view shows, when it is known, so the box keeps its size. */
|
|
333
|
+
const getAssetAspectRatio = (view, info, aspectRatio) => {
|
|
334
|
+
if (aspectRatio) return aspectRatio;
|
|
335
|
+
if ("width" in view && view.width && view.height) return view.width / view.height;
|
|
336
|
+
const size = info?.content?.rendered ?? info?.content?.preview;
|
|
337
|
+
return size?.width && size.height && view.kind !== "audio" ? size.width / size.height : void 0;
|
|
338
|
+
};
|
|
339
|
+
/** A key that changes whenever the view must load something new. */
|
|
340
|
+
const getAssetViewKey = (view) => {
|
|
341
|
+
switch (view.kind) {
|
|
342
|
+
case "image":
|
|
343
|
+
case "website": return `${view.kind} ${view.src} ${view.srcSet ?? ""}`;
|
|
344
|
+
case "video": return `video ${view.src} ${view.stream ?? ""} ${view.poster ?? ""}`;
|
|
345
|
+
case "audio": return `audio ${view.src} ${view.stream ?? ""} ${view.waveformUrl ?? ""}`;
|
|
346
|
+
case "document": return `document ${view.src}`;
|
|
347
|
+
case "file": return `file ${view.icon}`;
|
|
348
|
+
default: return view.kind;
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* The image URL to load for a box `displayWidth` pixels wide: the smallest width that covers it,
|
|
353
|
+
* or the largest when none does. Without widths, or before the box is measured, the view's own
|
|
354
|
+
* `src`. React Native, which has no `srcset`, calls this with its measured size.
|
|
355
|
+
*/
|
|
356
|
+
const pickAssetSource = (view, displayWidth) => {
|
|
357
|
+
const { sources } = view;
|
|
358
|
+
if (!sources?.length || !displayWidth) return view.src;
|
|
359
|
+
return (sources.find((source) => source.width >= displayWidth) ?? sources.at(-1))?.src ?? view.src;
|
|
360
|
+
};
|
|
361
|
+
/** Audio's waveform peaks as bar heights, in percent of the waveform's height. */
|
|
362
|
+
const getPeakHeights = (peaks) => peaks.map((peak) => Math.round(Math.min(1, Math.max(.02, peak)) * 1e3) / 10);
|
|
363
|
+
/** How much of a media element has played, in percent, to one decimal place. */
|
|
364
|
+
const getPlayedPercent = (media) => Number.isFinite(media.duration) && media.duration > 0 ? Math.round(Math.min(1, Math.max(0, media.currentTime / media.duration)) * 1e3) / 10 : 0;
|
|
365
|
+
const codeOf = (error) => CalmLensError.isCalmLensError(error) ? error.code : void 0;
|
|
366
|
+
/**
|
|
367
|
+
* One `CalmLensAsset`'s connection to the CDN. Each render calls `update` with the options and
|
|
368
|
+
* renders `getAssetView` of what it returns; once mounted, `load` fetches `{src}/info` when `info`
|
|
369
|
+
* isn't given. Nothing is requested before `src` is set. A resource that fails to load
|
|
370
|
+
* (`failed`) is retried once beneath a fresh asset URL from `onExpired`; then, or without
|
|
371
|
+
* `onExpired`, the info is read again, and the CDN's answer decides: content it now refuses shows
|
|
372
|
+
* the unavailable or age notice, whatever the info said before. `onChange` runs whenever the state
|
|
373
|
+
* changes, so the component renders again.
|
|
374
|
+
*/
|
|
375
|
+
var AssetSession = class {
|
|
376
|
+
#onChange;
|
|
377
|
+
#options = {};
|
|
378
|
+
#given;
|
|
379
|
+
#src;
|
|
380
|
+
#claimsKey;
|
|
381
|
+
#info;
|
|
382
|
+
#infoKey;
|
|
383
|
+
#loading;
|
|
384
|
+
#override;
|
|
385
|
+
#unavailable = false;
|
|
386
|
+
#refreshing = false;
|
|
387
|
+
#resume;
|
|
388
|
+
#generation = 0;
|
|
389
|
+
constructor(onChange) {
|
|
390
|
+
this.#onChange = onChange;
|
|
391
|
+
}
|
|
392
|
+
/** The state for these options. It sends nothing: call `load` once mounted. */
|
|
393
|
+
update(options) {
|
|
394
|
+
this.#options = options;
|
|
395
|
+
if (options.src !== this.#given) {
|
|
396
|
+
this.#generation++;
|
|
397
|
+
this.#given = options.src;
|
|
398
|
+
this.#src = options.src;
|
|
399
|
+
this.#info = void 0;
|
|
400
|
+
this.#infoKey = void 0;
|
|
401
|
+
this.#loading = void 0;
|
|
402
|
+
this.#override = void 0;
|
|
403
|
+
this.#unavailable = false;
|
|
404
|
+
this.#refreshing = false;
|
|
405
|
+
this.#resume = void 0;
|
|
406
|
+
}
|
|
407
|
+
const claimsKey = JSON.stringify(effectiveClaims(this.#src, options.viewerClaims) ?? null);
|
|
408
|
+
if (claimsKey !== this.#claimsKey) {
|
|
409
|
+
this.#claimsKey = claimsKey;
|
|
410
|
+
this.#override = void 0;
|
|
411
|
+
}
|
|
412
|
+
return this.state;
|
|
413
|
+
}
|
|
414
|
+
/** The current state, without new options. */
|
|
415
|
+
get state() {
|
|
416
|
+
const info = this.#options.info ?? this.#info;
|
|
417
|
+
return {
|
|
418
|
+
src: this.#src,
|
|
419
|
+
info: info && this.#override ? {
|
|
420
|
+
...info,
|
|
421
|
+
availability: this.#override
|
|
422
|
+
} : info,
|
|
423
|
+
unavailable: this.#unavailable
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
/** Fetches the display info when it is needed and not yet fetched or loading. Call once mounted. */
|
|
427
|
+
load() {
|
|
428
|
+
if (!this.#src || this.#options.info || this.#unavailable) return;
|
|
429
|
+
const key = this.#key();
|
|
430
|
+
if (this.#infoKey === key || this.#loading === key) return;
|
|
431
|
+
this.#loading = key;
|
|
432
|
+
this.#readInfo().then((info) => {
|
|
433
|
+
if (this.#loading !== key) return;
|
|
434
|
+
this.#loading = void 0;
|
|
435
|
+
this.#info = info;
|
|
436
|
+
this.#infoKey = key;
|
|
437
|
+
this.#onChange();
|
|
438
|
+
}, (error) => {
|
|
439
|
+
if (this.#loading !== key) return;
|
|
440
|
+
this.#loading = void 0;
|
|
441
|
+
this.#refused(error);
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* For a resource that failed to load, such as an image's `error`: retries once beneath a fresh
|
|
446
|
+
* asset URL, or asks the CDN whether the viewer may still see the asset.
|
|
447
|
+
*/
|
|
448
|
+
failed() {
|
|
449
|
+
if (this.#unavailable) return;
|
|
450
|
+
if (this.#options.onExpired && !this.#refreshing) {
|
|
451
|
+
this.refresh().catch(() => {});
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const generation = this.#generation;
|
|
455
|
+
this.#readInfo().then((info) => {
|
|
456
|
+
if (generation !== this.#generation) return;
|
|
457
|
+
if (info.availability === "available") this.#unavailable = true;
|
|
458
|
+
else this.#override = info.availability;
|
|
459
|
+
this.#onChange();
|
|
460
|
+
}, (error) => {
|
|
461
|
+
if (generation === this.#generation) this.#refused(error);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
/** For a resource that loaded: a later failure may refresh the asset URL again. */
|
|
465
|
+
loaded() {
|
|
466
|
+
this.#refreshing = false;
|
|
467
|
+
}
|
|
468
|
+
/** For a refusal the component read itself: `asset/not-available` shows the unavailable notice. */
|
|
469
|
+
unavailable() {
|
|
470
|
+
this.#override = "unavailable";
|
|
471
|
+
this.#onChange();
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* A fresh asset URL from `onExpired`, which every resource then loads beneath. Rejects without
|
|
475
|
+
* `onExpired`, or when it fails, and the component shows the unavailable notice.
|
|
476
|
+
*/
|
|
477
|
+
async refresh() {
|
|
478
|
+
const { onExpired } = this.#options;
|
|
479
|
+
const generation = this.#generation;
|
|
480
|
+
if (!onExpired) {
|
|
481
|
+
this.#unavailable = true;
|
|
482
|
+
this.#onChange();
|
|
483
|
+
throw new Error("No onExpired to refresh the asset URL");
|
|
484
|
+
}
|
|
485
|
+
this.#refreshing = true;
|
|
486
|
+
try {
|
|
487
|
+
const src = await onExpired();
|
|
488
|
+
if (typeof src !== "string" || !src) throw new Error("onExpired returned no asset URL");
|
|
489
|
+
if (generation === this.#generation) {
|
|
490
|
+
this.#src = src;
|
|
491
|
+
this.#onChange();
|
|
492
|
+
}
|
|
493
|
+
return src;
|
|
494
|
+
} catch (error) {
|
|
495
|
+
if (generation === this.#generation) {
|
|
496
|
+
this.#unavailable = true;
|
|
497
|
+
this.#onChange();
|
|
498
|
+
}
|
|
499
|
+
throw error;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/** For a video or audio element's `error`: where it was, so it resumes beneath a fresh URL. */
|
|
503
|
+
mediaFailed(playback) {
|
|
504
|
+
this.#resume = playback;
|
|
505
|
+
this.failed();
|
|
506
|
+
}
|
|
507
|
+
/** For `loadedmetadata`: the URL works, and a recovered element resumes where it stopped. */
|
|
508
|
+
mediaLoaded(media) {
|
|
509
|
+
this.loaded();
|
|
510
|
+
const resume = this.takeResume();
|
|
511
|
+
if (!media || !resume) return;
|
|
512
|
+
if (resume.position > 0) media.currentTime = resume.position;
|
|
513
|
+
if (resume.playing) media.play()?.catch(() => {});
|
|
514
|
+
}
|
|
515
|
+
/** Where to resume a recovered player, once: React Native hands it to your player. */
|
|
516
|
+
takeResume() {
|
|
517
|
+
const resume = this.#resume;
|
|
518
|
+
this.#resume = void 0;
|
|
519
|
+
return resume;
|
|
520
|
+
}
|
|
521
|
+
#key() {
|
|
522
|
+
return `${this.#src ?? ""}\n${this.#claimsKey ?? ""}`;
|
|
523
|
+
}
|
|
524
|
+
#readInfo() {
|
|
525
|
+
return getAssetInfo(baseOf(this.#src), { viewerClaims: effectiveClaims(this.#src, this.#options.viewerClaims) ?? {} });
|
|
526
|
+
}
|
|
527
|
+
/** The info couldn't be read: an expired signed URL is refreshed, anything else is unavailable. */
|
|
528
|
+
#refused(error) {
|
|
529
|
+
const code = codeOf(error);
|
|
530
|
+
if (code === "signed-url/expired" && this.#options.onExpired && !this.#refreshing) {
|
|
531
|
+
this.refresh().catch(() => {});
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (code === "viewer/age-confirmation-required") this.#override = "age-restricted";
|
|
535
|
+
else this.#unavailable = true;
|
|
536
|
+
this.#onChange();
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
// src/ui/Icons.ts
|
|
540
|
+
/** Path data for the components' 24×24 stroke icons, so every framework draws the same ones. */
|
|
541
|
+
const ICONS = {
|
|
542
|
+
upload: "M12 16V4m0 0L7 9m5-5 5 5M4 16v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-2",
|
|
543
|
+
remove: "M18 6 6 18M6 6l12 12",
|
|
544
|
+
retry: "M20 11a8 8 0 1 0-2.3 5.7M20 4v7h-7",
|
|
545
|
+
unavailable: "M3 3l18 18M10.6 10.6a2 2 0 0 0 2.8 2.8M9.9 5.1A9.8 9.8 0 0 1 12 5c5 0 9 5 10 7a17 17 0 0 1-3.3 4.2M6.6 6.6C4.4 8 2.8 10.4 2 12c1 2 5 7 10 7a9.6 9.6 0 0 0 5.4-1.6"
|
|
546
|
+
};
|
|
547
|
+
/** Attributes shared by every icon's `<svg>`. */
|
|
548
|
+
const ICON_ATTRIBUTES = {
|
|
549
|
+
width: "18",
|
|
550
|
+
height: "18",
|
|
551
|
+
viewBox: "0 0 24 24",
|
|
552
|
+
fill: "none",
|
|
553
|
+
stroke: "currentColor",
|
|
554
|
+
"stroke-width": "2",
|
|
555
|
+
"stroke-linecap": "round",
|
|
556
|
+
"stroke-linejoin": "round",
|
|
557
|
+
"aria-hidden": "true"
|
|
558
|
+
};
|
|
559
|
+
// src/ui/Messages.ts
|
|
560
|
+
/** The size of a row's file-type icon, the same as an image preview. */
|
|
561
|
+
const FILE_ICON_SIZE = 40;
|
|
562
|
+
const DEFAULT_MESSAGES = {
|
|
563
|
+
dropzoneTitle: "Click to upload or drag and drop",
|
|
564
|
+
dropzoneTitleDragging: "Drop to upload",
|
|
565
|
+
chooseFile: "Select file",
|
|
566
|
+
chooseFiles: "Select files",
|
|
567
|
+
chooseAnother: "Choose another",
|
|
568
|
+
maxFileSizeHint: "Up to {size}",
|
|
569
|
+
selected: "",
|
|
570
|
+
queued: "Waiting…",
|
|
571
|
+
uploading: "Uploading…",
|
|
572
|
+
checking: "Checking…",
|
|
573
|
+
submitted: "Uploaded.",
|
|
574
|
+
pending: "Uploaded. It will be shown once it has been checked.",
|
|
575
|
+
allowed: "Uploaded.",
|
|
576
|
+
limited: "Uploaded.",
|
|
577
|
+
"age-restricted": "Uploaded. It will only be shown to people who can see sensitive content.",
|
|
578
|
+
blocked: "This file can't be used because it may break our community guidelines.",
|
|
579
|
+
purged: "This file can't be used because it breaks our community guidelines.",
|
|
580
|
+
failed: "We couldn't upload this file. Please try again.",
|
|
581
|
+
"file/not-accepted": "This type of file isn't accepted.",
|
|
582
|
+
"file/too-large": "This file is too large.",
|
|
583
|
+
"upload/too-large": "This file is too large.",
|
|
584
|
+
"upload/unsupported-type": "This type of file isn't accepted.",
|
|
585
|
+
"upload-preset/daily-limit-reached": "You've reached today's upload limit. Try again tomorrow.",
|
|
586
|
+
"upload-token/expired": "Your upload expired. Please try again.",
|
|
587
|
+
"sdk/cancelled": "Cancelled.",
|
|
588
|
+
retry: "Retry",
|
|
589
|
+
remove: "Remove {name}",
|
|
590
|
+
cancel: "Cancel {name}",
|
|
591
|
+
sensitiveTitle: "Sensitive content",
|
|
592
|
+
sensitiveBody: "This may not be suitable for everyone.",
|
|
593
|
+
reveal: "View",
|
|
594
|
+
openDocument: "Open document",
|
|
595
|
+
openWebsite: "Open {host}",
|
|
596
|
+
play: "Play",
|
|
597
|
+
open: "Open",
|
|
598
|
+
unavailable: "Unavailable"
|
|
599
|
+
};
|
|
600
|
+
const resolveMessages = (overrides) => overrides ? {
|
|
601
|
+
...DEFAULT_MESSAGES,
|
|
602
|
+
...overrides
|
|
603
|
+
} : DEFAULT_MESSAGES;
|
|
604
|
+
/** Fills `{name}`-style placeholders. */
|
|
605
|
+
const formatMessage = (message, values) => message.replace(/\{(\w+)\}/g, (match, name) => values[name] ?? match);
|
|
606
|
+
/** The message for a file's current state. */
|
|
607
|
+
const getUploadItemMessage = (item, messages) => {
|
|
608
|
+
if (item.stage === "failed") return item.errorCode && messages[item.errorCode] || messages.failed;
|
|
609
|
+
if (item.stage === "complete") return messages[item.status ?? "submitted"] ?? messages.submitted;
|
|
610
|
+
return messages[item.stage];
|
|
611
|
+
};
|
|
612
|
+
/** How a finished file turned out, for styling: `accepted`, `restricted` or `rejected`. */
|
|
613
|
+
const getUploadItemOutcome = (item) => {
|
|
614
|
+
if (item.stage === "failed") return "rejected";
|
|
615
|
+
if (item.stage !== "complete") return void 0;
|
|
616
|
+
if (item.status === "blocked" || item.status === "purged") return "rejected";
|
|
617
|
+
return item.status === "age-restricted" ? "restricted" : "accepted";
|
|
618
|
+
};
|
|
619
|
+
/**
|
|
620
|
+
* A byte count for people, such as `1.2 MiB`: binary units, labelled as such, the unit of every
|
|
621
|
+
* CalmLens size limit.
|
|
622
|
+
*/
|
|
623
|
+
const formatBytes = (bytes) => {
|
|
624
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
625
|
+
const units = [
|
|
626
|
+
"KiB",
|
|
627
|
+
"MiB",
|
|
628
|
+
"GiB"
|
|
629
|
+
];
|
|
630
|
+
let value = bytes / 1024;
|
|
631
|
+
let unit = 0;
|
|
632
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
633
|
+
value /= 1024;
|
|
634
|
+
unit++;
|
|
635
|
+
}
|
|
636
|
+
return `${value >= 10 ? Math.round(value) : value.toFixed(1)} ${units[unit]}`;
|
|
637
|
+
};
|
|
638
|
+
const describeUploadItem = (item, messages) => {
|
|
639
|
+
const busy = item.stage === "queued" || item.stage === "uploading" || item.stage === "checking";
|
|
640
|
+
return {
|
|
641
|
+
message: getUploadItemMessage(item, messages),
|
|
642
|
+
outcome: getUploadItemOutcome(item),
|
|
643
|
+
busy,
|
|
644
|
+
progressPercent: item.stage === "uploading" && item.progress !== null ? Math.round(item.progress * 100) : void 0,
|
|
645
|
+
canRetry: item.stage === "failed" && !item.errorCode?.startsWith("file/"),
|
|
646
|
+
removeLabel: formatMessage(busy ? messages.cancel : messages.remove, { name: item.file.name }),
|
|
647
|
+
fileIcon: item.previewUrl ? "" : renderFileIconSvg(getFileIcon(item.file), FILE_ICON_SIZE),
|
|
648
|
+
size: formatBytes(item.file.size)
|
|
649
|
+
};
|
|
650
|
+
};
|
|
651
|
+
/** The hint under the drop zone's title, or `undefined` without a size limit. */
|
|
652
|
+
const getDropzoneHint = (messages, maxFileSize) => maxFileSize === void 0 ? void 0 : formatMessage(messages.maxFileSizeHint, { size: formatBytes(maxFileSize) });
|
|
653
|
+
const describeUploader = (snapshot, options, messages) => ({
|
|
654
|
+
showDropzone: Boolean(options.multiple) || snapshot.items.length === 0,
|
|
655
|
+
chooseLabel: options.multiple ? messages.chooseFiles : messages.chooseFile,
|
|
656
|
+
hint: getDropzoneHint(messages, options.maxFileSize)
|
|
657
|
+
});
|
|
658
|
+
// src/ui/Props.ts
|
|
659
|
+
/** The size of the file icon shown for an asset with nothing else to show. */
|
|
660
|
+
const ASSET_FILE_ICON_SIZE = 64;
|
|
661
|
+
// src/ui/Styles.ts
|
|
662
|
+
/**
|
|
663
|
+
* The components' stylesheet, also shipped as `calmlens/styles.css`. The React components
|
|
664
|
+
* render it themselves (React hoists it into the head, on the server too); with Vue, Svelte or
|
|
665
|
+
* the web components, import or link `calmlens/styles.css` once. Nothing is added to the page
|
|
666
|
+
* at runtime. Theme it with the `--calmlens-*` custom properties, or leave it out and style the
|
|
667
|
+
* `calmlens-*` classes yourself.
|
|
668
|
+
*/
|
|
669
|
+
const CALMLENS_STYLES = `
|
|
670
|
+
/* Zero specificity, so a page's own --calmlens-* values win even on the same element. */
|
|
671
|
+
:where(.calmlens-uploader, .calmlens-asset) {
|
|
672
|
+
--calmlens-accent: ${CALMLENS_THEME.accent};
|
|
673
|
+
--calmlens-accent-text: ${CALMLENS_THEME.accentText};
|
|
674
|
+
--calmlens-radius: ${CALMLENS_THEME.radius}px;
|
|
675
|
+
--calmlens-border: color-mix(in srgb, currentColor 18%, transparent);
|
|
676
|
+
--calmlens-surface: color-mix(in srgb, currentColor 4%, transparent);
|
|
677
|
+
--calmlens-background: ${CALMLENS_THEME.background};
|
|
678
|
+
--calmlens-muted: color-mix(in srgb, currentColor 72%, transparent);
|
|
679
|
+
--calmlens-success: ${CALMLENS_THEME.success};
|
|
680
|
+
--calmlens-warning: ${CALMLENS_THEME.warning};
|
|
681
|
+
--calmlens-danger: ${CALMLENS_THEME.danger};
|
|
682
|
+
--calmlens-focus-ring: var(--calmlens-accent);
|
|
683
|
+
--calmlens-focus-width: 2px;
|
|
684
|
+
--calmlens-focus-offset: 2px;
|
|
685
|
+
box-sizing: border-box;
|
|
686
|
+
font: inherit;
|
|
687
|
+
}
|
|
688
|
+
.calmlens-uploader *, .calmlens-asset * { box-sizing: border-box; }
|
|
689
|
+
.calmlens-uploader { display: grid; gap: 12px; }
|
|
690
|
+
.calmlens-uploader > [hidden] { display: none; }
|
|
691
|
+
.calmlens-dropzone {
|
|
692
|
+
display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
|
|
693
|
+
padding: 40px 24px; text-align: center;
|
|
694
|
+
border: 1.5px dashed var(--calmlens-border); border-radius: var(--calmlens-radius);
|
|
695
|
+
background: var(--calmlens-surface); cursor: pointer;
|
|
696
|
+
transition: border-color 200ms ease-in-out, background-color 200ms ease-in-out;
|
|
697
|
+
}
|
|
698
|
+
.calmlens-dropzone:hover, .calmlens-dropzone:focus-within {
|
|
699
|
+
border-color: color-mix(in srgb, var(--calmlens-accent) 30%, transparent);
|
|
700
|
+
background: color-mix(in srgb, var(--calmlens-accent) 4%, transparent);
|
|
701
|
+
}
|
|
702
|
+
.calmlens-dropzone[data-dragging] {
|
|
703
|
+
border-color: var(--calmlens-accent);
|
|
704
|
+
background: color-mix(in srgb, var(--calmlens-accent) 9%, transparent);
|
|
705
|
+
}
|
|
706
|
+
.calmlens-dropzone[data-disabled] {
|
|
707
|
+
cursor: not-allowed; border-color: var(--calmlens-border); background: none; color: var(--calmlens-muted);
|
|
708
|
+
}
|
|
709
|
+
.calmlens-dropzone__icon {
|
|
710
|
+
display: grid; place-items: center; width: 48px; height: 48px; border-radius: 50%;
|
|
711
|
+
color: var(--calmlens-accent); background: color-mix(in srgb, var(--calmlens-accent) 10%, transparent);
|
|
712
|
+
transition: transform 200ms;
|
|
713
|
+
}
|
|
714
|
+
.calmlens-dropzone[data-disabled] .calmlens-dropzone__icon { color: inherit; background: var(--calmlens-surface); }
|
|
715
|
+
.calmlens-dropzone[data-dragging] .calmlens-dropzone__icon { transform: scale(1.1); }
|
|
716
|
+
.calmlens-dropzone__text { display: grid; gap: 4px; }
|
|
717
|
+
.calmlens-dropzone__title { font-weight: 500; margin: 0; }
|
|
718
|
+
.calmlens-dropzone__hint { font-size: 0.875em; margin: 0; color: var(--calmlens-muted); }
|
|
719
|
+
.calmlens-button, .calmlens-icon-button { margin: 0; width: auto; text-transform: none; letter-spacing: normal; }
|
|
720
|
+
.calmlens-button {
|
|
721
|
+
font: inherit; font-weight: 500; font-size: 0.875em; padding: 7px 14px; border-radius: 8px; cursor: pointer;
|
|
722
|
+
border: 1px solid var(--calmlens-accent); background: var(--calmlens-accent); color: var(--calmlens-accent-text);
|
|
723
|
+
white-space: nowrap; transition: background-color 150ms, border-color 150ms;
|
|
724
|
+
}
|
|
725
|
+
.calmlens-button:focus-visible, .calmlens-icon-button:focus-visible {
|
|
726
|
+
outline: var(--calmlens-focus-width) solid var(--calmlens-focus-ring); outline-offset: var(--calmlens-focus-offset);
|
|
727
|
+
}
|
|
728
|
+
.calmlens-button:hover:not(:disabled) { background: var(--calmlens-accent); filter: brightness(1.08); }
|
|
729
|
+
.calmlens-button[data-variant="outline"] { background: none; color: inherit; border-color: var(--calmlens-border); }
|
|
730
|
+
.calmlens-button[data-variant="outline"]:hover:not(:disabled) {
|
|
731
|
+
filter: none; background: color-mix(in srgb, var(--calmlens-accent) 8%, transparent); border-color: var(--calmlens-accent);
|
|
732
|
+
}
|
|
733
|
+
.calmlens-button[data-size="small"] { padding: 4px 10px; }
|
|
734
|
+
.calmlens-button:disabled {
|
|
735
|
+
cursor: not-allowed; border-style: dashed; background: none; color: var(--calmlens-muted); border-color: var(--calmlens-border);
|
|
736
|
+
}
|
|
737
|
+
.calmlens-uploads { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
|
|
738
|
+
.calmlens-upload {
|
|
739
|
+
display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 12px; padding: 12px;
|
|
740
|
+
border: 1px solid var(--calmlens-border); border-radius: var(--calmlens-radius); background: var(--calmlens-background);
|
|
741
|
+
}
|
|
742
|
+
/* A finished file's outcome is a dot in its status colour before the message. */
|
|
743
|
+
.calmlens-upload[data-outcome] .calmlens-upload__message::before {
|
|
744
|
+
content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 8px; border-radius: 50%;
|
|
745
|
+
vertical-align: 0.05em;
|
|
746
|
+
}
|
|
747
|
+
.calmlens-upload[data-outcome="accepted"] .calmlens-upload__message::before { background: var(--calmlens-success); }
|
|
748
|
+
.calmlens-upload[data-outcome="restricted"] .calmlens-upload__message::before { background: var(--calmlens-warning); }
|
|
749
|
+
.calmlens-upload[data-outcome="rejected"] .calmlens-upload__message::before { background: var(--calmlens-danger); }
|
|
750
|
+
.calmlens-upload__thumb {
|
|
751
|
+
width: 40px; height: 40px; border-radius: 8px; object-fit: cover; display: grid; place-items: center;
|
|
752
|
+
background: var(--calmlens-surface); border: 1px solid var(--calmlens-border);
|
|
753
|
+
font-size: 0.6875em; font-weight: 700; text-transform: uppercase; overflow: hidden;
|
|
754
|
+
}
|
|
755
|
+
.calmlens-upload__thumb[data-kind="file"] { background: none; border: none; }
|
|
756
|
+
.calmlens-upload[data-outcome="rejected"] img.calmlens-upload__thumb { filter: blur(6px); }
|
|
757
|
+
.calmlens-upload__body { min-width: 0; display: grid; gap: 4px; }
|
|
758
|
+
.calmlens-upload__name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
759
|
+
.calmlens-upload__size { font-size: 0.875em; color: var(--calmlens-muted); }
|
|
760
|
+
.calmlens-upload__message { font-size: 0.875em; margin: 0; }
|
|
761
|
+
.calmlens-progress {
|
|
762
|
+
height: 4px; border-radius: 2px; overflow: hidden; background: var(--calmlens-border); position: relative;
|
|
763
|
+
}
|
|
764
|
+
.calmlens-progress__bar {
|
|
765
|
+
position: absolute; inset: 0 auto 0 0; background: var(--calmlens-accent); border-radius: inherit;
|
|
766
|
+
transition: width 150ms;
|
|
767
|
+
}
|
|
768
|
+
.calmlens-progress:not([aria-valuenow]) .calmlens-progress__bar {
|
|
769
|
+
width: 35%; animation: calmlens-indeterminate 1.1s ease-in-out infinite;
|
|
770
|
+
}
|
|
771
|
+
@keyframes calmlens-indeterminate { from { left: -35%; } to { left: 100%; } }
|
|
772
|
+
.calmlens-upload__actions { display: flex; gap: 8px; align-items: center; }
|
|
773
|
+
.calmlens-icon-button {
|
|
774
|
+
display: grid; place-items: center; width: 30px; height: 30px; padding: 0; border-radius: 8px; cursor: pointer;
|
|
775
|
+
border: 1px solid transparent; background: none; color: inherit;
|
|
776
|
+
}
|
|
777
|
+
.calmlens-icon-button:hover { background: color-mix(in srgb, var(--calmlens-accent) 8%, transparent); border-color: var(--calmlens-border); }
|
|
778
|
+
@media (prefers-reduced-motion: reduce) {
|
|
779
|
+
.calmlens-progress__bar, .calmlens-dropzone, .calmlens-dropzone__icon, .calmlens-asset__content { transition: none; }
|
|
780
|
+
.calmlens-progress:not([aria-valuenow]) .calmlens-progress__bar { animation: none; left: 0; width: 100%; }
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
.calmlens-asset {
|
|
784
|
+
position: relative; display: block; overflow: hidden; margin: 0; border-radius: var(--calmlens-media-radius, 0);
|
|
785
|
+
background: var(--calmlens-surface);
|
|
786
|
+
}
|
|
787
|
+
.calmlens-asset__placeholder, .calmlens-asset__content {
|
|
788
|
+
position: absolute; inset: 0; width: 100%; height: 100%; object-fit: var(--calmlens-fit, contain);
|
|
789
|
+
object-position: var(--calmlens-position, center);
|
|
790
|
+
}
|
|
791
|
+
.calmlens-asset:not([data-sized]) .calmlens-asset__content { position: relative; height: auto; }
|
|
792
|
+
.calmlens-asset:not([data-sized])[data-kind="age-restricted"], .calmlens-asset:not([data-sized])[data-kind="unavailable"],
|
|
793
|
+
.calmlens-asset:not([data-sized])[data-state="error"] { min-height: 160px; }
|
|
794
|
+
.calmlens-asset__placeholder { transform: scale(1.1); filter: blur(12px); }
|
|
795
|
+
.calmlens-asset__content { opacity: 0; transition: opacity 250ms; }
|
|
796
|
+
.calmlens-asset[data-state="loaded"] .calmlens-asset__content { opacity: 1; }
|
|
797
|
+
.calmlens-asset[data-kind="video"] .calmlens-asset__content, .calmlens-asset[data-kind="audio"] .calmlens-asset__content { opacity: 1; }
|
|
798
|
+
/* Documents, text and spreadsheets fill the box with their viewer; a document shows its first
|
|
799
|
+
page's still and an open link until the viewer loads. */
|
|
800
|
+
.calmlens-asset__viewer { position: absolute; inset: 0; }
|
|
801
|
+
.calmlens-asset:not([data-sized])[data-kind="document"], .calmlens-asset:not([data-sized])[data-kind="text"],
|
|
802
|
+
.calmlens-asset:not([data-sized])[data-kind="spreadsheet"] { height: 480px; }
|
|
803
|
+
.calmlens-asset[data-kind="document"] .calmlens-asset__content { opacity: 1; }
|
|
804
|
+
.calmlens-asset__open { position: absolute; right: 8px; bottom: 8px; text-decoration: none; }
|
|
805
|
+
/* A website's screenshot scrolls beneath the link to its page. */
|
|
806
|
+
.calmlens-asset[data-kind="website"] { display: flex; flex-direction: column; }
|
|
807
|
+
.calmlens-asset__bar { position: relative; z-index: 1; padding: 8px; }
|
|
808
|
+
.calmlens-asset__scroll { position: relative; flex: 1; overflow: auto; }
|
|
809
|
+
.calmlens-asset[data-kind="website"] .calmlens-asset__content { position: relative; height: auto; object-fit: initial; }
|
|
810
|
+
.calmlens-asset__file { position: absolute; inset: 0; display: grid; place-items: center; }
|
|
811
|
+
.calmlens-asset:not([data-sized])[data-kind="file"], .calmlens-asset:not([data-sized])[data-kind="pending"] { min-height: 160px; }
|
|
812
|
+
.calmlens-asset[data-kind="audio"] { background: none; display: grid; gap: 8px; }
|
|
813
|
+
.calmlens-asset[data-kind="audio"] .calmlens-asset__content { position: static; height: auto; }
|
|
814
|
+
/* Audio's waveform: the preview still, with the played part of its peaks in the accent colour. */
|
|
815
|
+
.calmlens-asset__waveform {
|
|
816
|
+
position: relative; height: var(--calmlens-waveform-height, 64px); border-radius: var(--calmlens-media-radius, 0);
|
|
817
|
+
overflow: hidden; background: var(--calmlens-surface);
|
|
818
|
+
}
|
|
819
|
+
.calmlens-asset__wave { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; }
|
|
820
|
+
.calmlens-asset__progress, .calmlens-asset__peaks { position: absolute; inset: 0; pointer-events: none; }
|
|
821
|
+
.calmlens-asset__peaks { display: flex; align-items: center; gap: 1px; }
|
|
822
|
+
.calmlens-asset__peaks > span { flex: 1 1 0; min-width: 1px; border-radius: 1px; background: var(--calmlens-border); }
|
|
823
|
+
.calmlens-asset__wave ~ .calmlens-asset__progress .calmlens-asset__peaks:not([data-played]) { visibility: hidden; }
|
|
824
|
+
.calmlens-asset__peaks[data-played] { clip-path: inset(0 calc(100% - var(--calmlens-played, 0%)) 0 0); }
|
|
825
|
+
.calmlens-asset__peaks[data-played] > span { background: var(--calmlens-accent); }
|
|
826
|
+
.calmlens-asset__notice {
|
|
827
|
+
position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 6px;
|
|
828
|
+
padding: 12px; text-align: center; color: #fff; background: rgba(0, 0, 0, 0.45);
|
|
829
|
+
}
|
|
830
|
+
.calmlens-asset__notice p { margin: 0; display: flex; gap: 6px; align-items: center; }
|
|
831
|
+
/* Thumbnails keep the notice's icon and leave its text to screen readers. */
|
|
832
|
+
.calmlens-asset[data-sized] { container-type: inline-size; }
|
|
833
|
+
@container (max-width: 119px) {
|
|
834
|
+
.calmlens-asset__label, .calmlens-asset__notice > strong, .calmlens-asset__notice > p:not(:has(svg)) {
|
|
835
|
+
position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
.calmlens-asset__notice strong { font-weight: 600; }
|
|
839
|
+
.calmlens-asset__notice .calmlens-button { margin-top: 4px; border-color: #fff; background: #fff; color: #111; }
|
|
840
|
+
.calmlens-asset[data-state="error"] .calmlens-asset__notice, .calmlens-asset[data-kind="unavailable"] .calmlens-asset__notice {
|
|
841
|
+
color: inherit; background: var(--calmlens-surface);
|
|
842
|
+
}
|
|
843
|
+
${VIEWER_STYLES}`;
|
|
844
|
+
/** Identifies the stylesheet, so React adds it once however many components render. */
|
|
845
|
+
const CALMLENS_STYLES_ID = "calmlens-styles";
|
|
846
|
+
export { CALMLENS_THEME as C, isSpreadsheetFormat as E, VIEWER_STYLES as S, getAssetViewerKind as T, getAssetView as _, describeUploadItem as a, getPlayedPercent as b, formatMessage as c, getUploadItemOutcome as d, resolveMessages as f, getAssetAspectRatio as g, AssetSession as h, DEFAULT_MESSAGES as i, getDropzoneHint as l, ICON_ATTRIBUTES as m, CALMLENS_STYLES_ID as n, describeUploader as o, ICONS as p, ASSET_FILE_ICON_SIZE as r, formatBytes as s, CALMLENS_STYLES as t, getUploadItemMessage as u, getAssetViewKey as v, resolveTheme as w, pickAssetSource as x, getPeakHeights as y };
|