|
| 1 | +import * as Dialog from '@radix-ui/react-dialog' |
| 2 | +import classNames from 'classnames' |
| 3 | +import React from 'react' |
| 4 | +import { useForm } from 'react-hook-form' |
| 5 | +import styles from '../../styles/ui.module.css' |
| 6 | +import { corePluginHooks } from '../core/index' |
| 7 | +import { imagePluginHooks } from './index' |
| 8 | +import { DownshiftAutoComplete } from '../core/ui/DownshiftAutoComplete' |
| 9 | + |
| 10 | +interface ImageFormFields { |
| 11 | + src: string |
| 12 | + title: string |
| 13 | + altText: string |
| 14 | + file: FileList |
| 15 | +} |
| 16 | + |
| 17 | +export const ImageDialog: React.FC = () => { |
| 18 | + const [imageAutocompleteSuggestions, state] = imagePluginHooks.useEmitterValues('imageAutocompleteSuggestions', 'imageDialogState') |
| 19 | + const saveImage = imagePluginHooks.usePublisher('saveImage') |
| 20 | + const [editorRootElementRef] = corePluginHooks.useEmitterValues('editorRootElementRef') |
| 21 | + const closeImageDialog = imagePluginHooks.usePublisher('closeImageDialog') |
| 22 | + |
| 23 | + const { register, handleSubmit, control, setValue, reset } = useForm<ImageFormFields>({ |
| 24 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 25 | + values: state.type === 'editing' ? (state.initialValues as any) : {} |
| 26 | + }) |
| 27 | + |
| 28 | + return ( |
| 29 | + <Dialog.Root |
| 30 | + open={state.type !== 'inactive'} |
| 31 | + onOpenChange={(open) => { |
| 32 | + if (!open) { |
| 33 | + closeImageDialog(true) |
| 34 | + reset({ src: '', title: '', altText: '' }) |
| 35 | + } |
| 36 | + }} |
| 37 | + > |
| 38 | + <Dialog.Portal container={editorRootElementRef?.current}> |
| 39 | + <Dialog.Overlay className={styles.dialogOverlay} /> |
| 40 | + <Dialog.Content |
| 41 | + className={styles.dialogContent} |
| 42 | + onOpenAutoFocus={(e) => { |
| 43 | + e.preventDefault() |
| 44 | + }} |
| 45 | + > |
| 46 | + <form |
| 47 | + onSubmit={(e) => { |
| 48 | + void handleSubmit(saveImage)(e) |
| 49 | + reset({ src: '', title: '', altText: '' }) |
| 50 | + e.nativeEvent.stopImmediatePropagation() |
| 51 | + }} |
| 52 | + className={styles.multiFieldForm} |
| 53 | + > |
| 54 | + <div className={styles.formField}> |
| 55 | + <label htmlFor="file">Upload an image from your device:</label> |
| 56 | + <input type="file" {...register('file')} /> |
| 57 | + </div> |
| 58 | + |
| 59 | + <div className={styles.formField}> |
| 60 | + <label htmlFor="src">Or add an image from an URL:</label> |
| 61 | + <DownshiftAutoComplete |
| 62 | + initialInputValue={state.type === 'editing' ? state.initialValues.src || '' : ''} |
| 63 | + inputName="src" |
| 64 | + suggestions={imageAutocompleteSuggestions} |
| 65 | + setValue={setValue} |
| 66 | + control={control} |
| 67 | + placeholder="Select or paste an image src" |
| 68 | + /> |
| 69 | + </div> |
| 70 | + |
| 71 | + <div className={styles.formField}> |
| 72 | + <label htmlFor="alt">Alt:</label> |
| 73 | + <input type="text" {...register('altText')} className={styles.textInput} /> |
| 74 | + </div> |
| 75 | + |
| 76 | + <div className={styles.formField}> |
| 77 | + <label htmlFor="title">Title:</label> |
| 78 | + <input type="text" {...register('title')} className={styles.textInput} /> |
| 79 | + </div> |
| 80 | + |
| 81 | + <div style={{ display: 'flex', justifyContent: 'flex-end', gap: 'var(--spacing-2)' }}> |
| 82 | + <button type="submit" title="Save" aria-label="Save" className={classNames(styles.primaryButton)}> |
| 83 | + Save |
| 84 | + </button> |
| 85 | + <Dialog.Close asChild> |
| 86 | + <button type="reset" title="Cancel" aria-label="Cancel" className={classNames(styles.secondaryButton)}> |
| 87 | + Cancel |
| 88 | + </button> |
| 89 | + </Dialog.Close> |
| 90 | + </div> |
| 91 | + </form> |
| 92 | + </Dialog.Content> |
| 93 | + </Dialog.Portal> |
| 94 | + </Dialog.Root> |
| 95 | + ) |
| 96 | +} |
0 commit comments