remove safari edge cases (#1273)

main
Michael Fix 3 years ago committed by GitHub
parent 3aef99b033
commit 6fd6d99a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,8 +32,8 @@ import {
DEFAULT_THEME, DEFAULT_THEME,
FONTS, FONTS,
} from '../lib/constants' } from '../lib/constants'
import { serializeState, getRouteState } from '../lib/routing' import { getRouteState } from '../lib/routing'
import { getSettings, unescapeHtml, formatCode, omit, dataURLtoBlob } from '../lib/util' import { getSettings, unescapeHtml, formatCode, omit } from '../lib/util'
import domtoimage from '../lib/dom-to-image' import domtoimage from '../lib/dom-to-image'
const languageIcon = <LanguageIcon /> const languageIcon = <LanguageIcon />
@ -79,9 +79,6 @@ class Editor extends React.Component {
this.setState(newState) this.setState(newState)
if (window.navigator) { if (window.navigator) {
this.isSafari =
window.navigator.userAgent.indexOf('Safari') !== -1 &&
window.navigator.userAgent.indexOf('Chrome') === -1
this.isFirefox = this.isFirefox =
window.navigator.userAgent.indexOf('Firefox') !== -1 && window.navigator.userAgent.indexOf('Firefox') !== -1 &&
window.navigator.userAgent.indexOf('Chrome') === -1 window.navigator.userAgent.indexOf('Chrome') === -1
@ -162,20 +159,6 @@ class Editor extends React.Component {
.then(data => new Blob([data], { type: 'image/svg+xml' })) .then(data => new Blob([data], { type: 'image/svg+xml' }))
} }
// if safari, get image from api
if (this.context.image && this.isSafari) {
const themeConfig = this.getTheme()
// pull from custom theme highlights, or state highlights
const encodedState = serializeState({
...this.state,
highlights: { ...themeConfig.highlights, ...this.state.highlights },
})
// TODO consider returning blob responseType from axios
return this.context
.image(encodedState)
.then(dataURL => (type === 'blob' ? dataURLtoBlob(dataURL) : dataURL))
}
if (type === 'blob') { if (type === 'blob') {
return domtoimage.toBlob(node, config) return domtoimage.toBlob(node, config)
} }
@ -389,7 +372,6 @@ class Editor extends React.Component {
onChange={this.updateSetting} onChange={this.updateSetting}
exportImage={this.exportImage} exportImage={this.exportImage}
exportSize={exportSize} exportSize={exportSize}
backgroundImage={backgroundImage}
/> />
</div> </div>
</div> </div>

@ -1,5 +1,5 @@
import React from 'react' import React from 'react'
import { useOnline, useKeyboardListener, useAsyncCallback } from 'actionsack' import { useKeyboardListener, useAsyncCallback } from 'actionsack'
import { COLORS, EXPORT_SIZES } from '../lib/constants' import { COLORS, EXPORT_SIZES } from '../lib/constants'
import Button from './Button' import Button from './Button'
@ -8,32 +8,8 @@ import Popout, { managePopout } from './Popout'
import { Down as ArrowDown } from './svg/Arrows' import { Down as ArrowDown } from './svg/Arrows'
const MAX_PAYLOAD_SIZE = 5e6 // bytes
function verifyPayloadSize(str) {
if (typeof str !== 'string') return true
if (typeof window !== 'undefined') {
return new Blob([str]).size < MAX_PAYLOAD_SIZE
}
return Buffer.byteLength(str, 'utf8')
}
const popoutStyle = { width: '256px', right: 0 } const popoutStyle = { width: '256px', right: 0 }
function useSafari() {
const [isSafari, setSafari] = React.useState(false)
React.useEffect(() => {
setSafari(
window.navigator &&
window.navigator.userAgent.indexOf('Safari') !== -1 &&
window.navigator.userAgent.indexOf('Chrome') === -1
)
}, [])
return isSafari
}
function preventDefault(fn) { function preventDefault(fn) {
return e => { return e => {
e.preventDefault() e.preventDefault()
@ -41,23 +17,11 @@ function preventDefault(fn) {
} }
} }
function ExportMenu({ function ExportMenu({ onChange, exportSize, isVisible, toggleVisibility, exportImage: exp }) {
backgroundImage,
onChange,
exportSize,
isVisible,
toggleVisibility,
exportImage: exp,
}) {
const tooLarge = React.useMemo(() => !verifyPayloadSize(backgroundImage), [backgroundImage])
const online = useOnline()
const isSafari = useSafari()
const input = React.useRef() const input = React.useRef()
const [exportImage, { loading }] = useAsyncCallback(exp) const [exportImage, { loading }] = useAsyncCallback(exp)
const disablePNG = isSafari && (tooLarge || !online)
const handleExportSizeChange = selectedSize => () => onChange('exportSize', selectedSize) const handleExportSizeChange = selectedSize => () => onChange('exportSize', selectedSize)
const handleExport = format => () => const handleExport = format => () =>
@ -133,7 +97,6 @@ function ExportMenu({
<div className="save-container"> <div className="save-container">
<span>Download</span> <span>Download</span>
<div> <div>
{!disablePNG && (
<Button <Button
center center
margin="0 8px 0 0" margin="0 8px 0 0"
@ -145,7 +108,6 @@ function ExportMenu({
> >
PNG PNG
</Button> </Button>
)}
<Button <Button
center center
hoverColor={COLORS.PURPLE} hoverColor={COLORS.PURPLE}

@ -40,10 +40,6 @@ function checkIfRateLimited(err) {
throw err throw err
} }
function image(state) {
return client.post('/image', { state }).then(res => res.data)
}
function openTwitterUrl(twitterUrl) { function openTwitterUrl(twitterUrl) {
const width = 575 const width = 575
const height = 400 const height = 400
@ -177,7 +173,6 @@ export default {
delete: id => deleteSnippet(id), delete: id => deleteSnippet(id),
}, },
tweet: debounce(tweet, ms('5s'), { leading: true, trailing: false }), tweet: debounce(tweet, ms('5s'), { leading: true, trailing: false }),
image: debounce(image, ms('5s'), { leading: true, trailing: false }),
unsplash, unsplash,
imgur, imgur,
downloadThumbnailImage, downloadThumbnailImage,

@ -94,15 +94,3 @@ export const formatCode = async code => {
export const stringifyColor = obj => `rgba(${obj.rgb.r},${obj.rgb.g},${obj.rgb.b},${obj.rgb.a})` export const stringifyColor = obj => `rgba(${obj.rgb.r},${obj.rgb.g},${obj.rgb.b},${obj.rgb.a})`
export const generateId = () => Math.random().toString(36).slice(2) export const generateId = () => Math.random().toString(36).slice(2)
export function dataURLtoBlob(dataurl) {
const [first, second] = dataurl.split(',')
const mime = first.match(/:(.*?);/)[1]
const bstr = atob(second)
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: mime })
}

Loading…
Cancel
Save