2018-06-12 13:11:43 +03:00
|
|
|
/*
|
2018-06-13 20:46:02 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2020-04-15 02:22:19 +03:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-15 02:22:19 +03:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as PropTypes from 'prop-types';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { User } from "matrix-js-sdk/src/models/user";
|
|
|
|
import { Group } from "matrix-js-sdk/src/models/group";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2018-06-12 13:11:43 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2020-04-12 01:46:58 +03:00
|
|
|
import QRCode from "../elements/QRCode";
|
2021-06-29 15:11: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";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { toRightOf } from "../../structures/ContextMenu";
|
|
|
|
import { copyPlaintext, selectText } from "../../../utils/strings";
|
2020-05-29 00:33:00 +03:00
|
|
|
import StyledCheckbox from '../elements/StyledCheckbox';
|
2020-08-03 21:21:24 +03:00
|
|
|
import AccessibleTooltipButton from '../elements/AccessibleTooltipButton';
|
2020-08-25 22:58:15 +03:00
|
|
|
import { IDialogProps } from "./IDialogProps";
|
2020-09-16 18:06:17 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { UIFeature } from "../../../settings/UIFeature";
|
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-07-03 11:44:03 +03:00
|
|
|
import BaseDialog from "./BaseDialog";
|
2021-07-12 20:43:20 +03:00
|
|
|
import GenericTextContextMenu from "../context_menus/GenericTextContextMenu";
|
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}`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-08-25 22:58:15 +03:00
|
|
|
interface IProps extends IDialogProps {
|
2020-04-15 02:22:19 +03:00
|
|
|
target: Room | User | Group | RoomMember | MatrixEvent;
|
|
|
|
permalinkCreator: RoomPermalinkCreator;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
linkSpecificEvent: boolean;
|
|
|
|
permalinkCreator: RoomPermalinkCreator;
|
|
|
|
}
|
|
|
|
|
2021-03-09 05:59:41 +03:00
|
|
|
@replaceableComponent("views.dialogs.ShareDialog")
|
2020-04-15 02:22:19 +03:00
|
|
|
export default class ShareDialog extends React.PureComponent<IProps, IState> {
|
2018-06-12 13:11:43 +03:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-04-15 02:22:19 +03:00
|
|
|
protected closeCopiedTooltip: () => void;
|
|
|
|
|
2018-06-12 13:11:43 +03:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static onLinkClick(e) {
|
|
|
|
e.preventDefault();
|
2020-04-15 02:16:11 +03:00
|
|
|
selectText(e.target);
|
2018-06-12 13:11:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 02:16:11 +03:00
|
|
|
async onCopyClick(e) {
|
2018-06-12 13:11:43 +03:00
|
|
|
e.preventDefault();
|
2020-04-15 02:16:11 +03:00
|
|
|
const target = e.target; // copy target before we go async and React throws it away
|
2018-06-12 13:11:43 +03:00
|
|
|
|
2020-04-15 02:16:11 +03:00
|
|
|
const successful = await copyPlaintext(this.getUrl());
|
|
|
|
const buttonRect = target.getBoundingClientRect();
|
2021-06-29 15:11:58 +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
|
2020-04-15 02:16:11 +03:00
|
|
|
this.closeCopiedTooltip = 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();
|
|
|
|
}
|
|
|
|
|
2020-04-15 02:16:11 +03:00
|
|
|
getUrl() {
|
2018-06-12 13:11:43 +03:00
|
|
|
let matrixToUrl;
|
|
|
|
|
2020-04-15 02:16:11 +03:00
|
|
|
if (this.props.target instanceof Room) {
|
|
|
|
if (this.state.linkSpecificEvent) {
|
|
|
|
const events = this.props.target.getLiveTimeline().getEvents();
|
|
|
|
matrixToUrl = this.state.permalinkCreator.forEvent(events[events.length - 1].getId());
|
|
|
|
} else {
|
2020-12-06 13:29:11 +03:00
|
|
|
matrixToUrl = this.state.permalinkCreator.forShareableRoom();
|
2020-04-15 02:16:11 +03:00
|
|
|
}
|
|
|
|
} else if (this.props.target instanceof User || this.props.target instanceof RoomMember) {
|
|
|
|
matrixToUrl = makeUserPermalink(this.props.target.userId);
|
|
|
|
} else if (this.props.target instanceof Group) {
|
|
|
|
matrixToUrl = makeGroupPermalink(this.props.target.groupId);
|
|
|
|
} else if (this.props.target instanceof MatrixEvent) {
|
|
|
|
if (this.state.linkSpecificEvent) {
|
|
|
|
matrixToUrl = this.props.permalinkCreator.forEvent(this.props.target.getId());
|
|
|
|
} else {
|
|
|
|
matrixToUrl = this.props.permalinkCreator.forRoom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matrixToUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let title;
|
2018-06-12 13:11:43 +03:00
|
|
|
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>
|
2020-05-29 00:33:00 +03:00
|
|
|
<StyledCheckbox
|
|
|
|
checked={this.state.linkSpecificEvent}
|
|
|
|
onChange={this.onLinkSpecificEventCheckboxClick}
|
|
|
|
>
|
2018-06-12 13:11:43 +03:00
|
|
|
{ _t('Link to most recent message') }
|
2020-05-29 00:33:00 +03:00
|
|
|
</StyledCheckbox>
|
2018-06-12 13:11:43 +03:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
} else if (this.props.target instanceof User || this.props.target instanceof RoomMember) {
|
|
|
|
title = _t('Share User');
|
|
|
|
} else if (this.props.target instanceof Group) {
|
|
|
|
title = _t('Share Community');
|
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>
|
2020-05-29 00:33:00 +03:00
|
|
|
<StyledCheckbox
|
2020-08-29 03:11:08 +03:00
|
|
|
checked={this.state.linkSpecificEvent}
|
|
|
|
onClick={this.onLinkSpecificEventCheckboxClick}
|
2020-05-29 00:33:00 +03:00
|
|
|
>
|
2018-06-14 15:41:12 +03:00
|
|
|
{ _t('Link to selected message') }
|
2020-05-29 00:33:00 +03:00
|
|
|
</StyledCheckbox>
|
2018-06-14 15:41:12 +03:00
|
|
|
</div>;
|
2018-06-12 13:11:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 02:16:11 +03:00
|
|
|
const matrixToUrl = this.getUrl();
|
2018-06-12 13:11:43 +03:00
|
|
|
const encodedUrl = encodeURIComponent(matrixToUrl);
|
|
|
|
|
2020-09-16 18:06:17 +03:00
|
|
|
const showQrCode = SettingsStore.getValue(UIFeature.ShareQRCode);
|
|
|
|
const showSocials = SettingsStore.getValue(UIFeature.ShareSocial);
|
|
|
|
|
|
|
|
let qrSocialSection;
|
|
|
|
if (showQrCode || showSocials) {
|
|
|
|
qrSocialSection = <>
|
|
|
|
<hr />
|
|
|
|
<div className="mx_ShareDialog_split">
|
|
|
|
{ showQrCode && <div className="mx_ShareDialog_qrcode_container">
|
|
|
|
<QRCode data={matrixToUrl} width={256} />
|
|
|
|
</div> }
|
|
|
|
{ showSocials && <div className="mx_ShareDialog_social_container">
|
|
|
|
{ socials.map((social) => (
|
|
|
|
<a
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
target="_blank"
|
|
|
|
key={social.name}
|
|
|
|
title={social.name}
|
|
|
|
href={social.url(encodedUrl)}
|
|
|
|
className="mx_ShareDialog_social_icon"
|
|
|
|
>
|
|
|
|
<img src={social.img} alt={social.name} height={64} width={64} />
|
|
|
|
</a>
|
|
|
|
)) }
|
|
|
|
</div> }
|
|
|
|
</div>
|
|
|
|
</>;
|
|
|
|
}
|
|
|
|
|
2020-08-29 03:11:08 +03:00
|
|
|
return <BaseDialog
|
|
|
|
title={title}
|
|
|
|
className='mx_ShareDialog'
|
|
|
|
contentId='mx_Dialog_content'
|
|
|
|
onFinished={this.props.onFinished}
|
2018-06-12 13:11:43 +03:00
|
|
|
>
|
|
|
|
<div className="mx_ShareDialog_content">
|
|
|
|
<div className="mx_ShareDialog_matrixto">
|
2020-08-29 03:11:08 +03:00
|
|
|
<a
|
|
|
|
href={matrixToUrl}
|
|
|
|
onClick={ShareDialog.onLinkClick}
|
|
|
|
className="mx_ShareDialog_matrixto_link"
|
2018-06-12 13:11:43 +03:00
|
|
|
>
|
|
|
|
{ matrixToUrl }
|
|
|
|
</a>
|
2020-08-03 21:21:24 +03:00
|
|
|
<AccessibleTooltipButton
|
|
|
|
title={_t("Copy")}
|
|
|
|
onClick={this.onCopyClick}
|
|
|
|
className="mx_ShareDialog_matrixto_copy"
|
|
|
|
/>
|
2018-06-12 13:11:43 +03:00
|
|
|
</div>
|
|
|
|
{ checkbox }
|
2020-09-16 18:06:17 +03:00
|
|
|
{ qrSocialSection }
|
2018-06-12 13:11:43 +03:00
|
|
|
</div>
|
|
|
|
</BaseDialog>;
|
|
|
|
}
|
|
|
|
}
|