Skip to content

Commit

Permalink
feat(fe): show toast when copied (#2008)
Browse files Browse the repository at this point in the history
* feat(fe): show-toast-when-copied

* chore(fe): improve code
  • Loading branch information
jwoojin9 authored Aug 26, 2024
1 parent 56c2216 commit eb22b9f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion apps/frontend/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { tags as t } from '@lezer/highlight'
import { createTheme } from '@uiw/codemirror-themes'
import type { ReactCodeMirrorProps } from '@uiw/react-codemirror'
import ReactCodeMirror, { EditorView } from '@uiw/react-codemirror'
import { toast } from 'sonner'
import { ScrollArea, ScrollBar } from './ui/scroll-area'

const editorTheme = createTheme({
Expand Down Expand Up @@ -69,12 +70,15 @@ interface Props extends ReactCodeMirrorProps {
const copyPasteHandler = () => {
return EditorView.domEventHandlers({
paste(event) {
toast.error('Copying and pasting is not allowed')
event.preventDefault()
},
copy(event) {
toast.error('Copying and pasting is not allowed')
event.preventDefault()
},
cut(event) {
toast.error('Copying and pasting is not allowed')
event.preventDefault()
}
})
Expand All @@ -95,7 +99,7 @@ export default function CodeEditor({
extensions={[
fontSize,
languageParser[language](),
enableCopyPaste ? [] : [copyPasteHandler()]
enableCopyPaste ? [] : copyPasteHandler()
]}
value={value}
onChange={onChange}
Expand Down
24 changes: 24 additions & 0 deletions apps/frontend/components/EditorDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@/components/ui/dialog'
import { renderKatex } from '@/lib/renderKatex'
import { convertToLetter } from '@/lib/utils'
import CopyIcon from '@/public/24_copy.svg'
import compileIcon from '@/public/compileVersion.svg'
import copyIcon from '@/public/copy.svg'
import copyCompleteIcon from '@/public/copyComplete.svg'
Expand All @@ -27,6 +28,7 @@ import { FileText } from 'lucide-react'
import Image from 'next/image'
import { useState, useEffect, useRef } from 'react'
import useCopyToClipboard from 'react-use/lib/useCopyToClipboard'
import { toast } from 'sonner'
import {
Tooltip,
TooltipContent,
Expand Down Expand Up @@ -158,6 +160,17 @@ export function EditorDescription({
<Image
onClick={() => {
copy(input + '\n\n', `input-${id}`) // add newline to the end for easy testing
toast('Successfully copied', {
unstyled: true,
closeButton: false,
icon: <Image src={CopyIcon} alt="copy" />,
style: { backgroundColor: '#f0f8ff' },
classNames: {
toast:
'inline-flex items-center py-2 px-3 rounded gap-2',
title: 'text-primary font-medium'
}
})
}}
className="cursor-pointer transition-opacity hover:opacity-60"
src={copyIcon}
Expand Down Expand Up @@ -210,6 +223,17 @@ export function EditorDescription({
<Image
onClick={() => {
copy(output + '\n\n', `output-${id}`) // add newline to the end for easy testing
toast('Successfully copied', {
unstyled: true,
closeButton: false,
icon: <Image src={CopyIcon} alt="copy" />,
style: { backgroundColor: '#f0f8ff' },
classNames: {
toast:
'inline-flex items-center py-2 px-3 rounded gap-2',
title: 'text-primary font-medium'
}
})
}}
className="cursor-pointer transition-opacity hover:opacity-60"
src={copyIcon}
Expand Down
7 changes: 7 additions & 0 deletions apps/frontend/public/24_copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eb22b9f

Please sign in to comment.