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

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 (99) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/contributions/cell-operations.d.ts.map +1 -1
  3. package/lib/browser/contributions/cell-operations.js +8 -1
  4. package/lib/browser/contributions/cell-operations.js.map +1 -1
  5. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  6. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  7. package/lib/browser/contributions/notebook-actions-contribution.js +31 -6
  8. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +4 -0
  10. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  11. package/lib/browser/contributions/notebook-cell-actions-contribution.js +49 -12
  12. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  13. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
  14. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
  15. package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
  16. package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
  17. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  18. package/lib/browser/notebook-editor-widget.js +3 -5
  19. package/lib/browser/notebook-editor-widget.js.map +1 -1
  20. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  21. package/lib/browser/notebook-frontend-module.js +3 -0
  22. package/lib/browser/notebook-frontend-module.js.map +1 -1
  23. package/lib/browser/notebook-open-handler.d.ts +3 -2
  24. package/lib/browser/notebook-open-handler.d.ts.map +1 -1
  25. package/lib/browser/notebook-open-handler.js +12 -5
  26. package/lib/browser/notebook-open-handler.js.map +1 -1
  27. package/lib/browser/service/notebook-options.d.ts +1 -0
  28. package/lib/browser/service/notebook-options.d.ts.map +1 -1
  29. package/lib/browser/service/notebook-options.js +1 -0
  30. package/lib/browser/service/notebook-options.js.map +1 -1
  31. package/lib/browser/service/notebook-service.d.ts +1 -0
  32. package/lib/browser/service/notebook-service.d.ts.map +1 -1
  33. package/lib/browser/service/notebook-service.js +7 -0
  34. package/lib/browser/service/notebook-service.js.map +1 -1
  35. package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
  36. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  37. package/lib/browser/view/notebook-cell-editor.js +30 -16
  38. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  39. package/lib/browser/view/notebook-cell-list-view.d.ts +7 -4
  40. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  41. package/lib/browser/view/notebook-cell-list-view.js +39 -29
  42. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  43. package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
  44. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  45. package/lib/browser/view-model/notebook-cell-model.js +5 -0
  46. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  47. package/package.json +8 -8
  48. package/src/browser/contributions/cell-operations.ts +44 -39
  49. package/src/browser/contributions/notebook-actions-contribution.ts +379 -351
  50. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -485
  51. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  52. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  53. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  54. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  55. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  56. package/src/browser/contributions/notebook-preferences.ts +92 -92
  57. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
  58. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  59. package/src/browser/index.ts +27 -27
  60. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  61. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  62. package/src/browser/notebook-editor-widget.tsx +330 -331
  63. package/src/browser/notebook-frontend-module.ts +119 -115
  64. package/src/browser/notebook-open-handler.ts +120 -114
  65. package/src/browser/notebook-output-utils.ts +119 -119
  66. package/src/browser/notebook-renderer-registry.ts +85 -85
  67. package/src/browser/notebook-type-registry.ts +54 -54
  68. package/src/browser/notebook-types.ts +186 -186
  69. package/src/browser/renderers/cell-output-webview.ts +33 -33
  70. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  71. package/src/browser/service/notebook-context-manager.ts +162 -162
  72. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  73. package/src/browser/service/notebook-execution-service.ts +139 -139
  74. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  75. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  76. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  77. package/src/browser/service/notebook-kernel-service.ts +357 -357
  78. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  79. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  80. package/src/browser/service/notebook-options.ts +155 -154
  81. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  82. package/src/browser/service/notebook-service.ts +215 -209
  83. package/src/browser/style/index.css +483 -467
  84. package/src/browser/view/notebook-cell-editor.tsx +263 -247
  85. package/src/browser/view/notebook-cell-list-view.tsx +279 -259
  86. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  87. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  88. package/src/browser/view/notebook-code-cell-view.tsx +350 -350
  89. package/src/browser/view/notebook-find-widget.tsx +335 -335
  90. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  91. package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
  92. package/src/browser/view/notebook-viewport-service.ts +61 -61
  93. package/src/browser/view-model/notebook-cell-model.ts +473 -466
  94. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  95. package/src/browser/view-model/notebook-model.ts +550 -550
  96. package/src/common/index.ts +18 -18
  97. package/src/common/notebook-common.ts +337 -337
  98. package/src/common/notebook-protocol.ts +35 -35
  99. package/src/common/notebook-range.ts +30 -30
@@ -1,350 +1,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
-
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
+
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
+ }