Bläddra i källkod

Add printing for images, JSON and inspector

Saul Shanabrook 6 år sedan
förälder
incheckning
ecf0a10d22

+ 9 - 0
packages/apputils/src/mainareawidget.ts

@@ -11,6 +11,8 @@ import { Toolbar } from './toolbar';
 
 import { DOMUtils } from './domutils';
 
+import { printSymbol, deferPrinting } from './printing';
+
 /**
  * A widget meant to be contained in the JupyterLab main area.
  *
@@ -42,6 +44,8 @@ export class MainAreaWidget<T extends Widget = Widget> extends Widget {
     layout.addWidget(toolbar);
     layout.addWidget(content);
 
+    deferPrinting(this, content);
+
     if (!content.id) {
       content.id = DOMUtils.createDomID();
     }
@@ -102,6 +106,11 @@ export class MainAreaWidget<T extends Widget = Widget> extends Widget {
     }
   }
 
+  /**
+   * Print method. Defered to content.
+   */
+  [printSymbol]: () => void;
+
   /**
    * The content hosted by the widget.
    */

+ 24 - 5
packages/apputils/src/printing.ts

@@ -1,4 +1,4 @@
-import { Printd } from 'printd';
+import { Printd, PrintdCallback } from 'printd';
 import { Widget } from '@phosphor/widgets';
 
 /**
@@ -17,8 +17,13 @@ export interface IPrintable {
 /**
  * Returns whether an object implements a print method.
  */
-export function isPrintable(a: object): a is IPrintable {
-  return printSymbol in a;
+export function isPrintable(a: any): a is IPrintable {
+  try {
+    return printSymbol in a;
+  } catch {
+    // `in` raises a type error on non objects.
+    return false;
+  }
 }
 
 /**
@@ -28,6 +33,16 @@ export function print(a: IPrintable) {
   a[printSymbol]();
 }
 
+/**
+ * Sets the print method on the parent to that of the child, if it
+ * exists on the child.
+ */
+export function deferPrinting(parent: IPrintable, child: object) {
+  if (isPrintable(child)) {
+    parent[printSymbol] = child[printSymbol].bind(child);
+  }
+}
+
 /**
  * Global print instance
  */
@@ -38,6 +53,10 @@ const _PRINTD = new Printd();
  * use the `printd` library to print the node, by
  * creating an iframe and copying the DOM into it.
  */
-export function printd(this: Widget) {
-  _PRINTD.print(this.node);
+export function printd(
+  this: Widget,
+  cssText?: string,
+  callback?: PrintdCallback
+) {
+  _PRINTD.print(this.node, cssText, callback);
 }

+ 1 - 1
packages/docmanager-extension/src/index.ts

@@ -789,7 +789,7 @@ function addLabCommands(
     label: 'Print...',
     isEnabled: () => {
       const { currentWidget } = shell;
-      return currentWidget && isPrintable(currentWidget);
+      return isPrintable(currentWidget);
     },
     execute: () => {
 

+ 1 - 5
packages/docregistry/src/default.ts

@@ -11,7 +11,7 @@ import { ISignal, Signal } from '@phosphor/signaling';
 
 import { Widget } from '@phosphor/widgets';
 
-import { MainAreaWidget, printSymbol, printd } from '@jupyterlab/apputils';
+import { MainAreaWidget } from '@jupyterlab/apputils';
 
 import { CodeEditor } from '@jupyterlab/codeeditor';
 
@@ -444,8 +444,6 @@ export class DocumentWidget<
     options.reveal = Promise.all([options.reveal, options.context.ready]);
     super(options);
 
-    this[printSymbol] = printd;
-
     this.context = options.context;
 
     // Handle context path changes
@@ -465,8 +463,6 @@ export class DocumentWidget<
     });
   }
 
-  [printSymbol]: () => void;
-
   /**
    * Set URI fragment identifier.
    */

+ 12 - 1
packages/docregistry/src/mimedocument.ts

@@ -1,7 +1,11 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import { showErrorMessage } from '@jupyterlab/apputils';
+import {
+  showErrorMessage,
+  deferPrinting,
+  printSymbol
+} from '@jupyterlab/apputils';
 
 import { ActivityMonitor } from '@jupyterlab/coreutils';
 
@@ -39,6 +43,8 @@ export class MimeContent extends Widget {
     const layout = (this.layout = new StackedLayout());
     layout.addWidget(this.renderer);
 
+    deferPrinting(this, options.renderer);
+
     this._context.ready
       .then(() => {
         return this._render();
@@ -78,6 +84,11 @@ export class MimeContent extends Widget {
    */
   readonly mimeType: string;
 
+  /**
+   * Print method. Defered to the renderer.
+   */
+  [printSymbol]: () => void;
+
   /**
    * A promise that resolves when the widget is ready.
    */

+ 7 - 0
packages/imageviewer/src/widget.ts

@@ -3,6 +3,8 @@
 
 import { PathExt } from '@jupyterlab/coreutils';
 
+import { printSymbol, printd } from '@jupyterlab/apputils';
+
 import {
   ABCWidgetFactory,
   DocumentRegistry,
@@ -63,6 +65,11 @@ export class ImageViewer extends Widget {
     });
   }
 
+  /**
+   * Print in iframe.
+   */
+  [printSymbol] = printd;
+
   /**
    * The image widget's context.
    */

+ 9 - 0
packages/inspector/src/inspector.ts

@@ -1,6 +1,8 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import { printSymbol, printd } from '@jupyterlab/apputils';
+
 import { Token } from '@phosphor/coreutils';
 
 import { ISignal } from '@phosphor/signaling';
@@ -97,6 +99,13 @@ export class InspectorPanel extends Panel implements IInspector {
     this.addClass(PANEL_CLASS);
   }
 
+  /**
+   * Print in iframe
+   */
+  [printSymbol]() {
+    printd.bind(this)('.p-mod-hidden {display: none;}');
+  }
+
   /**
    * The source of events the inspector panel listens for.
    */

+ 4 - 0
packages/json-extension/src/index.tsx

@@ -3,6 +3,8 @@
 
 import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
 
+import { printSymbol, printd } from '@jupyterlab/apputils';
+
 import { Message } from '@phosphor/messaging';
 
 import { Widget } from '@phosphor/widgets';
@@ -40,6 +42,8 @@ export class RenderedJSON extends Widget implements IRenderMime.IRenderer {
     this._mimeType = options.mimeType;
   }
 
+  [printSymbol] = printd;
+
   /**
    * Render JSON into this widget's node.
    */