|
| 1 | +import { play_circle } from '@equinor/eds-icons' |
| 2 | +import type { PortableTextBlock, Rule } from 'sanity' |
| 3 | +import blocksToText from '../../helpers/blocksToText' |
| 4 | +import { EdsIcon } from '../../icons' |
| 5 | +import { CompactBlockEditor } from '../components/CompactBlockEditor' |
| 6 | +import { configureBlockContent } from '../editors' |
| 7 | +import { validateCharCounterEditor } from '../validations/validateCharCounterEditor' |
| 8 | +import { gridColumns } from './commonFields/commonFields' |
| 9 | +import { cookiePolicy } from './iframe/sharedIframeFields' |
| 10 | + |
| 11 | +export default { |
| 12 | + name: 'embeddedVideoList', |
| 13 | + title: 'Embedded video list', |
| 14 | + type: 'object', |
| 15 | + fields: [ |
| 16 | + { |
| 17 | + name: 'title', |
| 18 | + type: 'array', |
| 19 | + components: { |
| 20 | + input: CompactBlockEditor, |
| 21 | + }, |
| 22 | + of: [configureBlockContent({ variant: 'title' })], |
| 23 | + title: 'Title', |
| 24 | + validation: (Rule: Rule) => Rule.required(), |
| 25 | + }, |
| 26 | + { |
| 27 | + type: 'boolean', |
| 28 | + name: 'hideTitle', |
| 29 | + title: 'Hide title', |
| 30 | + description: |
| 31 | + 'Hides the title, but screen readers will read title of carousel', |
| 32 | + }, |
| 33 | + { |
| 34 | + name: 'ingress', |
| 35 | + title: 'Ingress', |
| 36 | + description: 'Optional short description. Max 400 characters', |
| 37 | + type: 'array', |
| 38 | + of: [configureBlockContent({ variant: 'ingress' })], |
| 39 | + validation: (Rule: Rule) => |
| 40 | + Rule.custom((value: any) => |
| 41 | + validateCharCounterEditor(value, 400, true), |
| 42 | + ), |
| 43 | + }, |
| 44 | + cookiePolicy(), |
| 45 | + gridColumns(), |
| 46 | + { |
| 47 | + type: 'array', |
| 48 | + name: 'items', |
| 49 | + title: 'Video items', |
| 50 | + description: 'Add one or more embedded videos.', |
| 51 | + of: [ |
| 52 | + { |
| 53 | + title: 'Video item', |
| 54 | + type: 'object', |
| 55 | + fields: [ |
| 56 | + { |
| 57 | + name: 'videoId', |
| 58 | + type: 'string', |
| 59 | + title: 'Embed URL', |
| 60 | + description: |
| 61 | + 'Paste the full embed URL (YouTube or Vimeo iframe src).', |
| 62 | + }, |
| 63 | + { |
| 64 | + name: 'highlighted', |
| 65 | + type: 'boolean', |
| 66 | + title: 'Highlight video', |
| 67 | + description: |
| 68 | + 'Places this video first and larger at the top of the list on web.', |
| 69 | + initialValue: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: 'title', |
| 73 | + type: 'array', |
| 74 | + title: 'Title', |
| 75 | + description: 'Optional title shown beneath the video.', |
| 76 | + components: { input: CompactBlockEditor }, |
| 77 | + of: [configureBlockContent({ variant: 'title' })], |
| 78 | + }, |
| 79 | + ], |
| 80 | + preview: { |
| 81 | + select: { |
| 82 | + title: 'title', |
| 83 | + subtitle: 'videoId', |
| 84 | + highlighted: 'highlighted', |
| 85 | + }, |
| 86 | + prepare({ |
| 87 | + title = [], |
| 88 | + subtitle, |
| 89 | + highlighted, |
| 90 | + }: { |
| 91 | + title: PortableTextBlock[] |
| 92 | + subtitle: string |
| 93 | + highlighted?: boolean |
| 94 | + }) { |
| 95 | + return { |
| 96 | + title: blocksToText(title) || 'Embedded video', |
| 97 | + subtitle: highlighted ? `Highlighted | ${subtitle}` : subtitle, |
| 98 | + } |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + validation: (Rule: Rule) => |
| 104 | + Rule.required() |
| 105 | + .min(1) |
| 106 | + .custom((items?: Array<{ highlighted?: boolean }>) => { |
| 107 | + const highlightedCount = |
| 108 | + items?.filter(item => item?.highlighted).length ?? 0 |
| 109 | + |
| 110 | + return highlightedCount > 1 |
| 111 | + ? 'Only one video can be highlighted.' |
| 112 | + : true |
| 113 | + }), |
| 114 | + }, |
| 115 | + ], |
| 116 | + preview: { |
| 117 | + select: { |
| 118 | + title: 'title', |
| 119 | + items: 'items', |
| 120 | + }, |
| 121 | + prepare(selection: any) { |
| 122 | + const { title, items } = selection |
| 123 | + const length = items ? items.length : 0 |
| 124 | + |
| 125 | + return { |
| 126 | + title: title ? blocksToText(title) : 'Untitled embedded video list', |
| 127 | + subtitle: `Embedded video list with ${length} items`, |
| 128 | + media: EdsIcon(play_circle), |
| 129 | + } |
| 130 | + }, |
| 131 | + }, |
| 132 | +} |
0 commit comments