Browse Source

Refactor notebook tokens

Vidar Tonaas Fauske 6 years ago
parent
commit
4b19bde938

+ 1 - 0
packages/notebook/src/index.ts

@@ -14,3 +14,4 @@ export * from './widget';
 export * from './widgetfactory';
 export * from './modestatus';
 export * from './truststatus';
+export * from './tokens';

+ 3 - 22
packages/notebook/src/notebooktools.ts

@@ -3,7 +3,7 @@
 
 import { ArrayExt, each, chain } from '@phosphor/algorithm';
 
-import { JSONObject, JSONValue, Token } from '@phosphor/coreutils';
+import { JSONObject, JSONValue } from '@phosphor/coreutils';
 
 import { ConflatableMessage, Message, MessageLoop } from '@phosphor/messaging';
 
@@ -25,28 +25,9 @@ import { nbformat } from '@jupyterlab/coreutils';
 
 import { IObservableMap, ObservableJSON } from '@jupyterlab/observables';
 
-import { INotebookTracker } from './';
 import { NotebookPanel } from './panel';
 import { INotebookModel } from './model';
-
-/* tslint:disable */
-/**
- * The notebook tools token.
- */
-export const INotebookTools = new Token<INotebookTools>(
-  '@jupyterlab/notebook:INotebookTools'
-);
-/* tslint:enable */
-
-/**
- * The interface for notebook metadata tools.
- */
-export interface INotebookTools extends Widget {
-  activeNotebookPanel: NotebookPanel | null;
-  activeCell: Cell | null;
-  selectedCells: Cell[];
-  addItem(options: NotebookTools.IAddOptions): void;
-}
+import { INotebookTools, INotebookTracker } from './tokens';
 
 class RankedPanel<T extends Widget = Widget> extends Widget {
   constructor() {
@@ -321,7 +302,7 @@ export namespace NotebookTools {
   /**
    * The base notebook tool, meant to be subclassed.
    */
-  export class Tool extends Widget {
+  export class Tool extends Widget implements INotebookTools.ITool {
     /**
      * The notebook tools object.
      */

+ 98 - 0
packages/notebook/src/tokens.ts

@@ -0,0 +1,98 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import { Token } from '@phosphor/coreutils';
+import { ISignal } from '@phosphor/signaling';
+import { Widget } from '@phosphor/widgets';
+
+import { IInstanceTracker } from '@jupyterlab/apputils';
+import { Cell } from '@jupyterlab/cells';
+
+import { NotebookPanel } from './panel';
+import { NotebookTools } from './notebooktools';
+
+/* tslint:disable */
+/**
+ * The notebook tools token.
+ */
+export const INotebookTools = new Token<INotebookTools>(
+  '@jupyterlab/notebook:INotebookTools'
+);
+/* tslint:enable */
+
+/**
+ * The interface for notebook metadata tools.
+ */
+export interface INotebookTools extends Widget {
+  activeNotebookPanel: NotebookPanel | null;
+  activeCell: Cell | null;
+  selectedCells: Cell[];
+  addItem(options: NotebookTools.IAddOptions): void;
+}
+
+/**
+ * The namespace for NotebookTools class statics.
+ */
+export namespace INotebookTools {
+  /**
+   * The options used to add an item to the notebook tools.
+   */
+  export interface IAddOptions {
+    /**
+     * The tool to add to the notebook tools area.
+     */
+    tool: ITool;
+
+    /**
+     * The section to which the tool should be added.
+     */
+    section?: 'common' | 'advanced';
+
+    /**
+     * The rank order of the widget among its siblings.
+     */
+    rank?: number;
+  }
+
+  export interface ITool extends Widget {
+    /**
+     * The notebook tools object.
+     */
+    notebookTools: INotebookTools;
+  }
+}
+
+/* tslint:disable */
+/**
+ * The notebook tracker token.
+ */
+export const INotebookTracker = new Token<INotebookTracker>(
+  '@jupyterlab/notebook:INotebookTracker'
+);
+/* tslint:enable */
+
+/**
+ * An object that tracks notebook widgets.
+ */
+export interface INotebookTracker extends IInstanceTracker<NotebookPanel> {
+  /**
+   * The currently focused cell.
+   *
+   * #### Notes
+   * If there is no cell with the focus, then this value is `null`.
+   */
+  readonly activeCell: Cell;
+
+  /**
+   * A signal emitted when the current active cell changes.
+   *
+   * #### Notes
+   * If there is no cell with the focus, then `null` will be emitted.
+   */
+  readonly activeCellChanged: ISignal<this, Cell>;
+
+  /**
+   * A signal emitted when the selection state changes.
+   */
+  readonly selectionChanged: ISignal<this, void>;
+}

+ 2 - 37
packages/notebook/src/tracker.ts

@@ -1,50 +1,15 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import { IInstanceTracker, InstanceTracker } from '@jupyterlab/apputils';
+import { InstanceTracker } from '@jupyterlab/apputils';
 import { Cell } from '@jupyterlab/cells';
 
-import { Token } from '@phosphor/coreutils';
 import { ISignal, Signal } from '@phosphor/signaling';
 
+import { INotebookTracker } from './tokens';
 import { NotebookPanel } from './panel';
 import { Notebook } from './widget';
 
-/**
- * An object that tracks notebook widgets.
- */
-export interface INotebookTracker extends IInstanceTracker<NotebookPanel> {
-  /**
-   * The currently focused cell.
-   *
-   * #### Notes
-   * If there is no cell with the focus, then this value is `null`.
-   */
-  readonly activeCell: Cell;
-
-  /**
-   * A signal emitted when the current active cell changes.
-   *
-   * #### Notes
-   * If there is no cell with the focus, then `null` will be emitted.
-   */
-  readonly activeCellChanged: ISignal<this, Cell>;
-
-  /**
-   * A signal emitted when the selection state changes.
-   */
-  readonly selectionChanged: ISignal<this, void>;
-}
-
-/* tslint:disable */
-/**
- * The notebook tracker token.
- */
-export const INotebookTracker = new Token<INotebookTracker>(
-  '@jupyterlab/notebook:INotebookTracker'
-);
-/* tslint:enable */
-
 export class NotebookTracker extends InstanceTracker<NotebookPanel>
   implements INotebookTracker {
   /**