From cd81655ee46e9b00aab187d87b66c9c297a63d92 Mon Sep 17 00:00:00 2001 From: sunshinesmilelk <1176136681@qq.com> Date: Wed, 18 Sep 2024 10:45:26 +0800 Subject: [PATCH] refactor(core): move run from CellView to ExecutableCellView --- .changeset/cyan-weeks-share.md | 34 +++++++++++++++++++ .../libro-core/src/cell/libro-cell-model.ts | 4 --- .../libro-core/src/cell/libro-cell-view.tsx | 2 +- .../src/cell/libro-executable-cell-view.ts | 4 ++- packages/libro-core/src/libro-protocol.ts | 2 -- packages/libro-core/src/libro-view.tsx | 11 +++--- 6 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 .changeset/cyan-weeks-share.md diff --git a/.changeset/cyan-weeks-share.md b/.changeset/cyan-weeks-share.md new file mode 100644 index 00000000..94e188f4 --- /dev/null +++ b/.changeset/cyan-weeks-share.md @@ -0,0 +1,34 @@ +--- +'@difizen/libro-prompt-cell': patch +'@difizen/libro-sql-cell': patch +'@difizen/libro-core': patch +'@difizen/libro-l10n': patch +'@difizen/libro-lab': 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-jupyter': patch +'@difizen/libro-kernel': 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-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. add sql cell & 2.refactor(core): move run from CellView to ExecutableCellView diff --git a/packages/libro-core/src/cell/libro-cell-model.ts b/packages/libro-core/src/cell/libro-cell-model.ts index 4ca6e7ec..eb156b0f 100644 --- a/packages/libro-core/src/cell/libro-cell-model.ts +++ b/packages/libro-core/src/cell/libro-cell-model.ts @@ -96,10 +96,6 @@ export class LibroCellModel extends Model implements CellModel { return { ...this._decodeObject, value: this.value }; } - async run() { - return true; - } - toJSON(): Omit { return { id: this.id, diff --git a/packages/libro-core/src/cell/libro-cell-view.tsx b/packages/libro-core/src/cell/libro-cell-view.tsx index e6c6bc27..08b85c65 100644 --- a/packages/libro-core/src/cell/libro-cell-view.tsx +++ b/packages/libro-core/src/cell/libro-cell-view.tsx @@ -137,7 +137,7 @@ export class LibroCellView extends BaseView implements CellView { super.dispose(); } toJSON(): LibroCell { - const meta = { ...(this.model.toJSON() as LibroCell) }; + const meta = { ...(this.model.toJSON() as LibroCell), outputs: [] }; const modelContribution = this.cellService.findModelProvider(this.model.options); if (modelContribution?.cellMeta.nbformatType) { meta.metadata.libroCellType = modelContribution?.cellMeta.type; diff --git a/packages/libro-core/src/cell/libro-executable-cell-view.ts b/packages/libro-core/src/cell/libro-executable-cell-view.ts index 0523148f..02f5bc14 100644 --- a/packages/libro-core/src/cell/libro-executable-cell-view.ts +++ b/packages/libro-core/src/cell/libro-executable-cell-view.ts @@ -11,6 +11,7 @@ import { EditorCellView, LibroEditorCellView } from './libro-edit-cell-view.js'; import { ExecutableCellModel } from './libro-executable-cell-model.js'; interface ExecutableCellView extends EditorCellView { + run: () => Promise; clearExecution: () => void; outputArea: BaseOutputArea; @@ -27,7 +28,8 @@ export const ExecutableCellView = { 'outputArea' in arg && typeof (arg as any).outputArea === 'object' && 'clearExecution' in arg && - typeof (arg as any).clearExecution === 'function' + typeof (arg as any).clearExecution === 'function' && + typeof (arg as any).run === 'function' ); }, }; diff --git a/packages/libro-core/src/libro-protocol.ts b/packages/libro-core/src/libro-protocol.ts index fb524a46..da11db4f 100644 --- a/packages/libro-core/src/libro-protocol.ts +++ b/packages/libro-core/src/libro-protocol.ts @@ -250,8 +250,6 @@ export interface CellView extends View { blur: () => void; focus: (isEdit: boolean) => void; - run: () => Promise; - shouldEnterEditorMode: (e: React.FocusEvent) => boolean; hasCellHidden: () => boolean; diff --git a/packages/libro-core/src/libro-view.tsx b/packages/libro-core/src/libro-view.tsx index 7733f8d4..bad07c16 100644 --- a/packages/libro-core/src/libro-view.tsx +++ b/packages/libro-core/src/libro-view.tsx @@ -584,7 +584,11 @@ export class LibroView extends BaseView implements NotebookView { executeCellRun(cell: CellView) { this.runCellEmitter.fire(cell); - return cell.run(); + if (ExecutableCellView.is(cell)) { + return cell.run(); + } else { + return undefined; + } } runCells = async (cells: CellView[]) => { @@ -594,10 +598,7 @@ export class LibroView extends BaseView implements NotebookView { return Promise.all( cells.map((cell) => { - if (ExecutableCellModel.is(cell.model)) { - return this.executeCellRun(cell); - } - return undefined; + return this.executeCellRun(cell); }), ) .then((resultList) => {