-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
169 lines (159 loc) · 5.84 KB
/
index.tsx
File metadata and controls
169 lines (159 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { MediaBlockComponent } from '@/blocks/Media/Component'
import {
DefaultNodeTypes,
SerializedBlockNode,
SerializedInlineBlockNode,
SerializedLinkNode,
} from '@payloadcms/richtext-lexical'
import { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
import {
JSXConvertersFunction,
LinkJSXConverter,
RichText as RichTextLexical,
} from '@payloadcms/richtext-lexical/react'
import { BlogListBlockComponent } from '@/blocks/BlogList/Component'
import { ButtonBlockComponent } from '@/blocks/Button/Component'
import { CalloutBlockComponent } from '@/blocks/Callout/Component'
import { DocumentBlockComponent } from '@/blocks/Document/Component'
import { EventListBlockComponent } from '@/blocks/EventList/Component'
import { EventTableBlockComponent } from '@/blocks/EventTable/Component'
import { GenericEmbedBlockComponent } from '@/blocks/GenericEmbed/Component'
import { HeaderBlockComponent } from '@/blocks/Header/Component'
import { InlineMediaComponent } from '@/blocks/InlineMedia/Component'
import { SingleBlogPostBlockComponent } from '@/blocks/SingleBlogPost/Component'
import { SingleEventBlockComponent } from '@/blocks/SingleEvent/Component'
import { SponsorsBlockComponent } from '@/blocks/Sponsors/components'
import { LINK_ENABLED_COLLECTIONS } from '@/constants/linkCollections'
import type {
BlogListBlock as BlogListBlockProps,
BuiltInPage,
ButtonBlock as ButtonBlockProps,
CalloutBlock as CalloutBlockProps,
DocumentBlock as DocumentBlockProps,
EventListBlock as EventListBlockProps,
EventTableBlock as EventTableBlockProps,
GenericEmbedBlock as GenericEmbedBlockProps,
HeaderBlock as HeaderBlockProps,
InlineMediaBlock as InlineMediaBlockProps,
MediaBlock as MediaBlockProps,
Page,
Post,
SingleBlogPostBlock as SingleBlogPostBlockProps,
SingleEventBlock as SingleEventBlockProps,
SponsorsBlock as SponsorsBlockProps,
} from '@/payload-types'
import { handleReferenceURL } from '@/utilities/handleReferenceURL'
import { cn } from '@/utilities/ui'
type LinkDocRelationTo = (typeof LINK_ENABLED_COLLECTIONS)[number]
type LinkDocValue = BuiltInPage | Page | Post
type ResolvedLinkDoc = {
relationTo: LinkDocRelationTo
value: LinkDocValue
}
// Type guard to validate and narrow link doc type
function isResolvedLinkDoc(doc: unknown): doc is ResolvedLinkDoc {
if (!doc || typeof doc !== 'object') {
return false
}
if (!('relationTo' in doc) || !('value' in doc)) {
return false
}
const { relationTo, value } = doc
if (typeof value !== 'object' || value === null) {
return false
}
// Check relationTo is one of the enabled collections
const enabledCollections: readonly string[] = LINK_ENABLED_COLLECTIONS
return typeof relationTo === 'string' && enabledCollections.includes(relationTo)
}
type NodeTypes =
| DefaultNodeTypes
| SerializedBlockNode<
| BlogListBlockProps
| ButtonBlockProps
| CalloutBlockProps
| DocumentBlockProps
| EventListBlockProps
| EventTableBlockProps
| GenericEmbedBlockProps
| HeaderBlockProps
| MediaBlockProps
| SingleBlogPostBlockProps
| SingleEventBlockProps
| SponsorsBlockProps
>
| SerializedInlineBlockNode<InlineMediaBlockProps>
const internalDocToHref = ({ linkNode }: { linkNode: SerializedLinkNode }) => {
const { linkType, doc, url } = linkNode.fields
if (linkType === 'internal') {
if (!isResolvedLinkDoc(doc)) {
throw new Error('Expected doc to be a resolved link document')
}
return (
handleReferenceURL({
url,
type: linkType,
reference: {
relationTo: doc.relationTo,
value: doc.value,
},
}) || '/'
)
}
return url || '/'
}
const jsxConverters: JSXConvertersFunction<NodeTypes> = ({ defaultConverters }) => ({
...defaultConverters,
...LinkJSXConverter({ internalDocToHref }),
// if block has two variants - to make TS happy we fallback to the default for the block variant
blocks: {
blogList: ({ node }) => <BlogListBlockComponent {...node.fields} isLayoutBlock={false} />,
buttonBlock: ({ node }) => <ButtonBlockComponent {...node.fields} />,
calloutBlock: ({ node }) => <CalloutBlockComponent {...node.fields} />,
documentBlock: ({ node }) => <DocumentBlockComponent {...node.fields} isLayoutBlock={false} />,
eventList: ({ node }) => <EventListBlockComponent {...node.fields} isLayoutBlock={false} />,
eventTable: ({ node }) => <EventTableBlockComponent {...node.fields} isLayoutBlock={false} />,
singleEvent: ({ node }) => <SingleEventBlockComponent {...node.fields} isLayoutBlock={false} />,
genericEmbed: ({ node }) => (
<GenericEmbedBlockComponent {...node.fields} isLayoutBlock={false} />
),
headerBlock: ({ node }) => <HeaderBlockComponent {...node.fields} isLayoutBlock={false} />,
mediaBlock: ({ node }) => (
<MediaBlockComponent
className="col-start-1 col-span-3"
imgClassName="m-0"
{...node.fields}
isLayoutBlock={false}
captionClassName="mx-auto max-w-[48rem]"
/>
),
singleBlogPost: ({ node }) => (
<SingleBlogPostBlockComponent {...node.fields} isLayoutBlock={false} />
),
sponsorsBlock: ({ node }) => <SponsorsBlockComponent {...node.fields} isLayoutBlock={false} />,
},
inlineBlocks: {
inlineMedia: ({ node }) => <InlineMediaComponent {...node.fields} />,
},
})
type Props = {
data: SerializedEditorState
enableGutter?: boolean
} & React.HTMLAttributes<HTMLDivElement>
export default function RichText(props: Props) {
const { className, enableGutter = true, ...rest } = props
return (
<RichTextLexical
converters={jsxConverters}
className={cn(
'mx-auto prose md:prose-md dark:prose-invert',
{
'container ': enableGutter,
'max-w-none': !enableGutter,
},
className,
)}
{...rest}
/>
)
}