@theia/notebook 1.53.0-next.5 → 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 (88) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  3. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  4. package/lib/browser/contributions/notebook-actions-contribution.js +31 -5
  5. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  6. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +2 -0
  7. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  8. package/lib/browser/contributions/notebook-cell-actions-contribution.js +41 -7
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  10. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
  11. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
  12. package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
  13. package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
  14. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  15. package/lib/browser/notebook-editor-widget.js +3 -5
  16. package/lib/browser/notebook-editor-widget.js.map +1 -1
  17. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  18. package/lib/browser/notebook-frontend-module.js +3 -0
  19. package/lib/browser/notebook-frontend-module.js.map +1 -1
  20. package/lib/browser/notebook-open-handler.d.ts +3 -2
  21. package/lib/browser/notebook-open-handler.d.ts.map +1 -1
  22. package/lib/browser/notebook-open-handler.js +12 -5
  23. package/lib/browser/notebook-open-handler.js.map +1 -1
  24. package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
  25. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  26. package/lib/browser/view/notebook-cell-editor.js +30 -16
  27. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  28. package/lib/browser/view/notebook-cell-list-view.d.ts +6 -4
  29. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  30. package/lib/browser/view/notebook-cell-list-view.js +20 -13
  31. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  32. package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
  33. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  34. package/lib/browser/view-model/notebook-cell-model.js +5 -0
  35. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  36. package/package.json +8 -8
  37. package/src/browser/contributions/cell-operations.ts +44 -44
  38. package/src/browser/contributions/notebook-actions-contribution.ts +379 -350
  39. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -487
  40. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  41. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  42. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  43. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  44. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  45. package/src/browser/contributions/notebook-preferences.ts +92 -92
  46. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
  47. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  48. package/src/browser/index.ts +27 -27
  49. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  50. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  51. package/src/browser/notebook-editor-widget.tsx +330 -331
  52. package/src/browser/notebook-frontend-module.ts +119 -115
  53. package/src/browser/notebook-open-handler.ts +120 -114
  54. package/src/browser/notebook-output-utils.ts +119 -119
  55. package/src/browser/notebook-renderer-registry.ts +85 -85
  56. package/src/browser/notebook-type-registry.ts +54 -54
  57. package/src/browser/notebook-types.ts +186 -186
  58. package/src/browser/renderers/cell-output-webview.ts +33 -33
  59. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  60. package/src/browser/service/notebook-context-manager.ts +162 -162
  61. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  62. package/src/browser/service/notebook-execution-service.ts +139 -139
  63. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  64. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  65. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  66. package/src/browser/service/notebook-kernel-service.ts +357 -357
  67. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  68. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  69. package/src/browser/service/notebook-options.ts +155 -155
  70. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  71. package/src/browser/service/notebook-service.ts +215 -215
  72. package/src/browser/style/index.css +483 -471
  73. package/src/browser/view/notebook-cell-editor.tsx +263 -247
  74. package/src/browser/view/notebook-cell-list-view.tsx +279 -262
  75. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  76. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  77. package/src/browser/view/notebook-code-cell-view.tsx +350 -350
  78. package/src/browser/view/notebook-find-widget.tsx +335 -335
  79. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  80. package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
  81. package/src/browser/view/notebook-viewport-service.ts +61 -61
  82. package/src/browser/view-model/notebook-cell-model.ts +473 -466
  83. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  84. package/src/browser/view-model/notebook-model.ts +550 -550
  85. package/src/common/index.ts +18 -18
  86. package/src/common/notebook-common.ts +337 -337
  87. package/src/common/notebook-protocol.ts +35 -35
  88. package/src/common/notebook-range.ts +30 -30
@@ -1,331 +1,330 @@
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 * as React from '@theia/core/shared/react';
18
- import { CommandRegistry, MenuModelRegistry, URI } from '@theia/core';
19
- import { ReactWidget, Navigatable, SaveableSource, Message, DelegatingSaveable, lock, unlock, animationFrame } from '@theia/core/lib/browser';
20
- import { ReactNode } from '@theia/core/shared/react';
21
- import { CellKind } from '../common';
22
- import { CellRenderer as CellRenderer, NotebookCellListView } from './view/notebook-cell-list-view';
23
- import { NotebookCodeCellRenderer } from './view/notebook-code-cell-view';
24
- import { NotebookMarkdownCellRenderer } from './view/notebook-markdown-cell-view';
25
- import { NotebookModel } from './view-model/notebook-model';
26
- import { NotebookCellToolbarFactory } from './view/notebook-cell-toolbar-factory';
27
- import { inject, injectable, interfaces, postConstruct } from '@theia/core/shared/inversify';
28
- import { Emitter } from '@theia/core/shared/vscode-languageserver-protocol';
29
- import { NotebookEditorWidgetService } from './service/notebook-editor-widget-service';
30
- import { NotebookMainToolbarRenderer } from './view/notebook-main-toolbar';
31
- import { Deferred } from '@theia/core/lib/common/promise-util';
32
- import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
33
- import { NotebookContextManager } from './service/notebook-context-manager';
34
- import { NotebookViewportService } from './view/notebook-viewport-service';
35
- import { NotebookCellCommands } from './contributions/notebook-cell-actions-contribution';
36
- import { NotebookFindWidget } from './view/notebook-find-widget';
37
- import debounce = require('lodash/debounce');
38
- const PerfectScrollbar = require('react-perfect-scrollbar');
39
-
40
- export const NotebookEditorWidgetContainerFactory = Symbol('NotebookEditorWidgetContainerFactory');
41
-
42
- export function createNotebookEditorWidgetContainer(parent: interfaces.Container, props: NotebookEditorProps): interfaces.Container {
43
- const child = parent.createChild();
44
-
45
- child.bind(NotebookEditorProps).toConstantValue(props);
46
-
47
- child.bind(NotebookContextManager).toSelf().inSingletonScope();
48
- child.bind(NotebookMainToolbarRenderer).toSelf().inSingletonScope();
49
- child.bind(NotebookCellToolbarFactory).toSelf().inSingletonScope();
50
- child.bind(NotebookCodeCellRenderer).toSelf().inSingletonScope();
51
- child.bind(NotebookMarkdownCellRenderer).toSelf().inSingletonScope();
52
- child.bind(NotebookViewportService).toSelf().inSingletonScope();
53
-
54
- child.bind(NotebookEditorWidget).toSelf();
55
-
56
- return child;
57
- }
58
-
59
- export const NotebookEditorProps = Symbol('NotebookEditorProps');
60
-
61
- interface RenderMessage {
62
- rendererId: string;
63
- message: unknown;
64
- }
65
-
66
- export interface NotebookEditorProps {
67
- uri: URI,
68
- readonly notebookType: string,
69
- notebookData: Promise<NotebookModel>
70
- }
71
- export const NOTEBOOK_EDITOR_ID_PREFIX = 'notebook:';
72
-
73
- @injectable()
74
- export class NotebookEditorWidget extends ReactWidget implements Navigatable, SaveableSource {
75
- static readonly ID = 'notebook';
76
-
77
- readonly saveable = new DelegatingSaveable();
78
-
79
- @inject(NotebookCellToolbarFactory)
80
- protected readonly cellToolbarFactory: NotebookCellToolbarFactory;
81
-
82
- @inject(CommandRegistry)
83
- protected commandRegistry: CommandRegistry;
84
-
85
- @inject(MenuModelRegistry)
86
- protected menuRegistry: MenuModelRegistry;
87
-
88
- @inject(NotebookEditorWidgetService)
89
- protected notebookEditorService: NotebookEditorWidgetService;
90
-
91
- @inject(NotebookMainToolbarRenderer)
92
- protected notebookMainToolbarRenderer: NotebookMainToolbarRenderer;
93
-
94
- @inject(NotebookContextManager)
95
- protected notebookContextManager: NotebookContextManager;
96
-
97
- @inject(NotebookCodeCellRenderer)
98
- protected codeCellRenderer: NotebookCodeCellRenderer;
99
- @inject(NotebookMarkdownCellRenderer)
100
- protected markdownCellRenderer: NotebookMarkdownCellRenderer;
101
- @inject(NotebookEditorProps)
102
- protected readonly props: NotebookEditorProps;
103
-
104
- @inject(NotebookViewportService)
105
- protected readonly viewportService: NotebookViewportService;
106
-
107
- protected readonly onDidChangeModelEmitter = new Emitter<void>();
108
- readonly onDidChangeModel = this.onDidChangeModelEmitter.event;
109
-
110
- protected readonly onDidChangeReadOnlyEmitter = new Emitter<boolean | MarkdownString>();
111
- readonly onDidChangeReadOnly = this.onDidChangeReadOnlyEmitter.event;
112
-
113
- protected readonly onPostKernelMessageEmitter = new Emitter<unknown>();
114
- readonly onPostKernelMessage = this.onPostKernelMessageEmitter.event;
115
-
116
- protected readonly onDidPostKernelMessageEmitter = new Emitter<unknown>();
117
- readonly onDidPostKernelMessage = this.onDidPostKernelMessageEmitter.event;
118
-
119
- protected readonly onPostRendererMessageEmitter = new Emitter<RenderMessage>();
120
- readonly onPostRendererMessage = this.onPostRendererMessageEmitter.event;
121
-
122
- protected readonly onDidReceiveKernelMessageEmitter = new Emitter<unknown>();
123
- readonly onDidReceiveKernelMessage = this.onDidReceiveKernelMessageEmitter.event;
124
-
125
- protected readonly onDidChangeOutputInputFocusEmitter = new Emitter<boolean>();
126
- readonly onDidChangeOutputInputFocus = this.onDidChangeOutputInputFocusEmitter.event;
127
-
128
- protected readonly renderers = new Map<CellKind, CellRenderer>();
129
- protected _model?: NotebookModel;
130
- protected _ready: Deferred<NotebookModel> = new Deferred();
131
- protected _findWidgetVisible = false;
132
- protected _findWidgetRef = React.createRef<NotebookFindWidget>();
133
- protected scrollBarRef = React.createRef<{ updateScroll(): void }>();
134
- protected debounceFind = debounce(() => {
135
- this._findWidgetRef.current?.search({});
136
- }, 30, {
137
- trailing: true,
138
- maxWait: 100,
139
- leading: false
140
- });
141
-
142
- get notebookType(): string {
143
- return this.props.notebookType;
144
- }
145
-
146
- get ready(): Promise<NotebookModel> {
147
- return this._ready.promise;
148
- }
149
-
150
- get model(): NotebookModel | undefined {
151
- return this._model;
152
- }
153
-
154
- @postConstruct()
155
- protected init(): void {
156
- this.id = NOTEBOOK_EDITOR_ID_PREFIX + this.props.uri.toString();
157
- this.node.tabIndex = -1;
158
-
159
- this.scrollOptions = {
160
- suppressScrollY: true
161
- };
162
-
163
- this.title.closable = true;
164
- this.update();
165
-
166
- this.toDispose.push(this.onDidChangeModelEmitter);
167
- this.toDispose.push(this.onDidChangeReadOnlyEmitter);
168
-
169
- this.renderers.set(CellKind.Markup, this.markdownCellRenderer);
170
- this.renderers.set(CellKind.Code, this.codeCellRenderer);
171
- this._ready.resolve(this.waitForData());
172
- this.ready.then(model => {
173
- if (model.cells.length === 1 && model.cells[0].source === '') {
174
- this.commandRegistry.executeCommand(NotebookCellCommands.EDIT_COMMAND.id, model, model.cells[0]);
175
- model.setSelectedCell(model.cells[0]);
176
- }
177
- model.cells.forEach(cell => cell.onWillBlurCellEditor(() => this.node.focus()));
178
- model.onDidAddOrRemoveCell(e => e.newCellIds?.forEach(cellId => model.cells.find(cell => cell.handle === cellId)?.onWillBlurCellEditor(() => this.node.focus())));
179
- });
180
- }
181
-
182
- protected async waitForData(): Promise<NotebookModel> {
183
- this._model = await this.props.notebookData;
184
- this.saveable.delegate = this._model;
185
- this.toDispose.push(this._model);
186
- this.toDispose.push(this._model.onDidChangeContent(() => {
187
- // Update the scroll bar content after the content has changed
188
- // Wait one frame to ensure that the content has been rendered
189
- animationFrame().then(() => this.scrollBarRef.current?.updateScroll());
190
- }));
191
- this.toDispose.push(this._model.onContentChanged(() => {
192
- if (this._findWidgetVisible) {
193
- this.debounceFind();
194
- }
195
- }));
196
- this.toDispose.push(this._model.onDidChangeReadOnly(readOnly => {
197
- if (readOnly) {
198
- lock(this.title);
199
- } else {
200
- unlock(this.title);
201
- }
202
- this.onDidChangeReadOnlyEmitter.fire(readOnly);
203
- this.update();
204
- }));
205
- if (this._model.readOnly) {
206
- lock(this.title);
207
- }
208
- // Ensure that the model is loaded before adding the editor
209
- this.notebookEditorService.addNotebookEditor(this);
210
- this.update();
211
- this.notebookContextManager.init(this);
212
- return this._model;
213
- }
214
-
215
- protected override onActivateRequest(msg: Message): void {
216
- super.onActivateRequest(msg);
217
- this.node.focus();
218
- }
219
-
220
- getResourceUri(): URI | undefined {
221
- return this.props.uri;
222
- }
223
-
224
- createMoveToUri(resourceUri: URI): URI | undefined {
225
- return this.model?.uri.withPath(resourceUri.path);
226
- }
227
-
228
- undo(): void {
229
- this._model?.undo();
230
- }
231
-
232
- redo(): void {
233
- this._model?.redo();
234
- }
235
-
236
- protected render(): ReactNode {
237
- if (this._model) {
238
- return <div className='theia-notebook-main-container'>
239
- <div className='theia-notebook-overlay'>
240
- <NotebookFindWidget
241
- ref={this._findWidgetRef}
242
- hidden={!this._findWidgetVisible}
243
- onClose={() => {
244
- this._findWidgetVisible = false;
245
- this._model?.findMatches({
246
- activeFilters: [],
247
- matchCase: false,
248
- regex: false,
249
- search: '',
250
- wholeWord: false
251
- });
252
- this.update();
253
- }}
254
- onSearch={options => this._model?.findMatches(options) ?? []}
255
- onReplace={(matches, replaceText) => this._model?.replaceAll(matches, replaceText)}
256
- />
257
- </div>
258
- {this.notebookMainToolbarRenderer.render(this._model, this.node)}
259
- <div
260
- className='theia-notebook-viewport'
261
- ref={(ref: HTMLDivElement) => this.viewportService.viewportElement = ref}
262
- >
263
- <PerfectScrollbar className='theia-notebook-scroll-container'
264
- ref={this.scrollBarRef}
265
- onScrollY={(e: HTMLDivElement) => this.viewportService.onScroll(e)}>
266
- <NotebookCellListView renderers={this.renderers}
267
- notebookModel={this._model}
268
- notebookContext={this.notebookContextManager}
269
- toolbarRenderer={this.cellToolbarFactory}
270
- commandRegistry={this.commandRegistry} />
271
- </PerfectScrollbar>
272
- </div>
273
- </div>;
274
- } else {
275
- return <div className='theia-notebook-main-container'>
276
- <div className='theia-notebook-main-loading-indicator'></div>
277
- </div>;
278
- }
279
- }
280
-
281
- protected override onCloseRequest(msg: Message): void {
282
- super.onCloseRequest(msg);
283
- this.notebookEditorService.removeNotebookEditor(this);
284
- }
285
-
286
- postKernelMessage(message: unknown): void {
287
- this.onDidPostKernelMessageEmitter.fire(message);
288
- }
289
-
290
- postRendererMessage(rendererId: string, message: unknown): void {
291
- this.onPostRendererMessageEmitter.fire({ rendererId, message });
292
- }
293
-
294
- recieveKernelMessage(message: unknown): void {
295
- this.onDidReceiveKernelMessageEmitter.fire(message);
296
- }
297
-
298
- outputInputFocusChanged(focused: boolean): void {
299
- this.onDidChangeOutputInputFocusEmitter.fire(focused);
300
- }
301
-
302
- showFindWidget(): void {
303
- if (!this._findWidgetVisible) {
304
- this._findWidgetVisible = true;
305
- this.update();
306
- }
307
- this._findWidgetRef.current?.focusSearch(this._model?.selectedText);
308
- }
309
-
310
- override dispose(): void {
311
- this.notebookContextManager.dispose();
312
- this.onDidChangeModelEmitter.dispose();
313
- this.onDidPostKernelMessageEmitter.dispose();
314
- this.onDidReceiveKernelMessageEmitter.dispose();
315
- this.onPostRendererMessageEmitter.dispose();
316
- this.onDidChangeOutputInputFocusEmitter.dispose();
317
- this.viewportService.dispose();
318
- this._model?.dispose();
319
- super.dispose();
320
- }
321
-
322
- protected override onAfterShow(msg: Message): void {
323
- super.onAfterShow(msg);
324
- this.notebookEditorService.notebookEditorFocusChanged(this, true);
325
- }
326
-
327
- protected override onAfterHide(msg: Message): void {
328
- super.onAfterHide(msg);
329
- this.notebookEditorService.notebookEditorFocusChanged(this, false);
330
- }
331
- }
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 * as React from '@theia/core/shared/react';
18
+ import { CommandRegistry, MenuModelRegistry, URI } from '@theia/core';
19
+ import { ReactWidget, Navigatable, SaveableSource, Message, DelegatingSaveable, lock, unlock, animationFrame } from '@theia/core/lib/browser';
20
+ import { ReactNode } from '@theia/core/shared/react';
21
+ import { CellKind } from '../common';
22
+ import { CellRenderer as CellRenderer, NotebookCellListView } from './view/notebook-cell-list-view';
23
+ import { NotebookCodeCellRenderer } from './view/notebook-code-cell-view';
24
+ import { NotebookMarkdownCellRenderer } from './view/notebook-markdown-cell-view';
25
+ import { NotebookModel } from './view-model/notebook-model';
26
+ import { NotebookCellToolbarFactory } from './view/notebook-cell-toolbar-factory';
27
+ import { inject, injectable, interfaces, postConstruct } from '@theia/core/shared/inversify';
28
+ import { Emitter } from '@theia/core/shared/vscode-languageserver-protocol';
29
+ import { NotebookEditorWidgetService } from './service/notebook-editor-widget-service';
30
+ import { NotebookMainToolbarRenderer } from './view/notebook-main-toolbar';
31
+ import { Deferred } from '@theia/core/lib/common/promise-util';
32
+ import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
33
+ import { NotebookContextManager } from './service/notebook-context-manager';
34
+ import { NotebookViewportService } from './view/notebook-viewport-service';
35
+ import { NotebookCellCommands } from './contributions/notebook-cell-actions-contribution';
36
+ import { NotebookFindWidget } from './view/notebook-find-widget';
37
+ import debounce = require('lodash/debounce');
38
+ const PerfectScrollbar = require('react-perfect-scrollbar');
39
+
40
+ export const NotebookEditorWidgetContainerFactory = Symbol('NotebookEditorWidgetContainerFactory');
41
+
42
+ export function createNotebookEditorWidgetContainer(parent: interfaces.Container, props: NotebookEditorProps): interfaces.Container {
43
+ const child = parent.createChild();
44
+
45
+ child.bind(NotebookEditorProps).toConstantValue(props);
46
+
47
+ child.bind(NotebookContextManager).toSelf().inSingletonScope();
48
+ child.bind(NotebookMainToolbarRenderer).toSelf().inSingletonScope();
49
+ child.bind(NotebookCellToolbarFactory).toSelf().inSingletonScope();
50
+ child.bind(NotebookCodeCellRenderer).toSelf().inSingletonScope();
51
+ child.bind(NotebookMarkdownCellRenderer).toSelf().inSingletonScope();
52
+ child.bind(NotebookViewportService).toSelf().inSingletonScope();
53
+
54
+ child.bind(NotebookEditorWidget).toSelf();
55
+
56
+ return child;
57
+ }
58
+
59
+ export const NotebookEditorProps = Symbol('NotebookEditorProps');
60
+
61
+ interface RenderMessage {
62
+ rendererId: string;
63
+ message: unknown;
64
+ }
65
+
66
+ export interface NotebookEditorProps {
67
+ uri: URI,
68
+ readonly notebookType: string,
69
+ notebookData: Promise<NotebookModel>
70
+ }
71
+ export const NOTEBOOK_EDITOR_ID_PREFIX = 'notebook:';
72
+
73
+ @injectable()
74
+ export class NotebookEditorWidget extends ReactWidget implements Navigatable, SaveableSource {
75
+ static readonly ID = 'notebook';
76
+
77
+ readonly saveable = new DelegatingSaveable();
78
+
79
+ @inject(NotebookCellToolbarFactory)
80
+ protected readonly cellToolbarFactory: NotebookCellToolbarFactory;
81
+
82
+ @inject(CommandRegistry)
83
+ protected commandRegistry: CommandRegistry;
84
+
85
+ @inject(MenuModelRegistry)
86
+ protected menuRegistry: MenuModelRegistry;
87
+
88
+ @inject(NotebookEditorWidgetService)
89
+ protected notebookEditorService: NotebookEditorWidgetService;
90
+
91
+ @inject(NotebookMainToolbarRenderer)
92
+ protected notebookMainToolbarRenderer: NotebookMainToolbarRenderer;
93
+
94
+ @inject(NotebookContextManager)
95
+ protected notebookContextManager: NotebookContextManager;
96
+
97
+ @inject(NotebookCodeCellRenderer)
98
+ protected codeCellRenderer: NotebookCodeCellRenderer;
99
+ @inject(NotebookMarkdownCellRenderer)
100
+ protected markdownCellRenderer: NotebookMarkdownCellRenderer;
101
+ @inject(NotebookEditorProps)
102
+ protected readonly props: NotebookEditorProps;
103
+
104
+ @inject(NotebookViewportService)
105
+ protected readonly viewportService: NotebookViewportService;
106
+
107
+ protected readonly onDidChangeModelEmitter = new Emitter<void>();
108
+ readonly onDidChangeModel = this.onDidChangeModelEmitter.event;
109
+
110
+ protected readonly onDidChangeReadOnlyEmitter = new Emitter<boolean | MarkdownString>();
111
+ readonly onDidChangeReadOnly = this.onDidChangeReadOnlyEmitter.event;
112
+
113
+ protected readonly onPostKernelMessageEmitter = new Emitter<unknown>();
114
+ readonly onPostKernelMessage = this.onPostKernelMessageEmitter.event;
115
+
116
+ protected readonly onDidPostKernelMessageEmitter = new Emitter<unknown>();
117
+ readonly onDidPostKernelMessage = this.onDidPostKernelMessageEmitter.event;
118
+
119
+ protected readonly onPostRendererMessageEmitter = new Emitter<RenderMessage>();
120
+ readonly onPostRendererMessage = this.onPostRendererMessageEmitter.event;
121
+
122
+ protected readonly onDidReceiveKernelMessageEmitter = new Emitter<unknown>();
123
+ readonly onDidReceiveKernelMessage = this.onDidReceiveKernelMessageEmitter.event;
124
+
125
+ protected readonly onDidChangeOutputInputFocusEmitter = new Emitter<boolean>();
126
+ readonly onDidChangeOutputInputFocus = this.onDidChangeOutputInputFocusEmitter.event;
127
+
128
+ protected readonly renderers = new Map<CellKind, CellRenderer>();
129
+ protected _model?: NotebookModel;
130
+ protected _ready: Deferred<NotebookModel> = new Deferred();
131
+ protected _findWidgetVisible = false;
132
+ protected _findWidgetRef = React.createRef<NotebookFindWidget>();
133
+ protected scrollBarRef = React.createRef<{ updateScroll(): void }>();
134
+ protected debounceFind = debounce(() => {
135
+ this._findWidgetRef.current?.search({});
136
+ }, 30, {
137
+ trailing: true,
138
+ maxWait: 100,
139
+ leading: false
140
+ });
141
+
142
+ get notebookType(): string {
143
+ return this.props.notebookType;
144
+ }
145
+
146
+ get ready(): Promise<NotebookModel> {
147
+ return this._ready.promise;
148
+ }
149
+
150
+ get model(): NotebookModel | undefined {
151
+ return this._model;
152
+ }
153
+
154
+ @postConstruct()
155
+ protected init(): void {
156
+ this.id = NOTEBOOK_EDITOR_ID_PREFIX + this.props.uri.toString();
157
+
158
+ this.scrollOptions = {
159
+ suppressScrollY: true
160
+ };
161
+
162
+ this.title.closable = true;
163
+ this.update();
164
+
165
+ this.toDispose.push(this.onDidChangeModelEmitter);
166
+ this.toDispose.push(this.onDidChangeReadOnlyEmitter);
167
+
168
+ this.renderers.set(CellKind.Markup, this.markdownCellRenderer);
169
+ this.renderers.set(CellKind.Code, this.codeCellRenderer);
170
+ this._ready.resolve(this.waitForData());
171
+ this.ready.then(model => {
172
+ if (model.cells.length === 1 && model.cells[0].source === '') {
173
+ this.commandRegistry.executeCommand(NotebookCellCommands.EDIT_COMMAND.id, model, model.cells[0]);
174
+ model.setSelectedCell(model.cells[0]);
175
+ }
176
+ });
177
+ }
178
+
179
+ protected async waitForData(): Promise<NotebookModel> {
180
+ this._model = await this.props.notebookData;
181
+ this.saveable.delegate = this._model;
182
+ this.toDispose.push(this._model);
183
+ this.toDispose.push(this._model.onDidChangeContent(() => {
184
+ // Update the scroll bar content after the content has changed
185
+ // Wait one frame to ensure that the content has been rendered
186
+ animationFrame().then(() => this.scrollBarRef.current?.updateScroll());
187
+ }));
188
+ this.toDispose.push(this._model.onContentChanged(() => {
189
+ if (this._findWidgetVisible) {
190
+ this.debounceFind();
191
+ }
192
+ }));
193
+ this.toDispose.push(this._model.onDidChangeReadOnly(readOnly => {
194
+ if (readOnly) {
195
+ lock(this.title);
196
+ } else {
197
+ unlock(this.title);
198
+ }
199
+ this.onDidChangeReadOnlyEmitter.fire(readOnly);
200
+ this.update();
201
+ }));
202
+ if (this._model.readOnly) {
203
+ lock(this.title);
204
+ }
205
+ // Ensure that the model is loaded before adding the editor
206
+ this.notebookEditorService.addNotebookEditor(this);
207
+ this._model.selectedCell = this._model.cells[0];
208
+ this.update();
209
+ this.notebookContextManager.init(this);
210
+ return this._model;
211
+ }
212
+
213
+ protected override onActivateRequest(msg: Message): void {
214
+ super.onActivateRequest(msg);
215
+ this.node.focus();
216
+ }
217
+
218
+ getResourceUri(): URI | undefined {
219
+ return this.props.uri;
220
+ }
221
+
222
+ createMoveToUri(resourceUri: URI): URI | undefined {
223
+ return this.model?.uri.withPath(resourceUri.path);
224
+ }
225
+
226
+ undo(): void {
227
+ this._model?.undo();
228
+ }
229
+
230
+ redo(): void {
231
+ this._model?.redo();
232
+ }
233
+
234
+ protected render(): ReactNode {
235
+ if (this._model) {
236
+ return <div className='theia-notebook-main-container' tabIndex={-1}>
237
+ <div className='theia-notebook-overlay'>
238
+ <NotebookFindWidget
239
+ ref={this._findWidgetRef}
240
+ hidden={!this._findWidgetVisible}
241
+ onClose={() => {
242
+ this._findWidgetVisible = false;
243
+ this._model?.findMatches({
244
+ activeFilters: [],
245
+ matchCase: false,
246
+ regex: false,
247
+ search: '',
248
+ wholeWord: false
249
+ });
250
+ this.update();
251
+ }}
252
+ onSearch={options => this._model?.findMatches(options) ?? []}
253
+ onReplace={(matches, replaceText) => this._model?.replaceAll(matches, replaceText)}
254
+ />
255
+ </div>
256
+ {this.notebookMainToolbarRenderer.render(this._model, this.node)}
257
+ <div
258
+ className='theia-notebook-viewport'
259
+ ref={(ref: HTMLDivElement) => this.viewportService.viewportElement = ref}
260
+ >
261
+ <PerfectScrollbar className='theia-notebook-scroll-container'
262
+ ref={this.scrollBarRef}
263
+ onScrollY={(e: HTMLDivElement) => this.viewportService.onScroll(e)}>
264
+ <NotebookCellListView renderers={this.renderers}
265
+ notebookModel={this._model}
266
+ notebookContext={this.notebookContextManager}
267
+ toolbarRenderer={this.cellToolbarFactory}
268
+ commandRegistry={this.commandRegistry}
269
+ menuRegistry={this.menuRegistry} />
270
+ </PerfectScrollbar>
271
+ </div>
272
+ </div>;
273
+ } else {
274
+ return <div className='theia-notebook-main-container'>
275
+ <div className='theia-notebook-main-loading-indicator'></div>
276
+ </div>;
277
+ }
278
+ }
279
+
280
+ protected override onCloseRequest(msg: Message): void {
281
+ super.onCloseRequest(msg);
282
+ this.notebookEditorService.removeNotebookEditor(this);
283
+ }
284
+
285
+ postKernelMessage(message: unknown): void {
286
+ this.onDidPostKernelMessageEmitter.fire(message);
287
+ }
288
+
289
+ postRendererMessage(rendererId: string, message: unknown): void {
290
+ this.onPostRendererMessageEmitter.fire({ rendererId, message });
291
+ }
292
+
293
+ recieveKernelMessage(message: unknown): void {
294
+ this.onDidReceiveKernelMessageEmitter.fire(message);
295
+ }
296
+
297
+ outputInputFocusChanged(focused: boolean): void {
298
+ this.onDidChangeOutputInputFocusEmitter.fire(focused);
299
+ }
300
+
301
+ showFindWidget(): void {
302
+ if (!this._findWidgetVisible) {
303
+ this._findWidgetVisible = true;
304
+ this.update();
305
+ }
306
+ this._findWidgetRef.current?.focusSearch(this._model?.selectedText);
307
+ }
308
+
309
+ override dispose(): void {
310
+ this.notebookContextManager.dispose();
311
+ this.onDidChangeModelEmitter.dispose();
312
+ this.onDidPostKernelMessageEmitter.dispose();
313
+ this.onDidReceiveKernelMessageEmitter.dispose();
314
+ this.onPostRendererMessageEmitter.dispose();
315
+ this.onDidChangeOutputInputFocusEmitter.dispose();
316
+ this.viewportService.dispose();
317
+ this._model?.dispose();
318
+ super.dispose();
319
+ }
320
+
321
+ protected override onAfterShow(msg: Message): void {
322
+ super.onAfterShow(msg);
323
+ this.notebookEditorService.notebookEditorFocusChanged(this, true);
324
+ }
325
+
326
+ protected override onAfterHide(msg: Message): void {
327
+ super.onAfterHide(msg);
328
+ this.notebookEditorService.notebookEditorFocusChanged(this, false);
329
+ }
330
+ }