diff --git a/.changeset/dull-lamps-shake.md b/.changeset/dull-lamps-shake.md new file mode 100644 index 00000000..1a471b55 --- /dev/null +++ b/.changeset/dull-lamps-shake.md @@ -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 diff --git a/packages/libro-jupyter/src/contents/save-content-contribution.ts b/packages/libro-jupyter/src/contents/save-content-contribution.ts new file mode 100644 index 00000000..271aa5de --- /dev/null +++ b/packages/libro-jupyter/src/contents/save-content-contribution.ts @@ -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(); + }; +} diff --git a/packages/libro-jupyter/src/libro-jupyter-model.ts b/packages/libro-jupyter/src/libro-jupyter-model.ts index d9da505c..f7cc5e18 100644 --- a/packages/libro-jupyter/src/libro-jupyter-model.ts +++ b/packages/libro-jupyter/src/libro-jupyter-model.ts @@ -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; @@ -175,61 +174,6 @@ export class LibroJupyterModel extends LibroModel implements ExecutableNotebookM .catch(console.error); } - override async saveNotebookContent(): Promise { - 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 还没有建立')); diff --git a/packages/libro-jupyter/src/module.ts b/packages/libro-jupyter/src/module.ts index 5603eeb8..edd88a48 100644 --- a/packages/libro-jupyter/src/module.ts +++ b/packages/libro-jupyter/src/module.ts @@ -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'; @@ -67,6 +68,7 @@ export const LibroJupyterModule = ManaModule.create() SaveFileErrorContribution, LibroKeybindRegistry, LibroJupyterContentContribution, + LibroJupyterContentSaveContribution, LibroJupyterOutputArea, LibroJupyterColorContribution, LibroJupyterSettingContribution,