diff --git a/web/components/editor.tsx b/web/components/editor.tsx index 8f6f6929..c8676a7b 100644 --- a/web/components/editor.tsx +++ b/web/components/editor.tsx @@ -7,6 +7,7 @@ import clsx from 'clsx' import { useEffect } from 'react' import { Linkify } from './linkify' import { uploadImage } from 'web/lib/firebase/storage' +import { useMutation } from 'react-query' const LINE_HEIGHT = 2 @@ -23,6 +24,10 @@ export function useTextEditor(props: { const rowsClass = rows && `box-content min-h-[${LINE_HEIGHT * rows}em]` + const upload = useMutation((files: File[]) => + Promise.all(files.map((file) => uploadImage('default', file))) + ) + const editor = useEditor({ editorProps: { attributes: { @@ -38,18 +43,16 @@ export function useTextEditor(props: { } event.preventDefault() - ;(async () => { - // TODO: show loading state, compress large images - const urls = await Promise.all( - files.map((file) => uploadImage('default', file)) - ) - let trans = view.state.tr - urls.forEach((src: any) => { - const node = view.state.schema.nodes.image.create({ src }) - trans = trans.insert(view.state.selection.to, node) - }) - view.dispatch(trans) - })() + upload.mutate(files, { + onSuccess: (urls) => { + let trans = view.state.tr + urls.forEach((src: any) => { + const node = view.state.schema.nodes.image.create({ src }) + trans = trans.insert(view.state.selection.to, node) + }) + view.dispatch(trans) + }, + }) }, }, extensions: [ @@ -65,7 +68,7 @@ export function useTextEditor(props: { editor?.setEditable(!disabled) }, [editor, disabled]) - return editor + return { editor, upload } } function RichContent(props: { content: JSONContent }) { diff --git a/web/lib/firebase/storage.ts b/web/lib/firebase/storage.ts index e7794580..5293a6bc 100644 --- a/web/lib/firebase/storage.ts +++ b/web/lib/firebase/storage.ts @@ -7,6 +7,7 @@ import { const storage = getStorage() +// TODO: compress large images export const uploadImage = async ( username: string, file: File, diff --git a/web/pages/create.tsx b/web/pages/create.tsx index 2da8a881..e098b3d7 100644 --- a/web/pages/create.tsx +++ b/web/pages/create.tsx @@ -188,7 +188,7 @@ export function NewContract(props: { ? `e.g. This question resolves to "YES" if they receive the majority of votes...` : `e.g. I will choose the answer according to...` - const editor = useTextEditor({ + const { editor, upload } = useTextEditor({ rows: 3, max: MAX_DESCRIPTION_LENGTH, placeholder: descriptionPlaceholder, @@ -442,12 +442,18 @@ export function NewContract(props: { -
-