@@ -5,6 +5,7 @@ import { RouteComponentProps, withRouter } from 'react-router-dom';
5
5
import TextField from '@atlaskit/textfield' ;
6
6
import Button from '@atlaskit/button' ;
7
7
import { Markdown } from './markdown' ;
8
+ import YAML from 'yaml'
8
9
9
10
export type StartProps = RouteComponentProps & {
10
11
@@ -102,6 +103,50 @@ export class StartWR extends React.PureComponent<StartProps, StartState> {
102
103
this . setState ( ( ) => ( { urlInput : currentValue || '' } ) ) ;
103
104
}
104
105
106
+ interface ReadResult {
107
+ filename : string ;
108
+ result : string ;
109
+ }
110
+
111
+ const readFile = async ( file : File ) : Promise < ReadResult > => {
112
+ return new Promise ( ( resolve , reject ) => {
113
+ const arrayBufferReader = new FileReader ( ) ;
114
+ arrayBufferReader . readAsArrayBuffer ( file ) ;
115
+ arrayBufferReader . onload = async ( ) => {
116
+ const textReader = new FileReader ( ) ;
117
+ textReader . readAsText ( file , 'UTF-8' ) ;
118
+ textReader . onload = ( ) => resolve ( {
119
+ filename : file . name ,
120
+ // as string because we use readAsText...
121
+ result : textReader . result as string
122
+ } ) ;
123
+ textReader . onerror = reject ;
124
+ } ;
125
+ arrayBufferReader . onerror = reject ;
126
+ } ) ;
127
+ } ;
128
+
129
+ const toObject = ( str : string , extension : string ) => {
130
+ if ( extension === "yml" || extension === "yaml" ) {
131
+ return YAML . parse ( str ) ;
132
+ } else if ( extension === "json" ) {
133
+ return JSON . parse ( str ) ;
134
+ }
135
+ }
136
+
137
+ const onFileChange : React . FormEventHandler < HTMLInputElement > = async e => {
138
+ // @ts -ignore
139
+ const [ file ] = e . currentTarget . files ;
140
+ const { filename, result} = await readFile ( file ) ;
141
+ // @ts -ignore
142
+ const obj = toObject ( result , filename . split ( "." ) . at ( - 1 ) ) ;
143
+ const json = JSON . stringify ( obj )
144
+ console . log ( `data:application/json;base64,${ btoa ( json ) } ` ) ;
145
+ console . log ( 'currentValue' , obj ) ;
146
+ this . setState ( ( ) => ( { urlInput : `data:application/json;base64,${ btoa ( json ) } ` } ) ) ;
147
+ handleOnClick ( ) ;
148
+ }
149
+
105
150
return (
106
151
< EmptyState
107
152
header = "Load a JSON Schema"
@@ -112,6 +157,10 @@ export class StartWR extends React.PureComponent<StartProps, StartState> {
112
157
< TextField isCompact = { false } value = { this . state . urlInput || '' } onChange = { onTextChange } />
113
158
< Button label = "submit" onClick = { handleOnClick } appearance = "primary" > Load Schema</ Button >
114
159
</ StartWR . Flex >
160
+ < StartWR . Flex >
161
+ < span > load schema file (yml/json)</ span >
162
+ < input type = "file" onChange = { onFileChange } />
163
+ </ StartWR . Flex >
115
164
< StartWR . Guide > < Markdown source = { DevelopingSchemaInstructions } /> </ StartWR . Guide >
116
165
</ StartWR . InputWidth >
117
166
) }
0 commit comments