-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcodedrop.js
More file actions
328 lines (228 loc) · 7.02 KB
/
Copy pathcodedrop.js
File metadata and controls
328 lines (228 loc) · 7.02 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
function getFileLang(src) {
const lang_aliases = /*aliases_placeholder[*/ {
"html": "markup",
"xml": "markup",
"svg": "markup",
"mathml": "markup",
"ssml": "markup",
"atom": "markup",
"rss": "markup",
"js": "javascript",
"g4": "antlr4",
"ino": "arduino",
"arm-asm": "armasm",
"art": "arturo",
"adoc": "asciidoc",
"avs": "avisynth",
"avdl": "avro-idl",
"gawk": "awk",
"sh": "bash",
"shell": "bash",
"shortcode": "bbcode",
"rbnf": "bnf",
"oscript": "bsl",
"cs": "csharp",
"dotnet": "csharp",
"cfc": "cfscript",
"cilk-c": "cilkc",
"cilk-cpp": "cilkcpp",
"cilk": "cilkcpp",
"coffee": "coffeescript",
"conc": "concurnas",
"jinja2": "django",
"dns-zone": "dns-zone-file",
"dockerfile": "docker",
"gv": "dot",
"eta": "ejs",
"xlsx": "excel-formula",
"xls": "excel-formula",
"gamemakerlanguage": "gml",
"po": "gettext",
"gni": "gn",
"ld": "linker-script",
"go-mod": "go-module",
"hbs": "handlebars",
"mustache": "handlebars",
"hs": "haskell",
"idr": "idris",
"gitignore": "ignore",
"hgignore": "ignore",
"npmignore": "ignore",
"webmanifest": "json",
"kt": "kotlin",
"kts": "kotlin",
"kum": "kumir",
"tex": "latex",
"context": "latex",
"ly": "lilypond",
"emacs": "lisp",
"elisp": "lisp",
"emacs-lisp": "lisp",
"md": "markdown",
"moon": "moonscript",
"n4jsd": "n4js",
"nani": "naniscript",
"objc": "objectivec",
"qasm": "openqasm",
"objectpascal": "pascal",
"px": "pcaxis",
"pcode": "peoplecode",
"plantuml": "plant-uml",
"pq": "powerquery",
"mscript": "powerquery",
"pbfasm": "purebasic",
"purs": "purescript",
"py": "python",
"qs": "qsharp",
"rkt": "racket",
"razor": "cshtml",
"rpy": "renpy",
"res": "rescript",
"robot": "robotframework",
"rb": "ruby",
"sh-session": "shell-session",
"shellsession": "shell-session",
"smlnj": "sml",
"sol": "solidity",
"sln": "solution-file",
"rq": "sparql",
"sclang": "supercollider",
"t4": "t4-cs",
"trickle": "tremor",
"troy": "tremor",
"trig": "turtle",
"ts": "typescript",
"tsconfig": "typoscript",
"uscript": "unrealscript",
"uc": "unrealscript",
"url": "uri",
"vb": "visual-basic",
"vba": "visual-basic",
"webidl": "web-idl",
"mathematica": "wolfram",
"nb": "wolfram",
"wl": "wolfram",
"xeoracube": "xeora",
"yml": "yaml"
} /*]*/ ;
src = src.replaceAll('\n', '');
const extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
return lang_aliases[extension] || extension;
}
function processFile(file) {
showMessage('Opening file...', -1);
const reader = new FileReader();
reader.addEventListener('load', (event) => {
// clear existing selections in HTML
if (fileWrapper.querySelector('.selected')) {
fileWrapper.querySelector('.selected').classList.remove('selected');
}
// if adding a new file, remove it
if (fileWrapper.querySelector('.focused')) {
fileWrapper.querySelector('.focused').classList.add('hidden');
window.setTimeout(() => {
fileWrapper.querySelector('.focused').remove();
}, 180);
}
// show all files in HTML
let files = fileWrapper.querySelectorAll('.item[style="display: none;"]');
files.forEach(file => { file.style.display = '' });
header.classList.remove('searching');
// if previous file selection exists
if (selectedFile.sha) {
// get previous selection in modifiedFiles array
let selectedItem = modifiedFiles[selectedFile.sha];
// if previous selection was modified
if (selectedItem) {
// save previous selection in localStorage
updateModFileContent(selectedFile.sha, selectedFile.content);
updateModFileCaretPos(selectedFile.sha, selectedFile.caretPos);
updateModFileScrollPos(selectedFile.sha, selectedFile.scrollPos);
}
}
changeSelectedFile('', '', file.name, encodeUnicode(event.target.result), getFileLang(file.name), [0, 0], [0, 0], false);
if (hashCode(event.target.result) !== hashCode(cd.textContent)) {
cd.textContent = event.target.result;
}
cd.lang = getFileLang(file.name);
cd.scrollTo(0, 0);
// set caret pos in codeit
if (!isMobile) cd.setSelection(0, 0);
cd.focus();
cd.history.records = [{ html: cd.innerHTML, pos: cd.getSelection() }];
cd.history.pos = 0;
// update line numbers
updateLineNumbersHTML();
if (liveView.classList.contains('file-open')) {
liveView.classList.add('notransition');
liveView.classList.remove('file-open');
onNextFrame(() => {
liveView.classList.remove('notransition');
});
}
hideMessage();
});
reader.readAsText(file);
}
body.addEventListener('drop', (ev) => {
// prevent default behavior (prevent file from being opened)
ev.preventDefault();
// remove drop indication
if (!liveView.classList.contains('file-open')) {
cd.classList.remove('focus');
} else {
liveView.classList.remove('focus');
}
if (ev.dataTransfer.items) {
// use DataTransferItemList interface to access the file(s)
for (var i = 0; i < ev.dataTransfer.items.length; i++) {
// if dropped items aren't files, reject them
if (ev.dataTransfer.items[i].kind === 'file') {
var file = ev.dataTransfer.items[i].getAsFile();
processFile(file);
}
}
} else {
// use DataTransfer interface to access the file(s)
for (var i = 0; i < ev.dataTransfer.files.length; i++) {
processFile(ev.dataTransfer.files[i]);
}
}
})
body.addEventListener('dragover', (ev) => {
// prevent default behavior (prevent file from being opened)
ev.preventDefault();
// show drop indication
if (!liveView.classList.contains('file-open')) {
cd.classList.add('focus');
} else {
liveView.classList.add('focus');
}
})
body.addEventListener('dragleave', (ev) => {
// remove drop indication
if (!liveView.classList.contains('file-open')) {
cd.classList.remove('focus');
} else {
liveView.classList.remove('focus');
}
})
if ('launchQueue' in window) {
window.launchQueue.setConsumer(async (launchParams) => {
if (!launchParams.files.length) {
return;
}
const launchFile = launchParams.files[0];
console.log('[launchQueue] Launched with: ', launchFile);
// get the file
const fileData = await launchFile.getFile();
// if localStorage not loaded yet
if (typeof selectedFile === 'undefined') {
// wait until localStorage is loaded
window.addEventListener('load', () => {
// handle the file
processFile(fileData);
});
}
});
}