Skip to content

Commit

Permalink
refactor(jupyter): use ContentSaveContribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk committed Apr 1, 2024
1 parent de73431 commit cc94f1d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 56 deletions.
33 changes: 33 additions & 0 deletions .changeset/dull-lamps-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
"@difizen/libro-jupyter": patch
"@difizen/libro-docs": patch
"@difizen/libro-code-cell": patch
"@difizen/libro-code-editor": patch
"@difizen/libro-codemirror": patch
"@difizen/libro-cofine-editor": patch
"@difizen/libro-cofine-editor-contribution": patch
"@difizen/libro-cofine-editor-core": patch
"@difizen/libro-cofine-textmate": patch
"@difizen/libro-common": patch
"@difizen/libro-core": patch
"@difizen/libro-kernel": patch
"@difizen/libro-l10n": patch
"@difizen/libro-lab": patch
"@difizen/libro-language-client": patch
"@difizen/libro-lsp": patch
"@difizen/libro-markdown": patch
"@difizen/libro-markdown-cell": patch
"@difizen/libro-output": patch
"@difizen/libro-prompt-cell": patch
"@difizen/libro-raw-cell": patch
"@difizen/libro-rendermime": patch
"@difizen/libro-search": patch
"@difizen/libro-search-code-cell": patch
"@difizen/libro-shared-model": patch
"@difizen/libro-terminal": patch
"@difizen/libro-toc": patch
"@difizen/libro-virtualized": patch
"@difizen/libro-widget": patch
---

1.refactor(jupyter): use ContentSaveContribution
67 changes: 67 additions & 0 deletions packages/libro-jupyter/src/contents/save-content-contribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { NotebookOption } from '@difizen/libro-core';
import { ContentSaveContribution } from '@difizen/libro-core';
import type { IContentsModel } from '@difizen/libro-kernel';
import { ModalService, inject, singleton } from '@difizen/mana-app';

import type { LibroJupyterModel } from '../libro-jupyter-model.js';
import { SaveFileErrorModal } from '../toolbar/save-file-error.js';

@singleton({ contrib: ContentSaveContribution })
export class LibroJupyterContentSaveContribution implements ContentSaveContribution {
@inject(ModalService) protected readonly modalService: ModalService;

canHandle = () => {
return 2;
};
saveContent = async (options: NotebookOption, model: LibroJupyterModel) => {
const notebookContent = model.toJSON();
if (!model.currentFileContents) {
throw new Error('currentFileContents is undefined');
}

let res = {} as IContentsModel | undefined;

try {
res = await model.fileService.write(notebookContent, model.currentFileContents);
if (!res) {
return;
}
// 文件保存失败
if (res.last_modified === model.lastModified || res.size === 0) {
const errorMsg = `File Save Error: ${res?.message} `;
model.fileService.fileSaveErrorEmitter.fire({
cause: res.message,
msg: errorMsg,
name: res.name,
path: res.path,
created: res.created,
last_modified: res.last_modified,
size: res.size,
type: res.type,
});
this.modalService.openModal(SaveFileErrorModal);

throw new Error(errorMsg);
}
} catch (e: any) {
if (!res) {
return;
}
model.fileService.fileSaveErrorEmitter.fire({
cause: e.errorCause,
msg: e.message,
name: res.name || model.currentFileContents.name,
path: res.path || model.currentFileContents.path,
created: res.created || model.currentFileContents.created,
last_modified: res.last_modified || model.currentFileContents.last_modified,
size: res.size || model.currentFileContents.size,
type: res.type || model.currentFileContents.type,
});
this.modalService.openModal(SaveFileErrorModal);

throw new Error('File Save Error');
}

await model.createCheckpoint();
};
}
56 changes: 0 additions & 56 deletions packages/libro-jupyter/src/libro-jupyter-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
libroArgsMimetype,
LibroFileService,
} from './libro-jupyter-protocol.js';
import { SaveFileErrorModal } from './toolbar/save-file-error.js';
import { getDefaultKernel } from './utils/index.js';

type IModel = IContentsModel;
Expand Down Expand Up @@ -175,61 +174,6 @@ export class LibroJupyterModel extends LibroModel implements ExecutableNotebookM
.catch(console.error);
}

override async saveNotebookContent(): Promise<void> {
const notebookContent = this.toJSON();
if (!this.currentFileContents) {
throw new Error('currentFileContents is undefined');
}

let res = {} as IModel | undefined;

try {
res = await this.libroFileService.write(
notebookContent,
this.currentFileContents,
);
if (!res) {
return;
}
// 文件保存失败
if (res.last_modified === this.last_modified || res.size === 0) {
const errorMsg = `File Save Error: ${res?.message} `;
this.libroFileService.fileSaveErrorEmitter.fire({
cause: res.message,
msg: errorMsg,
name: res.name,
path: res.path,
created: res.created,
last_modified: res.last_modified,
size: res.size,
type: res.type,
});
this.modalService.openModal(SaveFileErrorModal);

throw new Error(errorMsg);
}
} catch (e: any) {
if (!res) {
return;
}
this.libroFileService.fileSaveErrorEmitter.fire({
cause: e.errorCause,
msg: e.message,
name: res.name || this.currentFileContents.name,
path: res.path || this.currentFileContents.path,
created: res.created || this.currentFileContents.created,
last_modified: res.last_modified || this.currentFileContents.last_modified,
size: res.size || this.currentFileContents.size,
type: res.type || this.currentFileContents.type,
});
this.modalService.openModal(SaveFileErrorModal);

throw new Error('File Save Error');
}

await this.createCheckpoint();
}

override canRun() {
if (!this.kernelConnection) {
alert(l10n.t('Kernel Connection 还没有建立'));
Expand Down
2 changes: 2 additions & 0 deletions packages/libro-jupyter/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
LibroJupyterSettingContribution,
} from './config/index.js';
import { LibroJupyterContentContribution } from './contents/index.js';
import { LibroJupyterContentSaveContribution } from './contents/save-content-contribution.js';
import { LibroJupyterFileModule } from './file/index.js';
import { KeybindInstructionsModule } from './keybind-instructions/index.js';
import { LibroJupyterFileService } from './libro-jupyter-file-service.js';
Expand Down Expand Up @@ -67,6 +68,7 @@ export const LibroJupyterModule = ManaModule.create()
SaveFileErrorContribution,
LibroKeybindRegistry,
LibroJupyterContentContribution,
LibroJupyterContentSaveContribution,
LibroJupyterOutputArea,
LibroJupyterColorContribution,
LibroJupyterSettingContribution,
Expand Down

0 comments on commit cc94f1d

Please sign in to comment.