Move capabilities to their own class

This commit is contained in:
Travis Ralston 2018-05-12 16:46:08 -06:00
parent afd9d6f6c1
commit e2ed9a8839
5 changed files with 47 additions and 31 deletions

View File

@ -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");
});
}
}
}

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -8,6 +8,6 @@
<h4>Sorry, this content cannot be embedded</h4>
</div>
</div>
<iframe [src]="embedUrl" *ngIf="!isLoading && canEmbed" frameborder="0" allowfullscreen myScreenshotCapable=""></iframe>
<iframe [src]="embedUrl" *ngIf="!isLoading && canEmbed" frameborder="0" allowfullscreen></iframe>
<my-fullscreen-button *ngIf="!isLoading && canEmbed" class="toggleFullscreen"></my-fullscreen-button>
</div>

View File

@ -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"*/]);
}
}
}