-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fa1ba2
commit 28c7fde
Showing
3 changed files
with
141 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* @typedef {Object} FileSystemHandlers | ||
* @property {Array<string>} files All the File names to be shown in the dropdown | ||
* @property {Object} fileOrDirHandle The FileSystemHandle of the selected file or directory | ||
*/ | ||
|
||
import cache from './cache.js'; | ||
|
||
/** | ||
* @param {FileSystemHandlers} fileSystemHandler | ||
*/ | ||
async function updateZimDropdownOptions (fileSystemHandler, selectedFile) { | ||
const select = document.getElementById('zimSelectDropdown') | ||
let options = '' | ||
fileSystemHandler.files.forEach(fileName => { | ||
options += `<option value="${fileName}">${fileName}</option>` | ||
}); | ||
select.innerHTML = options | ||
document.getElementById('zimSelectDropdown').value = selectedFile | ||
} | ||
|
||
async function selectDirectoryFromPicker () { | ||
const handle = await window.showDirectoryPicker(); | ||
const fileNames = [] | ||
for await (const entry of handle.values()) { | ||
fileNames.push(entry.name) | ||
} | ||
|
||
/** @type FileSystemHandlers */ | ||
const FSHandler = { | ||
fileOrDirHandle: handle, | ||
files: fileNames | ||
} | ||
updateZimDropdownOptions(FSHandler, '') | ||
cache.idxDB('zimFiles', FSHandler, function () { | ||
// save file in DB | ||
}); | ||
} | ||
|
||
async function selectFileFromPicker () { | ||
const fileHandles = await window.showOpenFilePicker({ multiple: false }) | ||
const [selectedFile] = fileHandles | ||
const file = await selectedFile.getFile(); | ||
|
||
/** @type FileSystemHandlers */ | ||
const FSHandler = { | ||
fileOrDirHandle: selectedFile, | ||
files: [selectedFile.name] | ||
} | ||
cache.idxDB('zimFiles', FSHandler, function () { | ||
// file saved in DB | ||
}) | ||
updateZimDropdownOptions(FSHandler, selectedFile.name) | ||
return [file]; | ||
} | ||
|
||
function changeSelectedZim (selectedFilename) { | ||
return new Promise((resolve, reject) => { | ||
cache.idxDB('zimFiles', async function (FSHandler) { | ||
// const selectedFile = FSHandler.fileOrDirHandle | ||
console.log(await FSHandler.fileOrDirHandle.queryPermission()); | ||
if (await FSHandler.fileOrDirHandle.queryPermission() !== 'granted') await FSHandler.fileOrDirHandle.requestPermission() | ||
let file = null | ||
if (FSHandler.fileOrDirHandle.kind === 'directory') { | ||
file = await (await FSHandler.fileOrDirHandle.getFileHandle(selectedFilename)).getFile() | ||
resolve(file) | ||
} else { | ||
file = await FSHandler.fileOrDirHandle.getFile(); | ||
resolve(file) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* Loads the Previously selected zim file via IndexedDB | ||
*/ | ||
function loadPreviousZimFile () { | ||
if (typeof window.showOpenFilePicker === 'function') { | ||
cache.idxDB('zimFiles', async function (FSHandler) { | ||
if (!FSHandler) return console.info('There is no previous zim file in DB') | ||
updateZimDropdownOptions(FSHandler, '') | ||
// refer to this article for easy explanation https://developer.chrome.com/articles/file-system-access/ | ||
}) | ||
} | ||
} | ||
|
||
async function onFileOrFolderDrop (packet) { | ||
if (typeof window.showOpenFilePicker !== 'function') return false | ||
|
||
// Only runs when browser support File System API | ||
const fileInfo = await packet.dataTransfer.items[0] | ||
const fileOrDirHandle = await fileInfo.getAsFileSystemHandle(); | ||
if (fileOrDirHandle.kind === 'file') { | ||
cache.idxDB('zimFile', fileOrDirHandle, function () { | ||
// save file in DB | ||
}); | ||
return false | ||
} | ||
// will be later on used | ||
if (fileOrDirHandle.kind === 'directory') { | ||
// const dirHandle = fileInfo.getAsFileSystemHandle(); | ||
const fileNames = [] | ||
for await (const entry of fileOrDirHandle.values()) { | ||
fileNames.push(entry.name) | ||
} | ||
/** @type FileSystemHandlers */ | ||
const FSHandler = { | ||
fileOrDirHandle: fileOrDirHandle, | ||
files: fileNames | ||
} | ||
updateZimDropdownOptions(FSHandler, '') | ||
console.log(FSHandler, fileNames); | ||
return true | ||
} | ||
} | ||
|
||
export default { | ||
updateZimDropdownOptions, | ||
selectDirectoryFromPicker, | ||
selectFileFromPicker, | ||
changeSelectedZim, | ||
loadPreviousZimFile, | ||
onFileOrFolderDrop | ||
} |