Skip to content

Commit 341010c

Browse files
author
Ives van Hoorne
committed
Add line highlighting to CodeMirror
1 parent ea8ab36 commit 341010c

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

src/app/components/sandbox/CodeEditor/CodeMirror.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Props = {
3232
modules: Array,
3333
directories: Array,
3434
hideNavigation: boolean,
35+
highlightedLines: ?Array<string>,
3536
};
3637

3738
const Container = styled.div`
@@ -141,7 +142,7 @@ const CodeContainer = styled.div`
141142
}
142143
143144
.CodeMirror-activeline-background {
144-
background: #374140;
145+
background: rgba(0, 0, 0, 0.2);
145146
}
146147
.CodeMirror-matchingbracket {
147148
text-decoration: underline;
@@ -161,6 +162,10 @@ const CodeContainer = styled.div`
161162
animation: ${fadeInAnimation} 0.3s;
162163
background-color: #561011;
163164
}
165+
166+
div.cm-line-highlight.CodeMirror-linebackground {
167+
background-color: rgba(0, 0, 0, 0.3);
168+
}
164169
`;
165170

166171
const handleError = (
@@ -195,6 +200,16 @@ const handleError = (
195200
}
196201
};
197202

203+
const highlightLines = (
204+
cm: typeof CodeMirror,
205+
highlightedLines: Array<string>
206+
) => {
207+
console.log(highlightedLines); // eslint-disable-line
208+
highlightedLines.forEach(line => {
209+
cm.addLineClass(+line, 'background', 'cm-line-highlight');
210+
});
211+
};
212+
198213
type State = {
199214
fuzzySearchEnabled: boolean,
200215
};
@@ -300,7 +315,7 @@ export default class CodeEditor extends React.Component<Props, State> {
300315
};
301316

302317
getCodeMirror = async (el: Element) => {
303-
const { code, id, title } = this.props;
318+
const { code, id, title, highlightedLines } = this.props;
304319
if (!this.props.onlyViewMode) {
305320
CodeMirror.commands.save = this.handleSaveCode;
306321
}
@@ -315,6 +330,11 @@ export default class CodeEditor extends React.Component<Props, State> {
315330
} else {
316331
this.codemirror.setOption('readOnly', true);
317332
}
333+
334+
console.log(this.props); // eslint-disable-line
335+
if (highlightedLines) {
336+
highlightLines(this.codemirror, highlightedLines);
337+
}
318338
};
319339

320340
setCodeMirrorPreferences = async () => {

src/common/__snapshots__/url.test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Object {
99
"isEditorScreen": false,
1010
"isInProjectView": true,
1111
"isPreviewScreen": false,
12+
"isSplitScreen": false,
1213
"useCodeMirror": false,
1314
}
1415
`;
@@ -23,6 +24,7 @@ Object {
2324
"isEditorScreen": true,
2425
"isInProjectView": true,
2526
"isPreviewScreen": false,
27+
"isSplitScreen": false,
2628
"useCodeMirror": false,
2729
}
2830
`;
@@ -36,6 +38,7 @@ Object {
3638
"isEditorScreen": false,
3739
"isInProjectView": true,
3840
"isPreviewScreen": false,
41+
"isSplitScreen": false,
3942
"useCodeMirror": false,
4043
}
4144
`;
@@ -49,6 +52,7 @@ Object {
4952
"isEditorScreen": false,
5053
"isInProjectView": true,
5154
"isPreviewScreen": false,
55+
"isSplitScreen": false,
5256
"useCodeMirror": false,
5357
}
5458
`;
@@ -62,6 +66,7 @@ Object {
6266
"isEditorScreen": false,
6367
"isInProjectView": true,
6468
"isPreviewScreen": false,
69+
"isSplitScreen": false,
6570
"useCodeMirror": false,
6671
}
6772
`;
@@ -76,6 +81,7 @@ Object {
7681
"isEditorScreen": false,
7782
"isInProjectView": true,
7883
"isPreviewScreen": false,
84+
"isSplitScreen": false,
7985
"useCodeMirror": false,
8086
}
8187
`;
@@ -89,6 +95,7 @@ Object {
8995
"isEditorScreen": true,
9096
"isInProjectView": true,
9197
"isPreviewScreen": false,
98+
"isSplitScreen": false,
9299
"useCodeMirror": false,
93100
}
94101
`;
@@ -102,6 +109,7 @@ Object {
102109
"isEditorScreen": false,
103110
"isInProjectView": true,
104111
"isPreviewScreen": true,
112+
"isSplitScreen": false,
105113
"useCodeMirror": false,
106114
}
107115
`;

src/common/url.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export const getSandboxOptions = (url: string) => {
1919
result.fontSize = +fontSizeMatch[3];
2020
}
2121

22+
const highlightMatch = url.match(/(\?|&)(highlights)=([^&]+)/);
23+
if (highlightMatch) {
24+
const lineHighlightsMatch = highlightMatch[3].match(/{(.*)}/);
25+
26+
if (lineHighlightsMatch) {
27+
result.highlightedLines = lineHighlightsMatch[1].split(',');
28+
}
29+
}
30+
2231
const editorSizeMatch = url.match(/(\?|&)(editorsize)=([^&]+)/);
2332
if (editorSizeMatch) {
2433
result.editorSize = +editorSizeMatch[3];

src/embed/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type State = {
6161
enableEslint: boolean,
6262
useCodeMirror: boolean,
6363
editorSize: number,
64+
highlightedLines: Array<string>,
6465
};
6566

6667
export default class App extends React.PureComponent<{}, State> {
@@ -79,6 +80,7 @@ export default class App extends React.PureComponent<{}, State> {
7980
enableEslint,
8081
useCodeMirror,
8182
editorSize,
83+
highlightedLines,
8284
} = getSandboxOptions(document.location.href);
8385

8486
this.state = {
@@ -96,6 +98,7 @@ export default class App extends React.PureComponent<{}, State> {
9698
enableEslint,
9799
useCodeMirror,
98100
editorSize,
101+
highlightedLines: highlightedLines || [],
99102
};
100103
}
101104

@@ -216,6 +219,7 @@ export default class App extends React.PureComponent<{}, State> {
216219
useCodeMirror={this.state.useCodeMirror}
217220
enableEslint={this.state.enableEslint}
218221
editorSize={this.state.editorSize}
222+
highlightedLines={this.state.highlightedLines}
219223
/>
220224
</Container>
221225
</ThemeProvider>

src/embed/components/Content.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Props = {
4343
enableEslint: boolean,
4444
isInProjectView: boolean,
4545
editorSize: number,
46+
highlightedLines: Array<string>,
4647
};
4748

4849
type State = {
@@ -181,6 +182,7 @@ export default class Content extends React.PureComponent<Props, State> {
181182
currentModule,
182183
hideNavigation,
183184
editorSize,
185+
highlightedLines,
184186
} = this.props;
185187

186188
const { errors } = this.state;
@@ -231,6 +233,7 @@ export default class Content extends React.PureComponent<Props, State> {
231233
hideNavigation={hideNavigation}
232234
canSave={false}
233235
corrections={[]}
236+
highlightedLines={highlightedLines}
234237
/>
235238
</Split>
236239
)}

0 commit comments

Comments
 (0)