2024-10-22 08:01:21 +00:00
|
|
|
/* eslint-disable import/no-unresolved */
|
2025-02-25 06:52:50 +00:00
|
|
|
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
2024-10-22 08:01:21 +00:00
|
|
|
import { PreviewBox } from './PreviewBox';
|
|
|
|
|
import { ToolTip } from '@/Editor/Inspector/Elements/Components/ToolTip';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { camelCase, isEmpty, noop } from 'lodash';
|
|
|
|
|
import CodeMirror from '@uiw/react-codemirror';
|
|
|
|
|
import { javascript } from '@codemirror/lang-javascript';
|
|
|
|
|
import { autocompletion, completionKeymap, completionStatus, acceptCompletion } from '@codemirror/autocomplete';
|
|
|
|
|
import { defaultKeymap } from '@codemirror/commands';
|
|
|
|
|
import { keymap } from '@codemirror/view';
|
|
|
|
|
import FxButton from '../CodeBuilder/Elements/FxButton';
|
|
|
|
|
import cx from 'classnames';
|
|
|
|
|
import { DynamicFxTypeRenderer } from './DynamicFxTypeRenderer';
|
|
|
|
|
import { resolveReferences } from './utils';
|
|
|
|
|
import { okaidia } from '@uiw/codemirror-theme-okaidia';
|
|
|
|
|
import { githubLight } from '@uiw/codemirror-theme-github';
|
|
|
|
|
import { getAutocompletion } from './autocompleteExtensionConfig';
|
|
|
|
|
import ErrorBoundary from '@/_ui/ErrorBoundary';
|
|
|
|
|
import CodeHinter from './CodeHinter';
|
|
|
|
|
// import { EditorContext } from '../Context/EditorContextWrapper';
|
|
|
|
|
import { removeNestedDoubleCurlyBraces } from '@/_helpers/utils';
|
|
|
|
|
import useStore from '@/AppBuilder/_stores/store';
|
|
|
|
|
import { shallow } from 'zustand/shallow';
|
|
|
|
|
|
|
|
|
|
const SingleLineCodeEditor = ({ componentName, fieldMeta = {}, componentId, ...restProps }) => {
|
|
|
|
|
const { initialValue, onChange, enablePreview = true, portalProps } = restProps;
|
|
|
|
|
const { validation = {} } = fieldMeta;
|
2025-02-25 06:52:50 +00:00
|
|
|
const [showPreview, setShowPreview] = useState(false);
|
2024-10-22 08:01:21 +00:00
|
|
|
const [isFocused, setIsFocused] = useState(false);
|
|
|
|
|
const [currentValue, setCurrentValue] = useState('');
|
|
|
|
|
const [errorStateActive, setErrorStateActive] = useState(false);
|
|
|
|
|
const [cursorInsidePreview, setCursorInsidePreview] = useState(false);
|
2025-02-25 06:52:50 +00:00
|
|
|
const validationFn = restProps?.validationFn;
|
2024-11-07 16:10:41 +00:00
|
|
|
const componentDefinition = useStore((state) => state.getComponentDefinition(componentId), shallow);
|
|
|
|
|
const parentId = componentDefinition?.component?.parent;
|
|
|
|
|
const customResolvables = useStore((state) => state.resolvedStore.modules.canvas?.customResolvables, shallow);
|
|
|
|
|
|
|
|
|
|
const customVariables = customResolvables?.[parentId]?.[0] || {};
|
|
|
|
|
|
2024-10-22 08:01:21 +00:00
|
|
|
const isPreviewFocused = useRef(false);
|
|
|
|
|
const wrapperRef = useRef(null);
|
|
|
|
|
|
|
|
|
|
const replaceIdsWithName = useStore((state) => state.replaceIdsWithName, shallow);
|
|
|
|
|
let newInitialValue = initialValue;
|
|
|
|
|
|
|
|
|
|
if (typeof initialValue === 'string' && (initialValue?.includes('components') || initialValue?.includes('queries'))) {
|
|
|
|
|
newInitialValue = replaceIdsWithName(initialValue);
|
|
|
|
|
}
|
|
|
|
|
//! Re render the component when the componentName changes as the initialValue is not updated
|
|
|
|
|
|
|
|
|
|
// const { variablesExposedForPreview } = useContext(EditorContext) || {};
|
|
|
|
|
|
|
|
|
|
// const customVariables = variablesExposedForPreview?.[componentId] ?? {};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (typeof newInitialValue !== 'string') return;
|
|
|
|
|
|
|
|
|
|
// const [valid, _error] = !isEmpty(validation)
|
|
|
|
|
// ? resolveReferences(newInitialValue, validation, customVariables)
|
|
|
|
|
// : [true, null];
|
|
|
|
|
|
2024-11-07 16:10:41 +00:00
|
|
|
//!TODO use the updated new resolver
|
2025-02-25 06:52:50 +00:00
|
|
|
const [valid, _error] =
|
|
|
|
|
!isEmpty(validation) || validationFn
|
|
|
|
|
? resolveReferences(newInitialValue, validation, customVariables, validationFn)
|
|
|
|
|
: [true, null];
|
|
|
|
|
|
|
|
|
|
setErrorStateActive(!valid);
|
2024-10-22 08:01:21 +00:00
|
|
|
|
|
|
|
|
setCurrentValue(newInitialValue);
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2025-02-25 06:52:50 +00:00
|
|
|
}, [componentName, newInitialValue, validationFn]);
|
2024-10-22 08:01:21 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleClickOutside = (event) => {
|
|
|
|
|
if (cursorInsidePreview || portalProps?.isOpen || event.target.closest('.cm-tooltip-autocomplete')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wrapperRef.current && isFocused && !wrapperRef.current.contains(event.target)) {
|
|
|
|
|
isPreviewFocused.current = false;
|
|
|
|
|
setIsFocused(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('mousedown', handleClickOutside);
|
|
|
|
|
};
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [wrapperRef, isFocused, isPreviewFocused, currentValue, portalProps?.isOpen, cursorInsidePreview]);
|
|
|
|
|
|
|
|
|
|
const isWorkspaceVariable =
|
|
|
|
|
typeof currentValue === 'string' && (currentValue.includes('%%client') || currentValue.includes('%%server'));
|
|
|
|
|
|
2025-02-25 06:52:50 +00:00
|
|
|
const previewRef = useRef(null);
|
|
|
|
|
|
2024-10-22 08:01:21 +00:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
ref={wrapperRef}
|
|
|
|
|
className="code-hinter-wrapper position-relative"
|
|
|
|
|
style={{ width: '100%', height: restProps?.lang === 'jsx' && '320px' }}
|
|
|
|
|
>
|
|
|
|
|
<PreviewBox.Container
|
2025-02-25 06:52:50 +00:00
|
|
|
previewRef={previewRef}
|
|
|
|
|
showPreview={showPreview}
|
2024-11-07 16:10:41 +00:00
|
|
|
customVariables={customVariables}
|
2024-10-22 08:01:21 +00:00
|
|
|
enablePreview={enablePreview}
|
|
|
|
|
currentValue={currentValue}
|
|
|
|
|
isFocused={isFocused}
|
|
|
|
|
setCursorInsidePreview={setCursorInsidePreview}
|
|
|
|
|
componentName={componentName}
|
|
|
|
|
validationSchema={validation}
|
|
|
|
|
setErrorStateActive={setErrorStateActive}
|
|
|
|
|
ignoreValidation={restProps?.ignoreValidation || isEmpty(validation)}
|
|
|
|
|
componentId={restProps?.componentId ?? null}
|
|
|
|
|
isWorkspaceVariable={isWorkspaceVariable}
|
|
|
|
|
errorStateActive={errorStateActive}
|
|
|
|
|
previewPlacement={restProps?.cyLabel === 'canvas-bg-colour' ? 'top' : 'left-start'}
|
|
|
|
|
isPortalOpen={restProps?.portalProps?.isOpen}
|
2025-02-25 06:52:50 +00:00
|
|
|
validationFn={validationFn}
|
2024-10-22 08:01:21 +00:00
|
|
|
>
|
|
|
|
|
<div className="code-editor-basic-wrapper d-flex">
|
|
|
|
|
<div className="codehinter-container w-100">
|
|
|
|
|
<SingleLineCodeEditor.Editor
|
2025-02-25 06:52:50 +00:00
|
|
|
previewRef={previewRef}
|
2024-10-22 08:01:21 +00:00
|
|
|
currentValue={currentValue}
|
|
|
|
|
setCurrentValue={setCurrentValue}
|
|
|
|
|
isFocused={isFocused}
|
|
|
|
|
setFocus={setIsFocused}
|
|
|
|
|
validationType={validation?.schema?.type}
|
|
|
|
|
onBlurUpdate={onChange}
|
|
|
|
|
error={errorStateActive}
|
|
|
|
|
cyLabel={restProps.cyLabel}
|
|
|
|
|
portalProps={portalProps}
|
|
|
|
|
componentName={componentName}
|
2025-02-25 06:52:50 +00:00
|
|
|
setShowPreview={setShowPreview}
|
|
|
|
|
showPreview={showPreview}
|
2024-10-22 08:01:21 +00:00
|
|
|
{...restProps}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</PreviewBox.Container>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const EditorInput = ({
|
|
|
|
|
currentValue,
|
|
|
|
|
setCurrentValue,
|
|
|
|
|
setFocus,
|
|
|
|
|
validationType,
|
|
|
|
|
onBlurUpdate,
|
|
|
|
|
placeholder = '',
|
|
|
|
|
error,
|
|
|
|
|
cyLabel = '',
|
|
|
|
|
componentName,
|
|
|
|
|
usePortalEditor = true,
|
|
|
|
|
renderPreview,
|
|
|
|
|
portalProps,
|
|
|
|
|
lang,
|
|
|
|
|
isFocused,
|
|
|
|
|
componentId,
|
|
|
|
|
type,
|
|
|
|
|
delayOnChange = true, // Added this prop to immediately update the onBlurUpdate callback
|
|
|
|
|
paramLabel = '',
|
|
|
|
|
disabled = false,
|
2025-02-25 06:52:50 +00:00
|
|
|
previewRef,
|
|
|
|
|
setShowPreview,
|
|
|
|
|
onInputChange,
|
2024-10-22 08:01:21 +00:00
|
|
|
}) => {
|
|
|
|
|
const getSuggestions = useStore((state) => state.getSuggestions, shallow);
|
|
|
|
|
function autoCompleteExtensionConfig(context) {
|
|
|
|
|
const hints = getSuggestions();
|
|
|
|
|
let word = context.matchBefore(/\w*/);
|
|
|
|
|
|
|
|
|
|
const totalReferences = (context.state.doc.toString().match(/{{/g) || []).length;
|
|
|
|
|
|
|
|
|
|
let queryInput = context.state.doc.toString();
|
|
|
|
|
const originalQueryInput = queryInput;
|
|
|
|
|
|
|
|
|
|
if (totalReferences > 0) {
|
|
|
|
|
const currentCursor = context.state.selection.main.head;
|
|
|
|
|
const currentCursorPos = context.pos;
|
|
|
|
|
|
|
|
|
|
let currentWord = queryInput.substring(currentCursor, currentCursorPos);
|
|
|
|
|
|
|
|
|
|
if (currentWord?.length === 0) {
|
|
|
|
|
const lastBracesFromPos = queryInput.lastIndexOf('{{', currentCursorPos);
|
|
|
|
|
currentWord = queryInput.substring(lastBracesFromPos, currentCursorPos);
|
|
|
|
|
//remove curly braces from the current word as will append it later
|
|
|
|
|
currentWord = removeNestedDoubleCurlyBraces(currentWord);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentWord.includes(' ')) {
|
|
|
|
|
currentWord = currentWord.split(' ').pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove \n from the current word if it is present
|
|
|
|
|
currentWord = currentWord.replace(/\n/g, '');
|
|
|
|
|
|
|
|
|
|
queryInput = '{{' + currentWord + '}}';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let completions = getAutocompletion(queryInput, validationType, hints, totalReferences, originalQueryInput);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
from: word.from,
|
|
|
|
|
options: completions,
|
|
|
|
|
validFor: /^\{\{.*\}\}$/,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
const overRideFunction = React.useCallback((context) => autoCompleteExtensionConfig(context), []);
|
|
|
|
|
|
|
|
|
|
const autoCompleteConfig = autocompletion({
|
|
|
|
|
override: [overRideFunction],
|
|
|
|
|
compareCompletions: (a, b) => {
|
|
|
|
|
return a.section.rank - b.section.rank && a.label.localeCompare(b.label);
|
|
|
|
|
},
|
|
|
|
|
aboveCursor: false,
|
|
|
|
|
defaultKeymap: true,
|
|
|
|
|
positionInfo: () => {
|
|
|
|
|
return {
|
|
|
|
|
class: 'cm-completionInfo-top cm-custom-completion-info',
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
maxRenderedOptions: 10,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const customKeyMaps = [...defaultKeymap, ...completionKeymap];
|
|
|
|
|
const customTabKeymap = keymap.of([
|
|
|
|
|
{
|
|
|
|
|
key: 'Tab',
|
|
|
|
|
run: (view) => {
|
|
|
|
|
if (completionStatus(view.state)) {
|
|
|
|
|
return acceptCompletion(view);
|
|
|
|
|
}
|
|
|
|
|
if (isOpen) {
|
|
|
|
|
const { state } = view;
|
|
|
|
|
const { selection } = state;
|
|
|
|
|
const { anchor } = selection.main;
|
|
|
|
|
const tabSize = 2;
|
|
|
|
|
|
|
|
|
|
view?.dispatch({
|
|
|
|
|
changes: { from: anchor, insert: ' '.repeat(tabSize) },
|
|
|
|
|
selection: { anchor: anchor + tabSize },
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const handleOnChange = React.useCallback((val) => {
|
|
|
|
|
setCurrentValue(val);
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleOnBlur = () => {
|
2025-02-25 06:52:50 +00:00
|
|
|
setShowPreview(false);
|
2024-10-22 08:01:21 +00:00
|
|
|
if (!delayOnChange) {
|
|
|
|
|
setFirstTimeFocus(false);
|
|
|
|
|
return onBlurUpdate(currentValue);
|
|
|
|
|
}
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setFirstTimeFocus(false);
|
|
|
|
|
onBlurUpdate(currentValue);
|
|
|
|
|
}, 0);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const darkMode = localStorage.getItem('darkMode') === 'true';
|
|
|
|
|
const theme = darkMode ? okaidia : githubLight;
|
|
|
|
|
|
|
|
|
|
const { handleTogglePopupExapand, isOpen, setIsOpen, forceUpdate } = portalProps;
|
2025-02-25 06:52:50 +00:00
|
|
|
// when full screen editor is closed, show the preview box
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isFocused && !isOpen) {
|
|
|
|
|
setShowPreview(true);
|
|
|
|
|
}
|
|
|
|
|
}, [isOpen, isFocused]);
|
2024-10-22 08:01:21 +00:00
|
|
|
|
|
|
|
|
const [firstTimeFocus, setFirstTimeFocus] = useState(false);
|
2025-02-25 06:52:50 +00:00
|
|
|
const currentEditorHeightRef = useRef(null);
|
|
|
|
|
const isInsideQueryPane = !!currentEditorHeightRef?.current?.closest('.query-details');
|
|
|
|
|
const showLineNumbers = lang == 'jsx' || type === 'extendedSingleLine' || false;
|
2024-10-22 08:01:21 +00:00
|
|
|
|
|
|
|
|
const customClassNames = cx('codehinter-input', {
|
|
|
|
|
'border-danger': error,
|
|
|
|
|
focused: isFocused,
|
|
|
|
|
'focus-box-shadow-active': firstTimeFocus,
|
|
|
|
|
'widget-code-editor': componentId,
|
|
|
|
|
'disabled-pointerevents': disabled,
|
2025-02-25 06:52:50 +00:00
|
|
|
'code-editor-query-panel': isInsideQueryPane,
|
|
|
|
|
'show-line-numbers': showLineNumbers,
|
2024-10-22 08:01:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleFocus = () => {
|
|
|
|
|
setFirstTimeFocus(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setFocus(true);
|
|
|
|
|
}, 50);
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-25 06:52:50 +00:00
|
|
|
// in query panel we are allowing code editor to have dynamic height, this observer is to show/hide preview box based on the visibility of the editor
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isInsideQueryPane) return;
|
|
|
|
|
const observer = new IntersectionObserver(
|
|
|
|
|
([entry]) => {
|
|
|
|
|
if (entry.isIntersecting && isFocused) {
|
|
|
|
|
setShowPreview(true);
|
|
|
|
|
} else {
|
|
|
|
|
setShowPreview(false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
root: document.querySelector('.query-details'),
|
|
|
|
|
threshold: 0.01,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if (currentEditorHeightRef.current) {
|
|
|
|
|
observer.observe(currentEditorHeightRef.current);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (currentEditorHeightRef.current) {
|
|
|
|
|
observer.unobserve(currentEditorHeightRef.current);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [isInsideQueryPane, isFocused]);
|
|
|
|
|
|
2024-10-22 08:01:21 +00:00
|
|
|
cyLabel = paramLabel ? paramLabel.toLowerCase().trim().replace(/\s+/g, '-') : cyLabel;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
ref={currentEditorHeightRef}
|
|
|
|
|
className={`cm-codehinter ${darkMode && 'cm-codehinter-dark-themed'} ${disabled ? 'disabled-cursor' : ''}`}
|
|
|
|
|
data-cy={`${cyLabel}-input-field`}
|
|
|
|
|
>
|
2025-02-25 06:52:50 +00:00
|
|
|
{/* sticky element to position the preview box correctly on top without flowing out of container */}
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
position: 'sticky',
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
zIndex: 1000,
|
|
|
|
|
}}
|
|
|
|
|
ref={previewRef}
|
|
|
|
|
></div>
|
2024-10-22 08:01:21 +00:00
|
|
|
{usePortalEditor && (
|
|
|
|
|
<CodeHinter.PopupIcon
|
|
|
|
|
callback={handleTogglePopupExapand}
|
|
|
|
|
icon="portal-open"
|
|
|
|
|
tip="Pop out code editor into a new window"
|
|
|
|
|
position={currentEditorHeightRef?.current?.getBoundingClientRect()}
|
2025-02-25 06:52:50 +00:00
|
|
|
isQueryManager={isInsideQueryPane}
|
2024-10-22 08:01:21 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<CodeHinter.Portal
|
|
|
|
|
isCopilotEnabled={false}
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
callback={setIsOpen}
|
|
|
|
|
componentName={componentName}
|
|
|
|
|
key={componentName}
|
|
|
|
|
customComponent={renderPreview}
|
|
|
|
|
forceUpdate={forceUpdate}
|
|
|
|
|
optionalProps={{ styles: { height: 300 }, cls: '' }}
|
|
|
|
|
darkMode={darkMode}
|
|
|
|
|
selectors={{ className: 'preview-block-portal' }}
|
|
|
|
|
dragResizePortal={true}
|
|
|
|
|
callgpt={null}
|
|
|
|
|
>
|
|
|
|
|
<ErrorBoundary>
|
|
|
|
|
<CodeMirror
|
|
|
|
|
value={currentValue}
|
|
|
|
|
placeholder={placeholder}
|
2025-02-25 06:52:50 +00:00
|
|
|
height={isInsideQueryPane ? '100%' : showLineNumbers ? '400px' : '100%'}
|
2024-10-22 08:01:21 +00:00
|
|
|
width="100%"
|
|
|
|
|
extensions={[
|
|
|
|
|
javascript({ jsx: lang === 'jsx' }),
|
|
|
|
|
autoCompleteConfig,
|
|
|
|
|
keymap.of([...customKeyMaps]),
|
|
|
|
|
customTabKeymap,
|
|
|
|
|
]}
|
|
|
|
|
onChange={(val) => {
|
|
|
|
|
setFirstTimeFocus(false);
|
|
|
|
|
handleOnChange(val);
|
2025-02-25 06:52:50 +00:00
|
|
|
onInputChange && onInputChange(val);
|
2024-10-22 08:01:21 +00:00
|
|
|
}}
|
|
|
|
|
basicSetup={{
|
|
|
|
|
lineNumbers: showLineNumbers,
|
|
|
|
|
syntaxHighlighting: true,
|
|
|
|
|
bracketMatching: true,
|
|
|
|
|
foldGutter: false,
|
|
|
|
|
highlightActiveLine: false,
|
|
|
|
|
autocompletion: true,
|
|
|
|
|
completionKeymap: true,
|
|
|
|
|
searchKeymap: false,
|
|
|
|
|
}}
|
|
|
|
|
onMouseDown={() => handleFocus()}
|
|
|
|
|
onBlur={() => handleOnBlur()}
|
|
|
|
|
className={customClassNames}
|
|
|
|
|
theme={theme}
|
|
|
|
|
indentWithTab={false}
|
|
|
|
|
readOnly={disabled}
|
|
|
|
|
/>
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
</CodeHinter.Portal>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DynamicEditorBridge = (props) => {
|
|
|
|
|
const {
|
|
|
|
|
initialValue,
|
|
|
|
|
type,
|
|
|
|
|
fxActive,
|
|
|
|
|
paramType = 'code',
|
|
|
|
|
paramLabel,
|
|
|
|
|
paramName,
|
|
|
|
|
fieldMeta,
|
|
|
|
|
darkMode,
|
|
|
|
|
className,
|
|
|
|
|
onFxPress = noop,
|
|
|
|
|
onChange,
|
|
|
|
|
styleDefinition,
|
|
|
|
|
component,
|
|
|
|
|
onVisibilityChange,
|
|
|
|
|
isEventManagerParam = false,
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
const [forceCodeBox, setForceCodeBox] = React.useState(fxActive);
|
|
|
|
|
const codeShow = paramType === 'code' || forceCodeBox;
|
|
|
|
|
const HIDDEN_CODE_HINTER_LABELS = ['Table data', 'Column data', 'Text Format'];
|
|
|
|
|
const { isFxNotRequired } = fieldMeta;
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [_, error, value] = type === 'fxEditor' ? resolveReferences(initialValue) : [];
|
|
|
|
|
let cyLabel = paramLabel ? paramLabel.toLowerCase().trim().replace(/\s+/g, '-') : props.cyLabel;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setForceCodeBox(fxActive);
|
|
|
|
|
}, [component, fxActive]);
|
|
|
|
|
|
|
|
|
|
const fxClass = isEventManagerParam ? 'justify-content-start' : 'justify-content-end';
|
|
|
|
|
return (
|
|
|
|
|
<div className={cx({ 'codeShow-active': codeShow }, 'wrapper-div-code-editor')}>
|
2025-02-25 06:52:50 +00:00
|
|
|
<div className={cx('d-flex align-items-center justify-content-between code-flex-wrapper')}>
|
2024-10-22 08:01:21 +00:00
|
|
|
{paramLabel !== ' ' && !HIDDEN_CODE_HINTER_LABELS.includes(paramLabel) && (
|
|
|
|
|
<div className={`field ${className}`} data-cy={`${cyLabel}-widget-parameter-label`}>
|
|
|
|
|
<ToolTip
|
|
|
|
|
label={t(`widget.commonProperties.${camelCase(paramLabel)}`, paramLabel)}
|
|
|
|
|
meta={fieldMeta}
|
|
|
|
|
labelClass={`tj-text-xsm color-slate12 ${codeShow ? 'mb-2' : 'mb-0'} ${
|
|
|
|
|
darkMode && 'color-whitish-darkmode'
|
|
|
|
|
}`}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className={`${(paramType ?? 'code') === 'code' ? 'd-none' : ''} flex-grow-1`}>
|
|
|
|
|
<div style={{ marginBottom: codeShow ? '0.5rem' : '0px' }} className={`d-flex align-items-center ${fxClass}`}>
|
|
|
|
|
{paramLabel !== 'Type' && isFxNotRequired === undefined && (
|
|
|
|
|
<div
|
|
|
|
|
className={`col-auto pt-0 fx-common fx-button-container ${
|
|
|
|
|
(isEventManagerParam || codeShow) && 'show-fx-button-container'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<FxButton
|
|
|
|
|
active={codeShow}
|
|
|
|
|
onPress={() => {
|
|
|
|
|
if (codeShow) {
|
|
|
|
|
setForceCodeBox(false);
|
|
|
|
|
onFxPress(false);
|
|
|
|
|
} else {
|
|
|
|
|
setForceCodeBox(true);
|
|
|
|
|
onFxPress(true);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
dataCy={cyLabel}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-02-25 06:52:50 +00:00
|
|
|
{!codeShow && (
|
|
|
|
|
<DynamicFxTypeRenderer
|
|
|
|
|
value={!error ? value : ''}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
paramName={paramName}
|
|
|
|
|
paramLabel={paramLabel}
|
|
|
|
|
paramType={paramType}
|
|
|
|
|
forceCodeBox={() => {
|
|
|
|
|
setForceCodeBox(true);
|
|
|
|
|
onFxPress(true);
|
|
|
|
|
}}
|
|
|
|
|
meta={fieldMeta}
|
|
|
|
|
cyLabel={cyLabel}
|
|
|
|
|
styleDefinition={styleDefinition}
|
|
|
|
|
component={component}
|
|
|
|
|
onVisibilityChange={onVisibilityChange}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-10-22 08:01:21 +00:00
|
|
|
</div>
|
|
|
|
|
{codeShow && (
|
|
|
|
|
<div className={`row custom-row`} style={{ display: codeShow ? 'flex' : 'none' }}>
|
|
|
|
|
<div className={`col code-hinter-col`}>
|
|
|
|
|
<div className="d-flex">
|
|
|
|
|
<SingleLineCodeEditor initialValue {...props} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SingleLineCodeEditor.Editor = EditorInput;
|
|
|
|
|
SingleLineCodeEditor.EditorBridge = DynamicEditorBridge;
|
|
|
|
|
|
|
|
|
|
export default SingleLineCodeEditor;
|