Skip to content

Commit 25d349c

Browse files
authored
Merge pull request #21 from devit-tel/feature/input-type-javascript-exec
Input type javascript exec
2 parents c567fa5 + a6e581a commit 25d349c

File tree

3 files changed

+132
-19
lines changed

3 files changed

+132
-19
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@
6363
"postcss-normalize": "7.0.1",
6464
"postcss-preset-env": "6.7.0",
6565
"postcss-safe-parser": "4.0.1",
66+
"prismjs": "^1.24.1",
6667
"ramda": "^0.26.1",
6768
"react": "^16.12.0",
6869
"react-app-polyfill": "^1.0.4",
6970
"react-dev-utils": "^9.1.0",
7071
"react-dom": "^16.12.0",
7172
"react-json-view": "^1.21.1",
7273
"react-router-dom": "^5.1.2",
74+
"react-simple-code-editor": "^0.11.0",
7375
"react-virtualized": "^9.21.2",
7476
"resolve": "1.12.0",
7577
"resolve-url-loader": "^3.1.2",
@@ -163,6 +165,7 @@
163165
]
164166
},
165167
"devDependencies": {
168+
"@types/prismjs": "^1.16.6",
166169
"@types/uuid": "^7.0.2"
167170
}
168171
}

src/components/WorkflowChart/modal.tsx

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@ import {
44
WorkflowDefinition,
55
} from "@melonade/melonade-declaration";
66
import { Form, Input, Modal, Radio, Select } from "antd";
7+
import { highlight, languages } from "prismjs";
8+
import "prismjs/components/prism-javascript";
9+
import "prismjs/themes/prism-twilight.css"; //Example style, you can use another
710
import * as R from "ramda";
811
import React from "react";
12+
import Editor from "react-simple-code-editor";
913
import styled from "styled-components";
1014
import JsonEditor from "../../components/JsonEditor";
1115

1216
const { Option } = Select;
1317

14-
const StyledInput = styled(Input)`
15-
width: 100%;
16-
`;
17-
1818
const StyledModal = styled(Modal)`
1919
min-width: 70vw;
2020
`;
2121

22+
const StyledTag = styled.span`
23+
background-color: #4caf50; /* Green */
24+
border: none;
25+
color: white;
26+
text-align: center;
27+
text-decoration: none;
28+
margin: 2px;
29+
padding: 2px;
30+
`;
31+
2232
interface ICreateTaskModalProps {
2333
visible?: boolean;
2434
onSubmit: (task: WorkflowDefinition.AllTaskType) => void;
@@ -31,6 +41,91 @@ interface ICreateTaskModalState {
3141
task?: WorkflowDefinition.ITaskTask;
3242
}
3343

44+
interface IPropsInput {
45+
placeholder?: string;
46+
value: string;
47+
onChange: (data: string) => void;
48+
}
49+
50+
const InputCode = (props: IPropsInput) => {
51+
return (
52+
<div>
53+
<Editor
54+
value={props.value || ""}
55+
onValueChange={props.onChange}
56+
highlight={(code) =>
57+
highlight(code, languages.javascript, "javascript")
58+
}
59+
padding={10}
60+
style={{
61+
fontFamily: '"Fira code", "Fira Mono", monospace',
62+
fontSize: 12,
63+
backgroundColor: "#322931",
64+
}}
65+
/>
66+
<StyledTag>type: {getType(props.value)}</StyledTag>
67+
</div>
68+
);
69+
};
70+
71+
const getType = (value: any) => {
72+
if (value === undefined) {
73+
return "undefined";
74+
}
75+
76+
if (!Number.isNaN(+value)) {
77+
return "number";
78+
}
79+
80+
if (typeof value === "string") {
81+
if (/^(```javascript\n)([\S\s]+)(```)$/gm.test(value)) {
82+
return "javascript";
83+
}
84+
85+
return "string";
86+
}
87+
88+
return "json";
89+
};
90+
91+
interface IPropsInputJsonOrCode {
92+
value: any;
93+
onChange: (data: any) => void;
94+
}
95+
96+
const InputJsonOrCode = (props: IPropsInputJsonOrCode) => {
97+
var data: string = "";
98+
99+
if (typeof props.value != "string") {
100+
data = JSON.stringify(props.value, null, 4);
101+
} else {
102+
data = props.value;
103+
}
104+
105+
return (
106+
<div>
107+
<Editor
108+
value={data}
109+
onValueChange={(v) => {
110+
try {
111+
props.onChange(JSON.parse(v));
112+
} catch (error) {
113+
props.onChange(v);
114+
}
115+
}}
116+
highlight={(code) => highlight(code, languages.js, "javascript")}
117+
padding={10}
118+
style={{
119+
fontFamily: '"Fira code", "Fira Mono", monospace',
120+
fontSize: 12,
121+
backgroundColor: "#322931",
122+
}}
123+
/>
124+
<StyledTag>type: {getType(props.value)}</StyledTag>
125+
</div>
126+
);
127+
};
128+
34129
export class CreateTaskModal extends React.Component<
35130
ICreateTaskModalProps,
36131
ICreateTaskModalState
@@ -122,38 +217,38 @@ export class CreateTaskModal extends React.Component<
122217
</Select>
123218
</Form.Item>
124219
<Form.Item label="Retry Limit">
125-
<StyledInput
220+
<InputCode
126221
placeholder="Retry Limit"
127222
value={R.path(["retry", "limit"], this.state.task) as any}
128-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
129-
this.onInputChanged(["retry", "limit"], e.target.value);
223+
onChange={(e) => {
224+
this.onInputChanged(["retry", "limit"], e);
130225
}}
131226
/>
132227
</Form.Item>
133228
<Form.Item label="Retry Delay">
134-
<StyledInput
229+
<InputCode
135230
placeholder="Retry Delay"
136231
value={R.path(["retry", "delay"], this.state.task) as any}
137-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
138-
this.onInputChanged(["retry", "delay"], e.target.value);
232+
onChange={(e) => {
233+
this.onInputChanged(["retry", "delay"], e);
139234
}}
140235
/>
141236
</Form.Item>
142237
<Form.Item label="Ack Timeout">
143-
<StyledInput
238+
<InputCode
144239
placeholder="Ack Timeout"
145240
value={R.path(["ackTimeout"], this.state.task) as any}
146-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
147-
this.onInputChanged(["ackTimeout"], e.target.value);
241+
onChange={(e) => {
242+
this.onInputChanged(["ackTimeout"], e);
148243
}}
149244
/>
150245
</Form.Item>
151246
<Form.Item label="Timeout">
152-
<StyledInput
247+
<InputCode
153248
placeholder="Timeout"
154249
value={R.path(["timeout"], this.state.task) as any}
155-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
156-
this.onInputChanged(["timeout"], e.target.value);
250+
onChange={(e) => {
251+
this.onInputChanged(["timeout"], e);
157252
}}
158253
/>
159254
</Form.Item>
@@ -170,9 +265,8 @@ export class CreateTaskModal extends React.Component<
170265
</Radio.Group>
171266
</Form.Item>
172267

173-
<JsonEditor
174-
key={`${this.props.visible}`} // Remount every time
175-
data={R.path(["inputParameters"], this.state.task) || {}}
268+
<InputJsonOrCode
269+
value={R.path(["inputParameters"], this.state.task) || {}}
176270
onChange={(data: any) => {
177271
this.onInputChanged(["inputParameters"], data);
178272
}}

0 commit comments

Comments
 (0)