lbry-desktop/src/renderer/component/viewers/codeViewer.jsx

46 lines
1,022 B
React
Raw Normal View History

2018-07-27 02:24:00 +02:00
// @flow
import React from 'react';
2018-07-28 03:42:35 +02:00
import CodeMirror from 'codemirror/lib/codemirror';
// Syntax mode
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/xml/xml';
2018-07-27 02:24:00 +02:00
type Props = {
2018-07-28 03:42:35 +02:00
theme: string,
2018-07-27 02:24:00 +02:00
value: string,
2018-07-28 03:42:35 +02:00
contentType: string,
2018-07-27 02:24:00 +02:00
};
class CodeViewer extends React.PureComponent<Props> {
constructor(props) {
super(props);
this.codeMirror = null;
this.textarea = React.createRef();
}
componentDidMount() {
2018-07-28 03:42:35 +02:00
const { theme, contentType } = this.props;
2018-07-27 02:24:00 +02:00
this.codeMirror = CodeMirror.fromTextArea(this.textarea.current, {
2018-07-28 03:42:35 +02:00
mode: contentType,
theme: theme === 'dark' ? 'dark' : 'default',
readOnly: 'nocursor',
2018-07-27 02:24:00 +02:00
dragDrop: false,
lineNumbers: true,
lineWrapping: true,
});
}
render() {
const { value } = this.props;
return (
2018-07-28 03:42:35 +02:00
<div className="code-viewer">
<textarea ref={this.textarea} disabled value={value} />
2018-07-27 02:24:00 +02:00
</div>
);
}
}
export default CodeViewer;