From e2ed9a88393c1963f852e62f06090f3f15673244 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sat, 12 May 2018 16:46:08 -0600 Subject: [PATCH] Move capabilities to their own class --- .../screenshot-capable.directive.ts | 23 +++++++++++----- .../services/scalar/scalar-widget.api.ts | 2 +- web/app/widget-wrappers/capable-widget.ts | 27 +++++++++++++++++++ .../generic/generic.component.html | 2 +- .../generic/generic.component.ts | 24 ++--------------- 5 files changed, 47 insertions(+), 31 deletions(-) create mode 100644 web/app/widget-wrappers/capable-widget.ts diff --git a/web/app/shared/directives/screenshot-capable.directive.ts b/web/app/shared/directives/screenshot-capable.directive.ts index 62e3c63..ef0bc6d 100644 --- a/web/app/shared/directives/screenshot-capable.directive.ts +++ b/web/app/shared/directives/screenshot-capable.directive.ts @@ -18,7 +18,7 @@ export class ScreenshotCapableDirective implements OnInit, OnDestroy { public ngOnInit() { this.widgetApiSubscription = ScalarWidgetApi.requestReceived.subscribe(request => { if (request.action === "screenshot") this.takeScreenshot(request); - }) + }); } public ngOnDestroy() { @@ -26,11 +26,20 @@ export class ScreenshotCapableDirective implements OnInit, OnDestroy { } private takeScreenshot(request: ScalarToWidgetRequest) { - domtoimage.toBlob(this.el.nativeElement).then(b => { - ScalarWidgetApi.replyScreenshot(request, b); - }).catch(error => { - console.error(error); - ScalarWidgetApi.replyError(request, error, "Failed to take screenshot"); - }); + if (this.el.nativeElement.tagName === "IFRAME") { + console.error("Attempted to take a screenshot of an iframe"); + ScalarWidgetApi.replyError(request, new Error("Cannot take screenshot of iframe"), "Failed to take screenshot: iframe not supported"); + } else { + domtoimage.toBlob(this.el.nativeElement).then(b => { + if (!b) { + console.warn("No screenshot produced - skipping reply"); + return; + } + ScalarWidgetApi.replyScreenshot(request, b); + }).catch(error => { + console.error(error); + ScalarWidgetApi.replyError(request, error, "Failed to take screenshot"); + }); + } } } diff --git a/web/app/shared/services/scalar/scalar-widget.api.ts b/web/app/shared/services/scalar/scalar-widget.api.ts index e70de23..1b62fe4 100644 --- a/web/app/shared/services/scalar/scalar-widget.api.ts +++ b/web/app/shared/services/scalar/scalar-widget.api.ts @@ -35,7 +35,7 @@ export class ScalarWidgetApi { } public static replyError(request: ScalarToWidgetRequest, error: Error, message: string = null): void { - ScalarWidgetApi.replyEvent(request, {error: {message: message || error.message, error: error}}); + ScalarWidgetApi.replyEvent(request, {error: {message: message || error.message, _error: error}}); } private static callAction(action, payload) { diff --git a/web/app/widget-wrappers/capable-widget.ts b/web/app/widget-wrappers/capable-widget.ts new file mode 100644 index 0000000..19bb125 --- /dev/null +++ b/web/app/widget-wrappers/capable-widget.ts @@ -0,0 +1,27 @@ +import { OnDestroy, OnInit } from "@angular/core"; +import { Subscription } from "rxjs/Subscription"; +import { ScalarWidgetApi } from "../shared/services/scalar/scalar-widget.api"; + +export abstract class CapableWidget implements OnInit, OnDestroy { + + private widgetApiSubscription: Subscription; + + // The capabilities we support + protected supportsScreenshots = false; + + public ngOnInit() { + this.widgetApiSubscription = ScalarWidgetApi.requestReceived.subscribe(request => { + if (request.action === "capabilities") { + const capabilities = []; + + if (this.supportsScreenshots) capabilities.push("m.capability.screenshot"); + + ScalarWidgetApi.replyCapabilities(request, capabilities); + } + }); + } + + public ngOnDestroy() { + if (this.widgetApiSubscription) this.widgetApiSubscription.unsubscribe(); + } +} \ No newline at end of file diff --git a/web/app/widget-wrappers/generic/generic.component.html b/web/app/widget-wrappers/generic/generic.component.html index 8dfc41c..a78bcef 100644 --- a/web/app/widget-wrappers/generic/generic.component.html +++ b/web/app/widget-wrappers/generic/generic.component.html @@ -8,6 +8,6 @@

Sorry, this content cannot be embedded

- + \ No newline at end of file diff --git a/web/app/widget-wrappers/generic/generic.component.ts b/web/app/widget-wrappers/generic/generic.component.ts index 631904b..613a152 100644 --- a/web/app/widget-wrappers/generic/generic.component.ts +++ b/web/app/widget-wrappers/generic/generic.component.ts @@ -1,24 +1,20 @@ -import { Component, OnDestroy, OnInit } from "@angular/core"; +import { Component } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; import { DomSanitizer, SafeUrl } from "@angular/platform-browser"; import { WidgetApiService } from "../../shared/services/integrations/widget-api.service"; import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api"; -import { Subscription } from "rxjs/Subscription"; -import { ScalarToWidgetRequest } from "../../shared/models/scalar-widget-actions"; @Component({ selector: "my-generic-widget-wrapper", templateUrl: "generic.component.html", styleUrls: ["generic.component.scss"], }) -export class GenericWidgetWrapperComponent implements OnInit, OnDestroy { +export class GenericWidgetWrapperComponent { public isLoading = true; public canEmbed = false; public embedUrl: SafeUrl = null; - private widgetApiSubscription: Subscription; - constructor(widgetApi: WidgetApiService, activatedRoute: ActivatedRoute, sanitizer: DomSanitizer) { let params: any = activatedRoute.snapshot.queryParams; @@ -34,20 +30,4 @@ export class GenericWidgetWrapperComponent implements OnInit, OnDestroy { this.isLoading = false; }); } - - public ngOnInit() { - this.widgetApiSubscription = ScalarWidgetApi.requestReceived.subscribe(e => this.onWidgetRequest(e)); - } - - public ngOnDestroy() { - if (this.widgetApiSubscription) this.widgetApiSubscription.unsubscribe(); - } - - private onWidgetRequest(request: ScalarToWidgetRequest) { - if (request.action === "capabilities") { - // Taking of the screenshot is handled elsewhere - // TODO: Re-enable screenshots via a configuration flag when Riot has better support for them (and move it to an abstract class) - ScalarWidgetApi.replyCapabilities(request, [/*"m.capability.screenshot"*/]); - } - } }