Skip to content

Commit

Permalink
Fix localStorage keys by using settingsStore consistently #1171 (#1185)
Browse files Browse the repository at this point in the history
Signed off by @Greeshmanth1909
  • Loading branch information
Greeshmanth1909 authored Dec 29, 2023
1 parent 4403292 commit fbc5af7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
10 changes: 5 additions & 5 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ function displayFileSelect () {

document.getElementById('archiveList').addEventListener('change', function (e) {
// handle zim selection from dropdown if multiple files are loaded via webkitdirectory or filesystem api
localStorage.setItem('previousZimFileName', e.target.value);
settingsStore.setItem('previousZimFileName', e.target.value, Infinity);
if (params.isFileSystemApiSupported) {
return abstractFilesystemAccess.getSelectedZimFromCache(e.target.value).then(function (files) {
setLocalArchiveFromFileList(files);
Expand All @@ -1399,7 +1399,7 @@ function displayFileSelect () {
});
} else {
if (webKitFileList === null) {
const element = localStorage.getItem('zimFilenames').split('|').length === 1 ? 'archiveFiles' : 'archiveFolders';
const element = settingsStore.getItem('zimFilenames').split('|').length === 1 ? 'archiveFiles' : 'archiveFolders';
if ('showPicker' in HTMLInputElement.prototype) {
document.getElementById(element).showPicker();
return;
Expand Down Expand Up @@ -1427,7 +1427,7 @@ function displayFileSelect () {
const filenames = [];

const previousZimFile = []
const lastFilename = localStorage.getItem('previousZimFileName') ?? '';
const lastFilename = settingsStore.getItem('previousZimFileName') ?? '';
const filenameWithoutExtension = lastFilename.replace(/\.zim\w\w$/i, '');
const regex = new RegExp(`\\${filenameWithoutExtension}.zim\\w\\w$`, 'i');

Expand All @@ -1436,7 +1436,7 @@ function displayFileSelect () {
if (regex.test(file.name) || file.name === lastFilename) previousZimFile.push(file);
}
webKitFileList = e.target.files;
localStorage.setItem('zimFilenames', filenames.join('|'));
settingsStogare.setItem('zimFilenames', filenames.join('|'), Infinity);
// will load the old file if the selected folder contains the same file
if (previousZimFile.length !== 0) setLocalArchiveFromFileList(previousZimFile);
await abstractFilesystemAccess.updateZimDropdownOptions(filenames, previousZimFile.length !== 0 ? lastFilename : '');
Expand Down Expand Up @@ -1472,7 +1472,7 @@ function useLegacyFilePicker () {
archiveFiles.addEventListener('change', async function (e) {
if (params.isWebkitDirApiSupported || params.isFileSystemApiSupported) {
const activeFilename = e.target.files[0].name;
localStorage.setItem('zimFilenames', [activeFilename].join('|'));
settingsStore.setItem('zimFilenames', [activeFilename].join('|'), Infinity);
await abstractFilesystemAccess.updateZimDropdownOptions([activeFilename], activeFilename);
}
setLocalArchiveFromFileSelect();
Expand Down
27 changes: 14 additions & 13 deletions www/js/lib/abstractFilesystemAccess.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* abstractFilesystemAccess.js: Abstraction layer for file access.
* This is currently only implemented for FirefoxOS and Standard browser (using File System Access API), but could be extended to
* Cordova, Electron or other ways to directly browse and read files from the
Expand Down Expand Up @@ -27,6 +27,7 @@

import cache from './cache.js';
import translateUI from './translateUI.js';
import settingsStore from './settingsStore.js';

function StorageFirefoxOS (storage) {
this._storage = storage;
Expand Down Expand Up @@ -137,20 +138,20 @@ async function selectDirectoryFromPickerViaFileSystemApi () {
const fileNames = [];
const previousZimFile = []

const lastZimNameWithoutExtension = (localStorage.getItem('previousZimFileName') ?? '').replace(/\.zim\w\w$/i, '');
const lastZimNameWithoutExtension = (settingsStore.getItem('previousZimFileName') ?? '').replace(/\.zim\w\w$/i, '');
const regex = new RegExp(`\\${lastZimNameWithoutExtension}.zim\\w\\w$`, 'i');

for await (const entry of handle.values()) {
fileNames.push(entry.name);
if (regex.test(entry.name) || entry.name === (localStorage.getItem('previousZimFileName') ?? '')) previousZimFile.push(await entry.getFile());
if (regex.test(entry.name) || entry.name === (settingsStore.getItem('previousZimFileName') ?? '')) previousZimFile.push(await entry.getFile());
}

localStorage.setItem('zimFilenames', fileNames.join('|'));
updateZimDropdownOptions(fileNames, previousZimFile.length !== 0 ? localStorage.getItem('previousZimFileName') : '');
settingsStore.setItem('zimFilenames', fileNames.join('|'), Infinity);
updateZimDropdownOptions(fileNames, previousZimFile.length !== 0 ? settingsStore.getItem('previousZimFileName') : '');
cache.idxDB('zimFiles', handle, function () {
// save file in DB
});
return previousZimFile;
return previousZimFile;
}

/**
Expand All @@ -162,7 +163,7 @@ async function selectFileFromPickerViaFileSystemApi () {
const [selectedFile] = fileHandles;
const file = await selectedFile.getFile();
const filenameList = [selectedFile.name];
localStorage.setItem('zimFilenames', filenameList.join('|'));
settingsStore.setItem('zimFilenames', filenameList.join('|'));
cache.idxDB('zimFiles', selectedFile, function () {
// file saved in DB
updateZimDropdownOptions(filenameList, selectedFile.name);
Expand Down Expand Up @@ -234,7 +235,7 @@ function loadPreviousZimFile () {
// It's a bit hacky but it works and I am not sure if there is any other way ATM
setTimeout(() => {
if (window.params.isFileSystemApiSupported || window.params.isWebkitDirApiSupported) {
const filenames = localStorage.getItem('zimFilenames');
const filenames = settingsStore.getItem('zimFilenames');
if (filenames) updateZimDropdownOptions(filenames.split('|'), '');
}
}, 200);
Expand All @@ -252,20 +253,20 @@ async function handleFolderOrFileDropViaFileSystemAPI (packet) {
const fileInfo = packet.dataTransfer.items[0];
const fileOrDirHandle = await fileInfo.getAsFileSystemHandle();
if (fileOrDirHandle.kind === 'file') {
localStorage.setItem([fileOrDirHandle.name], [fileOrDirHandle.name].join('|'));
settingsStore.setItem([fileOrDirHandle.name], [fileOrDirHandle.name].join('|'), Infinity);
cache.idxDB('zimFiles', fileOrDirHandle, function () {
// save file in DB
updateZimDropdownOptions([fileOrDirHandle.name], fileOrDirHandle.name);
});
localStorage.setItem('zimFilenames', [fileOrDirHandle.name].join('|'));
settingsStore.setItem('zimFilenames', [fileOrDirHandle.name].join('|'), Infinity);
return true;
}
if (fileOrDirHandle.kind === 'directory') {
const fileNames = [];
for await (const entry of fileOrDirHandle.values()) {
fileNames.push(entry.name);
}
localStorage.setItem('zimFilenames', fileNames.join('|'));
settingsStore.setItem('zimFilenames', fileNames.join('|'), Infinity);
cache.idxDB('zimFiles', fileOrDirHandle, function () {
updateZimDropdownOptions(fileNames, '');
// save file in DB
Expand All @@ -284,15 +285,15 @@ async function handleFolderOrFileDropViaWebkit (event) {

var entry = dt.items[0].webkitGetAsEntry();
if (entry.isFile) {
localStorage.setItem('zimFilenames', [entry.name].join('|'));
settingsStore.setItem('zimFilenames', [entry.name].join('|'), Infinity);
await updateZimDropdownOptions([entry.name], entry.name);
return { loadZim: true, files: [entry.file] };
} else if (entry.isDirectory) {
var reader = entry.createReader();
const files = await getFilesFromReader(reader);
const fileNames = [];
files.forEach((file) => fileNames.push(file.name));
localStorage.setItem('zimFilenames', fileNames.join('|'));
settingsStore.setItem('zimFilenames', fileNames.join('|'), Infinity);
await updateZimDropdownOptions(fileNames, '');
return { loadZim: false, files: files };
}
Expand Down

0 comments on commit fbc5af7

Please sign in to comment.