@alulore/kumiko 0.19.1 → 0.20.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 (136) hide show
  1. package/dist/board/board-api.d.ts +2 -1
  2. package/dist/board/board-api.js +1 -1
  3. package/dist/board/board-motion.js +29 -1
  4. package/dist/board/board-slots.d.ts +14 -7
  5. package/dist/board/board-slots.js +5 -3
  6. package/dist/board/board.d.ts +20 -5
  7. package/dist/board/board.js +28 -5
  8. package/dist/board/keep-capture.d.ts +10 -5
  9. package/dist/board/keep-capture.js +48 -13
  10. package/dist/cooldown/cooldown-button.d.ts +30 -0
  11. package/dist/cooldown/cooldown-button.js +75 -0
  12. package/dist/cooldown/cooldown-labels.d.ts +8 -0
  13. package/dist/cooldown/cooldown-labels.js +4 -0
  14. package/dist/cooldown/cooldown-time.d.ts +8 -0
  15. package/dist/cooldown/cooldown-time.js +20 -0
  16. package/dist/cooldown/cooldown.css +21 -0
  17. package/dist/cooldown/index.d.ts +4 -0
  18. package/dist/cooldown/index.js +4 -0
  19. package/dist/cooldown/use-cooldown.d.ts +30 -0
  20. package/dist/cooldown/use-cooldown.js +63 -0
  21. package/dist/dialog/dialog-content.d.ts +5 -0
  22. package/dist/dialog/dialog-content.js +7 -0
  23. package/dist/empty-state/empty-state-labels.d.ts +14 -0
  24. package/dist/empty-state/empty-state-labels.js +10 -0
  25. package/dist/empty-state/empty-state.css +134 -0
  26. package/dist/empty-state/empty-state.d.ts +34 -0
  27. package/dist/empty-state/empty-state.js +61 -0
  28. package/dist/empty-state/index.d.ts +2 -0
  29. package/dist/empty-state/index.js +2 -0
  30. package/dist/form/choice-fields.d.ts +34 -0
  31. package/dist/form/choice-fields.js +41 -0
  32. package/dist/form/error-line.d.ts +8 -0
  33. package/dist/form/error-line.js +47 -0
  34. package/dist/form/field-props.d.ts +12 -0
  35. package/dist/form/field-props.js +8 -0
  36. package/dist/form/field-shell.d.ts +18 -0
  37. package/dist/form/field-shell.js +51 -0
  38. package/dist/form/focus-invalid.d.ts +5 -0
  39. package/dist/form/focus-invalid.js +17 -0
  40. package/dist/form/form-context.d.ts +2 -0
  41. package/dist/form/form-context.js +3 -0
  42. package/dist/form/form-parts.d.ts +20 -0
  43. package/dist/form/form-parts.js +73 -0
  44. package/dist/form/form.css +128 -0
  45. package/dist/form/index.d.ts +10 -0
  46. package/dist/form/index.js +9 -0
  47. package/dist/form/shown-error.d.ts +12 -0
  48. package/dist/form/shown-error.js +27 -0
  49. package/dist/form/submit-errors.d.ts +36 -0
  50. package/dist/form/submit-errors.js +46 -0
  51. package/dist/form/text-fields.d.ts +18 -0
  52. package/dist/form/text-fields.js +21 -0
  53. package/dist/form/use-app-form.d.ts +1655 -0
  54. package/dist/form/use-app-form.js +94 -0
  55. package/dist/form/use-dialog-link.d.ts +5 -0
  56. package/dist/form/use-dialog-link.js +30 -0
  57. package/dist/form/value-fields.d.ts +16 -0
  58. package/dist/form/value-fields.js +15 -0
  59. package/dist/map/geo.d.ts +20 -0
  60. package/dist/map/geo.js +41 -0
  61. package/dist/map/index.d.ts +5 -0
  62. package/dist/map/index.js +4 -0
  63. package/dist/map/label-overlap.d.ts +13 -0
  64. package/dist/map/label-overlap.js +23 -0
  65. package/dist/map/load-map.d.ts +12 -0
  66. package/dist/map/load-map.js +22 -0
  67. package/dist/map/location-map.d.ts +39 -0
  68. package/dist/map/location-map.js +171 -0
  69. package/dist/map/map-frame.d.ts +18 -0
  70. package/dist/map/map-frame.js +6 -0
  71. package/dist/map/map-pin.d.ts +4 -0
  72. package/dist/map/map-pin.js +38 -0
  73. package/dist/map/map-popup.d.ts +17 -0
  74. package/dist/map/map-popup.js +101 -0
  75. package/dist/map/map-source.d.ts +52 -0
  76. package/dist/map/map-source.js +59 -0
  77. package/dist/map/map.css +311 -0
  78. package/dist/map/marker-pins.d.ts +29 -0
  79. package/dist/map/marker-pins.js +144 -0
  80. package/dist/map/markers-map.d.ts +57 -0
  81. package/dist/map/markers-map.js +154 -0
  82. package/dist/map/use-label-overlap.d.ts +9 -0
  83. package/dist/map/use-label-overlap.js +55 -0
  84. package/dist/map/use-map.d.ts +49 -0
  85. package/dist/map/use-map.js +201 -0
  86. package/dist/media/encoding.d.ts +32 -0
  87. package/dist/media/encoding.js +59 -0
  88. package/dist/media/errors.d.ts +33 -0
  89. package/dist/media/errors.js +26 -0
  90. package/dist/media/image.d.ts +9 -0
  91. package/dist/media/image.js +35 -0
  92. package/dist/media/index.d.ts +10 -0
  93. package/dist/media/index.js +10 -0
  94. package/dist/media/load-mediabunny.d.ts +7 -0
  95. package/dist/media/load-mediabunny.js +13 -0
  96. package/dist/media/media-upload.d.ts +40 -0
  97. package/dist/media/media-upload.js +132 -0
  98. package/dist/media/media.css +101 -0
  99. package/dist/media/probe.d.ts +19 -0
  100. package/dist/media/probe.js +49 -0
  101. package/dist/media/sizes.d.ts +28 -0
  102. package/dist/media/sizes.js +51 -0
  103. package/dist/media/slideshow.d.ts +43 -0
  104. package/dist/media/slideshow.js +174 -0
  105. package/dist/media/timeline.d.ts +59 -0
  106. package/dist/media/timeline.js +105 -0
  107. package/dist/media/transcode.d.ts +35 -0
  108. package/dist/media/transcode.js +132 -0
  109. package/dist/media/upload.d.ts +21 -0
  110. package/dist/media/upload.js +54 -0
  111. package/dist/media/use-media-job.d.ts +79 -0
  112. package/dist/media/use-media-job.js +143 -0
  113. package/dist/server-render.test.js +93 -0
  114. package/dist/smooth-caret/caret-controller.js +16 -3
  115. package/dist/table/table-labels.d.ts +2 -0
  116. package/dist/table/table-labels.js +1 -0
  117. package/dist/table/table-sections.js +10 -2
  118. package/dist/table/table.css +1 -0
  119. package/dist/theme/define-theme.d.ts +38 -0
  120. package/dist/theme/define-theme.js +56 -0
  121. package/dist/theme/index.d.ts +3 -0
  122. package/dist/theme/index.js +3 -0
  123. package/dist/theme/theme-style.d.ts +6 -0
  124. package/dist/theme/theme-style.js +11 -0
  125. package/dist/theme/theme-types.d.ts +1 -0
  126. package/dist/theme/theme-types.js +26 -0
  127. package/dist/theme/tokens-1.generated.d.ts +1 -0
  128. package/dist/theme/tokens-1.generated.js +404 -0
  129. package/dist/theme/tokens-2.generated.d.ts +1 -0
  130. package/dist/theme/tokens-2.generated.js +404 -0
  131. package/dist/theme/tokens-3.generated.d.ts +1 -0
  132. package/dist/theme/tokens-3.generated.js +158 -0
  133. package/dist/theme/tokens.generated.d.ts +2 -0
  134. package/dist/theme/tokens.generated.js +6 -0
  135. package/dist/undo/undo-provider.js +24 -9
  136. package/package.json +78 -1
@@ -0,0 +1,32 @@
1
+ import type { Mediabunny } from "./load-mediabunny";
2
+ import type { Size } from "./sizes";
3
+ /** A video codec Kumiko can write into a WebM. Both are royalty free. */
4
+ export type VideoCodec = "av1" | "vp9";
5
+ /** The codecs tried when an app names none: the smaller file first, the one more browsers encode second. */
6
+ export declare const DEFAULT_CODECS: readonly VideoCodec[];
7
+ /** The quality an app gets without naming one, or an explicit bitrate in bits per second. */
8
+ export declare function qualityFor(mediabunny: Mediabunny, bitrate: number | undefined): InstanceType<Mediabunny["Quality"]>;
9
+ /** What a codec has to be able to encode. */
10
+ export interface EncodeNeeds {
11
+ readonly size: Size;
12
+ readonly bitrate: number | undefined;
13
+ readonly frameRate?: number;
14
+ }
15
+ /** Whether the browser can encode `codec` for these needs. Never throws. */
16
+ export declare function canEncode(mediabunny: Mediabunny, codec: VideoCodec, needs: EncodeNeeds): Promise<boolean>;
17
+ /** Whether this browser has WebCodecs' video encoder at all. */
18
+ export declare function hasVideoEncoder(): boolean;
19
+ interface CodecSearch<T> {
20
+ readonly mediabunny: Mediabunny;
21
+ readonly codecs: readonly VideoCodec[];
22
+ readonly needs: EncodeNeeds;
23
+ readonly signal: AbortSignal | undefined;
24
+ readonly run: (codec: VideoCodec) => Promise<T>;
25
+ }
26
+ /**
27
+ * Runs `run` with each codec in turn and returns the first result. A codec the browser can't encode is
28
+ * skipped, and one whose run throws hands over to the next. Only cancelling (or an unreadable input)
29
+ * stops the search early.
30
+ */
31
+ export declare function withFirstCodec<T>(search: CodecSearch<T>): Promise<T>;
32
+ export {};
@@ -0,0 +1,59 @@
1
+ import { MediaError } from "./errors";
2
+ /** The codecs tried when an app names none: the smaller file first, the one more browsers encode second. */
3
+ export const DEFAULT_CODECS = ["av1", "vp9"];
4
+ /** The quality an app gets without naming one, or an explicit bitrate in bits per second. */
5
+ export function qualityFor(mediabunny, bitrate) {
6
+ return new mediabunny.Quality(bitrate === undefined ? "medium" : { bitrate });
7
+ }
8
+ /** Whether the browser can encode `codec` for these needs. Never throws. */
9
+ export async function canEncode(mediabunny, codec, needs) {
10
+ try {
11
+ return await mediabunny.canEncodeVideo(codec, {
12
+ ...needs.size,
13
+ quality: qualityFor(mediabunny, needs.bitrate),
14
+ ...(needs.frameRate === undefined ? {} : { frameRate: needs.frameRate })
15
+ });
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
21
+ /** Whether this browser has WebCodecs' video encoder at all. */
22
+ export function hasVideoEncoder() {
23
+ return typeof VideoEncoder !== "undefined";
24
+ }
25
+ /** A picture or file that can't be read ends the whole job: another codec would read it just as badly. */
26
+ function endsTheJob(error) {
27
+ return (error instanceof MediaError && (error.code === "unreadable" || error.code === "cancelled"));
28
+ }
29
+ async function tryFrom(search, index, configured, lastError) {
30
+ const { mediabunny, codecs, needs, signal, run } = search;
31
+ const codec = codecs[index];
32
+ if (signal?.aborted) {
33
+ throw new MediaError("cancelled");
34
+ }
35
+ if (codec === undefined) {
36
+ // `unsupported` when no codec could even be set up, `encode-failed` when each one broke.
37
+ throw new MediaError(configured ? "encode-failed" : "unsupported", { cause: lastError });
38
+ }
39
+ if (!(await canEncode(mediabunny, codec, needs))) {
40
+ return tryFrom(search, index + 1, configured, lastError);
41
+ }
42
+ try {
43
+ return await run(codec);
44
+ }
45
+ catch (error) {
46
+ if (signal?.aborted || endsTheJob(error)) {
47
+ throw error;
48
+ }
49
+ return tryFrom(search, index + 1, true, error);
50
+ }
51
+ }
52
+ /**
53
+ * Runs `run` with each codec in turn and returns the first result. A codec the browser can't encode is
54
+ * skipped, and one whose run throws hands over to the next. Only cancelling (or an unreadable input)
55
+ * stops the search early.
56
+ */
57
+ export function withFirstCodec(search) {
58
+ return tryFrom(search, 0, false, undefined);
59
+ }
@@ -0,0 +1,33 @@
1
+ /** Why a media function failed. The code is the whole message: words for people stay in the app. */
2
+ export type MediaErrorCode =
3
+ /** No WebCodecs, or none of the codecs can be encoded here. */
4
+ "unsupported"
5
+ /** Not a media file we can read. */
6
+ | "unreadable"
7
+ /** `transcodeVideo` on a file with no video track. */
8
+ | "no-video"
9
+ /** Encoding failed on every codec. */
10
+ | "encode-failed"
11
+ /** The signal aborted. */
12
+ | "cancelled"
13
+ /** The server answered with a status outside 200 to 299. */
14
+ | "upload-status"
15
+ /** The request failed. */
16
+ | "upload-network"
17
+ /** No answer within `timeoutMs`. */
18
+ | "upload-timeout";
19
+ /** The only error the media functions reject with. `cause` holds whatever the browser or mediabunny threw. */
20
+ export declare class MediaError extends Error {
21
+ readonly code: MediaErrorCode;
22
+ /** The HTTP status, for `upload-status`. */
23
+ readonly status?: number;
24
+ constructor(code: MediaErrorCode, options?: {
25
+ status?: number;
26
+ cause?: unknown;
27
+ });
28
+ }
29
+ /**
30
+ * Turns anything thrown into a `MediaError`. An aborted signal always wins: mediabunny and the browser
31
+ * throw all sorts of things on cancel, and the app only ever wants `cancelled`.
32
+ */
33
+ export declare function toMediaError(error: unknown, fallback: MediaErrorCode, signal?: AbortSignal): MediaError;
@@ -0,0 +1,26 @@
1
+ /** The only error the media functions reject with. `cause` holds whatever the browser or mediabunny threw. */
2
+ export class MediaError extends Error {
3
+ code;
4
+ /** The HTTP status, for `upload-status`. */
5
+ status;
6
+ constructor(code, options = {}) {
7
+ super(code, "cause" in options ? { cause: options.cause } : undefined);
8
+ this.name = "MediaError";
9
+ this.code = code;
10
+ if (options.status !== undefined) {
11
+ this.status = options.status;
12
+ }
13
+ }
14
+ }
15
+ /**
16
+ * Turns anything thrown into a `MediaError`. An aborted signal always wins: mediabunny and the browser
17
+ * throw all sorts of things on cancel, and the app only ever wants `cancelled`.
18
+ */
19
+ export function toMediaError(error, fallback, signal) {
20
+ if (signal?.aborted) {
21
+ return error instanceof MediaError && error.code === "cancelled"
22
+ ? error
23
+ : new MediaError("cancelled", { cause: error });
24
+ }
25
+ return error instanceof MediaError ? error : new MediaError(fallback, { cause: error });
26
+ }
@@ -0,0 +1,9 @@
1
+ import type { Size } from "./sizes";
2
+ /**
3
+ * Decodes a picture, turned by its EXIF orientation, and shrinks it (never enlarges it) to fit `maxSize`
4
+ * when given. Rejects with a `MediaError`: `unreadable` when the browser can't decode it.
5
+ */
6
+ export declare function decodeImage(file: Blob, options?: {
7
+ maxSize?: Size;
8
+ signal?: AbortSignal;
9
+ }): Promise<ImageBitmap>;
@@ -0,0 +1,35 @@
1
+ import { MediaError, toMediaError } from "./errors";
2
+ /**
3
+ * Decodes a picture, turned by its EXIF orientation, and shrinks it (never enlarges it) to fit `maxSize`
4
+ * when given. Rejects with a `MediaError`: `unreadable` when the browser can't decode it.
5
+ */
6
+ export async function decodeImage(file, options = {}) {
7
+ const { maxSize, signal } = options;
8
+ if (signal?.aborted) {
9
+ throw new MediaError("cancelled");
10
+ }
11
+ let bitmap;
12
+ try {
13
+ bitmap = await createImageBitmap(file, { imageOrientation: "from-image" });
14
+ const scale = maxSize
15
+ ? Math.min(1, maxSize.width / bitmap.width, maxSize.height / bitmap.height)
16
+ : 1;
17
+ if (scale < 1) {
18
+ const smaller = await createImageBitmap(bitmap, {
19
+ resizeWidth: Math.max(1, Math.round(bitmap.width * scale)),
20
+ resizeHeight: Math.max(1, Math.round(bitmap.height * scale)),
21
+ resizeQuality: "high"
22
+ });
23
+ bitmap.close();
24
+ bitmap = smaller;
25
+ }
26
+ if (signal?.aborted) {
27
+ throw new MediaError("cancelled");
28
+ }
29
+ return bitmap;
30
+ }
31
+ catch (error) {
32
+ bitmap?.close();
33
+ throw toMediaError(error, "unreadable", signal);
34
+ }
35
+ }
@@ -0,0 +1,10 @@
1
+ export { MediaError, type MediaErrorCode } from "./errors";
2
+ export { readMediaInfo, type MediaInfo } from "./probe";
3
+ export { fitWithin, orientationOf, placeImage, type ImageFit, type Orientation, type Rect, type Size } from "./sizes";
4
+ export { buildTimeline, slideshowIssues, type SlideshowIssue, type SlideshowLimits, type Timeline, type TimelineFrame, type TimelineOptions } from "./timeline";
5
+ export { decodeImage } from "./image";
6
+ export { imagesToVideo, type Slide, type SlideBackground, type SlideshowOptions } from "./slideshow";
7
+ export { canTranscode, transcodeVideo, type TranscodeOptions, type TranscodeResult, type VideoCodec } from "./transcode";
8
+ export { uploadToSignedUrl, type UploadOptions, type UploadResult } from "./upload";
9
+ export { useMediaJob, type MediaJob, type MediaJobContext, type MediaJobFailure, type MediaJobState, type MediaJobStep, type MediaJobSteps } from "./use-media-job";
10
+ export { MediaUpload, mediaUploadLabels, type DescribeFailure, type MediaUploadLabels, type MediaUploadProps } from "./media-upload";
@@ -0,0 +1,10 @@
1
+ export { MediaError } from "./errors";
2
+ export { readMediaInfo } from "./probe";
3
+ export { fitWithin, orientationOf, placeImage } from "./sizes";
4
+ export { buildTimeline, slideshowIssues } from "./timeline";
5
+ export { decodeImage } from "./image";
6
+ export { imagesToVideo } from "./slideshow";
7
+ export { canTranscode, transcodeVideo } from "./transcode";
8
+ export { uploadToSignedUrl } from "./upload";
9
+ export { useMediaJob } from "./use-media-job";
10
+ export { MediaUpload, mediaUploadLabels } from "./media-upload";
@@ -0,0 +1,7 @@
1
+ import type * as MediabunnyModule from "mediabunny";
2
+ export type Mediabunny = typeof MediabunnyModule;
3
+ /**
4
+ * Loads mediabunny once, in the browser only, so an app that never touches media never downloads it.
5
+ * A failed load is forgotten, so the next call tries again, and rejects with `unsupported`.
6
+ */
7
+ export declare function loadMediabunny(): Promise<Mediabunny>;
@@ -0,0 +1,13 @@
1
+ import { MediaError } from "./errors";
2
+ let loading;
3
+ /**
4
+ * Loads mediabunny once, in the browser only, so an app that never touches media never downloads it.
5
+ * A failed load is forgotten, so the next call tries again, and rejects with `unsupported`.
6
+ */
7
+ export function loadMediabunny() {
8
+ loading ??= import("mediabunny").catch((error) => {
9
+ loading = undefined;
10
+ throw new MediaError("unsupported", { cause: error });
11
+ });
12
+ return loading;
13
+ }
@@ -0,0 +1,40 @@
1
+ import { type FileDropzoneProps } from "../file-input/file-dropzone";
2
+ import type { MediaJob, MediaJobFailure } from "./use-media-job";
3
+ /** Every word the card shows or says. Each has an English default. */
4
+ export interface MediaUploadLabels {
5
+ /** @defaultValue "Reading" */
6
+ reading: string;
7
+ /** @defaultValue "Converting" */
8
+ converting: string;
9
+ /** @defaultValue "Uploading" */
10
+ uploading: string;
11
+ /** @defaultValue "Uploaded" */
12
+ done: string;
13
+ /** @defaultValue "Cancelled" */
14
+ cancelled: string;
15
+ /** @defaultValue "Cancel" */
16
+ cancel: string;
17
+ /** @defaultValue "Try again" */
18
+ retry: string;
19
+ /** The button that goes back to choosing a file. @defaultValue "Choose another" */
20
+ another: string;
21
+ /** The button that replaces an uploaded file. @defaultValue "Replace" */
22
+ replace: string;
23
+ /** What a failure says, by its code. `rejected` shows the app's own reason. */
24
+ failures: Partial<Record<MediaJobFailure["code"], string>>;
25
+ }
26
+ export declare const mediaUploadLabels: MediaUploadLabels;
27
+ /** Words for a failure, when a label per code isn't enough. */
28
+ export type DescribeFailure = (failure: MediaJobFailure) => React.ReactNode;
29
+ export type MediaUploadProps<Result> = Omit<FileDropzoneProps, "multiple" | "value" | "defaultValue" | "onValueChange" | "showList" | "status" | "labels"> & {
30
+ /** The job from `useMediaJob`, which does the work. */
31
+ job: MediaJob<Result>;
32
+ labels?: Partial<MediaUploadLabels> & FileDropzoneProps["labels"];
33
+ /** Words for a failure, when a label per code isn't enough. Beats `labels.failures`. */
34
+ describeFailure?: DescribeFailure;
35
+ };
36
+ /**
37
+ * One file through a media job: a drop zone to choose it, then where the job stands (reading, converting,
38
+ * uploading) with Cancel, and at the end Uploaded with Replace, or the failure with Try again and Choose another.
39
+ */
40
+ export declare function MediaUpload<Result>({ job, labels, describeFailure, locale, ...zone }: MediaUploadProps<Result>): React.JSX.Element;
@@ -0,0 +1,132 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { formatFileSize } from "@ui-sdk/react/file-input";
3
+ import { useEffect, useRef } from "react";
4
+ import { Button } from "../button/button";
5
+ import { FileDropzone } from "../file-input/file-dropzone";
6
+ import { Progress } from "../progress/progress";
7
+ export const mediaUploadLabels = {
8
+ reading: "Reading",
9
+ converting: "Converting",
10
+ uploading: "Uploading",
11
+ done: "Uploaded",
12
+ cancelled: "Cancelled",
13
+ cancel: "Cancel",
14
+ retry: "Try again",
15
+ another: "Choose another",
16
+ replace: "Replace",
17
+ failures: {
18
+ unsupported: "This browser can't convert video. Try another browser or a computer.",
19
+ unreadable: "This file can't be read.",
20
+ "no-video": "This file has no video.",
21
+ "encode-failed": "The video couldn't be converted.",
22
+ "upload-status": "The server didn't accept the file.",
23
+ "upload-network": "The upload was interrupted. Check your connection.",
24
+ "upload-timeout": "The upload took too long.",
25
+ unknown: "Something went wrong."
26
+ }
27
+ };
28
+ /** Failures that another try on the same file won't fix, so only "Choose another" is offered. */
29
+ const final = new Set([
30
+ "rejected",
31
+ "unreadable",
32
+ "no-video",
33
+ "unsupported"
34
+ ]);
35
+ function failureText(failure, words, describe) {
36
+ const custom = describe?.(failure);
37
+ if (custom !== undefined && custom !== null) {
38
+ return custom;
39
+ }
40
+ if (failure.code === "rejected") {
41
+ return failure.reason;
42
+ }
43
+ return words.failures[failure.code] ?? words.failures.unknown;
44
+ }
45
+ function BusyLine({ state, words }) {
46
+ const progress = state.phase === "reading" ? null : state.progress;
47
+ return (_jsxs(_Fragment, { children: [_jsx(Progress, { size: "sm", value: progress, max: 1, "aria-label": `${words[state.phase]} ${state.file.name}` }), _jsxs("span", { "data-slot": "media-upload-status", "data-phase": state.phase, children: [words[state.phase], progress === null ? null : ` ${Math.round(progress * 100)}%`] })] }));
48
+ }
49
+ function StatusLine({ state, words, describe }) {
50
+ if (state.phase === "done" || state.phase === "cancelled") {
51
+ return (_jsx("span", { "data-slot": "media-upload-status", "data-phase": state.phase, children: words[state.phase] }));
52
+ }
53
+ if (state.phase === "failed") {
54
+ return (_jsx("span", { "data-slot": "media-upload-status", "data-phase": "failed", role: "alert", children: failureText(state.failure, words, describe) }));
55
+ }
56
+ return _jsx(BusyLine, { state: state, words: words });
57
+ }
58
+ function Actions({ job, state, words }) {
59
+ if (state.phase === "done") {
60
+ return (_jsx(Button, { size: "sm", variant: "secondary", onClick: job.reset, children: words.replace }));
61
+ }
62
+ if (state.phase === "failed" || state.phase === "cancelled") {
63
+ const again = state.phase === "cancelled" || !final.has(state.failure.code);
64
+ return (_jsxs(_Fragment, { children: [again ? (_jsx(Button, { size: "sm", onClick: job.retry, children: words.retry })) : null, _jsx(Button, { size: "sm", variant: "secondary", onClick: job.reset, children: words.another })] }));
65
+ }
66
+ return (_jsx(Button, { size: "sm", variant: "secondary", onClick: job.cancel, children: words.cancel }));
67
+ }
68
+ function Panel(props) {
69
+ const { state, locale } = props;
70
+ return (_jsxs("div", { "data-slot": "media-upload", "data-phase": state.phase, children: [_jsxs("div", { "data-slot": "media-upload-file", children: [_jsx("span", { "data-slot": "media-upload-name", children: state.file.name }), _jsx("span", { "data-slot": "media-upload-size", children: formatFileSize(state.file.size, locale) })] }), _jsx("div", { "data-slot": "media-upload-line", "aria-live": "polite", children: _jsx(StatusLine, { ...props }) }), _jsx("div", { "data-slot": "media-upload-actions", children: _jsx(Actions, { ...props }) })] }));
71
+ }
72
+ /**
73
+ * The card swaps its content as the job moves on, which takes away the button that had focus. When focus was in
74
+ * the card, or fell to the page with that button, it goes to the first button the card shows now.
75
+ */
76
+ function useKeptFocus(phase) {
77
+ const card = useRef(null);
78
+ const had = useRef(false);
79
+ const shown = useRef(phase);
80
+ useEffect(() => {
81
+ const box = card.current;
82
+ if (!box) {
83
+ return undefined;
84
+ }
85
+ const into = () => {
86
+ had.current = true;
87
+ };
88
+ const away = (event) => {
89
+ /* A button that is removed while focused takes focus to the page with no new target: that still counts. */
90
+ if (event.relatedTarget instanceof Node && !box.contains(event.relatedTarget)) {
91
+ had.current = false;
92
+ }
93
+ };
94
+ box.addEventListener("focusin", into);
95
+ box.addEventListener("focusout", away);
96
+ return () => {
97
+ box.removeEventListener("focusin", into);
98
+ box.removeEventListener("focusout", away);
99
+ };
100
+ }, []);
101
+ useEffect(() => {
102
+ const box = card.current;
103
+ if (!box || shown.current === phase) {
104
+ return;
105
+ }
106
+ shown.current = phase;
107
+ const lost = document.activeElement === document.body || box.contains(document.activeElement);
108
+ if (had.current && lost) {
109
+ box.querySelector("button:not([disabled])")?.focus();
110
+ }
111
+ }, [phase]);
112
+ return card;
113
+ }
114
+ /**
115
+ * One file through a media job: a drop zone to choose it, then where the job stands (reading, converting,
116
+ * uploading) with Cancel, and at the end Uploaded with Replace, or the failure with Try again and Choose another.
117
+ */
118
+ export function MediaUpload({ job, labels, describeFailure, locale, ...zone }) {
119
+ const words = {
120
+ ...mediaUploadLabels,
121
+ ...labels,
122
+ failures: { ...mediaUploadLabels.failures, ...labels?.failures }
123
+ };
124
+ const { state } = job;
125
+ const card = useKeptFocus(state.phase);
126
+ return (_jsx("div", { ref: card, "data-slot": "media-upload-card", children: state.phase === "idle" ? (_jsx(FileDropzone, { ...zone, locale: locale, labels: labels, showList: false, value: [], onValueChange: (files) => {
127
+ const [file] = files;
128
+ if (file) {
129
+ job.start(file);
130
+ }
131
+ } })) : (_jsx(Panel, { job: job, state: state, words: words, describe: describeFailure, locale: locale })) }));
132
+ }
@@ -0,0 +1,101 @@
1
+ @import "../motion/motion.css";
2
+ @import "../file-input/file-input.css";
3
+ @import "../progress/progress.css";
4
+ @import "../button/button.css";
5
+
6
+ /*
7
+ * The media upload card: the file input's drop zone while idle, then one panel with the file, where the job
8
+ * stands and its buttons. The panel borrows the zone's edge, radius and fill, so it reads as the same box.
9
+ */
10
+ @layer kumiko {
11
+ :where(:root) {
12
+ --kumiko-media-upload-radius: var(--kumiko-file-input-radius);
13
+ --kumiko-media-upload-border: var(--kumiko-file-input-border);
14
+ --kumiko-media-upload-border-width: var(--kumiko-file-input-border-width);
15
+ --kumiko-media-upload-bg: var(--kumiko-file-input-bg);
16
+ --kumiko-media-upload-padding: 1rem 1.125rem;
17
+ --kumiko-media-upload-gap: 0.625rem;
18
+ --kumiko-media-upload-meta-fg: var(--kumiko-file-input-hint-fg);
19
+ --kumiko-media-upload-done: var(--kumiko-file-input-done);
20
+ --kumiko-media-upload-failed: light-dark(oklch(0.52 0.19 27), oklch(0.74 0.15 27));
21
+ }
22
+
23
+ :where([data-slot="media-upload"]) {
24
+ display: grid;
25
+ box-sizing: border-box;
26
+ gap: var(--kumiko-media-upload-gap);
27
+ padding: var(--kumiko-media-upload-padding);
28
+ border: var(--kumiko-media-upload-border-width) solid var(--kumiko-media-upload-border);
29
+ border-radius: var(--kumiko-media-upload-radius);
30
+ background: var(--kumiko-media-upload-bg);
31
+ grid-template:
32
+ "file actions" auto
33
+ "line line" auto / minmax(0, 1fr) auto;
34
+ align-items: center;
35
+ column-gap: 1rem;
36
+ }
37
+
38
+ :where([data-slot="media-upload-file"]) {
39
+ display: grid;
40
+ grid-area: file;
41
+ min-inline-size: 0;
42
+ }
43
+
44
+ :where([data-slot="media-upload-name"]) {
45
+ overflow: hidden;
46
+ font-weight: 550;
47
+ text-overflow: ellipsis;
48
+ white-space: nowrap;
49
+ }
50
+
51
+ :where([data-slot="media-upload-size"]) {
52
+ color: var(--kumiko-media-upload-meta-fg);
53
+ font-size: 0.875em;
54
+ font-variant-numeric: tabular-nums;
55
+ }
56
+
57
+ :where([data-slot="media-upload-line"]) {
58
+ display: grid;
59
+ grid-area: line;
60
+ gap: 0.375rem;
61
+ }
62
+
63
+ :where([data-slot="media-upload-status"]) {
64
+ color: var(--kumiko-media-upload-meta-fg);
65
+ font-size: 0.875em;
66
+ font-variant-numeric: tabular-nums;
67
+ animation: kumiko-media-upload-in var(--kumiko-fade-duration) var(--kumiko-fade-ease) both;
68
+ }
69
+
70
+ :where([data-slot="media-upload-status"][data-phase="done"]) {
71
+ color: var(--kumiko-media-upload-done);
72
+ }
73
+
74
+ :where([data-slot="media-upload-status"][data-phase="failed"]) {
75
+ color: var(--kumiko-media-upload-failed);
76
+ }
77
+
78
+ :where([data-slot="media-upload-actions"]) {
79
+ display: flex;
80
+ flex-wrap: wrap;
81
+ grid-area: actions;
82
+ gap: 0.5rem;
83
+ justify-content: end;
84
+ }
85
+
86
+ @keyframes kumiko-media-upload-in {
87
+ from {
88
+ opacity: 0;
89
+ }
90
+ }
91
+
92
+ @media (forced-colors: active) {
93
+ :where([data-slot="media-upload"]) {
94
+ border-color: CanvasText;
95
+ }
96
+
97
+ :where([data-slot="media-upload-status"][data-phase]) {
98
+ color: CanvasText;
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,19 @@
1
+ import { type Orientation } from "./sizes";
2
+ export interface MediaInfo {
3
+ /** Not rounded. */
4
+ readonly durationSeconds: number;
5
+ /** The display size of the primary video track, after rotation metadata; 0 with no video. */
6
+ readonly width: number;
7
+ readonly height: number;
8
+ /** Null with no video. */
9
+ readonly orientation: Orientation | null;
10
+ readonly hasVideo: boolean;
11
+ readonly hasAudio: boolean;
12
+ }
13
+ /**
14
+ * Reads the length, size and tracks of a media file without decoding it. It reads the content, never
15
+ * `file.type`. Rejects with a `MediaError`: `unreadable` when the file is not media we can read.
16
+ */
17
+ export declare function readMediaInfo(file: Blob, options?: {
18
+ signal?: AbortSignal;
19
+ }): Promise<MediaInfo>;
@@ -0,0 +1,49 @@
1
+ import { MediaError, toMediaError } from "./errors";
2
+ import { loadMediabunny } from "./load-mediabunny";
3
+ import { orientationOf } from "./sizes";
4
+ /**
5
+ * Reads the length, size and tracks of a media file without decoding it. It reads the content, never
6
+ * `file.type`. Rejects with a `MediaError`: `unreadable` when the file is not media we can read.
7
+ */
8
+ export async function readMediaInfo(file, options = {}) {
9
+ const { signal } = options;
10
+ if (signal?.aborted) {
11
+ throw new MediaError("cancelled");
12
+ }
13
+ try {
14
+ const mediabunny = await loadMediabunny();
15
+ const input = new mediabunny.Input({
16
+ source: new mediabunny.BlobSource(file),
17
+ formats: mediabunny.ALL_FORMATS
18
+ });
19
+ try {
20
+ const [durationSeconds, video, audio] = await Promise.all([
21
+ input.computeDuration(),
22
+ input.getPrimaryVideoTrack(),
23
+ input.getPrimaryAudioTrack()
24
+ ]);
25
+ const size = video
26
+ ? {
27
+ width: await video.getDisplayWidth(),
28
+ height: await video.getDisplayHeight()
29
+ }
30
+ : { width: 0, height: 0 };
31
+ if (signal?.aborted) {
32
+ throw new MediaError("cancelled");
33
+ }
34
+ return {
35
+ durationSeconds,
36
+ ...size,
37
+ orientation: video ? orientationOf(size) : null,
38
+ hasVideo: video !== null,
39
+ hasAudio: audio !== null
40
+ };
41
+ }
42
+ finally {
43
+ input.dispose();
44
+ }
45
+ }
46
+ catch (error) {
47
+ throw toMediaError(error, "unreadable", signal);
48
+ }
49
+ }
@@ -0,0 +1,28 @@
1
+ /** Whether a picture or video is wider than tall. A square counts as landscape. */
2
+ export type Orientation = "landscape" | "portrait";
3
+ export interface Size {
4
+ readonly width: number;
5
+ readonly height: number;
6
+ }
7
+ export interface Rect extends Size {
8
+ readonly x: number;
9
+ readonly y: number;
10
+ }
11
+ /** `fit` shows the whole picture; `fill` covers the frame and crops. */
12
+ export type ImageFit = "fit" | "fill";
13
+ /** The orientation of `size`; a square is landscape. */
14
+ export declare function orientationOf(size: Size): Orientation;
15
+ /** Rounds to the nearest even number, at least 2: encoders need even sides. */
16
+ export declare function evenSide(value: number): number;
17
+ /** `evenSide` on both sides of a size. */
18
+ export declare function evenSize(size: Size): Size;
19
+ /**
20
+ * Scales `size` down (never up) to fit `max` turned to the source's orientation, keeping the aspect ratio,
21
+ * with both sides even and at least 2. A size with no area gives `max` in landscape.
22
+ */
23
+ export declare function fitWithin(size: Size, max: Size): Size;
24
+ /**
25
+ * Where `source` goes in `target`, centred. `fit` shows all of it, letterboxed (and enlarged when it is
26
+ * smaller, so a small logo doesn't stay tiny); `fill` covers the target, so the frame crops the overflow.
27
+ */
28
+ export declare function placeImage(source: Size, target: Size, fit: ImageFit): Rect;