-
Notifications
You must be signed in to change notification settings - Fork 596
/
droppable-edit-yaml.tsx
157 lines (142 loc) · 4.58 KB
/
droppable-edit-yaml.tsx
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
/* eslint-disable @typescript-eslint/no-use-before-define */
import * as React from 'react';
import * as _ from 'lodash-es';
import { NativeTypes } from 'react-dnd-html5-backend';
import { DropTarget } from 'react-dnd';
import { ResourceYAMLEditorProps } from '@console/dynamic-plugin-sdk';
import { EditYAML } from './edit-yaml';
import withDragDropContext from './utils/drag-drop-context';
import { DropTargetMonitor } from 'react-dnd/lib/interfaces';
import { containsNonPrintableCharacters } from './utils/file-input';
// Maximal file size, in bytes, that user can upload
const maxFileUploadSize = 4000000;
const fileSizeErrorMsg = 'Maximum file size exceeded. File limit is 4MB.';
const fileTypeErrorMsg = 'Binary file detected. Edit text based YAML files only.';
const boxTarget = {
drop(props, monitor) {
if (props.onDrop && monitor.isOver()) {
props.onDrop(props, monitor);
}
},
};
const EditYAMLComponent = DropTarget(NativeTypes.FILE, boxTarget, (connectObj, monitor) => ({
connectDropTarget: connectObj.dropTarget(),
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
}))(EditYAML as React.FC<EditYAMLProps>);
type DroppableEditYAMLProps = ResourceYAMLEditorProps & {
allowMultiple?: boolean;
};
// Prevents SDK users from passing additional props
export const ResourceYAMLEditor: React.FC<ResourceYAMLEditorProps> = ({
initialResource,
header,
onSave,
readOnly,
}) => (
<DroppableEditYAML
initialResource={initialResource}
header={header}
onSave={onSave}
readOnly={readOnly}
/>
);
export const DroppableEditYAML = withDragDropContext<DroppableEditYAMLProps>(
class DroppableEditYAML extends React.Component<DroppableEditYAMLProps, DroppableEditYAMLState> {
private fileUploadContents: string = '';
constructor(props) {
super(props);
this.state = {
fileUpload: '',
errors: [],
};
this.handleFileDrop = this.handleFileDrop.bind(this);
this.clearFileUpload = this.clearFileUpload.bind(this);
}
addDocument(newFileContent: string) {
this.fileUploadContents = _.isEmpty(this.fileUploadContents)
? newFileContent
: `${this.fileUploadContents}\n---\n${newFileContent}`;
}
readFileContents(file, lastFile) {
// If unsupported file type is dropped into drop zone, file will be undefined
if (!file) {
return;
}
// limit size size uploading to 1 mb
if (file.size <= maxFileUploadSize) {
const reader = new FileReader();
reader.onload = () => {
const input = reader.result as string;
if (containsNonPrintableCharacters(input)) {
this.setState((previousState) => ({
errors: [...previousState.errors, `Ignoring ${file.name}: ${fileTypeErrorMsg}`],
}));
} else {
this.addDocument(input.trim());
if (lastFile) {
this.setState({ fileUpload: this.fileUploadContents });
}
}
};
reader.readAsText(file, 'UTF-8');
} else {
this.setState((previousState) => ({
errors: [...previousState.errors, `Ignoring ${file.name}: ${fileSizeErrorMsg}`],
}));
}
}
handleFileDrop(item, monitor) {
const { allowMultiple } = this.props;
if (!monitor) {
return;
}
this.clearFileUpload();
if (allowMultiple) {
monitor.getItem().files.forEach((yamlFile, i) => {
this.readFileContents(yamlFile, i === monitor.getItem().files.length - 1);
});
} else {
const [file] = monitor.getItem().files;
this.readFileContents(file, true);
}
}
clearFileUpload() {
this.setState({ fileUpload: '', errors: [] });
this.fileUploadContents = '';
}
render() {
const { allowMultiple, initialResource } = this.props;
const { errors, fileUpload } = this.state;
return (
<EditYAMLComponent
{...this.props}
allowMultiple={allowMultiple}
obj={initialResource}
fileUpload={fileUpload}
error={errors.join('\n')}
onDrop={this.handleFileDrop}
clearFileUpload={this.clearFileUpload}
/>
);
}
},
);
type EditYAMLProps = {
allowMultiple?: boolean;
obj: ResourceYAMLEditorProps['initialResource'];
fileUpload: string;
error: string;
onDrop: (item: any, monitor: DropTargetMonitor) => void;
clearFileUpload: () => void;
};
export type DroppedFile = {
error?: string;
id: string;
name: string;
size: number;
};
export type DroppableEditYAMLState = {
errors: string[];
fileUpload: string;
};