emdash-plugin-retouche 0.1.0 → 0.2.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/CHANGELOG.md +35 -10
- package/README.md +96 -85
- package/dist/index.d.mts +8 -8
- package/dist/index.mjs +66 -81
- package/package.json +5 -3
- package/src/admin/BeforeAfter.tsx +7 -5
- package/src/admin/ImageField.tsx +80 -0
- package/src/admin/RetouchePage.tsx +26 -28
- package/src/admin/api.ts +11 -15
- package/src/admin/errors.ts +34 -0
- package/src/admin/i18n/en.ts +35 -0
- package/src/admin/i18n/fr.ts +35 -0
- package/src/admin/i18n/messages.ts +37 -0
- package/src/admin/i18n/useMessages.ts +24 -0
- package/src/admin/usePreview.ts +11 -10
- package/src/shared/api.ts +2 -7
- package/src/shared/errors.ts +8 -0
- package/src/shared/filename.ts +2 -2
- package/src/shared/formats.ts +6 -0
- package/src/shared/presets.ts +1 -11
- package/src/admin/ImagePicker.tsx +0 -122
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isRecord } from "emdash/plugin-utils";
|
|
2
|
+
|
|
3
|
+
import { type ErrorCode, isErrorCode } from "../shared/errors.js";
|
|
4
|
+
|
|
5
|
+
/** Failures the admin reports when the server gives no known code. */
|
|
6
|
+
export type FallbackError = "PREVIEW_FAILED" | "SAVE_FAILED";
|
|
7
|
+
|
|
8
|
+
export type ErrorKey = ErrorCode | FallbackError;
|
|
9
|
+
|
|
10
|
+
/** Failed plugin API call, carrying the `error.code` of the response body. */
|
|
11
|
+
export class ApiError extends Error {
|
|
12
|
+
override readonly name = "ApiError";
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
readonly code: string | null,
|
|
16
|
+
message: string,
|
|
17
|
+
) {
|
|
18
|
+
super(message);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function apiError(response: Response): Promise<ApiError> {
|
|
23
|
+
const body: unknown = await response.json().catch(() => null);
|
|
24
|
+
const error = isRecord(body) && isRecord(body.error) ? body.error : {};
|
|
25
|
+
return new ApiError(
|
|
26
|
+
typeof error.code === "string" ? error.code : null,
|
|
27
|
+
typeof error.message === "string" ? error.message : `HTTP ${String(response.status)}`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Keeps a code the plugin routes send; anything else, network failures included, maps to `fallback`. */
|
|
32
|
+
export function errorKey(cause: unknown, fallback: FallbackError): ErrorKey {
|
|
33
|
+
return cause instanceof ApiError && isErrorCode(cause.code) ? cause.code : fallback;
|
|
34
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Messages } from "./messages.js";
|
|
2
|
+
|
|
3
|
+
export const en: Messages = {
|
|
4
|
+
intro:
|
|
5
|
+
"Pick a photo of a drawing, then a preset. The retouched image is added to the media library as a new image: the original is never modified.",
|
|
6
|
+
imageStep: "1. Image",
|
|
7
|
+
settingsStep: "2. Settings",
|
|
8
|
+
previewStep: "3. Preview",
|
|
9
|
+
chooseImage: "Choose an image",
|
|
10
|
+
changeImage: "Change",
|
|
11
|
+
pickerTitle: "Choose a photo of a drawing",
|
|
12
|
+
pickerConfirm: "Retouch this image",
|
|
13
|
+
presetLegend: "Preset",
|
|
14
|
+
presets: {
|
|
15
|
+
pencil: { label: "Pencil drawing", description: "Grayscale, whitened paper, stronger contrast." },
|
|
16
|
+
color: { label: "Color drawing", description: "Same lighting correction, colors kept, neutral paper." },
|
|
17
|
+
},
|
|
18
|
+
intensity: "Intensity",
|
|
19
|
+
before: "Before",
|
|
20
|
+
after: "After",
|
|
21
|
+
originalAlt: (filename) => `Original: ${filename}`,
|
|
22
|
+
previewAlt: "Retouch preview",
|
|
23
|
+
save: "Save a retouched copy",
|
|
24
|
+
savedTitle: "Retouched image added to the media library",
|
|
25
|
+
savedDescription: (filename) => `${filename} is ready to use in your content; the original is unchanged.`,
|
|
26
|
+
saveFailedTitle: "Save failed",
|
|
27
|
+
errors: {
|
|
28
|
+
IMAGE_NOT_FOUND: "This image is no longer in the media library.",
|
|
29
|
+
UNSUPPORTED_FORMAT: "This file format is not supported.",
|
|
30
|
+
UNDECODABLE_IMAGE: "This image could not be decoded.",
|
|
31
|
+
MEDIA_UNAVAILABLE: "The plugin cannot access the media library.",
|
|
32
|
+
PREVIEW_FAILED: "The preview could not be computed.",
|
|
33
|
+
SAVE_FAILED: "The retouched image could not be saved.",
|
|
34
|
+
},
|
|
35
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Messages } from "./messages.js";
|
|
2
|
+
|
|
3
|
+
export const fr: Messages = {
|
|
4
|
+
intro:
|
|
5
|
+
"Choisissez la photo d'un dessin, puis un préréglage. L'image retouchée est ajoutée à la médiathèque comme une nouvelle image : l'original n'est jamais modifié.",
|
|
6
|
+
imageStep: "1. Image",
|
|
7
|
+
settingsStep: "2. Réglages",
|
|
8
|
+
previewStep: "3. Aperçu",
|
|
9
|
+
chooseImage: "Choisir une image",
|
|
10
|
+
changeImage: "Changer",
|
|
11
|
+
pickerTitle: "Choisir la photo d'un dessin",
|
|
12
|
+
pickerConfirm: "Retoucher cette image",
|
|
13
|
+
presetLegend: "Préréglage",
|
|
14
|
+
presets: {
|
|
15
|
+
pencil: { label: "Dessin au crayon", description: "Niveaux de gris, papier blanchi, contraste renforcé." },
|
|
16
|
+
color: { label: "Dessin en couleur", description: "Même correction d'éclairage, couleurs conservées, papier neutre." },
|
|
17
|
+
},
|
|
18
|
+
intensity: "Intensité",
|
|
19
|
+
before: "Avant",
|
|
20
|
+
after: "Après",
|
|
21
|
+
originalAlt: (filename) => `Original : ${filename}`,
|
|
22
|
+
previewAlt: "Aperçu de la retouche",
|
|
23
|
+
save: "Enregistrer une copie retouchée",
|
|
24
|
+
savedTitle: "Image retouchée ajoutée à la médiathèque",
|
|
25
|
+
savedDescription: (filename) => `${filename} est disponible pour vos contenus ; l'original reste inchangé.`,
|
|
26
|
+
saveFailedTitle: "Enregistrement impossible",
|
|
27
|
+
errors: {
|
|
28
|
+
IMAGE_NOT_FOUND: "Cette image n'est plus dans la médiathèque.",
|
|
29
|
+
UNSUPPORTED_FORMAT: "Ce format de fichier n'est pas pris en charge.",
|
|
30
|
+
UNDECODABLE_IMAGE: "Cette image n'a pas pu être décodée.",
|
|
31
|
+
MEDIA_UNAVAILABLE: "Le plugin n'a pas accès à la médiathèque.",
|
|
32
|
+
PREVIEW_FAILED: "L'aperçu n'a pas pu être calculé.",
|
|
33
|
+
SAVE_FAILED: "L'image retouchée n'a pas pu être enregistrée.",
|
|
34
|
+
},
|
|
35
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Preset } from "../../shared/presets.js";
|
|
2
|
+
import type { ErrorKey } from "../errors.js";
|
|
3
|
+
import { en } from "./en.js";
|
|
4
|
+
import { fr } from "./fr.js";
|
|
5
|
+
|
|
6
|
+
export interface PresetText {
|
|
7
|
+
readonly label: string;
|
|
8
|
+
readonly description: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Messages {
|
|
12
|
+
readonly intro: string;
|
|
13
|
+
readonly imageStep: string;
|
|
14
|
+
readonly settingsStep: string;
|
|
15
|
+
readonly previewStep: string;
|
|
16
|
+
readonly chooseImage: string;
|
|
17
|
+
readonly changeImage: string;
|
|
18
|
+
readonly pickerTitle: string;
|
|
19
|
+
readonly pickerConfirm: string;
|
|
20
|
+
readonly presetLegend: string;
|
|
21
|
+
readonly presets: Readonly<Record<Preset, PresetText>>;
|
|
22
|
+
readonly intensity: string;
|
|
23
|
+
readonly before: string;
|
|
24
|
+
readonly after: string;
|
|
25
|
+
readonly originalAlt: (filename: string) => string;
|
|
26
|
+
readonly previewAlt: string;
|
|
27
|
+
readonly save: string;
|
|
28
|
+
readonly savedTitle: string;
|
|
29
|
+
readonly savedDescription: (filename: string) => string;
|
|
30
|
+
readonly saveFailedTitle: string;
|
|
31
|
+
readonly errors: Readonly<Record<ErrorKey, string>>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Picks the catalogue for a BCP 47 tag such as `fr` or `fr-CA`; English for any other language. */
|
|
35
|
+
export function messagesFor(lang: string): Messages {
|
|
36
|
+
return lang.toLowerCase().split("-")[0] === "fr" ? fr : en;
|
|
37
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { type Messages, messagesFor } from "./messages.js";
|
|
4
|
+
|
|
5
|
+
/** The EmDash admin writes the chosen locale to `<html lang>`; it exposes no translation API to plugins. */
|
|
6
|
+
function subscribe(onChange: () => void): () => void {
|
|
7
|
+
const observer = new MutationObserver(onChange);
|
|
8
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["lang"] });
|
|
9
|
+
return () => {
|
|
10
|
+
observer.disconnect();
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function adminLang(): string {
|
|
15
|
+
return document.documentElement.lang;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function serverLang(): string {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useMessages(): Messages {
|
|
23
|
+
return messagesFor(React.useSyncExternalStore(subscribe, adminLang, serverLang));
|
|
24
|
+
}
|
package/src/admin/usePreview.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
|
|
3
|
-
import type { PreviewResponse,
|
|
4
|
-
import {
|
|
3
|
+
import type { PreviewResponse, RetouchRequest } from "../shared/api.js";
|
|
4
|
+
import { fetchPreview } from "./api.js";
|
|
5
|
+
import { type ErrorKey, errorKey } from "./errors.js";
|
|
5
6
|
|
|
6
7
|
export interface PreviewState {
|
|
7
|
-
/**
|
|
8
|
+
/** Latest preview received for the selected image, kept while recomputing. */
|
|
8
9
|
readonly preview: PreviewResponse | null;
|
|
9
10
|
readonly loading: boolean;
|
|
10
|
-
readonly error:
|
|
11
|
+
readonly error: ErrorKey | null;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
interface Outcome {
|
|
14
15
|
readonly key: string;
|
|
15
16
|
readonly mediaId: string;
|
|
16
17
|
readonly preview: PreviewResponse | null;
|
|
17
|
-
readonly error:
|
|
18
|
+
readonly error: ErrorKey | null;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
export function requestKey(request:
|
|
21
|
+
export function requestKey(request: RetouchRequest | null): string | null {
|
|
21
22
|
return request === null ? null : `${request.mediaId}|${request.preset}|${String(request.intensity)}`;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
26
|
+
* Recomputes the preview on every settings change. A stale request is
|
|
27
|
+
* aborted: only the latest settings are shown.
|
|
27
28
|
*/
|
|
28
|
-
export function usePreview(request:
|
|
29
|
+
export function usePreview(request: RetouchRequest | null): PreviewState {
|
|
29
30
|
const [outcome, setOutcome] = React.useState<Outcome | null>(null);
|
|
30
31
|
const key = requestKey(request);
|
|
31
32
|
const mediaId = request?.mediaId ?? null;
|
|
@@ -45,7 +46,7 @@ export function usePreview(request: RetoucheRequest | null): PreviewState {
|
|
|
45
46
|
key,
|
|
46
47
|
mediaId,
|
|
47
48
|
preview: current?.mediaId === mediaId ? current.preview : null,
|
|
48
|
-
error:
|
|
49
|
+
error: errorKey(cause, "PREVIEW_FAILED"),
|
|
49
50
|
}));
|
|
50
51
|
}
|
|
51
52
|
},
|
package/src/shared/api.ts
CHANGED
|
@@ -4,10 +4,10 @@ export const PLUGIN_ID = "retouche";
|
|
|
4
4
|
|
|
5
5
|
export const API_BASE = `/_emdash/api/plugins/${PLUGIN_ID}`;
|
|
6
6
|
|
|
7
|
-
export interface
|
|
7
|
+
export interface RetouchRequest {
|
|
8
8
|
readonly mediaId: string;
|
|
9
9
|
readonly preset: Preset;
|
|
10
|
-
/**
|
|
10
|
+
/** Retouch strength, from 0 (original image) to 100. */
|
|
11
11
|
readonly intensity: number;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -19,11 +19,6 @@ export interface ImageSummary {
|
|
|
19
19
|
readonly height: number | null;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export interface ImageListResponse {
|
|
23
|
-
readonly items: readonly ImageSummary[];
|
|
24
|
-
readonly nextCursor: string | null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
22
|
export interface PreviewResponse {
|
|
28
23
|
readonly dataUrl: string;
|
|
29
24
|
readonly width: number;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Codes sent in `error.code` by the plugin routes, so the admin can show them in its own language. */
|
|
2
|
+
export const ERROR_CODES = ["IMAGE_NOT_FOUND", "UNSUPPORTED_FORMAT", "UNDECODABLE_IMAGE", "MEDIA_UNAVAILABLE"] as const;
|
|
3
|
+
|
|
4
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
5
|
+
|
|
6
|
+
export function isErrorCode(value: unknown): value is ErrorCode {
|
|
7
|
+
return ERROR_CODES.some((code) => code === value);
|
|
8
|
+
}
|
package/src/shared/filename.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const EXTENSION = /\.[^./\\]+$/;
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const RETOUCHED_SUFFIX = "-retouched";
|
|
4
4
|
|
|
5
5
|
export function retouchedFilename(original: string): string {
|
|
6
6
|
const base = original.trim().replace(EXTENSION, "");
|
|
7
|
-
return `${base.length > 0 ? base : "image"}${
|
|
7
|
+
return `${base.length > 0 ? base : "image"}${RETOUCHED_SUFFIX}.jpg`;
|
|
8
8
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Media library formats that sharp can decode. */
|
|
2
|
+
export const DECODABLE_TYPES = ["image/jpeg", "image/png", "image/webp", "image/avif", "image/gif"] as const;
|
|
3
|
+
|
|
4
|
+
export function isDecodable(mimeType: string): boolean {
|
|
5
|
+
return DECODABLE_TYPES.some((type) => type === mimeType);
|
|
6
|
+
}
|
package/src/shared/presets.ts
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
export const PRESETS = ["
|
|
1
|
+
export const PRESETS = ["pencil", "color"] as const;
|
|
2
2
|
|
|
3
3
|
export type Preset = (typeof PRESETS)[number];
|
|
4
4
|
|
|
5
|
-
export const PRESET_LABELS: Readonly<Record<Preset, string>> = {
|
|
6
|
-
crayon: "Dessin au crayon",
|
|
7
|
-
couleur: "Dessin en couleur",
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const PRESET_DESCRIPTIONS: Readonly<Record<Preset, string>> = {
|
|
11
|
-
crayon: "Niveaux de gris, papier blanchi, contraste renforcé.",
|
|
12
|
-
couleur: "Même correction d'éclairage, couleurs conservées, papier neutre.",
|
|
13
|
-
};
|
|
14
|
-
|
|
15
5
|
export const DEFAULT_INTENSITY = 100;
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { Button, Loader } from "@cloudflare/kumo";
|
|
2
|
-
import * as React from "react";
|
|
3
|
-
|
|
4
|
-
import type { ImageSummary } from "../shared/api.js";
|
|
5
|
-
import { errorMessage, fetchImages } from "./api.js";
|
|
6
|
-
|
|
7
|
-
interface ImagePickerProps {
|
|
8
|
-
readonly selectedId: string | null;
|
|
9
|
-
readonly onSelect: (image: ImageSummary) => void;
|
|
10
|
-
/** Change de valeur pour recharger la liste, par exemple après un enregistrement. */
|
|
11
|
-
readonly refreshKey: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface Listing {
|
|
15
|
-
readonly items: readonly ImageSummary[];
|
|
16
|
-
readonly nextCursor: string | null;
|
|
17
|
-
readonly loading: boolean;
|
|
18
|
-
readonly error: string | null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const EMPTY: Listing = { items: [], nextCursor: null, loading: true, error: null };
|
|
22
|
-
|
|
23
|
-
const grid: React.CSSProperties = {
|
|
24
|
-
display: "grid",
|
|
25
|
-
gridTemplateColumns: "repeat(auto-fill, minmax(7.5rem, 1fr))",
|
|
26
|
-
gap: "0.75rem",
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function thumbnail(selected: boolean): React.CSSProperties {
|
|
30
|
-
return {
|
|
31
|
-
display: "flex",
|
|
32
|
-
flexDirection: "column",
|
|
33
|
-
gap: "0.25rem",
|
|
34
|
-
padding: "0.25rem",
|
|
35
|
-
borderRadius: "0.5rem",
|
|
36
|
-
border: selected ? "2px solid var(--kumo-button-emphasis-bg, currentColor)" : "2px solid transparent",
|
|
37
|
-
background: "none",
|
|
38
|
-
cursor: "pointer",
|
|
39
|
-
textAlign: "start",
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const image: React.CSSProperties = {
|
|
44
|
-
width: "100%",
|
|
45
|
-
aspectRatio: "1",
|
|
46
|
-
objectFit: "cover",
|
|
47
|
-
borderRadius: "0.375rem",
|
|
48
|
-
background: "rgba(127, 127, 127, 0.15)",
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const caption: React.CSSProperties = { fontSize: "0.75rem", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" };
|
|
52
|
-
|
|
53
|
-
export function ImagePicker({ selectedId, onSelect, refreshKey }: ImagePickerProps) {
|
|
54
|
-
const [listing, setListing] = React.useState<Listing>(EMPTY);
|
|
55
|
-
|
|
56
|
-
const load = React.useCallback(async (cursor: string | null, signal?: AbortSignal) => {
|
|
57
|
-
setListing((current) => ({ ...current, loading: true, error: null }));
|
|
58
|
-
try {
|
|
59
|
-
const page = await fetchImages(cursor);
|
|
60
|
-
if (signal?.aborted === true) return;
|
|
61
|
-
setListing((current) => ({
|
|
62
|
-
items: cursor === null ? page.items : [...current.items, ...page.items],
|
|
63
|
-
nextCursor: page.nextCursor,
|
|
64
|
-
loading: false,
|
|
65
|
-
error: null,
|
|
66
|
-
}));
|
|
67
|
-
} catch (cause) {
|
|
68
|
-
if (signal?.aborted !== true) setListing((current) => ({ ...current, loading: false, error: errorMessage(cause) }));
|
|
69
|
-
}
|
|
70
|
-
}, []);
|
|
71
|
-
|
|
72
|
-
React.useEffect(() => {
|
|
73
|
-
const controller = new AbortController();
|
|
74
|
-
void load(null, controller.signal);
|
|
75
|
-
return () => {
|
|
76
|
-
controller.abort();
|
|
77
|
-
};
|
|
78
|
-
}, [load, refreshKey]);
|
|
79
|
-
|
|
80
|
-
return (
|
|
81
|
-
<section className="space-y-4" aria-label="Images de la médiathèque">
|
|
82
|
-
{listing.error === null ? null : (
|
|
83
|
-
<p role="alert" className="text-kumo-danger">
|
|
84
|
-
{listing.error}
|
|
85
|
-
</p>
|
|
86
|
-
)}
|
|
87
|
-
{!listing.loading && listing.error === null && listing.items.length === 0 ? (
|
|
88
|
-
<p className="text-kumo-subtle">La médiathèque ne contient encore aucune image.</p>
|
|
89
|
-
) : null}
|
|
90
|
-
<div style={grid}>
|
|
91
|
-
{listing.items.map((item) => (
|
|
92
|
-
<button
|
|
93
|
-
key={item.id}
|
|
94
|
-
type="button"
|
|
95
|
-
style={thumbnail(item.id === selectedId)}
|
|
96
|
-
aria-pressed={item.id === selectedId}
|
|
97
|
-
onClick={() => {
|
|
98
|
-
onSelect(item);
|
|
99
|
-
}}
|
|
100
|
-
>
|
|
101
|
-
<img src={item.url} alt="" loading="lazy" style={image} />
|
|
102
|
-
<span style={caption} title={item.filename}>
|
|
103
|
-
{item.filename}
|
|
104
|
-
</span>
|
|
105
|
-
</button>
|
|
106
|
-
))}
|
|
107
|
-
</div>
|
|
108
|
-
{listing.loading ? <Loader /> : null}
|
|
109
|
-
{!listing.loading && listing.nextCursor !== null ? (
|
|
110
|
-
<Button
|
|
111
|
-
type="button"
|
|
112
|
-
variant="secondary"
|
|
113
|
-
onClick={() => {
|
|
114
|
-
void load(listing.nextCursor);
|
|
115
|
-
}}
|
|
116
|
-
>
|
|
117
|
-
Afficher plus d'images
|
|
118
|
-
</Button>
|
|
119
|
-
) : null}
|
|
120
|
-
</section>
|
|
121
|
-
);
|
|
122
|
-
}
|