diff --git a/packages/apputils/src/mainareawidget.ts b/packages/apputils/src/mainareawidget.ts index c8c9ebdcd00b..0f87b0488fd1 100644 --- a/packages/apputils/src/mainareawidget.ts +++ b/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 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 extends Widget { } } + /** + * Print method. Defered to content. + */ + [printSymbol]: () => void; + /** * The content hosted by the widget. */ diff --git a/packages/apputils/src/printing.ts b/packages/apputils/src/printing.ts index 56c8c331094d..88f4d42b425f 100644 --- a/packages/apputils/src/printing.ts +++ b/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); } diff --git a/packages/docmanager-extension/src/index.ts b/packages/docmanager-extension/src/index.ts index 5994d699366b..5654d8149615 100644 --- a/packages/docmanager-extension/src/index.ts +++ b/packages/docmanager-extension/src/index.ts @@ -742,7 +742,7 @@ function addCommands( label: 'Print...', isEnabled: () => { const { currentWidget } = shell; - return currentWidget && isPrintable(currentWidget); + return isPrintable(currentWidget); }, execute: () => { const widget = contextMenuWidget(); diff --git a/packages/docregistry/src/default.ts b/packages/docregistry/src/default.ts index 77b1046d7f67..b40541249865 100644 --- a/packages/docregistry/src/default.ts +++ b/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. */ diff --git a/packages/docregistry/src/mimedocument.ts b/packages/docregistry/src/mimedocument.ts index b601fde2e596..387da16ee88d 100644 --- a/packages/docregistry/src/mimedocument.ts +++ b/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. */ diff --git a/packages/imageviewer/src/widget.ts b/packages/imageviewer/src/widget.ts index 4fb7d552b5f9..0288f397325b 100644 --- a/packages/imageviewer/src/widget.ts +++ b/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. */ diff --git a/packages/inspector/src/inspector.ts b/packages/inspector/src/inspector.ts index 76fe22a460b2..7e35dd370cd5 100644 --- a/packages/inspector/src/inspector.ts +++ b/packages/inspector/src/inspector.ts @@ -1,7 +1,12 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. -import { Toolbar, ToolbarButton } from '@jupyterlab/apputils'; +import { + Toolbar, + ToolbarButton, + printSymbol, + printd +} from '@jupyterlab/apputils'; import { Token } from '@phosphor/coreutils'; @@ -163,6 +168,13 @@ export class InspectorPanel extends TabPanel 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. */ diff --git a/packages/json-extension/src/index.tsx b/packages/json-extension/src/index.tsx index 2cc21ea20840..05013d3a4a3a 100644 --- a/packages/json-extension/src/index.tsx +++ b/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'; @@ -38,6 +40,8 @@ export class RenderedJSON extends Widget implements IRenderMime.IRenderer { this._mimeType = options.mimeType; } + [printSymbol] = printd; + /** * Render JSON into this widget's node. */