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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
import "fs";
import React = require("react");
import { Doc } from "../../../new_fields/Doc";
import { DocServer } from "../../DocServer";
import { RouteStore } from "../../../server/RouteStore";
import { action, observable, autorun, runInAction } from "mobx";
import { FieldViewProps, FieldView } from "../../views/nodes/FieldView";
import Measure, { ContentRect } from "react-measure";
import { library } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faArrowUp, faTag, faPlus } from '@fortawesome/free-solid-svg-icons';
import { Docs, DocumentOptions } from "../../documents/Documents";
import { observer } from "mobx-react";
import KeyValue from "./KeyValue";
import { Utils } from "../../../Utils";
import { DocumentManager } from "../DocumentManager";
@observer
export default class DirectoryImportBox extends React.Component<FieldViewProps> {
private selector = React.createRef<HTMLInputElement>();
@observable private top = 0;
@observable private left = 0;
private dimensions = 50;
@observable private editingMetadata = false;
@observable private metadata_guids: string[] = [];
@observable private entries: KeyValue[] = [];
@observable private quota = 1;
@observable private remaining = 1;
@observable private uploadBegun = false;
public static LayoutString() { return FieldView.LayoutString(DirectoryImportBox); }
constructor(props: FieldViewProps) {
super(props);
library.add(faArrowUp, faTag, faPlus);
}
@action
handleSelection = async (e: React.ChangeEvent<HTMLInputElement>) => {
this.uploadBegun = true;
let promises: Promise<void>[] = [];
let docs: Doc[] = [];
let files = e.target.files;
if (!files || files.length === 0) return;
let directory = (files.item(0) as any).webkitRelativePath.split("/", 1);
let validated: File[] = [];
for (let i = 0; i < files.length; i++) {
let file = files.item(i);
file && validated.push(file);
}
this.quota = validated.length;
for (let uploaded_file of validated) {
if (!uploaded_file) {
continue;
}
let formData = new FormData();
formData.append('file', uploaded_file);
let dropFileName = uploaded_file ? uploaded_file.name : "-empty-";
let type = uploaded_file.type;
this.remaining++;
let prom = fetch(DocServer.prepend(RouteStore.upload), {
method: 'POST',
body: formData
}).then(async (res: Response) => {
(await res.json()).map(action((file: any) => {
let path = DocServer.prepend(file);
let docPromise = Docs.getDocumentFromType(type, path, { nativeWidth: 300, width: 300, title: dropFileName });
docPromise.then(doc => {
doc && docs.push(doc) && runInAction(() => this.remaining--);
});
}));
});
promises.push(prom);
}
await Promise.all(promises);
docs.forEach(doc => this.entries.forEach(entry => doc[entry.key] = entry.value));
let doc = this.props.Document;
let options: DocumentOptions = {
title: `Import of ${directory}`,
width: 1105,
height: 500,
x: Doc.GetT(doc, "x", "number"),
y: Doc.GetT(doc, "y", "number")
};
let parent = this.props.ContainingCollectionView;
if (parent) {
let importContainer = Docs.StackingDocument(docs, options);
importContainer.singleColumn = false;
Doc.AddDocToList(Doc.GetProto(parent.props.Document), "data", importContainer);
this.props.removeDocument && this.props.removeDocument(doc);
DocumentManager.Instance.jumpToDocument(importContainer, true);
}
}
componentDidMount() {
this.selector.current!.setAttribute("directory", "");
this.selector.current!.setAttribute("webkitdirectory", "");
}
@action
preserveCentering = (rect: ContentRect) => {
let bounds = rect.offset!;
if (bounds.width === 0 || bounds.height === 0) {
return;
}
let offset = this.dimensions / 2;
this.left = bounds.width / 2 - offset;
this.top = bounds.height / 2 - offset;
}
@action
addMetadataEntry = () => {
this.metadata_guids.push(Utils.GenerateGuid());
}
@action
remove = (entry: KeyValue) => {
let index = this.entries.indexOf(entry);
let key = entry.key;
this.entries.splice(index, 1);
this.metadata_guids.splice(this.metadata_guids.indexOf(key), 1);
}
render() {
let dimensions = 50;
let guids = this.metadata_guids.map(el => el);
let isEditing = this.editingMetadata;
let remaining = this.remaining;
let quota = this.quota;
let percent = `${100 - (remaining / quota * 100)}`;
let uploadBegun = this.uploadBegun;
percent = percent.split(".")[0];
percent = percent.startsWith("100") ? "99" : percent;
return (
<Measure offset onResize={this.preserveCentering}>
{({ measureRef }) =>
<div ref={measureRef} style={{ width: "100%", height: "100%", pointerEvents: "all" }} >
<input
id={"selector"}
ref={this.selector}
onChange={this.handleSelection}
type="file"
style={{
position: "absolute",
display: "none"
}} />
<label
htmlFor={"selector"}
style={{
opacity: isEditing ? 0 : 1,
pointerEvents: isEditing ? "none" : "all",
transition: "0.4s ease opacity"
}}
>
<div style={{
width: dimensions,
height: dimensions,
borderRadius: "50%",
background: "black",
position: "absolute",
left: this.left,
top: this.top
}} />
<div style={{
position: "absolute",
left: this.left + 12.6,
top: this.top + 11,
opacity: uploadBegun ? 0 : 1,
transition: "0.4s opacity ease"
}}>
<FontAwesomeIcon icon={faArrowUp} color="#FFFFFF" size={"2x"} />
</div>
<img
style={{
width: 80,
height: 80,
transition: "0.4s opacity ease",
opacity: uploadBegun ? 0.7 : 0,
position: "absolute",
top: this.top - 15,
left: this.left - 15
}}
src={"/assets/loading.gif"}></img>
</label>
<div
style={{
transition: "0.4s opacity ease",
opacity: uploadBegun ? 1 : 0,
pointerEvents: "none",
position: "absolute",
left: 10,
top: this.top + 12.3,
fontSize: 18,
color: "white",
marginLeft: this.left - 1.6
}}>{percent}%</div>
<div
style={{
position: "absolute",
top: 10,
right: 10,
borderRadius: "50%",
width: 25,
height: 25,
background: "black"
}}
onClick={action(() => this.editingMetadata = !this.editingMetadata)}
/>
<FontAwesomeIcon
style={{
pointerEvents: "none",
position: "absolute",
right: isEditing ? 16.3 : 14.5,
top: isEditing ? 15.4 : 16
}}
icon={isEditing ? faArrowUp : faTag}
color="#FFFFFF"
size={"1x"}
/>
<div
style={{
transition: "0.4s ease opacity",
width: "100%",
height: "100%",
pointerEvents: isEditing ? "all" : "none",
opacity: isEditing ? 1 : 0,
overflowY: "scroll"
}}
>
<div
style={{
borderRadius: "50%",
width: 25,
height: 25,
marginLeft: 10,
position: "absolute",
right: 41,
top: 10
}}
onClick={this.addMetadataEntry}
>
<FontAwesomeIcon
style={{
pointerEvents: "none",
marginLeft: 6.4,
marginTop: 5.2
}}
icon={faPlus}
size={"1x"}
/>
</div>
<p style={{ paddingLeft: 10, paddingTop: 8, paddingBottom: 7 }} >Add metadata to your import...</p>
<hr style={{ margin: "6px 10px 12px 10px" }} />
{guids.map(guid => <KeyValue remove={this.remove} key={guid} ref={(el) => { if (el) this.entries.push(el); }} />)}
</div>
</div>
}
</Measure>
);
}
}
|