Implement export as SVG + improve style
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Siklos 2022-08-05 18:12:00 +02:00
parent 2dcdf769f1
commit aa29cd37ff
3 changed files with 49 additions and 24 deletions

View file

@ -15,14 +15,14 @@ const toggleState = (
const FloatingButton: React.FC<IFloatingButtonProps> = (props: IFloatingButtonProps) => {
const [isHidden, setHidden] = React.useState(true);
const buttonListClasses = isHidden ? 'opacity-0' : 'opacity-100';
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 ${buttonListClasses}`}>
<div className={`transition-all flex flex-col gap-2 items-center ${buttonListClasses}`}>
{ props.children }
</div>
<button

View file

@ -18,6 +18,7 @@ interface ISVGState {
export class SVG extends React.PureComponent<ISVGProps> {
public state: ISVGState;
public static ID = 'svg';
constructor(props: ISVGProps) {
super(props);
@ -47,25 +48,28 @@ export class SVG extends React.PureComponent<ISVGProps> {
}
return (
<ReactSVGPanZoom
width={window.innerWidth}
height={window.innerHeight}
background={'#ffffff'}
defaultTool='pan'
value={this.state.value} onChangeValue={value => this.setState({ value })}
tool={this.state.tool} onChangeTool={tool => this.setState({ tool })}
miniatureProps={{
position: 'left',
background: '#616264',
width: window.innerWidth - 12,
height: 120
}}
>
<svg {...properties}>
{ children }
<Selector selected={this.props.selected} />
</svg>
</ReactSVGPanZoom>
<div id={SVG.ID}>
<ReactSVGPanZoom
width={window.innerWidth}
height={window.innerHeight}
background={'#ffffff'}
defaultTool='pan'
value={this.state.value} onChangeValue={value => this.setState({ value })}
tool={this.state.tool} onChangeTool={tool => this.setState({ tool })}
miniatureProps={{
position: 'left',
background: '#616264',
width: window.innerWidth - 12,
height: 120
}}
>
<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';
@ -258,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);
@ -271,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
@ -339,10 +353,17 @@ class Editor extends React.Component<IEditorProps> {
<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 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>