From 89720cc82802218d31ff38c1ce585eed1856a278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aykut=20Sara=C3=A7?= Date: Wed, 16 Feb 2022 15:30:13 +0300 Subject: [PATCH] add option to disable autoformat --- src/components/Sidebar/index.tsx | 26 ++++++++++++++++++-------- src/constants/data.ts | 1 + src/containers/JsonEditor/index.tsx | 22 ++++++++++++++++++---- src/typings/global.ts | 1 + 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index 6dd4222..91ee4e8 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -3,7 +3,12 @@ import Link from "next/link"; import styled from "styled-components"; import { useLocalStorage } from "usehooks-ts"; import { FaFileImport } from "react-icons/fa"; -import { MdUnfoldMore, MdUnfoldLess } from "react-icons/md"; +import { + MdUnfoldMore, + MdUnfoldLess, + MdAutoFixHigh, + MdOutlineAutoFixOff, +} from "react-icons/md"; import { AiFillHome, AiOutlineClear, @@ -118,7 +123,7 @@ const Sidebar: React.FC<{ if (e.target.files) setJsonFile(e.target.files?.item(0)); }; - const toggle = (setting: "expand" | "controls") => { + const toggle = (setting: "expand" | "controls" | "autoformat") => { setConfig((c) => ({ ...c, [setting]: !c[setting], @@ -147,12 +152,17 @@ const Sidebar: React.FC<{ - - - - - - + + + + + + toggle("autoformat")} + > + {config.autoformat ? : } >; }> = ({ json, setJson }) => { + const [config] = useLocalStorage("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 (