Skip to content

Commit

Permalink
feat(customerCase): image block
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Oct 17, 2024
1 parent abcb145 commit 89fe792
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
35 changes: 35 additions & 0 deletions studio/schemas/fields/media.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { StringInputProps, defineField } from "sanity";

import { StringInputWithCharacterCount } from "studio/components/stringInputWithCharacterCount/StringInputWithCharacterCount";
import { isInternationalizedString } from "studio/lib/interfaces/global";
import { firstTranslation } from "studio/utils/i18n";

export enum ImageAlignment {
Left = "left",
Expand Down Expand Up @@ -30,6 +32,14 @@ const imageAltField = defineField({
},
});

const internationalizedImageAltField = defineField({
name: "alt",
type: "internationalizedArrayString",
title: "Alternative Text",
description:
"Provide a description of the image for accessibility. Leave empty if the image is purely decorative.",
});

const image = defineField({
name: "image",
title: "Image",
Expand All @@ -38,6 +48,31 @@ const image = defineField({
fields: [imageAltField],
});

export const internationalizedImage = defineField({
name: "image",
title: "Image",
type: "image",
options: { hotspot: true },
fields: [internationalizedImageAltField],
preview: {
select: {
alt: "alt",
media: "asset",
},
prepare({ alt, media }) {
if (!isInternationalizedString(alt)) {
throw new TypeError(
`Expected 'alt' to be InternationalizedString, was ${typeof alt}`,
);
}
return {
title: firstTranslation(alt) ?? undefined,
media,
};
},
},
});

export const imageExtended = defineField({
name: "imageExtended",
title: "Extended Image",
Expand Down
7 changes: 6 additions & 1 deletion studioShared/lib/queries/customerCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ export const CUSTOMER_CASE_QUERY = groq`
"sections": sections[] {
_type,
_type == "richTextBlock" => {
...,
"richText": ${translatedFieldFragment("richText")},
},
_type == "imageBlock" => {
"images": images[] {
asset,
"alt": ${translatedFieldFragment("alt")}
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions studioShared/schemas/documents/customerCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { richTextID, titleID } from "studio/schemas/fields/text";
import { titleSlug } from "studio/schemas/schemaTypes/slug";
import { firstTranslation } from "studio/utils/i18n";
import { customerCaseProjectInfo } from "studioShared/schemas/fields/customerCaseProjectInfo";
import imageBlock from "studioShared/schemas/objects/imageBlock";
import richTextBlock from "studioShared/schemas/objects/richTextBlock";

export const customerCaseID = "customerCase";
Expand All @@ -30,13 +31,18 @@ const customerCase = defineType({
description:
"Short paragraph displayed at the top of the customer case page",
}),
customerCaseProjectInfo,
defineField({
...customerCaseProjectInfo,
options: {
collapsible: true,
},
}),
defineField({
name: "sections",
title: "Sections",
description: "Add sections here",
type: "array",
of: [richTextBlock],
of: [richTextBlock, imageBlock],
}),
defineField({
name: richTextID,
Expand Down
44 changes: 44 additions & 0 deletions studioShared/schemas/objects/imageBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { defineField } from "sanity";

import { isInternationalizedString } from "studio/lib/interfaces/global";
import { internationalizedImage } from "studio/schemas/fields/media";
import { firstTranslation } from "studio/utils/i18n";

const imageBlock = defineField({
name: "imageBlock",
title: "Image Block",
type: "object",
fields: [
{
name: "images",
title: "Images",
type: "array",
of: [internationalizedImage],
},
],
preview: {
select: {
images: "images",
},
prepare: ({ images }) => {
const count = Object.keys(images).length;
const firstImage = count > 0 ? images[0] : undefined;
let firstImageAlt = null;
if (firstImage !== undefined) {
const imageAlt = firstImage.alt;
if (!isInternationalizedString(imageAlt)) {
throw new TypeError(
`Expected image 'alt' to be InternationalizedString, was ${typeof firstImage.alt}`,
);
}
firstImageAlt = firstTranslation(imageAlt);
}
return {
title: count > 1 ? `${count} images` : (firstImageAlt ?? undefined),
media: firstImage,
};
},
},
});

export default imageBlock;

0 comments on commit 89fe792

Please sign in to comment.