From dbde17a65f652976a60282b022d055eacbaa9c10 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Mon, 21 Jan 2019 17:13:33 -0800 Subject: [PATCH] reduce imports and exports --- components/Editor.js | 15 +++++++-------- lib/constants.js | 2 -- lib/util.js | 11 +++++------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/components/Editor.js b/components/Editor.js index a485635..3fd9056 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -30,8 +30,7 @@ import { DEFAULT_CODE, DEFAULT_SETTINGS, DEFAULT_LANGUAGE, - DEFAULT_PRESET_ID, - MAX_PAYLOAD_SIZE + DEFAULT_PRESET_ID } from '../lib/constants' import { serializeState, getQueryStringState } from '../lib/routing' import { @@ -40,8 +39,7 @@ import { unescapeHtml, formatCode, omit, - isSafari, - sizeOf + verifyPayloadSize } from '../lib/util' import LanguageIcon from './svg/Language' import ThemeIcon from './svg/Theme' @@ -113,7 +111,10 @@ class Editor extends React.Component { window.addEventListener('offline', this.setOffline) window.addEventListener('online', this.setOnline) - this.isSafari = isSafari() + this.isSafari = + window.navigator && + window.navigator.userAgent.indexOf('Safari') !== -1 && + window.navigator.userAgent.indexOf('Chrome') === -1 } componentWillUnmount() { @@ -271,9 +272,7 @@ class Editor extends React.Component { updateBackground({ photographer, ...changes } = {}) { if (this.isSafari) { - this.disablePNG = - typeof changes.backgroundImage === 'string' && - sizeOf(changes.backgroundImage) > MAX_PAYLOAD_SIZE + this.disablePNG = !verifyPayloadSize(changes.backgroundImage) } if (photographer) { diff --git a/lib/constants.js b/lib/constants.js index 873013c..19fdcd7 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -641,5 +641,3 @@ export const DEFAULT_PRESETS = [ id: 'preset:6' } ] - -export const MAX_PAYLOAD_SIZE = 5000000 //bytes diff --git a/lib/util.js b/lib/util.js index d3d50a0..fa9e65b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -77,10 +77,9 @@ 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 isSafari = () => - window && - window.navigator && - window.navigator.userAgent.indexOf('Safari') !== -1 && - window.navigator.userAgent.indexOf('Chrome') === -1 +const MAX_PAYLOAD_SIZE = 5e6 +export const verifyPayloadSize = str => { + if (typeof str !== 'string') return true -export const sizeOf = str => str.length * 2 //bytes + return new Blob([str]).size < MAX_PAYLOAD_SIZE // bytes +}