Implement export as SVG (#14)
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: https://git.siklos-chaneru.duckdns.org/Siklos/svg-layout-designer-react/pulls/14
This commit is contained in:
Siklos 2022-08-05 12:10:58 -04:00
parent c265659b2d
commit 8e34d6b72a
4 changed files with 90 additions and 32 deletions

View file

@ -1,10 +1,38 @@
import * as React from 'react';
import { MenuIcon, XIcon } from '@heroicons/react/outline';
interface IFloatingButtonProps {
children: React.ReactNode[] | React.ReactNode
className: string
}
const toggleState = (
isHidden: boolean,
setHidden: React.Dispatch<React.SetStateAction<boolean>>
) => {
setHidden(!isHidden);
};
const FloatingButton: React.FC<IFloatingButtonProps> = (props: IFloatingButtonProps) => {
return <></>;
const [isHidden, setHidden] = React.useState(true);
const buttonListClasses = isHidden ? 'invisible opacity-0' : 'visible opacity-100';
const icon = isHidden
? <MenuIcon className="floating-btn" />
: <XIcon className="floating-btn" />;
return (
<div className={props.className}>
<div className={`transition-all flex flex-col gap-2 items-center ${buttonListClasses}`}>
{ props.children }
</div>
<button
className={'transition-all w-14 h-14 p-2 align-middle items-center justify-center rounded-full bg-blue-500 hover:bg-blue-800'}
title='Open menu'
onClick={() => toggleState(isHidden, setHidden)}
>
{ icon }
</button>
</div>);
};
export default FloatingButton;

View file

@ -18,7 +18,7 @@ interface ISVGState {
export class SVG extends React.PureComponent<ISVGProps> {
public state: ISVGState;
public svg: React.RefObject<SVGSVGElement>;
public static ID = 'svg';
constructor(props: ISVGProps) {
super(props);
@ -29,7 +29,6 @@ export class SVG extends React.PureComponent<ISVGProps> {
} as Value,
tool: TOOL_PAN
};
this.svg = React.createRef<SVGSVGElement>();
}
render() {
@ -49,6 +48,7 @@ export class SVG extends React.PureComponent<ISVGProps> {
}
return (
<div id={SVG.ID}>
<ReactSVGPanZoom
width={window.innerWidth}
height={window.innerHeight}
@ -63,11 +63,13 @@ export class SVG extends React.PureComponent<ISVGProps> {
height: 120
}}
>
<svg ref={this.svg} {...properties}>
<svg {...properties}>
{ children }
<Selector selected={this.props.selected} />
</svg>
</ReactSVGPanZoom>
</div>
);
};
}

View file

@ -1,5 +1,5 @@
import React from 'react';
import { UploadIcon } from '@heroicons/react/outline';
import { UploadIcon, PhotographIcon } from '@heroicons/react/outline';
import './Editor.scss';
import Sidebar from './Components/Sidebar/Sidebar';
import { ElementsSidebar } from './Components/ElementsSidebar/ElementsSidebar';
@ -9,6 +9,7 @@ import { History } from './Components/History/History';
import { ContainerModel, findContainerById, IContainerModel, MakeIterator } from './Interfaces/ContainerModel';
import Properties from './Interfaces/Properties';
import { IHistoryState } from './App';
import FloatingButton from './Components/FloatingButton/FloatingButton';
interface IEditorProps {
configuration: Configuration,
@ -257,7 +258,7 @@ class Editor extends React.Component<IEditorProps> {
} as IEditorState);
}
public SaveEditor() {
public SaveEditorAsJSON() {
const exportName = 'state';
const spaces = import.meta.env.DEV ? 4 : 0;
const data = JSON.stringify(this.state, getCircularReplacer(), spaces);
@ -270,6 +271,20 @@ class Editor extends React.Component<IEditorProps> {
downloadAnchorNode.remove();
}
public SaveEditorAsSVG() {
const svgWrapper = document.getElementById(SVG.ID) as HTMLElement;
const svg = svgWrapper.querySelector('svg') as SVGSVGElement;
const preface = '<?xml version="1.0" standalone="no"?>\r\n';
const svgBlob = new Blob([preface, svg.outerHTML], { type: 'image/svg+xml;charset=utf-8' });
const svgUrl = URL.createObjectURL(svgBlob);
const downloadLink = document.createElement('a');
downloadLink.href = svgUrl;
downloadLink.download = 'newesttree.svg';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
/**
* Render the application
* @returns {JSX.Element} Rendered JSX element
@ -334,13 +349,23 @@ class Editor extends React.Component<IEditorProps> {
>
{ current.MainContainer }
</SVG>
<FloatingButton className={`fixed flex flex-col gap-2 items-center bottom-40 ${buttonRightOffsetClasses}`}>
<button
className={`fixed transition-all ${buttonRightOffsetClasses} bottom-40 w-14 h-14 p-2 align-middle items-center justify-center rounded-full bg-blue-500 hover:bg-blue-800`}
className={'transition-all w-10 h-10 p-2 align-middle items-center justify-center rounded-full bg-blue-500 hover:bg-blue-800'}
title='Export as JSON'
onClick={() => this.SaveEditor()}
onClick={() => this.SaveEditorAsJSON()}
>
<UploadIcon className="h-full w-full text-white align-middle items-center justify-center" />
</button>
<button
className={'transition-all w-10 h-10 p-2 align-middle items-center justify-center rounded-full bg-blue-500 hover:bg-blue-800'}
title='Export as SVG'
onClick={() => this.SaveEditorAsSVG()}
>
<PhotographIcon className="h-full w-full text-white align-middle items-center justify-center" />
</button>
</FloatingButton>
</div>
);
}

View file

@ -15,4 +15,7 @@
.mainmenu-btn {
@apply transition-all bg-blue-100 hover:bg-blue-200 text-blue-700 text-lg font-semibold p-8 rounded-lg
}
.floating-btn {
@apply h-full w-full text-white align-middle items-center justify-center
}
}