-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteinte_obtic.js
More file actions
180 lines (172 loc) · 6.1 KB
/
teinte_obtic.js
File metadata and controls
180 lines (172 loc) · 6.1 KB
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
const home_href = '';
const ext2format = {
"docx": "docx",
"epub": "epub",
"htm": "html",
"html": "html",
"markdown": "markdown",
"md": "markdown",
"tei": "tei",
"txt": "markdown",
"xhtml": "html",
"xml": "tei"
};
const formats = {
"docx": {
"ext":".docx",
"mime":"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
},
"epub": {
"ext":".epub",
"mime":"application/epub+zip"
},
"html": {
"ext":".html",
"mime":"text/html; charset=utf-8"
},
"markdown": {
"ext":".md",
"mime":"text/plain; charset=utf-8"
},
"md": {
"ext":".md",
"mime":"text/plain; charset=utf-8"
},
"tei": {
"ext":".xml",
"mime":"text/xml"
}
};
const conversions = {
"docx": ["html", "markdown", "tei"],
"epub": ["docx", "html", "markdown", "tei"],
"markdown": ["docx", "html", "tei"],
"tei": ["docx", "html", "markdown"],
"zip": ["docx", "html", "markdown", "tei"],
}
function dropInit() {
const dropZone = document.querySelector("#dropzone");
if (!dropZone) return; // seems another page
const dropOutput = dropZone.querySelector("output");
const dropBut = dropZone.querySelector("button");
const dropInput = dropZone.querySelector("input");
const dropPreview = document.getElementById('preview');
const dropDownzone = document.getElementById('downzone');
const dropExports = document.getElementById('exports');
const message = {
"default": "Déposer ici votre fichier",
"over": "<big>Lâcher pour téléverser</big>",
}
// shared variable
let file;
let format;
if (dropOutput) {
dropOutput.innerHTML = message['default'];
}
if (dropBut) {
dropBut.onclick = () => {
dropInput.click(); //if user click on the button then the input also clicked
}
}
function dropFocus() {
dropZone.classList.remove("inactive");
dropZone.classList.add("active");
dropPreview.classList.remove("active");
dropPreview.classList.add("inactive");
dropDownzone.classList.remove("active");
dropDownzone.classList.add("inactive");
}
dropZone.onmousedown = () => {
dropFocus();
dropInput.click();
}
if (dropInput) {
dropInput.addEventListener("change", function () {
//getting user select file and [0] this means if user select multiple files then we'll select only the first one
file = this.files[0];
dropFocus();
showFile(); //calling function
});
}
//If user Drag File Over DropArea
dropZone.addEventListener("dragover", (event) => {
event.preventDefault(); //preventing from default behaviour
dropFocus();
dropOutput.innerHTML = message['over'];
});
//If user leave dragged File from DropArea
dropZone.addEventListener("dragleave", () => {
dropZone.classList.remove("inactive");
dropZone.classList.remove("active");
dropOutput.innerHTML = message['default'];
});
//If user drop File on DropArea
dropZone.addEventListener("drop", (event) => {
event.preventDefault(); //preventing from default behaviour
//getting user select file and [0] this means if user select multiple files then we'll select only the first one
file = event.dataTransfer.files[0];
showFile(); //calling function
});
function showFile() {
// user interrupt ?
if (!file || !file.name) return;
dropZone.classList.add("inactive");
let ext = file.name.split('.').pop();
format = ext2format[ext];
if (!format) format = ext;
if (!(format in conversions)) {
dropOutput.innerHTML = '<b>“' + format + '” : ce format de fichier<br/>n’est pas supporté</b><br/>' + file.name;
return;
}
dropOutput.innerHTML = '<div class="filename">' + file.name + '</div>'
+ '<div class="format ' + format + '"></div>';
upload();
}
async function upload() {
dropPreview.classList.remove("inactive");
dropPreview.innerHTML = '<p class="center">Fichier en cours de traitement… (jusqu’à plusieurs secondes selon le format et la taille du fichier)</p>'
+ '<img width="80%" class="waiting" src="theme/waiting.svg"/>';
let timeStart = Date.now();
let formData = new FormData();
formData.append("file", file);
// url is resolved from the script url
fetch('upload', {
method: "POST",
body: formData
}).then((response) => {
if (response.ok) {
if (format == 'zip') {
dropPreview.classList.add("active");
dropExports.innerHTML = '';
return response.text();
}
let downs = conversions[format];
dropDownzone.classList.remove("inactive");
dropDownzone.classList.add("active");
let html = "";
const name = file.name.replace(/\.[^/.]+$/, "");
for (let i = 0, length = downs.length; i < length; i++) {
const format2 = downs[i];
if (!formats[format2]) continue;
let ext = formats[format2].ext;
html += '\n<a class="download format ' + format2 +'" href="download?format=' + format2 + '">'
+ '<div class="filename">' + name + ext + '</div>'
+ '</a>';
}
dropExports.innerHTML = html;
dropPreview.classList.add("active");
return response.text();
}
else if (response.status == 404) {
return "Erreur de développement, la page de téléchargement n’a pas été trouvée."
}
else {
return `HTTP error ${response.status}`;
}
}).then((html) => {
dropPreview.innerHTML = html;
Tree.load();
});
}
}
dropInit();