@prismicio/types-internal
Version:
Prismic types for Custom Types and Prismic Data
123 lines (122 loc) • 4.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCroppedImage = exports.buildCropUrl = exports.ImageContent = exports.ImageLegacy = exports.ImageContentView = exports.isImageContent = exports.ImageContentType = void 0;
const tslib_1 = require("tslib");
const fp_ts_1 = require("fp-ts");
const function_1 = require("fp-ts/lib/function");
const t = (0, tslib_1.__importStar)(require("io-ts"));
const Objects_1 = require("../../../utils/Objects");
const validators_1 = require("../../../validators");
const utils_1 = require("../../utils");
exports.ImageContentType = "ImageContent";
const isImageContent = (u) => (0, utils_1.hasContentType)(u) && u.__TYPE__ === exports.ImageContentType;
exports.isImageContent = isImageContent;
const originReader = t.exact(t.type({
id: t.string,
url: t.string,
width: t.number,
height: t.number,
}));
exports.ImageContentView = t.exact(t.intersection([
t.type({
origin: originReader,
width: t.number,
height: t.number,
edit: t.type({
zoom: t.number,
crop: t.type({
x: t.number,
y: t.number,
}),
background: t.string,
}),
}),
t.partial({
url: t.string,
credits: validators_1.NonEmptyStringOrNull,
alt: validators_1.NonEmptyStringOrNull,
provider: validators_1.StringOrNull,
}),
]));
const legacyReader = t.intersection([
exports.ImageContentView,
t.partial({
thumbnails: t.record(t.string, exports.ImageContentView),
}),
]);
const ImageLegacy = (ctx) => new t.Type("ImageLegacy", exports.isImageContent, (u) => {
return (0, function_1.pipe)(legacyReader.decode(u), fp_ts_1.either.map((i) => exports.ImageContent.encode({ ...i, __TYPE__: exports.ImageContentType })));
}, (i) => {
return {
content: (0, Objects_1.withOptionals)({
edit: i.edit,
height: i.height,
origin: i.origin,
width: i.width,
}, [
["alt", i.alt],
["credits", i.credits],
["provider", i.provider],
["thumbnails", i.thumbnails],
["url", i.url],
]),
types: { [ctx.keyOfType]: "Image" },
keys: {},
};
});
exports.ImageLegacy = ImageLegacy;
exports.ImageContent = t.intersection([
legacyReader,
t.strict({
__TYPE__: t.literal(exports.ImageContentType),
}),
]);
function buildCropUrl(args) {
const { origin, croppedImage } = args;
const { x, y, width, height, cropHeight, cropWidth } = croppedImage;
const url = new URL(origin.url);
const hasResize = origin.height !== height || origin.width !== width;
const hasCrop = x !== 0 ||
y !== 0 ||
cropHeight !== origin.height ||
cropWidth !== origin.width;
if (hasCrop) {
const crop = [x, y, cropWidth, cropHeight].map(Math.round).join(",");
url.searchParams.set("rect", crop);
url.searchParams.set("w", width.toString());
url.searchParams.set("h", height.toString());
}
if (hasResize) {
url.searchParams.set("w", width.toString());
url.searchParams.set("h", height.toString());
}
return url.toString();
}
exports.buildCropUrl = buildCropUrl;
function getCroppedImage({ contentView, }) {
const { edit: { crop: { x, y }, zoom, }, width, height, origin: { width: originWidth, height: originHeight }, } = contentView;
let cropWidth;
let cropHeight;
const originAspect = originWidth / originHeight;
const cropAspect = width / height;
if (cropAspect >= originAspect) {
// crop and origin image would be the same width if zoom was equal to 1
cropWidth = originWidth / zoom;
cropHeight = cropWidth / cropAspect;
}
else {
// crop and origin image would be the same height if zoom was equal to 1
cropHeight = originHeight / zoom;
cropWidth = cropHeight * cropAspect;
}
return {
x,
y,
width,
height,
zoom,
cropWidth,
cropHeight,
};
}
exports.getCroppedImage = getCroppedImage;