@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,379 +1,379 @@
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 { Command, CommandContribution, CommandHandler, CommandRegistry, CompoundMenuNodeRole, MenuContribution, MenuModelRegistry, nls, URI } from '@theia/core';
18
- import { inject, injectable } from '@theia/core/shared/inversify';
19
- import { ApplicationShell, codicon, KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser';
20
- import { NotebookModel } from '../view-model/notebook-model';
21
- import { NotebookService } from '../service/notebook-service';
22
- import { CellEditType, CellKind, NotebookCommand } from '../../common';
23
- import { NotebookKernelQuickPickService } from '../service/notebook-kernel-quick-pick-service';
24
- import { NotebookExecutionService } from '../service/notebook-execution-service';
25
- import { NotebookEditorWidgetService } from '../service/notebook-editor-widget-service';
26
- import { NOTEBOOK_CELL_CURSOR_FIRST_LINE, NOTEBOOK_CELL_CURSOR_LAST_LINE, NOTEBOOK_CELL_FOCUSED, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_HAS_OUTPUTS } from './notebook-context-keys';
27
- import { NotebookClipboardService } from '../service/notebook-clipboard-service';
28
- import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
29
- import { NotebookEditorWidget } from '../notebook-editor-widget';
30
-
31
- export namespace NotebookCommands {
32
- export const ADD_NEW_CELL_COMMAND = Command.toDefaultLocalizedCommand({
33
- id: 'notebook.add-new-cell',
34
- iconClass: codicon('add')
35
- });
36
-
37
- export const ADD_NEW_MARKDOWN_CELL_COMMAND = Command.toDefaultLocalizedCommand({
38
- id: 'notebook.add-new-markdown-cell',
39
- iconClass: codicon('add'),
40
- tooltip: nls.localizeByDefault('Add Markdown Cell')
41
- } as NotebookCommand);
42
-
43
- export const ADD_NEW_CODE_CELL_COMMAND = Command.toDefaultLocalizedCommand({
44
- id: 'notebook.add-new-code-cell',
45
- iconClass: codicon('add'),
46
- tooltip: nls.localizeByDefault('Add Code Cell')
47
- } as NotebookCommand);
48
-
49
- export const SELECT_KERNEL_COMMAND = Command.toDefaultLocalizedCommand({
50
- id: 'notebook.selectKernel',
51
- category: 'Notebook',
52
- iconClass: codicon('server-environment')
53
- });
54
-
55
- export const EXECUTE_NOTEBOOK_COMMAND = Command.toDefaultLocalizedCommand({
56
- id: 'notebook.execute',
57
- category: 'Notebook',
58
- iconClass: codicon('run-all')
59
- });
60
-
61
- export const CLEAR_ALL_OUTPUTS_COMMAND = Command.toDefaultLocalizedCommand({
62
- id: 'notebook.clear-all-outputs',
63
- category: 'Notebook',
64
- iconClass: codicon('clear-all')
65
- });
66
-
67
- export const CHANGE_SELECTED_CELL = Command.toDefaultLocalizedCommand({
68
- id: 'notebook.change-selected-cell',
69
- category: 'Notebook',
70
- });
71
-
72
- export const CUT_SELECTED_CELL = Command.toDefaultLocalizedCommand({
73
- id: 'notebook.cell.cut',
74
- category: 'Notebook',
75
- });
76
-
77
- export const COPY_SELECTED_CELL = Command.toDefaultLocalizedCommand({
78
- id: 'notebook.cell.copy',
79
- category: 'Notebook',
80
- });
81
-
82
- export const PASTE_CELL = Command.toDefaultLocalizedCommand({
83
- id: 'notebook.cell.paste',
84
- category: 'Notebook',
85
- });
86
-
87
- export const NOTEBOOK_FIND = Command.toDefaultLocalizedCommand({
88
- id: 'notebook.find',
89
- category: 'Notebook',
90
- });
91
-
92
- export const CENTER_ACTIVE_CELL = Command.toDefaultLocalizedCommand({
93
- id: 'notebook.centerActiveCell',
94
- category: 'Notebook',
95
- });
96
- }
97
-
98
- export enum CellChangeDirection {
99
- Up = 'up',
100
- Down = 'down'
101
- }
102
-
103
- @injectable()
104
- export class NotebookActionsContribution implements CommandContribution, MenuContribution, KeybindingContribution {
105
-
106
- @inject(NotebookService)
107
- protected notebookService: NotebookService;
108
-
109
- @inject(NotebookKernelQuickPickService)
110
- protected notebookKernelQuickPickService: NotebookKernelQuickPickService;
111
-
112
- @inject(NotebookExecutionService)
113
- protected notebookExecutionService: NotebookExecutionService;
114
-
115
- @inject(ApplicationShell)
116
- protected shell: ApplicationShell;
117
-
118
- @inject(NotebookEditorWidgetService)
119
- protected notebookEditorWidgetService: NotebookEditorWidgetService;
120
-
121
- @inject(NotebookClipboardService)
122
- protected notebookClipboardService: NotebookClipboardService;
123
-
124
- @inject(ContextKeyService)
125
- protected contextKeyService: ContextKeyService;
126
-
127
- registerCommands(commands: CommandRegistry): void {
128
- commands.registerCommand(NotebookCommands.ADD_NEW_CELL_COMMAND, {
129
- execute: (notebookModel: NotebookModel, cellKind: CellKind = CellKind.Markup, index?: number | 'above' | 'below', focusContainer?: boolean) => {
130
- notebookModel = notebookModel ?? this.notebookEditorWidgetService.focusedEditor?.model;
131
-
132
- let insertIndex: number = 0;
133
- if (typeof index === 'number' && index >= 0) {
134
- insertIndex = index;
135
- } else if (notebookModel.selectedCell && typeof index === 'string') {
136
- // if index is -1 insert below otherwise at the index of the selected cell which is above the selected.
137
- insertIndex = notebookModel.cells.indexOf(notebookModel.selectedCell) + (index === 'below' ? 1 : 0);
138
- }
139
-
140
- let cellLanguage: string = 'markdown';
141
- if (cellKind === CellKind.Code) {
142
- cellLanguage = this.notebookService.getCodeCellLanguage(notebookModel);
143
- }
144
-
145
- notebookModel.applyEdits([{
146
- editType: CellEditType.Replace,
147
- index: insertIndex,
148
- count: 0,
149
- cells: [{
150
- cellKind,
151
- language: cellLanguage,
152
- source: '',
153
- outputs: [],
154
- metadata: {},
155
- }]
156
- }], true);
157
- if (focusContainer) {
158
- notebookModel.selectedCell?.requestBlurEditor();
159
- }
160
- }
161
- });
162
-
163
- commands.registerCommand(NotebookCommands.ADD_NEW_MARKDOWN_CELL_COMMAND, this.editableCommandHandler(
164
- notebookModel => commands.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id, notebookModel, CellKind.Markup, 'below')
165
- ));
166
-
167
- commands.registerCommand(NotebookCommands.ADD_NEW_CODE_CELL_COMMAND, this.editableCommandHandler(
168
- notebookModel => commands.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id, notebookModel, CellKind.Code, 'below')
169
- ));
170
-
171
- commands.registerCommand(NotebookCommands.SELECT_KERNEL_COMMAND, this.editableCommandHandler(
172
- notebookModel => this.notebookKernelQuickPickService.showQuickPick(notebookModel)
173
- ));
174
-
175
- commands.registerCommand(NotebookCommands.EXECUTE_NOTEBOOK_COMMAND, this.editableCommandHandler(
176
- notebookModel => this.notebookExecutionService.executeNotebookCells(notebookModel, notebookModel.cells)
177
- ));
178
-
179
- commands.registerCommand(NotebookCommands.CLEAR_ALL_OUTPUTS_COMMAND, this.editableCommandHandler(
180
- notebookModel => notebookModel.applyEdits(notebookModel.cells.map(cell => ({
181
- editType: CellEditType.Output,
182
- handle: cell.handle, deleteCount: cell.outputs.length, outputs: []
183
- })), false)
184
- ));
185
-
186
- commands.registerCommand(NotebookCommands.CHANGE_SELECTED_CELL,
187
- {
188
- execute: (change: number | CellChangeDirection) => {
189
- const focusedEditor = this.notebookEditorWidgetService.focusedEditor;
190
- const model = focusedEditor?.model;
191
- if (model && typeof change === 'number') {
192
- model.setSelectedCell(model.cells[change]);
193
- } else if (model && model.selectedCell) {
194
- const currentIndex = model.cells.indexOf(model.selectedCell);
195
- const shouldFocusEditor = this.contextKeyService.match('editorTextFocus');
196
-
197
- if (change === CellChangeDirection.Up && currentIndex > 0) {
198
- model.setSelectedCell(model.cells[currentIndex - 1]);
199
- if ((model.selectedCell?.cellKind === CellKind.Code
200
- || (model.selectedCell?.cellKind === CellKind.Markup && model.selectedCell?.editing)) && shouldFocusEditor) {
201
- model.selectedCell.requestFocusEditor('lastLine');
202
- }
203
- } else if (change === CellChangeDirection.Down && currentIndex < model.cells.length - 1) {
204
- model.setSelectedCell(model.cells[currentIndex + 1]);
205
- if ((model.selectedCell?.cellKind === CellKind.Code
206
- || (model.selectedCell?.cellKind === CellKind.Markup && model.selectedCell?.editing)) && shouldFocusEditor) {
207
- model.selectedCell.requestFocusEditor();
208
- }
209
- }
210
-
211
- if (model.selectedCell.cellKind === CellKind.Markup) {
212
- // since were losing focus from the cell editor, we need to focus the notebook editor again
213
- focusedEditor?.node.focus();
214
- }
215
- }
216
- }
217
- }
218
- );
219
- commands.registerCommand({ id: 'list.focusUp' }, {
220
- execute: () => commands.executeCommand(NotebookCommands.CHANGE_SELECTED_CELL.id, CellChangeDirection.Up)
221
- });
222
- commands.registerCommand({ id: 'list.focusDown' }, {
223
- execute: () => commands.executeCommand(NotebookCommands.CHANGE_SELECTED_CELL.id, CellChangeDirection.Down)
224
- });
225
-
226
- commands.registerCommand(NotebookCommands.CUT_SELECTED_CELL, this.editableCommandHandler(
227
- () => {
228
- const model = this.notebookEditorWidgetService.focusedEditor?.model;
229
- const selectedCell = model?.selectedCell;
230
- if (selectedCell) {
231
- model.applyEdits([{ editType: CellEditType.Replace, index: model.cells.indexOf(selectedCell), count: 1, cells: [] }], true);
232
- this.notebookClipboardService.copyCell(selectedCell);
233
- }
234
- }));
235
-
236
- commands.registerCommand(NotebookCommands.COPY_SELECTED_CELL, {
237
- execute: () => {
238
- const model = this.notebookEditorWidgetService.focusedEditor?.model;
239
- const selectedCell = model?.selectedCell;
240
- if (selectedCell) {
241
- this.notebookClipboardService.copyCell(selectedCell);
242
- }
243
- }
244
- });
245
-
246
- commands.registerCommand(NotebookCommands.PASTE_CELL, {
247
- isEnabled: () => !Boolean(this.notebookEditorWidgetService.focusedEditor?.model?.readOnly),
248
- isVisible: () => !Boolean(this.notebookEditorWidgetService.focusedEditor?.model?.readOnly),
249
- execute: (position?: 'above') => {
250
- const copiedCell = this.notebookClipboardService.getCell();
251
- if (copiedCell) {
252
- const model = this.notebookEditorWidgetService.focusedEditor?.model;
253
- const insertIndex = model?.selectedCell ? model.cells.indexOf(model.selectedCell) + (position === 'above' ? 0 : 1) : 0;
254
- model?.applyEdits([{ editType: CellEditType.Replace, index: insertIndex, count: 0, cells: [copiedCell] }], true);
255
- }
256
- }
257
- });
258
-
259
- commands.registerCommand(NotebookCommands.NOTEBOOK_FIND, {
260
- execute: () => {
261
- this.notebookEditorWidgetService.focusedEditor?.showFindWidget();
262
- }
263
- });
264
-
265
- commands.registerCommand(NotebookCommands.CENTER_ACTIVE_CELL, {
266
- execute: (editor?: NotebookEditorWidget) => {
267
- const model = editor ? editor.model : this.notebookEditorWidgetService.focusedEditor?.model;
268
- model?.selectedCell?.requestCenterEditor();
269
- }
270
- });
271
-
272
- }
273
-
274
- protected editableCommandHandler(execute: (notebookModel: NotebookModel) => void): CommandHandler {
275
- return {
276
- isEnabled: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
277
- isVisible: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
278
- execute: (uri: URI | NotebookModel) => {
279
- this.withModel(uri, execute, undefined);
280
- }
281
- };
282
- }
283
-
284
- protected withModel<T>(item: URI | NotebookModel, execute: (notebookModel: NotebookModel) => T, defaultValue: T): T {
285
- if (item instanceof URI) {
286
- const model = this.notebookService.getNotebookEditorModel(item);
287
- if (!model) {
288
- return defaultValue;
289
- }
290
- item = model;
291
- }
292
- return execute(item);
293
- }
294
-
295
- registerMenus(menus: MenuModelRegistry): void {
296
- // independent submenu for plugins to add commands
297
- menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR, 'Notebook Main Toolbar');
298
- // Add Notebook Cell items
299
- menus.registerSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, 'Add Notebook Cell', { role: CompoundMenuNodeRole.Group });
300
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, {
301
- commandId: NotebookCommands.ADD_NEW_CODE_CELL_COMMAND.id,
302
- label: nls.localizeByDefault('Code'),
303
- icon: codicon('add'),
304
- });
305
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, {
306
- commandId: NotebookCommands.ADD_NEW_MARKDOWN_CELL_COMMAND.id,
307
- label: nls.localizeByDefault('Markdown'),
308
- icon: codicon('add'),
309
- });
310
-
311
- // Execution related items
312
- menus.registerSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, 'Cell Execution', { role: CompoundMenuNodeRole.Group });
313
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, {
314
- commandId: NotebookCommands.EXECUTE_NOTEBOOK_COMMAND.id,
315
- label: nls.localizeByDefault('Run All'),
316
- icon: codicon('run-all'),
317
- order: '10'
318
- });
319
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, {
320
- commandId: NotebookCommands.CLEAR_ALL_OUTPUTS_COMMAND.id,
321
- label: nls.localizeByDefault('Clear All Outputs'),
322
- icon: codicon('clear-all'),
323
- order: '30',
324
- when: NOTEBOOK_HAS_OUTPUTS
325
- });
326
-
327
- menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU, '');
328
- }
329
-
330
- registerKeybindings(keybindings: KeybindingRegistry): void {
331
- keybindings.registerKeybindings(
332
- {
333
- command: NotebookCommands.CHANGE_SELECTED_CELL.id,
334
- keybinding: 'up',
335
- args: CellChangeDirection.Up,
336
- when: `(!editorTextFocus || ${NOTEBOOK_CELL_CURSOR_FIRST_LINE}) && !suggestWidgetVisible && ${NOTEBOOK_EDITOR_FOCUSED} && ${NOTEBOOK_CELL_FOCUSED}`
337
- },
338
- {
339
- command: NotebookCommands.CHANGE_SELECTED_CELL.id,
340
- keybinding: 'down',
341
- args: CellChangeDirection.Down,
342
- when: `(!editorTextFocus || ${NOTEBOOK_CELL_CURSOR_LAST_LINE}) && !suggestWidgetVisible && ${NOTEBOOK_EDITOR_FOCUSED} && ${NOTEBOOK_CELL_FOCUSED}`
343
- },
344
- {
345
- command: NotebookCommands.CUT_SELECTED_CELL.id,
346
- keybinding: 'ctrlcmd+x',
347
- when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
348
- },
349
- {
350
- command: NotebookCommands.COPY_SELECTED_CELL.id,
351
- keybinding: 'ctrlcmd+c',
352
- when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
353
- },
354
- {
355
- command: NotebookCommands.PASTE_CELL.id,
356
- keybinding: 'ctrlcmd+v',
357
- when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
358
- },
359
- {
360
- command: NotebookCommands.NOTEBOOK_FIND.id,
361
- keybinding: 'ctrlcmd+f',
362
- when: `${NOTEBOOK_EDITOR_FOCUSED}`
363
- },
364
- {
365
- command: NotebookCommands.CENTER_ACTIVE_CELL.id,
366
- keybinding: 'ctrlcmd+l',
367
- when: `${NOTEBOOK_EDITOR_FOCUSED}`
368
- }
369
- );
370
- }
371
-
372
- }
373
-
374
- export namespace NotebookMenus {
375
- export const NOTEBOOK_MAIN_TOOLBAR = 'notebook/toolbar';
376
- export const NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP = [NOTEBOOK_MAIN_TOOLBAR, 'cell-add-group'];
377
- export const NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP = [NOTEBOOK_MAIN_TOOLBAR, 'cell-execution-group'];
378
- export const NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU = 'notebook-main-toolbar-hidden-items-context-menu';
379
- }
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 { Command, CommandContribution, CommandHandler, CommandRegistry, CompoundMenuNodeRole, MenuContribution, MenuModelRegistry, nls, URI } from '@theia/core';
18
+ import { inject, injectable } from '@theia/core/shared/inversify';
19
+ import { ApplicationShell, codicon, KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser';
20
+ import { NotebookModel } from '../view-model/notebook-model';
21
+ import { NotebookService } from '../service/notebook-service';
22
+ import { CellEditType, CellKind, NotebookCommand } from '../../common';
23
+ import { NotebookKernelQuickPickService } from '../service/notebook-kernel-quick-pick-service';
24
+ import { NotebookExecutionService } from '../service/notebook-execution-service';
25
+ import { NotebookEditorWidgetService } from '../service/notebook-editor-widget-service';
26
+ import { NOTEBOOK_CELL_CURSOR_FIRST_LINE, NOTEBOOK_CELL_CURSOR_LAST_LINE, NOTEBOOK_CELL_FOCUSED, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_HAS_OUTPUTS } from './notebook-context-keys';
27
+ import { NotebookClipboardService } from '../service/notebook-clipboard-service';
28
+ import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
29
+ import { NotebookEditorWidget } from '../notebook-editor-widget';
30
+
31
+ export namespace NotebookCommands {
32
+ export const ADD_NEW_CELL_COMMAND = Command.toDefaultLocalizedCommand({
33
+ id: 'notebook.add-new-cell',
34
+ iconClass: codicon('add')
35
+ });
36
+
37
+ export const ADD_NEW_MARKDOWN_CELL_COMMAND = Command.toDefaultLocalizedCommand({
38
+ id: 'notebook.add-new-markdown-cell',
39
+ iconClass: codicon('add'),
40
+ tooltip: nls.localizeByDefault('Add Markdown Cell')
41
+ } as NotebookCommand);
42
+
43
+ export const ADD_NEW_CODE_CELL_COMMAND = Command.toDefaultLocalizedCommand({
44
+ id: 'notebook.add-new-code-cell',
45
+ iconClass: codicon('add'),
46
+ tooltip: nls.localizeByDefault('Add Code Cell')
47
+ } as NotebookCommand);
48
+
49
+ export const SELECT_KERNEL_COMMAND = Command.toDefaultLocalizedCommand({
50
+ id: 'notebook.selectKernel',
51
+ category: 'Notebook',
52
+ iconClass: codicon('server-environment')
53
+ });
54
+
55
+ export const EXECUTE_NOTEBOOK_COMMAND = Command.toDefaultLocalizedCommand({
56
+ id: 'notebook.execute',
57
+ category: 'Notebook',
58
+ iconClass: codicon('run-all')
59
+ });
60
+
61
+ export const CLEAR_ALL_OUTPUTS_COMMAND = Command.toDefaultLocalizedCommand({
62
+ id: 'notebook.clear-all-outputs',
63
+ category: 'Notebook',
64
+ iconClass: codicon('clear-all')
65
+ });
66
+
67
+ export const CHANGE_SELECTED_CELL = Command.toDefaultLocalizedCommand({
68
+ id: 'notebook.change-selected-cell',
69
+ category: 'Notebook',
70
+ });
71
+
72
+ export const CUT_SELECTED_CELL = Command.toDefaultLocalizedCommand({
73
+ id: 'notebook.cell.cut',
74
+ category: 'Notebook',
75
+ });
76
+
77
+ export const COPY_SELECTED_CELL = Command.toDefaultLocalizedCommand({
78
+ id: 'notebook.cell.copy',
79
+ category: 'Notebook',
80
+ });
81
+
82
+ export const PASTE_CELL = Command.toDefaultLocalizedCommand({
83
+ id: 'notebook.cell.paste',
84
+ category: 'Notebook',
85
+ });
86
+
87
+ export const NOTEBOOK_FIND = Command.toDefaultLocalizedCommand({
88
+ id: 'notebook.find',
89
+ category: 'Notebook',
90
+ });
91
+
92
+ export const CENTER_ACTIVE_CELL = Command.toDefaultLocalizedCommand({
93
+ id: 'notebook.centerActiveCell',
94
+ category: 'Notebook',
95
+ });
96
+ }
97
+
98
+ export enum CellChangeDirection {
99
+ Up = 'up',
100
+ Down = 'down'
101
+ }
102
+
103
+ @injectable()
104
+ export class NotebookActionsContribution implements CommandContribution, MenuContribution, KeybindingContribution {
105
+
106
+ @inject(NotebookService)
107
+ protected notebookService: NotebookService;
108
+
109
+ @inject(NotebookKernelQuickPickService)
110
+ protected notebookKernelQuickPickService: NotebookKernelQuickPickService;
111
+
112
+ @inject(NotebookExecutionService)
113
+ protected notebookExecutionService: NotebookExecutionService;
114
+
115
+ @inject(ApplicationShell)
116
+ protected shell: ApplicationShell;
117
+
118
+ @inject(NotebookEditorWidgetService)
119
+ protected notebookEditorWidgetService: NotebookEditorWidgetService;
120
+
121
+ @inject(NotebookClipboardService)
122
+ protected notebookClipboardService: NotebookClipboardService;
123
+
124
+ @inject(ContextKeyService)
125
+ protected contextKeyService: ContextKeyService;
126
+
127
+ registerCommands(commands: CommandRegistry): void {
128
+ commands.registerCommand(NotebookCommands.ADD_NEW_CELL_COMMAND, {
129
+ execute: (notebookModel: NotebookModel, cellKind: CellKind = CellKind.Markup, index?: number | 'above' | 'below', focusContainer?: boolean) => {
130
+ notebookModel = notebookModel ?? this.notebookEditorWidgetService.focusedEditor?.model;
131
+
132
+ let insertIndex: number = 0;
133
+ if (typeof index === 'number' && index >= 0) {
134
+ insertIndex = index;
135
+ } else if (notebookModel.selectedCell && typeof index === 'string') {
136
+ // if index is -1 insert below otherwise at the index of the selected cell which is above the selected.
137
+ insertIndex = notebookModel.cells.indexOf(notebookModel.selectedCell) + (index === 'below' ? 1 : 0);
138
+ }
139
+
140
+ let cellLanguage: string = 'markdown';
141
+ if (cellKind === CellKind.Code) {
142
+ cellLanguage = this.notebookService.getCodeCellLanguage(notebookModel);
143
+ }
144
+
145
+ notebookModel.applyEdits([{
146
+ editType: CellEditType.Replace,
147
+ index: insertIndex,
148
+ count: 0,
149
+ cells: [{
150
+ cellKind,
151
+ language: cellLanguage,
152
+ source: '',
153
+ outputs: [],
154
+ metadata: {},
155
+ }]
156
+ }], true);
157
+ if (focusContainer) {
158
+ notebookModel.selectedCell?.requestBlurEditor();
159
+ }
160
+ }
161
+ });
162
+
163
+ commands.registerCommand(NotebookCommands.ADD_NEW_MARKDOWN_CELL_COMMAND, this.editableCommandHandler(
164
+ notebookModel => commands.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id, notebookModel, CellKind.Markup, 'below')
165
+ ));
166
+
167
+ commands.registerCommand(NotebookCommands.ADD_NEW_CODE_CELL_COMMAND, this.editableCommandHandler(
168
+ notebookModel => commands.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id, notebookModel, CellKind.Code, 'below')
169
+ ));
170
+
171
+ commands.registerCommand(NotebookCommands.SELECT_KERNEL_COMMAND, this.editableCommandHandler(
172
+ notebookModel => this.notebookKernelQuickPickService.showQuickPick(notebookModel)
173
+ ));
174
+
175
+ commands.registerCommand(NotebookCommands.EXECUTE_NOTEBOOK_COMMAND, this.editableCommandHandler(
176
+ notebookModel => this.notebookExecutionService.executeNotebookCells(notebookModel, notebookModel.cells)
177
+ ));
178
+
179
+ commands.registerCommand(NotebookCommands.CLEAR_ALL_OUTPUTS_COMMAND, this.editableCommandHandler(
180
+ notebookModel => notebookModel.applyEdits(notebookModel.cells.map(cell => ({
181
+ editType: CellEditType.Output,
182
+ handle: cell.handle, deleteCount: cell.outputs.length, outputs: []
183
+ })), false)
184
+ ));
185
+
186
+ commands.registerCommand(NotebookCommands.CHANGE_SELECTED_CELL,
187
+ {
188
+ execute: (change: number | CellChangeDirection) => {
189
+ const focusedEditor = this.notebookEditorWidgetService.focusedEditor;
190
+ const model = focusedEditor?.model;
191
+ if (model && typeof change === 'number') {
192
+ model.setSelectedCell(model.cells[change]);
193
+ } else if (model && model.selectedCell) {
194
+ const currentIndex = model.cells.indexOf(model.selectedCell);
195
+ const shouldFocusEditor = this.contextKeyService.match('editorTextFocus');
196
+
197
+ if (change === CellChangeDirection.Up && currentIndex > 0) {
198
+ model.setSelectedCell(model.cells[currentIndex - 1]);
199
+ if ((model.selectedCell?.cellKind === CellKind.Code
200
+ || (model.selectedCell?.cellKind === CellKind.Markup && model.selectedCell?.editing)) && shouldFocusEditor) {
201
+ model.selectedCell.requestFocusEditor('lastLine');
202
+ }
203
+ } else if (change === CellChangeDirection.Down && currentIndex < model.cells.length - 1) {
204
+ model.setSelectedCell(model.cells[currentIndex + 1]);
205
+ if ((model.selectedCell?.cellKind === CellKind.Code
206
+ || (model.selectedCell?.cellKind === CellKind.Markup && model.selectedCell?.editing)) && shouldFocusEditor) {
207
+ model.selectedCell.requestFocusEditor();
208
+ }
209
+ }
210
+
211
+ if (model.selectedCell.cellKind === CellKind.Markup) {
212
+ // since were losing focus from the cell editor, we need to focus the notebook editor again
213
+ focusedEditor?.node.focus();
214
+ }
215
+ }
216
+ }
217
+ }
218
+ );
219
+ commands.registerCommand({ id: 'list.focusUp' }, {
220
+ execute: () => commands.executeCommand(NotebookCommands.CHANGE_SELECTED_CELL.id, CellChangeDirection.Up)
221
+ });
222
+ commands.registerCommand({ id: 'list.focusDown' }, {
223
+ execute: () => commands.executeCommand(NotebookCommands.CHANGE_SELECTED_CELL.id, CellChangeDirection.Down)
224
+ });
225
+
226
+ commands.registerCommand(NotebookCommands.CUT_SELECTED_CELL, this.editableCommandHandler(
227
+ () => {
228
+ const model = this.notebookEditorWidgetService.focusedEditor?.model;
229
+ const selectedCell = model?.selectedCell;
230
+ if (selectedCell) {
231
+ model.applyEdits([{ editType: CellEditType.Replace, index: model.cells.indexOf(selectedCell), count: 1, cells: [] }], true);
232
+ this.notebookClipboardService.copyCell(selectedCell);
233
+ }
234
+ }));
235
+
236
+ commands.registerCommand(NotebookCommands.COPY_SELECTED_CELL, {
237
+ execute: () => {
238
+ const model = this.notebookEditorWidgetService.focusedEditor?.model;
239
+ const selectedCell = model?.selectedCell;
240
+ if (selectedCell) {
241
+ this.notebookClipboardService.copyCell(selectedCell);
242
+ }
243
+ }
244
+ });
245
+
246
+ commands.registerCommand(NotebookCommands.PASTE_CELL, {
247
+ isEnabled: () => !Boolean(this.notebookEditorWidgetService.focusedEditor?.model?.readOnly),
248
+ isVisible: () => !Boolean(this.notebookEditorWidgetService.focusedEditor?.model?.readOnly),
249
+ execute: (position?: 'above') => {
250
+ const copiedCell = this.notebookClipboardService.getCell();
251
+ if (copiedCell) {
252
+ const model = this.notebookEditorWidgetService.focusedEditor?.model;
253
+ const insertIndex = model?.selectedCell ? model.cells.indexOf(model.selectedCell) + (position === 'above' ? 0 : 1) : 0;
254
+ model?.applyEdits([{ editType: CellEditType.Replace, index: insertIndex, count: 0, cells: [copiedCell] }], true);
255
+ }
256
+ }
257
+ });
258
+
259
+ commands.registerCommand(NotebookCommands.NOTEBOOK_FIND, {
260
+ execute: () => {
261
+ this.notebookEditorWidgetService.focusedEditor?.showFindWidget();
262
+ }
263
+ });
264
+
265
+ commands.registerCommand(NotebookCommands.CENTER_ACTIVE_CELL, {
266
+ execute: (editor?: NotebookEditorWidget) => {
267
+ const model = editor ? editor.model : this.notebookEditorWidgetService.focusedEditor?.model;
268
+ model?.selectedCell?.requestCenterEditor();
269
+ }
270
+ });
271
+
272
+ }
273
+
274
+ protected editableCommandHandler(execute: (notebookModel: NotebookModel) => void): CommandHandler {
275
+ return {
276
+ isEnabled: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
277
+ isVisible: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
278
+ execute: (uri: URI | NotebookModel) => {
279
+ this.withModel(uri, execute, undefined);
280
+ }
281
+ };
282
+ }
283
+
284
+ protected withModel<T>(item: URI | NotebookModel, execute: (notebookModel: NotebookModel) => T, defaultValue: T): T {
285
+ if (item instanceof URI) {
286
+ const model = this.notebookService.getNotebookEditorModel(item);
287
+ if (!model) {
288
+ return defaultValue;
289
+ }
290
+ item = model;
291
+ }
292
+ return execute(item);
293
+ }
294
+
295
+ registerMenus(menus: MenuModelRegistry): void {
296
+ // independent submenu for plugins to add commands
297
+ menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR, 'Notebook Main Toolbar');
298
+ // Add Notebook Cell items
299
+ menus.registerSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, 'Add Notebook Cell', { role: CompoundMenuNodeRole.Group });
300
+ menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, {
301
+ commandId: NotebookCommands.ADD_NEW_CODE_CELL_COMMAND.id,
302
+ label: nls.localizeByDefault('Code'),
303
+ icon: codicon('add'),
304
+ });
305
+ menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, {
306
+ commandId: NotebookCommands.ADD_NEW_MARKDOWN_CELL_COMMAND.id,
307
+ label: nls.localizeByDefault('Markdown'),
308
+ icon: codicon('add'),
309
+ });
310
+
311
+ // Execution related items
312
+ menus.registerSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, 'Cell Execution', { role: CompoundMenuNodeRole.Group });
313
+ menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, {
314
+ commandId: NotebookCommands.EXECUTE_NOTEBOOK_COMMAND.id,
315
+ label: nls.localizeByDefault('Run All'),
316
+ icon: codicon('run-all'),
317
+ order: '10'
318
+ });
319
+ menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, {
320
+ commandId: NotebookCommands.CLEAR_ALL_OUTPUTS_COMMAND.id,
321
+ label: nls.localizeByDefault('Clear All Outputs'),
322
+ icon: codicon('clear-all'),
323
+ order: '30',
324
+ when: NOTEBOOK_HAS_OUTPUTS
325
+ });
326
+
327
+ menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU, '');
328
+ }
329
+
330
+ registerKeybindings(keybindings: KeybindingRegistry): void {
331
+ keybindings.registerKeybindings(
332
+ {
333
+ command: NotebookCommands.CHANGE_SELECTED_CELL.id,
334
+ keybinding: 'up',
335
+ args: CellChangeDirection.Up,
336
+ when: `(!editorTextFocus || ${NOTEBOOK_CELL_CURSOR_FIRST_LINE}) && !suggestWidgetVisible && ${NOTEBOOK_EDITOR_FOCUSED} && ${NOTEBOOK_CELL_FOCUSED}`
337
+ },
338
+ {
339
+ command: NotebookCommands.CHANGE_SELECTED_CELL.id,
340
+ keybinding: 'down',
341
+ args: CellChangeDirection.Down,
342
+ when: `(!editorTextFocus || ${NOTEBOOK_CELL_CURSOR_LAST_LINE}) && !suggestWidgetVisible && ${NOTEBOOK_EDITOR_FOCUSED} && ${NOTEBOOK_CELL_FOCUSED}`
343
+ },
344
+ {
345
+ command: NotebookCommands.CUT_SELECTED_CELL.id,
346
+ keybinding: 'ctrlcmd+x',
347
+ when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
348
+ },
349
+ {
350
+ command: NotebookCommands.COPY_SELECTED_CELL.id,
351
+ keybinding: 'ctrlcmd+c',
352
+ when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
353
+ },
354
+ {
355
+ command: NotebookCommands.PASTE_CELL.id,
356
+ keybinding: 'ctrlcmd+v',
357
+ when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
358
+ },
359
+ {
360
+ command: NotebookCommands.NOTEBOOK_FIND.id,
361
+ keybinding: 'ctrlcmd+f',
362
+ when: `${NOTEBOOK_EDITOR_FOCUSED}`
363
+ },
364
+ {
365
+ command: NotebookCommands.CENTER_ACTIVE_CELL.id,
366
+ keybinding: 'ctrlcmd+l',
367
+ when: `${NOTEBOOK_EDITOR_FOCUSED}`
368
+ }
369
+ );
370
+ }
371
+
372
+ }
373
+
374
+ export namespace NotebookMenus {
375
+ export const NOTEBOOK_MAIN_TOOLBAR = 'notebook/toolbar';
376
+ export const NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP = [NOTEBOOK_MAIN_TOOLBAR, 'cell-add-group'];
377
+ export const NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP = [NOTEBOOK_MAIN_TOOLBAR, 'cell-execution-group'];
378
+ export const NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU = 'notebook-main-toolbar-hidden-items-context-menu';
379
+ }