Skip to content

Commit 1e1970d

Browse files
committed
Merge branch 'beta' of https://github.com/circuitpython/web-editor into resize-fix
2 parents ce8c2d0 + 6425df9 commit 1e1970d

File tree

5 files changed

+185
-106
lines changed

5 files changed

+185
-106
lines changed

js/common/file_dialog.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,14 @@ class FileDialog extends GenericModal {
548548
input.click();
549549
}
550550

551-
// Currently only files are downloadable, but it would be nice to eventually download zipped folders
552551
async _handleDownloadButton() {
553-
// TODO: Implement a way to download multiple files at once into a zip file
554-
555-
await this._download(this._getSelectedFilesInfo());
552+
await this._showBusy(this._download(this._getSelectedFilesInfo()));
556553
}
557554

558555
async _download(files) {
559556
if (!this._canDownload()) return;
560557

561-
let folder, blob, filename;
562-
563-
// If we only have 1 item and it is a file, we can download it directly
564-
// Otherwise, we need to zip the files and download the zip keeping the structure intact
558+
let blob, filename;
565559

566560
// Function to read the file contents as a blob
567561
let getBlob = async (path) => {
@@ -576,36 +570,43 @@ class FileDialog extends GenericModal {
576570
}
577571
};
578572

573+
let addFileContentsToZip = async (zip, folder, location) => {
574+
let contents = await getBlob(folder + location);
575+
// Get the filename only from the path
576+
zip.file(location, contents);
577+
};
578+
579579
if (files.length == 1 && files[0].filetype != "folder") {
580+
// Single File Selected
580581
filename = files[0].filename;
581-
blob = await this._showBusy(getBlob(this._currentPath + filename));
582+
blob = await getBlob(this._currentPath + filename);
582583
} else {
583584
// We either have more than 1 item selected or we have a folder selected or we have no file selected and want to download the current folder
584585
// If we have nothing selected, we will download the current folder
585-
folder = this._currentPath;
586+
filename = `${getParentFolderName()}.zip`;
586587
if (files.length == 0) {
587-
files.push({filename: getParentFolderName(), filetype: "folder", path: this._currentPath});
588-
}
588+
// No Files Selected, so get everything in current folder
589+
const filesInFolder = await this._fileHelper.listDir(this._currentPath);
589590

590-
if (files.length == 1) {
591-
filename = files[0].filename;
592-
folder += filename + "/";
593-
filename = `${filename}.zip`;
594-
} else {
595-
filename = `${getParentFolderName()}.zip`;
591+
// Add all files in current folder to files array
592+
for (let fileObj of filesInFolder) {
593+
if (this._hidePaths.has(this._currentPath + fileObj.path)) continue;
594+
files.push({filename: fileObj.path, filetype: fileObj.isDir ? "folder" : "file", path: this._currentPath});
595+
}
596+
} else if (files.length == 1) {
597+
// Single Folder Selected
598+
filename = `${files[0].filename}.zip`;
596599
}
597600

598601
let zip = new JSZip();
599602
for (let item of files) {
600603
if (item.filetype == "folder") {
601-
let containedFiles = await this._fileHelper.findContainedFiles(folder + item + "/", true);
604+
let containedFiles = await this._fileHelper.findContainedFiles(item.path + item.filename + "/", true);
602605
for (let location of containedFiles) {
603-
let contents = await this._showBusy(getBlob(folder + location));
604-
zip.file(location, contents);
606+
await addFileContentsToZip(zip, item.path, item.filename + "/" + location);
605607
}
606608
} else {
607-
let contents = await this._showBusy(getBlob(folder + item.filename));
608-
zip.file(item.filename, contents);
609+
await addFileContentsToZip(zip, item.path, item.filename);
609610
}
610611
}
611612
blob = await zip.generateAsync({type: "blob"});

js/common/repl-file-transfer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class FileTransferClient {
3535
if (contents === null) {
3636
return raw ? null : "";
3737
}
38-
return contents;
38+
if (raw) {
39+
return contents;
40+
}
41+
return contents.replaceAll("\r\n", "\n");
3942
}
4043

4144
async writeFile(path, offset, contents, modificationTime, raw = false) {

js/workflows/usb.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ class USBWorkflow extends Workflow {
125125
// This would help with other workflows as well
126126
} else {
127127
console.log('Requesting any serial device...');
128-
device = await navigator.serial.requestPort();
128+
try {
129+
device = await navigator.serial.requestPort();
130+
} catch (e) {
131+
console.log(e);
132+
return false;
133+
}
129134
}
130135

131136
// If we didn't automatically use a saved device
@@ -155,7 +160,7 @@ class USBWorkflow extends Workflow {
155160
btnSelectHostFolder.disabled = true;
156161
let serialConnect = async (event) => {
157162
try {
158-
await this.connectToSerial();
163+
await this.showBusy(this.connectToSerial());
159164
} catch (e) {
160165
//console.log(e);
161166
//alert(e.message);

0 commit comments

Comments
 (0)