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

70 lines
1.8 KiB
React
Raw Normal View History

2018-07-27 02:24:00 +02:00
// @flow
2018-10-15 08:26:46 +02:00
import * as React from 'react';
2018-07-28 03:42:35 +02:00
import CodeMirror from 'codemirror/lib/codemirror';
2018-11-21 22:20:55 +01:00
import { openSnippetMenu, stopContextMenu } from 'util/context-menu';
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/go/go';
import 'codemirror/mode/jsx/jsx';
import 'codemirror/mode/css/css';
2018-07-28 03:42:35 +02:00
import 'codemirror/mode/xml/xml';
import 'codemirror/mode/php/php';
import 'codemirror/mode/ruby/ruby';
import 'codemirror/mode/clike/clike';
import 'codemirror/mode/shell/shell';
import 'codemirror/mode/python/python';
import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/javascript/javascript';
2018-07-27 02:24:00 +02:00
type Props = {
2018-07-28 03:42:35 +02:00
theme: string,
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: Props) {
2018-07-27 02:24:00 +02:00
super(props);
this.codeMirror = null;
}
componentDidMount() {
2018-07-28 03:42:35 +02:00
const { theme, contentType } = this.props;
2018-07-29 01:48:54 +02:00
// Init CodeMirror
2018-10-15 08:26:46 +02:00
this.codeMirror = CodeMirror.fromTextArea(this.textarea, {
2018-07-29 01:48:54 +02:00
// Auto detect syntax with file contentType
2018-07-28 03:42:35 +02:00
mode: contentType,
2018-07-29 01:48:54 +02:00
// Adaptive theme
2018-08-01 23:50:36 +02:00
theme: theme === 'dark' ? 'one-dark' : 'default',
2018-07-29 01:48:54 +02:00
// Hide the cursor
readOnly: true,
// Styled text selection
styleSelectedText: true,
// Additional config opts
2018-07-27 02:24:00 +02:00
dragDrop: false,
lineNumbers: true,
lineWrapping: true,
});
2018-07-29 01:48:54 +02:00
// Add events
this.codeMirror.on('contextmenu', openSnippetMenu);
2018-07-27 02:24:00 +02:00
}
2018-10-15 08:26:46 +02:00
textarea: ?HTMLTextAreaElement;
codeMirror: any;
2018-07-27 02:24:00 +02:00
render() {
const { value } = this.props;
return (
2018-07-29 01:48:54 +02:00
<div className="code-viewer" onContextMenu={stopContextMenu}>
2018-10-15 08:26:46 +02:00
<textarea ref={textarea => (this.textarea = textarea)} disabled value={value} />
2018-07-27 02:24:00 +02:00
</div>
);
}
}
export default CodeViewer;