2016-03-08 09:38:09 -03:00
|
|
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
import $ from 'jquery';
|
2016-06-01 08:28:54 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
|
|
import * as SyntaxHighlighting from 'utils/syntax_hightlighting.jsx';
|
2016-03-08 09:38:09 -03:00
|
|
|
import Constants from 'utils/constants.jsx';
|
2016-06-01 08:28:54 -04:00
|
|
|
|
2016-03-08 09:38:09 -03:00
|
|
|
import FileInfoPreview from './file_info_preview.jsx';
|
|
|
|
|
|
2016-06-01 08:28:54 -04:00
|
|
|
import loadingGif from 'images/load.gif';
|
2016-03-08 09:38:09 -03:00
|
|
|
|
|
|
|
|
export default class CodePreview extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.updateStateFromProps = this.updateStateFromProps.bind(this);
|
|
|
|
|
this.handleReceivedError = this.handleReceivedError.bind(this);
|
|
|
|
|
this.handleReceivedCode = this.handleReceivedCode.bind(this);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
code: '',
|
|
|
|
|
lang: '',
|
|
|
|
|
loading: true,
|
|
|
|
|
success: true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.updateStateFromProps(this.props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if (this.props.fileUrl !== nextProps.fileUrl) {
|
|
|
|
|
this.updateStateFromProps(nextProps);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateStateFromProps(props) {
|
2016-06-01 08:28:54 -04:00
|
|
|
var usedLanguage = SyntaxHighlighting.getLang(props.filename);
|
2016-03-08 09:38:09 -03:00
|
|
|
|
|
|
|
|
if (!usedLanguage || props.fileInfo.size > Constants.CODE_PREVIEW_MAX_FILE_SIZE) {
|
|
|
|
|
this.setState({code: '', lang: '', loading: false, success: false});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState({code: '', lang: usedLanguage, loading: true});
|
|
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
async: true,
|
|
|
|
|
url: props.fileUrl,
|
|
|
|
|
type: 'GET',
|
|
|
|
|
error: this.handleReceivedError,
|
|
|
|
|
success: this.handleReceivedCode
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleReceivedCode(data) {
|
2016-06-01 08:28:54 -04:00
|
|
|
this.setState({code: data, loading: false, success: true});
|
2016-03-08 09:38:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleReceivedError() {
|
|
|
|
|
this.setState({loading: false, success: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static support(filename) {
|
2016-06-01 08:28:54 -04:00
|
|
|
return !!SyntaxHighlighting.getLanguageFromFilename(filename);
|
2016-03-08 09:38:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
if (this.state.loading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className='view-image__loading'>
|
|
|
|
|
<img
|
|
|
|
|
className='loader-image'
|
2016-06-01 08:28:54 -04:00
|
|
|
src={loadingGif}
|
2016-03-08 09:38:09 -03:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.state.success) {
|
|
|
|
|
return (
|
|
|
|
|
<FileInfoPreview
|
|
|
|
|
filename={this.props.filename}
|
|
|
|
|
fileUrl={this.props.fileUrl}
|
|
|
|
|
fileInfo={this.props.fileInfo}
|
|
|
|
|
formatMessage={this.props.formatMessage}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 08:28:54 -04:00
|
|
|
// add line numbers when viewing a code file preview
|
|
|
|
|
const lines = this.state.code.match(/\r\n|\r|\n|$/g).length;
|
|
|
|
|
let strlines = '';
|
|
|
|
|
for (let i = 1; i <= lines; i++) {
|
|
|
|
|
if (strlines) {
|
|
|
|
|
strlines += '\n' + i;
|
|
|
|
|
} else {
|
|
|
|
|
strlines += i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const language = SyntaxHighlighting.getLanguageName(this.state.lang);
|
|
|
|
|
|
|
|
|
|
const highlighted = SyntaxHighlighting.highlight(this.state.lang, this.state.code);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className='post-code'>
|
|
|
|
|
<span className='post-code__language'>
|
|
|
|
|
{`${this.props.filename} - ${language}`}
|
|
|
|
|
</span>
|
|
|
|
|
<code className='hljs'>
|
|
|
|
|
<table>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td className='post-code__lineno'>{strlines}</td>
|
|
|
|
|
<td dangerouslySetInnerHTML={{__html: highlighted}}/>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</code>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2016-03-08 09:38:09 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodePreview.propTypes = {
|
|
|
|
|
filename: React.PropTypes.string.isRequired,
|
|
|
|
|
fileUrl: React.PropTypes.string.isRequired,
|
|
|
|
|
fileInfo: React.PropTypes.object.isRequired,
|
|
|
|
|
formatMessage: React.PropTypes.func.isRequired
|
|
|
|
|
};
|