@theia/notebook 1.53.0-next.55 → 1.53.0-next.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/index.d.ts +1 -0
  3. package/lib/browser/index.d.ts.map +1 -1
  4. package/lib/browser/index.js +1 -0
  5. package/lib/browser/index.js.map +1 -1
  6. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  7. package/lib/browser/notebook-frontend-module.js +2 -0
  8. package/lib/browser/notebook-frontend-module.js.map +1 -1
  9. package/lib/browser/service/notebook-cell-editor-service.d.ts +16 -0
  10. package/lib/browser/service/notebook-cell-editor-service.d.ts.map +1 -0
  11. package/lib/browser/service/notebook-cell-editor-service.js +53 -0
  12. package/lib/browser/service/notebook-cell-editor-service.js.map +1 -0
  13. package/lib/browser/view/notebook-cell-editor.d.ts +2 -0
  14. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  15. package/lib/browser/view/notebook-cell-editor.js +12 -1
  16. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  17. package/lib/browser/view/notebook-code-cell-view.d.ts +2 -0
  18. package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -1
  19. package/lib/browser/view/notebook-code-cell-view.js +6 -1
  20. package/lib/browser/view/notebook-code-cell-view.js.map +1 -1
  21. package/lib/browser/view/notebook-markdown-cell-view.d.ts +2 -0
  22. package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -1
  23. package/lib/browser/view/notebook-markdown-cell-view.js +8 -3
  24. package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -1
  25. package/package.json +7 -7
  26. package/src/browser/contributions/cell-operations.ts +44 -44
  27. package/src/browser/contributions/notebook-actions-contribution.ts +379 -379
  28. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -525
  29. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  30. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  31. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  32. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  33. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  34. package/src/browser/contributions/notebook-preferences.ts +92 -92
  35. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -77
  36. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  37. package/src/browser/index.ts +28 -27
  38. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  39. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  40. package/src/browser/notebook-editor-widget.tsx +330 -330
  41. package/src/browser/notebook-frontend-module.ts +121 -119
  42. package/src/browser/notebook-open-handler.ts +120 -120
  43. package/src/browser/notebook-output-utils.ts +119 -119
  44. package/src/browser/notebook-renderer-registry.ts +85 -85
  45. package/src/browser/notebook-type-registry.ts +54 -54
  46. package/src/browser/notebook-types.ts +186 -186
  47. package/src/browser/renderers/cell-output-webview.ts +33 -33
  48. package/src/browser/service/notebook-cell-editor-service.ts +56 -0
  49. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  50. package/src/browser/service/notebook-context-manager.ts +162 -162
  51. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  52. package/src/browser/service/notebook-execution-service.ts +139 -139
  53. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  54. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  55. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  56. package/src/browser/service/notebook-kernel-service.ts +357 -357
  57. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  58. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  59. package/src/browser/service/notebook-options.ts +155 -155
  60. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  61. package/src/browser/service/notebook-service.ts +215 -215
  62. package/src/browser/style/index.css +484 -483
  63. package/src/browser/view/notebook-cell-editor.tsx +276 -263
  64. package/src/browser/view/notebook-cell-list-view.tsx +279 -279
  65. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  66. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  67. package/src/browser/view/notebook-code-cell-view.tsx +355 -350
  68. package/src/browser/view/notebook-find-widget.tsx +335 -335
  69. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  70. package/src/browser/view/notebook-markdown-cell-view.tsx +215 -208
  71. package/src/browser/view/notebook-viewport-service.ts +61 -61
  72. package/src/browser/view-model/notebook-cell-model.ts +473 -473
  73. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  74. package/src/browser/view-model/notebook-model.ts +550 -550
  75. package/src/common/index.ts +18 -18
  76. package/src/common/notebook-common.ts +337 -337
  77. package/src/common/notebook-protocol.ts +35 -35
  78. package/src/common/notebook-range.ts +30 -30
@@ -1,350 +1,355 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2023 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { inject, injectable } from '@theia/core/shared/inversify';
18
- import * as React from '@theia/core/shared/react';
19
- import { MonacoEditorServices } from '@theia/monaco/lib/browser/monaco-editor';
20
- import { CellOutputWebviewFactory, CellOutputWebview } from '../renderers/cell-output-webview';
21
- import { NotebookRendererRegistry } from '../notebook-renderer-registry';
22
- import { NotebookCellModel } from '../view-model/notebook-cell-model';
23
- import { NotebookModel } from '../view-model/notebook-model';
24
- import { CellEditor } from './notebook-cell-editor';
25
- import { CellRenderer } from './notebook-cell-list-view';
26
- import { NotebookCellToolbarFactory } from './notebook-cell-toolbar-factory';
27
- import { NotebookCellActionContribution, NotebookCellCommands } from '../contributions/notebook-cell-actions-contribution';
28
- import { CellExecution, NotebookExecutionStateService } from '../service/notebook-execution-state-service';
29
- import { codicon } from '@theia/core/lib/browser';
30
- import { NotebookCellExecutionState } from '../../common';
31
- import { CommandRegistry, DisposableCollection, nls } from '@theia/core';
32
- import { NotebookContextManager } from '../service/notebook-context-manager';
33
- import { NotebookViewportService } from './notebook-viewport-service';
34
- import { EditorPreferences } from '@theia/editor/lib/browser';
35
- import { NotebookOptionsService } from '../service/notebook-options';
36
- import { MarkdownRenderer } from '@theia/core/lib/browser/markdown-rendering/markdown-renderer';
37
- import { MarkdownString } from '@theia/monaco-editor-core/esm/vs/base/common/htmlContent';
38
-
39
- @injectable()
40
- export class NotebookCodeCellRenderer implements CellRenderer {
41
- @inject(MonacoEditorServices)
42
- protected readonly monacoServices: MonacoEditorServices;
43
-
44
- @inject(NotebookRendererRegistry)
45
- protected readonly notebookRendererRegistry: NotebookRendererRegistry;
46
-
47
- @inject(CellOutputWebviewFactory)
48
- protected readonly cellOutputWebviewFactory: CellOutputWebviewFactory;
49
-
50
- @inject(NotebookCellToolbarFactory)
51
- protected readonly notebookCellToolbarFactory: NotebookCellToolbarFactory;
52
-
53
- @inject(NotebookExecutionStateService)
54
- protected readonly executionStateService: NotebookExecutionStateService;
55
-
56
- @inject(NotebookContextManager)
57
- protected readonly notebookContextManager: NotebookContextManager;
58
-
59
- @inject(NotebookViewportService)
60
- protected readonly notebookViewportService: NotebookViewportService;
61
-
62
- @inject(EditorPreferences)
63
- protected readonly editorPreferences: EditorPreferences;
64
-
65
- @inject(CommandRegistry)
66
- protected readonly commandRegistry: CommandRegistry;
67
-
68
- @inject(NotebookOptionsService)
69
- protected readonly notebookOptionsService: NotebookOptionsService;
70
-
71
- @inject(MarkdownRenderer)
72
- protected readonly markdownRenderer: MarkdownRenderer;
73
-
74
- render(notebookModel: NotebookModel, cell: NotebookCellModel, handle: number): React.ReactNode {
75
- return <div>
76
- <div className='theia-notebook-cell-with-sidebar'>
77
- <div className='theia-notebook-cell-sidebar'>
78
- {this.notebookCellToolbarFactory.renderSidebar(NotebookCellActionContribution.CODE_CELL_SIDEBAR_MENU, cell, {
79
- contextMenuArgs: () => [cell], commandArgs: () => [notebookModel, cell]
80
- })
81
- }
82
- <CodeCellExecutionOrder cell={cell} />
83
- </div>
84
- <div className='theia-notebook-cell-editor-container'>
85
- <CellEditor notebookModel={notebookModel} cell={cell}
86
- monacoServices={this.monacoServices}
87
- notebookContextManager={this.notebookContextManager}
88
- notebookViewportService={this.notebookViewportService}
89
- fontInfo={this.notebookOptionsService.editorFontInfo} />
90
- <NotebookCodeCellStatus cell={cell} notebook={notebookModel}
91
- commandRegistry={this.commandRegistry}
92
- executionStateService={this.executionStateService}
93
- onClick={() => cell.requestFocusEditor()} />
94
- </div >
95
- </div >
96
- <div className='theia-notebook-cell-with-sidebar'>
97
- <NotebookCodeCellOutputs cell={cell} notebook={notebookModel} outputWebviewFactory={this.cellOutputWebviewFactory}
98
- renderSidebar={() =>
99
- this.notebookCellToolbarFactory.renderSidebar(NotebookCellActionContribution.OUTPUT_SIDEBAR_MENU, cell, {
100
- contextMenuArgs: () => [notebookModel, cell, cell.outputs[0]]
101
- })
102
- } />
103
- </div>
104
- </div >;
105
- }
106
-
107
- renderDragImage(cell: NotebookCellModel): HTMLElement {
108
- const dragImage = document.createElement('div');
109
- dragImage.className = 'theia-notebook-drag-image';
110
- dragImage.style.width = this.notebookContextManager.context?.clientWidth + 'px';
111
- dragImage.style.height = '100px';
112
- dragImage.style.display = 'flex';
113
-
114
- const fakeRunButton = document.createElement('span');
115
- fakeRunButton.className = `${codicon('play')} theia-notebook-cell-status-item`;
116
- dragImage.appendChild(fakeRunButton);
117
-
118
- const fakeEditor = document.createElement('div');
119
- dragImage.appendChild(fakeEditor);
120
- const lines = cell.source.split('\n').slice(0, 5).join('\n');
121
- const codeSequence = this.getMarkdownCodeSequence(lines);
122
- const firstLine = new MarkdownString(`${codeSequence}${cell.language}\n${lines}\n${codeSequence}`, { supportHtml: true, isTrusted: false });
123
- fakeEditor.appendChild(this.markdownRenderer.render(firstLine).element);
124
- fakeEditor.classList.add('theia-notebook-cell-editor-container');
125
- fakeEditor.style.padding = '10px';
126
- return dragImage;
127
- }
128
-
129
- protected getMarkdownCodeSequence(input: string): string {
130
- // We need a minimum of 3 backticks to start a code block.
131
- let longest = 2;
132
- let current = 0;
133
- for (let i = 0; i < input.length; i++) {
134
- const char = input.charAt(i);
135
- if (char === '`') {
136
- current++;
137
- if (current > longest) {
138
- longest = current;
139
- }
140
- } else {
141
- current = 0;
142
- }
143
- }
144
- return Array(longest + 1).fill('`').join('');
145
- }
146
-
147
- }
148
-
149
- export interface NotebookCodeCellStatusProps {
150
- notebook: NotebookModel;
151
- cell: NotebookCellModel;
152
- commandRegistry: CommandRegistry;
153
- executionStateService?: NotebookExecutionStateService;
154
- onClick: () => void;
155
- }
156
-
157
- export interface NotebookCodeCellStatusState {
158
- currentExecution?: CellExecution;
159
- executionTime: number;
160
- }
161
-
162
- export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStatusProps, NotebookCodeCellStatusState> {
163
-
164
- protected toDispose = new DisposableCollection();
165
-
166
- constructor(props: NotebookCodeCellStatusProps) {
167
- super(props);
168
-
169
- this.state = {
170
- executionTime: 0
171
- };
172
-
173
- let currentInterval: NodeJS.Timeout | undefined;
174
- if (props.executionStateService) {
175
- this.toDispose.push(props.executionStateService.onDidChangeExecution(event => {
176
- if (event.affectsCell(this.props.cell.uri)) {
177
- this.setState({ currentExecution: event.changed, executionTime: 0 });
178
- clearInterval(currentInterval);
179
- if (event.changed?.state === NotebookCellExecutionState.Executing) {
180
- const startTime = Date.now();
181
- // The resolution of the time display is only a single digit after the decimal point.
182
- // Therefore, we only need to update the display every 100ms.
183
- currentInterval = setInterval(() => {
184
- this.setState({
185
- executionTime: Date.now() - startTime
186
- });
187
- }, 100);
188
- }
189
- }
190
- }));
191
- }
192
-
193
- this.toDispose.push(props.cell.onDidChangeLanguage(() => {
194
- this.forceUpdate();
195
- }));
196
- }
197
-
198
- override componentWillUnmount(): void {
199
- this.toDispose.dispose();
200
- }
201
-
202
- override render(): React.ReactNode {
203
- return <div className='notebook-cell-status' onClick={() => this.props.onClick()}>
204
- <div className='notebook-cell-status-left'>
205
- {this.props.executionStateService && this.renderExecutionState()}
206
- </div>
207
- <div className='notebook-cell-status-right'>
208
- <span className='notebook-cell-language-label' onClick={() => {
209
- this.props.commandRegistry.executeCommand(NotebookCellCommands.CHANGE_CELL_LANGUAGE.id, this.props.notebook, this.props.cell);
210
- }}>{this.props.cell.languageName}</span>
211
- </div>
212
- </div>;
213
- }
214
-
215
- private renderExecutionState(): React.ReactNode {
216
- const state = this.state.currentExecution?.state;
217
- const { lastRunSuccess } = this.props.cell.internalMetadata;
218
-
219
- let iconClasses: string | undefined = undefined;
220
- let color: string | undefined = undefined;
221
- if (!state && lastRunSuccess) {
222
- iconClasses = codicon('check');
223
- color = 'green';
224
- } else if (!state && lastRunSuccess === false) {
225
- iconClasses = codicon('error');
226
- color = 'red';
227
- } else if (state === NotebookCellExecutionState.Pending || state === NotebookCellExecutionState.Unconfirmed) {
228
- iconClasses = codicon('clock');
229
- } else if (state === NotebookCellExecutionState.Executing) {
230
- iconClasses = `${codicon('sync')} theia-animation-spin`;
231
- }
232
- return <>
233
- {iconClasses &&
234
- <>
235
- <span className={`${iconClasses} notebook-cell-status-item`} style={{ color }}></span>
236
- <div className='notebook-cell-status-item'>{this.renderTime(this.getExecutionTime())}</div>
237
- </>}
238
- </>;
239
- }
240
-
241
- private getExecutionTime(): number {
242
- const { runStartTime, runEndTime } = this.props.cell.internalMetadata;
243
- const { executionTime } = this.state;
244
- if (runStartTime !== undefined && runEndTime !== undefined) {
245
- return runEndTime - runStartTime;
246
- }
247
- return executionTime;
248
- }
249
-
250
- private renderTime(ms: number): string {
251
- return `${(ms / 1000).toLocaleString(undefined, { maximumFractionDigits: 1, minimumFractionDigits: 1 })}s`;
252
- }
253
- }
254
-
255
- interface NotebookCellOutputProps {
256
- cell: NotebookCellModel;
257
- notebook: NotebookModel;
258
- outputWebviewFactory: CellOutputWebviewFactory;
259
- renderSidebar: () => React.ReactNode;
260
- }
261
-
262
- export class NotebookCodeCellOutputs extends React.Component<NotebookCellOutputProps> {
263
-
264
- protected outputsWebview: CellOutputWebview | undefined;
265
- protected outputsWebviewPromise: Promise<CellOutputWebview> | undefined;
266
-
267
- protected toDispose = new DisposableCollection();
268
-
269
- constructor(props: NotebookCellOutputProps) {
270
- super(props);
271
- }
272
-
273
- override async componentDidMount(): Promise<void> {
274
- const { cell, notebook, outputWebviewFactory } = this.props;
275
- this.toDispose.push(cell.onDidChangeOutputs(() => this.updateOutputs()));
276
- this.toDispose.push(cell.onDidChangeOutputVisibility(visible => {
277
- if (!visible && this.outputsWebview) {
278
- this.outputsWebview?.dispose();
279
- this.outputsWebview = undefined;
280
- this.outputsWebviewPromise = undefined;
281
- this.forceUpdate();
282
- } else {
283
- this.updateOutputs();
284
- }
285
- }));
286
- if (cell.outputs.length > 0) {
287
- this.outputsWebviewPromise = outputWebviewFactory(cell, notebook).then(webview => {
288
- this.outputsWebview = webview;
289
- this.forceUpdate();
290
- return webview;
291
- });
292
- }
293
- }
294
-
295
- protected async updateOutputs(): Promise<void> {
296
- const { cell, notebook, outputWebviewFactory } = this.props;
297
- if (!this.outputsWebviewPromise && cell.outputs.length > 0) {
298
- this.outputsWebviewPromise = outputWebviewFactory(cell, notebook).then(webview => {
299
- this.outputsWebview = webview;
300
- this.forceUpdate();
301
- return webview;
302
- });
303
- this.forceUpdate();
304
- } else if (this.outputsWebviewPromise && cell.outputs.length === 0 && cell.internalMetadata.runEndTime) {
305
- (await this.outputsWebviewPromise).dispose();
306
- this.outputsWebview = undefined;
307
- this.outputsWebviewPromise = undefined;
308
- this.forceUpdate();
309
- }
310
- }
311
-
312
- override async componentDidUpdate(): Promise<void> {
313
- if (!(await this.outputsWebviewPromise)?.isAttached()) {
314
- (await this.outputsWebviewPromise)?.attachWebview();
315
- }
316
- }
317
-
318
- override async componentWillUnmount(): Promise<void> {
319
- this.toDispose.dispose();
320
- (await this.outputsWebviewPromise)?.dispose();
321
- }
322
-
323
- override render(): React.ReactNode {
324
- return this.outputsWebview && this.props.cell.outputVisible ?
325
- <>
326
- {this.props.renderSidebar()}
327
- {this.outputsWebview.render()}
328
- </> :
329
- this.props.cell.outputs?.length ? <i className='theia-notebook-collapsed-output'>{nls.localizeByDefault('Outputs are collapsed')}</i> : <></>;
330
-
331
- }
332
-
333
- }
334
-
335
- interface NotebookCellExecutionOrderProps {
336
- cell: NotebookCellModel;
337
- }
338
-
339
- function CodeCellExecutionOrder({ cell }: NotebookCellExecutionOrderProps): React.JSX.Element {
340
- const [executionOrder, setExecutionOrder] = React.useState(cell.internalMetadata.executionOrder ?? ' ');
341
-
342
- React.useEffect(() => {
343
- const listener = cell.onDidChangeInternalMetadata(e => {
344
- setExecutionOrder(cell.internalMetadata.executionOrder ?? ' ');
345
- });
346
- return () => listener.dispose();
347
- }, []);
348
-
349
- return <span className='theia-notebook-code-cell-execution-order'>{`[${executionOrder}]`}</span>;
350
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { inject, injectable } from '@theia/core/shared/inversify';
18
+ import * as React from '@theia/core/shared/react';
19
+ import { MonacoEditorServices } from '@theia/monaco/lib/browser/monaco-editor';
20
+ import { CellOutputWebviewFactory, CellOutputWebview } from '../renderers/cell-output-webview';
21
+ import { NotebookRendererRegistry } from '../notebook-renderer-registry';
22
+ import { NotebookCellModel } from '../view-model/notebook-cell-model';
23
+ import { NotebookModel } from '../view-model/notebook-model';
24
+ import { CellEditor } from './notebook-cell-editor';
25
+ import { CellRenderer } from './notebook-cell-list-view';
26
+ import { NotebookCellToolbarFactory } from './notebook-cell-toolbar-factory';
27
+ import { NotebookCellActionContribution, NotebookCellCommands } from '../contributions/notebook-cell-actions-contribution';
28
+ import { CellExecution, NotebookExecutionStateService } from '../service/notebook-execution-state-service';
29
+ import { codicon } from '@theia/core/lib/browser';
30
+ import { NotebookCellExecutionState } from '../../common';
31
+ import { CommandRegistry, DisposableCollection, nls } from '@theia/core';
32
+ import { NotebookContextManager } from '../service/notebook-context-manager';
33
+ import { NotebookViewportService } from './notebook-viewport-service';
34
+ import { EditorPreferences } from '@theia/editor/lib/browser';
35
+ import { NotebookOptionsService } from '../service/notebook-options';
36
+ import { MarkdownRenderer } from '@theia/core/lib/browser/markdown-rendering/markdown-renderer';
37
+ import { MarkdownString } from '@theia/monaco-editor-core/esm/vs/base/common/htmlContent';
38
+ import { NotebookCellEditorService } from '../service/notebook-cell-editor-service';
39
+
40
+ @injectable()
41
+ export class NotebookCodeCellRenderer implements CellRenderer {
42
+ @inject(MonacoEditorServices)
43
+ protected readonly monacoServices: MonacoEditorServices;
44
+
45
+ @inject(NotebookRendererRegistry)
46
+ protected readonly notebookRendererRegistry: NotebookRendererRegistry;
47
+
48
+ @inject(CellOutputWebviewFactory)
49
+ protected readonly cellOutputWebviewFactory: CellOutputWebviewFactory;
50
+
51
+ @inject(NotebookCellToolbarFactory)
52
+ protected readonly notebookCellToolbarFactory: NotebookCellToolbarFactory;
53
+
54
+ @inject(NotebookExecutionStateService)
55
+ protected readonly executionStateService: NotebookExecutionStateService;
56
+
57
+ @inject(NotebookContextManager)
58
+ protected readonly notebookContextManager: NotebookContextManager;
59
+
60
+ @inject(NotebookViewportService)
61
+ protected readonly notebookViewportService: NotebookViewportService;
62
+
63
+ @inject(EditorPreferences)
64
+ protected readonly editorPreferences: EditorPreferences;
65
+
66
+ @inject(NotebookCellEditorService)
67
+ protected readonly notebookCellEditorService: NotebookCellEditorService;
68
+
69
+ @inject(CommandRegistry)
70
+ protected readonly commandRegistry: CommandRegistry;
71
+
72
+ @inject(NotebookOptionsService)
73
+ protected readonly notebookOptionsService: NotebookOptionsService;
74
+
75
+ @inject(MarkdownRenderer)
76
+ protected readonly markdownRenderer: MarkdownRenderer;
77
+
78
+ render(notebookModel: NotebookModel, cell: NotebookCellModel, handle: number): React.ReactNode {
79
+ return <div>
80
+ <div className='theia-notebook-cell-with-sidebar'>
81
+ <div className='theia-notebook-cell-sidebar'>
82
+ {this.notebookCellToolbarFactory.renderSidebar(NotebookCellActionContribution.CODE_CELL_SIDEBAR_MENU, cell, {
83
+ contextMenuArgs: () => [cell], commandArgs: () => [notebookModel, cell]
84
+ })
85
+ }
86
+ <CodeCellExecutionOrder cell={cell} />
87
+ </div>
88
+ <div className='theia-notebook-cell-editor-container'>
89
+ <CellEditor notebookModel={notebookModel} cell={cell}
90
+ monacoServices={this.monacoServices}
91
+ notebookContextManager={this.notebookContextManager}
92
+ notebookViewportService={this.notebookViewportService}
93
+ notebookCellEditorService={this.notebookCellEditorService}
94
+ fontInfo={this.notebookOptionsService.editorFontInfo} />
95
+ <NotebookCodeCellStatus cell={cell} notebook={notebookModel}
96
+ commandRegistry={this.commandRegistry}
97
+ executionStateService={this.executionStateService}
98
+ onClick={() => cell.requestFocusEditor()} />
99
+ </div >
100
+ </div >
101
+ <div className='theia-notebook-cell-with-sidebar'>
102
+ <NotebookCodeCellOutputs cell={cell} notebook={notebookModel} outputWebviewFactory={this.cellOutputWebviewFactory}
103
+ renderSidebar={() =>
104
+ this.notebookCellToolbarFactory.renderSidebar(NotebookCellActionContribution.OUTPUT_SIDEBAR_MENU, cell, {
105
+ contextMenuArgs: () => [notebookModel, cell, cell.outputs[0]]
106
+ })
107
+ } />
108
+ </div>
109
+ </div >;
110
+ }
111
+
112
+ renderDragImage(cell: NotebookCellModel): HTMLElement {
113
+ const dragImage = document.createElement('div');
114
+ dragImage.className = 'theia-notebook-drag-image';
115
+ dragImage.style.width = this.notebookContextManager.context?.clientWidth + 'px';
116
+ dragImage.style.height = '100px';
117
+ dragImage.style.display = 'flex';
118
+
119
+ const fakeRunButton = document.createElement('span');
120
+ fakeRunButton.className = `${codicon('play')} theia-notebook-cell-status-item`;
121
+ dragImage.appendChild(fakeRunButton);
122
+
123
+ const fakeEditor = document.createElement('div');
124
+ dragImage.appendChild(fakeEditor);
125
+ const lines = cell.source.split('\n').slice(0, 5).join('\n');
126
+ const codeSequence = this.getMarkdownCodeSequence(lines);
127
+ const firstLine = new MarkdownString(`${codeSequence}${cell.language}\n${lines}\n${codeSequence}`, { supportHtml: true, isTrusted: false });
128
+ fakeEditor.appendChild(this.markdownRenderer.render(firstLine).element);
129
+ fakeEditor.classList.add('theia-notebook-cell-editor-container');
130
+ fakeEditor.style.padding = '10px';
131
+ return dragImage;
132
+ }
133
+
134
+ protected getMarkdownCodeSequence(input: string): string {
135
+ // We need a minimum of 3 backticks to start a code block.
136
+ let longest = 2;
137
+ let current = 0;
138
+ for (let i = 0; i < input.length; i++) {
139
+ const char = input.charAt(i);
140
+ if (char === '`') {
141
+ current++;
142
+ if (current > longest) {
143
+ longest = current;
144
+ }
145
+ } else {
146
+ current = 0;
147
+ }
148
+ }
149
+ return Array(longest + 1).fill('`').join('');
150
+ }
151
+
152
+ }
153
+
154
+ export interface NotebookCodeCellStatusProps {
155
+ notebook: NotebookModel;
156
+ cell: NotebookCellModel;
157
+ commandRegistry: CommandRegistry;
158
+ executionStateService?: NotebookExecutionStateService;
159
+ onClick: () => void;
160
+ }
161
+
162
+ export interface NotebookCodeCellStatusState {
163
+ currentExecution?: CellExecution;
164
+ executionTime: number;
165
+ }
166
+
167
+ export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStatusProps, NotebookCodeCellStatusState> {
168
+
169
+ protected toDispose = new DisposableCollection();
170
+
171
+ constructor(props: NotebookCodeCellStatusProps) {
172
+ super(props);
173
+
174
+ this.state = {
175
+ executionTime: 0
176
+ };
177
+
178
+ let currentInterval: NodeJS.Timeout | undefined;
179
+ if (props.executionStateService) {
180
+ this.toDispose.push(props.executionStateService.onDidChangeExecution(event => {
181
+ if (event.affectsCell(this.props.cell.uri)) {
182
+ this.setState({ currentExecution: event.changed, executionTime: 0 });
183
+ clearInterval(currentInterval);
184
+ if (event.changed?.state === NotebookCellExecutionState.Executing) {
185
+ const startTime = Date.now();
186
+ // The resolution of the time display is only a single digit after the decimal point.
187
+ // Therefore, we only need to update the display every 100ms.
188
+ currentInterval = setInterval(() => {
189
+ this.setState({
190
+ executionTime: Date.now() - startTime
191
+ });
192
+ }, 100);
193
+ }
194
+ }
195
+ }));
196
+ }
197
+
198
+ this.toDispose.push(props.cell.onDidChangeLanguage(() => {
199
+ this.forceUpdate();
200
+ }));
201
+ }
202
+
203
+ override componentWillUnmount(): void {
204
+ this.toDispose.dispose();
205
+ }
206
+
207
+ override render(): React.ReactNode {
208
+ return <div className='notebook-cell-status' onClick={() => this.props.onClick()}>
209
+ <div className='notebook-cell-status-left'>
210
+ {this.props.executionStateService && this.renderExecutionState()}
211
+ </div>
212
+ <div className='notebook-cell-status-right'>
213
+ <span className='notebook-cell-language-label' onClick={() => {
214
+ this.props.commandRegistry.executeCommand(NotebookCellCommands.CHANGE_CELL_LANGUAGE.id, this.props.notebook, this.props.cell);
215
+ }}>{this.props.cell.languageName}</span>
216
+ </div>
217
+ </div>;
218
+ }
219
+
220
+ private renderExecutionState(): React.ReactNode {
221
+ const state = this.state.currentExecution?.state;
222
+ const { lastRunSuccess } = this.props.cell.internalMetadata;
223
+
224
+ let iconClasses: string | undefined = undefined;
225
+ let color: string | undefined = undefined;
226
+ if (!state && lastRunSuccess) {
227
+ iconClasses = codicon('check');
228
+ color = 'green';
229
+ } else if (!state && lastRunSuccess === false) {
230
+ iconClasses = codicon('error');
231
+ color = 'red';
232
+ } else if (state === NotebookCellExecutionState.Pending || state === NotebookCellExecutionState.Unconfirmed) {
233
+ iconClasses = codicon('clock');
234
+ } else if (state === NotebookCellExecutionState.Executing) {
235
+ iconClasses = `${codicon('sync')} theia-animation-spin`;
236
+ }
237
+ return <>
238
+ {iconClasses &&
239
+ <>
240
+ <span className={`${iconClasses} notebook-cell-status-item`} style={{ color }}></span>
241
+ <div className='notebook-cell-status-item'>{this.renderTime(this.getExecutionTime())}</div>
242
+ </>}
243
+ </>;
244
+ }
245
+
246
+ private getExecutionTime(): number {
247
+ const { runStartTime, runEndTime } = this.props.cell.internalMetadata;
248
+ const { executionTime } = this.state;
249
+ if (runStartTime !== undefined && runEndTime !== undefined) {
250
+ return runEndTime - runStartTime;
251
+ }
252
+ return executionTime;
253
+ }
254
+
255
+ private renderTime(ms: number): string {
256
+ return `${(ms / 1000).toLocaleString(undefined, { maximumFractionDigits: 1, minimumFractionDigits: 1 })}s`;
257
+ }
258
+ }
259
+
260
+ interface NotebookCellOutputProps {
261
+ cell: NotebookCellModel;
262
+ notebook: NotebookModel;
263
+ outputWebviewFactory: CellOutputWebviewFactory;
264
+ renderSidebar: () => React.ReactNode;
265
+ }
266
+
267
+ export class NotebookCodeCellOutputs extends React.Component<NotebookCellOutputProps> {
268
+
269
+ protected outputsWebview: CellOutputWebview | undefined;
270
+ protected outputsWebviewPromise: Promise<CellOutputWebview> | undefined;
271
+
272
+ protected toDispose = new DisposableCollection();
273
+
274
+ constructor(props: NotebookCellOutputProps) {
275
+ super(props);
276
+ }
277
+
278
+ override async componentDidMount(): Promise<void> {
279
+ const { cell, notebook, outputWebviewFactory } = this.props;
280
+ this.toDispose.push(cell.onDidChangeOutputs(() => this.updateOutputs()));
281
+ this.toDispose.push(cell.onDidChangeOutputVisibility(visible => {
282
+ if (!visible && this.outputsWebview) {
283
+ this.outputsWebview?.dispose();
284
+ this.outputsWebview = undefined;
285
+ this.outputsWebviewPromise = undefined;
286
+ this.forceUpdate();
287
+ } else {
288
+ this.updateOutputs();
289
+ }
290
+ }));
291
+ if (cell.outputs.length > 0) {
292
+ this.outputsWebviewPromise = outputWebviewFactory(cell, notebook).then(webview => {
293
+ this.outputsWebview = webview;
294
+ this.forceUpdate();
295
+ return webview;
296
+ });
297
+ }
298
+ }
299
+
300
+ protected async updateOutputs(): Promise<void> {
301
+ const { cell, notebook, outputWebviewFactory } = this.props;
302
+ if (!this.outputsWebviewPromise && cell.outputs.length > 0) {
303
+ this.outputsWebviewPromise = outputWebviewFactory(cell, notebook).then(webview => {
304
+ this.outputsWebview = webview;
305
+ this.forceUpdate();
306
+ return webview;
307
+ });
308
+ this.forceUpdate();
309
+ } else if (this.outputsWebviewPromise && cell.outputs.length === 0 && cell.internalMetadata.runEndTime) {
310
+ (await this.outputsWebviewPromise).dispose();
311
+ this.outputsWebview = undefined;
312
+ this.outputsWebviewPromise = undefined;
313
+ this.forceUpdate();
314
+ }
315
+ }
316
+
317
+ override async componentDidUpdate(): Promise<void> {
318
+ if (!(await this.outputsWebviewPromise)?.isAttached()) {
319
+ (await this.outputsWebviewPromise)?.attachWebview();
320
+ }
321
+ }
322
+
323
+ override async componentWillUnmount(): Promise<void> {
324
+ this.toDispose.dispose();
325
+ (await this.outputsWebviewPromise)?.dispose();
326
+ }
327
+
328
+ override render(): React.ReactNode {
329
+ return this.outputsWebview && this.props.cell.outputVisible ?
330
+ <>
331
+ {this.props.renderSidebar()}
332
+ {this.outputsWebview.render()}
333
+ </> :
334
+ this.props.cell.outputs?.length ? <i className='theia-notebook-collapsed-output'>{nls.localizeByDefault('Outputs are collapsed')}</i> : <></>;
335
+
336
+ }
337
+
338
+ }
339
+
340
+ interface NotebookCellExecutionOrderProps {
341
+ cell: NotebookCellModel;
342
+ }
343
+
344
+ function CodeCellExecutionOrder({ cell }: NotebookCellExecutionOrderProps): React.JSX.Element {
345
+ const [executionOrder, setExecutionOrder] = React.useState(cell.internalMetadata.executionOrder ?? ' ');
346
+
347
+ React.useEffect(() => {
348
+ const listener = cell.onDidChangeInternalMetadata(e => {
349
+ setExecutionOrder(cell.internalMetadata.executionOrder ?? ' ');
350
+ });
351
+ return () => listener.dispose();
352
+ }, []);
353
+
354
+ return <span className='theia-notebook-code-cell-execution-order'>{`[${executionOrder}]`}</span>;
355
+ }