@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,259 +1,279 @@
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
- import * as React from '@theia/core/shared/react';
17
- import { CellEditType, CellKind, NotebookCellsChangeType } from '../../common';
18
- import { NotebookCellModel } from '../view-model/notebook-cell-model';
19
- import { NotebookModel } from '../view-model/notebook-model';
20
- import { NotebookCellToolbarFactory } from './notebook-cell-toolbar-factory';
21
- import { animationFrame, codicon, onDomEvent } from '@theia/core/lib/browser';
22
- import { CommandRegistry, DisposableCollection, nls } from '@theia/core';
23
- import { NotebookCommands } from '../contributions/notebook-actions-contribution';
24
- import { NotebookCellActionContribution } from '../contributions/notebook-cell-actions-contribution';
25
- import { NotebookContextManager } from '../service/notebook-context-manager';
26
-
27
- export interface CellRenderer {
28
- render(notebookData: NotebookModel, cell: NotebookCellModel, index: number): React.ReactNode
29
- renderDragImage(cell: NotebookCellModel): HTMLElement
30
- }
31
-
32
- interface CellListProps {
33
- renderers: Map<CellKind, CellRenderer>;
34
- notebookModel: NotebookModel;
35
- notebookContext: NotebookContextManager;
36
- toolbarRenderer: NotebookCellToolbarFactory;
37
- commandRegistry: CommandRegistry
38
- }
39
-
40
- interface NotebookCellListState {
41
- selectedCell?: NotebookCellModel;
42
- scrollIntoView: boolean;
43
- dragOverIndicator: { cell: NotebookCellModel, position: 'top' | 'bottom' } | undefined;
44
- }
45
-
46
- export class NotebookCellListView extends React.Component<CellListProps, NotebookCellListState> {
47
-
48
- protected toDispose = new DisposableCollection();
49
-
50
- protected static dragGhost: HTMLElement | undefined;
51
-
52
- constructor(props: CellListProps) {
53
- super(props);
54
- this.state = { selectedCell: props.notebookModel.selectedCell, dragOverIndicator: undefined, scrollIntoView: true };
55
- this.toDispose.push(props.notebookModel.onDidAddOrRemoveCell(e => {
56
- if (e.newCellIds && e.newCellIds.length > 0) {
57
- this.setState({
58
- ...this.state,
59
- selectedCell: this.props.notebookModel.cells.find(model => model.handle === e.newCellIds![e.newCellIds!.length - 1]),
60
- scrollIntoView: true
61
- });
62
- } else {
63
- this.setState({
64
- ...this.state,
65
- selectedCell: this.props.notebookModel.cells.find(cell => cell === this.state.selectedCell),
66
- scrollIntoView: false
67
- });
68
- }
69
- }));
70
-
71
- this.toDispose.push(props.notebookModel.onDidChangeContent(events => {
72
- if (events.some(e => e.kind === NotebookCellsChangeType.Move)) {
73
- // When a cell has been moved, we need to rerender the whole component
74
- this.forceUpdate();
75
- }
76
- }));
77
-
78
- this.toDispose.push(props.notebookModel.onDidChangeSelectedCell(e => {
79
- this.setState({
80
- ...this.state,
81
- selectedCell: e.cell,
82
- scrollIntoView: e.scrollIntoView
83
- });
84
- }));
85
- }
86
-
87
- override componentWillUnmount(): void {
88
- this.toDispose.dispose();
89
- }
90
-
91
- override render(): React.ReactNode {
92
- return <ul className='theia-notebook-cell-list' ref={
93
- ref => {
94
- this.toDispose.push(onDomEvent(document, 'focusin', () => {
95
- animationFrame().then(() => {
96
- let hasCellFocus = false;
97
- let hasFocus = false;
98
- if (ref?.contains(document.activeElement)) {
99
- if (this.props.notebookModel.selectedCell) {
100
- hasCellFocus = true;
101
- }
102
- hasFocus = true;
103
- }
104
- this.props.notebookContext.changeCellFocus(hasCellFocus);
105
- this.props.notebookContext.changeCellListFocus(hasFocus);
106
- });
107
- }));
108
- }
109
- }>
110
- {this.props.notebookModel.cells
111
- .map((cell, index) =>
112
- <React.Fragment key={'cell-' + cell.handle}>
113
- <NotebookCellDivider
114
- isVisible={() => this.isEnabled()}
115
- onAddNewCell={(kind: CellKind) => this.onAddNewCell(kind, index)}
116
- onDrop={e => this.onDrop(e, index)}
117
- onDragOver={e => this.onDragOver(e, cell, 'top')} />
118
- {this.shouldRenderDragOverIndicator(cell, 'top') && <CellDropIndicator />}
119
- <li className={'theia-notebook-cell' + (this.state.selectedCell === cell ? ' focused' : '') + (this.isEnabled() ? ' draggable' : '')}
120
- onClick={e => {
121
- this.setState({ ...this.state, selectedCell: cell });
122
- this.props.notebookModel.setSelectedCell(cell, false);
123
- }}
124
- onDragStart={e => this.onDragStart(e, index, cell)}
125
- onDragEnd={e => {
126
- NotebookCellListView.dragGhost?.remove();
127
- this.setState({ ...this.state, dragOverIndicator: undefined });
128
- }}
129
- onDragOver={e => this.onDragOver(e, cell)}
130
- onDrop={e => this.onDrop(e, index)}
131
- draggable={true}
132
- ref={ref => cell === this.state.selectedCell && this.state.scrollIntoView && ref?.scrollIntoView({ block: 'nearest' })}>
133
- <div className={'theia-notebook-cell-marker' + (this.state.selectedCell === cell ? ' theia-notebook-cell-marker-selected' : '')}></div>
134
- <div className='theia-notebook-cell-content'>
135
- {this.renderCellContent(cell, index)}
136
- </div>
137
- {this.state.selectedCell === cell &&
138
- this.props.toolbarRenderer.renderCellToolbar(NotebookCellActionContribution.ACTION_MENU, cell, {
139
- contextMenuArgs: () => [cell], commandArgs: () => [this.props.notebookModel]
140
- })
141
- }
142
- </li>
143
- {this.shouldRenderDragOverIndicator(cell, 'bottom') && <CellDropIndicator />}
144
- </React.Fragment>
145
- )
146
- }
147
- <NotebookCellDivider
148
- isVisible={() => this.isEnabled()}
149
- onAddNewCell={(kind: CellKind) => this.onAddNewCell(kind, this.props.notebookModel.cells.length)}
150
- onDrop={e => this.onDrop(e, this.props.notebookModel.cells.length - 1)}
151
- onDragOver={e => this.onDragOver(e, this.props.notebookModel.cells[this.props.notebookModel.cells.length - 1], 'bottom')} />
152
- </ul>;
153
- }
154
-
155
- renderCellContent(cell: NotebookCellModel, index: number): React.ReactNode {
156
- const renderer = this.props.renderers.get(cell.cellKind);
157
- if (!renderer) {
158
- throw new Error(`No renderer found for cell type ${cell.cellKind}`);
159
- }
160
- return renderer.render(this.props.notebookModel, cell, index);
161
- }
162
-
163
- protected onDragStart(event: React.DragEvent<HTMLLIElement>, index: number, cell: NotebookCellModel): void {
164
- event.stopPropagation();
165
- if (!this.isEnabled()) {
166
- event.preventDefault();
167
- return;
168
- }
169
-
170
- NotebookCellListView.dragGhost = document.createElement('div');
171
- NotebookCellListView.dragGhost.classList.add('theia-notebook-drag-ghost-image');
172
- NotebookCellListView.dragGhost.appendChild(this.props.renderers.get(cell.cellKind)?.renderDragImage(cell) ?? document.createElement('div'));
173
- document.body.appendChild(NotebookCellListView.dragGhost);
174
- event.dataTransfer.setDragImage(NotebookCellListView.dragGhost, -10, 0);
175
-
176
- event.dataTransfer.setData('text/theia-notebook-cell-index', index.toString());
177
- event.dataTransfer.setData('text/plain', this.props.notebookModel.cells[index].source);
178
- }
179
-
180
- protected onDragOver(event: React.DragEvent<HTMLLIElement>, cell: NotebookCellModel, position?: 'top' | 'bottom'): void {
181
- if (!this.isEnabled()) {
182
- return;
183
- }
184
- event.preventDefault();
185
- event.stopPropagation();
186
- // show indicator
187
- this.setState({ ...this.state, dragOverIndicator: { cell, position: position ?? event.nativeEvent.offsetY < event.currentTarget.clientHeight / 2 ? 'top' : 'bottom' } });
188
- }
189
-
190
- protected isEnabled(): boolean {
191
- return !Boolean(this.props.notebookModel.readOnly);
192
- }
193
-
194
- protected onDrop(event: React.DragEvent<HTMLLIElement>, dropElementIndex: number): void {
195
- if (!this.isEnabled()) {
196
- this.setState({ dragOverIndicator: undefined });
197
- return;
198
- }
199
- const index = parseInt(event.dataTransfer.getData('text/theia-notebook-cell-index'));
200
- const isTargetBelow = index < dropElementIndex;
201
- let newIdx = this.state.dragOverIndicator?.position === 'top' ? dropElementIndex : dropElementIndex + 1;
202
- newIdx = isTargetBelow ? newIdx - 1 : newIdx;
203
- if (index !== undefined && index !== dropElementIndex) {
204
- this.props.notebookModel.applyEdits([{
205
- editType: CellEditType.Move,
206
- length: 1,
207
- index,
208
- newIdx
209
- }], true);
210
- }
211
- this.setState({ ...this.state, dragOverIndicator: undefined });
212
- }
213
-
214
- protected onAddNewCell(kind: CellKind, index: number): void {
215
- if (this.isEnabled()) {
216
- this.props.commandRegistry.executeCommand(NotebookCommands.ADD_NEW_CELL_COMMAND.id,
217
- this.props.notebookModel,
218
- kind,
219
- index
220
- );
221
- }
222
- }
223
-
224
- protected shouldRenderDragOverIndicator(cell: NotebookCellModel, position: 'top' | 'bottom'): boolean {
225
- return this.isEnabled() &&
226
- this.state.dragOverIndicator !== undefined &&
227
- this.state.dragOverIndicator.cell === cell &&
228
- this.state.dragOverIndicator.position === position;
229
- }
230
-
231
- }
232
-
233
- export interface NotebookCellDividerProps {
234
- isVisible: () => boolean;
235
- onAddNewCell: (type: CellKind) => void;
236
- onDrop: (event: React.DragEvent<HTMLLIElement>) => void;
237
- onDragOver: (event: React.DragEvent<HTMLLIElement>) => void;
238
- }
239
-
240
- export function NotebookCellDivider({ isVisible, onAddNewCell, onDrop, onDragOver }: NotebookCellDividerProps): React.JSX.Element {
241
- const [hover, setHover] = React.useState(false);
242
-
243
- return <li className='theia-notebook-cell-divider' onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)} onDrop={onDrop} onDragOver={onDragOver}>
244
- {hover && isVisible() && <div className='theia-notebook-add-cell-buttons'>
245
- <button className='theia-notebook-add-cell-button' onClick={() => onAddNewCell(CellKind.Code)} title={nls.localizeByDefault('Add Code Cell')}>
246
- <div className={codicon('add') + ' theia-notebook-add-cell-button-icon'} />
247
- {nls.localizeByDefault('Code')}
248
- </button>
249
- <button className='theia-notebook-add-cell-button' onClick={() => onAddNewCell(CellKind.Markup)} title={nls.localizeByDefault('Add Markdown Cell')}>
250
- <div className={codicon('add') + ' theia-notebook-add-cell-button-icon'} />
251
- {nls.localizeByDefault('Markdown')}
252
- </button>
253
- </div>}
254
- </li>;
255
- }
256
-
257
- function CellDropIndicator(): React.JSX.Element {
258
- return <div className='theia-notebook-cell-drop-indicator' />;
259
- }
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
+ import * as React from '@theia/core/shared/react';
17
+ import { CellEditType, CellKind, NotebookCellsChangeType } from '../../common';
18
+ import { NotebookCellModel } from '../view-model/notebook-cell-model';
19
+ import { NotebookModel } from '../view-model/notebook-model';
20
+ import { NotebookCellToolbarFactory } from './notebook-cell-toolbar-factory';
21
+ import { animationFrame, onDomEvent } from '@theia/core/lib/browser';
22
+ import { CommandRegistry, DisposableCollection, MenuModelRegistry, MenuNode, nls } from '@theia/core';
23
+ import { NotebookCommands, NotebookMenus } from '../contributions/notebook-actions-contribution';
24
+ import { NotebookCellActionContribution } from '../contributions/notebook-cell-actions-contribution';
25
+ import { NotebookContextManager } from '../service/notebook-context-manager';
26
+
27
+ export interface CellRenderer {
28
+ render(notebookData: NotebookModel, cell: NotebookCellModel, index: number): React.ReactNode
29
+ renderDragImage(cell: NotebookCellModel): HTMLElement
30
+ }
31
+
32
+ interface CellListProps {
33
+ renderers: Map<CellKind, CellRenderer>;
34
+ notebookModel: NotebookModel;
35
+ notebookContext: NotebookContextManager;
36
+ toolbarRenderer: NotebookCellToolbarFactory;
37
+ commandRegistry: CommandRegistry;
38
+ menuRegistry: MenuModelRegistry;
39
+ }
40
+
41
+ interface NotebookCellListState {
42
+ selectedCell?: NotebookCellModel;
43
+ scrollIntoView: boolean;
44
+ dragOverIndicator: { cell: NotebookCellModel, position: 'top' | 'bottom' } | undefined;
45
+ }
46
+
47
+ export class NotebookCellListView extends React.Component<CellListProps, NotebookCellListState> {
48
+
49
+ protected toDispose = new DisposableCollection();
50
+
51
+ protected static dragGhost: HTMLElement | undefined;
52
+ protected cellListRef: React.RefObject<HTMLUListElement> = React.createRef();
53
+
54
+ constructor(props: CellListProps) {
55
+ super(props);
56
+ this.state = { selectedCell: props.notebookModel.selectedCell, dragOverIndicator: undefined, scrollIntoView: true };
57
+ this.toDispose.push(props.notebookModel.onDidAddOrRemoveCell(e => {
58
+ if (e.newCellIds && e.newCellIds.length > 0) {
59
+ this.setState({
60
+ ...this.state,
61
+ selectedCell: this.props.notebookModel.cells.find(model => model.handle === e.newCellIds![e.newCellIds!.length - 1]),
62
+ scrollIntoView: true
63
+ });
64
+ } else {
65
+ this.setState({
66
+ ...this.state,
67
+ selectedCell: this.props.notebookModel.cells.find(cell => cell === this.state.selectedCell),
68
+ scrollIntoView: false
69
+ });
70
+ }
71
+ }));
72
+
73
+ this.toDispose.push(props.notebookModel.onDidChangeContent(events => {
74
+ if (events.some(e => e.kind === NotebookCellsChangeType.Move)) {
75
+ // When a cell has been moved, we need to rerender the whole component
76
+ this.forceUpdate();
77
+ }
78
+ }));
79
+
80
+ this.toDispose.push(props.notebookModel.onDidChangeSelectedCell(e => {
81
+ this.setState({
82
+ ...this.state,
83
+ selectedCell: e.cell,
84
+ scrollIntoView: e.scrollIntoView
85
+ });
86
+ }));
87
+
88
+ this.toDispose.push(onDomEvent(document, 'focusin', () => {
89
+ animationFrame().then(() => {
90
+ if (!this.cellListRef.current) {
91
+ return;
92
+ }
93
+ let hasCellFocus = false;
94
+ let hasFocus = false;
95
+ if (this.cellListRef.current.contains(document.activeElement)) {
96
+ if (this.props.notebookModel.selectedCell) {
97
+ hasCellFocus = true;
98
+ }
99
+ hasFocus = true;
100
+ }
101
+ this.props.notebookContext.changeCellFocus(hasCellFocus);
102
+ this.props.notebookContext.changeCellListFocus(hasFocus);
103
+ });
104
+ }));
105
+ }
106
+
107
+ override componentWillUnmount(): void {
108
+ this.toDispose.dispose();
109
+ }
110
+
111
+ override render(): React.ReactNode {
112
+ return <ul className='theia-notebook-cell-list' ref={this.cellListRef}>
113
+ {this.props.notebookModel.cells
114
+ .map((cell, index) =>
115
+ <React.Fragment key={'cell-' + cell.handle}>
116
+ <NotebookCellDivider
117
+ menuRegistry={this.props.menuRegistry}
118
+ isVisible={() => this.isEnabled()}
119
+ onAddNewCell={(commandId: string) => this.onAddNewCell(commandId, index)}
120
+ onDrop={e => this.onDrop(e, index)}
121
+ onDragOver={e => this.onDragOver(e, cell, 'top')} />
122
+ {this.shouldRenderDragOverIndicator(cell, 'top') && <CellDropIndicator />}
123
+ <li className={'theia-notebook-cell' + (this.state.selectedCell === cell ? ' focused' : '') + (this.isEnabled() ? ' draggable' : '')}
124
+ onClick={e => {
125
+ this.setState({ ...this.state, selectedCell: cell });
126
+ this.props.notebookModel.setSelectedCell(cell, false);
127
+ }}
128
+ onDragStart={e => this.onDragStart(e, index, cell)}
129
+ onDragEnd={e => {
130
+ NotebookCellListView.dragGhost?.remove();
131
+ this.setState({ ...this.state, dragOverIndicator: undefined });
132
+ }}
133
+ onDragOver={e => this.onDragOver(e, cell)}
134
+ onDrop={e => this.onDrop(e, index)}
135
+ draggable={true}
136
+ tabIndex={-1}
137
+ ref={ref => {
138
+ if (ref && cell === this.state.selectedCell && this.state.scrollIntoView) {
139
+ ref.scrollIntoView({ block: 'nearest' });
140
+ if (cell.cellKind === CellKind.Markup && !cell.editing) {
141
+ ref.focus();
142
+ }
143
+ }
144
+ }}>
145
+ <div className={'theia-notebook-cell-marker' + (this.state.selectedCell === cell ? ' theia-notebook-cell-marker-selected' : '')}></div>
146
+ <div className='theia-notebook-cell-content'>
147
+ {this.renderCellContent(cell, index)}
148
+ </div>
149
+ {this.state.selectedCell === cell &&
150
+ this.props.toolbarRenderer.renderCellToolbar(NotebookCellActionContribution.ACTION_MENU, cell, {
151
+ contextMenuArgs: () => [cell], commandArgs: () => [this.props.notebookModel]
152
+ })
153
+ }
154
+ </li>
155
+ {this.shouldRenderDragOverIndicator(cell, 'bottom') && <CellDropIndicator />}
156
+ </React.Fragment>
157
+ )
158
+ }
159
+ <NotebookCellDivider
160
+ menuRegistry={this.props.menuRegistry}
161
+ isVisible={() => this.isEnabled()}
162
+ onAddNewCell={(commandId: string) => this.onAddNewCell(commandId, this.props.notebookModel.cells.length)}
163
+ onDrop={e => this.onDrop(e, this.props.notebookModel.cells.length - 1)}
164
+ onDragOver={e => this.onDragOver(e, this.props.notebookModel.cells[this.props.notebookModel.cells.length - 1], 'bottom')} />
165
+ </ul>;
166
+ }
167
+
168
+ renderCellContent(cell: NotebookCellModel, index: number): React.ReactNode {
169
+ const renderer = this.props.renderers.get(cell.cellKind);
170
+ if (!renderer) {
171
+ throw new Error(`No renderer found for cell type ${cell.cellKind}`);
172
+ }
173
+ return renderer.render(this.props.notebookModel, cell, index);
174
+ }
175
+
176
+ protected onDragStart(event: React.DragEvent<HTMLLIElement>, index: number, cell: NotebookCellModel): void {
177
+ event.stopPropagation();
178
+ if (!this.isEnabled()) {
179
+ event.preventDefault();
180
+ return;
181
+ }
182
+
183
+ NotebookCellListView.dragGhost = document.createElement('div');
184
+ NotebookCellListView.dragGhost.classList.add('theia-notebook-drag-ghost-image');
185
+ NotebookCellListView.dragGhost.appendChild(this.props.renderers.get(cell.cellKind)?.renderDragImage(cell) ?? document.createElement('div'));
186
+ document.body.appendChild(NotebookCellListView.dragGhost);
187
+ event.dataTransfer.setDragImage(NotebookCellListView.dragGhost, -10, 0);
188
+
189
+ event.dataTransfer.setData('text/theia-notebook-cell-index', index.toString());
190
+ event.dataTransfer.setData('text/plain', this.props.notebookModel.cells[index].source);
191
+ }
192
+
193
+ protected onDragOver(event: React.DragEvent<HTMLLIElement>, cell: NotebookCellModel, position?: 'top' | 'bottom'): void {
194
+ if (!this.isEnabled()) {
195
+ return;
196
+ }
197
+ event.preventDefault();
198
+ event.stopPropagation();
199
+ // show indicator
200
+ this.setState({ ...this.state, dragOverIndicator: { cell, position: position ?? event.nativeEvent.offsetY < event.currentTarget.clientHeight / 2 ? 'top' : 'bottom' } });
201
+ }
202
+
203
+ protected isEnabled(): boolean {
204
+ return !Boolean(this.props.notebookModel.readOnly);
205
+ }
206
+
207
+ protected onDrop(event: React.DragEvent<HTMLLIElement>, dropElementIndex: number): void {
208
+ if (!this.isEnabled()) {
209
+ this.setState({ dragOverIndicator: undefined });
210
+ return;
211
+ }
212
+ const index = parseInt(event.dataTransfer.getData('text/theia-notebook-cell-index'));
213
+ const isTargetBelow = index < dropElementIndex;
214
+ let newIdx = this.state.dragOverIndicator?.position === 'top' ? dropElementIndex : dropElementIndex + 1;
215
+ newIdx = isTargetBelow ? newIdx - 1 : newIdx;
216
+ if (index !== undefined && index !== dropElementIndex) {
217
+ this.props.notebookModel.applyEdits([{
218
+ editType: CellEditType.Move,
219
+ length: 1,
220
+ index,
221
+ newIdx
222
+ }], true);
223
+ }
224
+ this.setState({ ...this.state, dragOverIndicator: undefined });
225
+ }
226
+
227
+ protected onAddNewCell(commandId: string, index: number): void {
228
+ if (this.isEnabled()) {
229
+ this.props.commandRegistry.executeCommand(NotebookCommands.CHANGE_SELECTED_CELL.id, index - 1);
230
+ this.props.commandRegistry.executeCommand(commandId,
231
+ this.props.notebookModel,
232
+ index
233
+ );
234
+ }
235
+ }
236
+
237
+ protected shouldRenderDragOverIndicator(cell: NotebookCellModel, position: 'top' | 'bottom'): boolean {
238
+ return this.isEnabled() &&
239
+ this.state.dragOverIndicator !== undefined &&
240
+ this.state.dragOverIndicator.cell === cell &&
241
+ this.state.dragOverIndicator.position === position;
242
+ }
243
+
244
+ }
245
+
246
+ export interface NotebookCellDividerProps {
247
+ isVisible: () => boolean;
248
+ onAddNewCell: (commandId: string) => void;
249
+ onDrop: (event: React.DragEvent<HTMLLIElement>) => void;
250
+ onDragOver: (event: React.DragEvent<HTMLLIElement>) => void;
251
+ menuRegistry: MenuModelRegistry;
252
+ }
253
+
254
+ export function NotebookCellDivider({ isVisible, onAddNewCell, onDrop, onDragOver, menuRegistry }: NotebookCellDividerProps): React.JSX.Element {
255
+ const [hover, setHover] = React.useState(false);
256
+
257
+ const menuPath = NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP;
258
+ const menuItems = menuRegistry.getMenuNode(menuPath).children;
259
+
260
+ const renderItem = (item: MenuNode): React.ReactNode => <button
261
+ key={item.id}
262
+ className='theia-notebook-add-cell-button'
263
+ onClick={() => onAddNewCell(item.command || '')}
264
+ title={nls.localizeByDefault(`Add ${item.label} Cell`)}
265
+ >
266
+ <div className={item.icon + ' theia-notebook-add-cell-button-icon'} />
267
+ <div className='theia-notebook-add-cell-button-text'>{item.label}</div>
268
+ </button>;
269
+
270
+ return <li className='theia-notebook-cell-divider' onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)} onDrop={onDrop} onDragOver={onDragOver}>
271
+ {hover && isVisible() && <div className='theia-notebook-add-cell-buttons'>
272
+ {menuItems.map((item: MenuNode) => renderItem(item))}
273
+ </div>}
274
+ </li>;
275
+ }
276
+
277
+ function CellDropIndicator(): React.JSX.Element {
278
+ return <div className='theia-notebook-cell-drop-indicator' />;
279
+ }