@pseudolux-ui/media-library 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/package.json +22 -4
- package/src/components/AssetGrid.lsp +1777 -0
- package/src/components/AssetInspector.lsp +1622 -0
- package/src/components/BinDialog.lsp +208 -0
- package/src/components/CropEditor.lsp +616 -0
- package/src/components/ResponsiveImage.lsp +297 -0
- package/src/config.ts +137 -0
- package/src/index.ts +33 -0
- package/src/ladder.ts +82 -0
- package/src/port.ts +217 -0
- package/src/styles/px.css +177 -0
- package/README.md +0 -3
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
<view>
|
|
2
|
+
<div class="responsive-image" $ref="frame" $style="state.frameStyle">
|
|
3
|
+
<!-- The static render. A box of the right shape in the picture's own colour,
|
|
4
|
+
present before any JavaScript runs and removed only once the real image
|
|
5
|
+
has painted over it. -->
|
|
6
|
+
<div class="stand-in" $show="state.showStandIn" $style="state.standInStyle"></div>
|
|
7
|
+
|
|
8
|
+
<img
|
|
9
|
+
class="picture"
|
|
10
|
+
$show="state.src"
|
|
11
|
+
$src="state.src"
|
|
12
|
+
$alt="state.alt"
|
|
13
|
+
$width="state.width"
|
|
14
|
+
$height="state.height"
|
|
15
|
+
$loading="state.loading"
|
|
16
|
+
$style="state.pictureStyle"
|
|
17
|
+
@load="loaded"
|
|
18
|
+
@error="failed"
|
|
19
|
+
/>
|
|
20
|
+
|
|
21
|
+
<noscript>
|
|
22
|
+
<!-- Without JavaScript the box above is all there is, which is a coloured
|
|
23
|
+
rectangle where a picture should be. One default size is not the right
|
|
24
|
+
width for anybody and is the right order of magnitude for everybody. -->
|
|
25
|
+
<img class="picture plain" $src="state.fallback" $alt="state.alt" />
|
|
26
|
+
</noscript>
|
|
27
|
+
</div>
|
|
28
|
+
</view>
|
|
29
|
+
|
|
30
|
+
<logic lang="ts">
|
|
31
|
+
import { defineProps, reactive, onMounted, onUpdate, onUnmounted, defineRefs } from '@lightspeed-js/core'
|
|
32
|
+
import { getLibrary } from '../config.ts'
|
|
33
|
+
import { pixelsFor } from '../ladder.ts'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A picture that asks for the size it is actually being shown at.
|
|
37
|
+
*
|
|
38
|
+
* The alternative is `srcset`, which asks the author to guess the layout at
|
|
39
|
+
* build time and then asks the browser to guess again from `sizes`. This
|
|
40
|
+
* measures instead - but it can only measure after layout, which is why the
|
|
41
|
+
* static HTML carries the *shape* and the colour, and hydration supplies the
|
|
42
|
+
* pixels.
|
|
43
|
+
*
|
|
44
|
+
* That split is what a Lightspeed island already is: one component, whose
|
|
45
|
+
* server render *is* the placeholder. React needs two components for it,
|
|
46
|
+
* because the halves sit on opposite sides of a boundary.
|
|
47
|
+
*
|
|
48
|
+
* **Props may change under it.** In a picker the same instance is reused as
|
|
49
|
+
* the selection moves, so everything derived from a prop is computed in
|
|
50
|
+
* `configure`, which `onUpdate` re-runs. Reading a prop once into a local at
|
|
51
|
+
* setup is what stops that working - the framework keeps `props` current, and
|
|
52
|
+
* a snapshot opts out of it.
|
|
53
|
+
*/
|
|
54
|
+
const props = defineProps({
|
|
55
|
+
/** The asset. */
|
|
56
|
+
pid: { default: '' },
|
|
57
|
+
/** Unique on the page. Only used to find this element in order to measure
|
|
58
|
+
* it - see the note above about why that is necessary. */
|
|
59
|
+
id: { default: '' },
|
|
60
|
+
alt: { default: '' },
|
|
61
|
+
/** A number or `"320px"` fixes it; `"100%"` or `"50vw"` measures it. */
|
|
62
|
+
width: { default: undefined },
|
|
63
|
+
height: { default: undefined },
|
|
64
|
+
/** Width over height. Reserves the box when only one dimension is given. */
|
|
65
|
+
aspectRatio: { default: undefined },
|
|
66
|
+
/** The picture's own colour, from its palette. Falls back to a neutral. */
|
|
67
|
+
standIn: { default: '#eee' },
|
|
68
|
+
/** `contain` rather than `cover`. */
|
|
69
|
+
fit: { default: 'cover' },
|
|
70
|
+
objectPosition: { default: '' },
|
|
71
|
+
/** Above the fold: load eagerly and do not fade. */
|
|
72
|
+
priority: { default: false },
|
|
73
|
+
format: { default: 'webp' },
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
/** How long the fade lasts, and the point past which it is worth having. */
|
|
77
|
+
const FADE_MS = 400
|
|
78
|
+
const INSTANT_MS = 1000
|
|
79
|
+
|
|
80
|
+
/** What no-JS gets. Wrong for everybody, and wrong by less than any other
|
|
81
|
+
* single number would be. */
|
|
82
|
+
const FALLBACK_WIDTH = 800
|
|
83
|
+
|
|
84
|
+
const fixed = (value) => {
|
|
85
|
+
if (typeof value === 'number') return value
|
|
86
|
+
if (typeof value === 'string') {
|
|
87
|
+
const px = value.match(/^(\d+)px$/)
|
|
88
|
+
if (px) return parseInt(px[1], 10)
|
|
89
|
+
}
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const responsive = (value) =>
|
|
94
|
+
typeof value === 'string' && !/^\d+px$/.test(value) ? value : null
|
|
95
|
+
|
|
96
|
+
const ratio = (value) => {
|
|
97
|
+
if (typeof value === 'number') return value > 0 ? value : null
|
|
98
|
+
if (typeof value === 'string' && value.includes('/')) {
|
|
99
|
+
const [w, h] = value.split('/').map((one) => parseFloat(one))
|
|
100
|
+
return w > 0 && h > 0 ? w / h : null
|
|
101
|
+
}
|
|
102
|
+
const parsed = parseFloat(value)
|
|
103
|
+
return parsed > 0 ? parsed : null
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const refs = defineRefs()
|
|
107
|
+
|
|
108
|
+
const state = reactive({
|
|
109
|
+
src: '',
|
|
110
|
+
fallback: '',
|
|
111
|
+
alt: '',
|
|
112
|
+
width: 0,
|
|
113
|
+
height: 0,
|
|
114
|
+
loading: 'lazy',
|
|
115
|
+
showStandIn: true,
|
|
116
|
+
frameStyle: '',
|
|
117
|
+
standInStyle: '',
|
|
118
|
+
pictureStyle: '',
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
let root = null
|
|
122
|
+
let cleanUp = null
|
|
123
|
+
let startedAt = 0
|
|
124
|
+
let fade = FADE_MS
|
|
125
|
+
// The largest size already requested. Resizing smaller must not ask for a
|
|
126
|
+
// smaller file: the bigger one is in cache and the browser scales it for free,
|
|
127
|
+
// so a downward request costs a round trip to look identical.
|
|
128
|
+
let asked = 0
|
|
129
|
+
// What is on screen: the picture and the box it was given. The picker reuses
|
|
130
|
+
// one instance as the selection moves, so a new `pid` is a different picture
|
|
131
|
+
// in the same box rather than a new component - and a new shape for the same
|
|
132
|
+
// picture, which arrives when the detail refines a row's guess, is a new box.
|
|
133
|
+
let showing = ''
|
|
134
|
+
|
|
135
|
+
function signature() {
|
|
136
|
+
return [props.pid, props.aspectRatio, props.width, props.height, props.fit].join('|')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function ask(cssWidth, cssHeight) {
|
|
140
|
+
// Device pixels, not CSS pixels - and capped, snapped and tolerance-checked
|
|
141
|
+
// in one place so no component quietly disagrees with another.
|
|
142
|
+
const dpr = (typeof window !== 'undefined' && window.devicePixelRatio) || 1
|
|
143
|
+
const want = pixelsFor(cssWidth, dpr)
|
|
144
|
+
if (want <= asked) return
|
|
145
|
+
|
|
146
|
+
asked = want
|
|
147
|
+
state.width = Math.round(cssWidth)
|
|
148
|
+
state.height = Math.round(cssHeight)
|
|
149
|
+
state.src = getLibrary().imageUrl(props.pid, {
|
|
150
|
+
width: want,
|
|
151
|
+
format: props.format,
|
|
152
|
+
...(props.fit === 'contain' ? { fit: 'inside' } : {}),
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function measure() {
|
|
157
|
+
if (!root) return
|
|
158
|
+
|
|
159
|
+
const box = root.getBoundingClientRect()
|
|
160
|
+
const aspect = ratio(props.aspectRatio)
|
|
161
|
+
const w = fixed(props.width) ?? box.width ?? 0
|
|
162
|
+
let h = fixed(props.height) ?? box.height ?? 0
|
|
163
|
+
|
|
164
|
+
// A box with a reserved aspect ratio and no measured height still knows its
|
|
165
|
+
// height. Asking the DOM before layout would report zero.
|
|
166
|
+
if (!h && aspect && w) h = Math.round(w / aspect)
|
|
167
|
+
|
|
168
|
+
if (w > 0) ask(w, h)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function loaded() {
|
|
172
|
+
// A picture that arrived in under a second was almost certainly cached, and
|
|
173
|
+
// fading it in makes a fast load look slow.
|
|
174
|
+
if (Date.now() - startedAt < INSTANT_MS) fade = 0
|
|
175
|
+
|
|
176
|
+
state.pictureStyle = pictureStyle(1)
|
|
177
|
+
// Held until the fade finishes, so nothing shows through a half-transparent
|
|
178
|
+
// image on the way in.
|
|
179
|
+
setTimeout(() => {
|
|
180
|
+
state.showStandIn = false
|
|
181
|
+
}, fade)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function failed() {
|
|
185
|
+
// The stand-in stays. A box in the picture's own colour is a better answer
|
|
186
|
+
// than a broken-image glyph, and the layout does not move either way.
|
|
187
|
+
state.src = ''
|
|
188
|
+
state.showStandIn = true
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function pictureStyle(opacity) {
|
|
192
|
+
return [
|
|
193
|
+
'opacity:' + opacity,
|
|
194
|
+
'transition: opacity ' + fade + 'ms ease-in',
|
|
195
|
+
'object-fit:' + (props.fit === 'contain' ? 'contain' : 'cover'),
|
|
196
|
+
props.objectPosition ? 'object-position:' + props.objectPosition : '',
|
|
197
|
+
]
|
|
198
|
+
.filter(Boolean)
|
|
199
|
+
.join(';')
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Everything derived from a prop, in one place so a prop change can redo it.
|
|
204
|
+
*
|
|
205
|
+
* Called on mount and again from `onUpdate`. The reset matters as much as the
|
|
206
|
+
* recompute: `asked` is the largest size requested *for the previous picture*,
|
|
207
|
+
* and leaving it set would make a newly selected image skip its own measure.
|
|
208
|
+
*/
|
|
209
|
+
function configure() {
|
|
210
|
+
startedAt = Date.now()
|
|
211
|
+
showing = signature()
|
|
212
|
+
|
|
213
|
+
asked = 0
|
|
214
|
+
state.src = ''
|
|
215
|
+
state.showStandIn = true
|
|
216
|
+
fade = props.priority ? 0 : FADE_MS
|
|
217
|
+
|
|
218
|
+
const aspect = ratio(props.aspectRatio)
|
|
219
|
+
const w = fixed(props.width)
|
|
220
|
+
const h = fixed(props.height)
|
|
221
|
+
|
|
222
|
+
state.alt = props.alt
|
|
223
|
+
state.loading = props.priority ? 'eager' : 'lazy'
|
|
224
|
+
|
|
225
|
+
// The frame is what holds space. Everything inside it is absolutely
|
|
226
|
+
// positioned, so swapping the stand-in for the picture cannot move the page.
|
|
227
|
+
const frame = []
|
|
228
|
+
const wide = responsive(props.width)
|
|
229
|
+
const tall = responsive(props.height)
|
|
230
|
+
if (wide) frame.push('width:' + wide)
|
|
231
|
+
else if (w !== null) frame.push('width:' + w + 'px')
|
|
232
|
+
if (tall) frame.push('height:' + tall)
|
|
233
|
+
else if (h !== null) frame.push('height:' + h + 'px')
|
|
234
|
+
if (aspect && h === null && !tall) frame.push('aspect-ratio:' + aspect)
|
|
235
|
+
|
|
236
|
+
state.frameStyle = frame.join(';')
|
|
237
|
+
state.standInStyle = 'background:' + props.standIn
|
|
238
|
+
state.pictureStyle = pictureStyle(0)
|
|
239
|
+
state.fallback = getLibrary().imageUrl(props.pid, {
|
|
240
|
+
width: FALLBACK_WIDTH,
|
|
241
|
+
format: props.format,
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
measure()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
onMounted(() => {
|
|
248
|
+
root = refs.frame
|
|
249
|
+
configure()
|
|
250
|
+
|
|
251
|
+
let pending
|
|
252
|
+
const onResize = () => {
|
|
253
|
+
clearTimeout(pending)
|
|
254
|
+
pending = setTimeout(measure, 150)
|
|
255
|
+
}
|
|
256
|
+
window.addEventListener('resize', onResize)
|
|
257
|
+
cleanUp = () => {
|
|
258
|
+
window.removeEventListener('resize', onResize)
|
|
259
|
+
clearTimeout(pending)
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
onUpdate(() => {
|
|
264
|
+
if (!root || signature() === showing) return
|
|
265
|
+
configure()
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
onUnmounted(() => {
|
|
269
|
+
if (cleanUp) cleanUp()
|
|
270
|
+
})
|
|
271
|
+
</logic>
|
|
272
|
+
|
|
273
|
+
<style>
|
|
274
|
+
.responsive-image {
|
|
275
|
+
position: relative;
|
|
276
|
+
overflow: hidden;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
.stand-in {
|
|
280
|
+
position: absolute;
|
|
281
|
+
inset: 0;
|
|
282
|
+
z-index: 1;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
.picture {
|
|
286
|
+
position: absolute;
|
|
287
|
+
inset: 0;
|
|
288
|
+
z-index: 2;
|
|
289
|
+
width: 100%;
|
|
290
|
+
height: 100%;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/* No-JS: nothing measured it, so it is a plain image in a reserved box. */
|
|
294
|
+
.picture.plain {
|
|
295
|
+
opacity: 1;
|
|
296
|
+
}
|
|
297
|
+
</style>
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/** What the library's components talk to.
|
|
2
|
+
*
|
|
3
|
+
* Injected by the host rather than built here. The same components run in the
|
|
4
|
+
* standalone browser app and embedded in a CMS, and those two disagree about
|
|
5
|
+
* everything that would go into constructing a client: which distribution,
|
|
6
|
+
* whether requests carry a session, and who is allowed to see what. A package
|
|
7
|
+
* that read an environment variable would be right in exactly one of them.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { LibraryPort } from "./port.js"
|
|
11
|
+
|
|
12
|
+
/** The alt text and credit as one group sees them. The same merge Pseudolux
|
|
13
|
+
* and its client make; written here so a component needs only this package. */
|
|
14
|
+
export const describedFor = (
|
|
15
|
+
item: { altText?: string; credit?: string; views?: Record<string, { altText?: string; credit?: string }> },
|
|
16
|
+
view: string | undefined
|
|
17
|
+
): { altText?: string; credit?: string } => {
|
|
18
|
+
const override = view ? item.views?.[view.trim().toLowerCase()] : undefined
|
|
19
|
+
return {
|
|
20
|
+
...(item.altText !== undefined ? { altText: item.altText } : {}),
|
|
21
|
+
...(item.credit !== undefined ? { credit: item.credit } : {}),
|
|
22
|
+
...override,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let port: LibraryPort | null = null
|
|
27
|
+
let writable = false
|
|
28
|
+
let writeGroup: string | undefined
|
|
29
|
+
let writeGroups: string[] | undefined
|
|
30
|
+
let hostView: string | undefined
|
|
31
|
+
|
|
32
|
+
export const configure = (
|
|
33
|
+
instance: LibraryPort,
|
|
34
|
+
options: { canWrite?: boolean; writeGroup?: string; writeGroups?: string[]; view?: string } = {}
|
|
35
|
+
): void => {
|
|
36
|
+
port = instance
|
|
37
|
+
/** Declared by the host, because only the host knows. A browser client with
|
|
38
|
+
* no write endpoint cannot describe anything; a CMS knows whether *this
|
|
39
|
+
* account* may. Either way the form should say so rather than finding out
|
|
40
|
+
* on the save button. */
|
|
41
|
+
writable = options.canWrite ?? false
|
|
42
|
+
/** The one group this side may describe a view for. Unset with `canWrite`
|
|
43
|
+
* means any, which is what a direct build signing as itself gets. */
|
|
44
|
+
writeGroup = options.writeGroup
|
|
45
|
+
/** Every group this side may change pictures on, where it may write to
|
|
46
|
+
* more than one. */
|
|
47
|
+
writeGroups = options.writeGroups
|
|
48
|
+
/** The group the library is looked at as when none is chosen: a CMS
|
|
49
|
+
* mounts it as its own, so pictures are worded for where they will be
|
|
50
|
+
* used. Choosing a group in the grid looks at that one instead. */
|
|
51
|
+
hostView = options.view
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Why this side may not change a picture, or `""` when it may. With a write
|
|
56
|
+
* group declared, only pictures in that group can be changed here; the rest
|
|
57
|
+
* are there to be used.
|
|
58
|
+
*/
|
|
59
|
+
export const cannotChange = (tags: string[] | undefined): string => {
|
|
60
|
+
if (!writable) return "Read-only — no write endpoint configured"
|
|
61
|
+
const groups = writeGroups ?? (writeGroup !== undefined ? [writeGroup] : undefined)
|
|
62
|
+
if (groups !== undefined && !(tags ?? []).some((tag) => groups.includes(tag))) {
|
|
63
|
+
return `Read-only here — it can be used, but it is not in ${groups.join(" or ")}`
|
|
64
|
+
}
|
|
65
|
+
return ""
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The host's own view, if it declared one. */
|
|
69
|
+
export const defaultView = (): string | undefined => hostView
|
|
70
|
+
|
|
71
|
+
/** Whether this side may say what a picture means in `view`. */
|
|
72
|
+
export const canDescribeView = (view: string | undefined): boolean =>
|
|
73
|
+
Boolean(view) && writable && (writeGroup === undefined || writeGroup === view)
|
|
74
|
+
|
|
75
|
+
export const getLibrary = (): LibraryPort => {
|
|
76
|
+
if (!port) {
|
|
77
|
+
/** A clear failure at the first request beats a component that silently
|
|
78
|
+
* renders nothing, which is indistinguishable from an empty library. */
|
|
79
|
+
throw new Error(
|
|
80
|
+
"@pseudolux-ui/media-library is not configured. Call configure(port) " +
|
|
81
|
+
"before mounting its components."
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
return port
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const canWrite = (): boolean => writable
|
|
88
|
+
|
|
89
|
+
/** How large a thumbnail to request.
|
|
90
|
+
*
|
|
91
|
+
* A fixed size rather than the grid's rendered size: every distinct width is
|
|
92
|
+
* its own CDN entry and its own transform, so a handful of sizes shared by
|
|
93
|
+
* everybody beats a size per viewport. */
|
|
94
|
+
export const THUMBNAIL_WIDTH = 400
|
|
95
|
+
|
|
96
|
+
export const thumbnailUrl = (id: string): string =>
|
|
97
|
+
getLibrary().imageUrl(id, { width: THUMBNAIL_WIDTH, format: "webp" })
|
|
98
|
+
|
|
99
|
+
/** Large enough to judge a picture by, small enough not to send a 5000px
|
|
100
|
+
* original to a side panel. */
|
|
101
|
+
export const PREVIEW_WIDTH = 1000
|
|
102
|
+
|
|
103
|
+
export const previewUrl = (id: string): string =>
|
|
104
|
+
getLibrary().imageUrl(id, { width: PREVIEW_WIDTH, format: "webp" })
|
|
105
|
+
|
|
106
|
+
/** Which asset the library is looking at, if any.
|
|
107
|
+
*
|
|
108
|
+
* A cell rather than a prop: the grid opens the panel and the panel closes
|
|
109
|
+
* itself, and neither owns the other. It is also the seam a picker needs -
|
|
110
|
+
* choosing an asset is reading this, wherever the components are mounted.
|
|
111
|
+
*/
|
|
112
|
+
export const SELECTED = "media:selected"
|
|
113
|
+
|
|
114
|
+
/** Bumped when something changed the library behind the grid's back: an asset
|
|
115
|
+
* binned or restored from the panel. The grid reloads on it rather than
|
|
116
|
+
* learning what each action means. */
|
|
117
|
+
export const CHANGED = "media:changed"
|
|
118
|
+
|
|
119
|
+
/** Which group the library is being looked at as, if any: the grid's selected
|
|
120
|
+
* group, or the tag a host mounted it with. The inspector describes the
|
|
121
|
+
* picture for this view beside the defaults. */
|
|
122
|
+
export const VIEW = "media:view"
|
|
123
|
+
|
|
124
|
+
/** The shapes pictures are cut to, as the last page said. The grid fills it
|
|
125
|
+
* from the page's capabilities; the inspector draws its crop strip from it. */
|
|
126
|
+
export const PRESETS = "media:presets"
|
|
127
|
+
|
|
128
|
+
export const CROP_EDITOR = "media:crop-editor"
|
|
129
|
+
|
|
130
|
+
export const BIN_DIALOG = "media:bin-dialog"
|
|
131
|
+
|
|
132
|
+
/** What an upload may be when the host does not say: the types and size both
|
|
133
|
+
* hosts' servers accept today. A page's `uploadTypes` and `uploadLimit`
|
|
134
|
+
* capabilities replace them. */
|
|
135
|
+
export const UPLOAD_TYPES = ["image/jpeg", "image/png", "image/webp", "image/tiff", "image/gif", "image/avif"]
|
|
136
|
+
|
|
137
|
+
export const UPLOAD_LIMIT = 250 * 1024 * 1024
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export {
|
|
2
|
+
configure,
|
|
3
|
+
cannotChange,
|
|
4
|
+
defaultView,
|
|
5
|
+
describedFor,
|
|
6
|
+
getLibrary,
|
|
7
|
+
canWrite,
|
|
8
|
+
canDescribeView,
|
|
9
|
+
thumbnailUrl,
|
|
10
|
+
previewUrl,
|
|
11
|
+
THUMBNAIL_WIDTH,
|
|
12
|
+
PREVIEW_WIDTH,
|
|
13
|
+
SELECTED,
|
|
14
|
+
CHANGED,
|
|
15
|
+
VIEW,
|
|
16
|
+
PRESETS,
|
|
17
|
+
} from "./config.js"
|
|
18
|
+
export { snapWidth, pixelsFor, MAX_DPR, WIDTH_LADDER } from "./ladder.js"
|
|
19
|
+
export type {
|
|
20
|
+
LibraryPort,
|
|
21
|
+
LibraryPage,
|
|
22
|
+
LibraryItem,
|
|
23
|
+
LibraryQuery,
|
|
24
|
+
LibraryCapabilities,
|
|
25
|
+
EnrichResult,
|
|
26
|
+
CropRect,
|
|
27
|
+
Preset,
|
|
28
|
+
LibraryUsage,
|
|
29
|
+
UploadOutcome,
|
|
30
|
+
Swatch,
|
|
31
|
+
} from "./port.js"
|
|
32
|
+
export { CROP_EDITOR, BIN_DIALOG } from "./config.js"
|
|
33
|
+
export { UPLOAD_TYPES, UPLOAD_LIMIT } from "./config.js"
|
package/src/ladder.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** The widths a component is willing to ask for.
|
|
2
|
+
*
|
|
3
|
+
* Here rather than in the client package, because the components deliberately
|
|
4
|
+
* depend on nothing: the same grid runs against a CMS that has its own way of
|
|
5
|
+
* addressing images, and the rung is what *this component* decides to ask
|
|
6
|
+
* for - not something the Pseudolux URL grammar knows about. */
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Every distinct width is its own CDN entry and its own transform.
|
|
10
|
+
*
|
|
11
|
+
* A component that measures its container and asks for exactly that many
|
|
12
|
+
* pixels generates unbounded addresses - 387, 412, 1043 - and none of them is
|
|
13
|
+
* ever a cache hit for the next visitor.
|
|
14
|
+
*
|
|
15
|
+
* Steps of about 15%. The obvious worry is that more rungs mean a more
|
|
16
|
+
* fragmented cache, and it is mostly unfounded: **the ladder bounds the worst
|
|
17
|
+
* case, but what an asset actually accumulates is set by how many distinct box
|
|
18
|
+
* widths the site's layout produces.** Three layouts at two pixel ratios is
|
|
19
|
+
* six addresses whether this list has thirteen rungs or twenty-four. Fine
|
|
20
|
+
* rungs only cost anything on a genuinely fluid layout, which is the case
|
|
21
|
+
* where they are also saving the most.
|
|
22
|
+
*
|
|
23
|
+
* 500, 1000 and 2000 are checkpoint sizes, so those transforms read a small
|
|
24
|
+
* stored copy instead of decoding the original.
|
|
25
|
+
*/
|
|
26
|
+
export const WIDTH_LADDER = [
|
|
27
|
+
320, 370, 430, 500, 575, 660, 760, 875, 1000, 1150, 1320, 1520, 1750, 2000,
|
|
28
|
+
2300, 2650, 3050, 3500, 4000,
|
|
29
|
+
] as const
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* How much a picture may be stretched to save a whole rung.
|
|
33
|
+
*
|
|
34
|
+
* Rounding *up* is the safe direction and it is what costs the bytes: a box
|
|
35
|
+
* one pixel above a rung takes the next one and throws away a quarter of the
|
|
36
|
+
* file. Three per cent of upscaling on a photograph is not visible - the
|
|
37
|
+
* screen is usually downsampling anyway - and it saves the entire step.
|
|
38
|
+
*
|
|
39
|
+
* This one number takes the average waste from 18% to 8%. Raising it further
|
|
40
|
+
* keeps paying, but 5% starts to show on hard edges and text in an image.
|
|
41
|
+
*/
|
|
42
|
+
const TOLERANCE = 0.03
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The most device pixels we will ask for per CSS pixel.
|
|
46
|
+
*
|
|
47
|
+
* A 3× screen wants 2.25 times the pixels of a 2× one for a difference almost
|
|
48
|
+
* nobody can resolve on a photograph, and it is the single largest saving
|
|
49
|
+
* available here - larger than anything the ladder does. Capped rather than
|
|
50
|
+
* honoured, deliberately: this is a judgement about what a reader can see, not
|
|
51
|
+
* about arithmetic.
|
|
52
|
+
*/
|
|
53
|
+
export const MAX_DPR = 2
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The rung to ask for, given a measured CSS width and a device pixel ratio.
|
|
57
|
+
*
|
|
58
|
+
* The one entry point, so the cap and the tolerance cannot be applied in one
|
|
59
|
+
* component and forgotten in another.
|
|
60
|
+
*/
|
|
61
|
+
export const pixelsFor = (cssWidth: number, dpr: number): number =>
|
|
62
|
+
snapWidth(Math.round(cssWidth * Math.min(dpr || 1, MAX_DPR)))
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The rung that best covers `wanted`.
|
|
66
|
+
*
|
|
67
|
+
* Rounds up, except when the rung below is within {@link TOLERANCE} - see
|
|
68
|
+
* there for why that exception is the whole point. Past the top rung it
|
|
69
|
+
* returns the top rung: beyond 4,000 pixels the source is usually the limit
|
|
70
|
+
* anyway, and the server falls back to it.
|
|
71
|
+
*/
|
|
72
|
+
export const snapWidth = (wanted: number): number => {
|
|
73
|
+
if (!Number.isFinite(wanted) || wanted <= 0) return WIDTH_LADDER[0]
|
|
74
|
+
|
|
75
|
+
const below = [...WIDTH_LADDER].reverse().find((rung) => rung <= wanted)
|
|
76
|
+
if (below !== undefined && wanted <= below * (1 + TOLERANCE)) return below
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
WIDTH_LADDER.find((rung) => rung >= wanted) ??
|
|
80
|
+
WIDTH_LADDER[WIDTH_LADDER.length - 1]!
|
|
81
|
+
)
|
|
82
|
+
}
|