Skip to content

Commit 0072116

Browse files
committed
refactor with useCodeHighlighting hook
1 parent d3c2c30 commit 0072116

3 files changed

Lines changed: 43 additions & 44 deletions

File tree

‎src/components/Copybook.js‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import React, {useEffect, useRef} from 'react';
22
import {Button, Dropdown, Modal} from 'semantic-ui-react';
33
import Loading from "./Loading";
4+
import useCodeHighlighting from './hooks/useCodeHighlighting';
45

56
export default function Copybook(props) {
6-
const codeEl = useRef(null);
7+
const codeEl = useCodeHighlighting([props.copybookFileContent, props.copybookVisible]);
78
const editorEl = useRef(null);
89

9-
useEffect(() => {
10-
renderPrettyPrint();
11-
}, [props.copybookFileContent, props.copybookVisible]);
12-
1310
function handleClose() {
1411
props.onCloseCopybook();
1512
}
@@ -22,18 +19,6 @@ export default function Copybook(props) {
2219
}
2320
}
2421

25-
function renderPrettyPrint() {
26-
setTimeout(() => {
27-
if (editorEl.current) {
28-
editorEl.current.innerHTML = '';
29-
}
30-
if (codeEl.current) {
31-
codeEl.current.classList.remove('prettyprinted');
32-
setTimeout(() => PR.prettyPrint(), 100);
33-
}
34-
}, codeEl.current ? 0 : 1000);
35-
}
36-
3722
function renderDropdownItem() {
3823
if (!props.copybookFileList) {
3924
return null;

‎src/components/SourceCode.js‎

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
1-
import React, {useEffect} from 'react';
1+
import React from 'react';
22
import {Modal, Button, Dropdown, Label} from 'semantic-ui-react';
33
import * as Tools from '../utils/Tools';
44
import Loading from "./Loading";
5+
import useCodeHighlighting from './hooks/useCodeHighlighting';
56

67
export default function SourceCode(props) {
7-
const codeEl = React.createRef();
8-
let mark = null;
9-
10-
useEffect(() => {
11-
renderPrettyPrint();
12-
}, [props.sourceCode, props.sourceCodeVisible]);
8+
const codeEl = useCodeHighlighting([props.sourceCode, props.sourceCodeVisible], props.sourceCodeVariable?.keyword);
139

1410
function handleClose() {
1511
props.onCloseSourceCode();
1612
}
1713

18-
function renderPrettyPrint() {
19-
setTimeout(() => {
20-
if (codeEl.current) {
21-
codeEl.current.classList.remove('prettyprinted');
22-
setTimeout(() => PR.prettyPrint(
23-
() => setTimeout(() =>renderHighLight(), 1000)
24-
), 100);
25-
}
26-
}, codeEl.current ? 0 : 1000);
27-
}
28-
29-
function renderHighLight() {
30-
if (mark) {mark.unmark()}
31-
mark = new Mark(codeEl.current);
32-
let idx = 0;
33-
mark.mark(props.sourceCodeVariable.keyword, {each: el => {
34-
el.setAttribute('tabindex',idx++);
35-
}});
36-
}
37-
3814
if (!props.sourceCodeVariable || !props.sourceCodeRepo) { return null; }
3915
const sourceCodeVariable = props.sourceCodeVariable;
4016
const dropText = (
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, {useEffect, useRef} from 'react';
2+
3+
export default function useCodeHighlighting(watchedProps, keyword) {
4+
const container = React.createRef(null);
5+
const mark = useRef(null);
6+
useEffect(() => {
7+
renderPrettyPrint();
8+
}, [...watchedProps]);
9+
10+
function renderPrettyPrint() {
11+
setTimeout(() => {
12+
if (container.current) {
13+
container.current.classList.remove('prettyprinted');
14+
setTimeout(() => PR.prettyPrint(
15+
() => setTimeout(() => renderHighLight(), 1000)
16+
), 100);
17+
}
18+
}, container.current ? 0 : 1000);
19+
}
20+
21+
function renderHighLight() {
22+
if (!keyword) {
23+
return;
24+
}
25+
if (mark.current) {
26+
mark.current.unmark()
27+
}
28+
mark.current = new Mark(container.current);
29+
let idx = 0;
30+
mark.current.mark(keyword, {
31+
each: el => {
32+
el.setAttribute('tabindex', idx++);
33+
}
34+
});
35+
}
36+
37+
return container;
38+
}

0 commit comments

Comments
 (0)