From e02bbc48328500104a3679dfbc4db93a9e538cf9 Mon Sep 17 00:00:00 2001 From: raboid Date: Wed, 10 Apr 2019 18:00:09 -0400 Subject: [PATCH] address comments --- components/Editor.js | 24 ++++++++++++++---- components/Themes/index.js | 51 ++++++++++++++------------------------ lib/routing.js | 13 +--------- 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/components/Editor.js b/components/Editor.js index 03c1cba..bcd60d8 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -96,11 +96,20 @@ class Editor extends React.Component { carbonNode = React.createRef() - updateState = updates => - this.setState(updates, () => !this.gist && this.props.onUpdate(this.state)) + updateState = updates => { + this.setState(updates, () => { + // eslint-disable-next-line no-unused-vars + const { highlights, ...state } = this.state + !this.gist && this.props.onUpdate(state) + }) + } updateCode = code => this.updateState({ code }) + updateTheme = theme => this.updateState({ theme }) + + updateHighlights = highlights => this.updateState({ highlights }) + async getCarbonImage( { format, @@ -112,8 +121,7 @@ class Editor extends React.Component { // if safari, get image from api const isPNG = format !== 'svg' if (this.context.image && this.isSafari && isPNG) { - const { highlights, ...state } = this.state - const encodedState = serializeState({ ...state, ...highlights }) + const encodedState = serializeState(this.state) return this.context.image(encodedState) } @@ -282,7 +290,13 @@ class Editor extends React.Component { return (
- + import('./ThemeCreate'), { loading: () => null @@ -58,14 +56,6 @@ class Themes extends React.PureComponent { dropdown = React.createRef() componentDidMount() { - const { queryState } = getRouteState(this.props.router) - - const queryHighlights = queryState - ? Object.keys(queryState) - .filter(key => HIGHLIGHT_KEYS.includes(key)) - .reduce((obj, key) => ({ ...obj, [key]: queryState[key] }), {}) - : {} - const storedThemes = getThemes(localStorage) || [] this.setState(({ themes }) => { @@ -75,34 +65,30 @@ class Themes extends React.PureComponent { this.selectedTheme = newThemes.find(({ id }) => id === this.props.theme) || DEFAULT_THEME - const highlights = { - ...this.selectedTheme.highlights, - ...queryHighlights + if (Object.keys(this.props.highlights).length === 0) { + this.props.updateHighlights(this.selectedTheme.highlights) } - this.props.onChange('highlights', highlights) - return { themes: newThemes, - highlights, name } }) } componentDidUpdate(prevProps) { - const { isVisible, theme, onChange } = this.props + const { isVisible, theme, updateHighlights } = this.props const { themes } = this.state if (prevProps.isVisible && !isVisible) { this.setState({ name: getCustomName(themes) }) - onChange('highlights', themes.find(({ id }) => id === theme).highlights) + updateHighlights(themes.find(({ id }) => id === theme).highlights) } } applyPreset = preset => { this.setState(({ themes }) => { - this.props.onChange('highlights', themes.find(({ id }) => id === preset).highlights) + this.props.updateHighlights(themes.find(({ id }) => id === preset).highlights) return { preset } @@ -110,15 +96,15 @@ class Themes extends React.PureComponent { } handleChange = ({ id }) => { - const { theme, toggleVisibility, onChange } = this.props + const { toggleVisibility, updateTheme, updateHighlights } = this.props const { themes } = this.state if (id === 'create') { toggleVisibility() this.dropdown.current.closeMenu() } else { - onChange('theme', id) - onChange('highlights', themes.find(({ id }) => id === theme).highlights) + updateTheme(id) + updateHighlights(themes.find(theme => theme.id === id).highlights) } } @@ -130,30 +116,31 @@ class Themes extends React.PureComponent { })) updateHighlight = ({ rgb }) => - this.props.onChange('highlights', { + this.props.updateHighlights({ ...this.props.highlights, [this.state.selected]: stringifyRGBA(rgb) }) removeTheme = id => event => { - event.stopPropagation() - const { themes } = this.state + const { theme, updateHighlights, updateTheme } = this.props + + event.stopPropagation() const newThemes = themes.filter(t => t.id !== id) saveThemes(localStorage, newThemes.filter(({ custom }) => custom)) - if (this.props.theme === id) { - this.props.onChange('theme', DEFAULT_THEME.id) - this.props.onChange('highlights', DEFAULT_THEME.highlights) + if (theme === id) { + updateTheme(DEFAULT_THEME.id) + updateHighlights(DEFAULT_THEME.highlights) } else { this.setState({ themes: newThemes }) } } createTheme = () => { - const { highlights } = this.props + const { highlights, updateTheme } = this.props const { themes, name } = this.state const id = `theme:${generateId()}` @@ -169,7 +156,7 @@ class Themes extends React.PureComponent { saveThemes(localStorage, customThemes) - this.props.onChange('theme', id) + updateTheme(id) } itemWrapper = props => @@ -290,4 +277,4 @@ class Themes extends React.PureComponent { } } -export default managePopout(withRouter(Themes)) +export default managePopout(Themes) diff --git a/lib/routing.js b/lib/routing.js index 5b7022d..c9f8dba 100644 --- a/lib/routing.js +++ b/lib/routing.js @@ -36,18 +36,7 @@ const mappings = [ { field: 'copy', type: 'bool' }, { field: 'readonly', type: 'bool' }, { field: 'id' }, - { field: 'text' }, - { field: 'meta' }, - { field: 'tbg:background' }, - { field: 'var:variable' }, - { field: 'attr:attribute' }, - { field: 'def:definition' }, - { field: 'kw:keyword' }, - { field: 'op:operator' }, - { field: 'prop:property' }, - { field: 'num:number' }, - { field: 'str:string' }, - { field: 'cmt:comment' } + { field: 'highlights' } ] const reverseMappings = mappings.map(mapping =>