From 58ef28fe89ee4efe13ff9fc55ffb90a48a9458ef Mon Sep 17 00:00:00 2001 From: Siklos Date: Thu, 18 Aug 2022 17:10:28 +0200 Subject: [PATCH] Remove selector from svg export + fix svg format --- src/Components/Editor/Save.ts | 42 ++++++++++++++++++++++++++++++----- src/Components/SVG/SVG.tsx | 2 +- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/Components/Editor/Save.ts b/src/Components/Editor/Save.ts index 0dc783b..0b355b7 100644 --- a/src/Components/Editor/Save.ts +++ b/src/Components/Editor/Save.ts @@ -38,14 +38,44 @@ export function SaveEditorAsJSON( export function SaveEditorAsSVG(): void { const svgWrapper = document.getElementById(ID) as HTMLElement; - const svg = svgWrapper.querySelector('svg') as SVGSVGElement; - const preface = '\r\n'; - const svgBlob = new Blob([preface, svg.outerHTML], { type: 'image/svg+xml;charset=utf-8' }); - const svgUrl = URL.createObjectURL(svgBlob); - createDownloadNode('state.svg', svgUrl); + let svg = svgWrapper.querySelector('svg') as SVGSVGElement; + + if (svg === undefined) { + throw new Error('[SaveEditorAsSVG] Missing element'); + } + + // Recover svg from SVG Viewer + svg = svg.cloneNode(true) as SVGSVGElement; + svg.removeAttribute('height'); + svg.removeAttribute('width'); + const mainSvg = svg.children[1].children; + svg.replaceChildren(...mainSvg); + + // remove the selector + const group = svg.children[svg.children.length - 1]; + group.removeChild(group.children[group.children.length - 1]); + + // get svg source. + const serializer = new XMLSerializer(); + let source = serializer.serializeToString(svg); + + // add name spaces. + if (source.match(/^]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/) == null) { + source = source.replace(/^]+"http\:\/\/www\.w3\.org\/1999\/xlink"/) == null) { + source = source.replace(/^ = (props: ISVGProps) => { > { children } - { SHOW_DIMENSIONS_PER_DEPTH ? : null } + {/* leave this at the end so it can be removed during the svg export */}