Skip to content

Commit

Permalink
fix: codemirror render problem (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
hetao92 authored Feb 3, 2023
1 parent 3b9a730 commit 1d72556
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions app/components/CodeMirror/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,13 @@ export default class ReactCodeMirror extends React.PureComponent<IProps, any> {
this.editor.on('change', this.codemirrorValueChange);
this.editor.on('keydown', this.keydown);
this.editor.on('blur', this.blur);
this.editor.on('renderLine', this.renderLine);
const { value, width, height } = this.props;
this.editor.setValue(value || '');
if (width || height) {
// set size
this.editor.setSize(width, height);
}
}
renderLine = (_instance, line, _element) => {
const info = this.editor.lineInfo(line);
const _text = info?.text?.trim();
if(_text?.startsWith('//') || _text?.startsWith('#')) {
// will trigger error info in codemirror without setTimeout
setTimeout(() => {
this.editor.addLineClass(line, 'wrap', 'notes');
}, 0);
} else if(info?.wrapClass === 'notes') {
this.editor.removeLineClass(line, 'wrap', 'notes');
}
};
blur = instance => {
if (this.props.onBlur) {
this.props.onBlur(instance.doc.getValue());
Expand All @@ -137,6 +124,13 @@ export default class ReactCodeMirror extends React.PureComponent<IProps, any> {
};

codemirrorValueChange = (doc, change) => {
const line = change.from.line;
const lineInfo = doc?.lineInfo(line);
if (lineInfo?.text?.startsWith('//') || lineInfo?.text?.startsWith('#')) {
doc.addLineClass(line, 'wrap', 'notes');
} else if(lineInfo?.wrapClass === 'notes') {
doc.removeLineClass(line, 'wrap', 'notes');
}
if (change.origin !== 'setValue') {
if (this.props.onChange) {
this.props.onChange(doc.getValue());
Expand Down

0 comments on commit 1d72556

Please sign in to comment.