@backmatter/studio 0.0.25 → 0.0.26

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.
@@ -12,7 +12,8 @@ import { Field } from "../ui/Field";
12
12
  import { Input } from "../ui/Input";
13
13
  import { Text } from "../ui/Text";
14
14
  import { cx, popup, popupClassName } from "../ui/popup";
15
- import type { CatalogMedia } from "./media-grid";
15
+ import { isPdfMedia, type CatalogMedia } from "./media-grid";
16
+ import { PdfTile } from "./PdfTile";
16
17
 
17
18
  const stack = "@media (max-width: 800px)";
18
19
  const hover = "@media (hover: hover)";
@@ -79,7 +80,7 @@ export function MediaLightbox({
79
80
  <div {...stylex.props(styles.stage)} onClick={onClose}>
80
81
  <button
81
82
  type="button"
82
- aria-label="Previous image"
83
+ aria-label="Previous"
83
84
  disabled={index <= 0}
84
85
  {...stylex.props(styles.nav, styles.navPrev)}
85
86
  onClick={(event) => {
@@ -90,16 +91,29 @@ export function MediaLightbox({
90
91
  ‹
91
92
  </button>
92
93
  <div {...stylex.props(styles.frame)} onClick={(event) => event.stopPropagation()}>
93
- <StageImage
94
- key={item.id}
95
- thumb={item.thumbUrl}
96
- full={full}
97
- alt={item.alt?.trim() || item.filename}
98
- />
94
+ {isPdfMedia(item) || !item.thumbUrl ? (
95
+ <a
96
+ href={item.url}
97
+ target="_blank"
98
+ rel="noreferrer"
99
+ aria-label={`Open ${item.filename}`}
100
+ {...stylex.props(styles.pdfLink)}
101
+ >
102
+ <PdfTile name={item.filename} size="lg" xstyle={styles.pdfTile} />
103
+ <span {...stylex.props(styles.pdfOpen)}>Open PDF</span>
104
+ </a>
105
+ ) : (
106
+ <StageImage
107
+ key={item.id}
108
+ thumb={item.thumbUrl}
109
+ full={full}
110
+ alt={item.alt?.trim() || item.filename}
111
+ />
112
+ )}
99
113
  </div>
100
114
  <button
101
115
  type="button"
102
- aria-label="Next image"
116
+ aria-label="Next"
103
117
  disabled={index >= count - 1}
104
118
  {...stylex.props(styles.nav, styles.navNext)}
105
119
  onClick={(event) => {
@@ -250,6 +264,8 @@ function MediaText({
250
264
  canEdit: boolean;
251
265
  onDescribe?: (text: { alt?: string; caption?: string }) => Promise<void>;
252
266
  }) {
267
+ // Alt text describes an image to someone who cannot see it. A PDF carries its own words.
268
+ const withAlt = !isPdfMedia(item);
253
269
  const [alt, setAlt] = useState(item.alt ?? "");
254
270
  const [caption, setCaption] = useState(item.caption ?? "");
255
271
  const [error, setError] = useState<string | null>(null);
@@ -270,10 +286,10 @@ function MediaText({
270
286
  }
271
287
 
272
288
  if (!canEdit) {
273
- if (!item.alt?.trim() && !item.caption?.trim()) return null;
289
+ if (!(withAlt && item.alt?.trim()) && !item.caption?.trim()) return null;
274
290
  return (
275
291
  <dl {...stylex.props(styles.facts)}>
276
- {item.alt?.trim() ? <Fact label="Alt text" value={item.alt} /> : null}
292
+ {withAlt && item.alt?.trim() ? <Fact label="Alt text" value={item.alt} /> : null}
277
293
  {item.caption?.trim() ? <Fact label="Caption" value={item.caption} /> : null}
278
294
  </dl>
279
295
  );
@@ -281,16 +297,18 @@ function MediaText({
281
297
 
282
298
  return (
283
299
  <div {...stylex.props(styles.textFields)}>
284
- <Field label="Alt text">
285
- <Input
286
- value={alt}
287
- maxLength={MAX_MEDIA_TEXT}
288
- onChange={(event) => setAlt(event.target.value)}
289
- onBlur={() => {
290
- void commit("alt", alt);
291
- }}
292
- />
293
- </Field>
300
+ {withAlt ? (
301
+ <Field label="Alt text">
302
+ <Input
303
+ value={alt}
304
+ maxLength={MAX_MEDIA_TEXT}
305
+ onChange={(event) => setAlt(event.target.value)}
306
+ onBlur={() => {
307
+ void commit("alt", alt);
308
+ }}
309
+ />
310
+ </Field>
311
+ ) : null}
294
312
  <Field label="Caption">
295
313
  <Input
296
314
  value={caption}
@@ -369,6 +387,26 @@ const styles = stylex.create({
369
387
  width: "100%",
370
388
  height: "100%",
371
389
  },
390
+ pdfLink: {
391
+ position: "absolute",
392
+ inset: 0,
393
+ display: "flex",
394
+ flexDirection: "column",
395
+ alignItems: "center",
396
+ justifyContent: "center",
397
+ gap: space.lg,
398
+ color: colors.text,
399
+ textDecoration: "none",
400
+ },
401
+ pdfTile: {
402
+ width: "min(320px, 70%)",
403
+ aspectRatio: "3 / 4",
404
+ borderRadius: radius.md,
405
+ },
406
+ pdfOpen: {
407
+ fontSize: type.md,
408
+ textDecoration: { default: "none", ":hover": "underline" },
409
+ },
372
410
  photo: {
373
411
  position: "absolute",
374
412
  inset: 0,
@@ -0,0 +1,50 @@
1
+ // packages/studio/src/pages/PdfTile.tsx
2
+ // Stands in for a thumbnail where the media is a PDF. The CDN serves a PDF
3
+ // whole and draws no preview, so the tile shows its kind and, when there is
4
+ // room, its filename.
5
+ import { IconFilePdf } from "@central-icons-react/round-outlined-radius-2-stroke-1.5";
6
+ import * as stylex from "@stylexjs/stylex";
7
+ import { colors, space, type } from "../styles/tokens.stylex";
8
+
9
+ export function PdfTile({
10
+ name,
11
+ size = "md",
12
+ xstyle,
13
+ }: {
14
+ /** Shown under the icon. Omit for a tile too small to read. */
15
+ name?: string;
16
+ size?: "sm" | "md" | "lg";
17
+ /** The box the tile fills: the same style an image thumbnail would use. */
18
+ xstyle?: stylex.StyleXStyles;
19
+ }) {
20
+ const icon = size === "sm" ? 18 : size === "md" ? 28 : 56;
21
+ return (
22
+ <div data-media-pdf role="img" aria-label={name ? `PDF: ${name}` : "PDF"} {...stylex.props(xstyle, styles.tile)}>
23
+ <IconFilePdf size={icon} ariaHidden />
24
+ {name && size !== "sm" ? <span {...stylex.props(styles.name)}>{name}</span> : null}
25
+ </div>
26
+ );
27
+ }
28
+
29
+ const styles = stylex.create({
30
+ tile: {
31
+ display: "flex",
32
+ flexDirection: "column",
33
+ alignItems: "center",
34
+ justifyContent: "center",
35
+ gap: space.sm,
36
+ padding: space.md,
37
+ boxSizing: "border-box",
38
+ color: colors.textMuted,
39
+ backgroundColor: colors.bgPanel,
40
+ overflow: "hidden",
41
+ },
42
+ name: {
43
+ maxWidth: "100%",
44
+ overflow: "hidden",
45
+ textOverflow: "ellipsis",
46
+ whiteSpace: "nowrap",
47
+ fontSize: type.sm,
48
+ color: colors.text,
49
+ },
50
+ });
@@ -1,11 +1,19 @@
1
1
  // packages/studio/src/pages/media-grid.ts
2
- import { isMediaContentType, isMediaId, MAX_MEDIA_BYTES } from "@backmatter/core";
2
+ import {
3
+ isMediaContentType,
4
+ isMediaId,
5
+ MAX_MEDIA_BYTES,
6
+ MEDIA_KINDS,
7
+ mediaKindOf,
8
+ type MediaKind,
9
+ } from "@backmatter/core";
3
10
 
4
11
  export type CatalogMedia = {
5
12
  id: string;
13
+ kind?: MediaKind;
6
14
  filename: string;
7
15
  url: string;
8
- thumbUrl: string;
16
+ thumbUrl: string | null;
9
17
  width: number | null;
10
18
  height: number | null;
11
19
  alt?: string;
@@ -44,6 +52,8 @@ export type PendingUpload = {
44
52
  key: string;
45
53
  filename: string;
46
54
  previewUrl: string;
55
+ /** A PDF has no preview image; its tile stands in. */
56
+ kind?: MediaKind;
47
57
  status: "uploading" | "error" | "done";
48
58
  progress: number;
49
59
  error?: string;
@@ -63,7 +73,7 @@ export function emptyMediaGrid(): MediaGridState {
63
73
 
64
74
  export function beginUploads(
65
75
  state: MediaGridState,
66
- items: Array<{ key: string; filename: string; previewUrl: string }>,
76
+ items: Array<{ key: string; filename: string; previewUrl: string; kind?: MediaKind }>,
67
77
  ): MediaGridState {
68
78
  return {
69
79
  ...state,
@@ -196,8 +206,33 @@ export function nextMediaIndex(input: {
196
206
  return next < 0 ? index : next;
197
207
  }
198
208
 
199
- export function rejectionReason(file: { type: string; size: number; name: string }): string | null {
209
+ /** Why a dropped or chosen file cannot go into a place that takes these kinds. */
210
+ export function rejectionReason(
211
+ file: { type: string; size: number; name: string },
212
+ accept: readonly MediaKind[] = MEDIA_KINDS,
213
+ ): string | null {
200
214
  if (file.size > MAX_MEDIA_BYTES) return `${file.name} is larger than 20 MB`;
201
- if (file.type && !isMediaContentType(file.type)) return `${file.name} is not a supported image`;
215
+ if (file.type && (!isMediaContentType(file.type) || !accept.includes(mediaKindOf(file.type)))) {
216
+ return `${file.name} is not ${acceptPhrase(accept)}`;
217
+ }
202
218
  return null;
203
219
  }
220
+
221
+ /** `accept` for a file input that takes these kinds. */
222
+ export function fileInputAccept(accept: readonly MediaKind[] = MEDIA_KINDS): string {
223
+ const types: string[] = [];
224
+ if (accept.includes("image")) types.push("image/jpeg", "image/png", "image/webp", "image/gif", "image/avif");
225
+ if (accept.includes("pdf")) types.push("application/pdf");
226
+ return types.join(",");
227
+ }
228
+
229
+ /** "an image", "a PDF", or "an image or PDF". */
230
+ export function acceptPhrase(accept: readonly MediaKind[]): string {
231
+ if (accept.length > 1) return "an image or PDF";
232
+ return accept[0] === "pdf" ? "a PDF" : "a supported image";
233
+ }
234
+
235
+ /** A media item is a PDF when the catalog says so, or its content type does. */
236
+ export function isPdfMedia(item: { kind?: MediaKind; contentType?: string }): boolean {
237
+ return item.kind === "pdf" || item.contentType === "application/pdf";
238
+ }