-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
TextlintEditor.js
186 lines (175 loc) · 5.87 KB
/
TextlintEditor.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// MIT © 2017 azu
"use strict";
import i18next from "i18next";
// node
const remote = require("electron").remote;
const textlintToCodeMirror = require("textlint-message-to-codemirror");
const debug = require("debug")("textlint-app:TextlintEditor");
const debounce = require("lodash.debounce");
// infra
import TextlintAPI from "../../../infra/textlint/TextlintAPI";
// main
const React = require("react");
const CodeMirror = require("react-codemirror");
require("codemirror/mode/markdown/markdown.js");
require("codemirror/addon/lint/lint.js");
require("codemirror/addon/lint/lint.css");
// search
require("codemirror/addon/dialog/dialog.css");
require("codemirror/addon/search/matchesonscrollbar.css");
require("codemirror/addon/dialog/dialog.js");
require("codemirror/addon/search/searchcursor.js");
require("codemirror/addon/search/search.js");
require("codemirror/addon/scroll/annotatescrollbar.js");
require("codemirror/addon/search/matchesonscrollbar.js");
require("codemirror/addon/search/jump-to-line.js");
export default class TextlintEditor extends React.Component {
static propTypes = {
modulesDirectory: React.PropTypes.string,
textlintrcFilePath: React.PropTypes.string,
value: React.PropTypes.string,
defaultValue: React.PropTypes.string,
onChange: React.PropTypes.func,
onLintError: React.PropTypes.func
};
constructor() {
super();
/**
* @private
*/
this._CodeMirror = null;
this.state = {
textValue: i18next.t(`# Usage
1. Setting .textlintrc. (Go to \`Settings\` tab)
2. Install textlint rules via .textlintrc. (In \`Settings\` tab)
3. Write Texts and Lint! (Here!)
`)
};
this.updateValue = this._updateValue.bind(this);
this.validator = this._createValidator();
}
jumpToPos({line, ch}) {
if (!this._CodeMirror) {
return;
}
const codeMirror = this._CodeMirror.getCodeMirror();
codeMirror.focus();
codeMirror.setCursor({line, ch});
}
shouldComponentUpdate(nextProps, nextState) {
if (this.state.textValue !== nextState.textValue) {
return true;
}
if (this.props.value !== nextProps.value) {
return true;
}
return false;
}
componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this._updateValue(nextProps.value);
}
if (this.props.textlintrcFilePath !== nextProps.textlintrcFilePath ||
this.props.modulesDirectory !== nextProps.modulesDirectory
) {
this.validator = debounce(this._createValidator({
textlintrcFilePath: nextProps.textlintrcFilePath,
nodeModulesDirectory: nextProps.modulesDirectory
}), 300);
}
}
componentDidMount() {
if (this._CodeMirror) {
const codeMirror = this._CodeMirror.getCodeMirror();
codeMirror.getScrollerElement().style.minHeight = "30em";
// Workaround for IME position
// https://github.com/codemirror/CodeMirror/issues/4089
// https://github.com/BoostIO/Boostnote/commit/8f1c198406d68ef7818a84f4201c6df446e14592
codeMirror.getInputField().style.marginBottom = "-2em";
codeMirror.refresh();
}
this.validator = debounce(this._createValidator({
textlintrcFilePath: this.props.textlintrcFilePath,
nodeModulesDirectory: this.props.modulesDirectory
}), 300);
}
render() {
const options = {
lineNumbers: true,
lineWrapping: true,
mode: "markdown",
inputStyle: "textarea",
extraKeys: {"Alt-F": "findPersistent"},
gutters: ["CodeMirror-lint-markers"],
lint: {
"getAnnotations": this.validator,
"async": true
}
};
return <div className="TextlintEditor">
<CodeMirror
ref={c => this._CodeMirror = c }
value={this.state.textValue}
defaultValue={this.props.defaultValue}
onChange={this.updateValue}
options={options}/>
</div>;
}
/**
* @param {string}value
* @private
*/
_updateValue(value) {
if (this.state.textValue !== value) {
this.setState({
textValue: value
});
this.props.onChange(value);
}
}
/**
*
* @param {string} [textlintrcFilePath]
* @param {string} [nodeModulesDirectory]
* @returns {function()}
* @private
*/
_createValidator({
textlintrcFilePath,
nodeModulesDirectory
} = {}) {
debug("textlintrcFilePath", textlintrcFilePath, "nodeModulesDirectory", nodeModulesDirectory);
if (!textlintrcFilePath || !nodeModulesDirectory) {
return (text, callback) => {
callback([]);
};
}
const textlintAPI = new TextlintAPI({
configFile: textlintrcFilePath,
rulesBaseDirectory: nodeModulesDirectory
});
let isLinting = false;
return (text, callback) => {
if (!text) {
callback([]);
return;
}
if (isLinting) {
return;
}
isLinting = true;
textlintAPI.lintText(text, ".md").then(lintMessages => {
isLinting = false;
debug(`Found ${lintMessages.length} Errors`);
const lintErrors = lintMessages.map(textlintToCodeMirror);
this.props.onLintError({
lintMessages,
lintErrors
});
callback(lintErrors);
}).catch(error => {
debug(error);
});
};
}
}