Skip to content

Commit

Permalink
worked on image dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
LiveDuo committed Nov 6, 2023
1 parent 2e85d35 commit 90b7140
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/client/vanilla/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ function Editor({ standaloneServer = false }) {
open={openImage}
setOpen={setOpenImage}
selectedElement={selectedElement as unknown as HTMLImageElement}
standaloneServer={standaloneServer}
baseUrl={getBaseUrl(standaloneServer)}
/>
<ButtonDialog
open={openButton}
Expand Down
15 changes: 11 additions & 4 deletions lib/client/vanilla/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import cx from 'classnames'
interface DialogProps {
open: boolean
setOpen: (open: boolean) => void
standaloneServer: boolean
selectedElement: HTMLImageElement
baseUrl: string
}

const Dialog: React.FC<DialogProps> = ({ open, setOpen, selectedElement, standaloneServer }) => {
const Dialog: React.FC<DialogProps> = ({ open, setOpen, selectedElement, baseUrl }) => {
const [url, setUrl] = useState<string>('')
const [urlText, setUrlText] = useState<string>('')
const [file, setFile] = useState<File | null>(null)
const input = useRef<HTMLInputElement>(null)

useEffect(() => {
Expand All @@ -27,6 +28,8 @@ const Dialog: React.FC<DialogProps> = ({ open, setOpen, selectedElement, standal
e.preventDefault()

const file = e.target.files![0]
setFile(file)

const reader = new FileReader()
reader.onload = (e) => setUrl(e.target!.result as string)
reader.readAsDataURL(file)
Expand All @@ -36,8 +39,12 @@ const Dialog: React.FC<DialogProps> = ({ open, setOpen, selectedElement, standal
if (url === urlText) {
selectedElement.src = url
} else {
// TODO upload image
// selectedElement.src = {uploaded_url}
const formData = new FormData()
formData.append('file-0', file!)
const uploadOptions = { method: 'POST', body: formData }
const res = await fetch(`${baseUrl}/api/builder/handle?type=data`, uploadOptions)
const urls = await res.json()
selectedElement.src = urls[0]
}

setOpen(false)
Expand Down

0 comments on commit 90b7140

Please sign in to comment.