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-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-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({
|
|
|
|
|
initialValue, onChange, currentState
|
|
|
|
|
}) {
|
2021-05-03 14:27:32 +00:00
|
|
|
const options = {
|
|
|
|
|
lineNumbers: false,
|
|
|
|
|
singleLine: true,
|
|
|
|
|
mode: 'handlebars',
|
|
|
|
|
tabSize: 2,
|
|
|
|
|
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 (
|
|
|
|
|
<div className="code-hinter form-control">
|
|
|
|
|
<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
|
|
|
}
|