2018-06-12 10:11:43 +00:00
|
|
|
/*
|
2018-06-13 17:46:02 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2018-06-12 10:11:43 +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 PropTypes from 'prop-types';
|
2018-06-14 12:35:35 +00:00
|
|
|
import {Room, User, Group, RoomMember, MatrixEvent} from 'matrix-js-sdk';
|
2018-06-12 10:11:43 +00:00
|
|
|
import sdk from '../../../index';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import QRCode from 'qrcode-react';
|
|
|
|
import {makeEventPermalink, makeGroupPermalink, makeRoomPermalink, makeUserPermalink} from "../../../matrix-to";
|
|
|
|
import * as ContextualMenu from "../../structures/ContextualMenu";
|
|
|
|
|
|
|
|
const socials = [
|
|
|
|
{
|
|
|
|
name: 'Facebook',
|
|
|
|
img: 'img/social/facebook.png',
|
|
|
|
url: (url) => `https://www.facebook.com/sharer/sharer.php?u=${url}`,
|
|
|
|
}, {
|
|
|
|
name: 'Twitter',
|
|
|
|
img: 'img/social/twitter-2.png',
|
|
|
|
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 17:46:02 +00:00
|
|
|
name: 'LinkedIn',
|
2018-06-12 10:11:43 +00:00
|
|
|
img: 'img/social/linkedin.png',
|
|
|
|
url: (url) => `https://www.linkedin.com/shareArticle?mini=true&url=${url}`,
|
|
|
|
}, {
|
|
|
|
name: 'Reddit',
|
|
|
|
img: 'img/social/reddit.png',
|
|
|
|
url: (url) => `http://www.reddit.com/submit?url=${url}`,
|
|
|
|
}, {
|
|
|
|
name: 'email',
|
|
|
|
img: 'img/social/email-1.png',
|
|
|
|
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 12:35:35 +00:00
|
|
|
PropTypes.instanceOf(MatrixEvent),
|
2018-06-12 10:11:43 +00:00
|
|
|
]).isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.onCopyClick = this.onCopyClick.bind(this);
|
2018-06-14 12:41:12 +00:00
|
|
|
this.onLinkSpecificEventCheckboxClick = this.onLinkSpecificEventCheckboxClick.bind(this);
|
2018-06-12 10:11:43 +00:00
|
|
|
|
|
|
|
this.state = {
|
2018-06-14 12:41:12 +00:00
|
|
|
// MatrixEvent defaults to share linkSpecificEvent
|
|
|
|
linkSpecificEvent: this.props.target instanceof MatrixEvent,
|
2018-06-12 10:11:43 +00: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();
|
|
|
|
|
|
|
|
ShareDialog._selectText(this.refs.link);
|
|
|
|
|
|
|
|
let successful;
|
|
|
|
try {
|
|
|
|
successful = document.execCommand('copy');
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to copy: ', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const GenericTextContextMenu = sdk.getComponent('context_menus.GenericTextContextMenu');
|
|
|
|
const buttonRect = e.target.getBoundingClientRect();
|
|
|
|
|
|
|
|
// The window X and Y offsets are to adjust position when zoomed in to page
|
|
|
|
const x = buttonRect.right + window.pageXOffset;
|
|
|
|
const y = (buttonRect.top + (buttonRect.height / 2) + window.pageYOffset) - 19;
|
|
|
|
const {close} = ContextualMenu.createMenu(GenericTextContextMenu, {
|
|
|
|
chevronOffset: 10,
|
|
|
|
left: x,
|
|
|
|
top: y,
|
|
|
|
message: successful ? _t('Copied!') : _t('Failed to copy'),
|
|
|
|
}, false);
|
|
|
|
e.target.onmouseleave = close;
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:41:12 +00:00
|
|
|
onLinkSpecificEventCheckboxClick() {
|
2018-06-12 10:11:43 +00:00
|
|
|
this.setState({
|
2018-06-14 12:41:12 +00:00
|
|
|
linkSpecificEvent: !this.state.linkSpecificEvent,
|
2018-06-12 10:11:43 +00: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 12:47:33 +00:00
|
|
|
checked={this.state.linkSpecificEvent}
|
2018-06-14 12:41:12 +00:00
|
|
|
onClick={this.onLinkSpecificEventCheckboxClick} />
|
2018-06-12 10:11:43 +00:00
|
|
|
<label htmlFor="mx_ShareDialog_checkbox">
|
|
|
|
{ _t('Link to most recent message') }
|
|
|
|
</label>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:41:12 +00:00
|
|
|
if (this.state.linkSpecificEvent) {
|
2018-06-12 10:11:43 +00:00
|
|
|
matrixToUrl = makeEventPermalink(this.props.target.roomId, events[events.length - 1].getId());
|
|
|
|
} else {
|
|
|
|
matrixToUrl = makeRoomPermalink(this.props.target.roomId);
|
|
|
|
}
|
|
|
|
} 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 12:35:35 +00:00
|
|
|
} else if (this.props.target instanceof MatrixEvent) {
|
|
|
|
title = _t('Share Room Message');
|
2018-06-14 12:41:12 +00:00
|
|
|
checkbox = <div>
|
|
|
|
<input type="checkbox"
|
|
|
|
id="mx_ShareDialog_checkbox"
|
2018-06-14 12:47:33 +00:00
|
|
|
checked={this.state.linkSpecificEvent}
|
2018-06-14 12:41:12 +00:00
|
|
|
onClick={this.onLinkSpecificEventCheckboxClick} />
|
|
|
|
<label htmlFor="mx_ShareDialog_checkbox">
|
|
|
|
{ _t('Link to selected message') }
|
|
|
|
</label>
|
|
|
|
</div>;
|
|
|
|
|
|
|
|
if (this.state.linkSpecificEvent) {
|
2018-06-14 12:44:05 +00:00
|
|
|
matrixToUrl = makeEventPermalink(this.props.target.getRoomId(), this.props.target.getId());
|
2018-06-14 12:41:12 +00:00
|
|
|
} else {
|
2018-06-14 12:44:05 +00:00
|
|
|
matrixToUrl = makeRoomPermalink(this.props.target.getRoomId());
|
2018-06-14 12:41:12 +00:00
|
|
|
}
|
2018-06-12 10:11:43 +00: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">
|
|
|
|
<a ref="link"
|
|
|
|
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">
|
|
|
|
<div className="mx_ShareDialog_left">
|
|
|
|
<h3>QR Code</h3>
|
|
|
|
<div className="mx_ShareDialog_qrcode_container">
|
2018-06-12 10:33:38 +00:00
|
|
|
<QRCode value={matrixToUrl} size={256} logoWidth={48} logo="img/matrix-m.svg" />
|
2018-06-12 10:11:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mx_ShareDialog_right">
|
|
|
|
<h3>Social</h3>
|
|
|
|
<div className="mx_ShareDialog_social_container">
|
|
|
|
{
|
2018-06-13 17:46:02 +00:00
|
|
|
socials.map((social) => <a rel="noopener"
|
|
|
|
target="_blank"
|
2018-06-12 10:11:43 +00:00
|
|
|
key={social.name}
|
|
|
|
name={social.name}
|
|
|
|
href={social.url(encodedUrl)}
|
|
|
|
className="mx_ShareDialog_social_icon"
|
|
|
|
>
|
2018-06-13 17:46:02 +00:00
|
|
|
<img src={social.img} alt={social.name} height={64} width={64} />
|
2018-06-12 10:11:43 +00:00
|
|
|
</a>)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</BaseDialog>;
|
|
|
|
}
|
|
|
|
}
|