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