Skip to content

Commit 51888f9

Browse files
committed
🎨 merged promotiles and promotions v2
1 parent 32fda66 commit 51888f9

12 files changed

Lines changed: 252 additions & 148 deletions

File tree

studio/schemas/objects/anchorLinkList/anchorLinkReference.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { anchor } from '@equinor/eds-icons'
2-
import { EdsIcon } from '../../../icons'
2+
import type { Rule } from 'sanity'
3+
import { EdsBlockEditorIcon, EdsIcon } from '../../../icons'
34
import { AnchorLinkDescription } from '../anchorReferenceField'
4-
import { Rule } from 'sanity'
55

66
export type AnchorLinkReference = {
77
_type: 'anchorLinkReference'
@@ -11,7 +11,7 @@ export default {
1111
title: 'Anchor Link Reference',
1212
name: 'anchorLinkReference',
1313
type: 'object',
14-
14+
icon: EdsBlockEditorIcon(anchor),
1515
fields: [
1616
{
1717
name: 'title',

studio/schemas/objects/commonFields/commonFields.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export const ingress = {
2626
name: 'ingress',
2727
title: 'Ingress',
2828
type: 'array',
29+
components: {
30+
input: CompactBlockEditor,
31+
},
2932
of: [configureBlockContent({ variant: 'ingress' })],
3033
}
3134

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import type {
2-
CustomValidatorResult,
3-
Reference,
4-
Rule,
5-
ValidationContext,
6-
} from 'sanity'
1+
import type { Reference } from 'sanity'
72

83
export type ImageWithAlt = {
94
_type: string
@@ -21,39 +16,12 @@ export default {
2116
collapsed: false,
2217
},
2318
fields: [
24-
{
25-
name: 'isDecorative',
26-
type: 'boolean',
27-
title: 'Image is decorative',
28-
description:
29-
'If this image is purely decorative you can disable the alt tag input here. Please note that this makes the image invisible for screen reader users.',
30-
},
3119
{
3220
name: 'alt',
3321
type: 'string',
34-
title: 'Alt text',
35-
description: 'Alt attribute text description for image',
36-
validation: (Rule: Rule) =>
37-
Rule.custom(
38-
(
39-
value: string,
40-
context: ValidationContext,
41-
): CustomValidatorResult => {
42-
const { parent } = context as { parent: ImageWithAlt }
43-
44-
// Only make the alt tag required if an image has been selected
45-
if (!parent?.asset) return true
46-
47-
// Alt tag should only be required if the image is not decorative
48-
if (!parent?.isDecorative && !value) {
49-
return 'Alt attribute is required'
50-
}
51-
52-
return true
53-
},
54-
),
55-
hidden: ({ parent }: { parent: ImageWithAlt }) =>
56-
parent?.isDecorative === true,
22+
title: 'Alternative text for screen readers',
23+
description:
24+
'Recommended. Describe whats seen in the image. Leave empty if purely decorative.',
5725
},
5826
],
5927
}

studio/schemas/objects/linkSelector/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export const externalLink = {
110110
prepare({ href }: { href: string }) {
111111
return {
112112
title: href || 'Add external link',
113+
media: EdsBlockEditorIcon(external_link),
113114
}
114115
},
115116
},

studio/schemas/objects/linkSelector/linkSelector.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Rule, ValidationContext } from 'sanity'
1+
import type { Rule } from 'sanity'
22
import singleItemArray from '../singleItemArray'
33
import {
44
anchorReference,
@@ -22,9 +22,19 @@ const linkSelector = (
2222
includeLabels = true,
2323
) => ({
2424
name: 'linkSelector',
25+
title: 'Link',
2526
type: 'object',
2627
hidden: hidden,
2728
fields: [
29+
includeLabels && {
30+
name: 'label',
31+
title: 'Link label',
32+
type: 'string',
33+
validation: (Rule: Rule) =>
34+
Rule.custom((value: string) => {
35+
return value ? true : 'You must add a label'
36+
}),
37+
},
2838
singleItemArray(
2939
{
3040
name: 'link',
@@ -44,23 +54,6 @@ const linkSelector = (
4454
},
4555
true,
4656
),
47-
includeLabels && {
48-
name: 'label',
49-
title: 'Label',
50-
description:
51-
'Leave empty if you want to use referenced page title. Required for external and social media links',
52-
type: 'string',
53-
validation: (Rule: Rule) =>
54-
Rule.custom((value: string, ctx: ValidationContext) => {
55-
if (
56-
ctx.parent?.link?.[0]?._type === 'link' ||
57-
ctx?.parent?.link?.[0]?._type === 'socialMediaLink'
58-
) {
59-
return value ? true : 'You must add a label'
60-
}
61-
return true
62-
}),
63-
},
6457
anchorReference,
6558
].filter(e => e),
6659
})

studio/schemas/objects/promoteV2/promotionV2.tsx

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Card, Flex, Grid, Radio, Text } from '@sanity/ui'
22
import { useCallback } from 'react'
3-
import { HiOutlineViewColumns } from 'react-icons/hi2'
43
import { LuText } from 'react-icons/lu'
54
import { MdImage } from 'react-icons/md'
5+
import { RiMegaphoneLine } from 'react-icons/ri'
66
import {
77
type Image,
88
type PortableTextBlock,
@@ -12,14 +12,22 @@ import {
1212
set,
1313
type ValidationContext,
1414
} from 'sanity'
15+
import { capitalizeFirstLetter } from '@/helpers/formatters'
1516
import blocksToText from '../../../helpers/blocksToText'
17+
import anchorLinkReference from '../anchorLinkList/anchorLinkReference'
1618
import {
1719
hideTitle,
1820
ingress,
1921
layoutGrid,
2022
theme,
2123
title,
2224
} from '../commonFields/commonFields'
25+
import {
26+
externalLink,
27+
homepageLink,
28+
internalReference,
29+
internalReferenceOtherLanguage,
30+
} from '../linkSelector/common'
2331

2432
type PromotionLayoutInputProps = {
2533
options: any[]
@@ -125,50 +133,125 @@ export default {
125133
name: 'promotedItem',
126134
title: 'Promoted item',
127135
fields: [
128-
/* {
129-
title: 'Page to be promoted',
130-
name: 'reference',
131-
description: 'Select the page you want to promote',
132-
type: 'reference',
133-
to: [...routes].filter(e => e),
134-
options: {
135-
filter: topicPromotionFilter,
136-
disableNew: true,
137-
},
138-
}, */
139136
{
140137
name: 'link',
141-
type: 'linkSelector',
142-
validation: (Rule: Rule) => Rule.required(),
138+
type: 'array',
139+
of: [
140+
externalLink,
141+
internalReference,
142+
internalReferenceOtherLanguage,
143+
homepageLink,
144+
anchorLinkReference,
145+
],
146+
validation: (Rule: Rule) => Rule.length(1).required(),
147+
},
148+
{
149+
name: 'label',
150+
title: 'Link label',
151+
description:
152+
'Required when external, leave empty if you want to use referenced title',
153+
type: 'string',
154+
validation: (Rule: Rule) =>
155+
Rule.custom((value: string, ctx: ValidationContext) => {
156+
if (ctx.parent?.link?.[0]?._type === 'link') {
157+
return value ? true : 'You must add a label'
158+
}
159+
return true
160+
}),
143161
},
144162
{
145163
name: 'image',
146164
title: 'Image',
147165
type: 'imageWithAlt',
148166
hidden: ({ parent }: DocumentType) => {
149-
return parent?.link?.[0]?._type !== 'link'
167+
return (
168+
parent?.link?.[0]?._type !== 'link' &&
169+
parent?.link?.[0]?._type !== 'anchorLinkReference'
170+
)
150171
},
172+
validation: (Rule: Rule) =>
173+
Rule.custom((value: string, ctx: ValidationContext) => {
174+
if (
175+
ctx.parent?.link?.[0]?._type === 'link' ||
176+
ctx.parent?.link?.[0]?._type === 'anchorLinkReference'
177+
) {
178+
return value ? true : 'You must add an image'
179+
}
180+
return true
181+
}),
151182
},
152183
],
153184
preview: {
154185
select: {
155-
topicTitle: 'reference.content.title',
156-
topicMedia: 'reference.content.heroFigure.image',
186+
referenceNewsMagTitle: 'link.0.title',
187+
referenceNewsMedia: 'link.0.heroImage.image',
188+
referenceMagMedia: 'link.0.heroFigure.image',
189+
referenceTopicTitle: 'link.0.content.title',
190+
referenceTopicMedia: 'link.0.content.heroFigure.image',
191+
customImage: 'image',
192+
link: 'link',
193+
label: 'label',
194+
type: 'link.0._type',
157195
},
158196
prepare({
159-
topicTitle,
160-
topicMedia,
197+
referenceNewsMagTitle,
198+
referenceNewsMedia,
199+
referenceMagMedia,
200+
referenceTopicTitle,
201+
referenceTopicMedia,
202+
customImage,
203+
link,
204+
label,
205+
type,
161206
}: {
162-
topicTitle: PortableTextBlock[]
163-
topicMedia: Image
207+
referenceNewsMagTitle?: PortableTextBlock[]
208+
referenceNewsMedia?: Image
209+
referenceMagMedia?: Image
210+
referenceTopicTitle?: PortableTextBlock[]
211+
referenceTopicMedia?: Image
212+
customImage?: Image
213+
link?: any
214+
label?: string
215+
type?: string
164216
}) {
165-
const plainTitle = topicTitle
166-
? blocksToText(topicTitle)
167-
: 'Untitled'
217+
let promoType = type ?? 'not set'
218+
219+
if (type?.includes('route')) {
220+
promoType = 'internal route'
221+
}
222+
if (type?.includes('anchorLinkReference')) {
223+
promoType = 'anchor link'
224+
}
225+
if (type === 'link') {
226+
promoType = 'external link'
227+
}
228+
const referenceTitle =
229+
referenceTopicTitle ?? referenceNewsMagTitle
230+
let title = label ?? 'Missing title'
231+
if (
232+
link?.[0]?._type === 'anchorLinkReference' &&
233+
typeof label === 'undefined'
234+
) {
235+
title = link?.[0]?.title
236+
}
237+
if (
238+
link?.[0]?._type !== 'link' &&
239+
link?.[0]?._type !== 'anchorLinkReference' &&
240+
typeof label === 'undefined' &&
241+
referenceTitle
242+
) {
243+
title = blocksToText(referenceTitle) ?? 'Missing title'
244+
}
168245

169246
return {
170-
title: plainTitle,
171-
media: topicMedia,
247+
title: title,
248+
subtitle: `${capitalizeFirstLetter(promoType)}`,
249+
media:
250+
customImage ??
251+
referenceTopicMedia ??
252+
referenceNewsMedia ??
253+
referenceMagMedia ??
254+
RiMegaphoneLine,
172255
}
173256
},
174257
},
@@ -203,9 +286,11 @@ export default {
203286
if (Number(ctx.parent?.promoteList?.length) < Number(value)) {
204287
return 'Fewer promotions than grid columns. Please select lower grid columns'
205288
}
206-
//@ts-ignore:todo
289+
207290
if (
291+
//@ts-ignore:todo
208292
ctx.parent?.layoutDirection === 'row' &&
293+
//@ts-ignore:todo
209294
ctx.parent?.layoutGrid !== 'sm'
210295
) {
211296
if (value === '4') {
@@ -253,7 +338,7 @@ export default {
253338
return {
254339
title: plainTitle,
255340
subtitle: subTitle,
256-
media: HiOutlineViewColumns,
341+
media: RiMegaphoneLine,
257342
}
258343
},
259344
},

0 commit comments

Comments
 (0)