-
Notifications
You must be signed in to change notification settings - Fork 0
Add CodeEditor component #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { Editor } from '@monaco-editor/react' | ||
| import { useCallback } from 'react' | ||
| import { Badge } from '@/components/ui/badge' | ||
| import { Alert, AlertDescription } from '@/components/ui/alert' | ||
| import type { Language } from '@/types/editor' | ||
|
|
||
| interface CodeEditorProps { | ||
| language: Language | ||
| value: string | ||
| onChange: (value: string) => void | ||
| maxLength?: number | ||
| placeholder?: string | ||
| theme?: 'light' | 'dark' | ||
| } | ||
|
|
||
| export function CodeEditor({ | ||
| language, | ||
| value, | ||
| onChange, | ||
| maxLength = 1024, | ||
| placeholder = 'Cole seu código aqui...', | ||
| theme = 'light', | ||
| }: CodeEditorProps) { | ||
| const handleEditorChange = useCallback( | ||
| (val: string | undefined) => { | ||
| const newValue = val || '' | ||
| onChange(newValue) | ||
| }, | ||
| [onChange], | ||
| ) | ||
|
|
||
| const isOverLimit = value.length > maxLength | ||
| const remainingChars = maxLength - value.length | ||
|
|
||
| return ( | ||
| <div className="space-y-4"> | ||
| <div className="relative"> | ||
| <Editor | ||
| height="400px" | ||
| language={language} | ||
| value={value} | ||
| onChange={handleEditorChange} | ||
| options={{ | ||
| minimap: { enabled: false }, | ||
| fontSize: 14, | ||
| fontFamily: 'Fira Code, Monaco, monospace', | ||
| wordWrap: 'on', | ||
| scrollBeyondLastLine: false, | ||
| automaticLayout: true, | ||
| lineNumbers: 'on', | ||
| renderWhitespace: 'boundary', | ||
| }} | ||
| theme={theme === 'dark' ? 'vs-dark' : 'vs-light'} | ||
| placeholder={placeholder} | ||
| /> | ||
| </div> | ||
|
|
||
| <div className="flex justify-between items-center"> | ||
| <Badge variant={isOverLimit ? 'destructive' : 'secondary'}> | ||
| {value.length}/{maxLength} caracteres | ||
|
||
| </Badge> | ||
|
|
||
| {isOverLimit && ( | ||
| <Alert className="w-auto"> | ||
| <AlertDescription> | ||
| Limite excedido em {Math.abs(remainingChars)} caracteres | ||
|
||
| </AlertDescription> | ||
| </Alert> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { useState, useCallback, useMemo } from 'react' | ||
| import type { Language, CodeEditorState } from '@/types/editor' | ||
|
|
||
| interface UseCodeEditorProps { | ||
| initialValue?: string | ||
| initialLanguage?: Language | ||
| maxLength?: number | ||
| } | ||
|
|
||
| export function useCodeEditor({ | ||
| initialValue = '', | ||
| initialLanguage = 'javascript', | ||
| maxLength = 1024, | ||
| }: UseCodeEditorProps = {}) { | ||
| const [state, setState] = useState<CodeEditorState>({ | ||
| value: initialValue, | ||
| language: initialLanguage, | ||
| isValid: initialValue.length <= maxLength, | ||
| characterCount: initialValue.length, | ||
| }) | ||
|
|
||
| const updateValue = useCallback( | ||
| (value: string) => { | ||
| setState(prev => ({ | ||
| ...prev, | ||
| value, | ||
| characterCount: value.length, | ||
| isValid: value.length <= maxLength, | ||
| })) | ||
| }, | ||
| [maxLength], | ||
| ) | ||
|
|
||
| const updateLanguage = useCallback((language: Language) => { | ||
| setState(prev => ({ ...prev, language })) | ||
| }, []) | ||
|
|
||
| const validation = useMemo( | ||
| () => ({ | ||
| isValid: state.isValid, | ||
| isOverLimit: state.characterCount > maxLength, | ||
| remainingChars: maxLength - state.characterCount, | ||
| errorMessage: | ||
| state.characterCount > maxLength | ||
| ? `Código excede o limite em ${state.characterCount - maxLength} caracteres` | ||
|
||
| : null, | ||
| }), | ||
| [state.isValid, state.characterCount, maxLength], | ||
| ) | ||
|
|
||
| return { | ||
| ...state, | ||
| validation, | ||
| updateValue, | ||
| updateLanguage, | ||
| reset: () => | ||
| setState({ | ||
| value: '', | ||
| language: initialLanguage, | ||
| isValid: true, | ||
| characterCount: 0, | ||
| }), | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export type Language = 'javascript' | 'typescript' | 'csharp' | ||
| export type FeedbackProfile = 'flexible' | 'rigorous' | ||
|
|
||
| export interface EditorConfig { | ||
|
||
| language: Language | ||
| theme: 'vs-light' | 'vs-dark' | ||
| fontSize: number | ||
| maxLength: number | ||
| } | ||
|
|
||
| export interface CodeEditorState { | ||
| value: string | ||
| language: Language | ||
| isValid: boolean | ||
| characterCount: number | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| export type { Language, FeedbackProfile } from './common' | ||
|
|
||
| export interface EditorConfig { | ||
| language: Language | ||
| theme: 'vs-light' | 'vs-dark' | ||
| fontSize: number | ||
| maxLength: number | ||
| } | ||
|
|
||
| export interface CodeEditorState { | ||
| value: string | ||
| language: Language | ||
| isValid: boolean | ||
| characterCount: number | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The placeholder text is in Portuguese while other parts of the codebase use English. This should be consistent with the project's internationalization approach as defined in the Style Guide.