Skip to content

Commit

Permalink
Use notebook URI as context for toolbar commands (#13585)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored Apr 10, 2024
1 parent 32f393a commit e99e42b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

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

protected editableCommandHandler(execute: (notebookModel: NotebookModel) => void): CommandHandler {
return {
isEnabled: (notebookModel: NotebookModel) => !Boolean(notebookModel?.readOnly),
isVisible: (notebookModel: NotebookModel) => !Boolean(notebookModel?.readOnly),
execute: (notebookModel: NotebookModel) => {
execute(notebookModel);
isEnabled: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
isVisible: (item: URI | NotebookModel) => this.withModel(item, model => !Boolean(model?.readOnly), false),
execute: (uri: URI | NotebookModel) => {
this.withModel(uri, execute, undefined);
}
};
}

protected withModel<T>(item: URI | NotebookModel, execute: (notebookModel: NotebookModel) => T, defaultValue: T): T {
if (item instanceof URI) {
const model = this.notebookService.getNotebookEditorModel(item);
if (!model) {
return defaultValue;
}
item = model;
}
return execute(item);
}

registerMenus(menus: MenuModelRegistry): void {
// independent submenu for plugins to add commands
menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR, 'Notebook Main Toolbar');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class NotebookMainToolbar extends React.Component<NotebookMainToolbarProp
return <div key={item.id} title={title} className={`theia-notebook-main-toolbar-item action-label${this.getAdditionalClasses(item)}`}
onClick={() => {
if (item.command && (!item.when || this.props.contextKeyService.match(item.when, this.props.editorNode))) {
this.props.commandRegistry.executeCommand(item.command, this.props.notebookModel);
this.props.commandRegistry.executeCommand(item.command, this.props.notebookModel.uri);
}
}}>
<span className={item.icon} />
Expand Down

0 comments on commit e99e42b

Please sign in to comment.