Open file explorer to select image

This commit is contained in:
ekzyis 2023-10-18 18:24:02 +02:00 committed by ekzyis
parent 955e5cfc88
commit de7b8547ec
2 changed files with 39 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import textAreaCaret from 'textarea-caret'
import ReactDatePicker from 'react-datepicker' import ReactDatePicker from 'react-datepicker'
import 'react-datepicker/dist/react-datepicker.css' import 'react-datepicker/dist/react-datepicker.css'
import { debounce } from './use-debounce-callback' import { debounce } from './use-debounce-callback'
import { ImageUpload } from './image'
export function SubmitButton ({ export function SubmitButton ({
children, variant, value, onClick, disabled, cost, ...props children, variant, value, onClick, disabled, cost, ...props
@ -221,7 +222,9 @@ export function MarkdownInput ({ label, topLevel, groupClassName, onChange, setH
<Nav.Link className={styles.previewTab} eventKey='preview' disabled={!meta.value}>preview</Nav.Link> <Nav.Link className={styles.previewTab} eventKey='preview' disabled={!meta.value}>preview</Nav.Link>
</Nav.Item> </Nav.Item>
<span className='ms-auto text-muted d-flex align-items-center'> <span className='ms-auto text-muted d-flex align-items-center'>
<AddImageIcon className='me-1' width={18} height={18} /> <ImageUpload className='d-flex align-items-center me-1'>
<AddImageIcon width={18} height={18} onClick={() => console.log('icon click')} />
</ImageUpload>
<a <a
className='d-flex align-items-center' className='d-flex align-items-center'
href='https://guides.github.com/features/mastering-markdown/' target='_blank' rel='noreferrer' href='https://guides.github.com/features/mastering-markdown/' target='_blank' rel='noreferrer'

View File

@ -1,9 +1,11 @@
import styles from './text.module.css' import styles from './text.module.css'
import { useState, useEffect, useMemo, useCallback } from 'react' import { Fragment, useState, useEffect, useMemo, useCallback, useRef } from 'react'
import { IMGPROXY_URL_REGEXP } from '../lib/url' import { IMGPROXY_URL_REGEXP } from '../lib/url'
import { useShowModal } from './modal' import { useShowModal } from './modal'
import { useMe } from './me' import { useMe } from './me'
import { Dropdown } from 'react-bootstrap' import { Dropdown } from 'react-bootstrap'
import { UPLOAD_TYPES_ALLOW } from '../lib/constants'
import { useToast } from './toast'
export function decodeOriginalUrl (imgproxyUrl) { export function decodeOriginalUrl (imgproxyUrl) {
const parts = imgproxyUrl.split('/') const parts = imgproxyUrl.split('/')
@ -132,3 +134,35 @@ export default function ZoomableImage ({ src, srcSet, ...props }) {
return <ImageOriginal src={originalUrl} onClick={handleClick} {...props} /> return <ImageOriginal src={originalUrl} onClick={handleClick} {...props} />
} }
export function ImageUpload ({ children, className, onSelect, onSuccess }) {
const ref = useRef()
const toaster = useToast()
return (
<>
<input
ref={ref}
type='file'
className='d-none'
accept={UPLOAD_TYPES_ALLOW.join(', ')}
onChange={(e) => {
if (e.target.files.length === 0) {
return
}
const file = e.target.files[0]
if (UPLOAD_TYPES_ALLOW.indexOf(file.type) === -1) {
toaster.danger(`image must be ${UPLOAD_TYPES_ALLOW.map(t => t.replace('image/', '')).join(', ')}`)
return
}
onSelect?.(file)
// TODO find out if this is needed and if so, why (copied from components/upload.js)
e.target.value = null
}}
/>
<div className={className} onClick={() => ref.current?.click()} style={{ cursor: 'pointer' }}>
{children}
</div>
</>
)
}