@capacms/sdk 1.0.0-next.10 → 1.0.0-next.12
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 +170 -0
- package/README.md +368 -5
- package/bin/capa-check-bundle.js +80 -0
- package/bin/capa.js +3 -3
- package/bin/graphql-project.js +1 -1
- package/dist/client.d.ts +84 -4
- package/dist/client.js +23 -5
- package/dist/esm/image/index.d.ts +69 -2
- package/dist/esm/image/index.js +108 -5
- package/dist/esm/image/shared-params.generated.d.ts +126 -2
- package/dist/esm/image/shared-params.generated.js +224 -5
- package/dist/esm/overlay/index.d.ts +4 -1
- package/dist/esm/overlay/index.js +382 -30
- package/dist/esm/overlay/live-fields.d.ts +133 -0
- package/dist/esm/overlay/live-fields.js +478 -0
- package/dist/esm/overlay/markdown.d.ts +38 -0
- package/dist/esm/overlay/markdown.js +536 -0
- package/dist/esm/overlay/protocol.d.ts +51 -5
- package/dist/esm/overlay/protocol.js +79 -9
- package/dist/image/index.d.ts +69 -2
- package/dist/image/index.js +109 -4
- package/dist/image/shared-params.generated.d.ts +126 -2
- package/dist/image/shared-params.generated.js +235 -6
- package/dist/index.d.ts +1 -1
- package/dist/next/attrs.d.ts +42 -1
- package/dist/next/attrs.js +42 -3
- package/dist/next/client.d.ts +48 -1
- package/dist/next/client.js +33 -2
- package/dist/next/entry-fields.d.ts +43 -2
- package/dist/next/graphql/request.d.ts +4 -0
- package/dist/next/graphql/request.js +4 -0
- package/dist/next/graphql/tree.js +9 -0
- package/dist/next/index.d.ts +4 -4
- package/dist/next/index.js +3 -1
- package/dist/nextjs/index.d.ts +91 -7
- package/dist/nextjs/index.js +230 -17
- package/dist/nextjs/write-key-guard.d.ts +117 -0
- package/dist/nextjs/write-key-guard.js +285 -0
- package/dist/overlay/index.d.ts +4 -1
- package/dist/overlay/index.js +399 -29
- package/dist/overlay/live-fields.d.ts +133 -0
- package/dist/overlay/live-fields.js +493 -0
- package/dist/overlay/markdown.d.ts +38 -0
- package/dist/overlay/markdown.js +539 -0
- package/dist/overlay/protocol.d.ts +51 -5
- package/dist/overlay/protocol.js +82 -9
- package/dist/server/browser.d.ts +1 -0
- package/dist/server/browser.js +20 -0
- package/dist/server/client.d.ts +268 -0
- package/dist/server/client.js +335 -0
- package/dist/server/errors.d.ts +60 -0
- package/dist/server/errors.js +120 -0
- package/dist/server/index.d.ts +14 -0
- package/dist/server/index.js +32 -0
- package/dist/server/key-format.d.ts +41 -0
- package/dist/server/key-format.js +91 -0
- package/dist/server/transport.d.ts +47 -0
- package/dist/server/transport.js +114 -0
- package/dist/webhooks.d.ts +8 -0
- package/package.json +18 -4
|
@@ -113,6 +113,13 @@ export interface ImageParams {
|
|
|
113
113
|
dpr: number;
|
|
114
114
|
upscale: boolean;
|
|
115
115
|
keepMetadata: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* `fp` and `rect` (ENG-5959), read by `parseFocalParams` and only for a
|
|
118
|
+
* project with the focal point and crop on; absent otherwise, and then the
|
|
119
|
+
* two are unknown keys like any other.
|
|
120
|
+
*/
|
|
121
|
+
focalPoint?: FocalPoint;
|
|
122
|
+
rect?: CropRect;
|
|
116
123
|
/** True when any parameter that changes the bytes was supplied. */
|
|
117
124
|
wantsTransform: boolean;
|
|
118
125
|
}
|
|
@@ -154,7 +161,16 @@ export interface CanonicalResult {
|
|
|
154
161
|
*
|
|
155
162
|
* @param rawQuery the query string as received, without the leading `?`
|
|
156
163
|
*/
|
|
157
|
-
export declare function canonicalise(rawQuery: string, params: ImageParams): CanonicalResult;
|
|
164
|
+
export declare function canonicalise(rawQuery: string, params: ImageParams, options?: CanonicalOptions): CanonicalResult;
|
|
165
|
+
export interface CanonicalOptions {
|
|
166
|
+
/**
|
|
167
|
+
* True when `fp` and `rect` are parameters here (the project has the focal
|
|
168
|
+
* point and crop on): they are then spelled from `params` like the others,
|
|
169
|
+
* and their presence enables canonicalisation. False or absent, they are
|
|
170
|
+
* unknown keys, kept verbatim, exactly as before ENG-5959.
|
|
171
|
+
*/
|
|
172
|
+
focal?: boolean;
|
|
173
|
+
}
|
|
158
174
|
/**
|
|
159
175
|
* The canonical spelling itself, without `canonicalise`'s gate.
|
|
160
176
|
*
|
|
@@ -168,7 +184,7 @@ export declare function canonicalise(rawQuery: string, params: ImageParams): Can
|
|
|
168
184
|
* @param rawQuery the query string, without the leading `?`; read only for the
|
|
169
185
|
* keys this module does not know, which are kept
|
|
170
186
|
*/
|
|
171
|
-
export declare function canonicalQuery(rawQuery: string, params: ImageParams): string;
|
|
187
|
+
export declare function canonicalQuery(rawQuery: string, params: ImageParams, options?: CanonicalOptions): string;
|
|
172
188
|
/**
|
|
173
189
|
* A stable string identifying the transform, for the ETag.
|
|
174
190
|
*
|
|
@@ -188,3 +204,111 @@ export declare function buildSignature(params: ImageParams, resolvedFormat?: Con
|
|
|
188
204
|
* preference, so an entry carrying it is skipped rather than matched.
|
|
189
205
|
*/
|
|
190
206
|
export declare function resolveAutoFormat(accept: string | undefined | null): "avif" | "webp" | null;
|
|
207
|
+
/** A focal point: fractions of the oriented image, from the left and the top. */
|
|
208
|
+
export interface FocalPoint {
|
|
209
|
+
x: number;
|
|
210
|
+
y: number;
|
|
211
|
+
}
|
|
212
|
+
/** A crop: fractions of the oriented image. */
|
|
213
|
+
export interface CropRect {
|
|
214
|
+
x: number;
|
|
215
|
+
y: number;
|
|
216
|
+
width: number;
|
|
217
|
+
height: number;
|
|
218
|
+
}
|
|
219
|
+
/** `fp` and `rect`: parameters only where the focal point and crop are on. */
|
|
220
|
+
export declare const FOCAL_PARAMS: Set<string>;
|
|
221
|
+
/** Decimal places a fraction keeps: a ten-thousandth, under a pixel up to 10,000 px. */
|
|
222
|
+
export declare const FRACTION_DECIMALS = 4;
|
|
223
|
+
/** A number rounded to FRACTION_DECIMALS. */
|
|
224
|
+
export declare function roundFraction(n: number): number;
|
|
225
|
+
/**
|
|
226
|
+
* A focal point from stored metadata or a request body, or null when it is
|
|
227
|
+
* not one: an object whose `x` and `y` are numbers from 0 to 1.
|
|
228
|
+
*/
|
|
229
|
+
export declare function readFocalPoint(value: unknown): FocalPoint | null;
|
|
230
|
+
/**
|
|
231
|
+
* A crop from stored metadata or a request body, or null when it is not one:
|
|
232
|
+
* `x`, `y`, `width` and `height` from 0 to 1, a width and a height above 0,
|
|
233
|
+
* and inside the image.
|
|
234
|
+
*/
|
|
235
|
+
export declare function readCrop(value: unknown): CropRect | null;
|
|
236
|
+
/**
|
|
237
|
+
* A focal point exactly as the api stores one (`normaliseFocalPatch` in
|
|
238
|
+
* apps/api routes/v2/file-uploads.ts): `{ x, y }` and no other key, numbers
|
|
239
|
+
* from 0 to 1. Stricter than `readFocalPoint`, which reads one out of any
|
|
240
|
+
* object that has them: this is the test for "ENG-5959 wrote this", which a
|
|
241
|
+
* replace carries over and the legacy routes hide. Anything else under the
|
|
242
|
+
* name is a project's own metadata and is left alone.
|
|
243
|
+
*/
|
|
244
|
+
export declare function isStoredFocalPoint(value: unknown): value is FocalPoint;
|
|
245
|
+
/** A crop exactly as the api stores one: `{ x, y, width, height }` and no other key. See `isStoredFocalPoint`. */
|
|
246
|
+
export declare function isStoredCrop(value: unknown): value is CropRect;
|
|
247
|
+
export type FocalParseResult = {
|
|
248
|
+
ok: true;
|
|
249
|
+
focalPoint?: FocalPoint;
|
|
250
|
+
rect?: CropRect;
|
|
251
|
+
} | {
|
|
252
|
+
ok: false;
|
|
253
|
+
rejection: ParamRejection;
|
|
254
|
+
};
|
|
255
|
+
/**
|
|
256
|
+
* `fp` and `rect`, for a project with the focal point and crop on. The route
|
|
257
|
+
* calls it after it knows the file's project; anywhere else the two are
|
|
258
|
+
* unknown keys and this is never called.
|
|
259
|
+
*/
|
|
260
|
+
export declare function parseFocalParams(query: Record<string, unknown>): FocalParseResult;
|
|
261
|
+
/** What the file says, from its `file_uploads.metadata`. */
|
|
262
|
+
export interface StoredFocal {
|
|
263
|
+
focalPoint: FocalPoint | null;
|
|
264
|
+
crop: CropRect | null;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* The crop and focal point a request applies: the URL's, else the file's,
|
|
268
|
+
* and the file's only for a `fit=cover` crop to both a width and a height.
|
|
269
|
+
* A blur placeholder keeps its own square geometry and applies only a `rect`
|
|
270
|
+
* the URL names.
|
|
271
|
+
*/
|
|
272
|
+
/**
|
|
273
|
+
* A crop to a shape: `fit=cover` with a width and a height, and no blur. The
|
|
274
|
+
* one request the file's stored focal point and crop apply to.
|
|
275
|
+
*/
|
|
276
|
+
export declare function isCoverCrop(params: ImageParams): boolean;
|
|
277
|
+
/**
|
|
278
|
+
* True when this request's bytes depend on the file's STORED focal point or
|
|
279
|
+
* crop: a cover crop that does not name both `fp` and `rect` itself. Its
|
|
280
|
+
* bytes change under the same URL when an editor saves either, so a browser
|
|
281
|
+
* must not keep them as long as an immutable variant (`GET /files/*`).
|
|
282
|
+
*/
|
|
283
|
+
export declare function followsStoredFocal(params: ImageParams): boolean;
|
|
284
|
+
export declare function appliedFocal(params: ImageParams, stored: StoredFocal | null): {
|
|
285
|
+
rect: CropRect | null;
|
|
286
|
+
focalPoint: FocalPoint | null;
|
|
287
|
+
};
|
|
288
|
+
/** A region of the oriented source, in whole pixels, for sharp's `extract`. */
|
|
289
|
+
export interface PixelRegion {
|
|
290
|
+
left: number;
|
|
291
|
+
top: number;
|
|
292
|
+
width: number;
|
|
293
|
+
height: number;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* The part of the source to cut out before the resize, or null for none.
|
|
297
|
+
*
|
|
298
|
+
* `source` is the image as a person sees it. `target` is the `fit=cover` box
|
|
299
|
+
* (width and height after dpr), or null for any other request. The crop is cut
|
|
300
|
+
* first; with a target and a focal point, the window of the target's shape
|
|
301
|
+
* that a cover resize would keep is then moved, inside the crop, so its centre
|
|
302
|
+
* is as close to the focal point as the crop allows. Without a focal point the
|
|
303
|
+
* crop alone is returned and the resize centres in it as it always has.
|
|
304
|
+
*
|
|
305
|
+
* The window is the largest of the target's shape that fits the crop, so the
|
|
306
|
+
* resize that follows scales it to the target without cutting anything more.
|
|
307
|
+
*/
|
|
308
|
+
export declare function focalRegion(source: {
|
|
309
|
+
width: number;
|
|
310
|
+
height: number;
|
|
311
|
+
}, target: {
|
|
312
|
+
width: number;
|
|
313
|
+
height: number;
|
|
314
|
+
} | null, rect: CropRect | null, focalPoint: FocalPoint | null): PixelRegion | null;
|
|
@@ -312,6 +312,18 @@ function num(n) {
|
|
|
312
312
|
function encodePart(text) {
|
|
313
313
|
return encodeURIComponent(text).replace(/'/g, "%27");
|
|
314
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* `fp` and `rect` are comma lists, spelled with bare commas: a browser sends a
|
|
317
|
+
* query's `,` as it is, so `fp=0.25,0.5` comes back byte for byte, and reads
|
|
318
|
+
* better than `fp=0.25%2C0.5`. Only where they are parameters; an unknown key
|
|
319
|
+
* keeps `encodePart`'s spelling, as before.
|
|
320
|
+
*/
|
|
321
|
+
function listValue(key, options) {
|
|
322
|
+
return options.focal === true && FOCAL_PARAMS.has(key);
|
|
323
|
+
}
|
|
324
|
+
function encodeList(value) {
|
|
325
|
+
return value.split(",").map(encodePart).join(",");
|
|
326
|
+
}
|
|
315
327
|
/**
|
|
316
328
|
* The canonical spelling of a query, so `?width=200&format=webp` and
|
|
317
329
|
* `?format=webp&width=200` are one edge object rather than two.
|
|
@@ -324,12 +336,12 @@ function encodePart(text) {
|
|
|
324
336
|
*
|
|
325
337
|
* @param rawQuery the query string as received, without the leading `?`
|
|
326
338
|
*/
|
|
327
|
-
export function canonicalise(rawQuery, params) {
|
|
339
|
+
export function canonicalise(rawQuery, params, options = {}) {
|
|
328
340
|
const received = new URLSearchParams(rawQuery);
|
|
329
|
-
const usesNewParam = [...received.keys()].some((k) => NEW_PARAMS.has(k));
|
|
341
|
+
const usesNewParam = [...received.keys()].some((k) => NEW_PARAMS.has(k) || (options.focal === true && FOCAL_PARAMS.has(k)));
|
|
330
342
|
if (!usesNewParam)
|
|
331
343
|
return { query: rawQuery, isCanonical: true };
|
|
332
|
-
const query = canonicalQuery(rawQuery, params);
|
|
344
|
+
const query = canonicalQuery(rawQuery, params, options);
|
|
333
345
|
return { query, isCanonical: query === rawQuery };
|
|
334
346
|
}
|
|
335
347
|
/**
|
|
@@ -345,7 +357,7 @@ export function canonicalise(rawQuery, params) {
|
|
|
345
357
|
* @param rawQuery the query string, without the leading `?`; read only for the
|
|
346
358
|
* keys this module does not know, which are kept
|
|
347
359
|
*/
|
|
348
|
-
export function canonicalQuery(rawQuery, params) {
|
|
360
|
+
export function canonicalQuery(rawQuery, params, options = {}) {
|
|
349
361
|
const received = new URLSearchParams(rawQuery);
|
|
350
362
|
const keys = [...new Set([...received.keys()])];
|
|
351
363
|
const out = new Map();
|
|
@@ -377,6 +389,12 @@ export function canonicalQuery(rawQuery, params) {
|
|
|
377
389
|
out.set("upscale", "1");
|
|
378
390
|
if (params.keepMetadata)
|
|
379
391
|
out.set("keepMetadata", "1");
|
|
392
|
+
if (options.focal === true) {
|
|
393
|
+
if (params.focalPoint !== undefined)
|
|
394
|
+
out.set("fp", fractions([params.focalPoint.x, params.focalPoint.y]));
|
|
395
|
+
if (params.rect !== undefined)
|
|
396
|
+
out.set("rect", fractions([params.rect.x, params.rect.y, params.rect.width, params.rect.height]));
|
|
397
|
+
}
|
|
380
398
|
// Unknown keys are preserved verbatim (first value wins, repeats collapse),
|
|
381
399
|
// so a cache-buster survives the redirect instead of being silently dropped.
|
|
382
400
|
//
|
|
@@ -386,6 +404,8 @@ export function canonicalQuery(rawQuery, params) {
|
|
|
386
404
|
for (const key of keys) {
|
|
387
405
|
if (LEGACY_PARAMS.has(key) || NEW_PARAMS.has(key))
|
|
388
406
|
continue;
|
|
407
|
+
if (options.focal === true && FOCAL_PARAMS.has(key))
|
|
408
|
+
continue;
|
|
389
409
|
const value = received.get(key) ?? "";
|
|
390
410
|
if (key === "" && value === "")
|
|
391
411
|
continue;
|
|
@@ -393,7 +413,7 @@ export function canonicalQuery(rawQuery, params) {
|
|
|
393
413
|
}
|
|
394
414
|
const sorted = [...out.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
395
415
|
return sorted
|
|
396
|
-
.map(([k, v]) => (v === "" ? encodePart(k) : `${encodePart(k)}=${encodePart(v)}`))
|
|
416
|
+
.map(([k, v]) => (v === "" ? encodePart(k) : `${encodePart(k)}=${listValue(k, options) ? encodeList(v) : encodePart(v)}`))
|
|
397
417
|
.join("&");
|
|
398
418
|
}
|
|
399
419
|
/**
|
|
@@ -424,6 +444,16 @@ export function buildSignature(params, resolvedFormat) {
|
|
|
424
444
|
parts.push(`q${params.quality}`);
|
|
425
445
|
if (params.keepMetadata)
|
|
426
446
|
parts.push("meta");
|
|
447
|
+
// The crop and the focal point this transform applies (`appliedFocal`), from
|
|
448
|
+
// the URL or from the file, so a new focal point on the file is a new ETag
|
|
449
|
+
// under the same URL. Colons, not the URL's commas: If-None-Match is a
|
|
450
|
+
// comma-separated list, and an ETag with a comma in it never matches its
|
|
451
|
+
// own revalidation (files.ts ifNoneMatchSatisfied), so every one would be a
|
|
452
|
+
// full 200.
|
|
453
|
+
if (params.rect !== undefined)
|
|
454
|
+
parts.push(`r${fractions([params.rect.x, params.rect.y, params.rect.width, params.rect.height], ":")}`);
|
|
455
|
+
if (params.focalPoint !== undefined)
|
|
456
|
+
parts.push(`p${fractions([params.focalPoint.x, params.focalPoint.y], ":")}`);
|
|
427
457
|
return parts.join("_") || "raw";
|
|
428
458
|
}
|
|
429
459
|
/**
|
|
@@ -459,3 +489,192 @@ export function resolveAutoFormat(accept) {
|
|
|
459
489
|
return "webp";
|
|
460
490
|
return null;
|
|
461
491
|
}
|
|
492
|
+
/** `fp` and `rect`: parameters only where the focal point and crop are on. */
|
|
493
|
+
export const FOCAL_PARAMS = new Set(["fp", "rect"]);
|
|
494
|
+
/** Decimal places a fraction keeps: a ten-thousandth, under a pixel up to 10,000 px. */
|
|
495
|
+
export const FRACTION_DECIMALS = 4;
|
|
496
|
+
/** Slack for `x + width <= 1` after rounding to FRACTION_DECIMALS. */
|
|
497
|
+
const FRACTION_EPSILON = 1e-9;
|
|
498
|
+
/** A number rounded to FRACTION_DECIMALS. */
|
|
499
|
+
export function roundFraction(n) {
|
|
500
|
+
const scale = 10 ** FRACTION_DECIMALS;
|
|
501
|
+
return Math.round(n * scale) / scale;
|
|
502
|
+
}
|
|
503
|
+
function fraction(value) {
|
|
504
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1)
|
|
505
|
+
return null;
|
|
506
|
+
return roundFraction(value);
|
|
507
|
+
}
|
|
508
|
+
function plainObject(value) {
|
|
509
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* A focal point from stored metadata or a request body, or null when it is
|
|
513
|
+
* not one: an object whose `x` and `y` are numbers from 0 to 1.
|
|
514
|
+
*/
|
|
515
|
+
export function readFocalPoint(value) {
|
|
516
|
+
const v = plainObject(value);
|
|
517
|
+
if (!v)
|
|
518
|
+
return null;
|
|
519
|
+
const x = fraction(v.x);
|
|
520
|
+
const y = fraction(v.y);
|
|
521
|
+
return x === null || y === null ? null : { x, y };
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* A crop from stored metadata or a request body, or null when it is not one:
|
|
525
|
+
* `x`, `y`, `width` and `height` from 0 to 1, a width and a height above 0,
|
|
526
|
+
* and inside the image.
|
|
527
|
+
*/
|
|
528
|
+
export function readCrop(value) {
|
|
529
|
+
const v = plainObject(value);
|
|
530
|
+
if (!v)
|
|
531
|
+
return null;
|
|
532
|
+
const x = fraction(v.x);
|
|
533
|
+
const y = fraction(v.y);
|
|
534
|
+
const width = fraction(v.width);
|
|
535
|
+
const height = fraction(v.height);
|
|
536
|
+
if (x === null || y === null || width === null || height === null)
|
|
537
|
+
return null;
|
|
538
|
+
if (width <= 0 || height <= 0)
|
|
539
|
+
return null;
|
|
540
|
+
if (x + width > 1 + FRACTION_EPSILON || y + height > 1 + FRACTION_EPSILON)
|
|
541
|
+
return null;
|
|
542
|
+
return { x, y, width, height };
|
|
543
|
+
}
|
|
544
|
+
/** True when `value` has exactly these own keys, in any order. */
|
|
545
|
+
function hasExactly(value, keys) {
|
|
546
|
+
const own = Object.keys(value);
|
|
547
|
+
return own.length === keys.length && keys.every((key) => own.includes(key));
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* A focal point exactly as the api stores one (`normaliseFocalPatch` in
|
|
551
|
+
* apps/api routes/v2/file-uploads.ts): `{ x, y }` and no other key, numbers
|
|
552
|
+
* from 0 to 1. Stricter than `readFocalPoint`, which reads one out of any
|
|
553
|
+
* object that has them: this is the test for "ENG-5959 wrote this", which a
|
|
554
|
+
* replace carries over and the legacy routes hide. Anything else under the
|
|
555
|
+
* name is a project's own metadata and is left alone.
|
|
556
|
+
*/
|
|
557
|
+
export function isStoredFocalPoint(value) {
|
|
558
|
+
const v = plainObject(value);
|
|
559
|
+
return v !== null && hasExactly(v, ["x", "y"]) && readFocalPoint(v) !== null;
|
|
560
|
+
}
|
|
561
|
+
/** A crop exactly as the api stores one: `{ x, y, width, height }` and no other key. See `isStoredFocalPoint`. */
|
|
562
|
+
export function isStoredCrop(value) {
|
|
563
|
+
const v = plainObject(value);
|
|
564
|
+
return v !== null && hasExactly(v, ["x", "y", "width", "height"]) && readCrop(v) !== null;
|
|
565
|
+
}
|
|
566
|
+
/** Numbers as the canonical URL spells them: comma separated, no trailing zeros. */
|
|
567
|
+
function fractions(values, separator = ",") {
|
|
568
|
+
return values.map((n) => String(roundFraction(n))).join(separator);
|
|
569
|
+
}
|
|
570
|
+
/** `0.25,0.5` into numbers, or null when any part is not a plain decimal. */
|
|
571
|
+
function decimalList(text, count) {
|
|
572
|
+
const parts = text.split(",");
|
|
573
|
+
if (parts.length !== count)
|
|
574
|
+
return null;
|
|
575
|
+
const out = [];
|
|
576
|
+
for (const part of parts) {
|
|
577
|
+
const n = strictNumber(part);
|
|
578
|
+
if (n === null)
|
|
579
|
+
return null;
|
|
580
|
+
out.push(n);
|
|
581
|
+
}
|
|
582
|
+
return out;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* `fp` and `rect`, for a project with the focal point and crop on. The route
|
|
586
|
+
* calls it after it knows the file's project; anywhere else the two are
|
|
587
|
+
* unknown keys and this is never called.
|
|
588
|
+
*/
|
|
589
|
+
export function parseFocalParams(query) {
|
|
590
|
+
const reject = (error, param) => ({ ok: false, rejection: { error, param } });
|
|
591
|
+
const fpRaw = single(query.fp, "fp");
|
|
592
|
+
if (!fpRaw.ok)
|
|
593
|
+
return { ok: false, rejection: fpRaw.rejection };
|
|
594
|
+
let focalPoint;
|
|
595
|
+
if (fpRaw.value !== undefined) {
|
|
596
|
+
const list = decimalList(fpRaw.value, 2);
|
|
597
|
+
const read = list ? readFocalPoint({ x: list[0], y: list[1] }) : null;
|
|
598
|
+
if (!read)
|
|
599
|
+
return reject("`fp` must be two numbers from 0 to 1, x,y", "fp");
|
|
600
|
+
focalPoint = read;
|
|
601
|
+
}
|
|
602
|
+
const rectRaw = single(query.rect, "rect");
|
|
603
|
+
if (!rectRaw.ok)
|
|
604
|
+
return { ok: false, rejection: rectRaw.rejection };
|
|
605
|
+
let rect;
|
|
606
|
+
if (rectRaw.value !== undefined) {
|
|
607
|
+
const list = decimalList(rectRaw.value, 4);
|
|
608
|
+
const read = list ? readCrop({ x: list[0], y: list[1], width: list[2], height: list[3] }) : null;
|
|
609
|
+
if (!read) {
|
|
610
|
+
return reject("`rect` must be four numbers from 0 to 1, x,y,width,height, with a width and a height above 0, inside the image", "rect");
|
|
611
|
+
}
|
|
612
|
+
rect = read;
|
|
613
|
+
}
|
|
614
|
+
return { ok: true, focalPoint, rect };
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* The crop and focal point a request applies: the URL's, else the file's,
|
|
618
|
+
* and the file's only for a `fit=cover` crop to both a width and a height.
|
|
619
|
+
* A blur placeholder keeps its own square geometry and applies only a `rect`
|
|
620
|
+
* the URL names.
|
|
621
|
+
*/
|
|
622
|
+
/**
|
|
623
|
+
* A crop to a shape: `fit=cover` with a width and a height, and no blur. The
|
|
624
|
+
* one request the file's stored focal point and crop apply to.
|
|
625
|
+
*/
|
|
626
|
+
export function isCoverCrop(params) {
|
|
627
|
+
return params.fit === "cover" && params.width !== undefined && params.height !== undefined && params.blur === undefined;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* True when this request's bytes depend on the file's STORED focal point or
|
|
631
|
+
* crop: a cover crop that does not name both `fp` and `rect` itself. Its
|
|
632
|
+
* bytes change under the same URL when an editor saves either, so a browser
|
|
633
|
+
* must not keep them as long as an immutable variant (`GET /files/*`).
|
|
634
|
+
*/
|
|
635
|
+
export function followsStoredFocal(params) {
|
|
636
|
+
return isCoverCrop(params) && (params.focalPoint === undefined || params.rect === undefined);
|
|
637
|
+
}
|
|
638
|
+
export function appliedFocal(params, stored) {
|
|
639
|
+
const cover = isCoverCrop(params);
|
|
640
|
+
const rect = params.rect ?? (cover ? stored?.crop ?? null : null);
|
|
641
|
+
const focalPoint = cover ? params.focalPoint ?? stored?.focalPoint ?? null : null;
|
|
642
|
+
return { rect, focalPoint };
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* The part of the source to cut out before the resize, or null for none.
|
|
646
|
+
*
|
|
647
|
+
* `source` is the image as a person sees it. `target` is the `fit=cover` box
|
|
648
|
+
* (width and height after dpr), or null for any other request. The crop is cut
|
|
649
|
+
* first; with a target and a focal point, the window of the target's shape
|
|
650
|
+
* that a cover resize would keep is then moved, inside the crop, so its centre
|
|
651
|
+
* is as close to the focal point as the crop allows. Without a focal point the
|
|
652
|
+
* crop alone is returned and the resize centres in it as it always has.
|
|
653
|
+
*
|
|
654
|
+
* The window is the largest of the target's shape that fits the crop, so the
|
|
655
|
+
* resize that follows scales it to the target without cutting anything more.
|
|
656
|
+
*/
|
|
657
|
+
export function focalRegion(source, target, rect, focalPoint) {
|
|
658
|
+
const W = Math.max(1, Math.round(source.width));
|
|
659
|
+
const H = Math.max(1, Math.round(source.height));
|
|
660
|
+
let region = { left: 0, top: 0, width: W, height: H };
|
|
661
|
+
if (rect) {
|
|
662
|
+
const left = Math.min(W - 1, Math.round(rect.x * W));
|
|
663
|
+
const top = Math.min(H - 1, Math.round(rect.y * H));
|
|
664
|
+
region = {
|
|
665
|
+
left,
|
|
666
|
+
top,
|
|
667
|
+
width: Math.max(1, Math.min(W - left, Math.round(rect.width * W))),
|
|
668
|
+
height: Math.max(1, Math.min(H - top, Math.round(rect.height * H))),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
if (!target || !focalPoint)
|
|
672
|
+
return rect ? region : null;
|
|
673
|
+
const scale = Math.max(target.width / region.width, target.height / region.height);
|
|
674
|
+
const width = Math.max(1, Math.min(region.width, Math.round(target.width / scale)));
|
|
675
|
+
const height = Math.max(1, Math.min(region.height, Math.round(target.height / scale)));
|
|
676
|
+
const clamp = (value, lo, hi) => Math.min(hi, Math.max(lo, value));
|
|
677
|
+
const left = clamp(Math.round(focalPoint.x * W - width / 2), region.left, region.left + region.width - width);
|
|
678
|
+
const top = clamp(Math.round(focalPoint.y * H - height / 2), region.top, region.top + region.height - height);
|
|
679
|
+
return { left, top, width, height };
|
|
680
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
export { acceptMessage, hoverMessage, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, textAround, visibleMessage, ADMIN_SOURCE, PATCH_MAX_FIELDS, PATCH_MAX_LENGTH, PROTOCOL_VERSION, SITE_FEATURES, SITE_SOURCE, } from "./protocol.js";
|
|
1
|
+
export { acceptMessage, hoverMessage, isItemPath, itemLabel, itemMatches, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, textAround, visibleMessage, ADMIN_SOURCE, PATCH_MAX_FIELDS, PATCH_MAX_LENGTH, PROTOCOL_VERSION, SITE_FEATURES, SITE_SOURCE, } from "./protocol.js";
|
|
2
2
|
export type { AdminMessage, MessageLike, PatchField, SiteMessage, TaggedBox } from "./protocol.js";
|
|
3
|
+
export { acceptLiveFields, canonicalHtml, findDateFormat, formatDateAs, parseHtml, preparedHtml, richTextHtml, safeHref, safeImageUrl, sanitizeHtml, swapDatetime, swapImageUrl, FIELDS_MAX_FIELDS, FIELDS_MAX_LENGTH, } from "./live-fields.js";
|
|
4
|
+
export { renderMarkdown } from "./markdown.js";
|
|
5
|
+
export type { HtmlElement, HtmlNode, LiveField, LiveImage, LiveKind } from "./live-fields.js";
|
|
3
6
|
export interface OverlayOptions {
|
|
4
7
|
/**
|
|
5
8
|
* The Capa admin origins allowed to drive this page, for example
|