33import styles from "./CodeEditor.module.css" ;
44import ctx from "classnames" ;
55import { GeistMono } from "geist/font/mono" ;
6- import Editor , { useMonaco } from "@monaco-editor/react" ;
6+ import Editor from "@monaco-editor/react" ;
77import { Flex , useColorMode } from "@chakra-ui/react" ;
8- import { useEffect , useState } from "react" ;
8+ import { useEffect , useState , useRef } from "react" ;
99import MyBtn from "../MyBtn" ;
1010import { CodeFile , OutputResult } from "@/lib/types" ;
1111import { OutputReducerAction } from "@/lib/reducers" ;
12- import { validateCode } from "@/lib/client-functions" ;
12+ import { tryFormattingCode , validateCode } from "@/lib/client-functions" ;
1313import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen" ;
1414import { useRouter } from "next/navigation" ;
1515import { useUserSolutionStore , useEditorStore } from "@/lib/stores" ;
@@ -39,6 +39,7 @@ export default function CodeEditor({
3939 const router = useRouter ( ) ;
4040 const editorStore = useEditorStore ( ) ;
4141 const userSolutionStore = useUserSolutionStore ( ) ;
42+ const editorRef = useRef < any > ( null ) ;
4243
4344 useEffect ( ( ) => {
4445 if ( monaco ) {
@@ -55,12 +56,12 @@ export default function CodeEditor({
5556 } , [ monaco , colorMode ] ) ;
5657 useEffect ( ( ) => {
5758 const handleKeyDown = ( event : KeyboardEvent ) => {
58- // event.preventDefault();
5959 if ( event . key == "Enter" && event . shiftKey ) {
6060 sendGAEvent ( "event" , "buttonClicked" , {
6161 value : "Validate (through shortcut)" ,
6262 } ) ;
63- event . preventDefault ( ) ; // Prevent default behavior
63+ event . preventDefault ( ) ;
64+ tryFormattingCode ( editorRef , setCodeString ) ;
6465 validateCode (
6566 codeString ,
6667 codeFile ,
@@ -112,9 +113,16 @@ export default function CodeEditor({
112113 value = { codeString }
113114 height = { "100%" }
114115 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+ } }
116123 onMount = { ( editor , monaco ) => {
117124 setMonaco ( monaco ) ;
125+ editorRef . current = editor ;
118126 editorStore . setEditor ( editor ) ;
119127 editorStore . setMonaco ( monaco ) ;
120128 } }
@@ -123,15 +131,16 @@ export default function CodeEditor({
123131 < div className = { styles . buttonsWrapper } >
124132 < Flex dir = "row" gap = { "8px" } alignItems = { "end" } >
125133 < MyBtn
126- onClick = { async ( ) =>
134+ onClick = { async ( ) => {
135+ tryFormattingCode ( editorRef , setCodeString ) ;
127136 validateCode (
128137 codeString ,
129138 codeFile ,
130139 dispatchOutput ,
131140 stepIndex ,
132141 chapterIndex ,
133- )
134- }
142+ ) ;
143+ } }
135144 variant = {
136145 outputResult . validityStatus === "valid" ? "success" : "default"
137146 }
0 commit comments