Skip to content

Commit

Permalink
feat: prettify code in code editor (#116)
Browse files Browse the repository at this point in the history
* feat: prettify code in code editor

* refactor: move tryFormatting to separate file

* refactor: format tryFormattingCode function parameters for readability

* refactor: remove unused useCallback import in CodeEditor component

* refactor: remove unused useMonaco import in CodeEditor component

---------

Co-authored-by: JeelRajodiya <[email protected]>
  • Loading branch information
pavanydg and JeelRajodiya authored Dec 21, 2024
1 parent 8f952db commit 2757157
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
27 changes: 18 additions & 9 deletions app/components/CodeEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import styles from "./CodeEditor.module.css";
import ctx from "classnames";
import { GeistMono } from "geist/font/mono";
import Editor, { useMonaco } from "@monaco-editor/react";
import Editor from "@monaco-editor/react";
import { Flex, useColorMode } from "@chakra-ui/react";
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
import MyBtn from "../MyBtn";
import { CodeFile, OutputResult } from "@/lib/types";
import { OutputReducerAction } from "@/lib/reducers";
import { validateCode } from "@/lib/client-functions";
import { tryFormattingCode, validateCode } from "@/lib/client-functions";
import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen";
import { useRouter } from "next/navigation";
import { useUserSolutionStore, useEditorStore } from "@/lib/stores";
Expand Down Expand Up @@ -39,6 +39,7 @@ export default function CodeEditor({
const router = useRouter();
const editorStore = useEditorStore();
const userSolutionStore = useUserSolutionStore();
const editorRef = useRef<any>(null);

useEffect(() => {
if (monaco) {
Expand All @@ -55,12 +56,12 @@ export default function CodeEditor({
}, [monaco, colorMode]);
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
// event.preventDefault();
if (event.key == "Enter" && event.shiftKey) {
sendGAEvent("event", "buttonClicked", {
value: "Validate (through shortcut)",
});
event.preventDefault(); // Prevent default behavior
event.preventDefault();
tryFormattingCode(editorRef, setCodeString);
validateCode(
codeString,
codeFile,
Expand Down Expand Up @@ -112,9 +113,16 @@ export default function CodeEditor({
value={codeString}
height={"100%"}
onChange={(codeString) => setCodeString(codeString ?? "")}
options={{ minimap: { enabled: false }, fontSize: 14 }}
options={{
minimap: { enabled: false },

fontSize: 14,
formatOnPaste: true,
formatOnType: true,
}}
onMount={(editor, monaco) => {
setMonaco(monaco);
editorRef.current = editor;
editorStore.setEditor(editor);
editorStore.setMonaco(monaco);
}}
Expand All @@ -123,15 +131,16 @@ export default function CodeEditor({
<div className={styles.buttonsWrapper}>
<Flex dir="row" gap={"8px"} alignItems={"end"}>
<MyBtn
onClick={async () =>
onClick={async () => {
tryFormattingCode(editorRef, setCodeString);
validateCode(
codeString,
codeFile,
dispatchOutput,
stepIndex,
chapterIndex,
)
}
);
}}
variant={
outputResult.validityStatus === "valid" ? "success" : "default"
}
Expand Down
15 changes: 15 additions & 0 deletions lib/client-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,18 @@ export function hasNestedProperty(obj: any, path: string) {
// If we've made it through all the keys, the property exists
return true;
}
export async function tryFormattingCode(
editorRef: any,
setCodeString: (code: string) => void,
) {
try {
if (!editorRef.current) return;
const currentCode = editorRef.current.getValue();
JSON.parse(currentCode);
await editorRef.current.getAction("editor.action.formatDocument").run();
setCodeString(editorRef.current.getValue());
} catch (e) {
// If invalid JSON, do nothing
return;
}
}

0 comments on commit 2757157

Please sign in to comment.