Skip to content

Commit 6e26672

Browse files
authored
Merge pull request #5 from jtpio/save
Basic support for saving a file
2 parents c95f4a6 + 8b11e32 commit 6e26672

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/drive.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ export class FileSystemDrive implements Contents.IDrive {
4242
const root = this._rootHandle;
4343

4444
if (!root) {
45-
throw new Error('Files not available');
45+
return {
46+
name: '',
47+
path: '',
48+
created: new Date().toISOString(),
49+
last_modified: new Date().toISOString(),
50+
format: 'json',
51+
content: null,
52+
writable: true,
53+
type: 'directory',
54+
mimetype: 'application/json'
55+
};
4656
}
4757

4858
if (localPath) {
@@ -123,11 +133,22 @@ export class FileSystemDrive implements Contents.IDrive {
123133
throw new Error('Method not implemented.');
124134
}
125135

126-
save(
136+
async save(
127137
localPath: string,
128138
options?: Partial<Contents.IModel>
129139
): Promise<Contents.IModel> {
130-
throw new Error('Method not implemented.');
140+
const root = this._rootHandle;
141+
142+
if (!root) {
143+
throw new Error('No root file handle');
144+
}
145+
146+
const handle = await root.getFileHandle(localPath);
147+
const writable = await handle.createWritable({});
148+
const content = options?.content;
149+
await writable.write(content);
150+
await writable.close();
151+
return this.get(localPath);
131152
}
132153

133154
copy(localPath: string, toLocalDir: string): Promise<Contents.IModel> {

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const plugin: JupyterFrontEndPlugin<void> = {
2727
) => {
2828
if (!window.showDirectoryPicker) {
2929
// bail if the browser does not support the File System API
30-
console.warn('The File System API is not supported in this browser.');
30+
console.warn(
31+
'The File System Access API is not supported in this browser.'
32+
);
3133
return;
3234
}
3335

0 commit comments

Comments
 (0)