From e0a650b02122340b036c08618f9a1568d1147f31 Mon Sep 17 00:00:00 2001 From: raboid Date: Tue, 16 Apr 2019 12:33:07 -0400 Subject: [PATCH] close settings dialog on font upload --- components/Editor.js | 7 ++++++- components/FontSelect.js | 11 +++-------- components/Settings.js | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/components/Editor.js b/components/Editor.js index 74999ce..b21e9d7 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -26,7 +26,8 @@ import { DEFAULT_SETTINGS, DEFAULT_LANGUAGE, DEFAULT_PRESET_ID, - DEFAULT_THEME + DEFAULT_THEME, + FONTS } from '../lib/constants' import { serializeState, getRouteState } from '../lib/routing' import { getSettings, unescapeHtml, formatCode, omit } from '../lib/util' @@ -86,6 +87,10 @@ class Editor extends React.Component { newState.language = unescapeHtml(newState.language) } + if (newState.fontFamily && !FONTS.find(({ id }) => id === newState.fontFamily)) { + newState.fontFamily = DEFAULT_SETTINGS.fontFamily + } + this.updateState(newState) this.isSafari = diff --git a/components/FontSelect.js b/components/FontSelect.js index c2a4fb6..b54804a 100644 --- a/components/FontSelect.js +++ b/components/FontSelect.js @@ -13,15 +13,12 @@ const Font = ({ id, name }) => ( function FontSelect(props) { const inputEl = React.useRef(null) - const [fonts, updateFonts] = React.useState(FONTS) function onChange(id) { if (id === 'upload') { inputEl.current.click() } else { - props.onChange('fontFamily', id) - const font = fonts.find(f => f.id === id) - props.onChange('fontUrl', font.url || null) + props.onChange(id) } } @@ -31,16 +28,14 @@ function FontSelect(props) { const name = files[0].name.split('.')[0] const url = await blobToUrl(files[0]) - updateFonts(fonts => [{ id: name, name, url }, ...fonts]) - props.onChange('fontFamily', name) - props.onChange('fontUrl', url) + props.onUpload(name, url) } return (
diff --git a/components/Settings.js b/components/Settings.js index 98446b4..4b2fb94 100644 --- a/components/Settings.js +++ b/components/Settings.js @@ -106,10 +106,14 @@ const WindowSettings = React.memo( ) const TypeSettings = React.memo( - ({ onChange, font, size, lineHeight, onWidthChanging, onWidthChanged }) => { + ({ onChange, onUpload, font, size, lineHeight, onWidthChanging, onWidthChanged }) => { return (
- + { + this.props.onChange('fontFamily', id) + this.props.onChange('fontUrl', url) + this.props.toggleVisibility() + } + getSettingsFromProps = () => omitBy(this.props, (v, k) => typeof v === 'function' || k === 'preset') @@ -321,6 +331,7 @@ class Settings extends React.PureComponent { return (