Skip to content

Commit e4f9530

Browse files
Avoid direct modification of HTML
1 parent 54e743a commit e4f9530

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

client/modules/IDE/components/Editor/index.jsx

+25-9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const INDENTATION_AMOUNT = 2;
8484
class Editor extends React.Component {
8585
constructor(props) {
8686
super(props);
87+
this.state = {
88+
currentLine: 1
89+
};
90+
this._cm = null;
8791
this.tidyCode = this.tidyCode.bind(this);
8892

8993
this.updateLintingMessageAccessibility = debounce((annotations) => {
@@ -195,12 +199,9 @@ class Editor extends React.Component {
195199
}, 1000)
196200
);
197201

198-
this._cm.on('keyup', () => {
199-
const temp = this.props.t('Editor.KeyUpLineNumber', {
200-
lineNumber: parseInt(this._cm.getCursor().line + 1, 10)
201-
});
202-
document.getElementById('current-line').innerHTML = temp;
203-
});
202+
if (this._cm) {
203+
this._cm.on('keyup', this.handleKeyUp);
204+
}
204205

205206
this._cm.on('keydown', (_cm, e) => {
206207
// Show hint
@@ -336,7 +337,9 @@ class Editor extends React.Component {
336337
}
337338

338339
componentWillUnmount() {
339-
this._cm = null;
340+
if (this._cm) {
341+
this._cm.off('keyup', this.handleKeyUp);
342+
}
340343
this.props.provideController(null);
341344
}
342345

@@ -366,6 +369,11 @@ class Editor extends React.Component {
366369
return updatedFile;
367370
}
368371

372+
handleKeyUp = () => {
373+
const lineNumber = parseInt(this._cm.getCursor().line + 1, 10);
374+
this.setState({ currentLine: lineNumber });
375+
};
376+
369377
showFind() {
370378
this._cm.execCommand('findPersistent');
371379
}
@@ -522,6 +530,8 @@ class Editor extends React.Component {
522530
this.props.file.fileType === 'folder' || this.props.file.url
523531
});
524532

533+
const { currentLine } = this.state;
534+
525535
return (
526536
<MediaQuery minWidth={770}>
527537
{(matches) =>
@@ -565,7 +575,10 @@ class Editor extends React.Component {
565575
name={this.props.file.name}
566576
/>
567577
) : null}
568-
<EditorAccessibility lintMessages={this.props.lintMessages} />
578+
<EditorAccessibility
579+
lintMessages={this.props.lintMessages}
580+
currentLine={currentLine}
581+
/>
569582
</section>
570583
) : (
571584
<EditorContainer expanded={this.props.isExpanded}>
@@ -591,7 +604,10 @@ class Editor extends React.Component {
591604
name={this.props.file.name}
592605
/>
593606
) : null}
594-
<EditorAccessibility lintMessages={this.props.lintMessages} />
607+
<EditorAccessibility
608+
lintMessages={this.props.lintMessages}
609+
currentLine={currentLine}
610+
/>
595611
</section>
596612
</EditorContainer>
597613
)

client/modules/IDE/components/EditorAccessibility.jsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import PropTypes from 'prop-types';
22
import React from 'react';
33
import { useTranslation } from 'react-i18next';
44

5-
const EditorAccessibility = ({ lintMessages = [] }) => {
5+
const EditorAccessibility = ({ lintMessages = [], currentLine }) => {
66
const { t } = useTranslation();
7+
const lineText = t('Editor.KeyUpLineNumber', { lineNumber: currentLine });
8+
79
return (
810
<div className="editor-accessibility">
911
<ul className="editor-lintmessages" title="lint messages">
@@ -27,7 +29,7 @@ const EditorAccessibility = ({ lintMessages = [] }) => {
2729
aria-atomic="true"
2830
id="current-line"
2931
>
30-
{' '}
32+
{lineText}
3133
</span>
3234
</p>
3335
</div>
@@ -43,7 +45,8 @@ EditorAccessibility.propTypes = {
4345
message: PropTypes.string.isRequired,
4446
id: PropTypes.number.isRequired
4547
})
46-
).isRequired
48+
).isRequired,
49+
currentLine: PropTypes.number.isRequired
4750
};
4851

4952
export default EditorAccessibility;

0 commit comments

Comments
 (0)