Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced localStorage with settingsStore method #1185

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a note to self, not part of this PR, but this timeout may well be the problem with inconsistency in loading the contents of archives. It introduces a race condition, and needs looking at as a separate issue.

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