فهرست منبع

Merge pull request #366 from eskirk/notebook_menuitem

Added a Notebook toolbar item
Brian E. Granger 8 سال پیش
والد
کامیت
96bdd7a6f9
1فایلهای تغییر یافته به همراه128 افزوده شده و 2 حذف شده
  1. 128 2
      src/notebook/plugin.ts

+ 128 - 2
src/notebook/plugin.ts

@@ -17,6 +17,14 @@ import {
   Widget
 } from 'phosphor-widget';
 
+import {
+  MenuItem, Menu
+} from 'phosphor-menus';
+
+import {
+  MainMenu
+} from '../mainmenu/plugin';
+
 import {
   DocumentRegistry, restartKernel, selectKernelForContext, IWidgetFactoryOptions
 } from '../docregistry';
@@ -96,6 +104,10 @@ const cmdIds = {
 export
 class NotebookTracker extends WidgetTracker<NotebookPanel> { }
 
+/**
+ * Used to access the application/command palette
+ */
+let currentApp : Application;
 
 /**
  * The notebook file handler extension.
@@ -103,7 +115,7 @@ class NotebookTracker extends WidgetTracker<NotebookPanel> { }
 export
 const notebookHandlerExtension = {
   id: 'jupyter.extensions.notebookHandler',
-  requires: [DocumentRegistry, ServiceManager, RenderMime, IClipboard],
+  requires: [DocumentRegistry, ServiceManager, RenderMime, IClipboard, MainMenu],
   activate: activateNotebookHandler
 };
 
@@ -124,7 +136,7 @@ const notebookTrackerProvider = {
 /**
  * Activate the notebook handler extension.
  */
-function activateNotebookHandler(app: Application, registry: DocumentRegistry, services: ServiceManager, rendermime: RenderMime<Widget>, clipboard: IClipboard): void {
+function activateNotebookHandler(app: Application, registry: DocumentRegistry, services: ServiceManager, rendermime: RenderMime<Widget>, clipboard: IClipboard, mainMenu: MainMenu): void {
 
   let widgetFactory = new NotebookWidgetFactory(rendermime, clipboard);
   let options: IWidgetFactoryOptions = {
@@ -171,6 +183,19 @@ function activateNotebookHandler(app: Application, registry: DocumentRegistry, s
     tracker.addWidget(widget);
   });
 
+  // Add a MainMenu notebook item
+  let notebookMenu = new MenuItem({
+    text: 'Notebook',
+    submenu: makeNbMenu()
+  });
+
+  let menuOptions = {
+    'rank': 1
+  };
+  mainMenu.addItem(notebookMenu, menuOptions);
+
+  currentApp = app;
+
   app.commands.add([
   {
     id: cmdIds.runAndAdvance,
@@ -707,7 +732,108 @@ function activateNotebookHandler(app: Application, registry: DocumentRegistry, s
   }
   ]);
 }
+/**
+ * Creates a menu item for the notebook
+ */
+function makeNbMenu() {
+  let settings = new Menu([
+    new MenuItem({
+      text: 'Turn off line numbers',
+      handler: lineNumberHandler
+    }),
+    new MenuItem({
+      text: 'Command mode',
+      handler: commandModeHandler
+    }),
+    new MenuItem({
+      text: 'Edit mode',
+      handler: editModeHandler
+    })
+  ]);
+
+  let menu = new Menu([
+    new MenuItem({
+      text: 'Settings',
+      submenu: settings
+    }),
+    new MenuItem({
+      type: MenuItem.Separator
+    }),
+    new MenuItem({
+      text: 'Undo',
+      handler: undoHandler
+    }),
+    new MenuItem({
+      text: 'Redo',
+      handler: redoHandler
+    }),
+    new MenuItem({
+      text: 'Split cell',
+      handler: splitCellHandler
+    }),
+    new MenuItem({
+      text: 'Delete cell',
+      handler: deleteCellHandler
+    }),
+    new MenuItem({
+      text: 'Clear all outputs',
+      handler: clearOutputHandler
+    }),
+    new MenuItem({
+      text: 'Run all cells',
+      handler: runAllHandler
+    }),
+    new MenuItem({
+      text: 'Switch kernel',
+      handler: changeKernelHandler
+    })
+  ]);
+
+  return menu;
+}
 
+/**
+ * Handler functions for the notebook MainMenu item 
+ */
+function clearOutputHandler() {
+  currentApp.commands.execute('notebook:clear-outputs');
+}
+
+function runAllHandler() {
+  currentApp.commands.execute('notebook:run-all');
+}
+
+function changeKernelHandler() {
+  currentApp.commands.execute('notebook:switch-kernel');
+}
+
+function lineNumberHandler() {
+  currentApp.commands.execute('notebook-cells:toggle-all-line-numbers');
+}
+
+function undoHandler() {
+  currentApp.commands.execute('notebook-cells:undo');
+}
+
+function redoHandler() {
+  currentApp.commands.execute('notebook-cells:redo');
+}
+
+function deleteCellHandler() {
+  currentApp.commands.execute('notebook-cells:delete');
+}
+
+function splitCellHandler() {
+  currentApp.commands.execute('notebook-cells:split');
+}
+
+function commandModeHandler() {
+  currentApp.commands.execute('notebook:command-mode');
+}
+
+function editModeHandler() {
+  currentApp.commands.execute('notebook:edit-mode');
+}
 
 /**
  * A namespace for private data.