forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeEditor.tsx
More file actions
197 lines (181 loc) · 5.44 KB
/
codeEditor.tsx
File metadata and controls
197 lines (181 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, {
FC,
useState
} from 'react';
import {
Alert,
App,
Button,
Modal,
Tabs,
Typography
} from 'antd';
import { CodeEditor as BaseCodeEditor } from '@/components/codeEditor/codeEditor';
import { CodeOutlined, ExclamationCircleFilled } from '@ant-design/icons';
import { CodeVariablesTables } from '@/components/codeVariablesTable';
import { ICodeEditorProps } from './interfaces';
import { Show } from '@/components';
import { useSourcesFolder } from '@/providers/sourceFileManager/sourcesFolderProvider';
import type { TabsProps } from 'antd';
import { useStyles } from './styles';
type TabItem = TabsProps['items'][number];
export const CodeEditor: FC<ICodeEditorProps> = ({
mode = 'inline',
value,
exposedVariables,
readOnly = false,
language = 'typescript',
environment,
...props
}) => {
const [internalValue, setInternalValue] = useState<string>(value); // stores value for the `dialog` mode
const [showDialog, setShowDialog] = useState(false);
const { modal } = App.useApp();
const src = useSourcesFolder(false);
const { styles } = useStyles();
const onChange = (_value) => {
switch (mode) {
case 'inline': {
if (props.onChange) props.onChange(_value);
break;
}
case 'dialog': {
setInternalValue(_value);
break;
}
}
};
const hasValue = value && typeof (value) === 'string' && Boolean(value?.trim());
const onClear = () => {
if (hasValue) {
modal.confirm({
title: 'Clear code editor?',
icon: <ExclamationCircleFilled />,
content: 'If you clear the code editor, the changes will be lost and the editor will be closed',
okText: 'Yes',
okType: 'danger',
cancelText: 'No',
onOk() {
setInternalValue(null);
setShowDialog(false);
if (props.onChange) props.onChange(null);
}
});
}
};
const onDialogSave = () => {
if (props.onChange) props.onChange(internalValue);
setShowDialog(false);
};
const openEditorDialog = () => setShowDialog(true);
const onDialogCancel = () => {
if (!readOnly && (value ?? "").trim() !== (internalValue ?? "").trim()) {
modal.confirm({
title: 'Close code editor?',
icon: <ExclamationCircleFilled />,
content: 'Unsaved changes will be lost. Are you sure you want to cancel edit?',
okText: 'Yes',
okType: 'danger',
cancelText: 'No',
onOk() {
setInternalValue(value);
setShowDialog(false);
}
});
} else {
setInternalValue(value);
setShowDialog(false);
}
};
const effectiveValue = mode === 'inline' ? value : internalValue;
const renderCodeEditor = () => (
<BaseCodeEditor
value={effectiveValue}
onChange={onChange}
readOnly={readOnly}
placeholder={props.placeholder}
language={language}
path={src?.path}
wrapInTemplate={props.wrapInTemplate}
templateSettings={props.templateSettings}
fileName={props.fileName ?? props.propertyName}
availableConstants={props.availableConstants}
resultType={props.resultType}
style={mode === 'dialog' ? { height: "100%" } : undefined}
environment={environment}
/>
);
if (props.hidden) return null;
if (mode === 'inline')
return renderCodeEditor();
const tabItems: TabItem[] = exposedVariables?.length
? [
{
key: "code",
label: "Code",
children: (
<div className={styles.codeEditorContainer}>
{renderCodeEditor()}
</div>
)
},
{
key: "variable",
label: "Variables",
children: (<CodeVariablesTables data={exposedVariables} />)
}
]
: undefined;
return readOnly && !hasValue
? (<Typography.Text disabled>No Code</Typography.Text>)
: (
<>
<Button
className={props.className}
size="small"
onClick={openEditorDialog}
style={hasValue ? { fontFamily: 'monospace', fontSize: '12px' } : undefined}
>
{hasValue ? <><CodeOutlined /> {value}</> : <><CodeOutlined /> ...</>}
</Button>
{showDialog && (
<Modal
open={showDialog}
onCancel={onDialogCancel}
onOk={onDialogSave}
closable={true}
title={props.label}
okButtonProps={{ hidden: readOnly }}
cancelText={readOnly ? 'Close' : undefined}
destroyOnClose={true}
classNames={{ body: styles.codeEditorModalBody }}
className={styles.codeEditorModal}
width={null}
footer={[
hasValue && <Button key="clear" danger onClick={onClear} disabled={readOnly}>
Clear
</Button>,
<Button key="cancel" onClick={onDialogCancel}>
{readOnly ? 'Close' : 'Cancel'}
</Button>,
!readOnly && (
<Button key="ok" type="primary" onClick={onDialogSave}>
OK
</Button>
),
]}
>
<Show when={Boolean(props?.description)}>
<Alert message={props?.description} />
<br />
</Show>
{tabItems ? (
<Tabs items={tabItems} />
) : (
<div className={styles.codeEditorContainer}>{renderCodeEditor()}</div>
)}
</Modal>
)}
</>
);
};