2018-06-12 13:11:43 +03:00
|
|
|
/*
|
2018-06-13 20:46:02 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2018-06-12 13:11:43 +03: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.
|
|
|
|
*/
|
|
|
|
|
2019-12-08 15:16:17 +03:00
|
|
|
import React, {createRef} from 'react';
|
2018-06-12 13:11:43 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2018-06-14 15:35:35 +03:00
|
|
|
import {Room, User, Group, RoomMember, MatrixEvent} from 'matrix-js-sdk';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2018-06-12 13:11:43 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import QRCode from 'qrcode-react';
|
2019-10-01 05:39:58 +03:00
|
|
|
import {RoomPermalinkCreator, makeGroupPermalink, makeUserPermalink} from "../../../utils/permalinks/Permalinks";
|
2019-12-03 13:53:32 +03:00
|
|
|
import * as ContextMenu from "../../structures/ContextMenu";
|
2019-12-03 02:21:59 +03:00
|
|
|
import {toRightOf} from "../../structures/ContextMenu";
|
2018-06-12 13:11:43 +03:00
|
|
|
|
|
|
|
const socials = [
|
|
|
|
{
|
|
|
|
name: 'Facebook',
|
2019-01-11 04:37:28 +03:00
|
|
|
img: require("../../../../res/img/social/facebook.png"),
|
2018-06-12 13:11:43 +03:00
|
|
|
url: (url) => `https://www.facebook.com/sharer/sharer.php?u=${url}`,
|
|
|
|
}, {
|
|
|
|
name: 'Twitter',
|
2019-01-11 04:37:28 +03:00
|
|
|
img: require("../../../../res/img/social/twitter-2.png"),
|
2018-06-12 13:11:43 +03:00
|
|
|
url: (url) => `https://twitter.com/home?status=${url}`,
|
|
|
|
}, /* // icon missing
|
|
|
|
name: 'Google Plus',
|
|
|
|
img: 'img/social/',
|
|
|
|
url: (url) => `https://plus.google.com/share?url=${url}`,
|
|
|
|
},*/ {
|
2018-06-13 20:46:02 +03:00
|
|
|
name: 'LinkedIn',
|
2019-01-11 04:37:28 +03:00
|
|
|
img: require("../../../../res/img/social/linkedin.png"),
|
2018-06-12 13:11:43 +03:00
|
|
|
url: (url) => `https://www.linkedin.com/shareArticle?mini=true&url=${url}`,
|
|
|
|
}, {
|
|
|
|
name: 'Reddit',
|
2019-01-11 04:37:28 +03:00
|
|
|
img: require("../../../../res/img/social/reddit.png"),
|
2018-06-12 13:11:43 +03:00
|
|
|
url: (url) => `http://www.reddit.com/submit?url=${url}`,
|
|
|
|
}, {
|
|
|
|
name: 'email',
|
2019-01-11 04:37:28 +03:00
|
|
|
img: require("../../../../res/img/social/email-1.png"),
|
2018-06-12 13:11:43 +03:00
|
|
|
url: (url) => `mailto:?body=${url}`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export default class ShareDialog extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
onFinished: PropTypes.func.isRequired,
|
|
|
|
target: PropTypes.oneOfType([
|
|
|
|
PropTypes.instanceOf(Room),
|
|
|
|
PropTypes.instanceOf(User),
|
|
|
|
PropTypes.instanceOf(Group),
|
|
|
|
PropTypes.instanceOf(RoomMember),
|
2018-06-14 15:35:35 +03:00
|
|
|
PropTypes.instanceOf(MatrixEvent),
|
2018-06-12 13:11:43 +03:00
|
|
|
]).isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.onCopyClick = this.onCopyClick.bind(this);
|
2018-06-14 15:41:12 +03:00
|
|
|
this.onLinkSpecificEventCheckboxClick = this.onLinkSpecificEventCheckboxClick.bind(this);
|
2018-06-12 13:11:43 +03:00
|
|
|
|
2020-04-08 00:22:38 +03:00
|
|
|
let permalinkCreator: RoomPermalinkCreator = null;
|
|
|
|
if (props.target instanceof Room) {
|
|
|
|
permalinkCreator = new RoomPermalinkCreator(props.target);
|
|
|
|
permalinkCreator.load();
|
|
|
|
}
|
|
|
|
|
2018-06-12 13:11:43 +03:00
|
|
|
this.state = {
|
2018-06-14 15:41:12 +03:00
|
|
|
// MatrixEvent defaults to share linkSpecificEvent
|
|
|
|
linkSpecificEvent: this.props.target instanceof MatrixEvent,
|
2020-04-08 00:22:38 +03:00
|
|
|
permalinkCreator,
|
2018-06-12 13:11:43 +03:00
|
|
|
};
|
2019-12-08 15:16:17 +03:00
|
|
|
|
|
|
|
this._link = createRef();
|
2018-06-12 13:11:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static _selectText(target) {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(target);
|
|
|
|
|
|
|
|
const selection = window.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
static onLinkClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const {target} = e;
|
|
|
|
ShareDialog._selectText(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
onCopyClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2019-12-08 15:16:17 +03:00
|
|
|
ShareDialog._selectText(this._link.current);
|
2018-06-12 13:11:43 +03:00
|
|
|
|
|
|
|
let successful;
|
|
|
|
try {
|
|
|
|
successful = document.execCommand('copy');
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to copy: ', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const buttonRect = e.target.getBoundingClientRect();
|
2019-11-28 23:26:09 +03:00
|
|
|
const GenericTextContextMenu = sdk.getComponent('context_menus.GenericTextContextMenu');
|
2019-12-03 13:53:32 +03:00
|
|
|
const {close} = ContextMenu.createMenu(GenericTextContextMenu, {
|
2019-12-10 02:58:09 +03:00
|
|
|
...toRightOf(buttonRect, 2),
|
2018-06-12 13:11:43 +03:00
|
|
|
message: successful ? _t('Copied!') : _t('Failed to copy'),
|
2019-11-28 23:26:09 +03:00
|
|
|
});
|
2019-05-20 23:44:05 +03:00
|
|
|
// Drop a reference to this close handler for componentWillUnmount
|
|
|
|
this.closeCopiedTooltip = e.target.onmouseleave = close;
|
2018-06-12 13:11:43 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 15:41:12 +03:00
|
|
|
onLinkSpecificEventCheckboxClick() {
|
2018-06-12 13:11:43 +03:00
|
|
|
this.setState({
|
2018-06-14 15:41:12 +03:00
|
|
|
linkSpecificEvent: !this.state.linkSpecificEvent,
|
2018-06-12 13:11:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:05 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
// if the Copied tooltip is open then get rid of it, there are ways to close the modal which wouldn't close
|
|
|
|
// the tooltip otherwise, such as pressing Escape or clicking X really quickly
|
|
|
|
if (this.closeCopiedTooltip) this.closeCopiedTooltip();
|
|
|
|
}
|
|
|
|
|
2018-06-12 13:11:43 +03:00
|
|
|
render() {
|
|
|
|
let title;
|
|
|
|
let matrixToUrl;
|
|
|
|
|
|
|
|
let checkbox;
|
|
|
|
|
|
|
|
if (this.props.target instanceof Room) {
|
|
|
|
title = _t('Share Room');
|
|
|
|
|
|
|
|
const events = this.props.target.getLiveTimeline().getEvents();
|
|
|
|
if (events.length > 0) {
|
|
|
|
checkbox = <div>
|
|
|
|
<input type="checkbox"
|
|
|
|
id="mx_ShareDialog_checkbox"
|
2018-06-14 15:47:33 +03:00
|
|
|
checked={this.state.linkSpecificEvent}
|
2019-10-03 23:47:19 +03:00
|
|
|
onChange={this.onLinkSpecificEventCheckboxClick} />
|
2018-06-12 13:11:43 +03:00
|
|
|
<label htmlFor="mx_ShareDialog_checkbox">
|
|
|
|
{ _t('Link to most recent message') }
|
|
|
|
</label>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2018-06-14 15:41:12 +03:00
|
|
|
if (this.state.linkSpecificEvent) {
|
2019-02-25 17:35:09 +03:00
|
|
|
matrixToUrl = this.state.permalinkCreator.forEvent(events[events.length - 1].getId());
|
2018-06-12 13:11:43 +03:00
|
|
|
} else {
|
2019-02-25 17:35:09 +03:00
|
|
|
matrixToUrl = this.state.permalinkCreator.forRoom();
|
2018-06-12 13:11:43 +03:00
|
|
|
}
|
|
|
|
} else if (this.props.target instanceof User || this.props.target instanceof RoomMember) {
|
|
|
|
title = _t('Share User');
|
|
|
|
matrixToUrl = makeUserPermalink(this.props.target.userId);
|
|
|
|
} else if (this.props.target instanceof Group) {
|
|
|
|
title = _t('Share Community');
|
|
|
|
matrixToUrl = makeGroupPermalink(this.props.target.groupId);
|
2018-06-14 15:35:35 +03:00
|
|
|
} else if (this.props.target instanceof MatrixEvent) {
|
|
|
|
title = _t('Share Room Message');
|
2018-06-14 15:41:12 +03:00
|
|
|
checkbox = <div>
|
|
|
|
<input type="checkbox"
|
|
|
|
id="mx_ShareDialog_checkbox"
|
2018-06-14 15:47:33 +03:00
|
|
|
checked={this.state.linkSpecificEvent}
|
2018-06-14 15:41:12 +03:00
|
|
|
onClick={this.onLinkSpecificEventCheckboxClick} />
|
|
|
|
<label htmlFor="mx_ShareDialog_checkbox">
|
|
|
|
{ _t('Link to selected message') }
|
|
|
|
</label>
|
|
|
|
</div>;
|
|
|
|
|
|
|
|
if (this.state.linkSpecificEvent) {
|
2019-02-25 17:35:09 +03:00
|
|
|
matrixToUrl = this.props.permalinkCreator.forEvent(this.props.target.getId());
|
2018-06-14 15:41:12 +03:00
|
|
|
} else {
|
2019-02-25 17:35:09 +03:00
|
|
|
matrixToUrl = this.props.permalinkCreator.forRoom();
|
2018-06-14 15:41:12 +03:00
|
|
|
}
|
2018-06-12 13:11:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const encodedUrl = encodeURIComponent(matrixToUrl);
|
|
|
|
|
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
|
|
|
return <BaseDialog title={title}
|
|
|
|
className='mx_ShareDialog'
|
|
|
|
contentId='mx_Dialog_content'
|
|
|
|
onFinished={this.props.onFinished}
|
|
|
|
>
|
|
|
|
<div className="mx_ShareDialog_content">
|
|
|
|
<div className="mx_ShareDialog_matrixto">
|
2019-12-08 15:16:17 +03:00
|
|
|
<a ref={this._link}
|
2018-06-12 13:11:43 +03:00
|
|
|
href={matrixToUrl}
|
|
|
|
onClick={ShareDialog.onLinkClick}
|
|
|
|
className="mx_ShareDialog_matrixto_link"
|
|
|
|
>
|
|
|
|
{ matrixToUrl }
|
|
|
|
</a>
|
|
|
|
<a href={matrixToUrl} className="mx_ShareDialog_matrixto_copy" onClick={this.onCopyClick}>
|
|
|
|
{ _t('COPY') }
|
|
|
|
<div> </div>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
{ checkbox }
|
|
|
|
<hr />
|
|
|
|
|
|
|
|
<div className="mx_ShareDialog_split">
|
2018-06-14 20:56:46 +03:00
|
|
|
<div className="mx_ShareDialog_qrcode_container">
|
2019-01-11 04:37:28 +03:00
|
|
|
<QRCode value={matrixToUrl} size={256} logoWidth={48} logo={require("../../../../res/img/matrix-m.svg")} />
|
2018-06-12 13:11:43 +03:00
|
|
|
</div>
|
2018-06-14 20:56:46 +03:00
|
|
|
<div className="mx_ShareDialog_social_container">
|
|
|
|
{
|
2020-02-24 01:14:29 +03:00
|
|
|
socials.map((social) => <a rel="noreferrer noopener"
|
2018-06-14 20:56:46 +03:00
|
|
|
target="_blank"
|
|
|
|
key={social.name}
|
|
|
|
name={social.name}
|
|
|
|
href={social.url(encodedUrl)}
|
|
|
|
className="mx_ShareDialog_social_icon"
|
|
|
|
>
|
|
|
|
<img src={social.img} alt={social.name} height={64} width={64} />
|
|
|
|
</a>)
|
|
|
|
}
|
2018-06-12 13:11:43 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</BaseDialog>;
|
|
|
|
}
|
|
|
|
}
|