@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.
- package/README.md +30 -30
- package/lib/browser/contributions/cell-operations.d.ts.map +1 -1
- package/lib/browser/contributions/cell-operations.js +8 -1
- package/lib/browser/contributions/cell-operations.js.map +1 -1
- package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
- package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
- package/lib/browser/contributions/notebook-actions-contribution.js +31 -6
- package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
- package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +4 -0
- package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
- package/lib/browser/contributions/notebook-cell-actions-contribution.js +49 -12
- package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
- package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
- package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
- package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
- package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
- package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
- package/lib/browser/notebook-editor-widget.js +3 -5
- package/lib/browser/notebook-editor-widget.js.map +1 -1
- package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
- package/lib/browser/notebook-frontend-module.js +3 -0
- package/lib/browser/notebook-frontend-module.js.map +1 -1
- package/lib/browser/notebook-open-handler.d.ts +3 -2
- package/lib/browser/notebook-open-handler.d.ts.map +1 -1
- package/lib/browser/notebook-open-handler.js +12 -5
- package/lib/browser/notebook-open-handler.js.map +1 -1
- package/lib/browser/service/notebook-options.d.ts +1 -0
- package/lib/browser/service/notebook-options.d.ts.map +1 -1
- package/lib/browser/service/notebook-options.js +1 -0
- package/lib/browser/service/notebook-options.js.map +1 -1
- package/lib/browser/service/notebook-service.d.ts +1 -0
- package/lib/browser/service/notebook-service.d.ts.map +1 -1
- package/lib/browser/service/notebook-service.js +7 -0
- package/lib/browser/service/notebook-service.js.map +1 -1
- package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
- package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
- package/lib/browser/view/notebook-cell-editor.js +30 -16
- package/lib/browser/view/notebook-cell-editor.js.map +1 -1
- package/lib/browser/view/notebook-cell-list-view.d.ts +7 -4
- package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
- package/lib/browser/view/notebook-cell-list-view.js +39 -29
- package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
- package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
- package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
- package/lib/browser/view-model/notebook-cell-model.js +5 -0
- package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
- package/package.json +8 -8
- package/src/browser/contributions/cell-operations.ts +44 -39
- package/src/browser/contributions/notebook-actions-contribution.ts +379 -351
- package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -485
- package/src/browser/contributions/notebook-color-contribution.ts +268 -268
- package/src/browser/contributions/notebook-context-keys.ts +113 -113
- package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
- package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
- package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
- package/src/browser/contributions/notebook-preferences.ts +92 -92
- package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
- package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
- package/src/browser/index.ts +27 -27
- package/src/browser/notebook-cell-resource-resolver.ts +130 -130
- package/src/browser/notebook-editor-widget-factory.ts +82 -82
- package/src/browser/notebook-editor-widget.tsx +330 -331
- package/src/browser/notebook-frontend-module.ts +119 -115
- package/src/browser/notebook-open-handler.ts +120 -114
- package/src/browser/notebook-output-utils.ts +119 -119
- package/src/browser/notebook-renderer-registry.ts +85 -85
- package/src/browser/notebook-type-registry.ts +54 -54
- package/src/browser/notebook-types.ts +186 -186
- package/src/browser/renderers/cell-output-webview.ts +33 -33
- package/src/browser/service/notebook-clipboard-service.ts +43 -43
- package/src/browser/service/notebook-context-manager.ts +162 -162
- package/src/browser/service/notebook-editor-widget-service.ts +101 -101
- package/src/browser/service/notebook-execution-service.ts +139 -139
- package/src/browser/service/notebook-execution-state-service.ts +311 -311
- package/src/browser/service/notebook-kernel-history-service.ts +124 -124
- package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
- package/src/browser/service/notebook-kernel-service.ts +357 -357
- package/src/browser/service/notebook-model-resolver-service.ts +160 -160
- package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
- package/src/browser/service/notebook-options.ts +155 -154
- package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
- package/src/browser/service/notebook-service.ts +215 -209
- package/src/browser/style/index.css +483 -467
- package/src/browser/view/notebook-cell-editor.tsx +263 -247
- package/src/browser/view/notebook-cell-list-view.tsx +279 -259
- package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
- package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
- package/src/browser/view/notebook-code-cell-view.tsx +350 -350
- package/src/browser/view/notebook-find-widget.tsx +335 -335
- package/src/browser/view/notebook-main-toolbar.tsx +235 -235
- package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
- package/src/browser/view/notebook-viewport-service.ts +61 -61
- package/src/browser/view-model/notebook-cell-model.ts +473 -466
- package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
- package/src/browser/view-model/notebook-model.ts +550 -550
- package/src/common/index.ts +18 -18
- package/src/common/notebook-common.ts +337 -337
- package/src/common/notebook-protocol.ts +35 -35
- package/src/common/notebook-range.ts +30 -30
|
@@ -1,235 +1,235 @@
|
|
|
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 { ArrayUtils, CommandRegistry, CompoundMenuNodeRole, DisposableCollection, MenuModelRegistry, MenuNode, nls } from '@theia/core';
|
|
17
|
-
import * as React from '@theia/core/shared/react';
|
|
18
|
-
import { codicon, ContextMenuRenderer } from '@theia/core/lib/browser';
|
|
19
|
-
import { NotebookCommands, NotebookMenus } from '../contributions/notebook-actions-contribution';
|
|
20
|
-
import { NotebookModel } from '../view-model/notebook-model';
|
|
21
|
-
import { NotebookKernelService } from '../service/notebook-kernel-service';
|
|
22
|
-
import { inject, injectable } from '@theia/core/shared/inversify';
|
|
23
|
-
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
|
|
24
|
-
import { NotebookCommand } from '../../common';
|
|
25
|
-
import { NotebookContextManager } from '../service/notebook-context-manager';
|
|
26
|
-
|
|
27
|
-
export interface NotebookMainToolbarProps {
|
|
28
|
-
notebookModel: NotebookModel
|
|
29
|
-
menuRegistry: MenuModelRegistry;
|
|
30
|
-
notebookKernelService: NotebookKernelService;
|
|
31
|
-
commandRegistry: CommandRegistry;
|
|
32
|
-
contextKeyService: ContextKeyService;
|
|
33
|
-
editorNode: HTMLElement;
|
|
34
|
-
notebookContextManager: NotebookContextManager;
|
|
35
|
-
contextMenuRenderer: ContextMenuRenderer;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
@injectable()
|
|
39
|
-
export class NotebookMainToolbarRenderer {
|
|
40
|
-
@inject(NotebookKernelService) protected readonly notebookKernelService: NotebookKernelService;
|
|
41
|
-
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry;
|
|
42
|
-
@inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry;
|
|
43
|
-
@inject(ContextKeyService) protected readonly contextKeyService: ContextKeyService;
|
|
44
|
-
@inject(NotebookContextManager) protected readonly notebookContextManager: NotebookContextManager;
|
|
45
|
-
@inject(ContextMenuRenderer) protected readonly contextMenuRenderer: ContextMenuRenderer;
|
|
46
|
-
|
|
47
|
-
render(notebookModel: NotebookModel, editorNode: HTMLElement): React.ReactNode {
|
|
48
|
-
return <NotebookMainToolbar notebookModel={notebookModel}
|
|
49
|
-
menuRegistry={this.menuRegistry}
|
|
50
|
-
notebookKernelService={this.notebookKernelService}
|
|
51
|
-
commandRegistry={this.commandRegistry}
|
|
52
|
-
contextKeyService={this.contextKeyService}
|
|
53
|
-
editorNode={editorNode}
|
|
54
|
-
notebookContextManager={this.notebookContextManager}
|
|
55
|
-
contextMenuRenderer={this.contextMenuRenderer} />;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
interface NotebookMainToolbarState {
|
|
60
|
-
selectedKernelLabel?: string;
|
|
61
|
-
numberOfHiddenItems: number;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export class NotebookMainToolbar extends React.Component<NotebookMainToolbarProps, NotebookMainToolbarState> {
|
|
65
|
-
|
|
66
|
-
// The minimum area between items and kernel select before hiding items in a context menu
|
|
67
|
-
static readonly MIN_FREE_AREA = 10;
|
|
68
|
-
|
|
69
|
-
protected toDispose = new DisposableCollection();
|
|
70
|
-
|
|
71
|
-
protected nativeSubmenus = [
|
|
72
|
-
NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP[NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP.length - 1],
|
|
73
|
-
NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP[NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP.length - 1]];
|
|
74
|
-
|
|
75
|
-
protected gapElement: HTMLDivElement | undefined;
|
|
76
|
-
protected lastGapElementWidth: number = 0;
|
|
77
|
-
|
|
78
|
-
protected resizeObserver: ResizeObserver = new ResizeObserver(() => this.calculateItemsToHide());
|
|
79
|
-
|
|
80
|
-
constructor(props: NotebookMainToolbarProps) {
|
|
81
|
-
super(props);
|
|
82
|
-
|
|
83
|
-
this.state = {
|
|
84
|
-
selectedKernelLabel: props.notebookKernelService.getSelectedOrSuggestedKernel(props.notebookModel)?.label,
|
|
85
|
-
numberOfHiddenItems: 0,
|
|
86
|
-
};
|
|
87
|
-
this.toDispose.push(props.notebookKernelService.onDidChangeSelectedKernel(event => {
|
|
88
|
-
if (props.notebookModel.uri.isEqual(event.notebook)) {
|
|
89
|
-
this.setState({ selectedKernelLabel: props.notebookKernelService.getKernel(event.newKernel ?? '')?.label });
|
|
90
|
-
}
|
|
91
|
-
}));
|
|
92
|
-
// in case the selected kernel is added after the notebook is loaded
|
|
93
|
-
this.toDispose.push(props.notebookKernelService.onDidAddKernel(() => {
|
|
94
|
-
if (!this.state.selectedKernelLabel) {
|
|
95
|
-
this.setState({ selectedKernelLabel: props.notebookKernelService.getSelectedOrSuggestedKernel(props.notebookModel)?.label });
|
|
96
|
-
}
|
|
97
|
-
}));
|
|
98
|
-
|
|
99
|
-
// TODO maybe we need a mechanism to check for changes in the menu to update this toolbar
|
|
100
|
-
const contextKeys = new Set<string>();
|
|
101
|
-
this.getAllContextKeys(this.getMenuItems(), contextKeys);
|
|
102
|
-
props.notebookContextManager.onDidChangeContext(e => {
|
|
103
|
-
if (e.affects(contextKeys)) {
|
|
104
|
-
this.forceUpdate();
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
props.contextKeyService.onDidChange(e => {
|
|
108
|
-
if (e.affects(contextKeys)) {
|
|
109
|
-
this.forceUpdate();
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
override componentWillUnmount(): void {
|
|
116
|
-
this.toDispose.dispose();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
override componentDidUpdate(): void {
|
|
120
|
-
this.calculateItemsToHide();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
override componentDidMount(): void {
|
|
124
|
-
this.calculateItemsToHide();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
protected calculateItemsToHide(): void {
|
|
128
|
-
const numberOfMenuItems = this.getMenuItems().length;
|
|
129
|
-
if (this.gapElement && this.gapElement.getBoundingClientRect().width < NotebookMainToolbar.MIN_FREE_AREA && this.state.numberOfHiddenItems < numberOfMenuItems) {
|
|
130
|
-
this.setState({ ...this.state, numberOfHiddenItems: this.state.numberOfHiddenItems + 1 });
|
|
131
|
-
this.lastGapElementWidth = this.gapElement.getBoundingClientRect().width;
|
|
132
|
-
} else if (this.gapElement && this.gapElement.getBoundingClientRect().width > this.lastGapElementWidth && this.state.numberOfHiddenItems > 0) {
|
|
133
|
-
this.setState({ ...this.state, numberOfHiddenItems: 0 });
|
|
134
|
-
this.lastGapElementWidth = this.gapElement.getBoundingClientRect().width;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
protected renderContextMenu(event: MouseEvent, menuItems: readonly MenuNode[]): void {
|
|
139
|
-
const hiddenItems = menuItems.slice(menuItems.length - this.calculateNumberOfHiddenItems(menuItems));
|
|
140
|
-
const contextMenu = this.props.menuRegistry.getMenu([NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU]);
|
|
141
|
-
|
|
142
|
-
contextMenu.children.map(item => item.id).forEach(id => contextMenu.removeNode(id));
|
|
143
|
-
hiddenItems.forEach(item => contextMenu.addNode(item));
|
|
144
|
-
|
|
145
|
-
this.props.contextMenuRenderer.render({
|
|
146
|
-
anchor: event,
|
|
147
|
-
menuPath: [NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU],
|
|
148
|
-
context: this.props.editorNode,
|
|
149
|
-
args: [this.props.notebookModel.uri]
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
override render(): React.ReactNode {
|
|
154
|
-
const menuItems = this.getMenuItems();
|
|
155
|
-
return <div className='theia-notebook-main-toolbar'>
|
|
156
|
-
{menuItems.slice(0, menuItems.length - this.calculateNumberOfHiddenItems(menuItems)).map(item => this.renderMenuItem(item))}
|
|
157
|
-
{
|
|
158
|
-
this.state.numberOfHiddenItems > 0 &&
|
|
159
|
-
<span className={`${codicon('ellipsis')} action-label theia-notebook-main-toolbar-item`} onClick={e => this.renderContextMenu(e.nativeEvent, menuItems)} />
|
|
160
|
-
}
|
|
161
|
-
<div ref={element => this.gapElementChanged(element)} style={{ flexGrow: 1 }}></div>
|
|
162
|
-
<div className='theia-notebook-main-toolbar-item action-label'
|
|
163
|
-
onClick={() => this.props.commandRegistry.executeCommand(NotebookCommands.SELECT_KERNEL_COMMAND.id, this.props.notebookModel)}>
|
|
164
|
-
<span className={codicon('server-environment')} />
|
|
165
|
-
<span className=' theia-notebook-main-toolbar-item-text'>
|
|
166
|
-
{this.state.selectedKernelLabel ?? nls.localizeByDefault('Select Kernel')}
|
|
167
|
-
</span>
|
|
168
|
-
</div>
|
|
169
|
-
</div >;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
protected gapElementChanged(element: HTMLDivElement | null): void {
|
|
173
|
-
if (this.gapElement) {
|
|
174
|
-
this.resizeObserver.unobserve(this.gapElement);
|
|
175
|
-
}
|
|
176
|
-
this.gapElement = element ?? undefined;
|
|
177
|
-
if (this.gapElement) {
|
|
178
|
-
this.lastGapElementWidth = this.gapElement.getBoundingClientRect().width;
|
|
179
|
-
this.resizeObserver.observe(this.gapElement);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
protected renderMenuItem(item: MenuNode, submenu?: string): React.ReactNode {
|
|
184
|
-
if (item.role === CompoundMenuNodeRole.Group) {
|
|
185
|
-
const itemNodes = ArrayUtils.coalesce(item.children?.map(child => this.renderMenuItem(child, item.id)) ?? []);
|
|
186
|
-
return <React.Fragment key={item.id}>
|
|
187
|
-
{itemNodes}
|
|
188
|
-
{itemNodes && itemNodes.length > 0 && <span key={`${item.id}-separator`} className='theia-notebook-toolbar-separator'></span>}
|
|
189
|
-
</React.Fragment>;
|
|
190
|
-
} else if ((this.nativeSubmenus.includes(submenu ?? '')) || !item.when || this.props.contextKeyService.match(item.when, this.props.editorNode)) {
|
|
191
|
-
const visibleCommand = Boolean(this.props.commandRegistry.getVisibleHandler(item.command ?? '', this.props.notebookModel));
|
|
192
|
-
if (!visibleCommand) {
|
|
193
|
-
return undefined;
|
|
194
|
-
}
|
|
195
|
-
const command = this.props.commandRegistry.getCommand(item.command ?? '') as NotebookCommand | undefined;
|
|
196
|
-
const label = command?.shortTitle ?? item.label;
|
|
197
|
-
const title = command?.tooltip ?? item.label;
|
|
198
|
-
return <div key={item.id} title={title} className={`theia-notebook-main-toolbar-item action-label${this.getAdditionalClasses(item)}`}
|
|
199
|
-
onClick={() => {
|
|
200
|
-
if (item.command && (!item.when || this.props.contextKeyService.match(item.when, this.props.editorNode))) {
|
|
201
|
-
this.props.commandRegistry.executeCommand(item.command, this.props.notebookModel.uri);
|
|
202
|
-
}
|
|
203
|
-
}}>
|
|
204
|
-
<span className={item.icon} />
|
|
205
|
-
<span className='theia-notebook-main-toolbar-item-text'>{label}</span>
|
|
206
|
-
</div>;
|
|
207
|
-
}
|
|
208
|
-
return undefined;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
protected getMenuItems(): readonly MenuNode[] {
|
|
212
|
-
const menuPath = NotebookMenus.NOTEBOOK_MAIN_TOOLBAR;
|
|
213
|
-
const pluginCommands = this.props.menuRegistry.getMenuNode(menuPath).children;
|
|
214
|
-
const theiaCommands = this.props.menuRegistry.getMenu([menuPath]).children;
|
|
215
|
-
return theiaCommands.concat(pluginCommands);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
protected getAdditionalClasses(item: MenuNode): string {
|
|
219
|
-
return !item.when || this.props.contextKeyService.match(item.when, this.props.editorNode) ? '' : ' theia-mod-disabled';
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
protected getAllContextKeys(menus: readonly MenuNode[], keySet: Set<string>): void {
|
|
223
|
-
menus.filter(item => item.when)
|
|
224
|
-
.forEach(item => this.props.contextKeyService.parseKeys(item.when!)?.forEach(key => keySet.add(key)));
|
|
225
|
-
|
|
226
|
-
menus.filter(item => item.children && item.children.length > 0)
|
|
227
|
-
.forEach(item => this.getAllContextKeys(item.children!, keySet));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
protected calculateNumberOfHiddenItems(allMenuItems: readonly MenuNode[]): number {
|
|
231
|
-
return this.state.numberOfHiddenItems >= allMenuItems.length ?
|
|
232
|
-
allMenuItems.length :
|
|
233
|
-
this.state.numberOfHiddenItems % allMenuItems.length;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
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 { ArrayUtils, CommandRegistry, CompoundMenuNodeRole, DisposableCollection, MenuModelRegistry, MenuNode, nls } from '@theia/core';
|
|
17
|
+
import * as React from '@theia/core/shared/react';
|
|
18
|
+
import { codicon, ContextMenuRenderer } from '@theia/core/lib/browser';
|
|
19
|
+
import { NotebookCommands, NotebookMenus } from '../contributions/notebook-actions-contribution';
|
|
20
|
+
import { NotebookModel } from '../view-model/notebook-model';
|
|
21
|
+
import { NotebookKernelService } from '../service/notebook-kernel-service';
|
|
22
|
+
import { inject, injectable } from '@theia/core/shared/inversify';
|
|
23
|
+
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
|
|
24
|
+
import { NotebookCommand } from '../../common';
|
|
25
|
+
import { NotebookContextManager } from '../service/notebook-context-manager';
|
|
26
|
+
|
|
27
|
+
export interface NotebookMainToolbarProps {
|
|
28
|
+
notebookModel: NotebookModel
|
|
29
|
+
menuRegistry: MenuModelRegistry;
|
|
30
|
+
notebookKernelService: NotebookKernelService;
|
|
31
|
+
commandRegistry: CommandRegistry;
|
|
32
|
+
contextKeyService: ContextKeyService;
|
|
33
|
+
editorNode: HTMLElement;
|
|
34
|
+
notebookContextManager: NotebookContextManager;
|
|
35
|
+
contextMenuRenderer: ContextMenuRenderer;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@injectable()
|
|
39
|
+
export class NotebookMainToolbarRenderer {
|
|
40
|
+
@inject(NotebookKernelService) protected readonly notebookKernelService: NotebookKernelService;
|
|
41
|
+
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry;
|
|
42
|
+
@inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry;
|
|
43
|
+
@inject(ContextKeyService) protected readonly contextKeyService: ContextKeyService;
|
|
44
|
+
@inject(NotebookContextManager) protected readonly notebookContextManager: NotebookContextManager;
|
|
45
|
+
@inject(ContextMenuRenderer) protected readonly contextMenuRenderer: ContextMenuRenderer;
|
|
46
|
+
|
|
47
|
+
render(notebookModel: NotebookModel, editorNode: HTMLElement): React.ReactNode {
|
|
48
|
+
return <NotebookMainToolbar notebookModel={notebookModel}
|
|
49
|
+
menuRegistry={this.menuRegistry}
|
|
50
|
+
notebookKernelService={this.notebookKernelService}
|
|
51
|
+
commandRegistry={this.commandRegistry}
|
|
52
|
+
contextKeyService={this.contextKeyService}
|
|
53
|
+
editorNode={editorNode}
|
|
54
|
+
notebookContextManager={this.notebookContextManager}
|
|
55
|
+
contextMenuRenderer={this.contextMenuRenderer} />;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface NotebookMainToolbarState {
|
|
60
|
+
selectedKernelLabel?: string;
|
|
61
|
+
numberOfHiddenItems: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class NotebookMainToolbar extends React.Component<NotebookMainToolbarProps, NotebookMainToolbarState> {
|
|
65
|
+
|
|
66
|
+
// The minimum area between items and kernel select before hiding items in a context menu
|
|
67
|
+
static readonly MIN_FREE_AREA = 10;
|
|
68
|
+
|
|
69
|
+
protected toDispose = new DisposableCollection();
|
|
70
|
+
|
|
71
|
+
protected nativeSubmenus = [
|
|
72
|
+
NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP[NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_CELL_ADD_GROUP.length - 1],
|
|
73
|
+
NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP[NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_EXECUTION_GROUP.length - 1]];
|
|
74
|
+
|
|
75
|
+
protected gapElement: HTMLDivElement | undefined;
|
|
76
|
+
protected lastGapElementWidth: number = 0;
|
|
77
|
+
|
|
78
|
+
protected resizeObserver: ResizeObserver = new ResizeObserver(() => this.calculateItemsToHide());
|
|
79
|
+
|
|
80
|
+
constructor(props: NotebookMainToolbarProps) {
|
|
81
|
+
super(props);
|
|
82
|
+
|
|
83
|
+
this.state = {
|
|
84
|
+
selectedKernelLabel: props.notebookKernelService.getSelectedOrSuggestedKernel(props.notebookModel)?.label,
|
|
85
|
+
numberOfHiddenItems: 0,
|
|
86
|
+
};
|
|
87
|
+
this.toDispose.push(props.notebookKernelService.onDidChangeSelectedKernel(event => {
|
|
88
|
+
if (props.notebookModel.uri.isEqual(event.notebook)) {
|
|
89
|
+
this.setState({ selectedKernelLabel: props.notebookKernelService.getKernel(event.newKernel ?? '')?.label });
|
|
90
|
+
}
|
|
91
|
+
}));
|
|
92
|
+
// in case the selected kernel is added after the notebook is loaded
|
|
93
|
+
this.toDispose.push(props.notebookKernelService.onDidAddKernel(() => {
|
|
94
|
+
if (!this.state.selectedKernelLabel) {
|
|
95
|
+
this.setState({ selectedKernelLabel: props.notebookKernelService.getSelectedOrSuggestedKernel(props.notebookModel)?.label });
|
|
96
|
+
}
|
|
97
|
+
}));
|
|
98
|
+
|
|
99
|
+
// TODO maybe we need a mechanism to check for changes in the menu to update this toolbar
|
|
100
|
+
const contextKeys = new Set<string>();
|
|
101
|
+
this.getAllContextKeys(this.getMenuItems(), contextKeys);
|
|
102
|
+
props.notebookContextManager.onDidChangeContext(e => {
|
|
103
|
+
if (e.affects(contextKeys)) {
|
|
104
|
+
this.forceUpdate();
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
props.contextKeyService.onDidChange(e => {
|
|
108
|
+
if (e.affects(contextKeys)) {
|
|
109
|
+
this.forceUpdate();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
override componentWillUnmount(): void {
|
|
116
|
+
this.toDispose.dispose();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
override componentDidUpdate(): void {
|
|
120
|
+
this.calculateItemsToHide();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
override componentDidMount(): void {
|
|
124
|
+
this.calculateItemsToHide();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
protected calculateItemsToHide(): void {
|
|
128
|
+
const numberOfMenuItems = this.getMenuItems().length;
|
|
129
|
+
if (this.gapElement && this.gapElement.getBoundingClientRect().width < NotebookMainToolbar.MIN_FREE_AREA && this.state.numberOfHiddenItems < numberOfMenuItems) {
|
|
130
|
+
this.setState({ ...this.state, numberOfHiddenItems: this.state.numberOfHiddenItems + 1 });
|
|
131
|
+
this.lastGapElementWidth = this.gapElement.getBoundingClientRect().width;
|
|
132
|
+
} else if (this.gapElement && this.gapElement.getBoundingClientRect().width > this.lastGapElementWidth && this.state.numberOfHiddenItems > 0) {
|
|
133
|
+
this.setState({ ...this.state, numberOfHiddenItems: 0 });
|
|
134
|
+
this.lastGapElementWidth = this.gapElement.getBoundingClientRect().width;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
protected renderContextMenu(event: MouseEvent, menuItems: readonly MenuNode[]): void {
|
|
139
|
+
const hiddenItems = menuItems.slice(menuItems.length - this.calculateNumberOfHiddenItems(menuItems));
|
|
140
|
+
const contextMenu = this.props.menuRegistry.getMenu([NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU]);
|
|
141
|
+
|
|
142
|
+
contextMenu.children.map(item => item.id).forEach(id => contextMenu.removeNode(id));
|
|
143
|
+
hiddenItems.forEach(item => contextMenu.addNode(item));
|
|
144
|
+
|
|
145
|
+
this.props.contextMenuRenderer.render({
|
|
146
|
+
anchor: event,
|
|
147
|
+
menuPath: [NotebookMenus.NOTEBOOK_MAIN_TOOLBAR_HIDDEN_ITEMS_CONTEXT_MENU],
|
|
148
|
+
context: this.props.editorNode,
|
|
149
|
+
args: [this.props.notebookModel.uri]
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
override render(): React.ReactNode {
|
|
154
|
+
const menuItems = this.getMenuItems();
|
|
155
|
+
return <div className='theia-notebook-main-toolbar'>
|
|
156
|
+
{menuItems.slice(0, menuItems.length - this.calculateNumberOfHiddenItems(menuItems)).map(item => this.renderMenuItem(item))}
|
|
157
|
+
{
|
|
158
|
+
this.state.numberOfHiddenItems > 0 &&
|
|
159
|
+
<span className={`${codicon('ellipsis')} action-label theia-notebook-main-toolbar-item`} onClick={e => this.renderContextMenu(e.nativeEvent, menuItems)} />
|
|
160
|
+
}
|
|
161
|
+
<div ref={element => this.gapElementChanged(element)} style={{ flexGrow: 1 }}></div>
|
|
162
|
+
<div className='theia-notebook-main-toolbar-item action-label'
|
|
163
|
+
onClick={() => this.props.commandRegistry.executeCommand(NotebookCommands.SELECT_KERNEL_COMMAND.id, this.props.notebookModel)}>
|
|
164
|
+
<span className={codicon('server-environment')} />
|
|
165
|
+
<span className=' theia-notebook-main-toolbar-item-text'>
|
|
166
|
+
{this.state.selectedKernelLabel ?? nls.localizeByDefault('Select Kernel')}
|
|
167
|
+
</span>
|
|
168
|
+
</div>
|
|
169
|
+
</div >;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
protected gapElementChanged(element: HTMLDivElement | null): void {
|
|
173
|
+
if (this.gapElement) {
|
|
174
|
+
this.resizeObserver.unobserve(this.gapElement);
|
|
175
|
+
}
|
|
176
|
+
this.gapElement = element ?? undefined;
|
|
177
|
+
if (this.gapElement) {
|
|
178
|
+
this.lastGapElementWidth = this.gapElement.getBoundingClientRect().width;
|
|
179
|
+
this.resizeObserver.observe(this.gapElement);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
protected renderMenuItem(item: MenuNode, submenu?: string): React.ReactNode {
|
|
184
|
+
if (item.role === CompoundMenuNodeRole.Group) {
|
|
185
|
+
const itemNodes = ArrayUtils.coalesce(item.children?.map(child => this.renderMenuItem(child, item.id)) ?? []);
|
|
186
|
+
return <React.Fragment key={item.id}>
|
|
187
|
+
{itemNodes}
|
|
188
|
+
{itemNodes && itemNodes.length > 0 && <span key={`${item.id}-separator`} className='theia-notebook-toolbar-separator'></span>}
|
|
189
|
+
</React.Fragment>;
|
|
190
|
+
} else if ((this.nativeSubmenus.includes(submenu ?? '')) || !item.when || this.props.contextKeyService.match(item.when, this.props.editorNode)) {
|
|
191
|
+
const visibleCommand = Boolean(this.props.commandRegistry.getVisibleHandler(item.command ?? '', this.props.notebookModel));
|
|
192
|
+
if (!visibleCommand) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
const command = this.props.commandRegistry.getCommand(item.command ?? '') as NotebookCommand | undefined;
|
|
196
|
+
const label = command?.shortTitle ?? item.label;
|
|
197
|
+
const title = command?.tooltip ?? item.label;
|
|
198
|
+
return <div key={item.id} title={title} className={`theia-notebook-main-toolbar-item action-label${this.getAdditionalClasses(item)}`}
|
|
199
|
+
onClick={() => {
|
|
200
|
+
if (item.command && (!item.when || this.props.contextKeyService.match(item.when, this.props.editorNode))) {
|
|
201
|
+
this.props.commandRegistry.executeCommand(item.command, this.props.notebookModel.uri);
|
|
202
|
+
}
|
|
203
|
+
}}>
|
|
204
|
+
<span className={item.icon} />
|
|
205
|
+
<span className='theia-notebook-main-toolbar-item-text'>{label}</span>
|
|
206
|
+
</div>;
|
|
207
|
+
}
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
protected getMenuItems(): readonly MenuNode[] {
|
|
212
|
+
const menuPath = NotebookMenus.NOTEBOOK_MAIN_TOOLBAR;
|
|
213
|
+
const pluginCommands = this.props.menuRegistry.getMenuNode(menuPath).children;
|
|
214
|
+
const theiaCommands = this.props.menuRegistry.getMenu([menuPath]).children;
|
|
215
|
+
return theiaCommands.concat(pluginCommands);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
protected getAdditionalClasses(item: MenuNode): string {
|
|
219
|
+
return !item.when || this.props.contextKeyService.match(item.when, this.props.editorNode) ? '' : ' theia-mod-disabled';
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
protected getAllContextKeys(menus: readonly MenuNode[], keySet: Set<string>): void {
|
|
223
|
+
menus.filter(item => item.when)
|
|
224
|
+
.forEach(item => this.props.contextKeyService.parseKeys(item.when!)?.forEach(key => keySet.add(key)));
|
|
225
|
+
|
|
226
|
+
menus.filter(item => item.children && item.children.length > 0)
|
|
227
|
+
.forEach(item => this.getAllContextKeys(item.children!, keySet));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
protected calculateNumberOfHiddenItems(allMenuItems: readonly MenuNode[]): number {
|
|
231
|
+
return this.state.numberOfHiddenItems >= allMenuItems.length ?
|
|
232
|
+
allMenuItems.length :
|
|
233
|
+
this.state.numberOfHiddenItems % allMenuItems.length;
|
|
234
|
+
}
|
|
235
|
+
}
|