Consolidate type and format into one param (#1286)

* Consolidate type and format into one param

Closes #1274

* bug fix Open button

* update filename bug
main
Mike Fix 3 years ago committed by GitHub
parent 9c08219e10
commit b78520c90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -96,7 +96,6 @@ class Editor extends React.Component {
getCarbonImage = async ( getCarbonImage = async (
{ {
format, format,
type,
squared = this.state.squaredImage, squared = this.state.squaredImage,
exportSize = (EXPORT_SIZES_HASH[this.state.exportSize] || DEFAULT_EXPORT_SIZE).value, exportSize = (EXPORT_SIZES_HASH[this.state.exportSize] || DEFAULT_EXPORT_SIZE).value,
} = { format: 'png' } } = { format: 'png' }
@ -128,7 +127,6 @@ class Editor extends React.Component {
height, height,
} }
// TODO consolidate type/format to only use one param
if (format === 'svg') { if (format === 'svg') {
return domtoimage return domtoimage
.toSvg(node, config) .toSvg(node, config)
@ -154,7 +152,7 @@ class Editor extends React.Component {
.then(data => new Blob([data], { type: 'image/svg+xml' })) .then(data => new Blob([data], { type: 'image/svg+xml' }))
} }
if (type === 'blob') { if (format === 'blob') {
return domtoimage.toBlob(node, config) return domtoimage.toBlob(node, config)
} }
@ -174,16 +172,16 @@ class Editor extends React.Component {
return this.getCarbonImage({ format: 'png' }).then(data => this.context.imgur(data, prefix)) return this.getCarbonImage({ format: 'png' }).then(data => this.context.imgur(data, prefix))
} }
exportImage = (format = 'png', options = {}) => { exportImage = (format = 'blob', options = {}) => {
const link = document.createElement('a') const link = document.createElement('a')
const prefix = options.filename || this.state.name || 'carbon' const prefix = options.filename || this.state.name || 'carbon'
return this.getCarbonImage({ format, type: 'blob' }) return this.getCarbonImage({ format })
.then(blob => window.URL.createObjectURL(blob)) .then(blob => window.URL.createObjectURL(blob))
.then(url => { .then(url => {
if (format !== 'open') { if (!options.open) {
link.download = `${prefix}.${format}` link.download = `${prefix}.${format === 'svg' ? 'svg' : 'png'}`
} }
if ( if (
// isFirefox // isFirefox
@ -200,7 +198,7 @@ class Editor extends React.Component {
} }
copyImage = () => copyImage = () =>
this.getCarbonImage({ format: 'png', type: 'blob' }) this.getCarbonImage({ format: 'blob' })
.then(blob => .then(blob =>
navigator.clipboard.write([ navigator.clipboard.write([
new window.ClipboardItem({ new window.ClipboardItem({

@ -25,11 +25,9 @@ function ExportMenu({ onChange, exportSize, isVisible, toggleVisibility, exportI
const handleExportSizeChange = selectedSize => () => onChange('exportSize', selectedSize) const handleExportSizeChange = selectedSize => () => onChange('exportSize', selectedSize)
const handleExport = format => () => const handleExport = format => () =>
exportImage(format, { exportImage(format, { filename: input.current && input.current.value })
filename: input.current && input.current.value,
})
useKeyboardListener('⌘-⇧-e', preventDefault(handleExport('png'))) useKeyboardListener('⌘-⇧-e', preventDefault(handleExport('blob')))
useKeyboardListener('⌘-⇧-s', preventDefault(handleExport('svg'))) useKeyboardListener('⌘-⇧-s', preventDefault(handleExport('svg')))
return ( return (
@ -40,7 +38,7 @@ function ExportMenu({ onChange, exportSize, isVisible, toggleVisibility, exportI
large large
center center
color={COLORS.PURPLE} color={COLORS.PURPLE}
onClick={handleExport('png')} onClick={handleExport('blob')}
data-cy="quick-export-button" data-cy="quick-export-button"
style={{ minWidth: 92, borderBottomRightRadius: 0, borderTopRightRadius: 0 }} style={{ minWidth: 92, borderBottomRightRadius: 0, borderTopRightRadius: 0 }}
title="Quick export" title="Quick export"
@ -91,7 +89,13 @@ function ExportMenu({ onChange, exportSize, isVisible, toggleVisibility, exportI
</div> </div>
</div> </div>
<div className="export-row"> <div className="export-row">
<Button center color={COLORS.PURPLE} onClick={handleExport('open')}> <Button
center
color={COLORS.PURPLE}
onClick={() =>
exportImage('blob', { filename: input.current && input.current.value, open: true })
}
>
Open Open
</Button> </Button>
<div className="save-container"> <div className="save-container">
@ -102,7 +106,7 @@ function ExportMenu({ onChange, exportSize, isVisible, toggleVisibility, exportI
margin="0 8px 0 0" margin="0 8px 0 0"
hoverColor={COLORS.PURPLE} hoverColor={COLORS.PURPLE}
color={COLORS.DARK_PURPLE} color={COLORS.DARK_PURPLE}
onClick={handleExport('png')} onClick={handleExport('blob')}
id="export-png" id="export-png"
disabled={loading} disabled={loading}
> >

Loading…
Cancel
Save