mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-01-20 05:12:54 +08:00
localStorage fixes
This commit is contained in:
parent
e53d178ce7
commit
765a0f0486
@ -35,7 +35,7 @@ const StyledElement = styled.div<{ disabled?: boolean }>`
|
|||||||
color: ${({ theme, disabled }) =>
|
color: ${({ theme, disabled }) =>
|
||||||
disabled ? theme.SILVER_DARK : theme.SILVER};
|
disabled ? theme.SILVER_DARK : theme.SILVER};
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
pointer-events: ${({ disabled }) => disabled && 'none'};
|
pointer-events: ${({ disabled }) => disabled && "none"};
|
||||||
|
|
||||||
a {
|
a {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
@ -90,9 +90,10 @@ const StyledImportFile = styled.label`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const Sidebar = () => {
|
export const Sidebar: React.FC<{
|
||||||
|
setJson: React.Dispatch<React.SetStateAction<string>>;
|
||||||
|
}> = ({ setJson }) => {
|
||||||
const [jsonFile, setJsonFile] = React.useState<File | null>(null);
|
const [jsonFile, setJsonFile] = React.useState<File | null>(null);
|
||||||
const [json, setJson] = useLocalStorage("json", JSON.stringify(defaultValue));
|
|
||||||
const [config, setConfig] = useLocalStorage<StorageConfig>("config", {
|
const [config, setConfig] = useLocalStorage<StorageConfig>("config", {
|
||||||
layout: "LEFT",
|
layout: "LEFT",
|
||||||
minimap: true,
|
minimap: true,
|
||||||
|
@ -53,13 +53,22 @@ export const defaultValue = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export const JsonEditor: React.FC = () => {
|
export const JsonEditor: React.FC<{
|
||||||
const [clear, setClear] = React.useState(0);
|
json: string;
|
||||||
const [json, setJson] = useLocalStorage("json", JSON.stringify(defaultValue));
|
setJson: React.Dispatch<React.SetStateAction<string>>;
|
||||||
const initialJson = React.useMemo(() => JSON.parse(json), [clear]);
|
}> = ({ json, setJson }) => {
|
||||||
|
const [initialJson, setInitialJson] = React.useState(
|
||||||
|
React.useMemo(
|
||||||
|
() =>
|
||||||
|
typeof localStorage !== "undefined" && localStorage.getItem("json")
|
||||||
|
? localStorage.getItem("json")
|
||||||
|
: json,
|
||||||
|
[]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (json === "[]") setClear((c) => c + 1);
|
if (json === "[]") setInitialJson(json);
|
||||||
}, [json]);
|
}, [json]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
@ -78,13 +87,17 @@ export const JsonEditor: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
return (
|
return (
|
||||||
<StyledJSONInput
|
<StyledJSONInput
|
||||||
placeholder={initialJson}
|
placeholder={JSON.parse(initialJson as string)}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
locale={locale}
|
locale={locale}
|
||||||
height="100%"
|
height="100%"
|
||||||
width="auto"
|
width="auto"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
@ -82,7 +82,10 @@ const CustomNode = ({ nodeProps }) => {
|
|||||||
<StyledTextWrapper>
|
<StyledTextWrapper>
|
||||||
<StyledText width={width} height={height}>
|
<StyledText width={width} height={height}>
|
||||||
{entries.map((val) => (
|
{entries.map((val) => (
|
||||||
<div style={{ height: "fit-content" }}>
|
<div
|
||||||
|
key={nodeProps.id}
|
||||||
|
style={{ height: "fit-content" }}
|
||||||
|
>
|
||||||
<StyledKey>{val[0]}: </StyledKey>
|
<StyledKey>{val[0]}: </StyledKey>
|
||||||
{val[1]}
|
{val[1]}
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { ComponentType } from "react";
|
import React from "react";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import {
|
import {
|
||||||
TransformWrapper,
|
TransformWrapper,
|
||||||
@ -40,11 +40,12 @@ const StyledControls = styled.div`
|
|||||||
opacity: 0.8;
|
opacity: 0.8;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const LiveEditor: React.FC = () => {
|
export const LiveEditor: React.FC<{
|
||||||
|
json: string;
|
||||||
|
setJson: React.Dispatch<React.SetStateAction<string>>;
|
||||||
|
}> = ({ json }) => {
|
||||||
const canvasRef = React.useRef<CanvasRef | null>(null);
|
const canvasRef = React.useRef<CanvasRef | null>(null);
|
||||||
const wrapperRef = React.useRef<ReactZoomPanPinchRef | null>(null);
|
const wrapperRef = React.useRef<ReactZoomPanPinchRef | null>(null);
|
||||||
|
|
||||||
const [json] = useLocalStorage("json", JSON.stringify(defaultValue));
|
|
||||||
const [config] = useLocalStorage<StorageConfig>("config", {
|
const [config] = useLocalStorage<StorageConfig>("config", {
|
||||||
layout: "LEFT",
|
layout: "LEFT",
|
||||||
minimap: true,
|
minimap: true,
|
||||||
@ -52,7 +53,7 @@ export const LiveEditor: React.FC = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
wrapperRef.current?.resetTransform();
|
if (wrapperRef.current) wrapperRef.current?.resetTransform();
|
||||||
}, [json, wrapperRef]);
|
}, [json, wrapperRef]);
|
||||||
|
|
||||||
const { nodes, edges } = getEdgeNodes(json);
|
const { nodes, edges } = getEdgeNodes(json);
|
||||||
@ -127,7 +128,7 @@ export const LiveEditor: React.FC = () => {
|
|||||||
<Button onClick={() => wrapperRef.current?.resetTransform()}>
|
<Button onClick={() => wrapperRef.current?.resetTransform()}>
|
||||||
<AiOutlineFullscreen size={20} />
|
<AiOutlineFullscreen size={20} />
|
||||||
</Button>
|
</Button>
|
||||||
<Button>
|
<Button onClick={() => localStorage.setItem("json", json)}>
|
||||||
<AiFillSave size={20} />
|
<AiFillSave size={20} />
|
||||||
</Button>
|
</Button>
|
||||||
</StyledControls>
|
</StyledControls>
|
||||||
|
@ -6,7 +6,7 @@ import SplitPane from "react-split-pane";
|
|||||||
|
|
||||||
import { Button } from "src/components/Button";
|
import { Button } from "src/components/Button";
|
||||||
import { Sidebar } from "src/components/Sidebar";
|
import { Sidebar } from "src/components/Sidebar";
|
||||||
import { JsonEditor } from "src/containers/JsonEditor";
|
import { defaultValue, JsonEditor } from "src/containers/JsonEditor";
|
||||||
import { LiveEditor } from "src/containers/LiveEditor";
|
import { LiveEditor } from "src/containers/LiveEditor";
|
||||||
|
|
||||||
const StyledPageWrapper = styled.div`
|
const StyledPageWrapper = styled.div`
|
||||||
@ -117,14 +117,21 @@ const StyledEditor = styled(SplitPane)`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const Editor: React.FC = () => {
|
const Editor: React.FC = () => {
|
||||||
|
const [json, setJson] = React.useState<string>(JSON.stringify(defaultValue));
|
||||||
const route = useRouter();
|
const route = useRouter();
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const jsonStored = localStorage.getItem("json");
|
||||||
|
|
||||||
|
if (jsonStored) setJson(jsonStored);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledPageWrapper>
|
<StyledPageWrapper>
|
||||||
<Head>
|
<Head>
|
||||||
<title>Editor | JSON Visio</title>
|
<title>Editor | JSON Visio</title>
|
||||||
</Head>
|
</Head>
|
||||||
<Sidebar />
|
<Sidebar setJson={setJson} />
|
||||||
<StyledEditorWrapper>
|
<StyledEditorWrapper>
|
||||||
<StyledTools></StyledTools>
|
<StyledTools></StyledTools>
|
||||||
<StyledEditor
|
<StyledEditor
|
||||||
@ -133,8 +140,8 @@ const Editor: React.FC = () => {
|
|||||||
defaultSize={450}
|
defaultSize={450}
|
||||||
split="vertical"
|
split="vertical"
|
||||||
>
|
>
|
||||||
<JsonEditor />
|
<JsonEditor json={json} setJson={setJson} />
|
||||||
<LiveEditor />
|
<LiveEditor json={json} setJson={setJson} />
|
||||||
</StyledEditor>
|
</StyledEditor>
|
||||||
</StyledEditorWrapper>
|
</StyledEditorWrapper>
|
||||||
<StyledIncompatible>
|
<StyledIncompatible>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user