Merge pull request #12 from dawnlabs/fixes

Fixes edit bugs
main
Michael Fix 7 years ago committed by GitHub
commit 4ef01c26b0

@ -4,8 +4,11 @@ import React from 'react'
import domtoimage from 'dom-to-image' import domtoimage from 'dom-to-image'
import CodeMirror from 'react-codemirror' import CodeMirror from 'react-codemirror'
import Spinner from 'react-spinner' import Spinner from 'react-spinner'
import toHash from 'tohash'
import WindowControls from '../components/WindowControls' import WindowControls from '../components/WindowControls'
import { COLORS, DEFAULT_LANGUAGE, LANGUAGE_HASH } from '../lib/constants' import { COLORS, DEFAULT_LANGUAGE, LANGUAGES } from '../lib/constants'
const LANGUAGE_HASH = toHash(LANGUAGES, 'module')
const DEFAULT_SETTINGS = { const DEFAULT_SETTINGS = {
paddingVertical: '50px', paddingVertical: '50px',
@ -23,10 +26,11 @@ class Carbon extends React.Component {
this.state = { this.state = {
loading: true, loading: true,
language: props.config.language language: props.config.language,
} }
this.handleLanguageChange = this.handleLanguageChange.bind(this) this.handleLanguageChange = this.handleLanguageChange.bind(this)
this.codeUpdated = this.codeUpdated.bind(this)
} }
componentDidMount() { componentDidMount() {
@ -41,16 +45,21 @@ class Carbon extends React.Component {
this.handleLanguageChange(newProps.children, { customProps: newProps }) this.handleLanguageChange(newProps.children, { customProps: newProps })
} }
codeUpdated (newCode) {
this.handleLanguageChange(newCode)
this.props.updateCode(newCode)
}
handleLanguageChange(newCode, config) { handleLanguageChange(newCode, config) {
const props = (config && config.customProps) || this.props const props = (config && config.customProps) || this.props
if (props.config.language.name === 'Auto') { if (props.config.language === 'auto') {
// try to set the language // try to set the language
const detectedLanguage = hljs.highlightAuto(newCode).language const detectedLanguage = hljs.highlightAuto(newCode).language
const languageModule = LANGUAGE_HASH[detectedLanguage] const languageModule = LANGUAGE_HASH[detectedLanguage]
if (languageModule) { if (languageModule) {
this.setState({ language: languageModule }) this.setState({ language: languageModule.module })
} }
} else { } else {
this.setState({ language: props.config.language }) this.setState({ language: props.config.language })
@ -62,7 +71,7 @@ class Carbon extends React.Component {
const options = { const options = {
lineNumbers: false, lineNumbers: false,
mode: this.state.language ? this.state.language.module : 'plaintext', mode: this.state.language || 'plaintext',
theme: config.theme, theme: config.theme,
scrollBarStyle: null, scrollBarStyle: null,
viewportMargin: Infinity, viewportMargin: Infinity,
@ -93,7 +102,7 @@ class Carbon extends React.Component {
{ config.windowControls ? <WindowControls theme={config.windowTheme} /> : null } { config.windowControls ? <WindowControls theme={config.windowTheme} /> : null }
<CodeMirror <CodeMirror
className={`CodeMirror__container window-theme__${config.windowTheme} ${config.dropShadow ? 'dropshadow' : ''}`} className={`CodeMirror__container window-theme__${config.windowTheme} ${config.dropShadow ? 'dropshadow' : ''}`}
onChange={this.handleLanguageChange} onChange={this.codeUpdated}
value={this.props.children} value={this.props.children}
options={options} options={options}
/> />

@ -233,9 +233,7 @@ export const LANGUAGES = [
} }
] ]
export const LANGUAGE_HASH = toHash(LANGUAGES) export const DEFAULT_LANGUAGE = 'auto'
export const DEFAULT_LANGUAGE = { name: 'Auto' }
export const COLORS = { export const COLORS = {
BLACK: '#121212', BLACK: '#121212',
@ -263,13 +261,3 @@ if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
} }
}) })
} }
function toHash (list) {
return list.reduce((accum, item) => {
if (item.module) {
accum[item.module] = item
}
return accum
}, {})
}

@ -28,12 +28,13 @@
"react-spinkit": "^3.0.0", "react-spinkit": "^3.0.0",
"react-spinner": "^0.2.7", "react-spinner": "^0.2.7",
"react-syntax-highlight": "^0.0.6", "react-syntax-highlight": "^0.0.6",
"tohash": "^1.0.0",
"twitter": "^1.7.1" "twitter": "^1.7.1"
}, },
"now": { "now": {
"env": { "env": {
"TWITTER_CONSUMER_KEY": "@twitter-consumer-key", "TWITTER_CONSUMER_KEY": "@twitter-consumer-key",
"TWITTER_CONSUMER_SECRET":"@twitter-consumer-secret", "TWITTER_CONSUMER_SECRET": "@twitter-consumer-secret",
"TWITTER_ACCESS_TOKEN_KEY": "@twitter-access-token-key", "TWITTER_ACCESS_TOKEN_KEY": "@twitter-access-token-key",
"TWITTER_ACCESS_TOKEN_SECRET": "@twitter-access-token-secret" "TWITTER_ACCESS_TOKEN_SECRET": "@twitter-access-token-secret"
} }

@ -8,7 +8,7 @@ import ReadFileDropContainer from '../components/ReadFileDropContainer'
import Toolbar from '../components/Toolbar' import Toolbar from '../components/Toolbar'
import Carbon from '../components/Carbon' import Carbon from '../components/Carbon'
import api from '../lib/api' import api from '../lib/api'
import { THEMES, LANGUAGES, DEFAULT_LANGUAGE, COLORS, DEFAULT_CODE } from '../lib/constants' import { THEMES, DEFAULT_LANGUAGE, COLORS, DEFAULT_CODE } from '../lib/constants'
class Editor extends React.Component { class Editor extends React.Component {
/* pathname, asPath, err, req, res */ /* pathname, asPath, err, req, res */
@ -24,8 +24,8 @@ class Editor extends React.Component {
return {} return {}
} }
constructor() { constructor(props) {
super() super(props)
this.state = { this.state = {
background: '#ABB8C3', background: '#ABB8C3',
theme: THEMES[0].id, theme: THEMES[0].id,
@ -34,11 +34,13 @@ class Editor extends React.Component {
windowControls: true, windowControls: true,
paddingVertical: '48px', paddingVertical: '48px',
paddingHorizontal: '32px', paddingHorizontal: '32px',
uploading: false uploading: false,
code: props.content || DEFAULT_CODE
} }
this.save = this.save.bind(this) this.save = this.save.bind(this)
this.upload = this.upload.bind(this) this.upload = this.upload.bind(this)
this.updateCode = this.updateCode.bind(this)
} }
getCarbonImage () { getCarbonImage () {
@ -56,6 +58,10 @@ class Editor extends React.Component {
return domtoimage.toPng(node, config) return domtoimage.toPng(node, config)
} }
updateCode (code) {
this.setState({ code })
}
save () { save () {
this.getCarbonImage() this.getCarbonImage()
.then((dataUrl) => { .then((dataUrl) => {
@ -81,7 +87,7 @@ class Editor extends React.Component {
return ( return (
<Page enableHeroText> <Page enableHeroText>
<ReadFileDropContainer <ReadFileDropContainer
onDrop={droppedContent => this.setState({ droppedContent })} onDrop={droppedContent => this.setState({ code: droppedContent })}
> >
<div id="editor"> <div id="editor">
<Toolbar <Toolbar
@ -95,8 +101,8 @@ class Editor extends React.Component {
bg={this.state.background} bg={this.state.background}
enabled={this.state} enabled={this.state}
/> />
<Carbon config={this.state}> <Carbon config={this.state} updateCode={this.updateCode}>
{this.state.droppedContent || this.props.content || DEFAULT_CODE} {this.state.code}
</Carbon> </Carbon>
</div> </div>
</ReadFileDropContainer> </ReadFileDropContainer>

@ -3526,6 +3526,10 @@ to-fast-properties@^1.0.1:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47"
tohash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/tohash/-/tohash-1.0.0.tgz#cd740231ca16821e3744f2f18ff9727382eeb091"
touch@3.1.0: touch@3.1.0:
version "3.1.0" version "3.1.0"
resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.0.tgz#fe365f5f75ec9ed4e56825e0bb76d24ab74af83b" resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.0.tgz#fe365f5f75ec9ed4e56825e0bb76d24ab74af83b"

Loading…
Cancel
Save