better saving for yaml editor
This commit is contained in:
1 parent
7154f206b4
commit
34a4bd93bd
2 files changed
+52
-21
No files matched your search
@@ -6,7 +6,8 @@ import { useDelayEffect } from "utils/useDelay";
|
|||||||
import CodeMirror from "@uiw/react-codemirror";
|
import CodeMirror from "@uiw/react-codemirror";
|
||||||
import FormControl from "@mui/material/FormControl";
|
import FormControl from "@mui/material/FormControl";
|
||||||
import InputLabel from "@mui/material/InputLabel";
|
import InputLabel from "@mui/material/InputLabel";
|
||||||
import { FormHelperText, InputAdornment } from "@mui/material";
|
import { FormHelperText, IconButton, InputAdornment } from "@mui/material";
|
||||||
|
import { Save } from "@mui/icons-material";
|
||||||
|
|
||||||
export interface Props<T extends {}> {
|
export interface Props<T extends {}> {
|
||||||
label: string;
|
label: string;
|
||||||
@@ -32,42 +33,57 @@ export default function InputYaml<T extends {}>({
|
|||||||
helperText,
|
helperText,
|
||||||
endAdornment,
|
endAdornment,
|
||||||
}: Props<T>) {
|
}: Props<T>) {
|
||||||
const [{ text, error }, setState] = useState<{
|
const [{ text, error, hasChanged }, setState] = useState<{
|
||||||
text: string;
|
text: string;
|
||||||
error: undefined | JSX.Element | string;
|
error: undefined | JSX.Element | string;
|
||||||
|
hasChanged: boolean;
|
||||||
}>({
|
}>({
|
||||||
text: yaml.dump(value),
|
text: yaml.dump(value),
|
||||||
error: incmError,
|
error: incmError,
|
||||||
|
hasChanged: false,
|
||||||
});
|
});
|
||||||
const setText = (t: string) => {
|
const setText = (t: string) => {
|
||||||
try {
|
try {
|
||||||
yaml.load(t);
|
yaml.load(t);
|
||||||
setState({ text: t, error: undefined });
|
console.log("success!", yaml.load(t));
|
||||||
|
setState({ text: t, error: undefined, hasChanged: true });
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
setState({ text: t, error: "! Invalid Yaml !" });
|
setState({ text: t, error: "! Invalid Yaml !", hasChanged: true });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const onSave = () => {
|
||||||
|
if (!error && yaml.dump(value) !== text) {
|
||||||
|
if (text) {
|
||||||
|
onChange(yaml.load(text) as any);
|
||||||
|
} else if (Array.isArray(value)) {
|
||||||
|
onChange([] as any);
|
||||||
|
} else {
|
||||||
|
onChange({} as any);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setState({
|
setState({
|
||||||
text: yaml.dump(value),
|
text: yaml.dump(value),
|
||||||
error: incmError,
|
error: incmError,
|
||||||
|
hasChanged: false,
|
||||||
});
|
});
|
||||||
}, [value, incmError]);
|
}, [value, incmError]);
|
||||||
useDelayEffect(
|
// useDelayEffect(
|
||||||
() => {
|
// () => {
|
||||||
if (!error && yaml.dump(value) !== text) {
|
// if (!error && yaml.dump(value) !== text) {
|
||||||
if (text) {
|
// if (text) {
|
||||||
onChange(yaml.load(text) as any);
|
// onChange(yaml.load(text) as any);
|
||||||
} else if (Array.isArray(value)) {
|
// } else if (Array.isArray(value)) {
|
||||||
onChange([] as any);
|
// onChange([] as any);
|
||||||
} else {
|
// } else {
|
||||||
onChange({} as any);
|
// onChange({} as any);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
},
|
// },
|
||||||
[text],
|
// [text],
|
||||||
1000
|
// 1000
|
||||||
);
|
// );
|
||||||
return (
|
return (
|
||||||
<FormControl
|
<FormControl
|
||||||
className={["input-yaml", className ?? ""].join(" ")}
|
className={["input-yaml", className ?? ""].join(" ")}
|
||||||
@@ -89,7 +105,18 @@ export default function InputYaml<T extends {}>({
|
|||||||
<span className="input-yaml--placeholder">
|
<span className="input-yaml--placeholder">
|
||||||
{!!text ? "" : placeholder}
|
{!!text ? "" : placeholder}
|
||||||
</span>
|
</span>
|
||||||
<InputAdornment position="end">{endAdornment}</InputAdornment>
|
<InputAdornment position="end">
|
||||||
|
<IconButton
|
||||||
|
disabled={!hasChanged}
|
||||||
|
onChange={onSave}
|
||||||
|
title={!!error ? "Invalid YAML" : hasChanged ? "Save" : ""}
|
||||||
|
>
|
||||||
|
<Save
|
||||||
|
color={hasChanged ? (!!error ? "error" : "success") : "disabled"}
|
||||||
|
/>
|
||||||
|
</IconButton>
|
||||||
|
{endAdornment}
|
||||||
|
</InputAdornment>
|
||||||
<FormHelperText variant="filled" error={!!error}>
|
<FormHelperText variant="filled" error={!!error}>
|
||||||
{error ? error : helperText}
|
{error ? error : helperText}
|
||||||
</FormHelperText>
|
</FormHelperText>
|
||||||
|
|||||||
@@ -30,7 +30,11 @@ export const ServiceEditorField: FC<ServiceEditorFieldProps> = ({
|
|||||||
const removeIcon = !onRemove ? (
|
const removeIcon = !onRemove ? (
|
||||||
<></>
|
<></>
|
||||||
) : (
|
) : (
|
||||||
<IconButton className="service-editor--field--remove" onClick={onRemove}>
|
<IconButton
|
||||||
|
className="service-editor--field--remove"
|
||||||
|
onClick={onRemove}
|
||||||
|
title={`Remove ${label}`}
|
||||||
|
>
|
||||||
<RemoveCircle />
|
<RemoveCircle />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user