@@ -4,21 +4,31 @@ import {
44 WorkflowDefinition ,
55} from "@melonade/melonade-declaration" ;
66import { 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
710import * as R from "ramda" ;
811import React from "react" ;
12+ import Editor from "react-simple-code-editor" ;
913import styled from "styled-components" ;
1014import JsonEditor from "../../components/JsonEditor" ;
1115
1216const { Option } = Select ;
1317
14- const StyledInput = styled ( Input ) `
15- width: 100%;
16- ` ;
17-
1818const 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+
2232interface 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 ( / ^ ( ` ` ` j a v a s c r i p t \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+
34129export 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