-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (73 loc) · 2.27 KB
/
index.js
File metadata and controls
83 lines (73 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React from 'react';
import classNames from 'classnames';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { atomOneLight } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import './index.less';
const FONT_SIZE = 12;
const LINE_HEIGHT = 18;
const PADDING_TOP = 5;
//TODO: add select with several themes
//TODO: scrool to/highlight crumbed lines
//https://github.com/conorhastings/react-syntax-highlighter/blob/master/README.md
export default class extends React.Component {
fixScroll() {
const { dependenciesLines = [], crumbedLines = [], lineHeight } = this.props;
if (!this.codeRef || !this.codeRef.scrollTo) {
return;
}
const lines = dependenciesLines.length
? dependenciesLines
: crumbedLines.length
? crumbedLines
: null;
lines &&
this.codeRef.scrollTo(0, (lines[0][0] - 1) * (lineHeight || LINE_HEIGHT) + PADDING_TOP - 2);
}
componentDidUpdate(prevProps) {
this.fixScroll();
}
componentDidMount() {
this.fixScroll();
}
render() {
const {
code,
crumbedLines = [],
dependenciesLines = [],
limitedHeight,
fontSize
} = this.props;
// TODO: calc height for .Code based on dependenciesLines - it's not always need to be 300 px!!
return (
<div className={classNames('Code', { limitedHeight })} ref={el => (this.codeRef = el)}>
<SyntaxHighlighter
style={atomOneLight}
showLineNumbers={true}
wrapLines={true}
customStyle={{
fontSize: `${fontSize || FONT_SIZE}px`,
padding: `${PADDING_TOP}px 0px 5px 5px`
}}
lineProps={lineNumber => {
if (isMatchLineNumber(crumbedLines, lineNumber)) {
return { className: 'crumbedLine' };
}
if (isMatchLineNumber(dependenciesLines, lineNumber)) {
return { className: 'importedDependencyLine' };
}
return {};
}}
>
{code || ''}
</SyntaxHighlighter>
</div>
);
}
}
const isMatchLineNumber = (lines, lineNumber) =>
!!lines.find(lines => {
if (lines[0] === lineNumber && lines[1] === lineNumber) {
return true;
}
return lines[0] <= lineNumber && lines[1] >= lineNumber;
});