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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/index.d.ts +1 -0
  3. package/lib/browser/index.d.ts.map +1 -1
  4. package/lib/browser/index.js +1 -0
  5. package/lib/browser/index.js.map +1 -1
  6. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  7. package/lib/browser/notebook-frontend-module.js +2 -0
  8. package/lib/browser/notebook-frontend-module.js.map +1 -1
  9. package/lib/browser/service/notebook-cell-editor-service.d.ts +16 -0
  10. package/lib/browser/service/notebook-cell-editor-service.d.ts.map +1 -0
  11. package/lib/browser/service/notebook-cell-editor-service.js +53 -0
  12. package/lib/browser/service/notebook-cell-editor-service.js.map +1 -0
  13. package/lib/browser/view/notebook-cell-editor.d.ts +2 -0
  14. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  15. package/lib/browser/view/notebook-cell-editor.js +12 -1
  16. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  17. package/lib/browser/view/notebook-code-cell-view.d.ts +2 -0
  18. package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -1
  19. package/lib/browser/view/notebook-code-cell-view.js +6 -1
  20. package/lib/browser/view/notebook-code-cell-view.js.map +1 -1
  21. package/lib/browser/view/notebook-markdown-cell-view.d.ts +2 -0
  22. package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -1
  23. package/lib/browser/view/notebook-markdown-cell-view.js +8 -3
  24. package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -1
  25. package/package.json +7 -7
  26. package/src/browser/contributions/cell-operations.ts +44 -44
  27. package/src/browser/contributions/notebook-actions-contribution.ts +379 -379
  28. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -525
  29. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  30. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  31. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  32. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  33. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  34. package/src/browser/contributions/notebook-preferences.ts +92 -92
  35. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -77
  36. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  37. package/src/browser/index.ts +28 -27
  38. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  39. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  40. package/src/browser/notebook-editor-widget.tsx +330 -330
  41. package/src/browser/notebook-frontend-module.ts +121 -119
  42. package/src/browser/notebook-open-handler.ts +120 -120
  43. package/src/browser/notebook-output-utils.ts +119 -119
  44. package/src/browser/notebook-renderer-registry.ts +85 -85
  45. package/src/browser/notebook-type-registry.ts +54 -54
  46. package/src/browser/notebook-types.ts +186 -186
  47. package/src/browser/renderers/cell-output-webview.ts +33 -33
  48. package/src/browser/service/notebook-cell-editor-service.ts +56 -0
  49. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  50. package/src/browser/service/notebook-context-manager.ts +162 -162
  51. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  52. package/src/browser/service/notebook-execution-service.ts +139 -139
  53. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  54. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  55. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  56. package/src/browser/service/notebook-kernel-service.ts +357 -357
  57. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  58. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  59. package/src/browser/service/notebook-options.ts +155 -155
  60. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  61. package/src/browser/service/notebook-service.ts +215 -215
  62. package/src/browser/style/index.css +484 -483
  63. package/src/browser/view/notebook-cell-editor.tsx +276 -263
  64. package/src/browser/view/notebook-cell-list-view.tsx +279 -279
  65. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  66. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  67. package/src/browser/view/notebook-code-cell-view.tsx +355 -350
  68. package/src/browser/view/notebook-find-widget.tsx +335 -335
  69. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  70. package/src/browser/view/notebook-markdown-cell-view.tsx +215 -208
  71. package/src/browser/view/notebook-viewport-service.ts +61 -61
  72. package/src/browser/view-model/notebook-cell-model.ts +473 -473
  73. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  74. package/src/browser/view-model/notebook-model.ts +550 -550
  75. package/src/common/index.ts +18 -18
  76. package/src/common/notebook-common.ts +337 -337
  77. package/src/common/notebook-protocol.ts +35 -35
  78. package/src/common/notebook-range.ts +30 -30
@@ -1,279 +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, 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
- }
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
+ }