Skip to content

Commit 5dd42bc

Browse files
prettier code format in monaco editor done
1 parent 9b095e5 commit 5dd42bc

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
"@testing-library/user-event": "^12.8.3",
1010
"@types/jest": "^26.0.21",
1111
"@types/node": "^12.20.6",
12+
"@types/prettier": "^2.2.3",
1213
"@types/react": "^17.0.3",
1314
"@types/react-dom": "^17.0.2",
1415
"axios": "^0.21.1",
1516
"esbuild": "^0.9.6",
1617
"esbuild-wasm": "^0.9.6",
1718
"localforage": "^1.9.0",
1819
"monaco-editor": "^0.23.0",
20+
"prettier": "^2.2.1",
1921
"react": "^17.0.1",
2022
"react-dom": "^17.0.1",
2123
"react-scripts": "4.0.3",

src/components/CodeEditor/Editor.tsx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
1-
import MonacoEditor from "@monaco-editor/react";
1+
import MonacoEditor, { Monaco } from "@monaco-editor/react";
2+
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
23
import { EditorPropsInterface } from "../../interfaces/index";
3-
import React from "react";
4+
import React, {useRef} from "react";
5+
import prettier from "prettier";
6+
import parser from "prettier/parser-babel";
47

58
const Editor: React.FC<EditorPropsInterface> = ({ onChange, initialValue }) => {
6-
const onEditorDidMount = (getCurrentValue: any) => {
7-
console.log(getCurrentValue);
9+
10+
11+
const monacoEditorRef = useRef<monaco.editor.IStandaloneCodeEditor>();
12+
13+
14+
/**
15+
* On change in model, get the value of it's content and
16+
* pass it as an argument to onChange to set the input state
17+
*/
18+
const onEditorDidMount = (
19+
editor: monaco.editor.IStandaloneCodeEditor,
20+
monacoInstance: Monaco
21+
) => {
22+
// console.log(editor.getValue());
23+
monacoEditorRef.current = editor;
24+
editor.onDidChangeModelContent(() => {
25+
onChange(editor.getValue());
26+
});
827
};
928

29+
/**
30+
* Get unformatted code, format it and set it to model
31+
*/
32+
const onFormatClick = () => {
33+
const rawCode = monacoEditorRef!.current!.getModel()!.getValue();
34+
const formattedCode = prettier.format(rawCode, {
35+
parser: "babel",
36+
plugins:[parser],
37+
semi: true,
38+
singleQuote: true
39+
})
40+
41+
monacoEditorRef.current?.setValue(formattedCode);
42+
}
43+
44+
1045
return (
11-
<MonacoEditor
46+
<div>
47+
<button onClick={onFormatClick}>Format</button>
48+
<MonacoEditor
1249
onMount={onEditorDidMount}
1350
value={initialValue}
1451
height="30vh"
@@ -27,6 +64,8 @@ const Editor: React.FC<EditorPropsInterface> = ({ onChange, initialValue }) => {
2764
automaticLayout: true,
2865
}}
2966
/>
67+
</div>
68+
3069
);
3170
};
3271

0 commit comments

Comments
 (0)