mirror of https://github.com/sgoudham/carbon.git
Refactor index.js (#310)
* Move Editor to components * Separate index and editor a little * Revert dynamic import of hljs b/c it's critical * Move query param update to index.js * Clean up editor furthermain
parent
d6a31941d5
commit
5691236934
@ -1,3 +1,54 @@
|
|||||||
import Editor from './editor'
|
// Theirs
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
export default Editor
|
// Ours
|
||||||
|
import Editor from '../components/Editor'
|
||||||
|
import Page from '../components/Page'
|
||||||
|
import api from '../lib/api'
|
||||||
|
import { getQueryStringState, updateQueryString } from '../lib/routing'
|
||||||
|
import { saveState } from '../lib/util'
|
||||||
|
|
||||||
|
class Index extends React.Component {
|
||||||
|
static async getInitialProps({ asPath, query }) {
|
||||||
|
const path = removeQueryString(asPath.split('/').pop())
|
||||||
|
const queryParams = getQueryStringState(query)
|
||||||
|
const initialState = Object.keys(queryParams).length ? queryParams : null
|
||||||
|
try {
|
||||||
|
// TODO fix this hack
|
||||||
|
if (path.length >= 19 && path.indexOf('.') === -1) {
|
||||||
|
const content = await api.getGist(path)
|
||||||
|
return { content, initialState }
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
|
return { initialState }
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<Page enableHeroText={true}>
|
||||||
|
<Editor {...this.props} onUpdate={onEditorUpdate} />
|
||||||
|
</Page>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEditorUpdate(state) {
|
||||||
|
updateQueryString(state)
|
||||||
|
const s = { ...state }
|
||||||
|
delete s.code
|
||||||
|
delete s.backgroundImage
|
||||||
|
delete s.backgroundImageSelection
|
||||||
|
saveState(localStorage, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeQueryString(str) {
|
||||||
|
const qI = str.indexOf('?')
|
||||||
|
return (qI >= 0 ? str.substr(0, qI) : str)
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/\//g, '/')
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Index
|
||||||
|
Loading…
Reference in New Issue