2021-05-09 03:20:26 +00:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react';
|
2021-05-03 08:10:23 +00:00
|
|
|
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';
|
2021-05-09 03:58:34 +00:00
|
|
|
import 'codemirror/mode/sql/sql';
|
2021-05-03 08:10:23 +00:00
|
|
|
import 'codemirror/addon/hint/show-hint';
|
2021-05-13 17:55:39 +00:00
|
|
|
import 'codemirror/addon/display/placeholder';
|
2021-05-03 08:10:23 +00:00
|
|
|
import 'codemirror/addon/search/match-highlighter';
|
2021-05-03 14:27:32 +00:00
|
|
|
import 'codemirror/addon/hint/show-hint.css';
|
2021-05-09 03:58:34 +00:00
|
|
|
import 'codemirror/theme/base16-light.css';
|
2021-09-15 15:40:59 +00:00
|
|
|
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';
|
2021-06-26 17:23:00 +00:00
|
|
|
import { resolveReferences } from '@/_helpers/utils';
|
2021-05-03 14:27:32 +00:00
|
|
|
|
2021-05-03 08:10:23 +00:00
|
|
|
export function CodeHinter({
|
2021-09-15 15:40:59 +00:00
|
|
|
initialValue,
|
|
|
|
|
onChange,
|
|
|
|
|
currentState,
|
|
|
|
|
mode,
|
|
|
|
|
theme,
|
|
|
|
|
lineNumbers,
|
|
|
|
|
className,
|
|
|
|
|
placeholder,
|
|
|
|
|
ignoreBraces,
|
|
|
|
|
enablePreview,
|
2021-08-21 04:09:04 +00:00
|
|
|
height,
|
|
|
|
|
minHeight,
|
2021-09-15 15:40:59 +00:00
|
|
|
lineWrapping,
|
2021-05-03 08:10:23 +00:00
|
|
|
}) {
|
2021-05-03 14:27:32 +00:00
|
|
|
const options = {
|
2021-05-09 03:58:34 +00:00
|
|
|
lineNumbers: lineNumbers,
|
2021-08-21 04:09:04 +00:00
|
|
|
lineWrapping: lineWrapping,
|
2021-05-03 14:27:32 +00:00
|
|
|
singleLine: true,
|
2021-05-09 03:58:34 +00:00
|
|
|
mode: mode || 'handlebars',
|
2021-05-03 14:27:32 +00:00
|
|
|
tabSize: 2,
|
2021-05-09 03:58:34 +00:00
|
|
|
theme: theme || 'default',
|
2021-05-03 14:27:32 +00:00
|
|
|
readOnly: false,
|
2021-05-13 17:55:39 +00:00
|
|
|
highlightSelectionMatches: true,
|
2021-09-15 15:40:59 +00:00
|
|
|
placeholder,
|
2021-05-03 08:10:23 +00:00
|
|
|
};
|
|
|
|
|
|
2021-05-09 03:20:26 +00:00
|
|
|
const [realState, setRealState] = useState(currentState);
|
2021-06-26 17:23:00 +00:00
|
|
|
const [currentValue, setCurrentValue] = useState(initialValue);
|
2021-09-15 15:40:59 +00:00
|
|
|
const [isFocused, setFocused] = useState(false);
|
2021-05-09 03:20:26 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setRealState(currentState);
|
|
|
|
|
}, [currentState.components]);
|
|
|
|
|
|
|
|
|
|
let suggestions = useMemo(() => {
|
2021-06-26 08:21:52 +00:00
|
|
|
return getSuggestionKeys(realState);
|
|
|
|
|
}, [realState.components, realState.queries]);
|
2021-05-03 16:04:54 +00:00
|
|
|
|
2021-06-26 17:23:00 +00:00
|
|
|
function valueChanged(editor, onChange, suggestions, ignoreBraces) {
|
|
|
|
|
handleChange(editor, onChange, suggestions, ignoreBraces);
|
|
|
|
|
setCurrentValue(editor.getValue());
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:40:59 +00:00
|
|
|
const getPreviewContent = (content, type) => {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'object':
|
|
|
|
|
return JSON.stringify(content);
|
|
|
|
|
case 'boolean':
|
2021-09-16 12:01:22 +00:00
|
|
|
return content.toString();
|
2021-09-15 15:40:59 +00:00
|
|
|
default:
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getPreview = () => {
|
2021-09-16 12:01:22 +00:00
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:40:59 +00:00
|
|
|
const previewType = typeof preview;
|
|
|
|
|
const content = getPreviewContent(preview, previewType);
|
|
|
|
|
|
|
|
|
|
return (
|
2021-09-16 12:01:22 +00:00
|
|
|
<div className="dynamic-variable-preview bg-green-lt px-1 py-1">
|
|
|
|
|
<div>
|
|
|
|
|
<div class="heading my-1">
|
|
|
|
|
<span>{previewType}</span>
|
|
|
|
|
</div>
|
|
|
|
|
{content}
|
2021-09-15 15:40:59 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-03 14:27:32 +00:00
|
|
|
return (
|
2021-09-15 15:40:59 +00:00
|
|
|
<div
|
|
|
|
|
className={`code-hinter ${className || 'codehinter-default-input'}`}
|
2021-08-21 04:09:04 +00:00
|
|
|
key={suggestions.length}
|
2021-08-31 14:10:11 +00:00
|
|
|
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}
|
2021-05-09 03:20:26 +00:00
|
|
|
realState={realState}
|
2021-05-03 14:27:32 +00:00
|
|
|
scrollbarStyle={null}
|
2021-08-21 04:42:04 +00:00
|
|
|
height={height}
|
2021-09-15 15:40:59 +00:00
|
|
|
onFocus={() => setFocused(true)}
|
|
|
|
|
onBlur={(editor) => {
|
2021-06-01 12:05:37 +00:00
|
|
|
const value = editor.getValue();
|
|
|
|
|
onChange(value);
|
2021-09-16 12:01:22 +00:00
|
|
|
setFocused(false);
|
2021-06-01 12:05:37 +00:00
|
|
|
}}
|
2021-06-26 17:23:00 +00:00
|
|
|
onChange={(editor) => valueChanged(editor, onChange, suggestions, ignoreBraces)}
|
2021-06-03 06:18:55 +00:00
|
|
|
onBeforeChange={(editor, change) => onBeforeChange(editor, change, ignoreBraces)}
|
2021-05-03 14:27:32 +00:00
|
|
|
options={options}
|
|
|
|
|
/>
|
2021-09-16 12:01:22 +00:00
|
|
|
{isFocused && enablePreview && getPreview()}
|
2021-05-03 14:27:32 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2021-05-09 03:20:26 +00:00
|
|
|
}
|