@prismicio/types-internal 2.2.0-alpha.6 → 2.2.0-alpha.8

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.
@@ -1,20 +1,10 @@
1
1
  import type { Asset, AssetId } from "../../../../common"
2
2
  import { getAssetOrThrow } from "../../../../common"
3
3
  import type { ImageContent } from "../../../../content"
4
- import type {
5
- ImageConstraint,
6
- StaticWidget,
7
- Thumbnail,
8
- } from "../../../../customtypes"
9
4
  import { withOptionals } from "../../../../utils/Objects"
10
5
  import type { ImageField, ImportImage } from "../../../validators"
11
6
 
12
- function convertImage(
13
- imageField: ImageField | undefined,
14
- edit: ImageField["edit"] | undefined,
15
- constraints: ImageConstraint | undefined,
16
- image: Asset,
17
- ) {
7
+ function convertImage(imageField: ImageField, image: Asset) {
18
8
  return withOptionals<Omit<ImageContent, "__TYPE__" | "thumbnails">>(
19
9
  {
20
10
  origin: {
@@ -25,19 +15,19 @@ function convertImage(
25
15
  height: image.height ?? 0,
26
16
  },
27
17
  // All images returned form Asset API should have width and height properties.
28
- // Edit can only overwrite width and height if there are no constraints set
29
- width: constraints?.width ?? edit?.width ?? image.width ?? 0,
30
- height: constraints?.height ?? edit?.height ?? image.height ?? 0,
18
+ // Image width and height will be only applied if there are no constraints and user didn't overwrite it
19
+ width: imageField?.width ?? image.width ?? 0,
20
+ height: imageField?.height ?? image.height ?? 0,
31
21
  // If edit is not provided, we crop constraint width and height from the left upper corner.
32
22
  // WARN: If constraints are greater than image dimensions cut outside the image (background will fill extra space).
33
23
  edit: {
34
- zoom: edit?.zoom ?? 1,
24
+ zoom: imageField.edit?.zoom ?? 1,
35
25
  crop: {
36
- x: edit?.x ?? 0,
37
- y: edit?.y ?? 0,
26
+ x: imageField.edit?.x ?? 0,
27
+ y: imageField.edit?.y ?? 0,
38
28
  },
39
29
  background:
40
- edit?.background ??
30
+ imageField.edit?.background ??
41
31
  (image.extension === "png" ? "transparent" : "#ffffff"),
42
32
  },
43
33
  url: image.url,
@@ -49,81 +39,35 @@ function convertImage(
49
39
  )
50
40
  }
51
41
 
52
- // If image field contains thumbnailName definition use it to generate thumbnail
53
- // If image field is missing thumbnailName derive thumbnail from main image
54
- // WARN: edit is not inherited from main image as each thumbnail have different constraints
55
- function convertThumbnail(
56
- thumbnailName: string,
57
- thumbnailConstraints: ImageConstraint | undefined,
58
- imageField: Exclude<ImportImage["value"], null>,
59
- assets: Record<AssetId, Asset>,
60
- ) {
61
- const maybeThumbnail = imageField.thumbnails[thumbnailName]
62
- if (maybeThumbnail) {
63
- return convertImage(
64
- maybeThumbnail,
65
- maybeThumbnail.edit,
66
- thumbnailConstraints,
67
- getAssetOrThrow(assets)(maybeThumbnail.id),
68
- )
69
- } else {
70
- return convertImage(
71
- imageField,
72
- undefined,
73
- thumbnailConstraints,
74
- getAssetOrThrow(assets)(imageField.id),
75
- )
76
- }
77
- }
78
-
79
42
  function convertThumbnails(
80
43
  imageField: Exclude<ImportImage["value"], null>,
81
44
  assets: Record<AssetId, Asset>,
82
- thumbnailsModel: readonly Thumbnail[],
83
45
  ) {
84
- return thumbnailsModel.reduce<
85
- Record<string, ReturnType<typeof convertImage>>
86
- >((acc, { name, ...constraints }) => {
87
- return {
88
- ...acc,
89
- [name]: convertThumbnail(name, constraints, imageField, assets),
90
- }
91
- }, {})
46
+ return Object.entries(imageField.thumbnails).reduce(
47
+ (acc, [thumbnailName, thumbnail]) => {
48
+ return {
49
+ ...acc,
50
+ [thumbnailName]: convertImage(
51
+ thumbnail,
52
+ getAssetOrThrow(assets)(thumbnail.id),
53
+ ),
54
+ }
55
+ },
56
+ {},
57
+ )
92
58
  }
93
59
 
94
- // Function constraints:
95
- // * `model` is required
96
- // * `mode` is of type Image
97
- // * All assets from `imageField` must be present in `assets`
98
- // If any of these constraints is not met, function will throw an error
60
+ // All assets from `imageField` must be present in `assets`
61
+ // If not then function will throw an error
99
62
  export const imageConverter = (
100
63
  imageField: ImportImage["value"],
101
64
  assets: Record<AssetId, Asset>,
102
- model?: StaticWidget,
103
65
  ): ImageContent | undefined => {
104
- if (!model) {
105
- throw new Error(`'model' parameter is required!`)
106
- }
107
- if (model.type !== "Image") {
108
- throw new Error(
109
- `Wrong model type! Expected 'Image' but got '${model.type}'`,
110
- )
111
- }
112
-
113
66
  if (!imageField) return
114
67
 
115
68
  return {
116
- ...convertImage(
117
- imageField,
118
- imageField.edit,
119
- model.config?.constraint,
120
- getAssetOrThrow(assets)(imageField.id),
121
- ),
122
- thumbnails: convertThumbnails(
123
- imageField,
124
- assets,
125
- model.config?.thumbnails ?? [],
126
- ),
69
+ ...convertImage(imageField, getAssetOrThrow(assets)(imageField.id)),
70
+ thumbnails: convertThumbnails(imageField, assets),
127
71
  __TYPE__: "ImageContent" as const,
128
72
  }
129
73
  }
@@ -1,6 +1,5 @@
1
1
  import type { Asset, AssetId } from "../../../../common"
2
2
  import type { WidgetContent } from "../../../../content"
3
- import type { StaticWidget } from "../../../../customtypes"
4
3
  import type { ImportNestable } from "../../../validators"
5
4
  import {
6
5
  booleanConverter,
@@ -19,7 +18,6 @@ import {
19
18
  export function convertNestableWidget(
20
19
  field: ImportNestable,
21
20
  assets: Record<AssetId, Asset>,
22
- model?: StaticWidget,
23
21
  ): WidgetContent | undefined {
24
22
  switch (field.type) {
25
23
  case "Boolean":
@@ -43,7 +41,7 @@ export function convertNestableWidget(
43
41
  case "Link":
44
42
  return linkConverter(field.value, assets)
45
43
  case "Image":
46
- return imageConverter(field.value, assets, model)
44
+ return imageConverter(field.value, assets)
47
45
  default:
48
46
  throw new Error(
49
47
  `Unsupported type of nestable converter ${JSON.stringify(field)}`,
@@ -3,23 +3,24 @@ import { pipe } from "fp-ts/lib/function"
3
3
  import type { Validation } from "io-ts"
4
4
  import * as t from "io-ts"
5
5
 
6
- import { withOptionals } from "../../../../utils/Objects"
6
+ import type { Image } from "../../../../customtypes"
7
+ import type ImageConstraint from "../../../../customtypes/widgets/shared/ImageConstraint"
7
8
  import { AnyObject, NullOrElse, Number } from "../../../../validators"
8
- import { String, StringOrNull } from "../../../../validators/BasicTypes"
9
+ import { String, StringOrNull } from "../../../../validators"
9
10
  import { ImportContent } from "../ImportContent"
10
11
 
11
- const ImageFieldCodec = AnyObject.pipe(
12
+ const ImageFieldValidator = AnyObject.pipe(
12
13
  t.intersection([
13
14
  t.type({
14
15
  id: String,
15
16
  }),
16
17
  t.partial({
18
+ width: Number,
19
+ height: Number,
17
20
  edit: AnyObject.pipe(
18
21
  t.partial({
19
22
  x: Number,
20
23
  y: Number,
21
- width: Number,
22
- height: Number,
23
24
  zoom: Number,
24
25
  background: String,
25
26
  }),
@@ -30,48 +31,98 @@ const ImageFieldCodec = AnyObject.pipe(
30
31
  ]),
31
32
  )
32
33
 
33
- const ThumbnailsCodec = t.record(t.string, ImageFieldCodec)
34
- type Thumbnails = t.TypeOf<typeof ThumbnailsCodec>
34
+ const ThumbnailsValidator = t.record(t.string, ImageFieldValidator)
35
35
 
36
- export type ImageField = t.TypeOf<typeof ImageFieldCodec>
36
+ export type ImageField = {
37
+ id: string
38
+ width?: number
39
+ height?: number
40
+ edit: {
41
+ x: number
42
+ y: number
43
+ zoom: number
44
+ background?: string
45
+ }
46
+ credit?: string | null
47
+ alt?: string | null
48
+ }
49
+ type ImageFieldWithThumbnails = ImageField & {
50
+ thumbnails: Record<string, ImageField>
51
+ }
37
52
 
38
- type ImageFieldWithThumbnails = ImageField & { thumbnails: Thumbnails }
53
+ const encodeImageField = (
54
+ image: t.TypeOf<typeof ImageFieldValidator> | undefined,
55
+ mainImage: t.TypeOf<typeof ImageFieldValidator>,
56
+ constraints?: ImageConstraint,
57
+ ): ImageField => {
58
+ const background = image?.edit?.background
59
+ const width = constraints?.width ?? image?.width
60
+ const height = constraints?.height ?? image?.height
61
+ const alt = image?.alt
62
+ const credit = image?.credit
39
63
 
40
- const ImageFieldWithThumbnails = new t.Type(
41
- "ImageFieldWithThumbnails",
42
- (u: unknown): u is ImageFieldWithThumbnails =>
43
- ImageFieldCodec.is(u) &&
44
- "thumbnails" in u &&
45
- ThumbnailsCodec.is(u["thumbnails"]),
46
- (u: unknown, ctx): Validation<ImageFieldWithThumbnails> => {
47
- return pipe(
48
- ImageFieldCodec.validate(u, ctx),
49
- chain((proto) => {
50
- const { id, edit, credit, alt, ...maybeThumbnails } = proto
51
- return pipe(
52
- ThumbnailsCodec.validate(maybeThumbnails, ctx),
53
- map((thumbnails) =>
54
- withOptionals<ImageFieldWithThumbnails>(
55
- {
56
- id,
57
- thumbnails,
58
- },
59
- [
60
- ["edit", edit],
61
- ["credit", credit],
62
- ["alt", alt],
63
- ],
64
- ),
65
- ),
66
- )
67
- }),
68
- )
69
- },
70
- t.identity,
71
- )
64
+ return {
65
+ id: image?.id ?? mainImage?.id,
66
+ edit: {
67
+ x: image?.edit?.x ?? 0,
68
+ y: image?.edit?.y ?? 0,
69
+ zoom: image?.edit?.zoom ?? 1,
70
+ ...(background !== undefined ? { background } : {}),
71
+ },
72
+ ...(width !== undefined ? { width } : {}),
73
+ ...(height !== undefined ? { height } : {}),
74
+ ...(alt !== undefined ? { alt } : {}),
75
+ ...(credit !== undefined ? { credit } : {}),
76
+ }
77
+ }
72
78
 
73
- export const ImportImage = ImportContent(
74
- "Image",
75
- NullOrElse(ImageFieldWithThumbnails),
76
- )
77
- export type ImportImage = t.TypeOf<typeof ImportImage>
79
+ const encodeThumbnails = (
80
+ mainImage: t.TypeOf<typeof ImageFieldValidator>,
81
+ thumbnails: t.TypeOf<typeof ThumbnailsValidator>,
82
+ field?: Image,
83
+ ) =>
84
+ field?.config?.thumbnails?.reduce(
85
+ (acc, thumbnail) => ({
86
+ ...acc,
87
+ [thumbnail.name]: encodeImageField(
88
+ thumbnails[thumbnail.name],
89
+ mainImage,
90
+ thumbnail,
91
+ ),
92
+ }),
93
+ {},
94
+ ) ?? {}
95
+
96
+ const ImageFieldWithThumbnails = (field?: Image) =>
97
+ new t.Type<ImageFieldWithThumbnails>(
98
+ "ImageFieldWithThumbnails",
99
+ (u: unknown): u is ImageFieldWithThumbnails =>
100
+ ImageFieldValidator.is(u) &&
101
+ "thumbnails" in u &&
102
+ ThumbnailsValidator.is(u["thumbnails"]),
103
+ (u: unknown, ctx): Validation<ImageFieldWithThumbnails> => {
104
+ return pipe(
105
+ ImageFieldValidator.validate(u, ctx),
106
+ chain((mainImage) => {
107
+ const { id, width, height, edit, credit, alt, ...maybeThumbnails } =
108
+ mainImage
109
+ return pipe(
110
+ ThumbnailsValidator.validate(maybeThumbnails, ctx),
111
+ map((thumbnails) => ({
112
+ ...encodeImageField(
113
+ mainImage,
114
+ mainImage,
115
+ field?.config?.constraint,
116
+ ),
117
+ thumbnails: encodeThumbnails(mainImage, thumbnails, field),
118
+ })),
119
+ )
120
+ }),
121
+ )
122
+ },
123
+ t.identity,
124
+ )
125
+
126
+ export const ImportImage = (field?: Image) =>
127
+ ImportContent("Image", NullOrElse(ImageFieldWithThumbnails(field)))
128
+ export type ImportImage = t.TypeOf<ReturnType<typeof ImportImage>>
@@ -37,7 +37,7 @@ export const ImportNestable = {
37
37
  ImportEmbed.is(u) ||
38
38
  ImportLink.is(u) ||
39
39
  ImportGeoPoint.is(u) ||
40
- ImportImage.is(u)
40
+ ImportImage().is(u)
41
41
  )
42
42
  },
43
43
  decode: (field: NestableWidget) => {
@@ -63,7 +63,7 @@ export const ImportNestable = {
63
63
  case "Link":
64
64
  return ImportLink
65
65
  case "Image":
66
- return ImportImage
66
+ return ImportImage(field)
67
67
  case "GeoPoint":
68
68
  return ImportGeoPoint
69
69
  default: