From 89bac8953bf817ea00e213083d3b4e10d4056bd6 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 13 Jan 2019 10:57:58 -0500 Subject: [PATCH] Pulling SVG and PNG download link code into separate file --- src/components/FormActions/index.js | 48 +--------------------------- src/components/FormActions/links.js | 49 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 47 deletions(-) create mode 100644 src/components/FormActions/links.js diff --git a/src/components/FormActions/index.js b/src/components/FormActions/index.js index 3495328..85f7761 100644 --- a/src/components/FormActions/index.js +++ b/src/components/FormActions/index.js @@ -6,53 +6,7 @@ import LinkIcon from 'react-feather/dist/icons/link'; import style from './style.module.css'; -const createSvgLink = async ({ svg }) => { - try { - const type = 'image/svg+xml'; - const blob = new Blob([svg], { type }); - - return { - url: URL.createObjectURL(blob), - label: 'Download SVG', - filename: 'image.svg', - type - }; - } - catch (e) { - console.error(e); // eslint-disable-line no-console - } -}; - -const createPngLink = async ({ svg, width, height }) => { - try { - const type = 'image/png'; - const canvas = document.createElement('canvas'); - const context = canvas.getContext('2d'); - const loader = new Image(); - - loader.width = canvas.width = width * 2; - loader.height = canvas.height = height * 2; - - await new Promise(resolve => { - loader.onload = resolve; - loader.src = 'data:image/svg+xml,' + encodeURIComponent(svg); - }); - - context.drawImage(loader, 0, 0, loader.width, loader.height); - - const blob = await new Promise(resolve => canvas.toBlob(resolve, type)); - - return { - url: URL.createObjectURL(blob), - label: 'Download PNG', - filename: 'image.png', - type - }; - } - catch (e) { - console.error(e); // eslint-disable-line no-console - } -}; +import { createPngLink, createSvgLink } from './links'; class FormActions extends React.PureComponent { state = {} diff --git a/src/components/FormActions/links.js b/src/components/FormActions/links.js new file mode 100644 index 0000000..fe71511 --- /dev/null +++ b/src/components/FormActions/links.js @@ -0,0 +1,49 @@ +const createSvgLink = async ({ svg }) => { + try { + const type = 'image/svg+xml'; + const blob = new Blob([svg], { type }); + + return { + url: URL.createObjectURL(blob), + label: 'Download SVG', + filename: 'image.svg', + type + }; + } + catch (e) { + console.error(e); // eslint-disable-line no-console + } +}; + +const createPngLink = async ({ svg, width, height }) => { + try { + const type = 'image/png'; + const canvas = document.createElement('canvas'); + const context = canvas.getContext('2d'); + const loader = new Image(); + + loader.width = canvas.width = width * 2; + loader.height = canvas.height = height * 2; + + await new Promise(resolve => { + loader.onload = resolve; + loader.src = 'data:image/svg+xml,' + encodeURIComponent(svg); + }); + + context.drawImage(loader, 0, 0, loader.width, loader.height); + + const blob = await new Promise(resolve => canvas.toBlob(resolve, type)); + + return { + url: URL.createObjectURL(blob), + label: 'Download PNG', + filename: 'image.png', + type + }; + } + catch (e) { + console.error(e); // eslint-disable-line no-console + } +}; + +export { createSvgLink, createPngLink };