Skip to content

Commit

Permalink
fix: disable d & d and upload UI if no image upload handler
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Jul 20, 2024
1 parent 08b4434 commit 4bdb69a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/examples/images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ some more
some
`

export const ImageWithNoBackend: Story<{ readOnly: boolean }> = () => {
return (
<>
<MDXEditor
markdown=""
plugins={[
imagePlugin({
imageUploadHandler: expressImageUploadHandler
}),
diffSourcePlugin(),
toolbarPlugin({
toolbarContents: () => (
<DiffSourceToggleWrapper>
<InsertImage />
</DiffSourceToggleWrapper>
)
})
]}
onChange={console.log}
/>
</>
)
}

export const ImageWithBackend: Story<{ readOnly: boolean }> = () => {
return (
<>
Expand Down
23 changes: 15 additions & 8 deletions src/plugins/image/ImageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react'
import { useForm } from 'react-hook-form'
import styles from '../../styles/ui.module.css'
import { editorRootElementRef$, useTranslation } from '../core/index'
import { closeImageDialog$, imageAutocompleteSuggestions$, imageDialogState$, saveImage$ } from './index'
import { closeImageDialog$, imageAutocompleteSuggestions$, imageDialogState$, imageUploadHandler$, saveImage$ } from './index'
import { DownshiftAutoComplete } from '../core/ui/DownshiftAutoComplete'
import { useCellValues, usePublisher } from '@mdxeditor/gurx'

Expand All @@ -16,10 +16,11 @@ interface ImageFormFields {
}

export const ImageDialog: React.FC = () => {
const [imageAutocompleteSuggestions, state, editorRootElementRef] = useCellValues(
const [imageAutocompleteSuggestions, state, editorRootElementRef, imageUploadHandler] = useCellValues(
imageAutocompleteSuggestions$,
imageDialogState$,
editorRootElementRef$
editorRootElementRef$,
imageUploadHandler$
)
const saveImage = usePublisher(saveImage$)
const closeImageDialog = usePublisher(closeImageDialog$)
Expand Down Expand Up @@ -57,13 +58,19 @@ export const ImageDialog: React.FC = () => {
}}
className={styles.multiFieldForm}
>
<div className={styles.formField}>
<label htmlFor="file">{t('uploadImage.uploadInstructions', 'Upload an image from your device:')}</label>
<input type="file" accept="image/*" {...register('file')} />
</div>
{imageUploadHandler !== null && (
<div className={styles.formField}>
<label htmlFor="file">{t('uploadImage.uploadInstructions', 'Upload an image from your device:')}</label>
<input type="file" accept="image/*" {...register('file')} />
</div>
)}

<div className={styles.formField}>
<label htmlFor="src">{t('uploadImage.addViaUrlInstructions', 'Or add an image from an URL:')}</label>
<label htmlFor="src">
{imageUploadHandler !== null
? t('uploadImage.addViaUrlInstructions', 'Or add an image from an URL:')
: t('uploadImage.addViaUrlInstructionsNoUpload', 'Add an image from an URL:')}
</label>
<DownshiftAutoComplete
register={register}
initialInputValue={state.type === 'editing' ? state.initialValues.src ?? '' : ''}
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export const imageDialogState$ = Cell<InactiveImageDialogState | NewImageDialogS
editor.registerCommand<DragEvent>(
DRAGSTART_COMMAND,
(event) => {
if (!theUploadHandler) {
return false
}
return onDragStart(event)
},
COMMAND_PRIORITY_HIGH
Expand Down

0 comments on commit 4bdb69a

Please sign in to comment.