Skip to content

Commit 5a6aed2

Browse files
committed
build view sourcecode module
1 parent 6b83da8 commit 5a6aed2

22 files changed

Lines changed: 391 additions & 208 deletions

‎.eslintrc.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ module.exports = {
2222
'Promise': true,
2323
'd3': true,
2424
'NProgress': true,
25-
'ClipboardJS': true
25+
'ClipboardJS': true,
26+
'PR': true,
27+
'Mark': true
2628
},
2729
"parser": "babel-eslint",
2830
'parserOptions': {

‎lib.config.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
exports.js = [
44
'./node_modules/react/umd/react.production.min.js',
55
'./node_modules/react-dom/umd/react-dom.production.min.js',
6-
'./node_modules/clipboard/dist/clipboard.min.js'
6+
'./node_modules/clipboard/dist/clipboard.min.js',
7+
'./src/vendors/prettify.js',
8+
'./node_modules/mark.js/dist/mark.min.js'
79
];
810

911
exports.css = [
1012
'./node_modules/semantic-ui-css/semantic.min.css',
11-
'./node_modules/animate.css/animate.min.css'
13+
'./node_modules/animate.css/animate.min.css',
14+
'./src/vendors/prettify.css'
1215
];
1316

1417
exports.extra = [{

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@
6464
"dependencies": {
6565
"@babel/polyfill": "^7.2.3",
6666
"animate.css": "^3.7.0",
67+
"clipboard": "^2.0.4",
6768
"events": "^3.0.0",
6869
"file-saver": "^2.0.0",
6970
"lodash": "^4.17.11",
71+
"mark.js": "^8.11.1",
7072
"nprogress": "^0.2.0",
7173
"react": "^16.7.0",
7274
"react-dom": "^16.7.0",
7375
"react-router-dom": "^4.3.1",
7476
"semantic-ui-css": "^2.4.1",
7577
"semantic-ui-react": "^0.82.5",
76-
"clipboard": "^2.0.4",
7778
"whatwg-fetch": "^3.0.0"
7879
}
7980
}

‎src/components/SourceCode.js‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from 'react';
2+
import {Modal, Button, Dropdown, Label} from 'semantic-ui-react';
3+
import * as Tools from '../utils/Tools';
4+
import Loading from "./Loading";
5+
6+
export default class SourceCode extends React.Component {
7+
code = React.createRef();
8+
mark = null;
9+
visible = false;
10+
constructor(props) {
11+
super(props);
12+
}
13+
14+
componentDidUpdate(prevProps, prevState, snapshot) {
15+
if (prevProps.sourceCode != this.props.sourceCode || (!this.visiable && this.props.sourceCodeVisible)) {
16+
this.renderPrettyPrint();
17+
this.visible = true;
18+
}
19+
}
20+
21+
componentDidMount() {
22+
this.renderPrettyPrint();
23+
}
24+
25+
handleClose = () => {
26+
this.visiable = false;
27+
this.props.onCloseSourceCode();
28+
}
29+
renderPrettyPrint = () => {
30+
setTimeout(() => {
31+
if (this.code.current) {
32+
this.code.current.classList.remove('prettyprinted');
33+
setTimeout(() => PR.prettyPrint(
34+
() => setTimeout(() =>this.renderHighLight(), 1000)
35+
), 100);
36+
}
37+
}, this.code.current ? 0 : 1000);
38+
}
39+
40+
renderHighLight = () => {
41+
if (this.mark) {this.mark.unmark()}
42+
this.mark = new Mark(this.code.current);
43+
let idx = 0;
44+
this.mark.mark(this.props.sourceCodeVariable.keyword, {each: el => {
45+
el.setAttribute('tabindex',idx++);
46+
}});
47+
}
48+
49+
renderDropdownItem() {
50+
return this.props.sourceCodeVariable.repoList.map(repo => {
51+
return (
52+
<Dropdown.Item key={Tools.uuid()}>
53+
<Button size='mini' onClick={() => this.props.onRequestSourceCode(repo)}>Codes</Button>
54+
<Button size='mini' as='a' href={repo.repo} target='_blank'>Repo</Button>
55+
<Label size='mini' circular color={Tools.randomLabelColor()}>{repo.language}</Label>
56+
</Dropdown.Item>
57+
)
58+
});
59+
}
60+
61+
render() {
62+
if (!this.props.sourceCodeVariable) { return null; }
63+
const sourceCodeVariable = this.props.sourceCodeVariable;
64+
return (
65+
<Modal open={this.props.sourceCodeVisible} onClose={this.handleClose}
66+
centered={false} closeIcon className='source-code modal--fix'>
67+
<Modal.Header>
68+
<Dropdown floating labeled button blurring className='mini icon'
69+
text={`Matched results (${sourceCodeVariable.repoList.length})`}>
70+
<Dropdown.Menu>
71+
<Dropdown.Menu scrolling>
72+
{this.renderDropdownItem()}
73+
</Dropdown.Menu>
74+
</Dropdown.Menu>
75+
</Dropdown>
76+
<Button size='mini' as='a' href={this.props.sourceCodeRepo.repo} target='_blank'>Repo</Button>
77+
</Modal.Header>
78+
<Modal.Content>
79+
{this.props.requestingSourceCode ? <Loading/> : ''}
80+
<pre><code className='prettyprint linenums' ref={this.code}>{this.props.sourceCode}</code></pre>
81+
</Modal.Content>
82+
</Modal>
83+
)
84+
}
85+
}

‎src/components/Sourcecode.js‎

Lines changed: 0 additions & 71 deletions
This file was deleted.

‎src/components/VariableList.js‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import React from 'react';
22
import {Button, Label, Popup} from 'semantic-ui-react';
33
import * as Tools from '../utils/Tools';
44

5-
const formatRepobUrl = url => {
6-
return url.replace('git://github.com', 'https://github.com');
7-
}
8-
95
class Variable extends React.Component {
106
clipboardId = `clipboardId-${Tools.uuid()}`;
117
clipboard = null;
@@ -23,22 +19,23 @@ class Variable extends React.Component {
2319
}
2420

2521
render() {
22+
const variable = this.props.variable;
2623
return (
2724
<Popup style={{padding: '0'}}
2825
position='top center'
2926
trigger={
30-
<Label circular color={this.props.color} className={this.props.className} style={this.props.style}>
31-
{this.props.keyword}
27+
<Label circular color={variable.color} className={this.props.className} style={this.props.style}>
28+
{variable.keyword}
3229
</Label>}
3330
onMount={this.handlePopOnMount}
3431
onUnmount={this.handlePopUnmount}
3532
hoverable={true}>
3633
<Button.Group vertical basic style={{border: 0}}>
37-
<Button compact as='a' href={`#${this.props.keyword}`}>Search</Button>
38-
<Button compact as='a' href={formatRepobUrl(this.props.repo.repo)} target='_blank'>Repo</Button>
39-
<Button compact data-clipboard-text={this.props.keyword} id={this.clipboardId}>Copy</Button>
40-
<Button compact>
41-
Codes <Label size='mini' circular color={this.props.color}>{this.props.repoLen}</Label>
34+
<Button compact as='a' href={`#${variable.keyword}`}>Search</Button>
35+
<Button compact as='a' href={variable.repoLink} target='_blank'>Repo</Button>
36+
<Button compact data-clipboard-text={variable.keyword} id={this.clipboardId}>Copy</Button>
37+
<Button compact onClick={() => this.props.onOpenSourceCode(variable)}>
38+
Codes <Label size='mini' circular color={variable.color}>{variable.repoList.length}</Label>
4239
</Button>
4340
</Button.Group>
4441
</Popup>
@@ -68,10 +65,11 @@ export default class VariableList extends React.Component {
6865
style = {
6966
animationName: this.animationName,
7067
animationDelay: duration + 's',
71-
animationDuration: duration + Math.random() + 's'
68+
animationDuration: Math.min(duration, 0.8) + Math.random() + 's'
7269
};
7370
}
74-
return <Variable key={Tools.uuid()} {...variable} {...this.props} style={style} className={className}/>
71+
return <Variable key={Tools.uuid()} variable={variable}
72+
onOpenSourceCode={this.props.onOpenSourceCode} style={style} className={className}/>
7573
});
7674
if (variables && variables.length) {
7775
if (pages.length) {

0 commit comments

Comments
 (0)