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 * as React from 'react';
import { MenuIcon, XIcon } from '@heroicons/react/outline';
interface IFloatingButtonProps { 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) => { 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; export default FloatingButton;

View file

@ -18,7 +18,7 @@ interface ISVGState {
export class SVG extends React.PureComponent<ISVGProps> { export class SVG extends React.PureComponent<ISVGProps> {
public state: ISVGState; public state: ISVGState;
public svg: React.RefObject<SVGSVGElement>; public static ID = 'svg';
constructor(props: ISVGProps) { constructor(props: ISVGProps) {
super(props); super(props);
@ -29,7 +29,6 @@ export class SVG extends React.PureComponent<ISVGProps> {
} as Value, } as Value,
tool: TOOL_PAN tool: TOOL_PAN
}; };
this.svg = React.createRef<SVGSVGElement>();
} }
render() { render() {
@ -49,25 +48,28 @@ export class SVG extends React.PureComponent<ISVGProps> {
} }
return ( return (
<ReactSVGPanZoom <div id={SVG.ID}>
width={window.innerWidth} <ReactSVGPanZoom
height={window.innerHeight} width={window.innerWidth}
background={'#ffffff'} height={window.innerHeight}
defaultTool='pan' background={'#ffffff'}
value={this.state.value} onChangeValue={value => this.setState({ value })} defaultTool='pan'
tool={this.state.tool} onChangeTool={tool => this.setState({ tool })} value={this.state.value} onChangeValue={value => this.setState({ value })}
miniatureProps={{ tool={this.state.tool} onChangeTool={tool => this.setState({ tool })}
position: 'left', miniatureProps={{
background: '#616264', position: 'left',
width: window.innerWidth - 12, background: '#616264',
height: 120 width: window.innerWidth - 12,
}} height: 120
> }}
<svg ref={this.svg} {...properties}> >
{ children } <svg {...properties}>
<Selector selected={this.props.selected} /> { children }
</svg> <Selector selected={this.props.selected} />
</ReactSVGPanZoom> </svg>
</ReactSVGPanZoom>
</div>
); );
}; };
} }

View file

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { UploadIcon } from '@heroicons/react/outline'; import { UploadIcon, PhotographIcon } from '@heroicons/react/outline';
import './Editor.scss'; import './Editor.scss';
import Sidebar from './Components/Sidebar/Sidebar'; import Sidebar from './Components/Sidebar/Sidebar';
import { ElementsSidebar } from './Components/ElementsSidebar/ElementsSidebar'; 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 { ContainerModel, findContainerById, IContainerModel, MakeIterator } from './Interfaces/ContainerModel';
import Properties from './Interfaces/Properties'; import Properties from './Interfaces/Properties';
import { IHistoryState } from './App'; import { IHistoryState } from './App';
import FloatingButton from './Components/FloatingButton/FloatingButton';
interface IEditorProps { interface IEditorProps {
configuration: Configuration, configuration: Configuration,
@ -257,7 +258,7 @@ class Editor extends React.Component<IEditorProps> {
} as IEditorState); } as IEditorState);
} }
public SaveEditor() { public SaveEditorAsJSON() {
const exportName = 'state'; const exportName = 'state';
const spaces = import.meta.env.DEV ? 4 : 0; const spaces = import.meta.env.DEV ? 4 : 0;
const data = JSON.stringify(this.state, getCircularReplacer(), spaces); const data = JSON.stringify(this.state, getCircularReplacer(), spaces);
@ -270,6 +271,20 @@ class Editor extends React.Component<IEditorProps> {
downloadAnchorNode.remove(); 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 * Render the application
* @returns {JSX.Element} Rendered JSX element * @returns {JSX.Element} Rendered JSX element
@ -334,13 +349,23 @@ class Editor extends React.Component<IEditorProps> {
> >
{ current.MainContainer } { current.MainContainer }
</SVG> </SVG>
<button <FloatingButton className={`fixed flex flex-col gap-2 items-center bottom-40 ${buttonRightOffsetClasses}`}>
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`} <button
title='Export as JSON' className={'transition-all w-10 h-10 p-2 align-middle items-center justify-center rounded-full bg-blue-500 hover:bg-blue-800'}
onClick={() => this.SaveEditor()} title='Export as JSON'
> onClick={() => this.SaveEditorAsJSON()}
<UploadIcon className="h-full w-full text-white align-middle items-center justify-center" /> >
</button> <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> </div>
); );
} }

View file

@ -15,4 +15,7 @@
.mainmenu-btn { .mainmenu-btn {
@apply transition-all bg-blue-100 hover:bg-blue-200 text-blue-700 text-lg font-semibold p-8 rounded-lg @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
}
} }