-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
58 lines (48 loc) · 1.52 KB
/
helper.js
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
function showRenameConfirmation(file, newName) {
return new Promise((resolve, reject) => {
const modal = document.getElementById('myModal');
const modalImg = document.getElementById('modalImage');
const modalClose = document.getElementsByClassName('close')[0];
const newFileName = document.getElementById('newName');
const confirmRenameButton = document.getElementById('confirmRenameButton');
newFileName.textContent = `- ${newName}`;
modal.style.display = 'block';
modalImg.src = URL.createObjectURL(file);
modalClose.onclick = function () {
modal.style.display = 'none';
reject(new Error('Rename canceled'));
};
confirmRenameButton.onclick = function () {
modal.style.display = 'none';
resolve(true);
};
});
}
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString('base64'),
mimeType,
},
};
}
function handleFileSelect(event) {
const files = event.target.files;
const previewContainer = document.getElementById('imagePreview');
previewContainer.innerHTML = '';
for (const file of files) {
const reader = new FileReader();
reader.onload = function (e) {
const img = document.createElement('img');
img.src = e.target.result;
img.classList.add('image-preview-item');
previewContainer.appendChild(img);
};
reader.readAsDataURL(file);
}
}
module.exports = {
showRenameConfirmation,
fileToGenerativePart,
handleFileSelect,
};