-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathcontext.tsx
258 lines (215 loc) · 6.85 KB
/
context.tsx
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { type ExtendedRecordMap } from 'notion-types'
import { defaultMapImageUrl, defaultMapPageUrl } from 'notion-utils'
import * as React from 'react'
import { AssetWrapper } from './components/asset-wrapper'
import { Checkbox as DefaultCheckbox } from './components/checkbox'
import { Header } from './components/header'
import { wrapNextImage, wrapNextLegacyImage, wrapNextLink } from './next'
import {
type MapImageUrlFn,
type MapPageUrlFn,
type NotionComponents,
type SearchNotionFn
} from './types'
export interface NotionContext {
recordMap: ExtendedRecordMap
components: NotionComponents
mapPageUrl: MapPageUrlFn
mapImageUrl: MapImageUrlFn
searchNotion?: SearchNotionFn
isShowingSearch?: boolean
onHideSearch?: () => void
rootPageId?: string
rootDomain?: string
fullPage: boolean
darkMode: boolean
previewImages: boolean
forceCustomImages: boolean
showCollectionViewDropdown: boolean
showTableOfContents: boolean
minTableOfContentsItems: number
linkTableTitleProperties: boolean
isLinkCollectionToUrlProperty: boolean
pageWidth: number
defaultPageIcon?: string | null
defaultPageCover?: string | null
defaultPageCoverPosition?: number
zoom: any
}
export interface PartialNotionContext {
recordMap?: ExtendedRecordMap
components?: Partial<NotionComponents>
mapPageUrl?: MapPageUrlFn
mapImageUrl?: MapImageUrlFn
searchNotion?: SearchNotionFn
isShowingSearch?: boolean
onHideSearch?: () => void
rootPageId?: string
rootDomain?: string
fullPage?: boolean
darkMode?: boolean
previewImages?: boolean
forceCustomImages?: boolean
showCollectionViewDropdown?: boolean
linkTableTitleProperties?: boolean
isLinkCollectionToUrlProperty?: boolean
pageWidth?: number
showTableOfContents?: boolean
minTableOfContentsItems?: number
defaultPageIcon?: string | null
defaultPageCover?: string | null
defaultPageCoverPosition?: number
zoom?: any
}
function DefaultLink(props: any) {
return <a target='_blank' rel='noopener noreferrer' {...props} />
}
const DefaultLinkMemo = React.memo(DefaultLink)
function DefaultPageLink(props: any) {
return <a {...props} />
}
const DefaultPageLinkMemo = React.memo(DefaultPageLink)
function DefaultEmbed(props: any) {
return <AssetWrapper {...props} />
}
const DefaultHeader = Header
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function dummyLink({ href, rel, target, title, ...rest }: any) {
return <span {...rest} />
}
const dummyComponent = (name: string) => () => {
console.warn(
`Warning: using empty component "${name}" (you should override this in NotionRenderer.components)`
)
return null
}
// TODO: should we use React.memo here?
// https://reactjs.org/docs/react-api.html#reactmemo
const dummyOverrideFn = (_: any, defaultValueFn: () => React.ReactNode) =>
defaultValueFn()
const defaultComponents: NotionComponents = {
Image: null, // disable custom images by default
Link: DefaultLinkMemo,
PageLink: DefaultPageLinkMemo,
Checkbox: DefaultCheckbox,
Callout: undefined, // use the built-in callout rendering by default
Code: dummyComponent('Code'),
Equation: dummyComponent('Equation'),
Collection: dummyComponent('Collection'),
Property: undefined, // use the built-in property rendering by default
propertyTextValue: dummyOverrideFn,
propertySelectValue: dummyOverrideFn,
propertyRelationValue: dummyOverrideFn,
propertyFormulaValue: dummyOverrideFn,
propertyTitleValue: dummyOverrideFn,
propertyPersonValue: dummyOverrideFn,
propertyFileValue: dummyOverrideFn,
propertyCheckboxValue: dummyOverrideFn,
propertyUrlValue: dummyOverrideFn,
propertyEmailValue: dummyOverrideFn,
propertyPhoneNumberValue: dummyOverrideFn,
propertyNumberValue: dummyOverrideFn,
propertyLastEditedTimeValue: dummyOverrideFn,
propertyCreatedTimeValue: dummyOverrideFn,
propertyDateValue: dummyOverrideFn,
propertyAutoIncrementIdValue: dummyOverrideFn,
Pdf: dummyComponent('Pdf'),
Tweet: dummyComponent('Tweet'),
Modal: dummyComponent('Modal'),
Header: DefaultHeader,
Embed: DefaultEmbed
}
const defaultNotionContext: NotionContext = {
recordMap: {
block: {},
collection: {},
collection_view: {},
collection_query: {},
notion_user: {},
signed_urls: {}
},
components: defaultComponents,
mapPageUrl: defaultMapPageUrl(),
mapImageUrl: defaultMapImageUrl,
searchNotion: undefined,
isShowingSearch: false,
onHideSearch: undefined,
fullPage: false,
darkMode: false,
previewImages: false,
forceCustomImages: false,
showCollectionViewDropdown: true,
linkTableTitleProperties: true,
isLinkCollectionToUrlProperty: false,
pageWidth: 708,
showTableOfContents: false,
minTableOfContentsItems: 3,
defaultPageIcon: null,
defaultPageCover: null,
defaultPageCoverPosition: 0.5,
zoom: null
}
const ctx = React.createContext<NotionContext>(defaultNotionContext)
export function NotionContextProvider({
components: themeComponents = {},
children,
mapPageUrl,
mapImageUrl,
rootPageId,
...rest
}: PartialNotionContext & {
children?: React.ReactNode
}) {
for (const key of Object.keys(rest)) {
if ((rest as any)[key] === undefined) {
delete (rest as any)[key]
}
}
const wrappedThemeComponents = React.useMemo(
() => ({
...themeComponents
}),
[themeComponents]
)
if (
wrappedThemeComponents.nextImage &&
wrappedThemeComponents.nextLegacyImage
) {
console.warn(
'You should not pass both nextImage and nextLegacyImage. Only nextImage component will be used.'
)
wrappedThemeComponents.Image = wrapNextImage(themeComponents.nextImage)
} else if (wrappedThemeComponents.nextImage) {
wrappedThemeComponents.Image = wrapNextImage(themeComponents.nextImage)
} else if (wrappedThemeComponents.nextLegacyImage) {
wrappedThemeComponents.Image = wrapNextLegacyImage(
themeComponents.nextLegacyImage
)
}
if (wrappedThemeComponents.nextLink) {
wrappedThemeComponents.nextLink = wrapNextLink(themeComponents.nextLink)
}
// ensure the user can't override default components with falsy values
// since it would result in very difficult-to-debug react errors
for (const key of Object.keys(wrappedThemeComponents)) {
if (!(wrappedThemeComponents as any)[key]) {
delete (wrappedThemeComponents as any)[key]
}
}
const value = React.useMemo(
() => ({
...defaultNotionContext,
...rest,
rootPageId,
mapPageUrl: mapPageUrl ?? defaultMapPageUrl(rootPageId),
mapImageUrl: mapImageUrl ?? defaultMapImageUrl,
components: { ...defaultComponents, ...wrappedThemeComponents }
}),
[mapImageUrl, mapPageUrl, wrappedThemeComponents, rootPageId, rest]
)
return <ctx.Provider value={value}>{children}</ctx.Provider>
}
export const NotionContextConsumer = ctx.Consumer
export const useNotionContext = (): NotionContext => {
return React.useContext(ctx)
}