Import/export config (#771)

* import export config

* allow rendering button as link

* pass all props to button component

* fix invalid JSON path

* prevent dragging download URL

* address concerns
main
Michael Fix 5 years ago committed by repo-ranger[bot]
parent ae93f9880a
commit 22a0f9f2c1

@ -5,9 +5,7 @@ import { COLORS } from '../lib/constants'
const Button = ({ const Button = ({
id, id,
'data-cy': cypressElementId,
onClick = () => {}, onClick = () => {},
className = '',
background = COLORS.BLACK, background = COLORS.BLACK,
color = COLORS.SECONDARY, color = COLORS.SECONDARY,
hoverBackground = COLORS.HOVER, hoverBackground = COLORS.HOVER,
@ -18,25 +16,19 @@ const Button = ({
border, border,
center, center,
large, large,
style = {},
flex = 1, flex = 1,
padding = 0, padding = 0,
margin = 0, margin = 0,
title title,
Component = 'button',
...props
}) => ( }) => (
<button <Component id={id} onClick={onClick} disabled={disabled} {...props}>
id={id}
onClick={onClick}
className={className}
disabled={disabled}
style={style}
data-cy={cypressElementId}
>
{title && <VisuallyHidden>{title}</VisuallyHidden>} {title && <VisuallyHidden>{title}</VisuallyHidden>}
{children} {children}
<style jsx> <style jsx>
{` {`
button { ${Component} {
display: flex; display: flex;
flex: ${flex}; flex: ${flex};
background-color: ${background}; background-color: ${background};
@ -55,18 +47,17 @@ const Button = ({
font-size: ${large ? '14px' : '12px'}; font-size: ${large ? '14px' : '12px'};
} }
button:hover, ${Component}:hover, ${Component}:focus {
button:focus {
background-color: ${hoverBackground} !important; background-color: ${hoverBackground} !important;
color: ${hoverColor || color}; color: ${hoverColor || color};
} }
button:focus { ${Component}:focus {
box-shadow: ${border ? `inset 0px 0px 0px 2px ${color}` : 'initial'}; box-shadow: ${border ? `inset 0px 0px 0px 2px ${color}` : 'initial'};
} }
`} `}
</style> </style>
</button> </Component>
) )
export default Button export default Button

@ -9,7 +9,7 @@ import Popout, { managePopout } from './Popout'
import Button from './Button' import Button from './Button'
import Presets from './Presets' import Presets from './Presets'
import { COLORS, DEFAULT_PRESETS } from '../lib/constants' import { COLORS, DEFAULT_PRESETS } from '../lib/constants'
import { toggle, getPresets, savePresets, generateId } from '../lib/util' import { toggle, getPresets, savePresets, generateId, fileToJSON } from '../lib/util'
import SettingsIcon from './svg/Settings' import SettingsIcon from './svg/Settings'
import * as Arrows from './svg/Arrows' import * as Arrows from './svg/Arrows'
@ -140,10 +140,42 @@ const TypeSettings = React.memo(
const resetButtonStyle = { borderTop: `1px solid ${COLORS.SECONDARY}` } const resetButtonStyle = { borderTop: `1px solid ${COLORS.SECONDARY}` }
const MiscSettings = React.memo(({ format, reset }) => { const MiscSettings = React.memo(({ format, reset, applyPreset, settings }) => {
const input = React.useRef(null)
let download
try {
download = `data:text/json;charset=utf-8,${encodeURIComponent(JSON.stringify(settings))}`
} catch (error) {
// pass
}
return ( return (
<div className="settings-content"> <div className="settings-content">
<Button center onClick={format}> <div className="row">
<input
hidden
ref={input}
type="file"
accept=".json"
onChange={async e => {
const json = await fileToJSON(e.target.files[0])
if (json) {
applyPreset(json)
}
}}
/>
<Button
width="50%"
center
style={{ borderRight: `1px solid ${COLORS.SECONDARY}` }}
onClick={() => input.current.click()}
>
Import config
</Button>
<Button center Component="a" href={download} download="carbon-config.json">
Export config
</Button>
</div>
<Button center onClick={format} style={resetButtonStyle}>
Prettify code Prettify code
</Button> </Button>
<Button center color={COLORS.RED} onClick={reset} style={resetButtonStyle}> <Button center color={COLORS.RED} onClick={reset} style={resetButtonStyle}>
@ -151,10 +183,19 @@ const MiscSettings = React.memo(({ format, reset }) => {
</Button> </Button>
<style jsx> <style jsx>
{` {`
.row {
display: flex;
flex: 1;
}
.settings-content { .settings-content {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
} }
.settings-content :global(a) {
display: flex;
flex: 1;
user-drag: none;
}
`} `}
</style> </style>
</div> </div>
@ -340,8 +381,17 @@ class Settings extends React.PureComponent {
lineHeight={this.props.lineHeight} lineHeight={this.props.lineHeight}
/> />
) )
case 'Misc': case 'Misc': {
return <MiscSettings format={this.props.format} reset={this.handleReset} /> const settings = this.getSettingsFromProps()
return (
<MiscSettings
format={this.props.format}
reset={this.handleReset}
applyPreset={this.props.applyPreset}
settings={settings}
/>
)
}
default: default:
return null return null
} }

@ -68,6 +68,13 @@ export const fileToDataURL = blob =>
reader.readAsDataURL(blob) reader.readAsDataURL(blob)
}) })
export const fileToJSON = blob =>
new Promise(res => {
const reader = new FileReader()
reader.onload = e => res(parse(e.target.result))
reader.readAsText(blob)
})
export const formatCode = async code => { export const formatCode = async code => {
const prettier = await import('prettier/standalone') const prettier = await import('prettier/standalone')
const babylonParser = await import('prettier/parser-babylon') const babylonParser = await import('prettier/parser-babylon')

Loading…
Cancel
Save