Conditionally disable PNG export for Safari

main
raboid 6 years ago committed by Michael Fix
parent ff9e4102e8
commit 3aec3ebc28

@ -30,10 +30,19 @@ import {
DEFAULT_CODE, DEFAULT_CODE,
DEFAULT_SETTINGS, DEFAULT_SETTINGS,
DEFAULT_LANGUAGE, DEFAULT_LANGUAGE,
DEFAULT_PRESET_ID DEFAULT_PRESET_ID,
MAX_PAYLOAD_SIZE
} from '../lib/constants' } from '../lib/constants'
import { serializeState, getQueryStringState } from '../lib/routing' import { serializeState, getQueryStringState } from '../lib/routing'
import { getSettings, escapeHtml, unescapeHtml, formatCode, omit } from '../lib/util' import {
getSettings,
escapeHtml,
unescapeHtml,
formatCode,
omit,
isSafari,
sizeOf
} from '../lib/util'
import LanguageIcon from './svg/Language' import LanguageIcon from './svg/Language'
import ThemeIcon from './svg/Theme' import ThemeIcon from './svg/Theme'
@ -103,6 +112,8 @@ class Editor extends React.Component {
window.addEventListener('offline', this.setOffline) window.addEventListener('offline', this.setOffline)
window.addEventListener('online', this.setOnline) window.addEventListener('online', this.setOnline)
this.isSafari = isSafari()
} }
componentWillUnmount() { componentWillUnmount() {
@ -128,12 +139,7 @@ class Editor extends React.Component {
) { ) {
// if safari, get image from api // if safari, get image from api
const isPNG = format !== 'svg' const isPNG = format !== 'svg'
if ( if (this.props.api.image && this.isSafari && isPNG) {
this.props.api.image &&
navigator.userAgent.indexOf('Safari') !== -1 &&
navigator.userAgent.indexOf('Chrome') === -1 &&
isPNG
) {
const encodedState = serializeState(this.state) const encodedState = serializeState(this.state)
return this.props.api.image(encodedState) return this.props.api.image(encodedState)
} }
@ -264,6 +270,12 @@ class Editor extends React.Component {
} }
updateBackground({ photographer, ...changes } = {}) { updateBackground({ photographer, ...changes } = {}) {
if (this.isSafari) {
this.disablePNG =
typeof changes.backgroundImage === 'string' &&
sizeOf(changes.backgroundImage) > MAX_PAYLOAD_SIZE
}
if (photographer) { if (photographer) {
this.updateState(({ code = DEFAULT_CODE }) => ({ this.updateState(({ code = DEFAULT_CODE }) => ({
...changes, ...changes,
@ -370,6 +382,7 @@ class Editor extends React.Component {
onChange={this.updateSetting} onChange={this.updateSetting}
export={this.export} export={this.export}
exportSize={exportSize} exportSize={exportSize}
disablePNG={this.disablePNG}
/> />
</div> </div>
</Toolbar> </Toolbar>

@ -40,7 +40,7 @@ class ExportMenu extends React.PureComponent {
handleExport = format => () => this.props.export(format) handleExport = format => () => this.props.export(format)
render() { render() {
const { exportSize, filename, isVisible, toggleVisibility } = this.props const { exportSize, filename, isVisible, toggleVisibility, disablePNG } = this.props
return ( return (
<div className="export-menu-container" id="export-menu"> <div className="export-menu-container" id="export-menu">
@ -98,6 +98,7 @@ class ExportMenu extends React.PureComponent {
<div className="save-container"> <div className="save-container">
<span>Save as</span> <span>Save as</span>
<div> <div>
{!disablePNG && (
<Button <Button
center center
margin="0 8px 0 0" margin="0 8px 0 0"
@ -108,6 +109,7 @@ class ExportMenu extends React.PureComponent {
> >
PNG PNG
</Button> </Button>
)}
<Button <Button
center center
hoverColor={COLORS.PURPLE} hoverColor={COLORS.PURPLE}

@ -641,3 +641,5 @@ export const DEFAULT_PRESETS = [
id: 'preset:6' id: 'preset:6'
} }
] ]
export const MAX_PAYLOAD_SIZE = 5000000 //bytes

@ -76,3 +76,11 @@ export const omit = (object, keys) => omitBy(object, (_, k) => keys.indexOf(k) >
export const stringifyRGBA = obj => `rgba(${obj.r},${obj.g},${obj.b},${obj.a})` export const stringifyRGBA = obj => `rgba(${obj.r},${obj.g},${obj.b},${obj.a})`
export const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1) export const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1)
export const isSafari = () =>
window &&
window.navigator &&
window.navigator.userAgent.indexOf('Safari') !== -1 &&
window.navigator.userAgent.indexOf('Chrome') === -1
export const sizeOf = str => str.length * 2 //bytes

Loading…
Cancel
Save