Skip to content

Commit

Permalink
Add printing for images, JSON and inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Jan 9, 2019
1 parent 7264b0d commit d7cb863
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 13 deletions.
9 changes: 9 additions & 0 deletions packages/apputils/src/mainareawidget.ts
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
*/
Expand Down
29 changes: 24 additions & 5 deletions packages/apputils/src/printing.ts
@@ -1,4 +1,4 @@
import { Printd } from 'printd';
import { Printd, PrintdCallback } from 'printd';
import { Widget } from '@phosphor/widgets';

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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
*/
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion packages/docmanager-extension/src/index.ts
Expand Up @@ -742,7 +742,7 @@ function addCommands(
label: 'Print...',
isEnabled: () => {
const { currentWidget } = shell;
return currentWidget && isPrintable(currentWidget);
return isPrintable(currentWidget);
},
execute: () => {
const widget = contextMenuWidget();
Expand Down
6 changes: 1 addition & 5 deletions packages/docregistry/src/default.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand All @@ -465,8 +463,6 @@ export class DocumentWidget<
});
}

[printSymbol]: () => void;

/**
* Set URI fragment identifier.
*/
Expand Down
13 changes: 12 additions & 1 deletion 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';

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/imageviewer/src/widget.ts
Expand Up @@ -3,6 +3,8 @@

import { PathExt } from '@jupyterlab/coreutils';

import { printSymbol, printd } from '@jupyterlab/apputils';

import {
ABCWidgetFactory,
DocumentRegistry,
Expand Down Expand Up @@ -63,6 +65,11 @@ export class ImageViewer extends Widget {
});
}

/**
* Print in iframe.
*/
[printSymbol] = printd;

/**
* The image widget's context.
*/
Expand Down
14 changes: 13 additions & 1 deletion 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';

Expand Down Expand Up @@ -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.
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/json-extension/src/index.tsx
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit d7cb863

Please sign in to comment.