11/* @flow */
2- import React , { Component , Element , PropTypes } from 'react' ;
3- import shallowCompare from 'react-addons-shallow-compare' ;
2+ import React , { PureComponent , PropTypes } from 'react' ;
43import hljs from 'highlight.js' ;
54import cx from 'classnames' ;
65
@@ -16,7 +15,7 @@ type State = {
1615 language : ?string ,
1716} ;
1817
19- export default class Highlight extends Component {
18+ export default class Highlight extends PureComponent {
2019
2120 state : State = {
2221 highlightedCode : null ,
@@ -27,17 +26,13 @@ export default class Highlight extends Component {
2726 this . _highlightCode ( ) ;
2827 }
2928
30- shouldComponentUpdate ( nextProps : Props , nextState : State ) : boolean {
31- return shallowCompare ( this , nextProps , nextState ) ;
32- }
33-
3429 componentDidUpdate ( ) {
3530 this . _highlightCode ( ) ;
3631 }
3732
3833 props : Props ;
3934
40- get initialCode ( ) : string {
35+ getInitialCode ( ) {
4136 const type = typeof this . props . children ;
4237 if ( type !== 'string' ) {
4338 throw new Error ( `Children of <Highlight> must be a string. '${ type } ' supplied` ) ;
@@ -46,40 +41,40 @@ export default class Highlight extends Component {
4641 return this . props . children ;
4742 }
4843
49- get highlightCallback ( ) : ( resolve : Function ) => HighlightResult {
44+ getHighlightCallback ( ) {
5045 let callback ;
5146
5247 if ( this . props . languages && this . props . languages . length === 1 ) {
5348 const language :string = this . props . languages [ 0 ] ;
54- callback = ( resolve ) =>
55- resolve ( hljs . highlight ( language , this . initialCode ) ) ;
49+ callback = ( resolve : ( x : * ) => void ) =>
50+ resolve ( hljs . highlight ( language , this . getInitialCode ( ) ) ) ;
5651 } else {
57- callback = ( resolve ) =>
58- resolve ( hljs . highlightAuto ( this . initialCode , this . props . languages ) ) ;
52+ callback = ( resolve : ( x : * ) => void ) =>
53+ resolve ( hljs . highlightAuto ( this . getInitialCode ( ) , this . props . languages ) ) ;
5954 }
6055
6156 return callback ;
6257 }
6358
64- _highlightCode ( ) : void {
59+ _highlightCode ( ) {
6560 const worker = this . props . worker ;
6661 if ( worker ) {
6762 worker . onmessage = event => this . setState ( {
6863 highlightedCode : event . data . value ,
6964 language : event . data . language ,
7065 } ) ;
71- worker . postMessage ( { code : this . initialCode , languages : this . props . languages } ) ;
66+ worker . postMessage ( { code : this . getInitialCode ( ) , languages : this . props . languages } ) ;
7267 } else {
73- const promise = new Promise ( this . highlightCallback ) ;
68+ const promise = new Promise ( this . getHighlightCallback ( ) ) ;
7469
7570 promise . then (
7671 result => this . setState ( { highlightedCode : result . value , language : result . language } )
7772 ) ;
7873 }
7974 }
8075
81- render ( ) : ? Element {
82- const code : ? string = this . state . highlightedCode ;
76+ render ( ) {
77+ const code = this . state . highlightedCode ;
8378 const classes = cx ( this . props . className , 'hljs' , this . state . language ) ;
8479
8580 if ( code ) {
@@ -90,7 +85,7 @@ export default class Highlight extends Component {
9085 ) ;
9186 }
9287
93- return < pre > < code className = { classes } > { this . initialCode } </ code > </ pre > ;
88+ return < pre > < code className = { classes } > { this . getInitialCode ( ) } </ code > < / p r e > ;
9489 }
9590}
9691
0 commit comments