-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathField.tsx
221 lines (192 loc) · 7.41 KB
/
Field.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
'use client'
import type { EditorState, SerializedEditorState } from 'lexical'
import type { Validate } from 'payload'
import {
FieldDescription,
FieldError,
FieldLabel,
RenderCustomComponent,
useEditDepth,
useEffectEvent,
useField,
} from '@payloadcms/ui'
import { mergeFieldStyles } from '@payloadcms/ui/shared'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import type { SanitizedClientEditorConfig } from '../lexical/config/types.js'
import '../lexical/theme/EditorTheme.scss'
import './bundled.css'
import './index.scss'
import type { LexicalRichTextFieldProps } from '../types.js'
import { LexicalProvider } from '../lexical/LexicalProvider.js'
const baseClass = 'rich-text-lexical'
const RichTextComponent: React.FC<
{
readonly editorConfig: SanitizedClientEditorConfig // With rendered features n stuff
} & LexicalRichTextFieldProps
> = (props) => {
const {
editorConfig,
field,
field: {
name,
admin: { className, description, readOnly: readOnlyFromAdmin } = {},
label,
localized,
required,
},
path: pathFromProps,
readOnly: readOnlyFromTopLevelProps,
validate, // Users can pass in client side validation if they WANT to, but it's not required anymore
} = props
const readOnlyFromProps = readOnlyFromTopLevelProps || readOnlyFromAdmin
const path = pathFromProps ?? name
const editDepth = useEditDepth()
const memoizedValidate = useCallback<Validate>(
(value, validationOptions) => {
if (typeof validate === 'function') {
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
return validate(value, { ...validationOptions, required })
}
return true
},
// Important: do not add props to the dependencies array.
// This would cause an infinite loop and endless re-rendering.
// Removing props from the dependencies array fixed this issue: https://github.com/payloadcms/payload/issues/3709
[validate, required],
)
const {
customComponents: { AfterInput, BeforeInput, Description, Error, Label } = {},
disabled: disabledFromField,
initialValue,
setValue,
showError,
value,
} = useField<SerializedEditorState>({
path,
validate: memoizedValidate,
})
const disabled = readOnlyFromProps || disabledFromField
const [isSmallWidthViewport, setIsSmallWidthViewport] = useState<boolean>(false)
const [rerenderProviderKey, setRerenderProviderKey] = useState<Date>()
const prevInitialValueRef = React.useRef<SerializedEditorState | undefined>(initialValue)
const prevValueRef = React.useRef<SerializedEditorState | undefined>(value)
useEffect(() => {
const updateViewPortWidth = () => {
const isNextSmallWidthViewport = window.matchMedia('(max-width: 768px)').matches
if (isNextSmallWidthViewport !== isSmallWidthViewport) {
setIsSmallWidthViewport(isNextSmallWidthViewport)
}
}
updateViewPortWidth()
window.addEventListener('resize', updateViewPortWidth)
return () => {
window.removeEventListener('resize', updateViewPortWidth)
}
}, [isSmallWidthViewport])
const classes = [
baseClass,
'field-type',
className,
showError && 'error',
disabled && `${baseClass}--read-only`,
editorConfig?.admin?.hideGutter !== true && !isSmallWidthViewport
? `${baseClass}--show-gutter`
: null,
]
.filter(Boolean)
.join(' ')
const pathWithEditDepth = `${path}.${editDepth}`
const dispatchFieldUpdateTask = useRef<number>(undefined)
const updateFieldValue = (editorState: EditorState) => {
const newState = editorState.toJSON()
prevValueRef.current = newState
setValue(newState)
}
const handleChange = useCallback(
(editorState: EditorState) => {
if (typeof window.requestIdleCallback === 'function') {
// Cancel earlier scheduled value updates,
// so that a CPU-limited event loop isn't flooded with n callbacks for n keystrokes into the rich text field,
// but that there's only ever the latest one state update
// dispatch task, to be executed with the next idle time,
// or the deadline of 500ms.
if (typeof window.cancelIdleCallback === 'function' && dispatchFieldUpdateTask.current) {
cancelIdleCallback(dispatchFieldUpdateTask.current)
}
// Schedule the state update to happen the next time the browser has sufficient resources,
// or the latest after 500ms.
dispatchFieldUpdateTask.current = requestIdleCallback(() => updateFieldValue(editorState), {
timeout: 500,
})
} else {
updateFieldValue(editorState)
}
},
[setValue],
)
const styles = useMemo(() => mergeFieldStyles(field), [field])
const handleInitialValueChange = useEffectEvent(
(initialValue: SerializedEditorState | undefined) => {
// Object deep equality check here, as re-mounting the editor if
// the new value is the same as the old one is not necessary
if (
prevValueRef.current !== value &&
JSON.stringify(prevValueRef.current) !== JSON.stringify(value)
) {
prevInitialValueRef.current = initialValue
prevValueRef.current = value
setRerenderProviderKey(new Date())
}
},
)
useEffect(() => {
// Needs to trigger for object reference changes - otherwise,
// reacting to the same initial value change twice will cause
// the second change to be ignored, even though the value has changed.
// That's because initialValue is not kept up-to-date
if (!Object.is(initialValue, prevInitialValueRef.current)) {
handleInitialValueChange(initialValue)
}
}, [initialValue])
return (
<div className={classes} key={pathWithEditDepth} style={styles}>
<RenderCustomComponent
CustomComponent={Error}
Fallback={<FieldError path={path} showError={showError} />}
/>
{Label || <FieldLabel label={label} localized={localized} path={path} required={required} />}
<div className={`${baseClass}__wrap`}>
<ErrorBoundary fallbackRender={fallbackRender} onReset={() => {}}>
{BeforeInput}
<LexicalProvider
composerKey={pathWithEditDepth}
editorConfig={editorConfig}
fieldProps={props}
isSmallWidthViewport={isSmallWidthViewport}
key={JSON.stringify({ path, rerenderProviderKey })} // makes sure lexical is completely re-rendered when initialValue changes, bypassing the lexical-internal value memoization. That way, external changes to the form will update the editor. More infos in PR description (https://github.com/payloadcms/payload/pull/5010)
onChange={handleChange}
readOnly={disabled}
value={value}
/>
{AfterInput}
</ErrorBoundary>
{Description}
<RenderCustomComponent
CustomComponent={Description}
Fallback={<FieldDescription description={description} path={path} />}
/>
</div>
</div>
)
}
function fallbackRender({ error }: { error: Error }) {
// Call resetErrorBoundary() to reset the error boundary and retry the render.
return (
<div className="errorBoundary" role="alert">
<p>Something went wrong:</p>
<pre style={{ color: 'red' }}>{error.message}</pre>
</div>
)
}
export const RichText: typeof RichTextComponent = RichTextComponent