-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(jupyter): use ContentSaveContribution
- Loading branch information
1 parent
de73431
commit cc94f1d
Showing
4 changed files
with
102 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
packages/libro-jupyter/src/contents/save-content-contribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters