|
@@ -1,7 +1,10 @@
|
|
|
-import dynamic from "next/dynamic";
|
|
|
import React, { ComponentType } from "react";
|
|
|
+import dynamic from "next/dynamic";
|
|
|
import { IAceEditorProps } from "react-ace";
|
|
|
import toast from "react-hot-toast";
|
|
|
+import { StorageConfig } from "src/typings/global";
|
|
|
+import { useLocalStorage } from "usehooks-ts";
|
|
|
+import { defaultConfig } from "src/constants/data";
|
|
|
|
|
|
function isJson(value: string) {
|
|
|
value = typeof value !== "string" ? JSON.stringify(value) : value;
|
|
@@ -36,23 +39,34 @@ const JsonEditor: React.FC<{
|
|
|
json: string;
|
|
|
setJson: React.Dispatch<React.SetStateAction<string>>;
|
|
|
}> = ({ json, setJson }) => {
|
|
|
+ const [config] = useLocalStorage<StorageConfig>("config", defaultConfig);
|
|
|
const [value, setValue] = React.useState(
|
|
|
JSON.stringify(JSON.parse(json), null, 2)
|
|
|
);
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
- setValue(JSON.stringify(JSON.parse(json), null, 2));
|
|
|
+ if (config.autoformat) {
|
|
|
+ return setValue(JSON.stringify(JSON.parse(json), null, 2));
|
|
|
+ }
|
|
|
+
|
|
|
+ setValue(json);
|
|
|
}, [json]);
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
const formatTimer = setTimeout(() => {
|
|
|
if (!isJson(value)) return;
|
|
|
- setValue(JSON.stringify(JSON.parse(value), null, 2));
|
|
|
+
|
|
|
+ if (config.autoformat) {
|
|
|
+ setValue(JSON.stringify(JSON.parse(value), null, 2));
|
|
|
+ } else {
|
|
|
+ setValue(value);
|
|
|
+ }
|
|
|
+
|
|
|
setJson(value);
|
|
|
}, 2000);
|
|
|
|
|
|
return () => clearTimeout(formatTimer);
|
|
|
- }, [value]);
|
|
|
+ }, [value, config.autoformat]);
|
|
|
|
|
|
return (
|
|
|
<AceEditor
|