Skip to content

Commit 9f703f1

Browse files
committed
2 parents 6e6fbe5 + f8dbd5e commit 9f703f1

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

app/components/CodeEditor/CodeEditor.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import styles from "./CodeEditor.module.css";
44
import ctx from "classnames";
55
import { GeistMono } from "geist/font/mono";
6-
import Editor, { useMonaco } from "@monaco-editor/react";
6+
import Editor from "@monaco-editor/react";
77
import { Flex, useColorMode } from "@chakra-ui/react";
8-
import { useEffect, useState } from "react";
8+
import { useEffect, useState, useRef } from "react";
99
import MyBtn from "../MyBtn";
1010
import { CodeFile, OutputResult } from "@/lib/types";
1111
import { OutputReducerAction } from "@/lib/reducers";
12-
import { validateCode } from "@/lib/client-functions";
12+
import { tryFormattingCode, validateCode } from "@/lib/client-functions";
1313
import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen";
1414
import { useRouter } from "next/navigation";
1515
import { 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
}

app/components/CommunityLinks/CommunityLinks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function CompanyLogos() {
99
return [
1010
{ title: "Github", link: "https://github.com/json-schema-org" },
1111
{ title: "Slack", link: "https://json-schema.org/slack" },
12-
{ title: "Twitter", link: "https://x.com/jsonschema" },
12+
{ title: "X", link: "https://x.com/jsonschema" },
1313
{
1414
title: "Youtube",
1515
link: "https://www.youtube.com/@JSONSchemaOrgOfficial",

lib/client-functions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,18 @@ export function hasNestedProperty(obj: any, path: string) {
168168
// If we've made it through all the keys, the property exists
169169
return true;
170170
}
171+
export async function tryFormattingCode(
172+
editorRef: any,
173+
setCodeString: (code: string) => void,
174+
) {
175+
try {
176+
if (!editorRef.current) return;
177+
const currentCode = editorRef.current.getValue();
178+
JSON.parse(currentCode);
179+
await editorRef.current.getAction("editor.action.formatDocument").run();
180+
setCodeString(editorRef.current.getValue());
181+
} catch (e) {
182+
// If invalid JSON, do nothing
183+
return;
184+
}
185+
}

0 commit comments

Comments
 (0)