Skip to content

Commit 0c26c45

Browse files
committedApr 9, 2024·
load file from fs
1 parent bcc9e41 commit 0c26c45

File tree

3 files changed

+11131
-11071
lines changed

3 files changed

+11131
-11071
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"rehype-raw": "^6.1.1",
3636
"rehype-sanitize": "^5.0.1",
3737
"styled-components": "^3.2",
38-
"ts-is-present": "^1.2.1"
38+
"ts-is-present": "^1.2.1",
39+
"yaml": "^2.4.1"
3940
},
4041
"devDependencies": {
4142
"@babel/core": "^7.22.5",

‎src/Start.tsx

+49
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RouteComponentProps, withRouter } from 'react-router-dom';
55
import TextField from '@atlaskit/textfield';
66
import Button from '@atlaskit/button';
77
import { Markdown } from './markdown';
8+
import YAML from 'yaml'
89

910
export type StartProps = RouteComponentProps & {
1011

@@ -102,6 +103,50 @@ export class StartWR extends React.PureComponent<StartProps, StartState> {
102103
this.setState(() => ({ urlInput: currentValue || '' }));
103104
}
104105

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+
105150
return (
106151
<EmptyState
107152
header="Load a JSON Schema"
@@ -112,6 +157,10 @@ export class StartWR extends React.PureComponent<StartProps, StartState> {
112157
<TextField isCompact={false} value={this.state.urlInput || ''} onChange={onTextChange} />
113158
<Button label="submit" onClick={handleOnClick} appearance="primary">Load Schema</Button>
114159
</StartWR.Flex>
160+
<StartWR.Flex>
161+
<span>load schema file (yml/json)</span>
162+
<input type="file" onChange={onFileChange} />
163+
</StartWR.Flex>
115164
<StartWR.Guide><Markdown source={DevelopingSchemaInstructions} /></StartWR.Guide>
116165
</StartWR.InputWidth>
117166
)}

0 commit comments

Comments
 (0)
Please sign in to comment.