From 42c73aba776b84996f4e0d892c329cab6f710c2a Mon Sep 17 00:00:00 2001 From: Borghild Selle <104756130+BorghildSelle@users.noreply.github.com> Date: Mon, 4 Nov 2024 08:13:21 +0100 Subject: [PATCH] :sparkles: add image for text comp #2456 (#2459) * :sparkles: add image for text comp * :bug: fix review comments --- sanityv3/schemas/documents/page.ts | 1 + sanityv3/schemas/index.ts | 2 + sanityv3/schemas/objects/imageForText.tsx | 67 +++++++++++++++++++ web/lib/queries/common/pageContentFields.ts | 10 +++ .../shared/SharedPageContent.tsx | 4 ++ web/sections/ImageForText/ImageForText.tsx | 34 ++++++++++ web/types/types.ts | 7 ++ 7 files changed, 125 insertions(+) create mode 100644 sanityv3/schemas/objects/imageForText.tsx create mode 100644 web/sections/ImageForText/ImageForText.tsx diff --git a/sanityv3/schemas/documents/page.ts b/sanityv3/schemas/documents/page.ts index 872c0fc1c..1b9446b4b 100644 --- a/sanityv3/schemas/documents/page.ts +++ b/sanityv3/schemas/documents/page.ts @@ -74,6 +74,7 @@ export default { { type: 'videoPlayer' }, { type: 'videoPlayerCarousel' }, { type: 'table' }, + { type: 'imageForText' }, Flags.HAS_CAMPAIGN_BLOCKS && { type: 'grid' }, Flags.HAS_CAMPAIGN_BLOCKS && { type: 'campaignBanner' }, Flags.HAS_FORMS && { type: 'form' }, diff --git a/sanityv3/schemas/index.ts b/sanityv3/schemas/index.ts index 2802dcee1..c1d8eb62f 100644 --- a/sanityv3/schemas/index.ts +++ b/sanityv3/schemas/index.ts @@ -81,6 +81,7 @@ import gridColorTheme from './objects/grid/theme' import transcript from './objects/transcript' import anchorLinkList from './objects/anchorLinkList/anchorLinkList' import anchorLinkReference from './objects/anchorLinkList/anchorLinkReference' +import imageForText from './objects/imageForText' const { pageNotFound, @@ -208,6 +209,7 @@ const RemainingSchemas = [ transcript, anchorLinkList, anchorLinkReference, + imageForText, ] // Then we give our schema to the builder and provide the result to Sanity diff --git a/sanityv3/schemas/objects/imageForText.tsx b/sanityv3/schemas/objects/imageForText.tsx new file mode 100644 index 000000000..af306ee09 --- /dev/null +++ b/sanityv3/schemas/objects/imageForText.tsx @@ -0,0 +1,67 @@ +import { info_circle } from '@equinor/eds-icons' +import { PortableTextBlock, Rule } from 'sanity' +import { EdsIcon } from '../../icons' +import { configureBlockContent } from '../editors/blockContentType' +import type { ImageWithAlt } from './imageWithAlt' + +const blockContentType = configureBlockContent({ + h2: true, + h3: true, + h4: true, + internalLink: false, + externalLink: false, + lists: false, + attachment: false, + smallText: false, +}) + +export type ImageForText = { + _type: 'imageForText' + content?: PortableTextBlock[] + image?: ImageWithAlt +} + +export default { + title: 'Image for text', + name: 'imageForText', + type: 'object', + fields: [ + { + name: 'content', + title: 'Content', + type: 'array', + of: [blockContentType], + validation: (Rule: Rule) => Rule.required(), + }, + { + name: 'image', + title: 'Image', + type: 'imageWithAlt', + validation: (Rule: Rule) => Rule.required(), + }, + { + name: 'aspectRatio', + type: 'string', + description: '', + title: 'Aspect ratio', + options: { + list: [ + { title: '16:9', value: '16:9' }, + { title: 'Full width 16:9', value: 'fullWidth' }, + ], + }, + initialValue: '16:9', + }, + ], + preview: { + select: { + imageUrl: 'image.asset.url', + }, + prepare({ imageUrl }: { title: string; imageUrl: string }) { + return { + title: 'Image for text', + media: imageUrl ? : EdsIcon(info_circle), + } + }, + }, +} diff --git a/web/lib/queries/common/pageContentFields.ts b/web/lib/queries/common/pageContentFields.ts index 4f8f89b5d..3fb169ec2 100644 --- a/web/lib/queries/common/pageContentFields.ts +++ b/web/lib/queries/common/pageContentFields.ts @@ -590,6 +590,16 @@ _type == "keyNumbers" =>{ anchorReference, } }, + _type == "imageForText" => { + "type": _type, + "id": _key, + "content": content[]{..., ${markDefs}}, + "aspectRatio": coalesce(aspectRatio, '16:9'), + "image": image { + ..., + "extension": asset-> extension + }, + }, ` export default pageContentFields diff --git a/web/pageComponents/pageTemplates/shared/SharedPageContent.tsx b/web/pageComponents/pageTemplates/shared/SharedPageContent.tsx index 52d37f901..b8b49294e 100644 --- a/web/pageComponents/pageTemplates/shared/SharedPageContent.tsx +++ b/web/pageComponents/pageTemplates/shared/SharedPageContent.tsx @@ -53,6 +53,7 @@ import { CampaignBannerData, DesignOptions, AnchorLinkListData, + ImageForTextData, } from '../../../types/index' import { getColorForTheme } from '../../shared/textTeaser/theme' import Grid from '@sections/Grid/Grid' @@ -61,6 +62,7 @@ import { BackgroundContainerProps } from '@components/Backgrounds' import VideoPlayerCarousel from '@sections/VideoPlayerCarousel/VideoPlayerCarousel' import ImageCarousel from '@sections/ImageCarousel/ImageCarousel' import { AnchorLinkList } from '@sections/AnchorLinkList' +import ImageForText from '@sections/ImageForText/ImageForText' type DefaultComponent = { id?: string @@ -350,6 +352,8 @@ export const PageContent = ({ data, titleBackground }: PageContentProps) => { className={topSpacingClassName} /> ) + case 'imageForText': + return default: return null } diff --git a/web/sections/ImageForText/ImageForText.tsx b/web/sections/ImageForText/ImageForText.tsx new file mode 100644 index 000000000..fefa3f5e9 --- /dev/null +++ b/web/sections/ImageForText/ImageForText.tsx @@ -0,0 +1,34 @@ +import { ImageForTextData } from '../../types/types' +import { forwardRef } from 'react' +import Image, { Ratios } from '../../pageComponents/shared/SanityImage' +import Blocks from '../../pageComponents/shared/portableText/Blocks' + +type ImageForTextProps = { + data: ImageForTextData + anchor?: string +} + +const ImageForText = forwardRef(function ImageForText({ anchor, data }, ref) { + const { content, image, aspectRatio } = data + + return ( +
+
+ +
+ {content && } +
+ ) +}) + +export default ImageForText diff --git a/web/types/types.ts b/web/types/types.ts index 398484bbf..ab92f6ce1 100644 --- a/web/types/types.ts +++ b/web/types/types.ts @@ -465,3 +465,10 @@ export type AnchorLinkListData = { anchorReference?: string }[] } +export type ImageForTextData = { + type: 'imageForText' + id: string + image: ImageWithAlt + content?: PortableTextBlock[] + aspectRatio?: '16:9' | 'fullWidth' +}