From 93886dfb52987cebe3c8c8e4e1a8fab9f1ad5984 Mon Sep 17 00:00:00 2001 From: Mark Sujew Date: Wed, 10 Apr 2024 15:03:18 +0200 Subject: [PATCH] Use notebook URI as context for toolbar commands --- .../notebook-actions-contribution.ts | 21 ++++++++++++++----- .../browser/view/notebook-main-toolbar.tsx | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/notebook/src/browser/contributions/notebook-actions-contribution.ts b/packages/notebook/src/browser/contributions/notebook-actions-contribution.ts index c0fc2fb10f2d0..96a5f8ad625fc 100644 --- a/packages/notebook/src/browser/contributions/notebook-actions-contribution.ts +++ b/packages/notebook/src/browser/contributions/notebook-actions-contribution.ts @@ -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'; @@ -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(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'); diff --git a/packages/notebook/src/browser/view/notebook-main-toolbar.tsx b/packages/notebook/src/browser/view/notebook-main-toolbar.tsx index 56ee76d0b850c..78160e64ec19b 100644 --- a/packages/notebook/src/browser/view/notebook-main-toolbar.tsx +++ b/packages/notebook/src/browser/view/notebook-main-toolbar.tsx @@ -127,7 +127,7 @@ export class NotebookMainToolbar extends React.Component { 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); } }}>