2021-12-14 11:16:12 +00:00
|
|
|
import React from 'react';
|
2021-11-15 06:42:02 +00:00
|
|
|
import CodeMirror from '@uiw/react-codemirror';
|
|
|
|
|
import 'codemirror/addon/comment/comment';
|
|
|
|
|
import 'codemirror/addon/hint/show-hint';
|
|
|
|
|
import 'codemirror/addon/display/placeholder';
|
|
|
|
|
import 'codemirror/addon/search/match-highlighter';
|
|
|
|
|
import 'codemirror/addon/hint/show-hint.css';
|
|
|
|
|
import 'codemirror/theme/base16-light.css';
|
|
|
|
|
import 'codemirror/theme/duotone-light.css';
|
|
|
|
|
import 'codemirror/theme/monokai.css';
|
|
|
|
|
import { onBeforeChange, handleChange } from '../CodeBuilder/utils';
|
|
|
|
|
|
2021-12-14 11:16:12 +00:00
|
|
|
export const CodeEditor = ({ height, darkMode, properties, styles, exposedVariables, setExposedVariable }) => {
|
|
|
|
|
const { enableLineNumber, mode, placeholder } = properties;
|
|
|
|
|
const { visibility, disabledState } = styles;
|
2021-11-15 06:42:02 +00:00
|
|
|
|
|
|
|
|
function codeChanged(code) {
|
2021-12-14 11:16:12 +00:00
|
|
|
setExposedVariable('value', code);
|
2021-11-15 06:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 11:16:12 +00:00
|
|
|
const editorStyles = {
|
2021-11-15 06:42:02 +00:00
|
|
|
height: height,
|
2021-12-14 11:16:12 +00:00
|
|
|
display: !visibility ? 'none' : 'block',
|
2021-11-15 06:42:02 +00:00
|
|
|
};
|
|
|
|
|
const options = {
|
2021-12-14 11:16:12 +00:00
|
|
|
lineNumbers: enableLineNumber,
|
2021-11-15 06:42:02 +00:00
|
|
|
lineWrapping: true,
|
|
|
|
|
singleLine: true,
|
2021-12-14 11:16:12 +00:00
|
|
|
mode: mode,
|
2021-11-15 06:42:02 +00:00
|
|
|
tabSize: 2,
|
|
|
|
|
theme: darkMode ? 'monokai' : 'duotone-light',
|
|
|
|
|
readOnly: false,
|
|
|
|
|
highlightSelectionMatches: true,
|
|
|
|
|
placeholder,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function valueChanged(editor, onChange, ignoreBraces = false) {
|
|
|
|
|
handleChange(editor, onChange, [], ignoreBraces);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2021-12-14 11:16:12 +00:00
|
|
|
<div data-disabled={disabledState} style={editorStyles}>
|
2021-11-15 06:42:02 +00:00
|
|
|
<div
|
|
|
|
|
className={`code-hinter codehinter-default-input code-editor-widget`}
|
2021-12-10 03:13:05 +00:00
|
|
|
style={{
|
|
|
|
|
height: height || 'auto',
|
|
|
|
|
minHeight: height - 1,
|
|
|
|
|
maxHeight: '320px',
|
|
|
|
|
overflow: 'auto',
|
2022-01-29 01:36:08 +00:00
|
|
|
borderRadius: `${styles.borderRadius}px`,
|
2021-12-10 03:13:05 +00:00
|
|
|
}}
|
2021-11-15 06:42:02 +00:00
|
|
|
>
|
|
|
|
|
<CodeMirror
|
2021-12-14 11:16:12 +00:00
|
|
|
value={exposedVariables.value}
|
2021-11-15 06:42:02 +00:00
|
|
|
scrollbarStyle={null}
|
|
|
|
|
height={height - 1}
|
|
|
|
|
onBlur={(editor) => {
|
|
|
|
|
const value = editor.getValue();
|
|
|
|
|
codeChanged(value);
|
|
|
|
|
}}
|
|
|
|
|
onChange={(editor) => valueChanged(editor, codeChanged)}
|
|
|
|
|
onBeforeChange={(editor, change) => onBeforeChange(editor, change)}
|
|
|
|
|
options={options}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|