-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy path__note-editor.tsx
271 lines (259 loc) · 8 KB
/
__note-editor.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
259
260
261
262
263
264
265
266
267
268
269
270
271
import {
FormProvider,
getFieldsetProps,
getFormProps,
getInputProps,
getTextareaProps,
useForm,
type FieldMetadata,
} from '@conform-to/react'
import { getZodConstraint, parseWithZod } from '@conform-to/zod'
import { type Note, type NoteImage } from '@prisma/client'
import { type SerializeFrom } from '@remix-run/node'
import { Form, useActionData } from '@remix-run/react'
import { useState } from 'react'
import { z } from 'zod'
import { GeneralErrorBoundary } from '#app/components/error-boundary.tsx'
import { floatingToolbarClassName } from '#app/components/floating-toolbar.tsx'
import { ErrorList, Field, TextareaField } from '#app/components/forms.tsx'
import { Button } from '#app/components/ui/button.tsx'
import { Icon } from '#app/components/ui/icon.tsx'
import { Label } from '#app/components/ui/label.tsx'
import { StatusButton } from '#app/components/ui/status-button.tsx'
import { Textarea } from '#app/components/ui/textarea.tsx'
import { cn, getNoteImgSrc, useIsPending } from '#app/utils/misc.tsx'
import { type action } from './__note-editor.server'
const titleMinLength = 1
const titleMaxLength = 100
const contentMinLength = 1
const contentMaxLength = 10000
export const MAX_UPLOAD_SIZE = 1024 * 1024 * 3 // 3MB
const ImageFieldsetSchema = z.object({
id: z.string().optional(),
file: z
.instanceof(File)
.optional()
.refine((file) => {
return !file || file.size <= MAX_UPLOAD_SIZE
}, 'File size must be less than 3MB'),
altText: z.string().optional(),
})
export type ImageFieldset = z.infer<typeof ImageFieldsetSchema>
export const NoteEditorSchema = z.object({
id: z.string().optional(),
title: z.string().min(titleMinLength).max(titleMaxLength),
content: z.string().min(contentMinLength).max(contentMaxLength),
images: z.array(ImageFieldsetSchema).max(5).optional(),
})
export function NoteEditor({
note,
}: {
note?: SerializeFrom<
Pick<Note, 'id' | 'title' | 'content'> & {
images: Array<Pick<NoteImage, 'id' | 'altText'>>
}
>
}) {
const actionData = useActionData<typeof action>()
const isPending = useIsPending()
const [form, fields] = useForm({
id: 'note-editor',
constraint: getZodConstraint(NoteEditorSchema),
lastResult: actionData?.result,
onValidate({ formData }) {
return parseWithZod(formData, { schema: NoteEditorSchema })
},
defaultValue: {
...note,
images: note?.images ?? [{}],
},
shouldRevalidate: 'onBlur',
})
const imageList = fields.images.getFieldList()
return (
<div className="absolute inset-0">
<FormProvider context={form.context}>
<Form
method="POST"
className="flex h-full flex-col gap-y-4 overflow-y-auto overflow-x-hidden px-10 pb-28 pt-12"
{...getFormProps(form)}
encType="multipart/form-data"
>
{/*
This hidden submit button is here to ensure that when the user hits
"enter" on an input field, the primary form function is submitted
rather than the first button in the form (which is delete/add image).
*/}
<button type="submit" className="hidden" />
{note ? <input type="hidden" name="id" value={note.id} /> : null}
<div className="flex flex-col gap-1">
<Field
labelProps={{ children: 'Title' }}
inputProps={{
autoFocus: true,
...getInputProps(fields.title, { type: 'text' }),
}}
errors={fields.title.errors}
/>
<TextareaField
labelProps={{ children: 'Content' }}
textareaProps={{
...getTextareaProps(fields.content),
}}
errors={fields.content.errors}
/>
<div>
<Label>Images</Label>
<ul className="flex flex-col gap-4">
{imageList.map((image, index) => {
console.log('image.key', image.key)
return (
<li
key={image.key}
className="relative border-b-2 border-muted-foreground"
>
<button
className="absolute right-0 top-0 text-foreground-destructive"
{...form.remove.getButtonProps({
name: fields.images.name,
index,
})}
>
<span aria-hidden>
<Icon name="cross-1" />
</span>{' '}
<span className="sr-only">
Remove image {index + 1}
</span>
</button>
<ImageChooser meta={image} />
</li>
)
})}
</ul>
</div>
<Button
className="mt-3"
{...form.insert.getButtonProps({ name: fields.images.name })}
>
<span aria-hidden>
<Icon name="plus">Image</Icon>
</span>{' '}
<span className="sr-only">Add image</span>
</Button>
</div>
<ErrorList id={form.errorId} errors={form.errors} />
</Form>
<div className={floatingToolbarClassName}>
<Button variant="destructive" {...form.reset.getButtonProps()}>
Reset
</Button>
<StatusButton
form={form.id}
type="submit"
disabled={isPending}
status={isPending ? 'pending' : 'idle'}
>
Submit
</StatusButton>
</div>
</FormProvider>
</div>
)
}
function ImageChooser({ meta }: { meta: FieldMetadata<ImageFieldset> }) {
const fields = meta.getFieldset()
const existingImage = Boolean(fields.id.initialValue)
const [previewImage, setPreviewImage] = useState<string | null>(
fields.id.initialValue ? getNoteImgSrc(fields.id.initialValue) : null,
)
const [altText, setAltText] = useState(fields.altText.initialValue ?? '')
return (
<fieldset {...getFieldsetProps(meta)}>
<div className="flex gap-3">
<div className="w-32">
<div className="relative h-32 w-32">
<label
htmlFor={fields.file.id}
className={cn('group absolute h-32 w-32 rounded-lg', {
'bg-accent opacity-40 focus-within:opacity-100 hover:opacity-100':
!previewImage,
'cursor-pointer focus-within:ring-2': !existingImage,
})}
>
{previewImage ? (
<div className="relative">
<img
src={previewImage}
alt={altText ?? ''}
className="h-32 w-32 rounded-lg object-cover"
/>
{existingImage ? null : (
<div className="pointer-events-none absolute -right-0.5 -top-0.5 rotate-12 rounded-sm bg-secondary px-2 py-1 text-xs text-secondary-foreground shadow-md">
new
</div>
)}
</div>
) : (
<div className="flex h-32 w-32 items-center justify-center rounded-lg border border-muted-foreground text-4xl text-muted-foreground">
<Icon name="plus" />
</div>
)}
{existingImage ? (
<input {...getInputProps(fields.id, { type: 'hidden' })} />
) : null}
<input
aria-label="Image"
className="absolute left-0 top-0 z-0 h-32 w-32 cursor-pointer opacity-0"
onChange={(event) => {
const file = event.target.files?.[0]
if (file) {
const reader = new FileReader()
reader.onloadend = () => {
setPreviewImage(reader.result as string)
}
reader.readAsDataURL(file)
} else {
setPreviewImage(null)
}
}}
accept="image/*"
{...getInputProps(fields.file, { type: 'file' })}
/>
</label>
</div>
<div className="min-h-[32px] px-4 pb-3 pt-1">
<ErrorList id={fields.file.errorId} errors={fields.file.errors} />
</div>
</div>
<div className="flex-1">
<Label htmlFor={fields.altText.id}>Alt Text</Label>
<Textarea
onChange={(e) => setAltText(e.currentTarget.value)}
{...getTextareaProps(fields.altText)}
/>
<div className="min-h-[32px] px-4 pb-3 pt-1">
<ErrorList
id={fields.altText.errorId}
errors={fields.altText.errors}
/>
</div>
</div>
</div>
<div className="min-h-[32px] px-4 pb-3 pt-1">
<ErrorList id={meta.errorId} errors={meta.errors} />
</div>
</fieldset>
)
}
export function ErrorBoundary() {
return (
<GeneralErrorBoundary
statusHandlers={{
404: ({ params }) => (
<p>No note with the id "{params.noteId}" exists</p>
),
}}
/>
)
}