Make carbon a stateless component, move highlight.js calc into render (#479)

* make carbon a stateless component, move highlight.js calc into render

* remove unneeded class function

* inline handleLanguageChange
main
Michael Fix 6 years ago committed by GitHub
parent 09fa737ca1
commit fa983f6fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,34 +9,7 @@ import WindowControls from '../components/WindowControls'
import Watermark from '../components/svg/Watermark' import Watermark from '../components/svg/Watermark'
import { COLORS, LANGUAGE_MODE_HASH, LANGUAGE_NAME_HASH, DEFAULT_SETTINGS } from '../lib/constants' import { COLORS, LANGUAGE_MODE_HASH, LANGUAGE_NAME_HASH, DEFAULT_SETTINGS } from '../lib/constants'
const handleLanguageChange = (newCode, props) => {
if (props.config.language === 'auto') {
// try to set the language
const detectedLanguage = hljs.highlightAuto(newCode).language
const languageMode =
LANGUAGE_MODE_HASH[detectedLanguage] || LANGUAGE_NAME_HASH[detectedLanguage]
if (languageMode) {
return { language: languageMode.mime || languageMode.mode }
}
}
return { language: props.config.language }
}
class Carbon extends React.PureComponent { class Carbon extends React.PureComponent {
constructor(props) {
super(props)
this.state = {
...handleLanguageChange(this.props.children, this.props)
}
this.handleTitleBarChange = this.handleTitleBarChange.bind(this)
this.codeUpdated = this.codeUpdated.bind(this)
this.onBeforeChange = this.onBeforeChange.bind(this)
this.handleLanguageChange = this.handleLanguageChange.bind(this)
}
componentDidMount() { componentDidMount() {
const ro = new ResizeObserver(entries => { const ro = new ResizeObserver(entries => {
const cr = entries[0].contentRect const cr = entries[0].contentRect
@ -45,30 +18,38 @@ class Carbon extends React.PureComponent {
ro.observe(this.exportContainerNode) ro.observe(this.exportContainerNode)
} }
codeUpdated(newCode) { handleLanguageChange = debounce(
this.handleLanguageChange(newCode, this.props) (newCode, language) => {
this.props.updateCode(newCode) if (language === 'auto') {
} // try to set the language
const detectedLanguage = hljs.highlightAuto(newCode).language
const languageMode =
LANGUAGE_MODE_HASH[detectedLanguage] || LANGUAGE_NAME_HASH[detectedLanguage]
handleTitleBarChange(newTitle) { if (languageMode) {
this.props.updateTitleBar(newTitle) return languageMode.mime || languageMode.mode
}
} }
handleLanguageChange = debounce( return language
(...args) => this.setState(handleLanguageChange(...args)), },
ms('300ms'), ms('300ms'),
{ trailing: true } {
leading: true,
trailing: true
}
) )
onBeforeChange(editor, meta, code) { onBeforeChange = (editor, meta, code) => this.props.updateCode(code)
return this.codeUpdated(code)
}
render() { render() {
const config = { ...DEFAULT_SETTINGS, ...this.props.config } const config = { ...DEFAULT_SETTINGS, ...this.props.config }
const languageMode = this.handleLanguageChange(this.props.children, config.language)
const options = { const options = {
lineNumbers: config.lineNumbers, lineNumbers: config.lineNumbers,
mode: this.state.language || 'plaintext', mode: languageMode || 'plaintext',
theme: config.theme, theme: config.theme,
scrollBarStyle: null, scrollBarStyle: null,
viewportMargin: Infinity, viewportMargin: Infinity,
@ -87,7 +68,7 @@ class Carbon extends React.PureComponent {
<WindowControls <WindowControls
titleBar={this.props.titleBar} titleBar={this.props.titleBar}
theme={config.windowTheme} theme={config.windowTheme}
handleTitleBarChange={this.handleTitleBarChange} handleTitleBarChange={this.props.updateTitleBar}
/> />
) : null} ) : null}
<CodeMirror <CodeMirror

Loading…
Cancel
Save