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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/contributions/cell-operations.d.ts.map +1 -1
  3. package/lib/browser/contributions/cell-operations.js +8 -1
  4. package/lib/browser/contributions/cell-operations.js.map +1 -1
  5. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  6. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  7. package/lib/browser/contributions/notebook-actions-contribution.js +31 -6
  8. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +4 -0
  10. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  11. package/lib/browser/contributions/notebook-cell-actions-contribution.js +49 -12
  12. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  13. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
  14. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
  15. package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
  16. package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
  17. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  18. package/lib/browser/notebook-editor-widget.js +3 -5
  19. package/lib/browser/notebook-editor-widget.js.map +1 -1
  20. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  21. package/lib/browser/notebook-frontend-module.js +3 -0
  22. package/lib/browser/notebook-frontend-module.js.map +1 -1
  23. package/lib/browser/notebook-open-handler.d.ts +3 -2
  24. package/lib/browser/notebook-open-handler.d.ts.map +1 -1
  25. package/lib/browser/notebook-open-handler.js +12 -5
  26. package/lib/browser/notebook-open-handler.js.map +1 -1
  27. package/lib/browser/service/notebook-options.d.ts +1 -0
  28. package/lib/browser/service/notebook-options.d.ts.map +1 -1
  29. package/lib/browser/service/notebook-options.js +1 -0
  30. package/lib/browser/service/notebook-options.js.map +1 -1
  31. package/lib/browser/service/notebook-service.d.ts +1 -0
  32. package/lib/browser/service/notebook-service.d.ts.map +1 -1
  33. package/lib/browser/service/notebook-service.js +7 -0
  34. package/lib/browser/service/notebook-service.js.map +1 -1
  35. package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
  36. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  37. package/lib/browser/view/notebook-cell-editor.js +30 -16
  38. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  39. package/lib/browser/view/notebook-cell-list-view.d.ts +7 -4
  40. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  41. package/lib/browser/view/notebook-cell-list-view.js +39 -29
  42. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  43. package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
  44. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  45. package/lib/browser/view-model/notebook-cell-model.js +5 -0
  46. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  47. package/package.json +8 -8
  48. package/src/browser/contributions/cell-operations.ts +44 -39
  49. package/src/browser/contributions/notebook-actions-contribution.ts +379 -351
  50. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -485
  51. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  52. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  53. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  54. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  55. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  56. package/src/browser/contributions/notebook-preferences.ts +92 -92
  57. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
  58. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  59. package/src/browser/index.ts +27 -27
  60. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  61. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  62. package/src/browser/notebook-editor-widget.tsx +330 -331
  63. package/src/browser/notebook-frontend-module.ts +119 -115
  64. package/src/browser/notebook-open-handler.ts +120 -114
  65. package/src/browser/notebook-output-utils.ts +119 -119
  66. package/src/browser/notebook-renderer-registry.ts +85 -85
  67. package/src/browser/notebook-type-registry.ts +54 -54
  68. package/src/browser/notebook-types.ts +186 -186
  69. package/src/browser/renderers/cell-output-webview.ts +33 -33
  70. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  71. package/src/browser/service/notebook-context-manager.ts +162 -162
  72. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  73. package/src/browser/service/notebook-execution-service.ts +139 -139
  74. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  75. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  76. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  77. package/src/browser/service/notebook-kernel-service.ts +357 -357
  78. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  79. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  80. package/src/browser/service/notebook-options.ts +155 -154
  81. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  82. package/src/browser/service/notebook-service.ts +215 -209
  83. package/src/browser/style/index.css +483 -467
  84. package/src/browser/view/notebook-cell-editor.tsx +263 -247
  85. package/src/browser/view/notebook-cell-list-view.tsx +279 -259
  86. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  87. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  88. package/src/browser/view/notebook-code-cell-view.tsx +350 -350
  89. package/src/browser/view/notebook-find-widget.tsx +335 -335
  90. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  91. package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
  92. package/src/browser/view/notebook-viewport-service.ts +61 -61
  93. package/src/browser/view-model/notebook-cell-model.ts +473 -466
  94. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  95. package/src/browser/view-model/notebook-model.ts +550 -550
  96. package/src/common/index.ts +18 -18
  97. package/src/common/notebook-common.ts +337 -337
  98. package/src/common/notebook-protocol.ts +35 -35
  99. package/src/common/notebook-range.ts +30 -30
@@ -1,351 +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
- const firstCodeCell = notebookModel.cells.find(cell => cell.cellKind === CellKind.Code);
137
- cellLanguage = firstCodeCell?.language ?? 'plaintext';
138
- }
139
-
140
- notebookModel.applyEdits([{
141
- editType: CellEditType.Replace,
142
- index: insertIndex,
143
- count: 0,
144
- cells: [{
145
- cellKind,
146
- language: cellLanguage,
147
- source: '',
148
- outputs: [],
149
- metadata: {},
150
- }]
151
- }], true);
152
- }
153
- });
154
-
155
- commands.registerCommand(NotebookCommands.ADD_NEW_MARKDOWN_CELL_COMMAND, this.editableCommandHandler(
156
- notebookModel => commands.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id, notebookModel, CellKind.Markup, 'below')
157
- ));
158
-
159
- commands.registerCommand(NotebookCommands.ADD_NEW_CODE_CELL_COMMAND, this.editableCommandHandler(
160
- notebookModel => commands.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id, notebookModel, CellKind.Code, 'below')
161
- ));
162
-
163
- commands.registerCommand(NotebookCommands.SELECT_KERNEL_COMMAND, this.editableCommandHandler(
164
- notebookModel => this.notebookKernelQuickPickService.showQuickPick(notebookModel)
165
- ));
166
-
167
- commands.registerCommand(NotebookCommands.EXECUTE_NOTEBOOK_COMMAND, this.editableCommandHandler(
168
- notebookModel => this.notebookExecutionService.executeNotebookCells(notebookModel, notebookModel.cells)
169
- ));
170
-
171
- commands.registerCommand(NotebookCommands.CLEAR_ALL_OUTPUTS_COMMAND, this.editableCommandHandler(
172
- notebookModel => notebookModel.applyEdits(notebookModel.cells.map(cell => ({
173
- editType: CellEditType.Output,
174
- handle: cell.handle, deleteCount: cell.outputs.length, outputs: []
175
- })), false)
176
- ));
177
-
178
- commands.registerCommand(NotebookCommands.CHANGE_SELECTED_CELL,
179
- {
180
- execute: (change: number | CellChangeDirection) => {
181
- const focusedEditor = this.notebookEditorWidgetService.focusedEditor;
182
- const model = focusedEditor?.model;
183
- if (model && typeof change === 'number') {
184
- model.setSelectedCell(model.cells[change]);
185
- } else if (model && model.selectedCell) {
186
- const currentIndex = model.cells.indexOf(model.selectedCell);
187
- const shouldFocusEditor = this.contextKeyService.match('editorTextFocus');
188
-
189
- if (change === CellChangeDirection.Up && currentIndex > 0) {
190
- model.setSelectedCell(model.cells[currentIndex - 1]);
191
- if (model.selectedCell?.cellKind === CellKind.Code && shouldFocusEditor) {
192
- model.selectedCell.requestFocusEditor('lastLine');
193
- }
194
- } else if (change === CellChangeDirection.Down && currentIndex < model.cells.length - 1) {
195
- model.setSelectedCell(model.cells[currentIndex + 1]);
196
- if (model.selectedCell?.cellKind === CellKind.Code && shouldFocusEditor) {
197
- model.selectedCell.requestFocusEditor();
198
- }
199
- }
200
-
201
- if (model.selectedCell.cellKind === CellKind.Markup) {
202
- // since were losing focus from the cell editor, we need to focus the notebook editor again
203
- focusedEditor?.node.focus();
204
- }
205
- }
206
- }
207
- }
208
- );
209
-
210
- commands.registerCommand(NotebookCommands.CUT_SELECTED_CELL, this.editableCommandHandler(
211
- () => {
212
- const model = this.notebookEditorWidgetService.focusedEditor?.model;
213
- const selectedCell = model?.selectedCell;
214
- if (selectedCell) {
215
- model.applyEdits([{ editType: CellEditType.Replace, index: model.cells.indexOf(selectedCell), count: 1, cells: [] }], true);
216
- this.notebookClipboardService.copyCell(selectedCell);
217
- }
218
- }));
219
-
220
- commands.registerCommand(NotebookCommands.COPY_SELECTED_CELL, {
221
- execute: () => {
222
- const model = this.notebookEditorWidgetService.focusedEditor?.model;
223
- const selectedCell = model?.selectedCell;
224
- if (selectedCell) {
225
- this.notebookClipboardService.copyCell(selectedCell);
226
- }
227
- }
228
- });
229
-
230
- commands.registerCommand(NotebookCommands.PASTE_CELL, {
231
- isEnabled: () => !Boolean(this.notebookEditorWidgetService.focusedEditor?.model?.readOnly),
232
- isVisible: () => !Boolean(this.notebookEditorWidgetService.focusedEditor?.model?.readOnly),
233
- execute: (position?: 'above') => {
234
- const copiedCell = this.notebookClipboardService.getCell();
235
- if (copiedCell) {
236
- const model = this.notebookEditorWidgetService.focusedEditor?.model;
237
- const insertIndex = model?.selectedCell ? model.cells.indexOf(model.selectedCell) + (position === 'above' ? 0 : 1) : 0;
238
- model?.applyEdits([{ editType: CellEditType.Replace, index: insertIndex, count: 0, cells: [copiedCell] }], true);
239
- }
240
- }
241
- });
242
-
243
- commands.registerCommand(NotebookCommands.NOTEBOOK_FIND, {
244
- execute: () => {
245
- this.notebookEditorWidgetService.focusedEditor?.showFindWidget();
246
- }
247
- });
248
-
249
- }
250
-
251
- protected editableCommandHandler(execute: (notebookModel: NotebookModel) => void): CommandHandler {
252
- return {
253
- isEnabled: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
254
- isVisible: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
255
- execute: (uri: URI | NotebookModel) => {
256
- this.withModel(uri, execute, undefined);
257
- }
258
- };
259
- }
260
-
261
- protected withModel<T>(item: URI | NotebookModel, execute: (notebookModel: NotebookModel) => T, defaultValue: T): T {
262
- if (item instanceof URI) {
263
- const model = this.notebookService.getNotebookEditorModel(item);
264
- if (!model) {
265
- return defaultValue;
266
- }
267
- item = model;
268
- }
269
- return execute(item);
270
- }
271
-
272
- registerMenus(menus: MenuModelRegistry): void {
273
- // independent submenu for plugins to add commands
274
- menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR, 'Notebook Main Toolbar');
275
- // Add Notebook Cell items
276
- menus.registerSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, 'Add Notebook Cell', { role: CompoundMenuNodeRole.Group });
277
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, {
278
- commandId: NotebookCommands.ADD_NEW_CODE_CELL_COMMAND.id,
279
- label: nls.localizeByDefault('Code'),
280
- icon: codicon('add'),
281
- });
282
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP, {
283
- commandId: NotebookCommands.ADD_NEW_MARKDOWN_CELL_COMMAND.id,
284
- label: nls.localizeByDefault('Markdown'),
285
- icon: codicon('add'),
286
- });
287
-
288
- // Execution related items
289
- menus.registerSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, 'Cell Execution', { role: CompoundMenuNodeRole.Group });
290
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, {
291
- commandId: NotebookCommands.EXECUTE_NOTEBOOK_COMMAND.id,
292
- label: nls.localizeByDefault('Run All'),
293
- icon: codicon('run-all'),
294
- order: '10'
295
- });
296
- menus.registerMenuAction(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP, {
297
- commandId: NotebookCommands.CLEAR_ALL_OUTPUTS_COMMAND.id,
298
- label: nls.localizeByDefault('Clear All Outputs'),
299
- icon: codicon('clear-all'),
300
- order: '30',
301
- when: NOTEBOOK_HAS_OUTPUTS
302
- });
303
-
304
- menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU, '');
305
- }
306
-
307
- registerKeybindings(keybindings: KeybindingRegistry): void {
308
- keybindings.registerKeybindings(
309
- {
310
- command: NotebookCommands.CHANGE_SELECTED_CELL.id,
311
- keybinding: 'up',
312
- args: CellChangeDirection.Up,
313
- when: `(!editorTextFocus || ${NOTEBOOK_CELL_CURSOR_FIRST_LINE}) && !suggestWidgetVisible && ${NOTEBOOK_EDITOR_FOCUSED} && ${NOTEBOOK_CELL_FOCUSED}`
314
- },
315
- {
316
- command: NotebookCommands.CHANGE_SELECTED_CELL.id,
317
- keybinding: 'down',
318
- args: CellChangeDirection.Down,
319
- when: `(!editorTextFocus || ${NOTEBOOK_CELL_CURSOR_LAST_LINE}) && !suggestWidgetVisible && ${NOTEBOOK_EDITOR_FOCUSED} && ${NOTEBOOK_CELL_FOCUSED}`
320
- },
321
- {
322
- command: NotebookCommands.CUT_SELECTED_CELL.id,
323
- keybinding: 'ctrlcmd+x',
324
- when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
325
- },
326
- {
327
- command: NotebookCommands.COPY_SELECTED_CELL.id,
328
- keybinding: 'ctrlcmd+c',
329
- when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
330
- },
331
- {
332
- command: NotebookCommands.PASTE_CELL.id,
333
- keybinding: 'ctrlcmd+v',
334
- when: `${NOTEBOOK_EDITOR_FOCUSED} && !inputFocus`
335
- },
336
- {
337
- command: NotebookCommands.NOTEBOOK_FIND.id,
338
- keybinding: 'ctrlcmd+f',
339
- when: `${NOTEBOOK_EDITOR_FOCUSED}`
340
- },
341
- );
342
- }
343
-
344
- }
345
-
346
- export namespace NotebookMenus {
347
- export const NOTEBOOK_MAIN_TOOLBAR = 'notebook/toolbar';
348
- export const NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP = [NOTEBOOK_MAIN_TOOLBAR, 'cell-add-group'];
349
- export const NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP = [NOTEBOOK_MAIN_TOOLBAR, 'cell-execution-group'];
350
- export const NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU = 'notebook-main-toolbar-hidden-items-context-menu';
351
- }
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
+ }