Skip to content

Commit

Permalink
Offline+Chrome fixes
Browse files Browse the repository at this point in the history
- Chrome+Edge don't trivially accept dropped folders when the page is
  running as a local file:/// origin: danialfarid/ng-file-upload#236
  Setting `inputFile.webkitdirectory = true` allows for *browsing* to
  pick folders in this situation, but it only accepts folders and
  prevents individual files from being picked, so I've left it commented
  out while leaving the later logic it needs to work.

- This means local usage+development is best experienced with either
  Firefox or (for Chrome/Edge) a simple http server.

- Chrome+Edge don't always set file.webkitRelativePath, here we just
  patch it in but there might be a cleaner way.
  ant-design/ant-design#16426 (comment)
  • Loading branch information
aquacluck committed Feb 11, 2024
1 parent 5e402e8 commit 63fc927
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions savegame-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,22 @@ MarcDragAndDrop=(function(){
async function toFilePromise(fileEntry) {
try {
return await new Promise((resolve, reject) => {
fileEntry.file(resolve, reject);
fileEntry.file(function(file){
// Patch webkitRelativePath for Chrome which doesn't always set it? https://github.com/ant-design/ant-design/issues/16426
// TODO could we just pass fileEntry.fullpath out instead of patching this?
Object.defineProperties(file, {
webkitRelativePath: {
writable: true,
},
});
file.webkitRelativePath = file.webkitRelativePath || fileEntry.fullPath.replace(/^\//, '');
Object.defineProperties(file, {
webkitRelativePath: {
writable: false,
},
});
resolve(file);
}, reject);
});
} catch (err) {
console.log(err);
Expand Down Expand Up @@ -196,10 +211,6 @@ function _tempFileLoadFunction(){
}
}

function loadSavegameFromInput(input){
tempFile=new MarcFile(input.files[0], _tempFileLoadFunction);
}

function saveChanges(){
if(decodeURIComponent(document.cookie).indexOf('hideWarningMessage=1')>=0 || location.protocol==='file:'){ /* chrome does not write cookies in local, so skip warning message in that case */
SavegameEditor.save();
Expand Down Expand Up @@ -259,8 +270,17 @@ window.addEventListener('load', function(){
inputFile.type='file';
inputFile.className='hidden';
inputFile.id='file-load';
inputFile.addEventListener('change', function(){
loadSavegameFromInput(this);
// Requires a folder for "browse window" picking, but this works when running webpage from filesystem on Chrome, where dropping folders does not work.
// `webkitGetAsEntry` may be a better workaround https://github.com/danialfarid/ng-file-upload/issues/236#issuecomment-45053629
// inputFile.webkitdirectory = true;
inputFile.addEventListener('change', async function(evt){
if(this.files.length == 1 || typeof SavegameEditor.showSavegameIndex === 'undefined') {
// Load savegame from file
tempFile=new MarcFile(this.files[0], _tempFileLoadFunction);
} else {
// Some games have a complex structure of multiple savegames, so we provide a custom picker+overview
await SavegameEditor.showSavegameIndex(this.files);
}
}, false);

dragZone.appendChild(dragMessage);
Expand Down
2 changes: 1 addition & 1 deletion zelda-totk/zelda-totk.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
The legend of Zelda: Tears of the Kingdom savegame editor (last update 2024-01-02)
The legend of Zelda: Tears of the Kingdom savegame editor (last update 2024-02-11)
by Marc Robledo 2023-2024
*/
Expand Down

0 comments on commit 63fc927

Please sign in to comment.