Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support jupyter widget interactive #115

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .changeset/clean-feet-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
"@difizen/libro-cofine-editor-contribution": patch
"@difizen/libro-cofine-editor-core": patch
"@difizen/libro-search-code-cell": patch
"@difizen/libro-cofine-textmate": patch
"@difizen/libro-language-client": patch
"@difizen/libro-cofine-editor": patch
"@difizen/libro-markdown-cell": patch
"@difizen/libro-shared-model": patch
"@difizen/libro-code-editor": patch
"@difizen/libro-prompt-cell": patch
"@difizen/libro-virtualized": patch
"@difizen/libro-codemirror": patch
"@difizen/libro-rendermime": patch
"@difizen/libro-code-cell": patch
"@difizen/libro-markdown": patch
"@difizen/libro-raw-cell": patch
"@difizen/libro-terminal": patch
"@difizen/libro-jupyter": patch
"@difizen/libro-common": patch
"@difizen/libro-kernel": patch
"@difizen/libro-output": patch
"@difizen/libro-search": patch
"@difizen/libro-widget": patch
"@difizen/libro-core": patch
"@difizen/libro-l10n": patch
"@difizen/libro-lab": patch
"@difizen/libro-lsp": patch
"@difizen/libro-toc": patch
"@difizen/libro-docs": patch
---

Enhance widgets underlying capabilities
33 changes: 33 additions & 0 deletions .changeset/gold-lions-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
"@difizen/libro-cofine-editor-contribution": patch
"@difizen/libro-cofine-editor-core": patch
"@difizen/libro-search-code-cell": patch
"@difizen/libro-cofine-textmate": patch
"@difizen/libro-language-client": patch
"@difizen/libro-cofine-editor": patch
"@difizen/libro-markdown-cell": patch
"@difizen/libro-shared-model": patch
"@difizen/libro-code-editor": patch
"@difizen/libro-prompt-cell": patch
"@difizen/libro-virtualized": patch
"@difizen/libro-codemirror": patch
"@difizen/libro-rendermime": patch
"@difizen/libro-code-cell": patch
"@difizen/libro-markdown": patch
"@difizen/libro-raw-cell": patch
"@difizen/libro-terminal": patch
"@difizen/libro-jupyter": patch
"@difizen/libro-common": patch
"@difizen/libro-kernel": patch
"@difizen/libro-output": patch
"@difizen/libro-search": patch
"@difizen/libro-widget": patch
"@difizen/libro-core": patch
"@difizen/libro-l10n": patch
"@difizen/libro-lab": patch
"@difizen/libro-lsp": patch
"@difizen/libro-toc": patch
"@difizen/libro-docs": patch
---

Support widget interactive!
18 changes: 17 additions & 1 deletion packages/libro-core/src/cell/libro-cell-view.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ViewComponent } from '@difizen/mana-app';
import { Deferred } from '@difizen/mana-app';
import { useInject, watch } from '@difizen/mana-app';
import { BaseView, view, ViewInstance, ViewOption } from '@difizen/mana-app';
import { inject } from '@difizen/mana-app';
Expand Down Expand Up @@ -36,7 +37,22 @@ export class LibroCellView extends BaseView implements CellView {
model: CellModel;
protected cellService: CellService;
override view: ViewComponent = LibroCellComponent;
parent: NotebookView;

protected _parent: NotebookView;

get parent() {
return this._parent;
}
set parent(value: NotebookView) {
this._parent = value;
this.parentDefer.resolve(this.parent);
}

protected parentDefer = new Deferred<NotebookView>();

get parentReady() {
return this.parentDefer.promise;
}

@prop()
override className?: string | undefined = 'libro-cell-view-container';
Expand Down
1 change: 1 addition & 0 deletions packages/libro-core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from './settings/index.js';
export * from './virtualized-manager.js';
export * from './virtualized-manager-helper.js';
export * from './libro-workspace-service.js';
export * from './libro-context-key.js';
43 changes: 34 additions & 9 deletions packages/libro-core/src/output/output-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,26 @@ const LibroOutputAreaRender = forwardRef<HTMLDivElement>(
useEffect(() => {
outputArea.onUpdateEmitter.fire();
}, [outputArea.onUpdateEmitter, outputArea.outputs]);

const childrenCannotClear = [];
const children = [];
for (const output of outputArea.outputs) {
if (output.allowClear === false) {
childrenCannotClear.push(output);
} else {
children.push(output);
}
}
return (
<div
className="libro-output-area"
ref={ref}
//设置最小高度,用于优化长文本输出再次执行时的页面的滚动控制
// style={{ minHeight: `${executing ? outputArea.lastOutputContainerHeight + 'px' : 'unset'}` }}
>
{outputArea.outputs.map((output) => {
{childrenCannotClear.map((output) => {
return <ViewRender view={output} key={output.id} />;
})}
{children.map((output) => {
return <ViewRender view={output} key={output.id} />;
})}
</div>
Expand Down Expand Up @@ -103,10 +114,10 @@ export class LibroOutputArea extends BaseView implements BaseOutputArea {
});
}
add = async (output: IOutput): Promise<number> => {
// if (this.clearNext) {
// this.clear();
// this.clearNext = false;
// }
if (this.clearNext) {
this.clear();
this.clearNext = false;
}
// Consolidate outputs if they are stream outputs of the same kind.
if (
isStream(output) &&
Expand Down Expand Up @@ -141,13 +152,28 @@ export class LibroOutputArea extends BaseView implements BaseOutputArea {
} else {
this.lastStream = '';
}
return this.outputs.push(await outputModel);
const model = await outputModel;
model.onDisposed(() => {
this.remove(model);
});
return this.outputs.push(model);
};

protected remove(model: BaseOutputView) {
let outputs = [...this.outputs];
outputs = outputs.filter((item) => item !== model);
this.outputs = outputs;
}

set = async (index: number, output: IOutput) => {
const outputModel = this.doCreateOutput(output);
const current = this.outputs[index];
current.dispose();
this.outputs[index] = await outputModel;
const model = await outputModel;
model.onDisposed(() => {
this.remove(model);
});
this.outputs[index] = model;
};
clear(wait?: boolean | undefined) {
this.lastStream = '';
Expand All @@ -158,7 +184,6 @@ export class LibroOutputArea extends BaseView implements BaseOutputArea {
this.outputs.forEach((output) => {
output.dispose();
});
this.outputs = [];
}
fromJSON = async (values: IOutput[]) => {
if (!values) {
Expand Down
6 changes: 5 additions & 1 deletion packages/libro-core/src/output/output-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class LibroOutputView extends BaseView implements BaseOutputView {

@prop()
raw: IOutput;

@prop()
allowClear = true;

@prop()
data: JSONObject;
@prop()
Expand All @@ -45,7 +49,7 @@ export class LibroOutputView extends BaseView implements BaseOutputView {
render: FC<{ output: BaseOutputView }> = LibroOutputModelRender;

override dispose() {
//
super.dispose();
}
toJSON() {
return this.raw;
Expand Down
5 changes: 5 additions & 0 deletions packages/libro-core/src/output/output-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export interface BaseOutputView extends View {
* Depending on the implementation of the mime model,
*/
setData(options: ISetDataOptions): void;

/**
* undefined is considered allowed
*/
allowClear?: boolean;
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/libro-jupyter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"@difizen/libro-markdown-cell": "^0.1.33",
"@difizen/libro-raw-cell": "^0.1.33",
"@difizen/libro-language-client": "^0.1.33",
"@difizen/libro-widget": "^0.1.33",
"@difizen/mana-app": "latest",
"@difizen/mana-l10n": "latest",
"@ant-design/colors": "^7.0.0",
Expand Down
11 changes: 10 additions & 1 deletion packages/libro-jupyter/src/cell/jupyter-code-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ const JupyterCodeCellComponent = forwardRef<HTMLDivElement>(
@transient()
@view('jupyter-code-cell-view')
export class JupyterCodeCellView extends LibroCodeCellView {
declare parent: LibroJupyterView;
protected declare _parent: LibroJupyterView;

override get parent() {
return this._parent;
}
override set parent(value: LibroJupyterView) {
this._parent = value;
this.parentDefer.resolve(this.parent);
}

override view = JupyterCodeCellComponent;
declare model: JupyterCodeCellModel;

Expand Down
1 change: 1 addition & 0 deletions packages/libro-jupyter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * from './toolbar/index.js';
export * from './file/index.js';
export * from './libro-jupyter-view.js';
export * from './config/index.js';
export * from './widget/index.js';
4 changes: 1 addition & 3 deletions packages/libro-jupyter/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
import { RawCellModule } from '@difizen/libro-raw-cell';
import { LibroSearchModule } from '@difizen/libro-search';
import { SearchCodeCellModule } from '@difizen/libro-search-code-cell';
import { WidgetModule } from '@difizen/libro-widget';
import { ManaModule } from '@difizen/mana-app';

import { LibroBetweenCellModule } from './add-between-cell/index.js';
Expand Down Expand Up @@ -57,7 +56,7 @@ import {
LibroJupyterToolbarContribution,
SaveFileErrorContribution,
} from './toolbar/index.js';
import { LibroWidgetMimeContribution } from './widget/index.js';
import { WidgetModule } from './widget/index.js';

export const LibroJupyterModule = ManaModule.create()
.register(
Expand All @@ -76,7 +75,6 @@ export const LibroJupyterModule = ManaModule.create()
LibroJupyterSettingContribution,
JupyterServerLaunchManager,
LibroJupyterView,
LibroWidgetMimeContribution,
{
token: CellExecutionTimeProvider,
useValue: CellExecutionTip,
Expand Down
Loading
Loading