ToolJet/frontend/src/Editor/CodeBuilder/CodeHinter.jsx

127 lines
3.5 KiB
React
Raw Normal View History

import React, { useEffect, useMemo, useState } from 'react';
import CodeMirror from '@uiw/react-codemirror';
2021-05-03 11:26:31 +00:00
import 'codemirror/mode/handlebars/handlebars';
2021-05-09 04:25:17 +00:00
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/sql/sql';
import 'codemirror/addon/hint/show-hint';
import 'codemirror/addon/display/placeholder';
import 'codemirror/addon/search/match-highlighter';
2021-05-03 14:27:32 +00:00
import 'codemirror/addon/hint/show-hint.css';
import 'codemirror/theme/base16-light.css';
import 'codemirror/theme/duotone-light.css';
2021-07-03 17:07:50 +00:00
import 'codemirror/theme/monokai.css';
2021-05-03 16:04:54 +00:00
import { getSuggestionKeys, onBeforeChange, handleChange } from './utils';
import { resolveReferences } from '@/_helpers/utils';
2021-05-03 14:27:32 +00:00
export function CodeHinter({
initialValue,
onChange,
currentState,
mode,
theme,
lineNumbers,
className,
placeholder,
ignoreBraces,
enablePreview,
2021-08-21 04:09:04 +00:00
height,
minHeight,
lineWrapping,
}) {
2021-05-03 14:27:32 +00:00
const options = {
lineNumbers: lineNumbers,
2021-08-21 04:09:04 +00:00
lineWrapping: lineWrapping,
2021-05-03 14:27:32 +00:00
singleLine: true,
mode: mode || 'handlebars',
2021-05-03 14:27:32 +00:00
tabSize: 2,
theme: theme || 'default',
2021-05-03 14:27:32 +00:00
readOnly: false,
highlightSelectionMatches: true,
placeholder,
};
const [realState, setRealState] = useState(currentState);
const [currentValue, setCurrentValue] = useState(initialValue);
const [isFocused, setFocused] = useState(false);
useEffect(() => {
setRealState(currentState);
}, [currentState.components]);
let suggestions = useMemo(() => {
return getSuggestionKeys(realState);
}, [realState.components, realState.queries]);
2021-05-03 16:04:54 +00:00
function valueChanged(editor, onChange, suggestions, ignoreBraces) {
handleChange(editor, onChange, suggestions, ignoreBraces);
setCurrentValue(editor.getValue());
}
const getPreviewContent = (content, type) => {
switch (type) {
case 'object':
return JSON.stringify(content);
case 'boolean':
return content.toString();
default:
return content;
}
};
const getPreview = () => {
const [preview, error] = resolveReferences(currentValue, realState, null, {}, true);
if (error) {
return (
<div className="dynamic-variable-preview bg-red-lt px-1 py-1">
<div>
<div class="heading my-1">
<span>Error</span>
</div>
{error.toString()}
</div>
</div>
);
}
const previewType = typeof preview;
const content = getPreviewContent(preview, previewType);
return (
<div className="dynamic-variable-preview bg-green-lt px-1 py-1">
<div>
<div class="heading my-1">
<span>{previewType}</span>
</div>
{content}
</div>
</div>
);
};
2021-05-03 14:27:32 +00:00
return (
<div
className={`code-hinter ${className || 'codehinter-default-input'}`}
2021-08-21 04:09:04 +00:00
key={suggestions.length}
style={{ height: height || 'auto', minHeight, maxHeight: '320px', overflow: 'auto' }}
2021-08-21 04:09:04 +00:00
>
2021-05-03 14:27:32 +00:00
<CodeMirror
value={initialValue}
realState={realState}
2021-05-03 14:27:32 +00:00
scrollbarStyle={null}
2021-08-21 04:42:04 +00:00
height={height}
onFocus={() => setFocused(true)}
onBlur={(editor) => {
const value = editor.getValue();
onChange(value);
setFocused(false);
}}
onChange={(editor) => valueChanged(editor, onChange, suggestions, ignoreBraces)}
onBeforeChange={(editor, change) => onBeforeChange(editor, change, ignoreBraces)}
2021-05-03 14:27:32 +00:00
options={options}
/>
{isFocused && enablePreview && getPreview()}
2021-05-03 14:27:32 +00:00
</div>
);
}