2022-08-03 13:50:50 +00:00
|
|
|
import React, { useEffect, useState, useRef, useContext } from 'react';
|
2021-09-17 14:02:50 +00:00
|
|
|
import { useSpring, config, animated } from 'react-spring';
|
2021-12-10 03:09:23 +00:00
|
|
|
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
|
|
|
|
|
import Tooltip from 'react-bootstrap/Tooltip';
|
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';
|
2022-07-11 10:30:18 +00:00
|
|
|
import { onBeforeChange, handleChange } from './utils';
|
2022-02-15 06:49:22 +00:00
|
|
|
import { resolveReferences, hasCircularDependency, handleCircularStructureToJSON } from '@/_helpers/utils';
|
2021-09-17 14:02:50 +00:00
|
|
|
import useHeight from '@/_hooks/use-height-transition';
|
2021-12-10 03:09:23 +00:00
|
|
|
import usePortal from '@/_hooks/use-portal';
|
2022-02-01 14:16:21 +00:00
|
|
|
import { Color } from './Elements/Color';
|
|
|
|
|
import { Json } from './Elements/Json';
|
|
|
|
|
import { Select } from './Elements/Select';
|
|
|
|
|
import { Toggle } from './Elements/Toggle';
|
|
|
|
|
import { AlignButtons } from './Elements/AlignButtons';
|
|
|
|
|
import { TypeMapping } from './TypeMapping';
|
2022-02-07 03:43:12 +00:00
|
|
|
import { Number } from './Elements/Number';
|
2022-07-11 09:49:28 +00:00
|
|
|
import { BoxShadow } from './Elements/BoxShadow';
|
2022-02-01 14:16:21 +00:00
|
|
|
import FxButton from './Elements/FxButton';
|
2022-03-31 09:44:45 +00:00
|
|
|
import { ToolTip } from '../Inspector/Elements/Components/ToolTip';
|
2022-06-02 07:27:20 +00:00
|
|
|
import { toast } from 'react-hot-toast';
|
2022-08-03 13:50:50 +00:00
|
|
|
import { EditorContext } from '@/Editor/Context/EditorContextWrapper';
|
2022-09-14 08:04:49 +00:00
|
|
|
import { camelCase } from 'lodash';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-05-30 03:51:57 +00:00
|
|
|
import cx from 'classnames';
|
2023-07-19 06:12:28 +00:00
|
|
|
import { useCurrentState } from '@/_stores/currentStateStore';
|
2022-02-01 14:16:21 +00:00
|
|
|
|
|
|
|
|
const AllElements = {
|
|
|
|
|
Color,
|
|
|
|
|
Json,
|
|
|
|
|
Toggle,
|
|
|
|
|
Select,
|
|
|
|
|
AlignButtons,
|
2022-02-07 03:43:12 +00:00
|
|
|
Number,
|
2022-07-11 09:49:28 +00:00
|
|
|
BoxShadow,
|
2022-02-01 14:16:21 +00:00
|
|
|
};
|
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,
|
|
|
|
|
mode,
|
|
|
|
|
theme,
|
|
|
|
|
lineNumbers,
|
|
|
|
|
placeholder,
|
|
|
|
|
ignoreBraces,
|
|
|
|
|
enablePreview,
|
2021-08-21 04:09:04 +00:00
|
|
|
height,
|
|
|
|
|
minHeight,
|
2021-09-15 15:40:59 +00:00
|
|
|
lineWrapping,
|
2021-12-10 03:09:23 +00:00
|
|
|
componentName = null,
|
|
|
|
|
usePortalEditor = true,
|
2021-12-28 07:28:05 +00:00
|
|
|
className,
|
2022-02-07 10:38:42 +00:00
|
|
|
width = '',
|
2022-02-01 14:16:21 +00:00
|
|
|
paramName,
|
|
|
|
|
paramLabel,
|
|
|
|
|
type,
|
|
|
|
|
fieldMeta,
|
|
|
|
|
onFxPress,
|
|
|
|
|
fxActive,
|
2022-08-03 13:50:50 +00:00
|
|
|
component,
|
2022-11-17 08:39:44 +00:00
|
|
|
popOverCallback,
|
2022-11-11 05:45:12 +00:00
|
|
|
cyLabel = '',
|
2023-05-11 09:34:48 +00:00
|
|
|
callgpt = () => null,
|
2023-05-15 13:15:09 +00:00
|
|
|
isCopilotEnabled = false,
|
2023-07-20 11:05:39 +00:00
|
|
|
currentState: _currentState,
|
2021-05-03 08:10:23 +00:00
|
|
|
}) {
|
2021-12-15 04:23:17 +00:00
|
|
|
const darkMode = localStorage.getItem('darkMode') === 'true';
|
2021-05-03 14:27:32 +00:00
|
|
|
const options = {
|
2021-12-10 03:09:23 +00:00
|
|
|
lineNumbers: lineNumbers ?? false,
|
|
|
|
|
lineWrapping: lineWrapping ?? true,
|
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,
|
2022-02-07 12:43:28 +00:00
|
|
|
theme: theme ? theme : darkMode ? 'monokai' : '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
|
|
|
};
|
2023-07-19 06:12:28 +00:00
|
|
|
const currentState = useCurrentState();
|
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-09-17 14:02:50 +00:00
|
|
|
const [heightRef, currentHeight] = useHeight();
|
2022-06-02 07:27:20 +00:00
|
|
|
const isPreviewFocused = useRef(false);
|
|
|
|
|
const wrapperRef = useRef(null);
|
2021-09-17 14:02:50 +00:00
|
|
|
const slideInStyles = useSpring({
|
|
|
|
|
config: { ...config.stiff },
|
|
|
|
|
from: { opacity: 0, height: 0 },
|
|
|
|
|
to: {
|
|
|
|
|
opacity: isFocused ? 1 : 0,
|
|
|
|
|
height: isFocused ? currentHeight : 0,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-09-14 08:04:49 +00:00
|
|
|
const { t } = useTranslation();
|
2022-08-03 13:50:50 +00:00
|
|
|
const { variablesExposedForPreview } = useContext(EditorContext);
|
|
|
|
|
|
2022-06-02 07:27:20 +00:00
|
|
|
const prevCountRef = useRef(false);
|
|
|
|
|
|
2021-05-09 03:20:26 +00:00
|
|
|
useEffect(() => {
|
2023-07-20 11:05:39 +00:00
|
|
|
if (_currentState) {
|
|
|
|
|
setRealState(_currentState);
|
|
|
|
|
} else {
|
|
|
|
|
setRealState(currentState);
|
|
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-07-20 11:05:39 +00:00
|
|
|
}, [currentState.components, _currentState]);
|
2021-05-09 03:20:26 +00:00
|
|
|
|
2022-06-02 07:27:20 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const handleClickOutside = (event) => {
|
|
|
|
|
if (isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (wrapperRef.current && isFocused && !wrapperRef.current.contains(event.target) && prevCountRef.current) {
|
|
|
|
|
isPreviewFocused.current = false;
|
|
|
|
|
setFocused(false);
|
|
|
|
|
prevCountRef.current = false;
|
|
|
|
|
} else if (isFocused) {
|
|
|
|
|
prevCountRef.current = true;
|
|
|
|
|
} else if (!isFocused && prevCountRef.current) prevCountRef.current = false;
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('mousedown', handleClickOutside);
|
|
|
|
|
};
|
|
|
|
|
}, [wrapperRef, isFocused, isPreviewFocused, currentValue, prevCountRef, isOpen]);
|
|
|
|
|
|
2022-07-11 10:30:18 +00:00
|
|
|
function valueChanged(editor, onChange, ignoreBraces) {
|
2022-10-20 11:47:26 +00:00
|
|
|
if (editor.getValue()?.trim() !== currentValue) {
|
|
|
|
|
handleChange(editor, onChange, ignoreBraces, realState, componentName);
|
|
|
|
|
setCurrentValue(editor.getValue()?.trim());
|
|
|
|
|
}
|
2021-06-26 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:40:59 +00:00
|
|
|
const getPreviewContent = (content, type) => {
|
2022-05-20 08:33:43 +00:00
|
|
|
try {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'object':
|
|
|
|
|
return JSON.stringify(content);
|
|
|
|
|
case 'boolean':
|
|
|
|
|
return content.toString();
|
|
|
|
|
default:
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return undefined;
|
2021-09-15 15:40:59 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-02 07:27:20 +00:00
|
|
|
const focusPreview = () => (isPreviewFocused.current = true);
|
|
|
|
|
const unFocusPreview = () => (isPreviewFocused.current = false);
|
|
|
|
|
|
|
|
|
|
const copyToClipboard = (text) => {
|
|
|
|
|
navigator.clipboard.writeText(text);
|
|
|
|
|
toast.success('Copied to clipboard');
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-03 13:50:50 +00:00
|
|
|
const getCustomResolvables = () => {
|
|
|
|
|
if (variablesExposedForPreview.hasOwnProperty(component?.id)) {
|
|
|
|
|
if (component?.component?.component === 'Table' && fieldMeta?.name) {
|
|
|
|
|
return {
|
|
|
|
|
...variablesExposedForPreview[component?.id],
|
2023-03-02 06:20:27 +00:00
|
|
|
cellValue: variablesExposedForPreview[component?.id]?.rowData?.[fieldMeta?.name],
|
2022-08-03 13:50:50 +00:00
|
|
|
rowData: { ...variablesExposedForPreview[component?.id]?.rowData },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return variablesExposedForPreview[component.id];
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-15 15:40:59 +00:00
|
|
|
const getPreview = () => {
|
2022-05-10 09:39:09 +00:00
|
|
|
if (!enablePreview) return;
|
2022-08-03 13:50:50 +00:00
|
|
|
const customResolvables = getCustomResolvables();
|
2023-05-26 08:20:39 +00:00
|
|
|
const [preview, error] = resolveReferences(currentValue, realState, null, customResolvables, true, true);
|
2021-12-15 04:23:17 +00:00
|
|
|
const themeCls = darkMode ? 'bg-dark py-1' : 'bg-light py-1';
|
2021-09-16 12:01:22 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
2022-07-11 10:30:18 +00:00
|
|
|
const err = String(error);
|
|
|
|
|
const errorMessage = err.includes('.run()') ? `${err} in ${componentName.split('::')[0]}'s field` : err;
|
2021-09-16 12:01:22 +00:00
|
|
|
return (
|
2021-12-15 04:23:17 +00:00
|
|
|
<animated.div className={isOpen ? themeCls : null} style={{ ...slideInStyles, overflow: 'hidden' }}>
|
2021-09-17 14:02:50 +00:00
|
|
|
<div ref={heightRef} className="dynamic-variable-preview bg-red-lt px-1 py-1">
|
|
|
|
|
<div>
|
|
|
|
|
<div className="heading my-1">
|
|
|
|
|
<span>Error</span>
|
|
|
|
|
</div>
|
2022-07-11 10:30:18 +00:00
|
|
|
{errorMessage}
|
2021-09-16 12:01:22 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2021-09-17 14:02:50 +00:00
|
|
|
</animated.div>
|
2021-09-16 12:01:22 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 06:49:22 +00:00
|
|
|
let previewType = typeof preview;
|
|
|
|
|
let previewContent = preview;
|
|
|
|
|
|
|
|
|
|
if (hasCircularDependency(preview)) {
|
|
|
|
|
previewContent = JSON.stringify(preview, handleCircularStructureToJSON());
|
|
|
|
|
previewType = typeof previewContent;
|
|
|
|
|
}
|
|
|
|
|
const content = getPreviewContent(previewContent, previewType);
|
2021-09-15 15:40:59 +00:00
|
|
|
|
|
|
|
|
return (
|
2022-06-02 07:27:20 +00:00
|
|
|
<animated.div
|
|
|
|
|
className={isOpen ? themeCls : null}
|
|
|
|
|
style={{ ...slideInStyles, overflow: 'hidden' }}
|
|
|
|
|
onMouseEnter={() => focusPreview()}
|
|
|
|
|
onMouseLeave={() => unFocusPreview()}
|
|
|
|
|
>
|
2021-09-17 14:02:50 +00:00
|
|
|
<div ref={heightRef} className="dynamic-variable-preview bg-green-lt px-1 py-1">
|
|
|
|
|
<div>
|
2022-06-02 07:27:20 +00:00
|
|
|
<div className="d-flex my-1">
|
|
|
|
|
<div className="flex-grow-1" style={{ fontWeight: 700, textTransform: 'capitalize' }}>
|
|
|
|
|
{previewType}
|
|
|
|
|
</div>
|
|
|
|
|
{isFocused && (
|
2023-07-03 11:52:14 +00:00
|
|
|
<div className="preview-icons position-relative">
|
2022-06-02 07:27:20 +00:00
|
|
|
<CodeHinter.PopupIcon callback={() => copyToClipboard(content)} icon="copy" tip="Copy to clipboard" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2021-09-17 14:02:50 +00:00
|
|
|
</div>
|
|
|
|
|
{content}
|
2021-09-16 12:01:22 +00:00
|
|
|
</div>
|
2021-09-15 15:40:59 +00:00
|
|
|
</div>
|
2021-09-17 14:02:50 +00:00
|
|
|
</animated.div>
|
2021-09-15 15:40:59 +00:00
|
|
|
);
|
|
|
|
|
};
|
2021-11-15 06:18:09 +00:00
|
|
|
enablePreview = enablePreview ?? true;
|
2021-12-10 03:09:23 +00:00
|
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
const handleToggle = () => {
|
2022-11-17 08:39:44 +00:00
|
|
|
const changeOpen = (newOpen) => {
|
|
|
|
|
setIsOpen(newOpen);
|
|
|
|
|
if (typeof popOverCallback === 'function') popOverCallback(newOpen);
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-10 03:09:23 +00:00
|
|
|
if (!isOpen) {
|
2022-11-17 08:39:44 +00:00
|
|
|
changeOpen(true);
|
2021-12-10 03:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const element = document.getElementsByClassName('portal-container');
|
|
|
|
|
if (element) {
|
|
|
|
|
const checkPortalExits = element[0]?.classList.contains(componentName);
|
|
|
|
|
|
|
|
|
|
if (checkPortalExits === false) {
|
|
|
|
|
const parent = element[0].parentNode;
|
|
|
|
|
parent.removeChild(element[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 08:39:44 +00:00
|
|
|
changeOpen(false);
|
2021-12-10 03:09:23 +00:00
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}).then(() => {
|
2022-11-17 08:39:44 +00:00
|
|
|
changeOpen(true);
|
2021-12-10 03:09:23 +00:00
|
|
|
forceUpdate();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const [, forceUpdate] = React.useReducer((x) => x + 1, 0);
|
|
|
|
|
|
2022-05-10 09:39:09 +00:00
|
|
|
const defaultClassName =
|
|
|
|
|
className === 'query-hinter' || className === 'custom-component' || undefined ? '' : 'code-hinter';
|
2021-12-28 07:28:05 +00:00
|
|
|
|
2022-02-01 14:16:21 +00:00
|
|
|
const ElementToRender = AllElements[TypeMapping[type]];
|
|
|
|
|
|
|
|
|
|
const [forceCodeBox, setForceCodeBox] = useState(fxActive);
|
2022-02-07 03:43:12 +00:00
|
|
|
const codeShow = (type ?? 'code') === 'code' || forceCodeBox;
|
2023-02-14 10:10:59 +00:00
|
|
|
cyLabel = paramLabel ? paramLabel.toLowerCase().trim().replace(/\s+/g, '-') : cyLabel;
|
2022-02-07 03:43:12 +00:00
|
|
|
return (
|
2023-05-30 03:51:57 +00:00
|
|
|
<div ref={wrapperRef} className={cx({ 'codeShow-active': codeShow })}>
|
2022-07-11 10:30:18 +00:00
|
|
|
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
|
2022-04-07 11:33:55 +00:00
|
|
|
{paramLabel && (
|
2022-07-20 07:11:50 +00:00
|
|
|
<div className={`mb-2 field ${options.className}`} data-cy={`${cyLabel}-widget-parameter-label`}>
|
2022-09-12 08:47:04 +00:00
|
|
|
<ToolTip
|
2022-09-14 08:04:49 +00:00
|
|
|
label={t(`widget.commonProperties.${camelCase(paramLabel)}`, paramLabel)}
|
2022-09-12 08:47:04 +00:00
|
|
|
meta={fieldMeta}
|
|
|
|
|
labelClass={`form-label ${darkMode && 'color-whitish-darkmode'}`}
|
|
|
|
|
/>
|
2022-04-07 11:33:55 +00:00
|
|
|
</div>
|
|
|
|
|
)}
|
2022-03-31 09:44:45 +00:00
|
|
|
<div className={`col-auto ${(type ?? 'code') === 'code' ? 'd-none' : ''} `}>
|
|
|
|
|
<div style={{ width: width, display: codeShow ? 'flex' : 'none', marginTop: '-1px' }}>
|
|
|
|
|
<FxButton
|
|
|
|
|
active={true}
|
|
|
|
|
onPress={() => {
|
|
|
|
|
setForceCodeBox(false);
|
|
|
|
|
onFxPress(false);
|
|
|
|
|
}}
|
2022-07-20 07:11:50 +00:00
|
|
|
dataCy={cyLabel}
|
2022-03-31 09:44:45 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2022-02-07 12:43:28 +00:00
|
|
|
<div
|
2023-05-30 03:51:57 +00:00
|
|
|
className={`row${height === '150px' || height === '300px' ? ' tablr-gutter-x-0' : ''} custom-row`}
|
2022-07-11 10:30:18 +00:00
|
|
|
style={{ width: width, display: codeShow ? 'flex' : 'none' }}
|
2022-02-07 12:43:28 +00:00
|
|
|
>
|
2023-08-09 12:31:48 +00:00
|
|
|
<div className={`col code-hinter-col`}>
|
2023-07-03 11:52:14 +00:00
|
|
|
<div
|
|
|
|
|
className="code-hinter-wrapper position-relative"
|
|
|
|
|
style={{ width: '100%', backgroundColor: darkMode && '#272822' }}
|
|
|
|
|
>
|
2022-02-07 03:43:12 +00:00
|
|
|
<div
|
|
|
|
|
className={`${defaultClassName} ${className || 'codehinter-default-input'}`}
|
2022-07-11 10:30:18 +00:00
|
|
|
key={componentName}
|
2022-02-07 03:43:12 +00:00
|
|
|
style={{
|
|
|
|
|
height: height || 'auto',
|
|
|
|
|
minHeight,
|
|
|
|
|
maxHeight: '320px',
|
|
|
|
|
overflow: 'auto',
|
|
|
|
|
fontSize: ' .875rem',
|
|
|
|
|
}}
|
2022-07-20 07:11:50 +00:00
|
|
|
data-cy={`${cyLabel}-input-field`}
|
2022-02-01 14:16:21 +00:00
|
|
|
>
|
2022-06-02 07:27:20 +00:00
|
|
|
{usePortalEditor && (
|
|
|
|
|
<CodeHinter.PopupIcon
|
|
|
|
|
callback={handleToggle}
|
|
|
|
|
icon="portal-open"
|
|
|
|
|
tip="Pop out code editor into a new window"
|
2023-05-11 09:34:48 +00:00
|
|
|
transformation={componentName === 'transformation'}
|
2022-06-02 07:27:20 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
2022-02-07 03:43:12 +00:00
|
|
|
<CodeHinter.Portal
|
2023-05-15 13:15:09 +00:00
|
|
|
isCopilotEnabled={isCopilotEnabled}
|
2022-02-07 03:43:12 +00:00
|
|
|
isOpen={isOpen}
|
|
|
|
|
callback={setIsOpen}
|
|
|
|
|
componentName={componentName}
|
2022-07-11 10:30:18 +00:00
|
|
|
key={componentName}
|
2022-02-07 03:43:12 +00:00
|
|
|
customComponent={getPreview}
|
|
|
|
|
forceUpdate={forceUpdate}
|
|
|
|
|
optionalProps={{ styles: { height: 300 }, cls: className }}
|
|
|
|
|
darkMode={darkMode}
|
|
|
|
|
selectors={{ className: 'preview-block-portal' }}
|
2022-06-08 06:48:47 +00:00
|
|
|
dragResizePortal={true}
|
2023-05-11 09:34:48 +00:00
|
|
|
callgpt={callgpt}
|
2022-02-07 03:43:12 +00:00
|
|
|
>
|
|
|
|
|
<CodeMirror
|
|
|
|
|
value={typeof initialValue === 'string' ? initialValue : ''}
|
|
|
|
|
realState={realState}
|
|
|
|
|
scrollbarStyle={null}
|
2022-02-14 13:33:47 +00:00
|
|
|
height={'100%'}
|
2022-02-07 03:43:12 +00:00
|
|
|
onFocus={() => setFocused(true)}
|
2023-02-23 06:38:09 +00:00
|
|
|
onBlur={(editor, e) => {
|
|
|
|
|
e.stopPropagation();
|
2022-10-27 10:21:26 +00:00
|
|
|
const value = editor.getValue()?.trimEnd();
|
2022-02-07 03:43:12 +00:00
|
|
|
onChange(value);
|
2022-06-02 07:27:20 +00:00
|
|
|
if (!isPreviewFocused.current) {
|
|
|
|
|
setFocused(false);
|
|
|
|
|
}
|
2022-02-07 03:43:12 +00:00
|
|
|
}}
|
2022-07-11 10:30:18 +00:00
|
|
|
onChange={(editor) => valueChanged(editor, onChange, ignoreBraces)}
|
2022-02-07 03:43:12 +00:00
|
|
|
onBeforeChange={(editor, change) => onBeforeChange(editor, change, ignoreBraces)}
|
|
|
|
|
options={options}
|
|
|
|
|
viewportMargin={Infinity}
|
|
|
|
|
/>
|
|
|
|
|
</CodeHinter.Portal>
|
|
|
|
|
</div>
|
|
|
|
|
{enablePreview && !isOpen && getPreview()}
|
2022-02-01 14:16:21 +00:00
|
|
|
</div>
|
2022-02-07 03:43:12 +00:00
|
|
|
</div>
|
2022-02-01 14:16:21 +00:00
|
|
|
</div>
|
2022-02-07 03:43:12 +00:00
|
|
|
{!codeShow && (
|
|
|
|
|
<div style={{ display: !codeShow ? 'block' : 'none' }}>
|
|
|
|
|
<ElementToRender
|
|
|
|
|
value={resolveReferences(initialValue, realState)}
|
2022-10-20 11:47:26 +00:00
|
|
|
onChange={(value) => {
|
|
|
|
|
if (value !== currentValue) {
|
|
|
|
|
onChange(value);
|
2022-10-27 10:04:01 +00:00
|
|
|
setCurrentValue(value);
|
2022-10-20 11:47:26 +00:00
|
|
|
}
|
|
|
|
|
}}
|
2022-02-07 03:43:12 +00:00
|
|
|
paramName={paramName}
|
|
|
|
|
paramLabel={paramLabel}
|
|
|
|
|
forceCodeBox={() => {
|
|
|
|
|
setForceCodeBox(true);
|
|
|
|
|
onFxPress(true);
|
|
|
|
|
}}
|
|
|
|
|
meta={fieldMeta}
|
2022-07-20 07:11:50 +00:00
|
|
|
cyLabel={cyLabel}
|
2022-02-07 03:43:12 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2022-06-02 07:27:20 +00:00
|
|
|
</div>
|
2021-05-03 14:27:32 +00:00
|
|
|
);
|
2021-05-09 03:20:26 +00:00
|
|
|
}
|
2021-12-10 03:09:23 +00:00
|
|
|
|
2023-05-11 09:34:48 +00:00
|
|
|
const PopupIcon = ({ callback, icon, tip, transformation = false }) => {
|
|
|
|
|
const size = transformation ? 20 : 12;
|
|
|
|
|
|
2021-12-10 03:09:23 +00:00
|
|
|
return (
|
2023-07-03 11:52:14 +00:00
|
|
|
<div className="d-flex justify-content-end w-100 position-absolute" style={{ top: 0 }}>
|
2021-12-10 03:09:23 +00:00
|
|
|
<OverlayTrigger
|
|
|
|
|
trigger={['hover', 'focus']}
|
|
|
|
|
placement="top"
|
|
|
|
|
delay={{ show: 800, hide: 100 }}
|
2022-06-02 07:27:20 +00:00
|
|
|
overlay={<Tooltip id="button-tooltip">{tip}</Tooltip>}
|
2021-12-10 03:09:23 +00:00
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
className="svg-icon m-2 popup-btn"
|
2022-08-27 16:28:24 +00:00
|
|
|
src={`assets/images/icons/${icon}.svg`}
|
2023-05-11 09:34:48 +00:00
|
|
|
width={size}
|
|
|
|
|
height={size}
|
2021-12-10 03:09:23 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
callback();
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</OverlayTrigger>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Portal = ({ children, ...restProps }) => {
|
|
|
|
|
const renderPortal = usePortal({ children, ...restProps });
|
|
|
|
|
|
|
|
|
|
return <React.Fragment>{renderPortal}</React.Fragment>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CodeHinter.PopupIcon = PopupIcon;
|
|
|
|
|
CodeHinter.Portal = Portal;
|