2018-01-16 18:14:32 +00:00
|
|
|
/*
|
2018-03-29 20:48:03 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2018-01-16 18:14:32 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import Widgets from '../../../utils/widgets';
|
|
|
|
import AppTile from '../elements/AppTile';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2018-01-17 00:04:06 +00:00
|
|
|
import Modal from '../../../Modal';
|
2018-01-16 18:14:32 +00:00
|
|
|
import sdk from '../../../index';
|
2018-01-17 00:04:06 +00:00
|
|
|
import SdkConfig from '../../../SdkConfig';
|
|
|
|
import ScalarAuthClient from '../../../ScalarAuthClient';
|
2018-02-07 09:23:00 +00:00
|
|
|
import dis from '../../../dispatcher';
|
2018-04-02 09:18:35 +00:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2018-01-16 18:14:32 +00:00
|
|
|
|
2018-02-27 13:40:21 +00:00
|
|
|
const widgetType = 'm.stickerpicker';
|
|
|
|
|
2018-02-25 22:10:38 +00:00
|
|
|
export default class Stickerpicker extends React.Component {
|
2018-01-16 18:14:32 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-03-29 16:25:07 +00:00
|
|
|
this._onShowStickersClick = this._onShowStickersClick.bind(this);
|
|
|
|
this._onHideStickersClick = this._onHideStickersClick.bind(this);
|
2018-01-17 00:04:06 +00:00
|
|
|
this._launchManageIntegrations = this._launchManageIntegrations.bind(this);
|
2018-02-25 22:10:38 +00:00
|
|
|
this._removeStickerpickerWidgets = this._removeStickerpickerWidgets.bind(this);
|
2018-02-07 09:23:00 +00:00
|
|
|
this._onWidgetAction = this._onWidgetAction.bind(this);
|
2018-05-11 13:47:57 +00:00
|
|
|
this._onResize = this._onResize.bind(this);
|
2018-04-02 09:56:45 +00:00
|
|
|
this._onFinished = this._onFinished.bind(this);
|
2018-01-16 18:14:32 +00:00
|
|
|
|
2018-05-11 15:23:18 +00:00
|
|
|
this._collectWidgetMessaging = this._collectWidgetMessaging.bind(this);
|
|
|
|
|
2018-01-16 18:14:32 +00:00
|
|
|
this.popoverWidth = 300;
|
|
|
|
this.popoverHeight = 300;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
showStickers: false,
|
2018-02-23 10:20:16 +00:00
|
|
|
imError: null,
|
2018-05-11 13:47:57 +00:00
|
|
|
stickerpickerX: null,
|
|
|
|
stickerpickerY: null,
|
|
|
|
stickerpickerWidget: null,
|
|
|
|
widgetId: null,
|
2018-01-16 18:14:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:10:38 +00:00
|
|
|
_removeStickerpickerWidgets() {
|
|
|
|
console.warn('Removing Stickerpicker widgets');
|
2018-05-11 13:47:57 +00:00
|
|
|
if (this.state.widgetId) {
|
|
|
|
this.scalarClient.disableWidgetAssets(widgetType, this.state.widgetId).then(() => {
|
2018-02-24 00:10:28 +00:00
|
|
|
console.warn('Assets disabled');
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Failed to disable assets');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn('No widget ID specified, not disabling assets');
|
|
|
|
}
|
2018-03-29 20:16:35 +00:00
|
|
|
|
2018-05-11 13:47:57 +00:00
|
|
|
this.setState({showStickers: false});
|
2018-03-29 20:16:35 +00:00
|
|
|
Widgets.removeStickerpickerWidgets().then(() => {
|
2018-04-02 19:25:24 +00:00
|
|
|
this.forceUpdate();
|
2018-03-29 20:16:35 +00:00
|
|
|
}).catch((e) => {
|
|
|
|
console.error('Failed to remove sticker picker widget', e);
|
|
|
|
});
|
2018-02-05 11:49:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 21:35:25 +00:00
|
|
|
componentDidMount() {
|
2018-05-11 13:47:57 +00:00
|
|
|
// Close the sticker picker when the window resizes
|
|
|
|
window.addEventListener('resize', this._onResize);
|
|
|
|
|
2018-01-17 00:04:06 +00:00
|
|
|
this.scalarClient = null;
|
|
|
|
if (SdkConfig.get().integrations_ui_url && SdkConfig.get().integrations_rest_url) {
|
|
|
|
this.scalarClient = new ScalarAuthClient();
|
|
|
|
this.scalarClient.connect().then(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
}).catch((e) => {
|
2018-02-23 10:20:16 +00:00
|
|
|
this._imError("Failed to connect to integrations server", e);
|
2018-01-17 00:04:06 +00:00
|
|
|
});
|
|
|
|
}
|
2018-02-23 10:20:16 +00:00
|
|
|
|
|
|
|
if (!this.state.imError) {
|
|
|
|
this.dispatcherRef = dis.register(this._onWidgetAction);
|
|
|
|
}
|
2018-05-11 13:47:57 +00:00
|
|
|
const stickerpickerWidget = Widgets.getStickerpickerWidgets()[0];
|
|
|
|
this.setState({
|
|
|
|
stickerpickerWidget,
|
|
|
|
widgetId: stickerpickerWidget ? stickerpickerWidget.id : null,
|
|
|
|
});
|
2018-02-07 09:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-05-11 13:47:57 +00:00
|
|
|
window.removeEventListener('resize', this._onResize);
|
2018-03-29 16:35:21 +00:00
|
|
|
if (this.dispatcherRef) {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
}
|
2018-02-07 09:23:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 15:23:18 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (this._appWidgetMessaging &&
|
|
|
|
prevState.showStickers !== this.state.showStickers
|
|
|
|
) {
|
|
|
|
this._appWidgetMessaging.sendVisibility(this.state.showStickers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 10:20:16 +00:00
|
|
|
_imError(errorMsg, e) {
|
|
|
|
console.error(errorMsg, e);
|
|
|
|
this.setState({
|
|
|
|
showStickers: false,
|
|
|
|
imError: errorMsg,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-07 09:23:00 +00:00
|
|
|
_onWidgetAction(payload) {
|
|
|
|
if (payload.action === "user_widget_updated") {
|
2018-04-02 19:25:24 +00:00
|
|
|
this.forceUpdate();
|
2018-02-26 13:43:16 +00:00
|
|
|
} else if (payload.action === "stickerpicker_close") {
|
2018-05-11 13:47:57 +00:00
|
|
|
this.setState({showStickers: false});
|
2018-02-07 09:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 00:04:06 +00:00
|
|
|
|
2018-04-02 09:56:45 +00:00
|
|
|
_defaultStickerpickerContent() {
|
|
|
|
return (
|
|
|
|
<AccessibleButton onClick={this._launchManageIntegrations}
|
|
|
|
className='mx_Stickers_contentPlaceholder'>
|
|
|
|
<p>{ _t("You don't currently have any stickerpacks enabled") }</p>
|
|
|
|
<p className='mx_Stickers_addLink'>Add some now</p>
|
|
|
|
<img src='img/stickerpack-placeholder.png' alt={_t('Add a stickerpack')} />
|
|
|
|
</AccessibleButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_errorStickerpickerContent() {
|
|
|
|
return (
|
|
|
|
<div style={{"text-align": "center"}} className="error">
|
|
|
|
<p> { this.state.imError } </p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:23:18 +00:00
|
|
|
async _collectWidgetMessaging(prom) {
|
|
|
|
const widgetMessaging = await prom;
|
|
|
|
this._appWidgetMessaging = widgetMessaging;
|
|
|
|
this._appWidgetMessaging.sendVisibility(true);
|
|
|
|
}
|
|
|
|
|
2018-04-02 09:56:45 +00:00
|
|
|
_getStickerpickerContent() {
|
|
|
|
// Handle Integration Manager errors
|
|
|
|
if (this.state._imError) {
|
|
|
|
return this._errorStickerpickerContent();
|
|
|
|
}
|
|
|
|
|
2018-01-16 18:14:32 +00:00
|
|
|
// Stickers
|
2018-02-25 22:10:38 +00:00
|
|
|
// TODO - Add support for Stickerpickers from multiple app stores.
|
2018-04-02 09:56:45 +00:00
|
|
|
// Render content from multiple stickerpack sources, each within their
|
|
|
|
// own iframe, within the stickerpicker UI element.
|
2018-05-11 13:47:57 +00:00
|
|
|
const stickerpickerWidget = this.state.stickerpickerWidget;
|
2018-01-16 18:14:32 +00:00
|
|
|
let stickersContent;
|
|
|
|
|
2018-05-11 14:07:51 +00:00
|
|
|
// Use a separate ReactDOM tree to render the AppTile separately so that it persists and does
|
|
|
|
// not unmount when we (a) close the sticker picker (b) switch rooms. It's properties are still
|
|
|
|
// updated.
|
|
|
|
const PersistedElement = sdk.getComponent("elements.PersistedElement");
|
|
|
|
|
2018-01-16 18:14:32 +00:00
|
|
|
// Load stickerpack content
|
2018-02-25 22:10:38 +00:00
|
|
|
if (stickerpickerWidget && stickerpickerWidget.content && stickerpickerWidget.content.url) {
|
2018-02-07 09:23:00 +00:00
|
|
|
// Set default name
|
2018-03-29 16:47:53 +00:00
|
|
|
stickerpickerWidget.content.name = stickerpickerWidget.name || _t("Stickerpack");
|
2018-02-07 14:44:01 +00:00
|
|
|
|
2018-01-16 23:25:07 +00:00
|
|
|
stickersContent = (
|
2018-03-29 21:22:57 +00:00
|
|
|
<div className='mx_Stickers_content_container'>
|
2018-01-16 23:25:07 +00:00
|
|
|
<div
|
|
|
|
id='stickersContent'
|
2018-01-17 15:49:36 +00:00
|
|
|
className='mx_Stickers_content'
|
2018-01-16 23:25:07 +00:00
|
|
|
style={{
|
|
|
|
border: 'none',
|
2018-02-07 14:44:01 +00:00
|
|
|
height: this.popoverHeight,
|
2018-01-16 23:25:07 +00:00
|
|
|
width: this.popoverWidth,
|
|
|
|
}}
|
|
|
|
>
|
2018-05-11 14:07:51 +00:00
|
|
|
<PersistedElement>
|
2018-01-16 23:25:07 +00:00
|
|
|
<AppTile
|
2018-05-11 15:23:18 +00:00
|
|
|
collectWidgetMessaging={this._collectWidgetMessaging}
|
2018-02-25 22:10:38 +00:00
|
|
|
id={stickerpickerWidget.id}
|
|
|
|
url={stickerpickerWidget.content.url}
|
|
|
|
name={stickerpickerWidget.content.name}
|
2018-01-16 23:25:07 +00:00
|
|
|
room={this.props.room}
|
2018-02-25 22:10:38 +00:00
|
|
|
type={stickerpickerWidget.content.type}
|
2018-01-16 23:25:07 +00:00
|
|
|
fullWidth={true}
|
2018-02-25 22:10:38 +00:00
|
|
|
userId={stickerpickerWidget.sender || MatrixClientPeg.get().credentials.userId}
|
2018-01-16 23:25:07 +00:00
|
|
|
creatorUserId={MatrixClientPeg.get().credentials.userId}
|
|
|
|
waitForIframeLoad={true}
|
|
|
|
show={true}
|
2018-02-07 14:44:01 +00:00
|
|
|
showMenubar={true}
|
|
|
|
onEditClick={this._launchManageIntegrations}
|
2018-02-25 22:10:38 +00:00
|
|
|
onDeleteClick={this._removeStickerpickerWidgets}
|
2018-02-07 14:44:01 +00:00
|
|
|
showTitle={false}
|
2018-03-08 17:20:42 +00:00
|
|
|
showMinimise={true}
|
|
|
|
showDelete={false}
|
2018-04-25 15:28:27 +00:00
|
|
|
showPopout={false}
|
2018-03-29 16:25:07 +00:00
|
|
|
onMinimiseClick={this._onHideStickersClick}
|
2018-03-08 17:20:42 +00:00
|
|
|
handleMinimisePointerEvents={true}
|
2018-05-11 15:23:18 +00:00
|
|
|
whitelistCapabilities={['m.sticker', 'visibility']}
|
2018-01-16 23:25:07 +00:00
|
|
|
/>
|
2018-05-11 14:07:51 +00:00
|
|
|
</PersistedElement>
|
2018-01-16 23:25:07 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2018-01-16 18:14:32 +00:00
|
|
|
} else {
|
2018-02-25 22:10:38 +00:00
|
|
|
// Default content to show if stickerpicker widget not added
|
2018-02-07 09:23:00 +00:00
|
|
|
console.warn("No available sticker picker widgets");
|
2018-04-02 19:25:24 +00:00
|
|
|
stickersContent = this._defaultStickerpickerContent();
|
2018-01-16 18:14:32 +00:00
|
|
|
}
|
2018-04-02 09:56:45 +00:00
|
|
|
return stickersContent;
|
2018-01-16 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 16:00:50 +00:00
|
|
|
/**
|
|
|
|
* Show the sticker picker overlay
|
|
|
|
* If no stickerpacks have been added, show a link to the integration manager add sticker packs page.
|
|
|
|
* @param {Event} e Event that triggered the function
|
|
|
|
*/
|
2018-03-29 16:25:07 +00:00
|
|
|
_onShowStickersClick(e) {
|
2018-01-16 18:14:32 +00:00
|
|
|
const buttonRect = e.target.getBoundingClientRect();
|
|
|
|
|
|
|
|
// The window X and Y offsets are to adjust position when zoomed in to page
|
2018-03-08 17:33:07 +00:00
|
|
|
const x = buttonRect.right + window.pageXOffset - 42;
|
2018-01-16 18:14:32 +00:00
|
|
|
const y = (buttonRect.top + (buttonRect.height / 2) + window.pageYOffset) - 19;
|
|
|
|
|
2018-05-11 13:47:57 +00:00
|
|
|
this.setState({
|
|
|
|
showStickers: true,
|
|
|
|
stickerPickerX: x,
|
|
|
|
stickerPickerY: y,
|
|
|
|
});
|
2018-01-16 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 16:00:50 +00:00
|
|
|
/**
|
|
|
|
* Trigger hiding of the sticker picker overlay
|
|
|
|
* @param {Event} ev Event that triggered the function call
|
|
|
|
*/
|
2018-03-29 16:25:07 +00:00
|
|
|
_onHideStickersClick(ev) {
|
2018-05-11 13:47:57 +00:00
|
|
|
this.setState({showStickers: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the window is resized
|
|
|
|
*/
|
|
|
|
_onResize() {
|
|
|
|
this.setState({showStickers: false});
|
2018-01-16 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 16:00:50 +00:00
|
|
|
/**
|
|
|
|
* The stickers picker was hidden
|
|
|
|
*/
|
2018-04-02 09:56:45 +00:00
|
|
|
_onFinished() {
|
2018-01-16 21:28:15 +00:00
|
|
|
this.setState({showStickers: false});
|
|
|
|
}
|
|
|
|
|
2018-01-22 16:00:50 +00:00
|
|
|
/**
|
|
|
|
* Launch the integrations manager on the stickers integration page
|
|
|
|
*/
|
2018-01-17 00:04:06 +00:00
|
|
|
_launchManageIntegrations() {
|
|
|
|
const IntegrationsManager = sdk.getComponent("views.settings.IntegrationsManager");
|
|
|
|
const src = (this.scalarClient !== null && this.scalarClient.hasCredentials()) ?
|
|
|
|
this.scalarClient.getScalarInterfaceUrlForRoom(
|
2018-02-09 11:44:27 +00:00
|
|
|
this.props.room,
|
2018-02-27 13:40:21 +00:00
|
|
|
'type_' + widgetType,
|
2018-05-11 13:47:57 +00:00
|
|
|
this.state.widgetId,
|
2018-01-17 00:04:06 +00:00
|
|
|
) :
|
|
|
|
null;
|
|
|
|
Modal.createTrackedDialog('Integrations Manager', '', IntegrationsManager, {
|
|
|
|
src: src,
|
|
|
|
}, "mx_IntegrationsManager");
|
2018-02-26 17:47:52 +00:00
|
|
|
|
2018-05-11 13:47:57 +00:00
|
|
|
this.setState({showStickers: false});
|
2018-01-17 00:04:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 18:14:32 +00:00
|
|
|
render() {
|
|
|
|
const TintableSvg = sdk.getComponent("elements.TintableSvg");
|
2018-05-11 13:47:57 +00:00
|
|
|
const ContextualMenu = sdk.getComponent('structures.ContextualMenu');
|
|
|
|
const GenericElementContextMenu = sdk.getComponent('context_menus.GenericElementContextMenu');
|
2018-01-16 18:14:32 +00:00
|
|
|
let stickersButton;
|
2018-05-11 13:47:57 +00:00
|
|
|
|
|
|
|
const stickerPicker = <ContextualMenu
|
|
|
|
elementClass={GenericElementContextMenu}
|
|
|
|
chevronOffset={10}
|
|
|
|
chevronFace={'bottom'}
|
|
|
|
left={this.state.stickerPickerX}
|
|
|
|
top={this.state.stickerPickerY}
|
|
|
|
menuWidth={this.popoverWidth}
|
|
|
|
menuHeight={this.popoverHeight}
|
|
|
|
element={this._getStickerpickerContent()}
|
|
|
|
onFinished={this._onFinished}
|
|
|
|
menuPaddingTop={0}
|
|
|
|
menuPaddingLeft={0}
|
|
|
|
menuPaddingRight={0}
|
|
|
|
/>;
|
|
|
|
|
2018-01-16 18:14:32 +00:00
|
|
|
if (this.state.showStickers) {
|
|
|
|
// Show hide-stickers button
|
|
|
|
stickersButton =
|
|
|
|
<div
|
|
|
|
id='stickersButton'
|
|
|
|
key="controls_hide_stickers"
|
2018-01-17 15:49:36 +00:00
|
|
|
className="mx_MessageComposer_stickers mx_Stickers_hideStickers"
|
2018-03-29 16:25:07 +00:00
|
|
|
onClick={this._onHideStickersClick}
|
2018-01-16 18:14:32 +00:00
|
|
|
ref='target'
|
|
|
|
title={_t("Hide Stickers")}>
|
|
|
|
<TintableSvg src="img/icons-hide-stickers.svg" width="35" height="35" />
|
|
|
|
</div>;
|
|
|
|
} else {
|
|
|
|
// Show show-stickers button
|
|
|
|
stickersButton =
|
|
|
|
<div
|
|
|
|
id='stickersButton'
|
|
|
|
key="constrols_show_stickers"
|
|
|
|
className="mx_MessageComposer_stickers"
|
2018-03-29 16:25:07 +00:00
|
|
|
onClick={this._onShowStickersClick}
|
2018-01-16 18:14:32 +00:00
|
|
|
title={_t("Show Stickers")}>
|
|
|
|
<TintableSvg src="img/icons-show-stickers.svg" width="35" height="35" />
|
|
|
|
</div>;
|
|
|
|
}
|
2018-05-11 13:47:57 +00:00
|
|
|
return <div>
|
|
|
|
{stickersButton}
|
|
|
|
{this.state.showStickers && stickerPicker}
|
|
|
|
</div>;
|
2018-01-16 18:14:32 +00:00
|
|
|
}
|
|
|
|
}
|