3
3
import styles from "./CodeEditor.module.css" ;
4
4
import ctx from "classnames" ;
5
5
import { GeistMono } from "geist/font/mono" ;
6
- import Editor , { useMonaco } from "@monaco-editor/react" ;
6
+ import Editor from "@monaco-editor/react" ;
7
7
import { Flex , useColorMode } from "@chakra-ui/react" ;
8
- import { useEffect , useState } from "react" ;
8
+ import { useEffect , useState , useRef } from "react" ;
9
9
import MyBtn from "../MyBtn" ;
10
10
import { CodeFile , OutputResult } from "@/lib/types" ;
11
11
import { OutputReducerAction } from "@/lib/reducers" ;
12
- import { validateCode } from "@/lib/client-functions" ;
12
+ import { tryFormattingCode , validateCode } from "@/lib/client-functions" ;
13
13
import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen" ;
14
14
import { useRouter } from "next/navigation" ;
15
15
import { useUserSolutionStore , useEditorStore } from "@/lib/stores" ;
@@ -39,6 +39,7 @@ export default function CodeEditor({
39
39
const router = useRouter ( ) ;
40
40
const editorStore = useEditorStore ( ) ;
41
41
const userSolutionStore = useUserSolutionStore ( ) ;
42
+ const editorRef = useRef < any > ( null ) ;
42
43
43
44
useEffect ( ( ) => {
44
45
if ( monaco ) {
@@ -55,12 +56,12 @@ export default function CodeEditor({
55
56
} , [ monaco , colorMode ] ) ;
56
57
useEffect ( ( ) => {
57
58
const handleKeyDown = ( event : KeyboardEvent ) => {
58
- // event.preventDefault();
59
59
if ( event . key == "Enter" && event . shiftKey ) {
60
60
sendGAEvent ( "event" , "buttonClicked" , {
61
61
value : "Validate (through shortcut)" ,
62
62
} ) ;
63
- event . preventDefault ( ) ; // Prevent default behavior
63
+ event . preventDefault ( ) ;
64
+ tryFormattingCode ( editorRef , setCodeString ) ;
64
65
validateCode (
65
66
codeString ,
66
67
codeFile ,
@@ -112,9 +113,16 @@ export default function CodeEditor({
112
113
value = { codeString }
113
114
height = { "100%" }
114
115
onChange = { ( codeString ) => setCodeString ( codeString ?? "" ) }
115
- options = { { minimap : { enabled : false } , fontSize : 14 } }
116
+ options = { {
117
+ minimap : { enabled : false } ,
118
+
119
+ fontSize : 14 ,
120
+ formatOnPaste : true ,
121
+ formatOnType : true ,
122
+ } }
116
123
onMount = { ( editor , monaco ) => {
117
124
setMonaco ( monaco ) ;
125
+ editorRef . current = editor ;
118
126
editorStore . setEditor ( editor ) ;
119
127
editorStore . setMonaco ( monaco ) ;
120
128
} }
@@ -123,15 +131,16 @@ export default function CodeEditor({
123
131
< div className = { styles . buttonsWrapper } >
124
132
< Flex dir = "row" gap = { "8px" } alignItems = { "end" } >
125
133
< MyBtn
126
- onClick = { async ( ) =>
134
+ onClick = { async ( ) => {
135
+ tryFormattingCode ( editorRef , setCodeString ) ;
127
136
validateCode (
128
137
codeString ,
129
138
codeFile ,
130
139
dispatchOutput ,
131
140
stepIndex ,
132
141
chapterIndex ,
133
- )
134
- }
142
+ ) ;
143
+ } }
135
144
variant = {
136
145
outputResult . validityStatus === "valid" ? "success" : "default"
137
146
}
0 commit comments