Lexical: Allow images only

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-08-03 16:28:09 +02:00
parent 206a8d40e0
commit d5d6f89707

View file

@ -26,7 +26,6 @@ import { defineMessages, useIntl } from 'react-intl';
import { uploadFile } from 'soapbox/actions/compose'; import { uploadFile } from 'soapbox/actions/compose';
import { useAppDispatch, useInstance } from 'soapbox/hooks'; import { useAppDispatch, useInstance } from 'soapbox/hooks';
import { onlyImages } from '../../components/upload-button';
import { $createImageNode } from '../nodes/image-node'; import { $createImageNode } from '../nodes/image-node';
import { setFloatingElemPosition } from '../utils/set-floating-elem-position'; import { setFloatingElemPosition } from '../utils/set-floating-elem-position';
@ -47,17 +46,24 @@ const UploadButton: React.FC<IUploadButton> = ({ onSelectFile }) => {
const intl = useIntl(); const intl = useIntl();
const { configuration } = useInstance(); const { configuration } = useInstance();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const [disabled, setDisabled] = useState(false);
const fileElement = useRef<HTMLInputElement>(null); const fileElement = useRef<HTMLInputElement>(null);
const attachmentTypes = configuration.getIn(['media_attachments', 'supported_mime_types']) as ImmutableList<string>; const attachmentTypes = configuration.getIn(['media_attachments', 'supported_mime_types']) as ImmutableList<string>;
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => { const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
if (e.target.files?.length) { if (e.target.files?.length) {
setDisabled(true);
// @ts-ignore // @ts-ignore
dispatch(uploadFile( dispatch(uploadFile(
e.target.files.item(0) as File, e.target.files.item(0) as File,
intl, intl,
({ url }) => onSelectFile(url), ({ url }) => {
onSelectFile(url);
setDisabled(false);
},
() => setDisabled(false),
)); ));
} }
}; };
@ -66,11 +72,7 @@ const UploadButton: React.FC<IUploadButton> = ({ onSelectFile }) => {
fileElement.current?.click(); fileElement.current?.click();
}; };
const src = ( const src = require('@tabler/icons/photo.svg');
onlyImages(attachmentTypes)
? require('@tabler/icons/photo.svg')
: require('@tabler/icons/paperclip.svg')
);
return ( return (
<label> <label>
@ -80,13 +82,12 @@ const UploadButton: React.FC<IUploadButton> = ({ onSelectFile }) => {
icon={src} icon={src}
/> />
<input <input
// key={resetFileKey}
ref={fileElement} ref={fileElement}
type='file' type='file'
multiple multiple
accept={attachmentTypes && attachmentTypes.toArray().join(',')} accept={attachmentTypes ? attachmentTypes.filter(type => type.startsWith('image/')).toArray().join(',') : 'image/*'}
onChange={handleChange} onChange={handleChange}
// disabled={disabled} disabled={disabled}
className='hidden' className='hidden'
/> />
</label> </label>
@ -200,12 +201,12 @@ const BlockTypeFloatingToolbar = ({
> >
{editor.isEditable() && ( {editor.isEditable() && (
<> <>
<UploadButton onSelectFile={createImage} />
<ToolbarButton <ToolbarButton
onClick={createHorizontalLine} onClick={createHorizontalLine}
aria-label={intl.formatMessage(messages.createHorizontalLine)} aria-label={intl.formatMessage(messages.createHorizontalLine)}
icon={require('@tabler/icons/line-dashed.svg')} icon={require('@tabler/icons/line-dashed.svg')}
/> />
<UploadButton onSelectFile={createImage} />
</> </>
)} )}
</div> </div>