Skip to content

Commit

Permalink
feat(monaco): add onValidation before submit, and close #1491
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Sep 7, 2024
1 parent c8b7abc commit 85d80d7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
27 changes: 22 additions & 5 deletions frontend/nyanpasu/src/components/profiles/profile-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { version } from "~/package.json";
import { useAsyncEffect } from "ahooks";
import { type editor } from "monaco-editor";
import {
createContext,
lazy,
Expand All @@ -18,6 +19,7 @@ import {
} from "react-hook-form-mui";
import { useTranslation } from "react-i18next";
import { useLatest } from "react-use";
import { message } from "@/utils/notification";
import { Divider, InputAdornment } from "@mui/material";
import { Profile, useClash } from "@nyanpasu/interface";
import { BaseDialog } from "@nyanpasu/ui";
Expand Down Expand Up @@ -86,11 +88,14 @@ export const ProfileDialog = ({
setIsEdit(!!profile);
}, [profile]);

const commonProps = {
autoComplete: "off",
autoCorrect: "off",
fullWidth: true,
};
const commonProps = useMemo(
() => ({
autoComplete: "off",
autoCorrect: "off",
fullWidth: true,
}),
[],
);

const handleProfileSelected = (content: string) => {
localProfile.current = content;
Expand All @@ -105,7 +110,18 @@ export const ProfileDialog = ({

const latestEditor = useLatest(editor);

const editorMarks = useRef<editor.IMarker[]>([]);
const editorHasError = () =>
editorMarks.current.length > 0 &&
editorMarks.current.some((m) => m.severity === 8);

const onSubmit = handleSubmit(async (form) => {
if (editorHasError()) {
message("Please fix the error before saving", {
type: "error",
});
return;
}
const toCreate = async () => {
if (isRemote) {
await createProfile(form);
Expand Down Expand Up @@ -310,6 +326,7 @@ export const ProfileDialog = ({
onChange={(value) =>
setEditor((editor) => ({ ...editor, value }))
}
onValidate={(marks) => (editorMarks.current = marks)}
language={editor.language}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useAtomValue } from "jotai";
import { type JSONSchema7 } from "json-schema";
import nyanpasuMergeSchema from "meta-json-schema/schemas/clash-nyanpasu-merge-json-schema.json";
import clashMetaSchema from "meta-json-schema/schemas/meta-json-schema.json";
import { type editor } from "monaco-editor";
import { configureMonacoYaml } from "monaco-yaml";
import { nanoid } from "nanoid";
import { useCallback, useMemo } from "react";
Expand All @@ -19,6 +20,7 @@ export interface ProfileMonacoViewProps {
className?: string;
readonly?: boolean;
schemaType?: "clash" | "merge";
onValidate?: (markers: editor.IMarker[]) => void;
}

export interface ProfileMonacoViewRef {
Expand Down Expand Up @@ -65,6 +67,7 @@ export default function ProfileMonacoViewer({
readonly = false,
schemaType,
className,
onValidate,
...others
}: ProfileMonacoViewProps) {
const mode = useAtomValue(themeMode);
Expand Down Expand Up @@ -92,6 +95,7 @@ export default function ProfileMonacoViewer({
theme={mode === "light" ? "vs" : "vs-dark"}
beforeMount={beforeEditorMount}
onChange={onChange}
onValidate={onValidate}
options={{
readOnly: readonly,
mouseWheelZoom: true,
Expand Down
19 changes: 18 additions & 1 deletion frontend/nyanpasu/src/components/profiles/script-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useAsyncEffect, useReactive } from "ahooks";
import { lazy, Suspense, useEffect, useState } from "react";
import { type editor } from "monaco-editor";
import { lazy, Suspense, useEffect, useRef, useState } from "react";
import { SelectElement, TextFieldElement, useForm } from "react-hook-form-mui";
import { useTranslation } from "react-i18next";
import { message } from "@/utils/notification";
import { Divider } from "@mui/material";
import { Profile, useClash } from "@nyanpasu/interface";
import { BaseDialog, BaseDialogProps } from "@nyanpasu/ui";
Expand Down Expand Up @@ -93,7 +95,19 @@ export const ScriptDialog = ({
rawType: "merge",
});

const editorMarks = useRef<editor.IMarker[]>([]);
const editorHasError = () =>
editorMarks.current.length > 0 &&
editorMarks.current.some((m) => m.severity === 8);

const onSubmit = form.handleSubmit(async (data) => {
if (editorHasError()) {
message("Please fix the error before submitting", {
type: "error",
});
return;
}

convertTypeMapping(data);

const editorValue = editor.value;
Expand Down Expand Up @@ -224,6 +238,9 @@ export const ScriptDialog = ({
editor.value = value;
}}
language={editor.language}
onValidate={(marks) => {
editorMarks.current = marks;
}}
schemaType={
editor.rawType === Profile.Type.Merge ? "merge" : undefined
}
Expand Down

0 comments on commit 85d80d7

Please sign in to comment.