Skip to content

Commit

Permalink
Merge 97063f8 into 04fadbc
Browse files Browse the repository at this point in the history
  • Loading branch information
sachingupta committed Oct 5, 2018
2 parents 04fadbc + 97063f8 commit 7f6d448
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/MicrosoftTeams.ts
Expand Up @@ -496,6 +496,7 @@ namespace microsoftTeams {
let callbacks: { [id: number]: Function } = {};
let frameContext: string;
let hostClientType: string;
let printCapabilityEnabled: boolean = false;

let themeChangeHandler: (theme: string) => void;
handlers["themeChange"] = handleThemeChange;
Expand Down Expand Up @@ -588,6 +589,32 @@ namespace microsoftTeams {
};
}

/**
* Enable print capability to support printing page using Ctrl+P and cmd+P
*/
export function enablePrintCapability(): void {
if (!printCapabilityEnabled) {
printCapabilityEnabled = true;
ensureInitialized();
// adding ctrl+P and cmd+P handler
document.addEventListener("keydown", (event: KeyboardEvent) => {
if ((event.ctrlKey || event.metaKey) && event.keyCode === 80) {
microsoftTeams.print();
event.cancelBubble = true;
event.preventDefault();
event.stopImmediatePropagation();
}
});
}
}

/**
* default print handler
*/
export function print(): void {
window.print();
}

/**
* Retrieves the current context the frame is running in.
* @param callback The callback to invoke when the {@link Context} object is retrieved.
Expand Down
79 changes: 79 additions & 0 deletions test/MicrosoftTeams.spec.ts
Expand Up @@ -566,6 +566,85 @@ describe("MicrosoftTeams", () => {

sendMessage("settings.remove");

expect(handlerCalled).toBeTruthy();
});

it("Ctrl+P shouldn't call print handler if printCapabilty is disabled", () => {
let handlerCalled = false;
microsoftTeams.initialize();
spyOn(microsoftTeams, "print").and.callFake((): void => {
handlerCalled = true;
});
let printEvent = new Event("keydown");
// tslint:disable:no-any
(printEvent as any).keyCode = 80;
(printEvent as any).ctrlKey = true;
// tslint:enable:no-any

document.dispatchEvent(printEvent);
expect(handlerCalled).toBeFalsy();
});

it("Cmd+P shouldn't call print handler if printCapabilty is disabled", () => {
let handlerCalled = false;
microsoftTeams.initialize();
spyOn(microsoftTeams, "print").and.callFake((): void => {
handlerCalled = true;
});
let printEvent = new Event("keydown");
// tslint:disable:no-any
(printEvent as any).keyCode = 80;
(printEvent as any).metaKey = true;
// tslint:enable:no-any

document.dispatchEvent(printEvent);
expect(handlerCalled).toBeFalsy();
});

it("print handler should successfully call default print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
microsoftTeams.enablePrintCapability();
spyOn(window, "print").and.callFake((): void => {
handlerCalled = true;
});

microsoftTeams.print();

expect(handlerCalled).toBeTruthy();
});

it("Ctrl+P should successfully call print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
microsoftTeams.enablePrintCapability();
spyOn(microsoftTeams, "print").and.callFake((): void => {
handlerCalled = true;
});
let printEvent = new Event("keydown");
// tslint:disable:no-any
(printEvent as any).keyCode = 80;
(printEvent as any).ctrlKey = true;
// tslint:enable:no-any

document.dispatchEvent(printEvent);
expect(handlerCalled).toBeTruthy();
});

it("Cmd+P should successfully call print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
microsoftTeams.enablePrintCapability();
spyOn(microsoftTeams, "print").and.callFake((): void => {
handlerCalled = true;
});
let printEvent = new Event("keydown");
// tslint:disable:no-any
(printEvent as any).keyCode = 80;
(printEvent as any).metaKey = true;
// tslint:enable:no-any

document.dispatchEvent(printEvent);
expect(handlerCalled).toBe(true);
});

Expand Down

0 comments on commit 7f6d448

Please sign in to comment.