Skip to content

Commit 213ea86

Browse files
committed
build variable list
1 parent e4732dd commit 213ea86

21 files changed

Lines changed: 464 additions & 1018 deletions

‎.eslintrc.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
],
66
'rules': {
77
'no-unused-vars': [2, {'vars': 'all', 'args': 'none'}],
8+
'no-empty': [0],
89
'react/prop-types': [0],
910
'react/no-unescaped-entities': [0]
1011
},
@@ -18,7 +19,8 @@ module.exports = {
1819
'ReactDOM': true,
1920
'Promise': true,
2021
'd3': true,
21-
'NProgress': true
22+
'NProgress': true,
23+
'ClipboardJS': true
2224
},
2325
"parser": "babel-eslint",
2426
'parserOptions': {

‎lib.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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'
67
];
78

89
exports.css = [

‎package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@
6464
"dependencies": {
6565
"@babel/polyfill": "^7.0.0",
6666
"animate.css": "^3.7.0",
67+
"clipboard": "^2.0.4",
6768
"events": "^3.0.0",
6869
"react": "^16.5.2",
6970
"react-dom": "^16.5.2",
7071
"react-router-dom": "^4.3.1",
7172
"semantic-ui-css": "^2.4.1",
72-
"semantic-ui-react": "^0.82.5"
73+
"semantic-ui-react": "^0.82.5",
74+
"whatwg-fetch": "^3.0.0"
7375
}
7476
}

‎src/App.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'whatwg-fetch';
12
import ReactDOM from 'react-dom';
23
import MainContainer from './containers/MainContainer';
34

‎src/components/SearchBar.js‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ export default class SearchBar extends React.Component {
88
constructor(props) {
99
super(props);
1010
this.state = {
11-
type: 'title',
12-
prevProps: props
11+
lang: null,
12+
prevProps: props,
13+
inputSize: 'huge'
1314
}
15+
window.addEventListener('resize', this.resizeInput, false)
1416
}
1517

1618
static getDerivedStateFromProps(nextProps, prevState) {
@@ -20,17 +22,22 @@ export default class SearchBar extends React.Component {
2022
prevProps: nextProps // prevProps memoization
2123
}
2224
// derived state from props
23-
if (prevState.prevProps.searchType != nextProps.searchType) {
24-
newState.type = nextProps.searchType;
25+
if (prevState.prevProps.searchLang != nextProps.searchLang) {
26+
newState.lang = nextProps.searchLang;
2527
}
2628
return newState;
2729
}
2830

31+
componentDidMount() {
32+
this.resizeInput();
33+
}
34+
35+
resizeInput = () => {
36+
this.setState({inputSize: document.body.offsetWidth < 800 ? '' : 'huge'})
37+
}
38+
2939
handleSearch = () => {
30-
this.props.onSearch({
31-
type: this.state.type,
32-
value: this.input.current.inputRef.value
33-
});
40+
this.props.onSearch(this.input.current.inputRef.value, this.state.lang);
3441
}
3542

3643
handleTypeChange = (e, { value }) => this.setState({ type: value });
@@ -41,8 +48,8 @@ export default class SearchBar extends React.Component {
4148
<div className='search-bar__desc'>
4249
Search over GitHub, Bitbucket, GitLab to find real-world usage variable names
4350
</div>
44-
<Input className='search-bar__input'
45-
icon fluid placeholder={this.props.placeholder} size={document.body.offsetWidth > 800 ? 'huge' : ''}>
51+
<Input ref={this.input} className='search-bar__input'
52+
icon fluid placeholder={this.props.placeholder} size={this.state.inputSize}>
4653
<Dropdown text='' icon='filter' className='search-bar__dropdown'>
4754
<Dropdown.Menu>
4855
<Dropdown.Item icon='undo' text='All 90 Languages (Reset)' />

‎src/components/VariableList.js‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
import React from 'react';
2-
import {Dropdown, Icon, Input } from 'semantic-ui-react';
2+
import {Popup, Label, Button} from 'semantic-ui-react';
3+
import * as Tools from '../utils/Tools';
4+
5+
const formatRepobUrl = url => {
6+
return url.replace('git://github.com', 'https://github.com');
7+
}
8+
9+
class Variable extends React.Component {
10+
clipboardId = `clipboardId-${Tools.uuid()}`;
11+
clipboard = null;
12+
13+
constructor(props) {
14+
super(props);
15+
}
16+
17+
handlePopOnMount = () => {
18+
this.clipboard = new ClipboardJS(`#${this.clipboardId}`);
19+
}
20+
21+
handlePopUnmount = () => {
22+
this.clipboard && this.clipboard.destroy();
23+
}
24+
25+
render() {
26+
return (
27+
<Popup style={{padding: '0'}}
28+
position='top center'
29+
trigger={
30+
<Label circular color={this.props.color}>
31+
{this.props.keyword}
32+
</Label>}
33+
onMount={this.handlePopOnMount}
34+
onUnmount={this.handlePopUnmount}
35+
hoverable={true}>
36+
<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>
42+
</Button>
43+
</Button.Group>
44+
</Popup>
45+
);
46+
}
47+
}
48+
349

450
export default class VariableList extends React.Component {
551

@@ -9,10 +55,26 @@ export default class VariableList extends React.Component {
955
}
1056
}
1157

58+
renderPage() {
59+
let pages = [];
60+
this.props.variableList.forEach(list => {
61+
const variables = list.map((variable, i) => {
62+
return <Variable key={i} {...variable} {...this.props}/>
63+
});
64+
if (variables && variables.length) {
65+
if (pages.length) {
66+
pages.unshift(<hr/>);
67+
}
68+
Array.prototype.unshift.apply(pages, variables)
69+
}
70+
});
71+
return pages;
72+
}
73+
1274
render() {
1375
return (
14-
<div className='search-bar'>
15-
76+
<div className='variable-list'>
77+
{this.renderPage()}
1678
</div>
1779
)
1880
}

‎src/containers/MainContainer.js‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,77 @@ import React from 'react';
22
import {Container} from 'semantic-ui-react';
33
import SearchBar from '../components/SearchBar';
44
import Title from '../components/Title';
5+
import SearchcodeModel from '../models/SearchcodeModel';
6+
import HashHandler from '../utils/HashHandler';
7+
import VariableList from '../components/VariableList';
58

69
export default class MainContainer extends React.Component {
710
state = {
8-
searchValue: null,
11+
isSearchingVariable: false,
12+
searchValue: SearchcodeModel.searchValue,
13+
searchLang: SearchcodeModel.searchLang,
14+
page: SearchcodeModel.page,
15+
variableList: SearchcodeModel.variableList
916
}
1017

1118
constructor(props) {
1219
super(props);
20+
SearchcodeModel.onUpdated(this.handleSearchcodeModelUpdate);
21+
window.addEventListener('hashchange', this.handleLocationHashChanged, false);
1322
}
1423

1524
componentDidUpdate(prevProps) {
16-
if (this.props.location !== prevProps.location) {
17-
}
25+
this.state.variableList.length && document.body.classList.add('dark');
1826
}
1927

2028
componentDidMount() {
21-
document.body.classList.add('dark')
29+
this.handleLocationHashChanged();
30+
}
31+
32+
handleLocationHashChanged = e => {
33+
e && e.preventDefault();
34+
const hash = HashHandler.get();
35+
hash && this.requestVariable(hash.replace(/(\?.*)/, ''));
36+
}
37+
38+
handleSearchcodeModelUpdate = (curr, prev, mutation) => {
39+
this.setState({
40+
isSearchingVariable: !mutation.variableList,
41+
searchValue: SearchcodeModel.searchValue,
42+
searchLang: SearchcodeModel.searchLang,
43+
page: SearchcodeModel.page,
44+
variableList: SearchcodeModel.variableList
45+
});
46+
}
47+
48+
handleSearch = (val, lang) => {
49+
this.setState({searchLang: lang});
50+
if (val === null || val === undefined || this.state.isSearchingVariable) { return; }
51+
val = val.trim().replace(/\s+/ig, ' '); // filter spaces
52+
if (val.length < 1) { return; }
53+
if (val == this.state.searchValue) {
54+
this.requestVariable(val);
55+
} else {
56+
HashHandler.set(val); // update window.location.hash
57+
}
58+
}
59+
60+
requestVariable = val => {
61+
val = decodeURIComponent(val);
62+
let page = this.state.page;
63+
if (val === this.state.searchValue) {
64+
page += 1;
65+
}
66+
this.setState({isSearchingVariable: true});
67+
SearchcodeModel.requestVariable(val, page, this.state.searchLang);
2268
}
2369

2470
render() {
2571
return (
2672
<Container className='main'>
2773
<Title {...this.state}/>
28-
<SearchBar placeholder='AI 人工智能' {...this.state}/>
74+
<SearchBar placeholder='AI 人工智能' {...this.state} onSearch={this.handleSearch}/>
75+
<VariableList {...this.state}/>
2976
</Container>
3077
)
3178
}

‎src/models/AppModel.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import BaseModel from './BaseModel';
2+
import LocalStorage from '../utils/LocalStorage';
3+
import * as Tools from '../utils/Tools';
4+
import {searchParams} from '../utils/Param';
5+
6+
const APP_NANE = 'codelf';
7+
const DEVICE_ID_KEY = `${APP_NANE}_device_id`;
8+
9+
class AppModel extends BaseModel {
10+
constructor() {
11+
super();
12+
this._data = {
13+
debug: searchParams()['debug'],
14+
appName: APP_NANE,
15+
deviceId: LocalStorage.getItem(DEVICE_ID_KEY),
16+
};
17+
if (!this._data.deviceId) {
18+
this._data.deviceId = Tools.uuid();
19+
LocalStorage.setItem(DEVICE_ID_KEY, this._data.deviceId)
20+
}
21+
}
22+
23+
get debug() {
24+
return this._data.debug;
25+
}
26+
}
27+
28+
export default new AppModel();

0 commit comments

Comments
 (0)