Skip to content

Commit 53a125d

Browse files
committed
Fixes syntax highlighter issues
1 parent 1a46bdb commit 53a125d

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

src/component/modals/FileEdit.jsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { highlight, languages } from 'prismjs/components/prism-core';
44
import { saveAs } from 'file-saver';
55
import Modal from './ParentModal';
66
import './file-edit.css';
7+
import 'prismjs/components/prism-bash';
78
import 'prismjs/components/prism-clike';
9+
import 'prismjs/components/prism-c';
10+
import 'prismjs/components/prism-cpp';
11+
import 'prismjs/components/prism-matlab';
12+
import 'prismjs/components/prism-verilog';
13+
import 'prismjs/components/prism-python';
814
import 'prismjs/components/prism-javascript';
915
import 'prismjs/themes/prism.css';
1016
import { actionType as T } from '../../reducer';
@@ -20,8 +26,12 @@ const FileEditModal = ({ superState, dispatcher }) => {
2026
}
2127
}, []);
2228

23-
const close = () => dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
24-
// TODO - Save file
29+
const close = () => {
30+
dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
31+
setCodeStuff('');
32+
setFileName('');
33+
};
34+
2535
async function submit() {
2636
if (superState.fileHandle) {
2737
const stream = await superState.fileHandle.createWritable();
@@ -62,6 +72,25 @@ const FileEditModal = ({ superState, dispatcher }) => {
6272
}
6373
}, [superState.fileObj]);
6474

75+
function highlightSyntax(code) {
76+
const extensions = ['v', 'c', 'h', 'hpp', 'cpp', 'py', 'm', 'sh'];
77+
const fileEx = fileName.split('.').pop();
78+
if (extensions.includes(fileEx)) {
79+
switch (fileEx) {
80+
case 'v': return highlight(code, languages.verilog, 'verilog');
81+
case 'c': return highlight(code, languages.c, 'c');
82+
case 'h': return highlight(code, languages.c, 'c');
83+
case 'hpp': return highlight(code, languages.c, 'c');
84+
case 'cpp': return highlight(code, languages.cpp, 'cpp');
85+
case 'py': return highlight(code, languages.python, 'python');
86+
case 'm': return highlight(code, languages.matlab, 'matlab');
87+
case 'sh': return highlight(code, languages.bash, 'bash');
88+
default: return highlight(code, languages.plaintext);
89+
}
90+
}
91+
return highlight(code, languages.plaintext);
92+
}
93+
6594
return (
6695
<Modal
6796
ModelOpen={superState.textFileModal}
@@ -89,7 +118,7 @@ const FileEditModal = ({ superState, dispatcher }) => {
89118
<Editor
90119
value={codeStuff}
91120
onValueChange={(e) => setCodeStuff(e)}
92-
highlight={(code) => highlight(code, languages.js)}
121+
highlight={(code) => highlightSyntax(code)}
93122
padding={10}
94123
style={{
95124
fontFamily: '"Arial"',
@@ -100,9 +129,6 @@ const FileEditModal = ({ superState, dispatcher }) => {
100129
}}
101130
/>
102131
</div>
103-
<div className="footer">
104-
<button type="submit" className="btn btn-primary" onClick={submit}>Save</button>
105-
</div>
106132
</div>
107133
</Modal>
108134
);

0 commit comments

Comments
 (0)