From d4d0ef9949b7eacebeeb0df36321b0a8a4a3f7b2 Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 29 Mar 2019 23:43:23 -0400 Subject: [PATCH] update gist handling (#715) * update gist handling * address comments * clean up a bit * getRouteState(router) * move try/catch into getGist * updateQueryString -> updateRouteState * clean up more * add cypress tests * delete comments * remove wait --- components/Editor.js | 32 +++++++++++++-------- components/GistContainer.js | 40 -------------------------- cypress/integration/gist.js | 18 ++++++++++++ lib/api.js | 51 +++++++++++++++++++-------------- lib/routing.js | 56 ++++++++++++++++++++++++------------- pages/embed.js | 29 ++++++++++--------- pages/index.js | 4 +-- 7 files changed, 123 insertions(+), 107 deletions(-) delete mode 100644 components/GistContainer.js diff --git a/components/Editor.js b/components/Editor.js index c5e85b1..cd6f4a0 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -1,5 +1,4 @@ // Theirs -import url from 'url' import React from 'react' import domtoimage from 'dom-to-image' import dynamic from 'next/dynamic' @@ -15,7 +14,6 @@ import Carbon from './Carbon' import ExportMenu from './ExportMenu' import Themes from './Themes' import TweetButton from './TweetButton' -import GistContainer from './GistContainer' import { LANGUAGES, LANGUAGE_MIME_HASH, @@ -29,7 +27,7 @@ import { DEFAULT_LANGUAGE, DEFAULT_PRESET_ID } from '../lib/constants' -import { serializeState, getQueryStringState } from '../lib/routing' +import { serializeState, getRouteState } from '../lib/routing' import { getSettings, unescapeHtml, formatCode, omit } from '../lib/util' import LanguageIcon from './svg/Language' @@ -41,6 +39,7 @@ const BackgroundSelect = dynamic(() => import('./BackgroundSelect'), { class Editor extends React.Component { static contextType = ApiContext + constructor(props) { super(props) this.state = { @@ -61,15 +60,24 @@ class Editor extends React.Component { } async componentDidMount() { - const { asPath = '' } = this.props.router - const { query } = url.parse(asPath, true) - const initialState = getQueryStringState(query) + const { queryState, parameter } = getRouteState(this.props.router) + + // TODO we could create an interface for loading this config, so that it looks identical + // whether config is loaded from localStorage, gist, or even something like IndexDB + let gistState + if (this.context.gist && parameter) { + const { config, ...gist } = (await this.context.gist.get(parameter)) || {} + if (typeof config === 'object') { + this.gist = gist + gistState = config + } + } const newState = { - // Load from localStorage - ...getSettings(localStorage), + // Load options from gist or localStorage + ...(gistState ? gistState : getSettings(localStorage)), // and then URL params - ...initialState, + ...queryState, loading: false } @@ -88,9 +96,11 @@ class Editor extends React.Component { carbonNode = React.createRef() - updateState = updates => this.setState(updates, () => this.props.onUpdate(this.state)) + updateState = updates => + this.setState(updates, () => !this.gist && this.props.onUpdate(this.state)) updateCode = code => this.updateState({ code }) + updateAspectRatio = aspectRatio => this.updateState({ aspectRatio }) async getCarbonImage( @@ -335,8 +345,6 @@ class Editor extends React.Component { )} - - this.setState(stateFromGist)} />