Skip to content

Commit d50cbd0

Browse files
committed
Bump version to 1.1.6 and add a download progress
1 parent 85c919d commit d50cbd0

File tree

2 files changed

+102
-46
lines changed

2 files changed

+102
-46
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iotas-notepad",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"main": "index.js",
55
"scripts": {
66
"electron": "electron .",

src/script.js

Lines changed: 101 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let saveTimeout;
22

3-
const version = "1.1.5";
3+
const version = "1.1.6";
44
const electronVersion = "34.2.0";
55

66
const defaultThemes = {
@@ -83,14 +83,16 @@ document.addEventListener("DOMContentLoaded", () => {
8383
});
8484

8585
async function fetchReleases() {
86-
const response = await fetch('https://api.github.com/repos/vorlie/iotas-notepad/releases');
86+
const response = await fetch(
87+
"https://api.github.com/repos/vorlie/iotas-notepad/releases",
88+
);
8789
const releases = await response.json();
8890
return releases;
8991
}
9092

9193
function isNewerVersion(currentVersion, latestVersion) {
92-
const current = currentVersion.split('.').map(Number);
93-
const latest = latestVersion.split('.').map(Number);
94+
const current = currentVersion.split(".").map(Number);
95+
const latest = latestVersion.split(".").map(Number);
9496

9597
for (let i = 0; i < latest.length; i++) {
9698
if (latest[i] > (current[i] || 0)) {
@@ -108,25 +110,107 @@ async function displayReleases() {
108110

109111
if (latestRelease && isNewerVersion(version, latestRelease.tag_name)) {
110112
// Show notification
111-
const notification = document.getElementById('notification');
112-
const message = document.getElementById('message');
113-
const downloadLink = document.getElementById('download-link');
113+
const notification = document.getElementById("notification");
114+
const message = document.getElementById("message");
115+
const downloadLink = document.getElementById("download-link");
114116
message.innerText = `New version ${latestRelease.tag_name} is available!`;
115117
downloadLink.href = latestRelease.assets[0].browser_download_url; // Assuming the first asset is the setup file
116-
downloadLink.innerText = 'Download';
118+
downloadLink.innerText = "Download";
117119
downloadLink.onclick = (e) => {
118120
e.preventDefault();
119-
const link = document.createElement('a');
120-
link.href = latestRelease.assets[0].browser_download_url;
121-
link.download = '';
122-
document.body.appendChild(link);
123-
link.click();
124-
document.body.removeChild(link);
121+
const downloadUrl = latestRelease.assets[0].browser_download_url;
122+
const fileName = latestRelease.assets[0].name;
123+
notification.classList.add("hidden");
124+
downloadFile(downloadUrl, fileName);
125125
};
126-
notification.classList.remove('hidden');
126+
notification.classList.remove("hidden");
127127
}
128128
}
129129

130+
async function checkForUpdates() {
131+
const releases = await fetchReleases();
132+
const latestRelease = releases[0]; // Get the latest release
133+
134+
if (latestRelease && isNewerVersion(version, latestRelease.tag_name)) {
135+
// Show notification for new version
136+
const notification = document.getElementById("notification");
137+
const message = document.getElementById("message");
138+
const downloadLink = document.getElementById("download-link");
139+
message.innerText = `New version ${latestRelease.tag_name} is available!`;
140+
downloadLink.href = latestRelease.assets[0].browser_download_url; // Assuming the first asset is the setup file
141+
downloadLink.innerText = "Download";
142+
downloadLink.onclick = (e) => {
143+
e.preventDefault();
144+
const downloadUrl = latestRelease.assets[0].browser_download_url;
145+
const fileName = latestRelease.assets[0].name;
146+
notification.classList.add("hidden");
147+
downloadFile(downloadUrl, fileName);
148+
};
149+
notification.classList.remove("hidden");
150+
} else {
151+
// Show popup indicating the app is up-to-date
152+
alert("The app is up-to-date.");
153+
}
154+
}
155+
156+
function downloadFile(url, fileName) {
157+
const xhr = new XMLHttpRequest();
158+
xhr.open("GET", url, true);
159+
xhr.responseType = "blob";
160+
161+
// Create progress bar elements
162+
const progressDiv = document.createElement("div");
163+
progressDiv.style.width = "100%";
164+
progressDiv.style.backgroundColor = "var(--color-bg-light)";
165+
progressDiv.style.padding = "5px";
166+
progressDiv.style.borderTop = "1px solid var(--color-border)";
167+
168+
const fileNameDisplay = document.createElement("div");
169+
fileNameDisplay.textContent = `Downloading: ${fileName}`;
170+
fileNameDisplay.style.color = "var(--color-text)";
171+
fileNameDisplay.style.marginBottom = "5px";
172+
173+
const progressBar = document.createElement("div");
174+
progressBar.style.width = "0%";
175+
progressBar.style.height = "5px";
176+
progressBar.style.backgroundColor = "var(--color-button-download)";
177+
progressBar.style.borderRadius = "5px";
178+
progressBar.style.padding = "5px";
179+
180+
progressDiv.appendChild(fileNameDisplay);
181+
progressDiv.appendChild(progressBar);
182+
document.body.appendChild(progressDiv);
183+
184+
xhr.onprogress = (event) => {
185+
if (event.lengthComputable) {
186+
const percentComplete = (event.loaded / event.total) * 100;
187+
progressBar.style.width = percentComplete + "%";
188+
}
189+
};
190+
191+
xhr.onload = () => {
192+
if (xhr.status === 200) {
193+
const blob = xhr.response;
194+
const downloadUrl = window.URL.createObjectURL(blob);
195+
const a = document.createElement("a");
196+
a.href = downloadUrl;
197+
a.download = fileName; // Use the provided filename
198+
document.body.appendChild(a);
199+
a.click();
200+
document.body.removeChild(a);
201+
window.URL.revokeObjectURL(downloadUrl);
202+
}
203+
document.body.removeChild(progressDiv); // Remove progress bar after download
204+
};
205+
206+
xhr.onerror = () => {
207+
console.error("Download failed");
208+
document.body.removeChild(progressDiv); // Remove progress bar on error
209+
};
210+
211+
xhr.send();
212+
}
213+
130214
document.getElementById('dismiss-button').addEventListener('click', () => {
131215
const notification = document.getElementById('notification');
132216
notification.classList.add('hidden');
@@ -166,7 +250,7 @@ function importTheme(event) {
166250
const file = event.target.files[0];
167251
if (file) {
168252
const reader = new FileReader();
169-
reader.onload = function(e) {
253+
reader.onload = function (e) {
170254
try {
171255
const customThemes = JSON.parse(e.target.result);
172256
for (const [name, theme] of Object.entries(customThemes)) {
@@ -238,34 +322,6 @@ function loadTheme(themeName) {
238322
}
239323
}
240324

241-
async function checkForUpdates() {
242-
const releases = await fetchReleases();
243-
const latestRelease = releases[0]; // Get the latest release
244-
245-
if (latestRelease && isNewerVersion(version, latestRelease.tag_name)) {
246-
// Show notification for new version
247-
const notification = document.getElementById('notification');
248-
const message = document.getElementById('message');
249-
const downloadLink = document.getElementById('download-link');
250-
message.innerText = `New version ${latestRelease.tag_name} is available!`;
251-
downloadLink.href = latestRelease.assets[0].browser_download_url; // Assuming the first asset is the setup file
252-
downloadLink.innerText = 'Download';
253-
downloadLink.onclick = (e) => {
254-
e.preventDefault();
255-
const link = document.createElement('a');
256-
link.href = latestRelease.assets[0].browser_download_url;
257-
link.download = '';
258-
document.body.appendChild(link);
259-
link.click();
260-
document.body.removeChild(link);
261-
};
262-
notification.classList.remove('hidden');
263-
} else {
264-
// Show popup indicating the app is up-to-date
265-
alert('The app is up-to-date.');
266-
}
267-
}
268-
269325
document.getElementById('sort-options').addEventListener('change', loadNotes);
270326

271327
function sortNotes(notes, option) {
@@ -462,7 +518,7 @@ function importNotes(event) {
462518
Array.from(files).forEach(file => {
463519
if (file.type === "text/plain") {
464520
const reader = new FileReader();
465-
reader.onload = function(e) {
521+
reader.onload = function (e) {
466522
const content = e.target.result;
467523
const title = file.name.replace('.txt', '');
468524
notes.push({ id: generateUniqueId(), title, content });

0 commit comments

Comments
 (0)