Skip to content

Commit

Permalink
chore(fe): add alert when image size larger than 5mb (#2083)
Browse files Browse the repository at this point in the history
* chore(fe): add alert if upload image larger than 5mb

* chore(fe): delete console

* refactor(fe): refactor components
  • Loading branch information
youznn authored Sep 13, 2024
1 parent 471fe43 commit 2f51e8d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 96 deletions.
57 changes: 57 additions & 0 deletions apps/frontend/components/InsertDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use client'

import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger
} from '@/components/ui/dialog'
import type { Editor } from '@tiptap/core'
import React from 'react'
import { Button } from './ui/button'
import { Toggle } from './ui/toggle'

interface InsertDialogProps {
editor: Editor | null
activeType: string
triggerIcon: React.ReactNode
title: string
description: React.ReactNode
onInsert?: () => void
onToggleClick?: () => void
}

export default function InsertDialog({
editor,
activeType,
title,
description,
triggerIcon,
onInsert,
onToggleClick
}: InsertDialogProps) {
return (
<Dialog>
<DialogTrigger asChild onClick={onToggleClick}>
<Toggle size="sm" pressed={editor?.isActive({ activeType })}>
{triggerIcon}
</Toggle>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
</DialogHeader>
<DialogDescription>{description}</DialogDescription>
<DialogFooter>
<DialogClose asChild>
<Button onClick={onInsert}>Insert</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
160 changes: 64 additions & 96 deletions apps/frontend/components/TextEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import { CautionDialog } from '@/app/admin/problem/_components/CautionDialog'
import {
Dialog,
DialogContent,
Expand Down Expand Up @@ -37,6 +38,7 @@ import {
ImagePlus
} from 'lucide-react'
import { useCallback, useState } from 'react'
import InsertDialog from './InsertDialog'
import { Button } from './ui/button'

function MathPreview(props: NodeViewWrapperProps) {
Expand Down Expand Up @@ -159,6 +161,9 @@ export default function TextEditor({
const [url, setUrl] = useState('')
const [imageUrl, setImageUrl] = useState<string | undefined>('')
const [equation, setEquation] = useState('')
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [dialogDescription, setDialogDescription] = useState<string>('')

const handleEquation = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setEquation(event.target.value)
Expand Down Expand Up @@ -194,14 +199,12 @@ export default function TextEditor({

const setLink = useCallback(
(linkUrl: string | null) => {
console.log(linkUrl)
if (!editor) return null

// cancelled
if (linkUrl === null) {
return
}
console.log('실행됨')
console.log('url: ' + linkUrl)
// empty
if (linkUrl === '') {
editor.chain().focus().extendMarkRange('link').unsetLink().run()
Expand All @@ -220,6 +223,7 @@ export default function TextEditor({

const addImage = useCallback(
(imageUrl: string | undefined) => {
console.log(imageUrl)
if (!editor) return null
if (imageUrl === null) {
return
Expand All @@ -245,9 +249,16 @@ export default function TextEditor({
input: { file }
}
})
setImageUrl(data?.uploadImage.src)
console.log('data', data)
setImageUrl(data?.uploadImage.src ?? '')
} catch (error) {
console.error('Error uploading file:', error)
if (error instanceof Error) {
const errorMessage = error.message
if (errorMessage === 'File size exceeds maximum limit') {
setDialogDescription('Images larger than 5MB cannot be uploaded.')
setIsDialogOpen(true)
}
}
}
}

Expand Down Expand Up @@ -287,91 +298,45 @@ export default function TextEditor({
<ListOrdered className="h-[14px] w-[14px]" />
</Toggle>

<Dialog>
<DialogTrigger asChild>
<Toggle size="sm" pressed={editor?.isActive('link')}>
<LinkIcon className="h-[14px] w-[14px]" />
</Toggle>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Insert URL</DialogTitle>
</DialogHeader>
<DialogDescription>
<Input
placeholder="Enter URL"
onChange={(e) => {
setUrl(e.target.value)
}}
/>
</DialogDescription>
<DialogFooter>
<DialogClose asChild>
<Button
onClick={() => {
setLink(url)
}}
>
Insert
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
<InsertDialog
editor={editor}
activeType="link"
title="Insert Link"
description={
<Input
placeholder="Enter URL"
onChange={(e) => setUrl(e.target.value)}
/>
}
triggerIcon={<LinkIcon className="h-[14px] w-[14px]" />}
onInsert={() => setLink(url)}
/>

<Dialog>
<DialogTrigger asChild>
<Toggle
size="sm"
pressed={editor?.isActive('katex')}
onPressedChange={() => {
setEquation('')
}}
>
<Pi className="h-[14px] w-[14px]" />
</Toggle>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Insert Equation</DialogTitle>
</DialogHeader>
<DialogDescription>
<Input placeholder="Enter Equation" onChange={handleEquation} />
<Tex block className="text-black">
{equation}
</Tex>
</DialogDescription>
<DialogFooter>
<DialogClose asChild>
<Button
onClick={() => {
editor
?.chain()
.focus()
.insertContent(
`<math-component content="${equation}"></math-component>`
)
.run()
}}
>
Insert
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
<InsertDialog
editor={editor}
activeType="katex"
title="Insert Equation"
description={
<Input placeholder="Enter Equation" onChange={handleEquation} />
}
triggerIcon={<Pi className="h-[14px] w-[14px]" />}
onInsert={() => {
editor
?.chain()
.focus()
.insertContent(
`<math-component content="${equation}"></math-component>`
)
.run()
}}
/>

<Dialog>
<DialogTrigger asChild>
<Toggle size="sm" pressed={editor?.isActive('image')}>
<ImagePlus className="h-[14px] w-[14px]" />
</Toggle>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Upload Image</DialogTitle>
</DialogHeader>
<DialogDescription className="flex-col gap-1">
<InsertDialog
editor={editor}
activeType="image"
title="Upload Image"
description={
<>
<Input
type="file"
accept="image/*"
Expand All @@ -380,15 +345,18 @@ export default function TextEditor({
}}
/>
<p className="text-sm"> * Image must be under 5MB</p>
</DialogDescription>
<DialogFooter className="flex items-center justify-between">
<DialogClose asChild>
<Button onClick={() => addImage(imageUrl)}>Insert</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
</>
}
triggerIcon={<ImagePlus className="h-[14px] w-[14px]" />}
onInsert={() => addImage(imageUrl)}
onToggleClick={() => setImageUrl('')}
/>
</div>
<CautionDialog
isOpen={isDialogOpen}
onClose={() => setIsDialogOpen(false)}
description={dialogDescription}
/>
<EditorContent className="prose max-w-5xl" editor={editor} />
</div>
)
Expand Down

0 comments on commit 2f51e8d

Please sign in to comment.