Connect, collaborate, and grow with a community of TypeScript developers
allow images to be uploaded on solutions (#314)
bautistaaaa7470e53169b
diff --git a/src/components/ui/rich-markdown-editor.tsx b/src/components/ui/rich-markdown-editor.tsxindex 3b7aa6ff..9093b4f3 100644--- a/src/components/ui/rich-markdown-editor.tsx+++ b/src/components/ui/rich-markdown-editor.tsx@@ -1,52 +1,11 @@import { useTheme } from 'next-themes';import { useContext, useEffect, useRef, useState } from 'react';-import MDEditor, { commands, type ICommand, EditorContext } from '@uiw/react-md-editor';-import { useUploadThing } from '~/utils/useUploadthing';+import MDEditor, { EditorContext, commands, type ICommand } from '@uiw/react-md-editor';import { insertText } from '~/utils/domUtils';+import { useUploadThing } from '~/utils/useUploadthing';import { toast } from './use-toast';-const PreviewToggle = () => {- const { preview, dispatch } = useContext(EditorContext);- const click = () => {- dispatch?.({- preview: preview === 'edit' ? 'preview' : 'edit',- });- };- if (preview === 'edit') {- return (- // TODO: styles don't work for some reason- <button- className="mr-2 w-10 px-2 font-bold"- style={{- width: '4rem !important',- padding: '0.25rem 1rem !important',- fontWeight: 'bold !important',- }}- type="button"- onClick={click}- >- Preview- </button>- );- }- return (- // TODO: styles don't work for some reason- <button- className="mr-2 w-10 px-2 font-bold"- style={{- width: '4rem !important',- padding: '0.25rem 1rem !important',- fontWeight: 'bold !important',- }}- type="button"- onClick={click}- >- Edit- </button>- );-};-const codePreview: ICommand = {name: 'preview',keyCommand: 'preview',@@ -72,9 +31,10 @@ export function RichMarkdownEditor({onChange,allowImageUpload = false,}: Props) {- // @ts-ignore someone fix the types, #GFI- const editorRef = useRef<MDEditor>(null);+ const editorRef = useRef<typeof MDEditor>(null);+ const fileInputRef = useRef<HTMLInputElement>(null);const [isImageUploading, setIsImageUploading] = useState(false);+ const handleFileChange = () => {};const { theme } = useTheme();useEffect(() => {@@ -85,9 +45,41 @@ export function RichMarkdownEditor({const extraCommands = [...(dismissPreview ? [] : [codePreview, commands.fullscreen])];+ const uploadImage: ICommand = {+ name: 'Upload Image',+ keyCommand: 'image',+ buttonProps: { 'aria-label': 'Insert title3' },+ icon: (+ <svg width="12" height="12" viewBox="0 0 20 20">+ <path+ fill="currentColor"+ d="M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 13l-6-5-2 2-4-5-4 8V4h16v11z"+ ></path>+ </svg>+ ),+ execute: async () => {+ const ref = fileInputRef.current!;++ if (!ref) return;++ // @ts-ignore+ ref.value = null;+ ref.click();+ },+ };++ const myCommands = [+ commands.bold,+ commands.link,+ commands.italic,+ commands.strikethrough,+ commands.codeBlock,+ commands.code,+ ...(allowImageUpload ? [uploadImage] : []),+ ];+const { startUpload } = useUploadThing('imageUploader', {onClientUploadComplete: (res) => {- console.log('client upload complete');if (!res) return;// fist file in the arrayconst uploadedFile = res[0];@@ -95,6 +87,7 @@ export function RichMarkdownEditor({// insert string at cursor position by calling on changeinsertText(``,+ // @ts-ignoreeditorRef.current.textarea,);@@ -106,7 +99,7 @@ export function RichMarkdownEditor({});},onUploadError: (err) => {- console.log(err);+ console.error(err);setIsImageUploading(false);@@ -117,6 +110,24 @@ export function RichMarkdownEditor({},});+ useEffect(() => {+ const ref = fileInputRef.current!;+ if (!ref || !allowImageUpload) return;++ const handler = async () => {+ if (!ref.files?.[0]) return;+ setIsImageUploading(true);++ await startUpload([ref.files[0]]);+ };++ ref.addEventListener('change', handler);++ return () => {+ ref.removeEventListener('change', handler);+ };+ }, [startUpload, allowImageUpload]);+const handlePasta = async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {// only allow image if it's enabledif (!allowImageUpload) return;@@ -148,6 +159,7 @@ export function RichMarkdownEditor({preview="edit"visibleDragbar={false}extraCommands={extraCommands}+ commands={myCommands}// @ts-ignoreonChange={onChange}// @ts-ignore@@ -160,7 +172,6 @@ export function RichMarkdownEditor({command.keyCommand === 'hr' ||command.keyCommand === 'link' ||command.keyCommand === 'quote' ||- command.keyCommand === 'image' ||command.keyCommand === 'comment' ||command.keyCommand === 'list' ||// TODO: these aren't under command.keyCommand?@@ -197,6 +208,48 @@ export function RichMarkdownEditor({<div>Uploading image...</div></div>)}+ <input type="file" ref={fileInputRef} className="hidden" onChange={handleFileChange} /></div>);}++function PreviewToggle() {+ const { preview, dispatch } = useContext(EditorContext);+ const click = () => {+ dispatch?.({+ preview: preview === 'edit' ? 'preview' : 'edit',+ });+ };+ if (preview === 'edit') {+ return (+ // TODO: styles don't work for some reason+ <button+ className="mr-2 w-10 px-2 font-bold"+ style={{+ width: '4rem !important',+ padding: '0.25rem 1rem !important',+ fontWeight: 'bold !important',+ }}+ type="button"+ onClick={click}+ >+ Preview+ </button>+ );+ }+ return (+ // TODO: styles don't work for some reason+ <button+ className="mr-2 w-10 px-2 font-bold"+ style={{+ width: '4rem !important',+ padding: '0.25rem 1rem !important',+ fontWeight: 'bold !important',+ }}+ type="button"+ onClick={click}+ >+ Edit+ </button>+ );+}