forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeEditor.jsx
More file actions
127 lines (116 loc) · 3.62 KB
/
CodeEditor.jsx
File metadata and controls
127 lines (116 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable import/no-unresolved */
import React, { useState, useEffect } from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { okaidia } from '@uiw/codemirror-theme-okaidia';
import { githubLight } from '@uiw/codemirror-theme-github';
import { javascript } from '@codemirror/lang-javascript';
import { python } from '@codemirror/lang-python';
import { sql } from '@codemirror/lang-sql';
import { sass } from '@codemirror/lang-sass';
import { debounce } from 'lodash';
import { useDynamicHeight } from '@/_hooks/useDynamicHeight';
import './codeEditor.scss';
const langSupport = Object.freeze({
javascript: javascript(),
python: python(),
sql: sql(),
jsx: javascript({ jsx: true }),
css: sass(),
});
export const CodeEditor = ({
id,
height,
darkMode,
properties,
styles,
setExposedVariable,
dataCy,
adjustComponentPositions,
currentLayout,
width,
}) => {
const { enableLineNumber, mode, placeholder, dynamicHeight } = properties;
const { visibility, disabledState } = styles;
const [forceDynamicHeightUpdate, setForceDynamicHeightUpdate] = useState(false);
const [value, setValue] = useState('');
useDynamicHeight({
dynamicHeight,
id,
height,
value: forceDynamicHeightUpdate,
adjustComponentPositions,
currentLayout,
width,
visibility,
});
const codeChanged = debounce((code) => {
setExposedVariable('value', code);
}, 500);
const editorStyles = {
height: dynamicHeight ? 'auto' : height,
display: !visibility ? 'none' : 'block',
};
const setupConfig = {
lineNumbers: enableLineNumber ?? true,
syntaxHighlighting: true,
bracketMatching: true,
foldGutter: true,
highlightActiveLine: false,
autocompletion: true,
highlightActiveLineGutter: false,
completionKeymap: true,
searchKeymap: false,
};
const theme = darkMode ? okaidia : githubLight;
const langExtention = langSupport?.[mode?.toLowerCase()];
const editorHeight = React.useMemo(() => {
return dynamicHeight ? 'auto' : height || 'auto';
}, [height, dynamicHeight]);
useEffect(() => {
const _setValue = (value) => {
if (typeof value === 'string') {
codeChanged(value);
setValue(value);
}
};
setExposedVariable('setValue', _setValue);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<div data-disabled={disabledState} style={editorStyles} data-cy={dataCy}>
<div
className={`code-hinter codehinter-default-input code-editor-widget scrollbar-container`}
style={{
height: dynamicHeight ? 'auto' : height || 'auto',
...(dynamicHeight
? { minHeight: '0', maxHeight: '100%' }
: { minHeight: height - 1, maxHeight: '320px', overflow: 'auto' }),
borderRadius: `${styles.borderRadius}px`,
boxShadow: styles.boxShadow,
}}
>
<CodeMirror
value={value}
placeholder={placeholder}
height={'100%'}
minHeight={dynamicHeight ? 'none' : editorHeight}
maxHeight={dynamicHeight ? 'none' : editorHeight}
width="100%"
theme={theme}
extensions={[langExtention ?? javascript()]}
onChange={(value) => {
setValue(value);
codeChanged(value);
setForceDynamicHeightUpdate(!forceDynamicHeightUpdate);
}}
basicSetup={setupConfig}
style={{
...(dynamicHeight ? {} : { overflowY: 'auto' }),
}}
className={`codehinter-multi-line-input code-editor-component`}
indentWithTab={true}
/>
</div>
</div>
);
};