Skip to content

Commit

Permalink
Move printint to apputils and add support docs
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Jan 9, 2019
1 parent 3a4dd7f commit 386654a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/apputils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@phosphor/virtualdom": "^1.1.2",
"@phosphor/widgets": "^1.6.0",
"@types/react": "~16.4.13",
"printd": "1.0.1",
"react": "~16.4.2",
"react-dom": "~16.4.2",
"sanitize-html": "~1.18.2"
Expand Down
1 change: 1 addition & 0 deletions packages/apputils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './hoverbox';
export * from './iframe';
export * from './instancetracker';
export * from './mainareawidget';
export * from './printing';
export * from './sanitizer';
export * from './spinner';
export * from './splash';
Expand Down
43 changes: 43 additions & 0 deletions packages/apputils/src/printing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Printd } from 'printd';
import { Widget } from '@phosphor/widgets';

/**
* Symbol to use for a method that prints out out the object.
*/
export const printSymbol = Symbol();

/**
* Widgets who provide a custom way of printing themselves
* should implement this interface.
*/
export interface IPrintable {
[printSymbol]: () => void;
}

/**
* Returns whether an object implements a print method.
*/
export function isPrintable(a: object): a is IPrintable {
return printSymbol in a;
}

/**
* Calls the print symbol on an object to print it.
*/
export function print(a: IPrintable) {
a[printSymbol]();
}

/**
* Global print instance
*/
const _PRINTD = new Printd();

/**
* Use this as a print property for a widget that will
* 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);
}
3 changes: 1 addition & 2 deletions packages/docmanager-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"@jupyterlab/statusbar": "^0.7.1",
"@phosphor/algorithm": "^1.1.2",
"@phosphor/disposable": "^1.1.2",
"@phosphor/widgets": "^1.6.0",
"printd": "1.0.1"
"@phosphor/widgets": "^1.6.0"
},
"devDependencies": {
"rimraf": "~2.6.2",
Expand Down
21 changes: 11 additions & 10 deletions packages/docmanager-extension/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { Printd } from 'printd';

import { toArray, iter } from '@phosphor/algorithm';

import { Widget, DockLayout } from '@phosphor/widgets';
Expand All @@ -13,7 +11,10 @@ import {
showDialog,
showErrorMessage,
Dialog,
ICommandPalette
ICommandPalette,
isPrintable,
print,
IPrintable
} from '@jupyterlab/apputils';

import { IChangedArgs, ISettingRegistry, Time } from '@jupyterlab/coreutils';
Expand Down Expand Up @@ -42,8 +43,6 @@ import { IDisposable } from '@phosphor/disposable';
*/
const MARKDOWN_FACTORY = 'Markdown Preview';

const _PRINTD = new Printd();

/**
* The command IDs used by the document manager plugin.
*/
Expand Down Expand Up @@ -741,13 +740,15 @@ function addCommands(

commands.addCommand(CommandIDs.print, {
label: 'Print...',
isEnabled: () => {
const { currentWidget } = shell;
return currentWidget && isPrintable(currentWidget);
},
execute: () => {
const widget = contextMenuWidget();
function callback(win: any, doc: any, node: any, launchPrint: any) {
console.log(win, doc, node, launchPrint);
setTimeout(() => launchPrint(win), 800);
}
_PRINTD.print(widget.node, '', callback);

// We know widget has to be printable because is only enabled on printable widgets.
print((widget as unknown) as IPrintable);
}
});

Expand Down
6 changes: 5 additions & 1 deletion packages/docregistry/src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ISignal, Signal } from '@phosphor/signaling';

import { Widget } from '@phosphor/widgets';

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

import { CodeEditor } from '@jupyterlab/codeeditor';

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

[printSymbol]: () => void;

/**
* Set URI fragment identifier.
*/
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7886,6 +7886,10 @@ pretty-format@^23.6.0:
ansi-regex "^3.0.0"
ansi-styles "^3.2.0"

printd@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/printd/-/printd-1.0.1.tgz#fddfb374b14130d8aa31f02734e3a0f103f6ad98"

private@^0.1.8, private@~0.1.5:
version "0.1.8"
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
Expand Down

0 comments on commit 386654a

Please sign in to comment.