Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add big text teaser component #1890 #1913

Merged
merged 5 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sanityv3/schemas/editors/blockContentType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export type BlockContentProps = {
attachment?: boolean
lists?: boolean
smallText?: boolean
normalTextOverride?: {
title: string
value: 'normal'
component?: ({ children }: { children: React.ReactNode }) => JSX.Element
}
}
const SmallTextRender = (props: any) => {
const { children } = props
Expand All @@ -34,11 +39,12 @@ export const configureBlockContent = (options: BlockContentProps = {}): BlockFie
attachment = false,
lists = true,
smallText = true,
normalTextOverride = { title: 'Normal', value: 'normal' },
} = options

const config: BlockFieldType = {
type: 'block',
styles: [{ title: 'Normal', value: 'normal' }],
styles: [normalTextOverride],
lists: lists
? [
{ title: 'Numbered', value: 'number' },
Expand Down
74 changes: 67 additions & 7 deletions sanityv3/schemas/objects/teaser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CompactBlockEditor from '../components/CompactBlockEditor'
import { configureBlockContent, configureTitleBlockContent } from '../editors'
import { validateCharCounterEditor } from '../validations/validateCharCounterEditor'

import type { PortableTextBlock, Reference, Rule } from 'sanity'
import type { PortableTextBlock, Reference, Rule, ValidationContext } from 'sanity'
import type { DownloadableImage } from './downloadableImage'
import type { DownloadableFile } from './files'
import type { ImageWithAlt } from './imageWithAlt'
Expand All @@ -25,7 +25,7 @@ const imageAlignmentOptions = [
{ value: 'right', icon: RightAlignedImage },
]

const blockContentType = configureBlockContent({
const blockConfig = {
h1: false,
h2: false,
h3: false,
Expand All @@ -34,20 +34,38 @@ const blockContentType = configureBlockContent({
externalLink: false,
attachment: false,
lists: false,
}

const blockContentType = configureBlockContent({ ...blockConfig })

const blockContentTypeForBigText = configureBlockContent({
...blockConfig,
smallText: false,
normalTextOverride: {
title: 'Normal',
value: 'normal',
component: ({ children }: { children: React.ReactNode }) => <span style={{ fontSize: '42px' }}>{children}</span>,
},
})

export type Teaser = {
_type: 'teaser'
overline?: string
title?: PortableTextBlock[]
text?: PortableTextBlock[]
isBigText?: boolean
bigText?: PortableTextBlock[]
action?: (LinkSelector | DownloadableFile | DownloadableImage)[]
image: ImageWithAlt
imagePosition?: string
imageSize?: string
background?: ColorSelectorValue
}

type TeaserDocument = {
parent: Teaser
}

export default {
name: 'teaser',
title: 'Teaser',
Expand All @@ -62,6 +80,7 @@ export default {
collapsible: true,
collapsed: true,
},
hidden: ({ parent }: TeaserDocument) => parent.isBigText,
},
{
name: 'link',
Expand All @@ -74,6 +93,11 @@ export default {
},
],
fields: [
{
title: 'Big text',
name: 'isBigText',
type: 'boolean',
},
{
name: 'overline',
title: 'Eyebrow',
Expand All @@ -87,14 +111,35 @@ export default {
input: CompactBlockEditor,
},
of: [titleContentType],
hidden: ({ parent }: TeaserDocument) => parent.isBigText,
},
{
name: 'text',
title: 'Text content',
type: 'array',
of: [blockContentType],
validation: (Rule: Rule) =>
Rule.custom((value: PortableTextBlock[]) => validateCharCounterEditor(value, 600)).warning(),
Rule.custom((value: PortableTextBlock[], ctx: ValidationContext) => {
if (!(ctx.parent as Teaser)?.isBigText) {
return validateCharCounterEditor(value, 600)
}
return true
}).warning(),
hidden: ({ parent }: TeaserDocument) => parent.isBigText,
},
{
name: 'bigText',
title: 'Text content',
type: 'array',
of: [blockContentTypeForBigText],
validation: (Rule: Rule) =>
Rule.custom((value: PortableTextBlock[], ctx: ValidationContext) => {
if ((ctx.parent as Teaser)?.isBigText) {
return validateCharCounterEditor(value, 600)
}
return true
}),
hidden: ({ parent }: TeaserDocument) => !parent.isBigText,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use the Rule.custom((value: PortableTextBlock[]) => validateCharCounterEditor(value, 600)).warning() and
Rule.custom((value: PortableTextBlock[]) => validateCharCounterEditor(value, 600)) for text and bigText.. seems like no difference to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one is a warning, the other is required

{
name: 'action',
Expand Down Expand Up @@ -165,14 +210,29 @@ export default {
preview: {
select: {
title: 'title',
text: 'text',
isBigText: 'isBigText',
bigText: 'bigText',
image: 'image.asset',
},
prepare({ title, image }: { title: PortableTextBlock[]; image: Reference }) {
const plainTitle = title ? blocksToText(title) : undefined
prepare({
title,
text,
isBigText,
bigText,
image,
}: {
title: PortableTextBlock[]
text: PortableTextBlock[]
isBigText: boolean
bigText: PortableTextBlock[]
image: Reference
}) {
const plainTitle = isBigText ? blocksToText(bigText) : blocksToText(title || text)

return {
title: plainTitle || 'Missing title!',
subtitle: 'Teaser component',
title: plainTitle || 'Missing title/content',
subtitle: isBigText ? 'Teaser component (BIG TEXT)' : 'Teaser component',
media: image,
}
},
Expand Down
88 changes: 66 additions & 22 deletions sanityv3/schemas/objects/textBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { text_field } from '@equinor/eds-icons'
import type { Reference, Rule } from 'sanity'
import type { PortableTextBlock, Reference, Rule, SanityDocument, ValidationContext } from 'sanity'
import type { ColorSelectorValue } from '../components/ColorSelector'
import blocksToText from '../../helpers/blocksToText'
import { EdsIcon } from '../../icons'
import { SchemaType } from '../../types'
import CompactBlockEditor from '../components/CompactBlockEditor'
import { configureBlockContent, configureTitleBlockContent } from '../editors'
import { validateComponentAnchor } from '../validations/validateAnchorReference'
Expand All @@ -16,13 +15,29 @@ const blockContentType = configureBlockContent({
h4: false,
attachment: false,
})

const ingressContentType = configureBlockContent({
h1: false,
h2: false,
h3: false,
h4: false,
attachment: false,
})

const blockContentTypeForBigText = configureBlockContent({
h1: false,
h2: false,
h3: false,
h4: false,
attachment: false,
smallText: false,
normalTextOverride: {
title: 'Normal',
value: 'normal',
component: ({ children }: { children: React.ReactNode }) => <span style={{ fontSize: '42px' }}>{children}</span>,
},
})

const titleContentType = configureTitleBlockContent()

type TextBlock = {
Expand All @@ -31,12 +46,18 @@ type TextBlock = {
anchor?: string
ingress?: string
text?: string
isBigText?: boolean
bigText?: PortableTextBlock[]
action?: Reference[]
splitList?: boolean
overrideButtonStyle?: boolean
background?: ColorSelectorValue
}

type TextBlockDocument = {
parent: TextBlock
}

export default {
name: 'textBlock',
title: 'Text block',
Expand All @@ -50,6 +71,7 @@ export default {
collapsible: true,
collapsed: true,
},
hidden: ({ parent }: TextBlockDocument) => parent.isBigText,
},
{
title: 'Eyebrow headline',
Expand All @@ -59,6 +81,7 @@ export default {
collapsible: true,
collapsed: true,
},
hidden: ({ parent }: TextBlockDocument) => parent.isBigText,
},
{
title: 'Call to action(s)',
Expand All @@ -84,6 +107,11 @@ export default {
},
],
fields: [
{
title: 'Big text',
name: 'isBigText',
type: 'boolean',
},
{
name: 'image',
type: 'imageWithAlt',
Expand All @@ -105,7 +133,11 @@ export default {
input: CompactBlockEditor,
},
of: [titleContentType],
validation: (Rule: SchemaType.ValidationRule) => Rule.required().warning('A title is recommended'),
validation: (Rule: Rule) =>
Rule.custom((value: PortableTextBlock[], ctx: ValidationContext) =>
!value && !(ctx.parent as TextBlock)?.isBigText ? 'A title is recommended' : true,
).warning(),
hidden: ({ parent }: TextBlockDocument) => parent.isBigText,
},
{
name: 'anchor',
Expand All @@ -124,6 +156,18 @@ export default {
title: 'Ingress',
type: 'array',
of: [ingressContentType],
hidden: ({ parent }: TextBlockDocument) => parent.isBigText,
},
{
name: 'bigTitle',
title: 'Title',
type: 'array',
of: [blockContentTypeForBigText],
hidden: ({ parent }: TextBlockDocument) => !parent.isBigText,
validation: (Rule: Rule) =>
Rule.custom((value: PortableTextBlock[], ctx: ValidationContext) =>
!value && (ctx.parent as TextBlock)?.isBigText ? 'Title is required' : true,
),
},
{
name: 'text',
Expand Down Expand Up @@ -176,27 +220,27 @@ export default {
title: 'title',
ingress: 'ingress',
text: 'text',
},
prepare({ title = [], ingress, text }: { title: any[]; ingress: any; text: any }) {
const textBlock = (text || []).find((introBlock: any) => introBlock._type === 'block')
const ingressBlock = (ingress || []).find((introBlock: any) => introBlock._type === 'block')
const plainTitle = title ? blocksToText(title) : undefined
isBigText: 'isBigText',
bigTitle: 'bigTitle',
},
prepare({
title,
isBigText,
bigTitle,
ingress,
text,
}: {
title: PortableTextBlock[]
ingress: PortableTextBlock[]
isBigText: boolean
bigTitle: PortableTextBlock[]
text: PortableTextBlock[]
}) {
const plainTitle = isBigText ? blocksToText(bigTitle) : blocksToText(title || ingress || text)

return {
title:
plainTitle ||
(textBlock &&
textBlock.children
.filter((child: any) => child._type === 'span')
.map((span: any) => span.text)
.join('')) ||
(ingressBlock &&
ingressBlock.children
.filter((child: any) => child._type === 'span')
.map((span: any) => span.text)
.join('')) ||
'Missing content!',
subtitle: `Text block component.`,
title: plainTitle || 'Missing title/content',
subtitle: isBigText ? 'Text block component (BIG TEXT)' : 'Text block component',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨🧹✨

media: EdsIcon(text_field),
}
},
Expand Down
6 changes: 5 additions & 1 deletion search/IndexSanityContent/magazine/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export const query = /* groq */ `*[_type == "magazine" && _lang == $lang && !(_i
"ingress": pt::text(ingress),
"textBlocks": content[_type == "textBlock"]{
"_key": _key,
"title": pt::text(title),
"title": select(
"isBigText" == true => {
pt::text(bigTitle),
pt::text(title)
}),
"ingress": pt::text(ingress),
"text": pt::text(text) // TODO: Do this manually to cover all cases
},
Expand Down
6 changes: 5 additions & 1 deletion search/IndexSanityContent/topic/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export const query = /* groq */ `*[_type match "route_" + $lang + "*" && content
"type": content->_type,
"textBlocks": content->content[_type == "textBlock"]{
"_key": _key,
"title": pt::text(title),
"title": select(
"isBigText" == true => {
pt::text(bigTitle),
pt::text(title)
}),
"ingress": pt::text(ingress),
"text": pt::text(text) // TODO: Do this manually to cover all cases
},
Expand Down
Loading