|
14 | 14 | // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
15 | 15 | // *****************************************************************************
|
16 | 16 |
|
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'; |
18 | 18 | import { inject, injectable } from '@theia/core/shared/inversify';
|
19 | 19 | import { ApplicationShell, codicon, CommonCommands, KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser';
|
20 | 20 | import { NotebookModel } from '../view-model/notebook-model';
|
@@ -184,14 +184,25 @@ export class NotebookActionsContribution implements CommandContribution, MenuCon
|
184 | 184 |
|
185 | 185 | protected editableCommandHandler(execute: (notebookModel: NotebookModel) => void): CommandHandler {
|
186 | 186 | 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); |
191 | 191 | }
|
192 | 192 | };
|
193 | 193 | }
|
194 | 194 |
|
| 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 | + |
195 | 206 | registerMenus(menus: MenuModelRegistry): void {
|
196 | 207 | // independent submenu for plugins to add commands
|
197 | 208 | menus.registerIndependentSubmenu(NotebookMenus.NOTEBOOK_MAIN_TOOLBAR, 'Notebook Main Toolbar');
|
|
0 commit comments