forked from codeitcodes/codeit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodedrop.js
More file actions
142 lines (87 loc) · 2.53 KB
/
Copy pathcodedrop.js
File metadata and controls
142 lines (87 loc) · 2.53 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
function getFileLang(src) {
var EXTENSIONS = {
'js': 'javascript',
'py': 'python',
'rb': 'ruby',
'ps1': 'powershell',
'psm1': 'powershell',
'sh': 'bash',
'bat': 'batch',
'h': 'c',
'tex': 'latex'
};
src = src.replaceAll('\n', '');
const extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
return EXTENSIONS[extension] || extension;
}
function processFile(file) {
const reader = new FileReader();
reader.addEventListener('load', (event) => {
cd.textContent = event.target.result;
cd.lang = getFileLang(file.name);
cd.focus();
cd.history = [];
saveSelectedFileContent();
saveSelectedFileCaretPos();
saveSelectedFileScrollPos();
saveSelectedFileLang();
console.log('Loaded local file. Name: ' + file.name + ' Size: ' + file.size + ' bytes');
});
reader.readAsText(file);
}
cd.on('drop', (ev) => {
// prevent default behavior (prevent file from being opened)
ev.preventDefault();
// if not logged into git
if (gitToken == null) {
// remove drop indication
document.body.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]);
}
}
}
})
cd.on('dragover', (ev) => {
// prevent default behavior (prevent file from being opened)
ev.preventDefault();
// if not logged into git
if (gitToken == null) {
// show drop indication
document.body.classList.add('focus');
}
})
cd.on('dragleave', (ev) => {
// if not logged into git
if (gitToken == null) {
// remove drop indication
document.body.classList.remove('focus');
}
})
if ('launchQueue' in window) {
launchQueue.setConsumer(async (launchParams) => {
// if not logged into git
if (gitToken == null) {
// nothing to do when the queue is empty
if (!launchParams.files.length) {
return;
}
for (const fileHandle of launchParams.files) {
// handle the file
const fileData = await fileHandle.getFile();
processFile(fileData);
}
}
});
}