From 83f9a4162b1fd3d5dcd279dc227583fb142a25d6 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Sat, 16 Dec 2017 09:16:24 +0000 Subject: [PATCH] Request capabilities (e.g. ability to take snapshots) from widgets. --- src/WidgetMessaging.js | 14 +++++++++++ src/components/views/elements/AppTile.js | 32 +++++++++++++----------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/WidgetMessaging.js b/src/WidgetMessaging.js index 2efdc366b6..d71d86f8f5 100644 --- a/src/WidgetMessaging.js +++ b/src/WidgetMessaging.js @@ -383,6 +383,20 @@ export default class WidgetMessaging extends MatrixPostMessageApi { }); }); } + + getCapabilities() { + return new Promise((resolve, reject) => { + this.exec({ + api: "widget_client", + action: "capabilities", + }).then(function(response) { + // console.warn("got capabilities", response.capabilities); + resolve(response.capabilities); + }).catch((error) => { + reject(Error("Failed to get capabilities: " + error.message)); + }); + }); + } } diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index b10d7f91ad..770406e645 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -33,7 +33,6 @@ import AppWarning from './AppWarning'; import MessageSpinner from './MessageSpinner'; import WidgetUtils from '../../../WidgetUtils'; import dis from '../../../dispatcher'; -import domtoimage from 'dom-to-image'; const ALLOWED_APP_URL_SCHEMES = ['https:', 'http:']; @@ -83,9 +82,20 @@ export default React.createClass({ error: null, deleting: false, widgetPageTitle: newProps.widgetPageTitle, + capabilities: [], }; }, + + /** + * Does the widget support a given capability + * @param {[type]} capability Capability to check for + * @return {Boolean} True if capability supported + */ + _hasCapability(capability) { + return this.state.capabilities.some((c) => {return c === capability;}); + }, + /** * Add widget instance specific parameters to pass in wUrl * Properties passed to widget instance: @@ -327,6 +337,12 @@ export default React.createClass({ this.widgetMessaging = new WidgetMessaging(this.refs.appFrame.contentWindow); this.widgetMessaging.startListening(); this.widgetMessaging.addEndpoint(this.props.id, this.props.url); + this.widgetMessaging.getCapabilities().then((capabilities) => { + console.log("Got widget capabilities", this.widgetId, capabilities); + this.setState({capabilities}); + }).catch((err) => { + console.log("Failed to get widget capabilities", this.widgetId, err); + }); this.setState({loading: false}); }, @@ -468,7 +484,7 @@ export default React.createClass({ } // Picture snapshot - const showPictureSnapshotButton = true; // FIXME - Make this dynamic + const showPictureSnapshotButton = this._hasCapability('screenshot'); const showPictureSnapshotIcon = 'img/camera_green.svg'; const windowStateIcon = (this.props.show ? 'img/minimize.svg' : 'img/maximize.svg'); @@ -525,15 +541,3 @@ export default React.createClass({ ); }, }); - -function dataURLtoBlob(dataurl) { - const arr = dataurl.split(','); - const mime = arr[0].match(/:(.*?);/)[1]; - const bstr = atob(arr[1]); - let n = bstr.length; - const u8arr = new Uint8Array(n); - while (n--) { - u8arr[n] = bstr.charCodeAt(n); - } - return new Blob([u8arr], {type: mime}); -}