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,27 +399,32 @@ 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) { }
return;
} async selectFS() {
const rootPath = '/' + directoryHandle.name; const fs = this.#fileTree_.getFS();
this.#fileTree_.setFolderPath('/'); const directoryHandle = await fs.showDirectoryPicker();
this.#fileTree_.setRootFolderTitle(rootPath); await this.openFS(directoryHandle);
this.#fileTree_.setRootFolderName(directoryHandle.name); }
this.#fileTree_.openRootFolder();
this.showFileTree(); async openFS(directoryHandle) {
return window.pyodide.mountNativeFS(rootPath, directoryHandle); if (!directoryHandle?.name) {
}) return;
.then((nativefs) => { }
this.#nativefs_ = nativefs; const rootPath = '/' + directoryHandle.name;
}) this.#fileTree_.setFolderPath('/');
.catch(Debug.error); this.#fileTree_.setRootFolderTitle(rootPath);
this.#fileTree_.setRootFolderName(directoryHandle.name);
this.#fileTree_.openRootFolder();
this.showFileTree();
this.#nativefs_ = await window.pyodide.mountNativeFS(rootPath, directoryHandle);
} }
closeFS() { closeFS() {