Skip to content
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

feat: prettify code in code editor #116

Merged
merged 5 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading