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
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.0 (2026-12-03)
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- The admin page is available in English and French and follows the EmDash admin language, read from
|
|
8
|
+
`<html lang>`. Any language other than French falls back to English.
|
|
9
|
+
- Route errors carry a code (`IMAGE_NOT_FOUND`, `UNSUPPORTED_FORMAT`, `UNDECODABLE_IMAGE`,
|
|
10
|
+
`MEDIA_UNAVAILABLE`) in `error.code`, which the admin translates.
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- The image is chosen in the EmDash media picker, opened from a single line that shows the selected
|
|
15
|
+
image, instead of a thumbnail gallery. The picker searches the library and accepts uploads.
|
|
16
|
+
- New peer dependency `@emdash-cms/admin`, with the same range as `emdash`.
|
|
17
|
+
|
|
18
|
+
### Breaking changes
|
|
19
|
+
|
|
20
|
+
- The `images` route is removed: the media picker lists the images itself.
|
|
21
|
+
- Documentation, code, logs and API error messages are now in English. Only the plugin name,
|
|
22
|
+
**Retouche**, stays in French.
|
|
23
|
+
- Preset identifiers renamed: `crayon` becomes `pencil` and `couleur` becomes `color`, in the
|
|
24
|
+
`preview` and `save` routes and in the `Preset` type.
|
|
25
|
+
- The `retoucher()` export becomes `retouch()`, and `RetoucheOptions` and `RetoucheResult` become
|
|
26
|
+
`RetouchOptions` and `RetouchResult`.
|
|
27
|
+
- Retouched images are saved as `<name>-retouched.jpg` instead of `<name>-retouche.jpg`.
|
|
2
28
|
|
|
3
29
|
## 0.1.0 (2026-12-03)
|
|
4
30
|
|
|
5
|
-
|
|
31
|
+
First release.
|
|
6
32
|
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
`media:upload`.
|
|
33
|
+
- **Retouche** admin page: pick an image from the media library, a "Pencil drawing" or "Color
|
|
34
|
+
drawing" preset, an intensity slider and a before/after preview computed by the server.
|
|
35
|
+
- Saves a new `<name>-retouche.jpg` image at most 2000 px wide, without modifying the original.
|
|
36
|
+
- Retouch with sharp: lighting correction by division by the blurred background, autocontrast,
|
|
37
|
+
mozjpeg JPEG at quality 86, photo metadata removed.
|
|
38
|
+
- Capabilities `media:read`, `media:bytes:read` and `media:write`; private routes guarded by
|
|
39
|
+
`media:read` or `media:upload`.
|
package/README.md
CHANGED
|
@@ -1,63 +1,65 @@
|
|
|
1
1
|
# emdash-plugin-retouche
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
médiathèque : l'original reste intact.
|
|
3
|
+
Native [EmDash](https://emdashcms.com) plugin that retouches phone photos of drawings: evenly white
|
|
4
|
+
paper, stronger contrast, image scaled down to 2000 px wide. The retouch runs from the admin, one
|
|
5
|
+
image at a time, and always creates a new image in the media library: the original stays untouched.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
Supported EmDash versions: 1.6 and later on the 1.x line.
|
|
9
8
|
|
|
10
|
-
##
|
|
9
|
+
## How it works
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
A **Retouche** page is added to the admin (`/_emdash/admin/plugins/retouche`). There you pick:
|
|
13
12
|
|
|
14
|
-
1.
|
|
15
|
-
2.
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
18
|
-
3.
|
|
13
|
+
1. an image, through the EmDash media picker, where you can also search or upload a new photo;
|
|
14
|
+
2. a preset:
|
|
15
|
+
- **Pencil drawing**: grayscale, whitened paper, stronger contrast;
|
|
16
|
+
- **Color drawing**: same lighting correction, colors kept, neutral paper;
|
|
17
|
+
3. an intensity, from 0 % (original image) to 100 % (full retouch).
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
The page follows the language of the EmDash admin: French when the admin is in French, English for
|
|
20
|
+
every other language. The admin exposes no translation API to plugins, so the page reads the locale
|
|
21
|
+
the admin writes to `<html lang>` and switches as soon as it changes.
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
The server computes the before/after preview on every settings change, on a copy 900 px wide. The
|
|
24
|
+
**Save a retouched copy** button applies the same retouch at 2000 px and adds the result to the media
|
|
25
|
+
library as `<original name>-retouched.jpg`.
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
### Algorithm
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
plus.
|
|
30
|
-
2. Préréglage crayon : passage en niveaux de gris.
|
|
31
|
-
3. Correction de l'éclairage : chaque pixel est divisé par une copie très floutée de l'image, avec
|
|
32
|
-
un gain de 235. Le flou vaut 1/15 de la largeur, soit 40 px pour 600 px de large. Il est calculé
|
|
33
|
-
sur une copie de 240 px puis agrandi, ce qui donne le même fond qu'un flou en pleine taille pour
|
|
34
|
-
une fraction du temps. En couleur, la division se fait canal par canal : un papier jauni par une
|
|
35
|
-
lumière chaude redevient neutre.
|
|
36
|
-
4. Contraste automatique, sur le modèle de `ImageOps.autocontrast` : 2 % des pixels les plus sombres
|
|
37
|
-
et 13 % des plus clairs sont écrêtés, puis l'histogramme est étiré. En couleur, la même table
|
|
38
|
-
s'applique aux trois canaux, calculée sur la luminance, pour ne pas décaler les teintes.
|
|
39
|
-
5. Mélange avec l'image de l'étape 2 selon l'intensité choisie.
|
|
40
|
-
6. JPEG qualité 86 avec mozjpeg. Les métadonnées de la photo, dont la position GPS, ne sont pas
|
|
41
|
-
reprises.
|
|
29
|
+
The processing reproduces the retouch first done by hand with Python/PIL:
|
|
42
30
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
1. EXIF orientation applied, transparency flattened onto white, image scaled down to 2000 px wide
|
|
32
|
+
at most.
|
|
33
|
+
2. Pencil preset: conversion to grayscale.
|
|
34
|
+
3. Lighting correction: each pixel is divided by a heavily blurred copy of the image, with a gain of
|
|
35
|
+
235. The blur is 1/15 of the width, which is 40 px for a 600 px wide image. It is computed on a
|
|
36
|
+
240 px copy and then enlarged, which gives the same background as a full-size blur in a fraction
|
|
37
|
+
of the time. In color, the division runs per channel, so paper yellowed by warm light turns
|
|
38
|
+
neutral again.
|
|
39
|
+
4. Autocontrast, modelled on `ImageOps.autocontrast`: the darkest 2 % and the lightest 13 % of the
|
|
40
|
+
pixels are clipped, then the histogram is stretched. In color, the same table applies to all three
|
|
41
|
+
channels, computed on the luma, so hues do not shift.
|
|
42
|
+
5. Blend with the image from step 2 according to the chosen intensity.
|
|
43
|
+
6. JPEG at quality 86 with mozjpeg. The photo metadata, GPS position included, is not carried over.
|
|
46
44
|
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
The sharp `composite` operation in `colour-dodge` mode was tried for step 3: on a test sheet it
|
|
46
|
+
differed from the division by 6 levels on average and 59 at worst, for the same computing time. The
|
|
47
|
+
plugin therefore computes the division directly on the pixels.
|
|
48
|
+
|
|
49
|
+
On a 4032 × 3024 px photo (Apple M4, Node 24), the preview takes 50 to 90 ms and saving takes 170 to
|
|
50
|
+
330 ms depending on the preset.
|
|
49
51
|
|
|
50
52
|
## Installation
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
module
|
|
54
|
+
The site must already use `@astrojs/react` (required by the EmDash admin) and have `sharp` as a
|
|
55
|
+
direct dependency: the plugin uses it without bundling it, to avoid a second copy of this native
|
|
56
|
+
module.
|
|
55
57
|
|
|
56
58
|
```bash
|
|
57
59
|
pnpm add emdash-plugin-retouche sharp
|
|
58
60
|
```
|
|
59
61
|
|
|
60
|
-
|
|
62
|
+
Then add the plugin to `plugins` of the EmDash integration in `astro.config.mjs`:
|
|
61
63
|
|
|
62
64
|
```js
|
|
63
65
|
import react from "@astrojs/react";
|
|
@@ -75,52 +77,57 @@ export default defineConfig({
|
|
|
75
77
|
});
|
|
76
78
|
```
|
|
77
79
|
|
|
78
|
-
|
|
80
|
+
Redeploy the site: a native plugin is only active after a new build.
|
|
81
|
+
|
|
82
|
+
The package declares `@cloudflare/kumo` at the exact version `2.19.0`, the one used by the EmDash
|
|
83
|
+
1.6 admin. If an EmDash release moves to another Kumo version, the plugin has to be updated with it.
|
|
79
84
|
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
The image is chosen with `MediaPickerModal`, exported by `@emdash-cms/admin`, the package of the
|
|
86
|
+
EmDash admin. The plugin declares it as a peer dependency with the same range as `emdash`. The site
|
|
87
|
+
must resolve a single copy of it, the one `emdash` uses: the picker relies on the admin's query
|
|
88
|
+
client and translations, and a second copy would not find them. pnpm installs the peer on its own and
|
|
89
|
+
shares the copy when the versions match. This component is exported but not described in the plugin
|
|
90
|
+
documentation, so an EmDash release can change it without notice.
|
|
82
91
|
|
|
83
|
-
##
|
|
92
|
+
## Access and permissions
|
|
84
93
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
94
|
+
The plugin is native because its admin page is written in React and it runs `sharp` inside the site
|
|
95
|
+
process. It is therefore not sandboxed: it has the same rights as the site. The capabilities below
|
|
96
|
+
limit what `ctx` exposes to it, but do not isolate it from the rest of the process.
|
|
88
97
|
|
|
89
|
-
|
|
|
98
|
+
| Capability | Use |
|
|
90
99
|
| --- | --- |
|
|
91
|
-
| `media:read` |
|
|
92
|
-
| `media:bytes:read` |
|
|
93
|
-
| `media:write` |
|
|
100
|
+
| `media:read` | read the metadata of the image to retouch |
|
|
101
|
+
| `media:bytes:read` | read the file of the image to retouch (16 MiB at most) |
|
|
102
|
+
| `media:write` | add the retouched image to the media library |
|
|
94
103
|
|
|
95
|
-
|
|
104
|
+
The plugin never calls `ctx.media.delete()` and never modifies an existing media item.
|
|
96
105
|
|
|
97
|
-
Routes,
|
|
106
|
+
Routes, all private, under `/_emdash/api/plugins/retouche/`:
|
|
98
107
|
|
|
99
|
-
| Route |
|
|
108
|
+
| Route | Method | Permission | Role |
|
|
100
109
|
| --- | --- | --- | --- |
|
|
101
|
-
| `
|
|
102
|
-
| `
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
## Développement
|
|
110
|
+
| `preview` | POST | `media:read` | 900 px JPEG preview, returned as a data URL |
|
|
111
|
+
| `save` | POST | `media:upload` | 2000 px retouch added to the media library |
|
|
112
|
+
|
|
113
|
+
The plugin has no settings, no storage and no network access.
|
|
114
|
+
|
|
115
|
+
## Known limitations
|
|
116
|
+
|
|
117
|
+
- The original photo must not exceed 16 MiB, the cap of `ctx.media.readBytes()` in EmDash.
|
|
118
|
+
- EmDash 1.6 does not pass the file bytes to the `media:beforeUpload` hook: the retouch cannot run on
|
|
119
|
+
upload and stays a manual action.
|
|
120
|
+
- No "Retouch" button on the image fields of content entries. A field widget replaces the whole field
|
|
121
|
+
editor, and an editor panel that replaced the image would need the `admin.editor-draft:read` and
|
|
122
|
+
`admin.editor-draft:patch` capabilities, outside the scope of this version.
|
|
123
|
+
- The alt text and caption of the original are not copied to the retouched image: that would need
|
|
124
|
+
the `media:metadata:write` capability.
|
|
125
|
+
- Retouching the same image twice creates two separate media items with the same
|
|
126
|
+
`<name>-retouched.jpg` name.
|
|
127
|
+
- The blur extends the image edges: in the darkest corner of a very unevenly lit photo, the paper can
|
|
128
|
+
stay slightly grey, as with the original PIL retouch.
|
|
129
|
+
|
|
130
|
+
## Development
|
|
124
131
|
|
|
125
132
|
```bash
|
|
126
133
|
pnpm install
|
|
@@ -130,20 +137,24 @@ pnpm install
|
|
|
130
137
|
pnpm check
|
|
131
138
|
```
|
|
132
139
|
|
|
133
|
-
`pnpm check`
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
140
|
+
`pnpm check` runs ESLint, `tsc --noEmit`, Vitest and the tsdown build; the GitHub Actions CI runs the
|
|
141
|
+
same steps. Publishing to npm goes through a tag, as described in [RELEASING.md](RELEASING.md). The
|
|
142
|
+
tests generate their own images: a synthetic sheet with a lighting gradient, tinted paper, a stroke
|
|
143
|
+
grid and a red disc. The repository contains no photo of a real drawing.
|
|
137
144
|
|
|
138
|
-
|
|
145
|
+
To try the plugin in a local site without publishing it:
|
|
139
146
|
|
|
140
147
|
```bash
|
|
141
148
|
pnpm add ../emdash-plugin-retouche
|
|
142
149
|
```
|
|
143
150
|
|
|
144
|
-
|
|
145
|
-
|
|
151
|
+
Translations live in `src/admin/i18n/`, one catalogue per language typed by the `Messages` interface.
|
|
152
|
+
Adding a language means adding a catalogue and a case in `messagesFor()`. The routes return an
|
|
153
|
+
English message for logs and API clients, plus an `error.code` that the admin translates.
|
|
154
|
+
|
|
155
|
+
The admin page (`src/admin/`) ships as TypeScript source: the site's Vite build compiles it, with
|
|
156
|
+
the site's own React and Kumo.
|
|
146
157
|
|
|
147
|
-
##
|
|
158
|
+
## License
|
|
148
159
|
|
|
149
160
|
[MIT](LICENSE)
|
package/dist/index.d.mts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { PluginDescriptor, ResolvedPlugin } from "emdash";
|
|
2
|
-
declare const PRESETS: readonly ["
|
|
2
|
+
declare const PRESETS: readonly ["pencil", "color"];
|
|
3
3
|
type Preset = (typeof PRESETS)[number];
|
|
4
|
-
interface
|
|
4
|
+
interface RetouchOptions {
|
|
5
5
|
readonly preset: Preset;
|
|
6
|
-
/**
|
|
6
|
+
/** From 0 (resized original image) to 1 (full retouch). */
|
|
7
7
|
readonly intensity: number;
|
|
8
8
|
readonly maxWidth: number;
|
|
9
9
|
}
|
|
10
|
-
interface
|
|
11
|
-
/**
|
|
10
|
+
interface RetouchResult {
|
|
11
|
+
/** Independent copy: `jpeg.buffer` holds exactly the file. */
|
|
12
12
|
readonly jpeg: Uint8Array<ArrayBuffer>;
|
|
13
13
|
readonly width: number;
|
|
14
14
|
readonly height: number;
|
|
15
15
|
}
|
|
16
|
-
export declare function
|
|
17
|
-
/**
|
|
16
|
+
export declare function retouch(input: Uint8Array, options: RetouchOptions): Promise<RetouchResult>;
|
|
17
|
+
/** Add to `plugins: []` of the `emdash()` integration in `astro.config.mjs`. */
|
|
18
18
|
export declare function retouchePlugin(): PluginDescriptor;
|
|
19
19
|
export declare function createPlugin(): ResolvedPlugin;
|
|
20
|
-
export type { Preset,
|
|
20
|
+
export type { Preset, RetouchOptions, RetouchResult };
|