Skip to content

Commit

Permalink
feat: support image captions
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Mar 21, 2024
1 parent bd55071 commit a729c88
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
"devDependencies": {
"@prismicio/mock": "^0.3.1",
"@prismicio/types-internal": "2.3.0",
"@prismicio/types-internal": "^2.4.0-alpha.1",
"@size-limit/preset-small-lib": "^9.0.0",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^6.7.4",
Expand Down
1 change: 1 addition & 0 deletions src/helpers/asImageWidthSrcSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const asImageWidthSrcSet = <
alt: _alt,
copyright: _copyright,
edit: _edit,
caption: _caption,

Check failure on line 101 in src/helpers/asImageWidthSrcSet.ts

View workflow job for this annotation

GitHub Actions / Types (ubuntu-latest, Node 20, TS 4.6)

Property 'caption' does not exist on type 'FilledImageFieldImage'.

Check failure on line 101 in src/helpers/asImageWidthSrcSet.ts

View workflow job for this annotation

GitHub Actions / Types (ubuntu-latest, Node 20, TS 4.9)

Property 'caption' does not exist on type 'FilledImageFieldImage'.

Check failure on line 101 in src/helpers/asImageWidthSrcSet.ts

View workflow job for this annotation

GitHub Actions / Types (ubuntu-latest, Node 20, TS 4.8)

Property 'caption' does not exist on type 'FilledImageFieldImage'.

Check failure on line 101 in src/helpers/asImageWidthSrcSet.ts

View workflow job for this annotation

GitHub Actions / Types (ubuntu-latest, Node 20, TS 5.0)

Property 'caption' does not exist on type 'FilledImageFieldImage'.

Check failure on line 101 in src/helpers/asImageWidthSrcSet.ts

View workflow job for this annotation

GitHub Actions / Types (ubuntu-latest, Node 20, TS 4.7)

Property 'caption' does not exist on type 'FilledImageFieldImage'.
...responsiveViews
} = field;

Expand Down
1 change: 1 addition & 0 deletions src/types/model/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export interface CustomTypeModelImageField<
label?: string | null;
constraint?: CustomTypeModelImageConstraint;
thumbnails?: readonly CustomTypeModelImageThumbnail<ThumbnailNames>[];
allowCaption?: boolean;
};
}
8 changes: 6 additions & 2 deletions src/types/value/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export type ImageField<
// editor experience.
State extends "filled"
? ImageFieldImage<State> &
Record<Extract<ThumbnailNames, string>, ImageFieldImage<State>>
Record<Extract<ThumbnailNames, string>, ImageFieldImage<State>> & {
caption: string | null;
}
: ImageFieldImage<State> &
Record<Extract<ThumbnailNames, string>, ImageFieldImage<State>>
Record<Extract<ThumbnailNames, string>, ImageFieldImage<State>> & {
caption?: null;
}
>;
10 changes: 10 additions & 0 deletions test/types/customType-image.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ expectType<prismic.CustomTypeModelImageField>({
},
});

/**
* Supports optional caption.
*/
expectType<prismic.CustomTypeModelImageField>({
type: prismic.CustomTypeModelFieldType.Image,
config: {
allowCaption: true,
},
});

/**
* Supports custom thumbnail names.
*/
Expand Down
24 changes: 18 additions & 6 deletions test/types/fields-image.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ expectType<prismic.ImageField>({
edit: { x: 0, y: 0, zoom: 1, background: "background" },
alt: "alt",
copyright: "copyright",
caption: "caption",
});
expectType<prismic.ImageField<never, "filled">>({
id: "id",
Expand All @@ -36,6 +37,7 @@ expectType<prismic.ImageField<never, "filled">>({
edit: { x: 0, y: 0, zoom: 1, background: "background" },
alt: "alt",
copyright: "copyright",
caption: "caption",
});
expectType<prismic.ImageField<never, "empty">>({
// @ts-expect-error - Empty fields cannot contain a filled value.
Expand All @@ -50,6 +52,8 @@ expectType<prismic.ImageField<never, "empty">>({
alt: "alt",
// @ts-expect-error - Empty fields cannot contain a filled value.
copyright: "copyright",
// @ts-expect-error - Empty fields cannot contain a filled value.
caption: "caption",
});

/**
Expand All @@ -60,13 +64,15 @@ expectType<prismic.ImageField>({
dimensions: null,
alt: null,
copyright: null,
caption: null,
});
expectType<prismic.ImageField>({});
expectType<prismic.ImageField<never, "empty">>({
url: null,
dimensions: null,
alt: null,
copyright: null,
caption: null,
});
expectType<prismic.ImageField<never, "filled">>({
// @ts-expect-error - Filled fields cannot contain an empty value.
Expand All @@ -79,10 +85,11 @@ expectType<prismic.ImageField<never, "filled">>({
edit: null,
alt: null,
copyright: null,
caption: null,
});

/**
* Allows null alt and copyright.
* Allows null alt, copyright, and caption.
*/
expectType<prismic.ImageField>({
id: "id",
Expand All @@ -91,6 +98,7 @@ expectType<prismic.ImageField>({
edit: { x: 0, y: 0, zoom: 1, background: "background" },
alt: null,
copyright: null,
caption: null,
});

/**
Expand All @@ -101,6 +109,7 @@ expectType<prismic.ImageField>({
dimensions: { width: 1, height: 1 },
alt: "alt",
copyright: "copyright",
caption: "caption",
// @ts-expect-error - No thumbnails are included by default.
Foo: {},
});
Expand All @@ -115,6 +124,7 @@ expectType<prismic.ImageField<"Foo" | "Bar">>({
edit: { x: 0, y: 0, zoom: 1, background: "background" },
alt: "alt",
copyright: "copyright",
caption: "caption",
Foo: {
id: "id",
url: "url",
Expand Down Expand Up @@ -145,6 +155,7 @@ expectType<prismic.ImageField<never>>({
dimensions: { width: 1, height: 1 },
alt: null,
copyright: null,
caption: null,
// @ts-expect-error - No thumbnails should be included when set to `never`.
Foo: {
url: "url",
Expand All @@ -169,6 +180,7 @@ expectType<prismic.ImageField<null>>({
dimensions: { width: 1, height: 1 },
alt: null,
copyright: null,
caption: null,
// @ts-expect-error - No thumbnails should be included when set to `null`.
Foo: {
url: "url",
Expand All @@ -191,6 +203,7 @@ expectType<prismic.ImageField>({
dimensions: { width: 1, height: 1 },
alt: "alt",
copyright: "copyright",
caption: "caption",
// @ts-expect-error - `"length"` shouldn't be available as a thumbnail name by default
length: {
url: "url",
Expand All @@ -206,6 +219,7 @@ expectType<prismic.ImageField<"length">>({
edit: { x: 0, y: 0, zoom: 1, background: "background" },
alt: "alt",
copyright: "copyright",
caption: "caption",
length: {
id: "id",
url: "url",
Expand All @@ -229,13 +243,11 @@ const _ImageFieldIsValidImageFieldImageExtendDebug: prismic.ImageFieldImage =
{} as prismic.ImageField;

/**
* ImageFieldImages are valid ImageField extends with no thumbnails.
* ImageFieldImages are not valid ImageField extends, even without thumbnails.
*/
expectType<TypeOf<prismic.ImageField, prismic.ImageFieldImage>>(true);
expectType<TypeOf<prismic.ImageField<never>, prismic.ImageFieldImage>>(true);
expectType<TypeOf<prismic.ImageField, prismic.ImageFieldImage>>(false);
expectType<TypeOf<prismic.ImageField<never>, prismic.ImageFieldImage>>(false);
expectType<TypeOf<prismic.ImageField<"Foo">, prismic.ImageFieldImage>>(false);
expectType<TypeOf<prismic.ImageField<"Foo" | "Bar">, prismic.ImageFieldImage>>(
false,
);
const _ImageFieldImageIsValidImageFieldExtendDebug: prismic.ImageField =
{} as prismic.ImageFieldImage;

0 comments on commit a729c88

Please sign in to comment.