diff --git a/clients/gallery/contract/SPEC.md b/clients/gallery/contract/SPEC.md index 2c7faf46..f72b696b 100644 --- a/clients/gallery/contract/SPEC.md +++ b/clients/gallery/contract/SPEC.md @@ -358,6 +358,88 @@ An image, for its variants (`v` the first of them): `width` and `height` stay the original's: a thumbnail has the same shape, and the full-size view and its fit rule use them. The card's link is the original. +## A single screenshot + +One screen's image, for a page or a README, from the same manifest: `screenshot(manifest, options)` gives HTML and +`screenshotMarkdown(manifest, options)` gives Markdown (`screenshot_markdown` in Ruby, Python and Rust). It needs no +script or stylesheet. `screenshots/` holds its fixtures: `manifest.json`, then `cases/.json` (options) with the +exact output beside it in `.html` and `.md`, and `errors/.json` (options every renderer refuses with +that manifest) with `.message`. + +| Option | Default | Meaning | +| --- | --- | --- | +| `screen` | (required) | The screen's key, as the gallery's `screens` and `order` name it. | +| `group` | the first group that has the screen | The group's name. | +| `theme` | the screen's first | A value of the `theme` dimension. Left out, a reader whose setting is dark sees the dark shot. | +| `size` | the screen's first that has the theme | A value of the `size` dimension. | +| `alt` | the screen's title | The image's alt text. | +| `link` | `false` | `true` links the image to its full-size original. | +| `thumb` | `false` | `true` shows the manifest's small copy, where it has one. | +| `loading` | `"lazy"` | `"lazy"` or `"eager"`. | +| `dimensions`, `groups` | | The gallery's options of those names, so the screen's shots pair as they do in the gallery. | + +An option set to `null` is the same as leaving it out. + +### Validation + +The manifest is checked as the gallery's (Validation, above), then the options must be an object (else +`screenshot options must be an object`), then `dimensions` (with the gallery's message, +`gallery option dimensions is invalid`), then, in +this order, each failing with `screenshot option is invalid`: `screen` (a non-empty string, else +`screenshot option screen is required`), `group` a string, `theme` and `size` a key of a value of their dimension, +`alt` a string, `link` and `thumb` booleans, `loading` `"lazy"` or `"eager"`. Then: + +- With `group`: the first manifest group of that name (else `the gallery manifest has no group `), which must + have an item whose name's key (A shot's name) is the screen (else `gallery group has no screen `). + Without it: the first group that has one (else `the gallery manifest has no screen `). +- The model (The model, above) of a manifest of that one group (its `name` and `title`) whose items are the group's + items with the screen's key, in order, with the options `dimensions` and `groups`. When every one of those items is + a 2x shot, they're followed by a copy of each: its name with the extension and the last `-2x` removed, its `url`, + its `thumb`, a `width` `W` of `floor((width + 1) / 2)` and a `height` of + `floor((2 * height * W + width) / (2 * width))`. Otherwise the screen is the gallery's, a 2x shot only a 2x copy (an invalid `groups` fails with the gallery's message too, +`gallery option groups is invalid`, as the screenshot passes it to the gallery). So a + screen that has only 2x shots shows them at half their size, and the gallery's errors (a missing combination, an + invalid `groups`) apply to this screen alone. The model's one screen is the screen. + +### The image + +- The choice `C`, for each dimension in order: the `theme` or `size` option when given; else the value of the + dimension in the screen's first variant whose values match `C`'s so far (else in its first variant). The variant + `v` is the one whose values are `C`'s; when there is none, the error is the gallery's + (`gallery screen billing in group Web has no dark desktop screenshot`), with `C`'s value keys in the dimensions' + order. +- The dark variant `d`: when `theme` isn't given and `C`'s theme isn't `dark`, the variant whose values are `C`'s with + the theme `dark`, when there is one and its `url` or `url2x` differs from `v`'s (a name with no theme stands for + both, with the same image). +- The thumbs are used when `thumb` is true, `v` has one, and `d`, when there is one, has one too (so light and dark + readers see the same size). +- A variant's image: with the thumbs used, the thumb's `url`, `width` and `height` and no 2x; else its + `url`, `width`, `height` and `url2x` (none when it's null or the same as `url`, a 2x shot shown at half its size). + `SRCSET` is the `url`, then, with a 2x, `, ` and the 2x's URL followed by ` 2x`. +- `ALT` is the `alt` option, else the screen's title (Screens, above: for one screen, its key's last part), with + each run of spaces, tabs, `\n` and `\r` as one space and a space at either end removed (a blank line would end a Markdown image, or the HTML block + a `` is in). `HREF` + is `v`'s `url` (the original, even with `thumb`). + +HTML, escaped as the gallery's markup (`IMG` the `` of `v`'s image, `A` and `/A` present only when `link` is +true): + +``` +IMG: {ALT} + +without d: []{IMG}[] +with d: [] + + {IMG} + [] +``` + +each ending with `\n`. Markdown has no size, so its image shows at the file's own (a 2x shot at twice the HTML's). +Markdown: with `d`, the HTML (GitHub and most renderers show a `` as it is); +without, `![{ALT}]({url})`, or with `link` `[![{ALT}]({url})]({HREF})`, then `\n`, where in `ALT` every ASCII +punctuation character (`!` to `/`, `:` to `@`, `[` to `` ` ``, `{` to `~`) gets a `\` before it, and in a URL each +`\`, `(`, `)` and `&` does. + ## Behaviour `hydrate(el)` (the JS library) works from the markup alone: diff --git a/clients/gallery/contract/screenshots/cases/alt-lines.html b/clients/gallery/contract/screenshots/cases/alt-lines.html new file mode 100644 index 00000000..1dbcbf9a --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/alt-lines.html @@ -0,0 +1 @@ +Billing, the plans diff --git a/clients/gallery/contract/screenshots/cases/alt-lines.json b/clients/gallery/contract/screenshots/cases/alt-lines.json new file mode 100644 index 00000000..030c4a6c --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/alt-lines.json @@ -0,0 +1 @@ +{"screen": "billing", "alt": " Billing,\r\n\r\n the plans "} diff --git a/clients/gallery/contract/screenshots/cases/alt-lines.md b/clients/gallery/contract/screenshots/cases/alt-lines.md new file mode 100644 index 00000000..487e8914 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/alt-lines.md @@ -0,0 +1 @@ +![Billing\, the plans](https://app.visualci.com/g/acme/web/billing-light.png) diff --git a/clients/gallery/contract/screenshots/cases/default.html b/clients/gallery/contract/screenshots/cases/default.html new file mode 100644 index 00000000..38a66fe5 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/default.html @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/default.json b/clients/gallery/contract/screenshots/cases/default.json new file mode 100644 index 00000000..4ec96dde --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/default.json @@ -0,0 +1 @@ +{"screen": "overview"} diff --git a/clients/gallery/contract/screenshots/cases/default.md b/clients/gallery/contract/screenshots/cases/default.md new file mode 100644 index 00000000..38a66fe5 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/default.md @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/dimension.html b/clients/gallery/contract/screenshots/cases/dimension.html new file mode 100644 index 00000000..0e71e6ee --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/dimension.html @@ -0,0 +1,4 @@ + + +Intl + diff --git a/clients/gallery/contract/screenshots/cases/dimension.json b/clients/gallery/contract/screenshots/cases/dimension.json new file mode 100644 index 00000000..1bea948f --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/dimension.json @@ -0,0 +1 @@ +{"screen": "intl", "dimensions": [{"key": "lang", "label": "Language", "values": [{"key": "en"}, {"key": "fr"}]}]} diff --git a/clients/gallery/contract/screenshots/cases/dimension.md b/clients/gallery/contract/screenshots/cases/dimension.md new file mode 100644 index 00000000..0e71e6ee --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/dimension.md @@ -0,0 +1,4 @@ + + +Intl + diff --git a/clients/gallery/contract/screenshots/cases/duplicate-group.html b/clients/gallery/contract/screenshots/cases/duplicate-group.html new file mode 100644 index 00000000..fffc40ba --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/duplicate-group.html @@ -0,0 +1 @@ +Dup diff --git a/clients/gallery/contract/screenshots/cases/duplicate-group.json b/clients/gallery/contract/screenshots/cases/duplicate-group.json new file mode 100644 index 00000000..19275b83 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/duplicate-group.json @@ -0,0 +1 @@ +{"screen": "dup"} diff --git a/clients/gallery/contract/screenshots/cases/duplicate-group.md b/clients/gallery/contract/screenshots/cases/duplicate-group.md new file mode 100644 index 00000000..c2f409de --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/duplicate-group.md @@ -0,0 +1 @@ +![Dup](https://app.visualci.com/g/acme/web/dup-light.png) diff --git a/clients/gallery/contract/screenshots/cases/escaping.html b/clients/gallery/contract/screenshots/cases/escaping.html new file mode 100644 index 00000000..bfb8af9d --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/escaping.html @@ -0,0 +1 @@ +Quote [1] (beta) *new* <b>"x"</b> & `y` diff --git a/clients/gallery/contract/screenshots/cases/escaping.json b/clients/gallery/contract/screenshots/cases/escaping.json new file mode 100644 index 00000000..ddc64d54 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/escaping.json @@ -0,0 +1 @@ +{"screen": "quote", "alt": "Quote [1]\n\n(beta)\t*new* \"x\" & `y`", "link": true} diff --git a/clients/gallery/contract/screenshots/cases/escaping.md b/clients/gallery/contract/screenshots/cases/escaping.md new file mode 100644 index 00000000..7bca05ae --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/escaping.md @@ -0,0 +1 @@ +[![Quote \[1\] \(beta\) \*new\* \\"x\"\<\/b\> \& \`y\`](https://example.com/shots/quote\(1\)\&x='y'"z"\\v.png)](https://example.com/shots/quote\(1\)\&x='y'"z"\\v.png) diff --git a/clients/gallery/contract/screenshots/cases/folder.html b/clients/gallery/contract/screenshots/cases/folder.html new file mode 100644 index 00000000..f18d7c5a --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/folder.html @@ -0,0 +1 @@ +Sessions diff --git a/clients/gallery/contract/screenshots/cases/folder.json b/clients/gallery/contract/screenshots/cases/folder.json new file mode 100644 index 00000000..328f848b --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/folder.json @@ -0,0 +1 @@ +{"screen": "ios/sessions", "theme": "light", "loading": "eager"} diff --git a/clients/gallery/contract/screenshots/cases/folder.md b/clients/gallery/contract/screenshots/cases/folder.md new file mode 100644 index 00000000..1e866644 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/folder.md @@ -0,0 +1 @@ +![Sessions](https://app.visualci.com/g/acme/web/ios-sessions-light.png) diff --git a/clients/gallery/contract/screenshots/cases/group-size.html b/clients/gallery/contract/screenshots/cases/group-size.html new file mode 100644 index 00000000..adcc32f2 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/group-size.html @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/group-size.json b/clients/gallery/contract/screenshots/cases/group-size.json new file mode 100644 index 00000000..30e13da7 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/group-size.json @@ -0,0 +1 @@ +{"screen": "overview", "group": "iPhone", "size": "desktop", "groups": {"iPhone": {"size": "desktop"}}} diff --git a/clients/gallery/contract/screenshots/cases/group-size.md b/clients/gallery/contract/screenshots/cases/group-size.md new file mode 100644 index 00000000..adcc32f2 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/group-size.md @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/group.html b/clients/gallery/contract/screenshots/cases/group.html new file mode 100644 index 00000000..1749b467 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/group.html @@ -0,0 +1,4 @@ + + +The app on an iPhone + diff --git a/clients/gallery/contract/screenshots/cases/group.json b/clients/gallery/contract/screenshots/cases/group.json new file mode 100644 index 00000000..7758a817 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/group.json @@ -0,0 +1 @@ +{"screen": "overview", "group": "iPhone", "alt": "The app on an iPhone"} diff --git a/clients/gallery/contract/screenshots/cases/group.md b/clients/gallery/contract/screenshots/cases/group.md new file mode 100644 index 00000000..1749b467 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/group.md @@ -0,0 +1,4 @@ + + +The app on an iPhone + diff --git a/clients/gallery/contract/screenshots/cases/light-only.html b/clients/gallery/contract/screenshots/cases/light-only.html new file mode 100644 index 00000000..382eb778 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/light-only.html @@ -0,0 +1 @@ +Billing diff --git a/clients/gallery/contract/screenshots/cases/light-only.json b/clients/gallery/contract/screenshots/cases/light-only.json new file mode 100644 index 00000000..4cf96e85 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/light-only.json @@ -0,0 +1 @@ +{"screen": "billing"} diff --git a/clients/gallery/contract/screenshots/cases/light-only.md b/clients/gallery/contract/screenshots/cases/light-only.md new file mode 100644 index 00000000..48f691a9 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/light-only.md @@ -0,0 +1 @@ +![Billing](https://app.visualci.com/g/acme/web/billing-light.png) diff --git a/clients/gallery/contract/screenshots/cases/link-thumb-dark.html b/clients/gallery/contract/screenshots/cases/link-thumb-dark.html new file mode 100644 index 00000000..ac073860 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/link-thumb-dark.html @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/link-thumb-dark.json b/clients/gallery/contract/screenshots/cases/link-thumb-dark.json new file mode 100644 index 00000000..867e65bd --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/link-thumb-dark.json @@ -0,0 +1 @@ +{"screen": "overview", "link": true, "thumb": true, "loading": "eager"} diff --git a/clients/gallery/contract/screenshots/cases/link-thumb-dark.md b/clients/gallery/contract/screenshots/cases/link-thumb-dark.md new file mode 100644 index 00000000..ac073860 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/link-thumb-dark.md @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/link-thumb.html b/clients/gallery/contract/screenshots/cases/link-thumb.html new file mode 100644 index 00000000..556a54a8 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/link-thumb.html @@ -0,0 +1 @@ +Overview diff --git a/clients/gallery/contract/screenshots/cases/link-thumb.json b/clients/gallery/contract/screenshots/cases/link-thumb.json new file mode 100644 index 00000000..be3d0c53 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/link-thumb.json @@ -0,0 +1 @@ +{"screen": "overview", "theme": "light", "link": true, "thumb": true} diff --git a/clients/gallery/contract/screenshots/cases/link-thumb.md b/clients/gallery/contract/screenshots/cases/link-thumb.md new file mode 100644 index 00000000..2857ee6d --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/link-thumb.md @@ -0,0 +1 @@ +[![Overview](https://app.visualci.com/g/acme/web/overview-light-1440.thumb.webp)](https://app.visualci.com/g/acme/web/overview-light-1440.png) diff --git a/clients/gallery/contract/screenshots/cases/mixed-thumb.html b/clients/gallery/contract/screenshots/cases/mixed-thumb.html new file mode 100644 index 00000000..52562e89 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/mixed-thumb.html @@ -0,0 +1,4 @@ + + +Mixed + diff --git a/clients/gallery/contract/screenshots/cases/mixed-thumb.json b/clients/gallery/contract/screenshots/cases/mixed-thumb.json new file mode 100644 index 00000000..e7120c97 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/mixed-thumb.json @@ -0,0 +1 @@ +{"screen": "mixed", "thumb": true} diff --git a/clients/gallery/contract/screenshots/cases/mixed-thumb.md b/clients/gallery/contract/screenshots/cases/mixed-thumb.md new file mode 100644 index 00000000..52562e89 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/mixed-thumb.md @@ -0,0 +1,4 @@ + + +Mixed + diff --git a/clients/gallery/contract/screenshots/cases/no-theme.html b/clients/gallery/contract/screenshots/cases/no-theme.html new file mode 100644 index 00000000..0ba7aaba --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/no-theme.html @@ -0,0 +1 @@ +Plain diff --git a/clients/gallery/contract/screenshots/cases/no-theme.json b/clients/gallery/contract/screenshots/cases/no-theme.json new file mode 100644 index 00000000..500fca2b --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/no-theme.json @@ -0,0 +1 @@ +{"screen": "plain"} diff --git a/clients/gallery/contract/screenshots/cases/no-theme.md b/clients/gallery/contract/screenshots/cases/no-theme.md new file mode 100644 index 00000000..57905715 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/no-theme.md @@ -0,0 +1 @@ +![Plain](https://app.visualci.com/g/acme/web/plain/1440.png) diff --git a/clients/gallery/contract/screenshots/cases/odd-2x.html b/clients/gallery/contract/screenshots/cases/odd-2x.html new file mode 100644 index 00000000..b0fdeb2c --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/odd-2x.html @@ -0,0 +1 @@ +Odd diff --git a/clients/gallery/contract/screenshots/cases/odd-2x.json b/clients/gallery/contract/screenshots/cases/odd-2x.json new file mode 100644 index 00000000..3bc2ad2c --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/odd-2x.json @@ -0,0 +1 @@ +{"screen": "odd", "link": true} diff --git a/clients/gallery/contract/screenshots/cases/odd-2x.md b/clients/gallery/contract/screenshots/cases/odd-2x.md new file mode 100644 index 00000000..e3dba748 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/odd-2x.md @@ -0,0 +1 @@ +[![Odd](https://app.visualci.com/g/acme/web/odd-light-2x.png)](https://app.visualci.com/g/acme/web/odd-light-2x.png) diff --git a/clients/gallery/contract/screenshots/cases/only-2x.html b/clients/gallery/contract/screenshots/cases/only-2x.html new file mode 100644 index 00000000..36de0e8c --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/only-2x.html @@ -0,0 +1,4 @@ + + +Retina + diff --git a/clients/gallery/contract/screenshots/cases/only-2x.json b/clients/gallery/contract/screenshots/cases/only-2x.json new file mode 100644 index 00000000..f46c3298 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/only-2x.json @@ -0,0 +1 @@ +{"screen": "retina"} diff --git a/clients/gallery/contract/screenshots/cases/only-2x.md b/clients/gallery/contract/screenshots/cases/only-2x.md new file mode 100644 index 00000000..36de0e8c --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/only-2x.md @@ -0,0 +1,4 @@ + + +Retina + diff --git a/clients/gallery/contract/screenshots/cases/phone.html b/clients/gallery/contract/screenshots/cases/phone.html new file mode 100644 index 00000000..1262075a --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/phone.html @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/phone.json b/clients/gallery/contract/screenshots/cases/phone.json new file mode 100644 index 00000000..7f0574d0 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/phone.json @@ -0,0 +1 @@ +{"screen": "overview", "size": "phone"} diff --git a/clients/gallery/contract/screenshots/cases/phone.md b/clients/gallery/contract/screenshots/cases/phone.md new file mode 100644 index 00000000..1262075a --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/phone.md @@ -0,0 +1,4 @@ + + +Overview + diff --git a/clients/gallery/contract/screenshots/cases/small-thumb.html b/clients/gallery/contract/screenshots/cases/small-thumb.html new file mode 100644 index 00000000..f133b674 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/small-thumb.html @@ -0,0 +1 @@ +Small diff --git a/clients/gallery/contract/screenshots/cases/small-thumb.json b/clients/gallery/contract/screenshots/cases/small-thumb.json new file mode 100644 index 00000000..9066168d --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/small-thumb.json @@ -0,0 +1 @@ +{"screen": "small", "thumb": true, "link": true} diff --git a/clients/gallery/contract/screenshots/cases/small-thumb.md b/clients/gallery/contract/screenshots/cases/small-thumb.md new file mode 100644 index 00000000..3cd1d035 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/small-thumb.md @@ -0,0 +1 @@ +[![Small](https://app.visualci.com/g/acme/web/small-light-2x.thumb.webp)](https://app.visualci.com/g/acme/web/small-light-2x.png) diff --git a/clients/gallery/contract/screenshots/cases/stray-2x.html b/clients/gallery/contract/screenshots/cases/stray-2x.html new file mode 100644 index 00000000..7c52a84f --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/stray-2x.html @@ -0,0 +1,4 @@ + + +Stray + diff --git a/clients/gallery/contract/screenshots/cases/stray-2x.json b/clients/gallery/contract/screenshots/cases/stray-2x.json new file mode 100644 index 00000000..5270973e --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/stray-2x.json @@ -0,0 +1 @@ +{"screen": "stray"} diff --git a/clients/gallery/contract/screenshots/cases/stray-2x.md b/clients/gallery/contract/screenshots/cases/stray-2x.md new file mode 100644 index 00000000..7c52a84f --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/stray-2x.md @@ -0,0 +1,4 @@ + + +Stray + diff --git a/clients/gallery/contract/screenshots/cases/theme-dark.html b/clients/gallery/contract/screenshots/cases/theme-dark.html new file mode 100644 index 00000000..5150c6b7 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/theme-dark.html @@ -0,0 +1 @@ +Overview diff --git a/clients/gallery/contract/screenshots/cases/theme-dark.json b/clients/gallery/contract/screenshots/cases/theme-dark.json new file mode 100644 index 00000000..47d628d9 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/theme-dark.json @@ -0,0 +1 @@ +{"screen": "overview", "theme": "dark"} diff --git a/clients/gallery/contract/screenshots/cases/theme-dark.md b/clients/gallery/contract/screenshots/cases/theme-dark.md new file mode 100644 index 00000000..73b8b9d1 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/theme-dark.md @@ -0,0 +1 @@ +![Overview](https://app.visualci.com/g/acme/web/overview-dark-1440.png) diff --git a/clients/gallery/contract/screenshots/cases/wide-2x.html b/clients/gallery/contract/screenshots/cases/wide-2x.html new file mode 100644 index 00000000..1ecb66f4 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/wide-2x.html @@ -0,0 +1 @@ +Wide diff --git a/clients/gallery/contract/screenshots/cases/wide-2x.json b/clients/gallery/contract/screenshots/cases/wide-2x.json new file mode 100644 index 00000000..c18fe922 --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/wide-2x.json @@ -0,0 +1 @@ +{"screen": "wide", "link": true} diff --git a/clients/gallery/contract/screenshots/cases/wide-2x.md b/clients/gallery/contract/screenshots/cases/wide-2x.md new file mode 100644 index 00000000..32beac3e --- /dev/null +++ b/clients/gallery/contract/screenshots/cases/wide-2x.md @@ -0,0 +1 @@ +[![Wide](https://app.visualci.com/g/acme/web/wide-light-390-2x.png)](https://app.visualci.com/g/acme/web/wide-light-390-2x.png) diff --git a/clients/gallery/contract/screenshots/errors/alt-number.json b/clients/gallery/contract/screenshots/errors/alt-number.json new file mode 100644 index 00000000..6fb11a0f --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/alt-number.json @@ -0,0 +1 @@ +{"screen": "overview", "alt": 7} diff --git a/clients/gallery/contract/screenshots/errors/alt-number.message b/clients/gallery/contract/screenshots/errors/alt-number.message new file mode 100644 index 00000000..decd92d8 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/alt-number.message @@ -0,0 +1 @@ +screenshot option alt is invalid diff --git a/clients/gallery/contract/screenshots/errors/dimensions-invalid.json b/clients/gallery/contract/screenshots/errors/dimensions-invalid.json new file mode 100644 index 00000000..1719cb47 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/dimensions-invalid.json @@ -0,0 +1 @@ +{"screen": "overview", "dimensions": {"key": "lang"}} diff --git a/clients/gallery/contract/screenshots/errors/dimensions-invalid.message b/clients/gallery/contract/screenshots/errors/dimensions-invalid.message new file mode 100644 index 00000000..65c01854 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/dimensions-invalid.message @@ -0,0 +1 @@ +gallery option dimensions is invalid diff --git a/clients/gallery/contract/screenshots/errors/duplicate-group-first.json b/clients/gallery/contract/screenshots/errors/duplicate-group-first.json new file mode 100644 index 00000000..b31b127e --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/duplicate-group-first.json @@ -0,0 +1 @@ +{"screen": "dup", "group": "Web"} diff --git a/clients/gallery/contract/screenshots/errors/duplicate-group-first.message b/clients/gallery/contract/screenshots/errors/duplicate-group-first.message new file mode 100644 index 00000000..96a6eb24 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/duplicate-group-first.message @@ -0,0 +1 @@ +gallery group Web has no screen dup diff --git a/clients/gallery/contract/screenshots/errors/group-number.json b/clients/gallery/contract/screenshots/errors/group-number.json new file mode 100644 index 00000000..c607c0d5 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/group-number.json @@ -0,0 +1 @@ +{"screen": "overview", "group": 3} diff --git a/clients/gallery/contract/screenshots/errors/group-number.message b/clients/gallery/contract/screenshots/errors/group-number.message new file mode 100644 index 00000000..26965771 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/group-number.message @@ -0,0 +1 @@ +screenshot option group is invalid diff --git a/clients/gallery/contract/screenshots/errors/group-without-screen.json b/clients/gallery/contract/screenshots/errors/group-without-screen.json new file mode 100644 index 00000000..92120f45 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/group-without-screen.json @@ -0,0 +1 @@ +{"screen": "billing", "group": "iPhone"} diff --git a/clients/gallery/contract/screenshots/errors/group-without-screen.message b/clients/gallery/contract/screenshots/errors/group-without-screen.message new file mode 100644 index 00000000..a7eba69c --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/group-without-screen.message @@ -0,0 +1 @@ +gallery group iPhone has no screen billing diff --git a/clients/gallery/contract/screenshots/errors/groups-invalid.json b/clients/gallery/contract/screenshots/errors/groups-invalid.json new file mode 100644 index 00000000..195a9396 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/groups-invalid.json @@ -0,0 +1 @@ +{"screen": "overview", "groups": {"Web": {"fit": "fill"}}} diff --git a/clients/gallery/contract/screenshots/errors/groups-invalid.message b/clients/gallery/contract/screenshots/errors/groups-invalid.message new file mode 100644 index 00000000..fdea2bf1 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/groups-invalid.message @@ -0,0 +1 @@ +gallery option groups is invalid diff --git a/clients/gallery/contract/screenshots/errors/link-string.json b/clients/gallery/contract/screenshots/errors/link-string.json new file mode 100644 index 00000000..11501b98 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/link-string.json @@ -0,0 +1 @@ +{"screen": "overview", "link": "https://acme.example/"} diff --git a/clients/gallery/contract/screenshots/errors/link-string.message b/clients/gallery/contract/screenshots/errors/link-string.message new file mode 100644 index 00000000..8c585494 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/link-string.message @@ -0,0 +1 @@ +screenshot option link is invalid diff --git a/clients/gallery/contract/screenshots/errors/loading-auto.json b/clients/gallery/contract/screenshots/errors/loading-auto.json new file mode 100644 index 00000000..11575012 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/loading-auto.json @@ -0,0 +1 @@ +{"screen": "overview", "loading": "auto"} diff --git a/clients/gallery/contract/screenshots/errors/loading-auto.message b/clients/gallery/contract/screenshots/errors/loading-auto.message new file mode 100644 index 00000000..98dbd817 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/loading-auto.message @@ -0,0 +1 @@ +screenshot option loading is invalid diff --git a/clients/gallery/contract/screenshots/errors/missing-size.json b/clients/gallery/contract/screenshots/errors/missing-size.json new file mode 100644 index 00000000..54e0a94d --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/missing-size.json @@ -0,0 +1 @@ +{"screen": "billing", "size": "phone"} diff --git a/clients/gallery/contract/screenshots/errors/missing-size.message b/clients/gallery/contract/screenshots/errors/missing-size.message new file mode 100644 index 00000000..e285fe00 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/missing-size.message @@ -0,0 +1 @@ +gallery screen billing in group Web has no light phone screenshot diff --git a/clients/gallery/contract/screenshots/errors/missing-theme.json b/clients/gallery/contract/screenshots/errors/missing-theme.json new file mode 100644 index 00000000..953bbc87 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/missing-theme.json @@ -0,0 +1 @@ +{"screen": "billing", "theme": "dark"} diff --git a/clients/gallery/contract/screenshots/errors/missing-theme.message b/clients/gallery/contract/screenshots/errors/missing-theme.message new file mode 100644 index 00000000..10cb5ab0 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/missing-theme.message @@ -0,0 +1 @@ +gallery screen billing in group Web has no dark desktop screenshot diff --git a/clients/gallery/contract/screenshots/errors/no-group.json b/clients/gallery/contract/screenshots/errors/no-group.json new file mode 100644 index 00000000..e5be27e1 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/no-group.json @@ -0,0 +1 @@ +{"screen": "overview", "group": "Android"} diff --git a/clients/gallery/contract/screenshots/errors/no-group.message b/clients/gallery/contract/screenshots/errors/no-group.message new file mode 100644 index 00000000..d68ca8d4 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/no-group.message @@ -0,0 +1 @@ +the gallery manifest has no group Android diff --git a/clients/gallery/contract/screenshots/errors/no-screen.json b/clients/gallery/contract/screenshots/errors/no-screen.json new file mode 100644 index 00000000..8ccebbff --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/no-screen.json @@ -0,0 +1 @@ +{"screen": "pricing"} diff --git a/clients/gallery/contract/screenshots/errors/no-screen.message b/clients/gallery/contract/screenshots/errors/no-screen.message new file mode 100644 index 00000000..367062f0 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/no-screen.message @@ -0,0 +1 @@ +the gallery manifest has no screen pricing diff --git a/clients/gallery/contract/screenshots/errors/options-array.json b/clients/gallery/contract/screenshots/errors/options-array.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/options-array.json @@ -0,0 +1 @@ +[] diff --git a/clients/gallery/contract/screenshots/errors/options-array.message b/clients/gallery/contract/screenshots/errors/options-array.message new file mode 100644 index 00000000..230a7861 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/options-array.message @@ -0,0 +1 @@ +screenshot options must be an object diff --git a/clients/gallery/contract/screenshots/errors/options-null.json b/clients/gallery/contract/screenshots/errors/options-null.json new file mode 100644 index 00000000..19765bd5 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/options-null.json @@ -0,0 +1 @@ +null diff --git a/clients/gallery/contract/screenshots/errors/options-null.message b/clients/gallery/contract/screenshots/errors/options-null.message new file mode 100644 index 00000000..230a7861 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/options-null.message @@ -0,0 +1 @@ +screenshot options must be an object diff --git a/clients/gallery/contract/screenshots/errors/order.json b/clients/gallery/contract/screenshots/errors/order.json new file mode 100644 index 00000000..846bb703 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/order.json @@ -0,0 +1 @@ +{"screen": "overview", "loading": "auto", "theme": "sepia"} diff --git a/clients/gallery/contract/screenshots/errors/order.message b/clients/gallery/contract/screenshots/errors/order.message new file mode 100644 index 00000000..58928612 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/order.message @@ -0,0 +1 @@ +screenshot option theme is invalid diff --git a/clients/gallery/contract/screenshots/errors/screen-absent.json b/clients/gallery/contract/screenshots/errors/screen-absent.json new file mode 100644 index 00000000..216cb546 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/screen-absent.json @@ -0,0 +1 @@ +{"theme": "dark"} diff --git a/clients/gallery/contract/screenshots/errors/screen-absent.message b/clients/gallery/contract/screenshots/errors/screen-absent.message new file mode 100644 index 00000000..11fc015d --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/screen-absent.message @@ -0,0 +1 @@ +screenshot option screen is required diff --git a/clients/gallery/contract/screenshots/errors/screen-empty.json b/clients/gallery/contract/screenshots/errors/screen-empty.json new file mode 100644 index 00000000..e1e5cf99 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/screen-empty.json @@ -0,0 +1 @@ +{"screen": ""} diff --git a/clients/gallery/contract/screenshots/errors/screen-empty.message b/clients/gallery/contract/screenshots/errors/screen-empty.message new file mode 100644 index 00000000..11fc015d --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/screen-empty.message @@ -0,0 +1 @@ +screenshot option screen is required diff --git a/clients/gallery/contract/screenshots/errors/size-unknown.json b/clients/gallery/contract/screenshots/errors/size-unknown.json new file mode 100644 index 00000000..603eb669 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/size-unknown.json @@ -0,0 +1 @@ +{"screen": "overview", "size": "tablet"} diff --git a/clients/gallery/contract/screenshots/errors/size-unknown.message b/clients/gallery/contract/screenshots/errors/size-unknown.message new file mode 100644 index 00000000..360c1f0b --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/size-unknown.message @@ -0,0 +1 @@ +screenshot option size is invalid diff --git a/clients/gallery/contract/screenshots/errors/size-without-group-option.json b/clients/gallery/contract/screenshots/errors/size-without-group-option.json new file mode 100644 index 00000000..b5e3ea64 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/size-without-group-option.json @@ -0,0 +1 @@ +{"screen": "overview", "group": "iPhone", "size": "desktop"} diff --git a/clients/gallery/contract/screenshots/errors/size-without-group-option.message b/clients/gallery/contract/screenshots/errors/size-without-group-option.message new file mode 100644 index 00000000..68d190e2 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/size-without-group-option.message @@ -0,0 +1 @@ +gallery screen overview in group iPhone has no light desktop screenshot diff --git a/clients/gallery/contract/screenshots/errors/theme-unknown.json b/clients/gallery/contract/screenshots/errors/theme-unknown.json new file mode 100644 index 00000000..085a4875 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/theme-unknown.json @@ -0,0 +1 @@ +{"screen": "overview", "theme": "sepia"} diff --git a/clients/gallery/contract/screenshots/errors/theme-unknown.message b/clients/gallery/contract/screenshots/errors/theme-unknown.message new file mode 100644 index 00000000..58928612 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/theme-unknown.message @@ -0,0 +1 @@ +screenshot option theme is invalid diff --git a/clients/gallery/contract/screenshots/errors/thumb-string.json b/clients/gallery/contract/screenshots/errors/thumb-string.json new file mode 100644 index 00000000..51fe93db --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/thumb-string.json @@ -0,0 +1 @@ +{"screen": "overview", "thumb": "yes"} diff --git a/clients/gallery/contract/screenshots/errors/thumb-string.message b/clients/gallery/contract/screenshots/errors/thumb-string.message new file mode 100644 index 00000000..0bdb26c8 --- /dev/null +++ b/clients/gallery/contract/screenshots/errors/thumb-string.message @@ -0,0 +1 @@ +screenshot option thumb is invalid diff --git a/clients/gallery/contract/screenshots/manifest.json b/clients/gallery/contract/screenshots/manifest.json new file mode 100644 index 00000000..cdae995e --- /dev/null +++ b/clients/gallery/contract/screenshots/manifest.json @@ -0,0 +1,205 @@ +{ + "project": "acme/web", + "groups": [ + { + "name": "Web", + "items": [ + { + "name": "overview-light-1440", + "url": "https://app.visualci.com/g/acme/web/overview-light-1440.png", + "width": 1440, + "height": 900, + "bytes": 182000, + "thumb": { + "url": "https://app.visualci.com/g/acme/web/overview-light-1440.thumb.webp", + "width": 640, + "height": 400, + "bytes": 21000 + } + }, + { + "name": "overview-light-1440-2x", + "url": "https://app.visualci.com/g/acme/web/overview-light-1440-2x.png", + "width": 2880, + "height": 1800 + }, + { + "name": "overview-dark-1440", + "url": "https://app.visualci.com/g/acme/web/overview-dark-1440.png", + "width": 1440, + "height": 900, + "thumb": { + "url": "https://app.visualci.com/g/acme/web/overview-dark-1440.thumb.webp", + "width": 640, + "height": 400 + } + }, + { + "name": "overview-light-390", + "url": "https://app.visualci.com/g/acme/web/overview-light-390.png", + "width": 1170, + "height": 2532 + }, + { + "name": "overview-dark-390", + "url": "https://app.visualci.com/g/acme/web/overview-dark-390.png", + "width": 1170, + "height": 2532 + }, + { + "name": "billing-light", + "url": "https://app.visualci.com/g/acme/web/billing-light.png", + "width": 1280, + "height": 800 + }, + { + "name": "retina-light-2x", + "url": "https://app.visualci.com/g/acme/web/retina-light-2x.png", + "width": 2560, + "height": 1601 + }, + { + "name": "retina-dark-2x.png", + "url": "https://app.visualci.com/g/acme/web/retina-dark-2x.png", + "width": 2560, + "height": 1601 + }, + { + "name": "quote-light", + "url": "https://example.com/shots/quote(1)&x='y'\"z\"\\v.png", + "width": 800, + "height": 600 + }, + { + "name": "stray-light-1440", + "url": "https://app.visualci.com/g/acme/web/stray-light-1440.png", + "width": 1440, + "height": 900 + }, + { + "name": "stray-dark-1440", + "url": "https://app.visualci.com/g/acme/web/stray-dark-1440.png", + "width": 1440, + "height": 900 + }, + { + "name": "stray-light-390-2x", + "url": "https://app.visualci.com/g/acme/web/stray-light-390-2x.png", + "width": 780, + "height": 1688 + }, + { + "name": "mixed-light-1440", + "url": "https://app.visualci.com/g/acme/web/mixed-light-1440.png", + "width": 1440, + "height": 900, + "thumb": { + "url": "https://app.visualci.com/g/acme/web/mixed-light-1440.thumb.webp", + "width": 640, + "height": 400 + } + }, + { + "name": "mixed-dark-1440", + "url": "https://app.visualci.com/g/acme/web/mixed-dark-1440.png", + "width": 1440, + "height": 900 + }, + { + "name": "plain/1440", + "url": "https://app.visualci.com/g/acme/web/plain/1440.png", + "width": 1440, + "height": 900 + }, + { + "name": "odd-light-2x", + "url": "https://app.visualci.com/g/acme/web/odd-light-2x.png", + "width": 1281, + "height": 801 + }, + { + "name": "wide-light-390-2x", + "url": "https://app.visualci.com/g/acme/web/wide-light-390-2x.png", + "width": 1170, + "height": 2532 + }, + { + "name": "small-light-2x", + "url": "https://app.visualci.com/g/acme/web/small-light-2x.png", + "width": 2560, + "height": 1600, + "thumb": { + "url": "https://app.visualci.com/g/acme/web/small-light-2x.thumb.webp", + "width": 640, + "height": 400 + } + }, + { + "name": "intl-light-en", + "url": "https://app.visualci.com/g/acme/web/intl-light-en.png", + "width": 1440, + "height": 900 + }, + { + "name": "intl-light-fr", + "url": "https://app.visualci.com/g/acme/web/intl-light-fr.png", + "width": 1440, + "height": 900 + }, + { + "name": "intl-dark-en", + "url": "https://app.visualci.com/g/acme/web/intl-dark-en.png", + "width": 1440, + "height": 900 + }, + { + "name": "intl-dark-fr", + "url": "https://app.visualci.com/g/acme/web/intl-dark-fr.png", + "width": 1440, + "height": 900 + } + ] + }, + { + "name": "iPhone", + "items": [ + { + "name": "overview-light", + "url": "https://app.visualci.com/g/acme/web/ios-overview-light.png", + "width": 1179, + "height": 2556 + }, + { + "name": "overview-dark", + "url": "https://app.visualci.com/g/acme/web/ios-overview-dark.png", + "width": 1179, + "height": 2556 + }, + { + "name": "ios/sessions-light", + "url": "https://app.visualci.com/g/acme/web/ios-sessions-light.png", + "width": 1179, + "height": 2556 + }, + { + "name": "ios/sessions-dark", + "url": "https://app.visualci.com/g/acme/web/ios-sessions-dark.png", + "width": 1179, + "height": 2556 + } + ] + }, + { + "name": "Web", + "title": "Web again", + "items": [ + { + "name": "dup-light", + "url": "https://app.visualci.com/g/acme/web/dup-light.png", + "width": 1280, + "height": 800 + } + ] + } + ] +} diff --git a/clients/gallery/js/README.md b/clients/gallery/js/README.md index ecbce065..81a06692 100644 --- a/clients/gallery/js/README.md +++ b/clients/gallery/js/README.md @@ -48,6 +48,24 @@ credit, and}`, any of them). `load` and `mount` also take `manifest`, `timeout`, `fetch`. [SPEC.md](../contract/SPEC.md) has the details, including how screenshot names pair (`-[-]` or `/-`). +## One screenshot + +`screenshot` gives one screen's image as HTML: a `` that shows the dark shot to readers whose setting is +dark, or an ``. `screenshotMarkdown` gives the same for a README: `![alt](url)`, or the `` when there's a +dark shot. Neither needs the script or the stylesheet. + +```ts +import { screenshot } from "visualci-gallery/render"; + +const manifest = await fetch("https://app.visualci.com/g/acme/web.json").then((r) => r.json()); +const html = screenshot(manifest, { screen: "overview", link: true }); +``` + +Options: `screen` (its key, required), `group` (the first that has it), `theme` and `size` (the screen's first; with +no `theme`, dark readers see the dark shot), `alt` (the screen's title), `link` (false: true links to the original), +`thumb` (false: true shows the manifest's small copy), `loading` ("lazy" or "eager"), and the gallery's `dimensions` +and `groups`. They throw when the screen has no shot for what you ask. + ## Styling `gallery.css` is the default look. Set the `--vg-*` variables (`--vg-ink`, `--vg-muted`, `--vg-paper`, diff --git a/clients/gallery/js/src/index.ts b/clients/gallery/js/src/index.ts index e9c0182c..a4408a82 100644 --- a/clients/gallery/js/src/index.ts +++ b/clients/gallery/js/src/index.ts @@ -29,6 +29,7 @@ export type { Theme, } from "./model.js"; export { render, escapeHtml } from "./render.js"; +export { screenshot, screenshotMarkdown, type ScreenshotOptions } from "./screenshot.js"; export { hydrate, type Gallery, type HydrateOptions } from "./hydrate.js"; export { load, type LoadOptions } from "./load.js"; diff --git a/clients/gallery/js/src/model.ts b/clients/gallery/js/src/model.ts index e063ffc0..419ca32d 100644 --- a/clients/gallery/js/src/model.ts +++ b/clients/gallery/js/src/model.ts @@ -388,7 +388,7 @@ const isTexts = (keys: string[]) => (v: unknown) => ); /** The manifest's shape (contract/SPEC.md, "Validation"): anything else is refused. */ -function checkManifest(manifest: unknown): asserts manifest is Manifest { +export function checkManifest(manifest: unknown): asserts manifest is Manifest { if (!isObject(manifest) || !Array.isArray(manifest.groups)) fail("not a gallery manifest: it has no groups"); manifest.groups.forEach((g: unknown, i: number) => { if (!isObject(g) || typeof g.name !== "string" || g.name === "") fail(`gallery manifest group ${i} has no name`); @@ -431,7 +431,7 @@ export function buildModel(manifest: Manifest, options: Options = {}): Model { const headingLevel = Math.min(Math.max(level, 1), 4); const titleOpt = o.title === undefined ? undefined : (opt(o.title, isString, "title") ?? null); const id = opt(o.id, (v) => isString(v) && ID.test(v), "id") ?? "vg"; - const dimensions = resolveDimensions(opt(o.dimensions, checkDimensions, "dimensions")); + const dimensions = dimensionsOption(o.dimensions); const sizeValues = dimensions.find((d) => d.key === "size")!.values; const screensOpt = opt>(o.screens, isTexts(["title", "caption"]), "screens") ?? {}; const groupsOpt = @@ -708,6 +708,11 @@ function checkDimensions(list: unknown): boolean { return true; } +/** The `dimensions` option, checked and resolved: the built-in dimensions with its changes, then its own. */ +export function dimensionsOption(value: unknown): Dimension[] { + return resolveDimensions(opt(value as DimensionOptions[], checkDimensions, "dimensions")); +} + /** The built-in dimensions with the option's changes, then the option's own. */ function resolveDimensions(list: DimensionOptions[] | undefined): Dimension[] { const given = list ?? []; diff --git a/clients/gallery/js/src/render-entry.ts b/clients/gallery/js/src/render-entry.ts index 40543701..007d2205 100644 --- a/clients/gallery/js/src/render-entry.ts +++ b/clients/gallery/js/src/render-entry.ts @@ -13,4 +13,5 @@ export type { Theme, } from "./model.js"; export { render, escapeHtml } from "./render.js"; +export { screenshot, screenshotMarkdown, type ScreenshotOptions } from "./screenshot.js"; export { load, type LoadOptions } from "./load.js"; diff --git a/clients/gallery/js/src/screenshot.ts b/clients/gallery/js/src/screenshot.ts new file mode 100644 index 00000000..e5a185dc --- /dev/null +++ b/clients/gallery/js/src/screenshot.ts @@ -0,0 +1,196 @@ +// One screenshot from the gallery's manifest, for a page or a README: an , or a that shows the dark +// shot when the reader's setting is dark (contract/SPEC.md, "A single screenshot"). Pure string building, like +// render.ts. + +import { + buildModel, + checkManifest, + dimensionsOption, + parseShot, + type GroupOptions, + type Manifest, + type ManifestGroup, + type ManifestItem, + type Variant, +} from "./model.js"; +import { escapeHtml } from "./render.js"; + +export interface ScreenshotOptions { + /** The screen's key, as the gallery's `screens` and `order` name it (`overview`, `ios/sessions`). */ + screen: string; + /** The group it's in. Default: the first group that has the screen. */ + group?: string | null; + /** The theme value to show. Default: the screen's first, with its dark shot for readers whose setting is dark. */ + theme?: string | null; + /** The size value to show. Default: the screen's first that has the theme. */ + size?: string | null; + /** The image's alt text. Default: the screen's title, made from its key. */ + alt?: string | null; + /** Link the image to its full-size original. Default false. */ + link?: boolean | null; + /** Show the manifest's small copy where it has one. Default false. */ + thumb?: boolean | null; + /** "lazy" (the default) or "eager". */ + loading?: "lazy" | "eager" | null; + /** As the gallery's options of the same names, so a screen's shots pair the same way. */ + dimensions?: unknown; + groups?: Record | null; +} + +/** The image a screenshot shows: its URL, its 2x copy's, and its size in CSS pixels. */ +interface Picked { + src: string; + src2x: string | null; + width: number; + height: number; +} + +interface Shown { + light: Picked; + dark: Picked | null; + href: string; + link: boolean; + alt: string; + loading: string; +} + +function fail(message: string): never { + throw new Error(message); +} + +const given = (v: unknown) => v !== undefined && v !== null; + +function check(options: unknown, key: string, ok: (v: unknown) => boolean): void { + const v = (options as Record)[key]; + if (given(v) && !ok(v)) fail(`screenshot option ${key} is invalid`); +} + +/** The screen's items in its group. When every one is a 2x shot, a 1x copy of each comes after them, so the screen + * still has images: its 2x shots at half their size. Otherwise the screen is the gallery's, 2x shots only as 2x copies. */ +function screenItems(group: ManifestGroup, screen: string, dims: ReturnType): ManifestItem[] { + const items = group.items.filter((item) => parseShot(item.name, dims).key === screen); + if (!items.every((item) => parseShot(item.name, dims).x2)) return items; + const halves = items.map((item) => { + const width = Math.floor((item.width + 1) / 2); + const height = Math.floor((2 * item.height * width + item.width) / (2 * item.width)); + const name = item.name.replace(/\.(?:[pP][nN][gG]|[jJ][pP][eE]?[gG]|[wW][eE][bB][pP])$/, "").replace(/-2x$/, ""); + return { name, url: item.url, width, height, thumb: item.thumb }; + }); + return items.concat(halves); +} + +function pick(v: Variant, thumb: boolean): Picked { + if (thumb && v.thumb) return { src: v.thumb.url, src2x: null, width: v.thumb.width, height: v.thumb.height }; + return { + src: v.url, + src2x: v.url2x !== null && v.url2x !== v.url ? v.url2x : null, + width: v.width, + height: v.height, + }; +} + +function shown(manifest: Manifest, options: ScreenshotOptions): Shown { + checkManifest(manifest); + if (typeof options !== "object" || options === null || Array.isArray(options)) + fail("screenshot options must be an object"); + const dims = dimensionsOption(options.dimensions); + const valueOf = (key: string) => dims.find((d) => d.key === key)!.values.map((v) => v.key); + if (typeof options.screen !== "string" || options.screen === "") fail("screenshot option screen is required"); + check(options, "group", (v) => typeof v === "string"); + check(options, "theme", (v) => typeof v === "string" && valueOf("theme").includes(v)); + check(options, "size", (v) => typeof v === "string" && valueOf("size").includes(v)); + check(options, "alt", (v) => typeof v === "string"); + check(options, "link", (v) => typeof v === "boolean"); + check(options, "thumb", (v) => typeof v === "boolean"); + check(options, "loading", (v) => v === "lazy" || v === "eager"); + const screen = options.screen; + const has = (g: ManifestGroup) => g.items.some((item) => parseShot(item.name, dims).key === screen); + let group: ManifestGroup | undefined; + if (given(options.group)) { + group = manifest.groups.find((g) => g.name === options.group); + if (!group) fail(`the gallery manifest has no group ${options.group}`); + if (!has(group)) fail(`gallery group ${group.name} has no screen ${screen}`); + } else { + group = manifest.groups.find(has); + if (!group) fail(`the gallery manifest has no screen ${screen}`); + } + const model = buildModel( + { groups: [{ name: group.name, title: group.title ?? null, items: screenItems(group, screen, dims) }] }, + { dimensions: options.dimensions as never, groups: options.groups ?? undefined }, + ); + const sc = model.groups[0].screens[0]; + // The values shown: each dimension's option, else the first value of the screen's first variant that has the values + // chosen so far. + const choice: Record = {}; + const set = options as unknown as Record; + for (const d of model.dimensions) { + const asked = d.key === "theme" || d.key === "size" ? set[d.key] : undefined; + if (given(asked)) { + choice[d.key] = asked!; + continue; + } + const fits = sc.variants.find((v) => Object.keys(choice).every((k) => v.values[k] === choice[k])); + choice[d.key] = (fits ?? sc.variants[0]).values[d.key]; + } + const find = (c: Record) => + sc.variants.find((v) => model.dimensions.every((d) => v.values[d.key] === c[d.key])); + const v = find(choice); + if (!v) { + const keys = model.dimensions.map((d) => choice[d.key]).join(" "); + fail(`gallery screen ${screen} in group ${group.name} has no ${keys} screenshot`); + } + // The dark shot, for readers whose setting is dark, when no theme was asked for. + const darkChoice: Record = {}; + for (const d of model.dimensions) darkChoice[d.key] = d.key === "theme" ? "dark" : choice[d.key]; + const found = !given(options.theme) && choice.theme !== "dark" ? find(darkChoice) : undefined; + // Not when it's the same image (a screen whose names give no theme). + const dv = found && (found.url !== v.url || found.url2x !== v.url2x) ? found : undefined; + // The small copies only when every image shown has one, so light and dark readers see the same size. + const thumb = options.thumb === true && v.thumb !== null && (!dv || dv.thumb !== null); + return { + light: pick(v, thumb), + dark: dv ? pick(dv, thumb) : null, + href: v.url, + link: options.link === true, + // A blank line would end the image in Markdown, or the HTML block the is in. + alt: (given(options.alt) ? options.alt! : sc.title).replace(/[\t\n\r ]+/g, " ").replace(/^ | $/g, ""), + loading: options.loading ?? "lazy", + }; +} + +/** `url` with its 2x copy, as a srcset. */ +function srcset(p: Picked): string { + return p.src2x === null ? escapeHtml(p.src) : `${escapeHtml(p.src)}, ${escapeHtml(p.src2x)} 2x`; +} + +function pictureHtml(s: Shown): string { + const img = + `${escapeHtml(s.alt)}`; + const open = s.link ? `` : ""; + const close = open ? "" : ""; + if (!s.dark) return `${open}${img}${close}\n`; + const source = ``; + return [`${open}`, source, img, `${close}`].join("\n") + "\n"; +} + +/** One screenshot as HTML: an , or a with the dark shot for readers whose setting is dark. Throws when + * the manifest or the options are invalid, or the screen has no shot for the values asked for. */ +export function screenshot(manifest: Manifest, options: ScreenshotOptions): string { + return pictureHtml(shown(manifest, options)); +} + +/** Markdown's backslash escape for every ASCII punctuation character. */ +const markdownText = (s: string) => s.replace(/[!-/:-@[-`{-~]/g, (c) => `\\${c}`); +/** A link destination: the characters that would end it, start an escape or an entity, escaped. */ +const markdownUrl = (s: string) => s.replace(/[\\()&]/g, (c) => `\\${c}`); + +/** One screenshot for a README: Markdown's `![alt](url)`, linked when asked, or, when it has a dark shot, the HTML + * (which GitHub and most Markdown renderers show as it is). */ +export function screenshotMarkdown(manifest: Manifest, options: ScreenshotOptions): string { + const s = shown(manifest, options); + if (s.dark) return pictureHtml(s); + const image = `![${markdownText(s.alt)}](${markdownUrl(s.light.src)})`; + return (s.link ? `[${image}](${markdownUrl(s.href)})` : image) + "\n"; +} diff --git a/clients/gallery/js/test/contract.test.ts b/clients/gallery/js/test/contract.test.ts index bfc451a6..966db0f6 100644 --- a/clients/gallery/js/test/contract.test.ts +++ b/clients/gallery/js/test/contract.test.ts @@ -4,7 +4,15 @@ import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { buildModel, render, type Manifest, type Options } from "../src/render-entry.js"; +import { + buildModel, + render, + screenshot, + screenshotMarkdown, + type Manifest, + type Options, + type ScreenshotOptions, +} from "../src/render-entry.js"; const contract = join(import.meta.dirname, "../../contract"); const fixtures = join(contract, "fixtures"); @@ -68,3 +76,37 @@ describe("manifests that can't be shown", () => { it(name, () => refuses(dir, name, () => buildModel(read(join(dir, name)) as Manifest, {}))); } }); + +describe("single screenshots (contract/screenshots)", () => { + const dir = join(contract, "screenshots"); + const manifest = read(join(dir, "manifest.json")) as Manifest; + const cases = join(dir, "cases"); + const names = readdirSync(cases) + .filter((f) => f.endsWith(".json")) + .map((f) => f.replace(/\.json$/, "")) + .sort(); + it("has cases", () => expect(names.length).toBeGreaterThan(0)); + for (const name of names) { + it(name, () => { + const options = read(join(cases, `${name}.json`)) as ScreenshotOptions; + const html = screenshot(manifest, options); + const markdown = screenshotMarkdown(manifest, options); + if (update) { + writeFileSync(join(cases, `${name}.html`), html); + writeFileSync(join(cases, `${name}.md`), markdown); + } + expect(html).toBe(readFileSync(join(cases, `${name}.html`), "utf8")); + expect(markdown).toBe(readFileSync(join(cases, `${name}.md`), "utf8")); + }); + } + const errors = join(dir, "errors"); + for (const name of readdirSync(errors) + .filter((f) => f.endsWith(".json")) + .sort()) { + it(`refuses ${name}`, () => { + const options = read(join(errors, name)) as ScreenshotOptions; + refuses(errors, name, () => screenshot(manifest, options)); + refuses(errors, name, () => screenshotMarkdown(manifest, options)); + }); + } +});