Skip to content

Commit

Permalink
Try adding support for code mirror printing
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed May 13, 2019
1 parent 609799b commit 3851594
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
38 changes: 20 additions & 18 deletions packages/apputils/src/printing.ts
Expand Up @@ -60,7 +60,7 @@ export namespace Printing {
* to a hidden iframe and printing that iframe.
*/
export function printWidget(widget: Widget): Promise<void> {
return printContent(widget.node);
return printElement(widget.node);
}

const settings = ServerConnection.makeSettings();
Expand All @@ -79,27 +79,37 @@ export namespace Printing {
}

/**
* Prints a URL or an element in an iframe and then removes the iframe after printing.
* Prints an element by copying it into an iframe.
*/
async function printContent(textOrEl: string | HTMLElement): Promise<void> {
const isText = typeof textOrEl === 'string';
export function printElement(el: HTMLElement): Promise<void> {
return printContent(parent => parent.appendChild(el.cloneNode(true)));
}
/**
* Prints a URL or an callback that sets a node element in an iframe and then removes the iframe after printing.
*/
export async function printContent(
textOrCallback: string | ((el: HTMLElement) => void)
): Promise<void> {
const iframe = createIFrame();

let loaded = resolveWhenLoaded(iframe);
const parent = window.document.body;
parent.appendChild(iframe);
if (isText) {
iframe.srcdoc = textOrEl as string;
await resolveWhenLoaded(iframe);
if (typeof textOrCallback === 'string') {
iframe.srcdoc = textOrCallback as string;
} else {
iframe.src = 'about:blank';
setIFrameNode(iframe, textOrEl as HTMLElement);
await loaded;
textOrCallback(iframe.contentDocument.body);
iframe.contentDocument.close();
}
await loaded;
console.log('new new', iframe);
const printed = resolveAfterEvent();
launchPrint(iframe.contentWindow);
// Once the print dialog has been dismissed, we regain event handling,
// and it should be safe to discard the hidden iframe.
await printed;
parent.removeChild(iframe);
// parent.removeChild(iframe);
}

/**
Expand All @@ -125,14 +135,6 @@ export namespace Printing {
return el;
}

/**
* Copies a node from the base document to the iframe.
*/
function setIFrameNode(iframe: HTMLIFrameElement, node: HTMLElement) {
iframe.contentDocument.body.appendChild(node.cloneNode(true));
iframe.contentDocument.close();
}

/**
* Promise that resolves when all resources are loaded in the window.
*/
Expand Down
18 changes: 16 additions & 2 deletions packages/codemirror/src/editor.ts
Expand Up @@ -13,7 +13,7 @@ import { IDisposable, DisposableDelegate } from '@phosphor/disposable';

import { Signal } from '@phosphor/signaling';

import { showDialog } from '@jupyterlab/apputils';
import { showDialog, Printing } from '@jupyterlab/apputils';

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

Expand Down Expand Up @@ -89,7 +89,8 @@ const HOVER_TIMEOUT = 1000;
/**
* CodeMirror editor.
*/
export class CodeMirrorEditor implements CodeEditor.IEditor {
export class CodeMirrorEditor
implements CodeEditor.IEditor, Printing.IPrintable {
/**
* Construct a CodeMirror editor.
*/
Expand Down Expand Up @@ -173,6 +174,19 @@ export class CodeMirrorEditor implements CodeEditor.IEditor {
});
}

/**
* Prints this editor by creating a new one in an iframe.
*/
[Printing.symbol]() {
return async () => {
Printing.printContent(el => {
Private.createEditor(el, this._config)
.getDoc()
.setValue(this.model.value.text);
});
};
}

/**
* A signal emitted when either the top or bottom edge is requested.
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/fileeditor/src/widget.ts
Expand Up @@ -20,6 +20,7 @@ import {
import { PromiseDelegate } from '@phosphor/coreutils';

import { Message } from '@phosphor/messaging';
import { Printing } from '@jupyterlab/apputils';

/**
* The data attribute added to a widget that can run code.
Expand Down Expand Up @@ -156,7 +157,7 @@ export class FileEditorCodeWrapper extends CodeEditorWrapper {
/**
* A widget for editors.
*/
export class FileEditor extends Widget {
export class FileEditor extends Widget implements Printing.IPrintable {
/**
* Construct a new editor widget.
*/
Expand Down Expand Up @@ -216,6 +217,13 @@ export class FileEditor extends Widget {
}
}

/**
* Delegate printing to code editor widget
*/
[Printing.symbol]() {
return Printing.getPrintFunction(this.editorWidget.editor);
}

/**
* Handle `after-attach` messages for the widget.
*/
Expand Down

0 comments on commit 3851594

Please sign in to comment.