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

51 lines
1.1 KiB
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';
2018-07-28 22:17:06 +02:00
// Addons
import 'codemirror/addon/selection/mark-selection';
2018-07-28 03:42:35 +02:00
// 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,
2018-07-28 22:17:06 +02:00
styleSelectedText: true,
2018-07-27 02:24:00 +02:00
});
}
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;