Skip to content

Commit e99e42b

Browse files
authored
Use notebook URI as context for toolbar commands (#13585)
1 parent 32f393a commit e99e42b

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

packages/notebook/src/browser/contributions/notebook-actions-contribution.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
1515
// *****************************************************************************
1616

17-
import { Command, CommandContribution, CommandHandler, CommandRegistry, CompoundMenuNodeRole, MenuContribution, MenuModelRegistry, nls } from '@theia/core';
17+
import { Command, CommandContribution, CommandHandler, CommandRegistry, CompoundMenuNodeRole, MenuContribution, MenuModelRegistry, nls, URI } from '@theia/core';
1818
import { inject, injectable } from '@theia/core/shared/inversify';
1919
import { ApplicationShell, codicon, CommonCommands, KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser';
2020
import { NotebookModel } from '../view-model/notebook-model';
@@ -184,14 +184,25 @@ export class NotebookActionsContribution implements CommandContribution, MenuCon
184184

185185
protected editableCommandHandler(execute: (notebookModel: NotebookModel) => void): CommandHandler {
186186
return {
187-
isEnabled: (notebookModel: NotebookModel) => !Boolean(notebookModel?.readOnly),
188-
isVisible: (notebookModel: NotebookModel) => !Boolean(notebookModel?.readOnly),
189-
execute: (notebookModel: NotebookModel) => {
190-
execute(notebookModel);
187+
isEnabled: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
188+
isVisible: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
189+
execute: (uri: URI | NotebookModel) => {
190+
this.withModel(uri, execute, undefined);
191191
}
192192
};
193193
}
194194

195+
protected withModel<T>(item: URI | NotebookModel, execute: (notebookModel: NotebookModel) => T, defaultValue: T): T {
196+
if (item instanceof URI) {
197+
const model = this.notebookService.getNotebookEditorModel(item);
198+
if (!model) {
199+
return defaultValue;
200+
}
201+
item = model;
202+
}
203+
return execute(item);
204+
}
205+
195206
registerMenus(menus: MenuModelRegistry): void {
196207
// independent submenu for plugins to add commands
197208
menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR, 'Notebook Main Toolbar');

packages/notebook/src/browser/view/notebook-main-toolbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class NotebookMainToolbar extends React.Component<NotebookMainToolbarProp
127127
return <div key={item.id} title={title} className={`theia-notebook-main-toolbar-item action-label${this.getAdditionalClasses(item)}`}
128128
onClick={() => {
129129
if (item.command && (!item.when || this.props.contextKeyService.match(item.when, this.props.editorNode))) {
130-
this.props.commandRegistry.executeCommand(item.command, this.props.notebookModel);
130+
this.props.commandRegistry.executeCommand(item.command, this.props.notebookModel.uri);
131131
}
132132
}}>
133133
<span className={item.icon} />

0 commit comments

Comments
 (0)