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

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,5 @@
import { WebAccessFS } from '@zenfs/dom'; import { WebAccessFS } from '@zenfs/dom';
import { get, set } from 'idb-keyval';
import { FS } from 'mixly'; import { FS } from 'mixly';
@@ -32,6 +33,20 @@ export default class FileSystemFS extends FS {
if (permissionStatus !== 'granted') { if (permissionStatus !== 'granted') {
throw new Error('readwrite access to directory not 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); this.#fs_ = new WebAccessFSExt(directoryHandle);
return directoryHandle; return directoryHandle;
} }

View File

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