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(web): format code on save #1002

Merged
merged 2 commits into from
Apr 4, 2023
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
2 changes: 1 addition & 1 deletion web/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
border-radius: 4px;
background: rgba(0, 0, 0, 0.5);
background: rgba(0, 0, 0, 0.2);
}

html,
Expand Down
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
4 changes: 2 additions & 2 deletions web/src/components/Editor/JSONViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ export default function JSONViewer(props: JSONViewerProps) {
return (
<div
style={{
maxHeight: 100,
overflow: "hidden",
maxHeight: 390,
overflow: "auto",
}}
>
<SyntaxHighlighter
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/Editor/JsonEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function JsonEditor(props: {
alwaysConsumeMouseWheel: false,
},
lineNumbersMinChars: 0,
fontSize: 14,
fontSize: 13,
scrollBeyondLastLine: false,
folding: false,
overviewRulerBorder: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from "react";
import { useColorMode } from "@chakra-ui/react";
import clsx from "clsx";
import { t } from "i18next";
Expand Down