From 4eb83d3ebec6fbf2f1e1afafa33f5bca16fc1d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=95=E6=96=B0?= Date: Mon, 1 Feb 2016 11:14:54 +0800 Subject: [PATCH] [format]: update lint --- .eslintrc | 3 +- src/actions/LeafActions.js | 33 ++-- src/components/Desktop/AddFile.jsx | 161 +++++++++--------- src/components/Desktop/Aside.jsx | 9 +- src/components/Desktop/Head.jsx | 6 +- src/components/Desktop/List.jsx | 14 +- src/components/Login/loginForm.jsx | 30 ++-- src/components/Post/Editor.jsx | 257 +++++++++++++++-------------- src/components/Post/Head.jsx | 6 +- src/components/Post/Meta.jsx | 66 ++++---- src/constants/LeafActionTypes.js | 2 +- src/containers/Desktop.jsx | 47 +++--- src/containers/Dir.jsx | 46 +++--- src/containers/Login.jsx | 11 +- src/containers/Post.jsx | 53 +++--- src/entry/App.jsx | 2 +- src/reducers/Blob.js | 28 ++-- src/reducers/RepoInfo.js | 10 +- src/reducers/RepoTree.js | 8 +- src/reducers/Tree.js | 16 +- src/reducers/User.js | 4 +- src/reducers/index.js | 2 +- src/services/auth.js | 12 +- src/services/repo.js | 47 ++++-- src/services/user.js | 25 +-- src/utils/editorFormat.js | 51 +++--- 26 files changed, 515 insertions(+), 434 deletions(-) diff --git a/.eslintrc b/.eslintrc index 99df44a..0990fef 100644 --- a/.eslintrc +++ b/.eslintrc @@ -17,7 +17,8 @@ "vars-on-top": 0, // Disable comma-dangle unless need to support it - "comma-dangle": 0 + "comma-dangle": 0, + "consistent-return": 1 }, "plugins": [ "react" diff --git a/src/actions/LeafActions.js b/src/actions/LeafActions.js index 1fae7b2..dbad14b 100644 --- a/src/actions/LeafActions.js +++ b/src/actions/LeafActions.js @@ -16,7 +16,7 @@ export function loginDone() { auth.loginDone(); return { type: types.AUTH_LOGIN_DONE, - } + }; } export function logout() { @@ -31,62 +31,61 @@ export function updateUserInfo(data) { return { type: types.UPDATE_USER_INFO, data, - } - } else { - return { - types: [types.LOAD_USER_INFO, types.LOAD_USER_INFO_SUCCESS, types.LOAD_USER_INFO_FAIL], - promise: user.getInfo(), - } + }; } + return { + types: [types.LOAD_USER_INFO, types.LOAD_USER_INFO_SUCCESS, types.LOAD_USER_INFO_FAIL], + promise: user.getInfo(), + }; } export function loadRepoInfo(data) { return { types: [types.LOAD_REPO_INFO, types.LOAD_REPO_INFO_SUCCESS, types.LOAD_REPO_INFO_FAIL], promise: user.checkRepo(data.username), - } + }; } export function updateRepoInfo(data) { return { type: types.UPLOAD_REPO_INFO, data, - } + }; } export function loadRepoTree(data) { return { types: [types.LOAD_REPO_TREE, types.LOAD_REPO_TREE_SUCCESS, types.LOAD_REPO_TREE_FAIL], promise: repo.getTree(data.username, data.reponame), - } + }; } export function readRepoTree(data) { return { types: [types.READ_REPO_TREE, types.READ_REPO_TREE_SUCCESS, types.READ_REPO_TREE_FAIL], promise: repo.readTree(data.username, data.reponame, data.path), - } + }; } export function readRepoBlob(data) { return { types: [types.READ_REPO_BLOB, types.READ_REPO_BLOB_SUCCESS, types.READ_REPO_BLOB_FAIL], promise: repo.readBlob(data.username, data.reponame, data.path), - } + }; } export function readRepoBlobCommit(data) { return { types: [types.READ_REPO_BLOB_COMMIT, types.READ_REPO_BLOB_COMMIT_SUCCESS, types.READ_REPO_BLOB_COMMIT_FAIL], promise: repo.readBlobCommit(data.username, data.reponame, data.sha), - } + }; } export function addRepoBlob(data) { return { types: [types.ADD_REPO_BLOB, types.ADD_REPO_BLOB_SUCCESS, types.ADD_REPO_BLOB_FAIL], promise: repo.addBlob(data), - } + }; } export function updateRepoBlob(data) { @@ -94,7 +93,7 @@ export function updateRepoBlob(data) { types: [types.UPDATE_REPO_BLOB, types.UPDATE_REPO_BLOB_SUCCESS, types.UPDATE_REPO_BLOB_FAIL], promise: repo.writeBlob(data), data, - } + }; } export function removeRepoBlob(data) { @@ -102,11 +101,11 @@ export function removeRepoBlob(data) { types: [types.REMOVE_REPO_BLOB, types.REMOVE_REPO_BLOB_SUCCESS, types.REMOVE_REPO_BLOB_FAIL], promise: repo.removeBlob(data), data, - } + }; } export function clearRepoBlob() { return { type: types.CLEAR_REPO_BLOB, - } + }; } diff --git a/src/components/Desktop/AddFile.jsx b/src/components/Desktop/AddFile.jsx index bf699fb..18d6de4 100644 --- a/src/components/Desktop/AddFile.jsx +++ b/src/components/Desktop/AddFile.jsx @@ -2,7 +2,6 @@ import React, { Component, PropTypes } from 'react'; import { Form, Item as FormItem } from 'antd/lib/form'; import Modal from 'antd/lib/modal'; -import Row from 'antd/lib/row'; import Col from 'antd/lib/col'; import Input from 'antd/lib/input'; import Button from 'antd/lib/button'; @@ -11,101 +10,109 @@ import createForm from 'rc-form/lib/createForm'; class CreateFileForm extends Component { + + static propTypes = { + modalVisible: PropTypes.bool, + modalHandleOk: PropTypes.func, + modalHandleCancel: PropTypes.func, + form: PropTypes.object, + } + + modalHandleOk(e) { + e.preventDefault(); + const { form, modalHandleOk } = this.props; + const { validateFields } = form; + validateFields((error, values) => { + if (!error) { + modalHandleOk(values); + } else { + console.log('error', error, values); + } + }); + } + render() { const { modalVisible, modalHandleOk, modalHandleCancel, form } = this.props; const { getFieldProps, getFieldError } = form; return ( Cancle, - - ]}> + , + + ]} + >
- - -

{getFieldError('name') ? getFieldError('name').join('') : ''}

- -
+ > + + +

{getFieldError('name') ? getFieldError('name').join('') : ''}

+ + - - -

{getFieldError('title') ? getFieldError('title').join('') : ''}

- -
+ > + + +

{getFieldError('title') ? getFieldError('title').join('') : ''}

+ + - - -

{getFieldError('description') ? getFieldError('description').join('') : ''}

- -
-
-
- ) + > + + +

{getFieldError('description') ? getFieldError('description').join('') : ''}

+ + + + + ); } - - modalHandleOk(e) { - e.preventDefault(); - const { form, modalHandleOk } = this.props; - const { validateFields } = form; - validateFields((error, values) => { - if (!error) { - modalHandleOk(values); - } else { - console.log('error', error, values) - } - }) - } -} - -CreateFileForm.propTypes = { - form: PropTypes.object } -export default createForm()(CreateFileForm); \ No newline at end of file +export default createForm()(CreateFileForm); diff --git a/src/components/Desktop/Aside.jsx b/src/components/Desktop/Aside.jsx index 1206c0f..f4f6c5f 100644 --- a/src/components/Desktop/Aside.jsx +++ b/src/components/Desktop/Aside.jsx @@ -9,6 +9,13 @@ import classNames from 'classnames'; class Aside extends Component { + static propTypes = { + logout: PropTypes.func, + user: PropTypes.object, + repoInfo: PropTypes.object, + tree: PropTypes.object, + } + logout() { const { logout } = this.props; logout(); @@ -85,7 +92,7 @@ class Aside extends Component { - ) + ); } } diff --git a/src/components/Desktop/Head.jsx b/src/components/Desktop/Head.jsx index 188a3d3..229848e 100644 --- a/src/components/Desktop/Head.jsx +++ b/src/components/Desktop/Head.jsx @@ -11,6 +11,9 @@ class Head extends Component { static propTypes = { logout: PropTypes.func, + addFile: PropTypes.func, + repoInfo: PropTypes.object, + params: PropTypes.object, } render() { @@ -22,7 +25,7 @@ class Head extends Component { const data = { name: item, path: crumbData.slice(0, index + 1).join('/'), - } + }; return ( { index === crumbData.length - 1 && @@ -66,7 +69,6 @@ class Head extends Component { ); } - } export default Head; diff --git a/src/components/Desktop/List.jsx b/src/components/Desktop/List.jsx index 95a5295..1ce9ff2 100644 --- a/src/components/Desktop/List.jsx +++ b/src/components/Desktop/List.jsx @@ -1,22 +1,24 @@ import React, { Component, PropTypes } from 'react'; -import Row from 'antd/lib/row'; -import Col from 'antd/lib/col'; import Icon from 'antd/lib/icon'; import classNames from 'classnames'; import { Link } from 'react-router'; class List extends Component { + + static propTypes = { + tree: PropTypes.object, + } + render() { const { tree } = this.props; let files; if (tree.loaded) { - files = ( tree.data.map(item => { let fileName = item.name.split(/^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-/); fileName = fileName[4] || item.name; - let filePath = item.path.split('_posts/')[1]; + const filePath = item.path.split('_posts/')[1]; const fileCls = classNames({ 'file': true, 'file-f': item.type === 'file', @@ -42,7 +44,7 @@ class List extends Component { } - ) + ); }) ); } @@ -68,4 +70,4 @@ class List extends Component { } } -export default List; \ No newline at end of file +export default List; diff --git a/src/components/Login/loginForm.jsx b/src/components/Login/loginForm.jsx index 0ac4a0b..4678b99 100644 --- a/src/components/Login/loginForm.jsx +++ b/src/components/Login/loginForm.jsx @@ -1,6 +1,5 @@ import React, { Component, PropTypes } from 'react'; -import Modal from 'antd/lib/modal'; import Row from 'antd/lib/row'; import Col from 'antd/lib/col'; import Input from 'antd/lib/input'; @@ -14,12 +13,19 @@ import createForm from 'rc-form/lib/createForm'; class loginForm extends Component { + static propTypes = { + onSubmit: PropTypes.func, + form: PropTypes.object, + auth: PropTypes.object, + } + onSubmit(e) { e.preventDefault(); - const { onSubmit } = this.props; - this.props.form.validateFields((error, values) => { + const { onSubmit, form } = this.props; + const { validateFields } = form; + validateFields((error, values) => { if (!error) { - onSubmit(values) + onSubmit(values); } }); } @@ -34,17 +40,17 @@ class loginForm extends Component { } + > ()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, - message: "Please input your GitHub Email", + message: 'Please input your GitHub Email', }] })} /> @@ -53,15 +59,15 @@ class loginForm extends Component { + > diff --git a/src/components/Post/Editor.jsx b/src/components/Post/Editor.jsx index 475e032..ade92c6 100644 --- a/src/components/Post/Editor.jsx +++ b/src/components/Post/Editor.jsx @@ -3,9 +3,9 @@ import classNames from 'classnames'; import CM from 'codemirror/lib/codemirror'; import { getCursorState, applyFormat } from '../../utils/editorFormat'; -import marked from 'marked/lib/marked'; -import hljs from 'highlight.js/lib/highlight.js'; -import Key from 'keymaster'; +import marked from 'marked/lib/marked'; +// import hljs from 'highlight.js/lib/highlight.js'; +import key from 'keymaster'; import Icon from 'antd/lib/icon'; import Tooltip from 'antd/lib/tooltip'; import '../../styles/EditIcon.less'; @@ -40,19 +40,23 @@ class Editor extends Component { options: PropTypes.object, path: PropTypes.string, value: PropTypes.string, + history: PropTypes.object, + blob: PropTypes.object, + params: PropTypes.object, + handleSave: PropTypes.func, + className: PropTypes.string, } constructor(props, context) { super(props, context); - this.state = { isPreview: false, onScrolled: false, cs: {}, - } + }; } - componentDidMount () { + componentDidMount() { const { value } = this.props; const editorNode = this.refs.editor; const that = this; @@ -62,35 +66,58 @@ class Editor extends Component { this._currentCodemirrorValue = value; // 保存 - Key('⌘+s, ctrl+s', function(e) { + key('⌘+s, ctrl+s', (e) => { e.preventDefault(); that.handleSave(); }); // 返回 - Key('⌘+shift+left', function(e) { + key('⌘+shift+left', (e) => { e.preventDefault(); that.handleBack(); }); // 预览 - Key('⌘+0, ctrl+0', function(e) { + key('⌘+0, ctrl+0', (e) => { e.preventDefault(); that.handlePreviewEvent(); - }) + }); } - handleBack() { - const { history, blob } = this.props; - let backDir = ''; - if (blob.loaded) { - backDir = params.splat.split(blob.data.name)[0]; - backDir = backDir !== '' ? 'd/' + backDir : ''; + componentWillReceiveProps(nextProps) { + if (this.codeMirror && nextProps.value !== undefined && this._currentCodemirrorValue !== nextProps.value) { + this.codeMirror.setValue(nextProps.value); + this.codeMirror.refresh(); + this.codeMirror.focus(); } - history.pushState(null, '/_posts/' + backDir); } - getOptions () { + /* + * 1. 切换视图 + * 2. 滚动条位置 + * 3. 选中文字 + * 4. 内容变化了 + */ + shouldComponentUpdate(nextProps, nextState) { + return nextState.isPreview !== this.state.isPreview || nextState.onScrolled !== this.state.onScrolled || nextState.cs.render || nextState.cs.render !== this.state.cs.render || nextProps.value !== this.props.value; + } + + componentWillUpdate(nextProps, nextState) { + if (nextState.isPreview !== this.state.isPreview) { + this.previewMarkup = marked(this._currentCodemirrorValue); + } + } + + componentWillUnmount() { + if (this.codeMirror) { + this.codeMirror.setValue(''); + this.codeMirror.clearHistory(); + // http://stackoverflow.com/questions/18828658/how-to-kill-a-codemirror-instance + this.codeMirror.toTextArea(); + } + } + + getOptions() { const that = this; return Object.assign({ mode: 'gfm', @@ -101,103 +128,134 @@ class Editor extends Component { autofocus: true, tabSize: '2', extraKeys: { - "Enter": "newlineAndIndentContinueMarkdownList", - "Cmd-S": function() { + 'Enter': 'newlineAndIndentContinueMarkdownList', + 'Cmd-S': () => { that.handleSave(); }, - "Cmd-B": function() { + 'Cmd-B': () => { that.toggleFormat('bold'); }, - "Cmd-I": function() { - that.toggleFormat('italic') + 'Cmd-I': () => { + that.toggleFormat('italic'); }, - "Cmd-1": function() { + 'Cmd-1': () => { that.toggleFormat('h1'); }, - "Cmd-2": function() { + 'Cmd-2': () => { that.toggleFormat('h2'); }, - "Cmd-3": function() { + 'Cmd-3': () => { that.toggleFormat('h3'); }, - "Cmd-Alt-U": function() { + 'Cmd-Alt-U': () => { that.toggleFormat('uList'); }, - "Cmd-Alt-O": function() { + 'Cmd-Alt-O': () => { that.toggleFormat('oList'); }, - "Cmd-Alt-G": function() { + 'Cmd-Alt-G': () => { that.toggleFormat('del'); }, - "Cmd-Alt-C": function() { + 'Cmd-Alt-C': () => { that.toggleFormat('code'); }, - "Cmd-Alt-E": function() { + 'Cmd-Alt-E': () => { that.toggleFormat('quote'); }, - "Cmd-Alt-L": function() { + 'Cmd-Alt-L': () => { that.toggleFormat('link'); }, - "Cmd-Alt-P": function() { + 'Cmd-Alt-P': () => { that.toggleFormat('image'); }, - "Cmd-0": function() { + 'Cmd-0': () => { that.handlePreviewEvent(); - } - } + }, + }, }, this.props.options); } - componentWillReceiveProps (nextProps) { - if (this.codeMirror && nextProps.value !== undefined && this._currentCodemirrorValue !== nextProps.value) { - this.codeMirror.setValue(nextProps.value); - this.codeMirror.refresh(); - this.codeMirror.focus(); + updateCursorState() { + this.setState({ cs: getCursorState(this.codeMirror) }); + } + + codemirrorValueChanged(doc) { + const newValue = doc.getValue(); + this._currentCodemirrorValue = newValue; + } + + toggleFormat(formatKey) { + if (this.state.isPreview) { + return; } + applyFormat(this.codeMirror, formatKey); } - /* - * 1. 切换视图 - * 2. 滚动条位置 - * 3. 选中文字 - * 4. 内容变化了 - */ - shouldComponentUpdate(nextProps, nextState) { - return nextState.isPreview !== this.state.isPreview || nextState.onScrolled !== this.state.onScrolled || nextState.cs.render || nextState.cs.render !== this.state.cs.render || nextProps.value !== this.props.value; + handleScrollEditor() { + if (this.refs.editorContainer.scrollTop > 0 && !this.state.onScrolled) { + this.setState({ onScrolled: true }); + } else if (this.refs.editorContainer.scrollTop < 10) { + this.setState({ onScrolled: false }); + } } - componentWillUnmount () { + handleBack() { + const { history, blob, params } = this.props; + let backDir = ''; + if (blob.loaded) { + backDir = params.splat.split(blob.data.name)[0]; + backDir = backDir !== '' ? 'd/' + backDir : ''; + } + history.pushState(null, '/_posts/' + backDir); + } + + handlePenFocus() { + event.preventDefault(); if (this.codeMirror) { - this.codeMirror.setValue(''); - this.codeMirror.clearHistory(); - // http://stackoverflow.com/questions/18828658/how-to-kill-a-codemirror-instance - this.codeMirror.toTextArea(); + this.codeMirror.focus(); } } - updateCursorState () { - this.setState({ cs: getCursorState(this.codeMirror) }); + handlePreviewEvent() { + this.setState({ + isPreview: !this.state.isPreview + }); } - codemirrorValueChanged (doc, change) { - const newValue = doc.getValue(); - this._currentCodemirrorValue = newValue; + handleSave() { + const { handleSave } = this.props; + // 重新组装 + if (handleSave) { + handleSave(this._currentCodemirrorValue); + } } - toggleFormat (formatKey) { - if (this.state.isPreview) { - return; + handleUndo() { + event.preventDefault(); + if (this.codeMirror && !this.state.isPreview) { + this.codeMirror.undoSelection(); } - applyFormat(this.codeMirror, formatKey); } - renderIcon (icon) { - return ; + handleRedo() { + event.preventDefault(); + if (this.codeMirror && !this.state.isPreview) { + this.codeMirror.redoSelection(); + } } - renderButton (formatKey, label, action) { + renderIcon(icon) { + return ; + } + + renderButton(formatKey, label, action) { const { blob } = this.props; - if (!action) action = this.toggleFormat.bind(this, formatKey); + let actionTmp; + if (!action) { + actionTmp = this.toggleFormat.bind(this, formatKey); + } else { + actionTmp = action; + } const className = classNames('leaf-editor-tool-icon', { 'leaf-editor-tool-icon-active': this.state.cs[formatKey], @@ -206,31 +264,14 @@ class Editor extends Component { ('leaf-editor-tool-icon-' + formatKey)); return ( - ); } - handleScrollEditor() { - if (this.refs.editorContainer.scrollTop > 0 && !this.state.onScrolled) { - this.setState({ onScrolled: true }); - } else if (this.refs.editorContainer.scrollTop < 10) { - this.setState({ onScrolled: false }); - } - } - - componentWillUpdate(nextProps, nextState) { - if (nextState.isPreview !== this.state.isPreview) { - const start_time = new Date(); - let rawMarkup = marked(this._currentCodemirrorValue); - this.previewMarkup = rawMarkup; - console.info('[log] 渲染预览消耗 %s ms', new Date() - start_time); - } - } - - renderToolbar () { + renderToolbar() { const { blob } = this.props; const previewClassName = classNames({ 'edit-icon': true, @@ -286,40 +327,7 @@ class Editor extends Component { ); } - handlePenFocus() { - event.preventDefault(); - if (this.codeMirror) { - this.codeMirror.focus(); - } - } - - handlePreviewEvent() { - this.setState({ - isPreview: !this.state.isPreview - }); - } - - handleSave() { - const { handleSave } = this.props; - // 重新组装 - handleSave && handleSave(this._currentCodemirrorValue); - } - - handleUndo() { - event.preventDefault(); - if (this.codeMirror && !this.state.isPreview) { - this.codeMirror.undoSelection(); - } - } - - handleRedo() { - event.preventDefault(); - if (this.codeMirror && !this.state.isPreview) { - this.codeMirror.redoSelection(); - } - } - - render () { + render() { const { className, blob } = this.props; const editorClassName = classNames({ @@ -362,14 +370,15 @@ class Editor extends Component {