Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Graph As Image #32

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"react-icons": "^4.3.1",
"react-zoom-pan-pinch": "^2.1.3",
"reaflow": "^5.0.4",
"save-html-as-image": "^1.7.1",
"styled-components": "^5.3.5"
},
"devDependencies": {
Expand Down
105 changes: 51 additions & 54 deletions src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import React from "react";
import toast from "react-hot-toast";
import Link from "next/link";
import styled from "styled-components";
import { FaFileImport } from "react-icons/fa";
import {
MdUnfoldMore,
MdUnfoldLess,
MdAutoFixHigh,
MdOutlineAutoFixOff,
} from "react-icons/md";
import { CanvasDirection } from "reaflow";
import { TiFlowMerge } from "react-icons/ti";
import { BsList } from "react-icons/bs";
import { MdUploadFile } from "react-icons/md";
import { RiPatreonFill } from "react-icons/ri";
import { CgArrowsMergeAltH, CgArrowsShrinkH } from "react-icons/cg";
import {
AiFillHome,
AiFillDelete,
AiOutlineDelete,
AiFillGithub,
AiOutlineTwitter,
AiOutlineSave,
AiOutlineFileAdd,
} from "react-icons/ai";
import {
CgArrowLongDownE,
CgArrowLongLeftE,
CgArrowLongRightE,
CgArrowLongUpE,
} from "react-icons/cg";

import { CanvasDirection } from "reaflow";
import toast from "react-hot-toast";
import { Tooltip } from "../Tooltip";
import { Tooltip } from "src/components/Tooltip";
import { ConfigActionType } from "src/reducer/reducer";
import { useConfig } from "src/hocs/config";
import { useRouter } from "next/router";
Expand All @@ -33,7 +26,7 @@ const StyledSidebar = styled.div`
justify-content: space-between;
flex-direction: column;
align-items: center;
width: 42px;
width: 36px;
background: ${({ theme }) => theme.BACKGROUND_TERTIARY};
padding: 8px;
border-right: 1px solid ${({ theme }) => theme.BACKGROUND_MODIFIER_ACCENT};
Expand Down Expand Up @@ -64,6 +57,10 @@ const StyledText = styled.span<{ secondary?: boolean }>`
secondary ? theme.INTERACTIVE_NORMAL : theme.ORANGE};
`;

const StyledFlowIcon = styled(TiFlowMerge)<{ rotate: number }>`
transform: rotate(${({ rotate }) => `${rotate}deg`});
`;

const StyledTopWrapper = styled.nav`
display: flex;
justify-content: space-between;
Expand Down Expand Up @@ -101,15 +98,15 @@ const StyledImportFile = styled.label`
}
`;

function getLayoutIcon(layout: CanvasDirection) {
if (layout === "LEFT") return <CgArrowLongLeftE />;
if (layout === "UP") return <CgArrowLongUpE />;
if (layout === "RIGHT") return <CgArrowLongRightE />;
return <CgArrowLongDownE />;
function rotateLayout(layout: CanvasDirection) {
if (layout === "LEFT") return 90;
if (layout === "UP") return 180;
if (layout === "RIGHT") return 270;
return 360;
}

export const Sidebar: React.FC = () => {
const { settings, dispatch } = useConfig();
const { json, settings, dispatch } = useConfig();
const router = useRouter();
const [jsonFile, setJsonFile] = React.useState<File | null>(null);

Expand All @@ -123,11 +120,9 @@ export const Sidebar: React.FC = () => {
toast.success(`Cleared JSON and removed from memory.`);
};

const toggleAutoFormat = () => {
dispatch({ type: ConfigActionType.TOGGLE_AUTOFORMAT });
toast(
`Auto format has been ${settings.autoformat ? "disabled." : "enabled."}`
);
const handleSave = () => {
localStorage.setItem("json", json);
toast.success("Saved JSON successfully!");
};

const toggleExpandCollapse = () => {
Expand Down Expand Up @@ -160,47 +155,42 @@ export const Sidebar: React.FC = () => {
</StyledLogo>
</StyledElement>
</Link>
<Tooltip title="Home">
<StyledElement onClick={() => router.push("/")}>
<AiFillHome />
</StyledElement>
</Tooltip>
<Tooltip title="Auto Format">
<StyledElement onClick={toggleAutoFormat}>
{settings.autoformat ? <MdAutoFixHigh /> : <MdOutlineAutoFixOff />}
<Tooltip title="Import File">
<StyledElement>
<StyledImportFile>
<input
key={jsonFile?.name}
onChange={handleFileChange}
type="file"
accept="application/JSON"
/>
<AiOutlineFileAdd />
</StyledImportFile>
</StyledElement>
</Tooltip>
<Tooltip title="Change Layout">
<Tooltip title="Rotate Layout">
<StyledElement
onClick={() => dispatch({ type: ConfigActionType.TOGGLE_LAYOUT })}
>
{getLayoutIcon(settings.layout)}
<StyledFlowIcon rotate={rotateLayout(settings.layout)} />
</StyledElement>
</Tooltip>
<Tooltip title="Toggle Compact Nodes">
<Tooltip title={settings.expand ? "Shrink Nodes" : "Expand Nodes"}>
<StyledElement
title="Toggle Expand/Collapse"
onClick={toggleExpandCollapse}
>
{settings.expand ? <MdUnfoldMore /> : <MdUnfoldLess />}
{settings.expand ? <CgArrowsMergeAltH /> : <CgArrowsShrinkH />}
</StyledElement>
</Tooltip>
<Tooltip title="Clear JSON">
<StyledElement onClick={handleClear}>
<AiFillDelete />
<AiOutlineDelete />
</StyledElement>
</Tooltip>
<Tooltip title="Import File">
<StyledElement>
<StyledImportFile>
<input
key={jsonFile?.name}
onChange={handleFileChange}
type="file"
accept="application/JSON"
/>
<FaFileImport />
</StyledImportFile>
<Tooltip title="Save JSON">
<StyledElement onClick={handleSave}>
<AiOutlineSave />
</StyledElement>
</Tooltip>
</StyledTopWrapper>
Expand All @@ -219,6 +209,13 @@ export const Sidebar: React.FC = () => {
</a>
</Link>
</StyledElement>
<StyledElement>
<Link href="https://www.patreon.com/aykutsarac">
<a aria-label="Patreon" rel="me" target="_blank">
<RiPatreonFill />
</a>
</Link>
</StyledElement>
</StyledBottomWrapper>
</StyledSidebar>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const StyledTooltip = styled.div<{ visible: boolean }>`
position: absolute;
top: 0;
right: 0;
transform: translate(calc(100% + 15px), 25%);
transform: translate(calc(100% + 15px), 20%);
z-index: 5;
background: ${({ theme }) => theme.BACKGROUND_PRIMARY};
color: ${({ theme }) => theme.TEXT_NORMAL};
Expand Down
1 change: 0 additions & 1 deletion src/constants/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const defaultJson = {
export const defaultConfig: StorageConfig = {
layout: "RIGHT",
expand: true,
autoformat: true,
hideEditor: false,
zoomPanPinch: null,
lightmode: false
Expand Down
19 changes: 11 additions & 8 deletions src/containers/Editor/Tools.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from "react";
import toast from "react-hot-toast";
import { saveAsPng } from "save-html-as-image";
import {
AiOutlineFullscreen,
AiOutlineSave,
AiOutlineMinus,
AiOutlinePlus,
} from "react-icons/ai";
import { FiDownload } from "react-icons/fi";
import { HiOutlineSun, HiOutlineMoon } from "react-icons/hi";
import { MdCenterFocusWeak } from "react-icons/md";
import { Input } from "src/components/Input";
Expand Down Expand Up @@ -42,10 +42,6 @@ const StyledToolElement = styled.button`

export const Tools: React.FC = () => {
const { json, settings, dispatch } = useConfig();
const handleSave = () => {
localStorage.setItem("json", json);
toast.success("Saved JSON successfully!");
};

const zoomIn = () => dispatch({ type: ConfigActionType.ZOOM_IN });

Expand All @@ -57,6 +53,13 @@ export const Tools: React.FC = () => {

const toggleTheme = () => dispatch({ type: ConfigActionType.TOGGLE_THEME });

const exportAsImage = () => {
saveAsPng(document.querySelector("svg[id*='ref']"), {
filename: "jsonvisio.com",
printDate: true,
});
};

return (
<StyledTools>
<StyledToolElement aria-label="fullscreen" onClick={toggleEditor}>
Expand All @@ -66,8 +69,8 @@ export const Tools: React.FC = () => {
{settings.lightmode ? <HiOutlineMoon /> : <HiOutlineSun />}
</StyledToolElement>
<Input />
<StyledToolElement aria-label="save" onClick={handleSave}>
<AiOutlineSave />
<StyledToolElement aria-label="save" onClick={exportAsImage}>
<FiDownload />
</StyledToolElement>
<StyledToolElement aria-label="center canvas" onClick={centerView}>
<MdCenterFocusWeak />
Expand Down
45 changes: 17 additions & 28 deletions src/containers/JsonEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import Editor from "@monaco-editor/react";
import parseJson from "parse-json";
import styled from "styled-components";
import { ErrorContainer } from "../../components/ErrorContainer/ErrorContainer";
import { ErrorContainer } from "src/components/ErrorContainer/ErrorContainer";
import { ConfigActionType } from "src/reducer/reducer";
import { useConfig } from "src/hocs/config";
import { Loading } from "src/components/Loading";
Expand All @@ -16,6 +16,7 @@ const StyledEditorWrapper = styled.div`
`;

const editorOptions = {
formatOnPaste: true,
minimap: {
enabled: false,
},
Expand All @@ -35,36 +36,24 @@ export const JsonEditor: React.FC = () => {
);

React.useEffect(() => {
if (settings.autoformat) {
return setValue(JSON.stringify(JSON.parse(json), null, 2));
}

setValue(json);
}, [settings.autoformat, json]);
setValue(JSON.stringify(JSON.parse(json), null, 2));
}, [json]);

React.useEffect(() => {
const formatTimer = setTimeout(
() => {
try {
if (value) {
const parsedJson = parseJson(value);

if (settings.autoformat) {
setValue(JSON.stringify(parsedJson, null, 2));
} else {
setValue(value);
}

dispatch({ type: ConfigActionType.SET_JSON, payload: value });
}

const formatTimer = setTimeout(() => {
try {
if (!value) {
setError((err) => ({ ...err, message: "" }));
} catch (jsonError: any) {
setError((err) => ({ ...err, message: jsonError.message }));
return dispatch({ type: ConfigActionType.SET_JSON, payload: "[]" });
}
},
settings.autoformat ? 1200 : 1800
);

parseJson(value);
dispatch({ type: ConfigActionType.SET_JSON, payload: value });
setError((err) => ({ ...err, message: "" }));
} catch (jsonError: any) {
setError((err) => ({ ...err, message: jsonError.message }));
}
}, 1500);

return () => clearTimeout(formatTimer);
}, [value, dispatch]);
Expand All @@ -76,8 +65,8 @@ export const JsonEditor: React.FC = () => {
height="100%"
defaultLanguage="json"
value={value}
options={editorOptions}
theme={editorTheme}
options={editorOptions}
loading={<Loading message="Loading Editor..." />}
onChange={(value) => setValue(value as string)}
/>
Expand Down
10 changes: 0 additions & 10 deletions src/reducer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export enum ConfigActionType {
SET_CONFIG,
TOGGLE_LAYOUT,
TOGGLE_EXPAND,
TOGGLE_AUTOFORMAT,
TOGGLE_DOCK,
TOGGLE_THEME,
ZOOM_IN,
Expand Down Expand Up @@ -70,15 +69,6 @@ export const useConfigReducer: React.Reducer<AppConfig, ReducerAction> = (
);
return state;

case ConfigActionType.TOGGLE_AUTOFORMAT:
return {
...state,
settings: {
...state.settings,
autoformat: !state.settings.autoformat,
},
};

case ConfigActionType.TOGGLE_DOCK:
return {
...state,
Expand Down
1 change: 0 additions & 1 deletion src/typings/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { CanvasDirection } from "reaflow";
export interface StorageConfig {
layout: CanvasDirection;
expand: boolean;
autoformat: boolean;
hideEditor: boolean;
zoomPanPinch: ReactZoomPanPinchRef | null;
lightmode: boolean;
Expand Down
Loading