Skip to content

Commit

Permalink
feat(web): format code on save
Browse files Browse the repository at this point in the history
  • Loading branch information
LeezQ committed Apr 3, 2023
1 parent 1c4040a commit d1a8f8a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions web/src/components/Editor/FunctionEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { useEffect, useRef } from "react";
import { debounce } from "lodash";
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";

import { Pages } from "@/constants";

import "./userWorker";

import { AutoImportTypings } from "./typesResolve";

import useHotKey, { DEFAULT_SHORTCUTS } from "@/hooks/useHotKey";
import useGlobalStore from "@/pages/globalStore";

const autoImportTypings = new AutoImportTypings();
const parseImports = debounce(autoImportTypings.parse, 1500).bind(autoImportTypings);

Expand Down Expand Up @@ -108,6 +113,19 @@ function FunctionEditor(props: {
const subscriptionRef = useRef<monaco.IDisposable | undefined>(undefined);
const monacoEl = useRef(null);

const globalStore = useGlobalStore((state) => state);

useHotKey(
DEFAULT_SHORTCUTS.send_request,
() => {
// format
editorRef.current?.trigger("keyboard", "editor.action.formatDocument", {});
},
{
enabled: globalStore.currentPageId === Pages.function,
},
);

useEffect(() => {
if (monacoEl && !editorRef.current) {
editorRef.current = monaco.editor.create(monacoEl.current!, {
Expand All @@ -120,6 +138,7 @@ function FunctionEditor(props: {
scrollbar: {
verticalScrollbarSize: 6,
},
formatOnPaste: true,
overviewRulerLanes: 0,
lineNumbersMinChars: 4,
fontSize: 14,
Expand Down

0 comments on commit d1a8f8a

Please sign in to comment.