Update: Python Online板卡下「本地文件系统」增加对本地目录操作对象的缓存

This commit is contained in:
王立帮
2024-11-27 14:53:02 +08:00
parent 57a979b118
commit bac0e68cca
3 changed files with 40 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
import { WebAccessFS } from '@zenfs/dom';
import { get, set } from 'idb-keyval';
import { FS } from 'mixly';
@@ -32,6 +33,20 @@ export default class FileSystemFS extends FS {
if (permissionStatus !== 'granted') {
throw new Error('readwrite access to directory not granted');
}
await set('mixly-pyodide-fs', directoryHandle);
this.#fs_ = new WebAccessFSExt(directoryHandle);
return directoryHandle;
}
async loadFS() {
let directoryHandle = await get('mixly-pyodide-fs');
if (!directoryHandle) {
return null;
}
const permissionStatus = await directoryHandle.requestPermission({ mode: 'readwrite' });
if (permissionStatus !== 'granted') {
throw new Error('readwrite access to directory not granted');
}
this.#fs_ = new WebAccessFSExt(directoryHandle);
return directoryHandle;
}

View File

@@ -90,7 +90,7 @@ export default class StatusBarFileSystem extends PageBase {
});
this.#$openFS_.children('button').click(() => {
this.openFS();
this.selectFS().catch(Debug.error);
});
this.#fileTree_.bind('beforeSelectLeaf', (selected) => {
@@ -399,27 +399,32 @@ export default class StatusBarFileSystem extends PageBase {
editor.on('change', () => {
this.setStatus(true);
});
this.loadFS().catch(Debug.error);
}
openFS() {
async loadFS() {
const fs = this.#fileTree_.getFS();
fs.showDirectoryPicker()
.then((directoryHandle) => {
if (!directoryHandle.name) {
return;
}
const rootPath = '/' + directoryHandle.name;
this.#fileTree_.setFolderPath('/');
this.#fileTree_.setRootFolderTitle(rootPath);
this.#fileTree_.setRootFolderName(directoryHandle.name);
this.#fileTree_.openRootFolder();
this.showFileTree();
return window.pyodide.mountNativeFS(rootPath, directoryHandle);
})
.then((nativefs) => {
this.#nativefs_ = nativefs;
})
.catch(Debug.error);
const directoryHandle = await fs.loadFS();
await this.openFS(directoryHandle);
}
async selectFS() {
const fs = this.#fileTree_.getFS();
const directoryHandle = await fs.showDirectoryPicker();
await this.openFS(directoryHandle);
}
async openFS(directoryHandle) {
if (!directoryHandle?.name) {
return;
}
const rootPath = '/' + directoryHandle.name;
this.#fileTree_.setFolderPath('/');
this.#fileTree_.setRootFolderTitle(rootPath);
this.#fileTree_.setRootFolderName(directoryHandle.name);
this.#fileTree_.openRootFolder();
this.showFileTree();
this.#nativefs_ = await window.pyodide.mountNativeFS(rootPath, directoryHandle);
}
closeFS() {