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 03:58:34 +00:00
|
|
|
import 'codemirror/mode/sql/sql';
|
2021-05-03 08:10:23 +00:00
|
|
|
import 'codemirror/addon/hint/show-hint';
|
|
|
|
|
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';
|
|
|
|
|
import 'codemirror/theme/duotone-light.css';
|
2021-05-03 16:04:54 +00:00
|
|
|
import { getSuggestionKeys, onBeforeChange, handleChange } from './utils';
|
2021-05-03 14:27:32 +00:00
|
|
|
|
2021-05-03 08:10:23 +00:00
|
|
|
export function CodeHinter({
|
2021-05-09 03:58:34 +00:00
|
|
|
initialValue, onChange, currentState, mode, theme, lineNumbers, className
|
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-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,
|
|
|
|
|
highlightSelectionMatches: true
|
2021-05-03 08:10:23 +00:00
|
|
|
};
|
|
|
|
|
|
2021-05-09 03:20:26 +00:00
|
|
|
const [realState, setRealState] = useState(currentState);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setRealState(currentState);
|
|
|
|
|
}, [currentState.components]);
|
|
|
|
|
|
|
|
|
|
let suggestions = useMemo(() => {
|
|
|
|
|
return getSuggestionKeys(currentState);
|
|
|
|
|
}, [currentState.components, currentState.queries]);
|
2021-05-03 16:04:54 +00:00
|
|
|
|
2021-05-03 14:27:32 +00:00
|
|
|
return (
|
2021-05-09 03:58:34 +00:00
|
|
|
<div className={`code-hinter ${className || 'codehinter-default-input'}`}>
|
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-05-03 16:04:54 +00:00
|
|
|
onChange={(editor) => handleChange(editor, onChange, suggestions)}
|
2021-05-03 14:27:32 +00:00
|
|
|
onBeforeChange={(editor, change) => onBeforeChange(editor, change)}
|
|
|
|
|
options={options}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-05-09 03:20:26 +00:00
|
|
|
}
|