2016-03-31 20:38:01 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-12-20 03:45:24 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-03-31 20:38:01 +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';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-01-06 01:22:09 +03:00
|
|
|
import { AllHtmlEntities } from 'html-entities';
|
|
|
|
import {linkifyElement} from '../../../HtmlUtils';
|
2019-09-28 06:08:31 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2019-12-21 00:41:07 +03:00
|
|
|
import * as sdk from "../../../index";
|
|
|
|
import Modal from "../../../Modal";
|
|
|
|
import * as ImageUtils from "../../../ImageUtils";
|
2019-12-13 17:02:50 +03:00
|
|
|
import { _t } from "../../../languageHandler";
|
2016-03-31 20:38:01 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class LinkPreviewWidget extends React.Component {
|
|
|
|
static propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
link: PropTypes.string.isRequired, // the URL being previewed
|
|
|
|
mxEvent: PropTypes.object.isRequired, // the Event associated with the preview
|
|
|
|
onCancelClick: PropTypes.func, // called when the preview's cancel ('hide') button is clicked
|
2019-03-06 14:27:16 +03:00
|
|
|
onHeightChanged: PropTypes.func, // called when the preview's contents has loaded
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-03-31 20:38:01 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
2017-10-11 19:56:17 +03:00
|
|
|
preview: null,
|
2016-03-31 20:38:01 +03:00
|
|
|
};
|
|
|
|
|
2016-04-18 16:51:17 +03:00
|
|
|
this.unmounted = false;
|
2016-04-03 04:50:36 +03:00
|
|
|
MatrixClientPeg.get().getUrlPreview(this.props.link, this.props.mxEvent.getTs()).then((res)=>{
|
2016-04-18 16:51:17 +03:00
|
|
|
if (this.unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-07 20:58:50 +03:00
|
|
|
this.setState(
|
|
|
|
{ preview: res },
|
2019-03-06 14:27:16 +03:00
|
|
|
this.props.onHeightChanged,
|
2016-04-07 20:58:50 +03:00
|
|
|
);
|
2016-03-31 20:38:01 +03:00
|
|
|
}, (error)=>{
|
2019-03-22 21:50:08 +03:00
|
|
|
console.error("Failed to get URL preview: " + error);
|
2019-11-18 13:03:05 +03:00
|
|
|
});
|
2019-12-08 15:16:17 +03:00
|
|
|
|
|
|
|
this._description = createRef();
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-03-31 20:38:01 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidMount() {
|
2019-12-08 15:16:17 +03:00
|
|
|
if (this._description.current) {
|
|
|
|
linkifyElement(this._description.current);
|
2017-01-20 17:22:27 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-04-01 04:23:29 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidUpdate() {
|
2019-12-08 15:16:17 +03:00
|
|
|
if (this._description.current) {
|
|
|
|
linkifyElement(this._description.current);
|
2017-01-20 17:22:27 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-04-01 04:23:29 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentWillUnmount() {
|
2016-04-18 16:51:17 +03:00
|
|
|
this.unmounted = true;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-04-18 16:51:17 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onImageClick = ev => {
|
2017-10-11 19:56:17 +03:00
|
|
|
const p = this.state.preview;
|
2016-07-15 18:54:56 +03:00
|
|
|
if (ev.button != 0 || ev.metaKey) return;
|
|
|
|
ev.preventDefault();
|
2017-10-11 19:56:17 +03:00
|
|
|
const ImageView = sdk.getComponent("elements.ImageView");
|
2016-07-15 18:54:56 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let src = p["og:image"];
|
2016-07-15 18:54:56 +03:00
|
|
|
if (src && src.startsWith("mxc://")) {
|
|
|
|
src = MatrixClientPeg.get().mxcUrlToHttp(src);
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const params = {
|
2016-07-15 18:54:56 +03:00
|
|
|
src: src,
|
|
|
|
width: p["og:image:width"],
|
|
|
|
height: p["og:image:height"],
|
|
|
|
name: p["og:title"] || p["og:description"] || this.props.link,
|
|
|
|
fileSize: p["matrix:image:size"],
|
|
|
|
link: this.props.link,
|
|
|
|
};
|
|
|
|
|
|
|
|
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox");
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-07-15 18:54:56 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const p = this.state.preview;
|
2017-05-08 16:01:44 +03:00
|
|
|
if (!p || Object.keys(p).length === 0) {
|
2017-10-11 19:56:17 +03:00
|
|
|
return <div />;
|
2017-05-08 16:01:44 +03:00
|
|
|
}
|
2016-04-02 02:36:19 +03:00
|
|
|
|
2016-04-02 04:46:19 +03:00
|
|
|
// FIXME: do we want to factor out all image displaying between this and MImageBody - especially for lightboxing?
|
2017-10-11 19:56:17 +03:00
|
|
|
let image = p["og:image"];
|
2019-09-28 06:08:31 +03:00
|
|
|
if (!SettingsStore.getValue("showImages")) {
|
|
|
|
image = null; // Don't render a button to show the image, just hide it outright
|
|
|
|
}
|
2021-02-23 15:03:08 +03:00
|
|
|
const imageMaxWidth = 100; const imageMaxHeight = 100;
|
2016-04-02 02:36:19 +03:00
|
|
|
if (image && image.startsWith("mxc://")) {
|
|
|
|
image = MatrixClientPeg.get().mxcUrlToHttp(image, imageMaxWidth, imageMaxHeight);
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let thumbHeight = imageMaxHeight;
|
2016-04-02 02:36:19 +03:00
|
|
|
if (p["og:image:width"] && p["og:image:height"]) {
|
2020-10-30 21:18:17 +03:00
|
|
|
thumbHeight = ImageUtils.thumbHeight(
|
|
|
|
p["og:image:width"], p["og:image:height"],
|
|
|
|
imageMaxWidth, imageMaxHeight,
|
|
|
|
);
|
2016-04-02 02:36:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let img;
|
2016-04-02 02:36:19 +03:00
|
|
|
if (image) {
|
2016-07-15 18:54:56 +03:00
|
|
|
img = <div className="mx_LinkPreviewWidget_image" style={{ height: thumbHeight }}>
|
2017-10-11 19:56:17 +03:00
|
|
|
<img style={{ maxWidth: imageMaxWidth, maxHeight: imageMaxHeight }} src={image} onClick={this.onImageClick} />
|
2017-01-20 17:22:27 +03:00
|
|
|
</div>;
|
2016-04-02 02:36:19 +03:00
|
|
|
}
|
|
|
|
|
2020-01-06 03:18:24 +03:00
|
|
|
// The description includes &-encoded HTML entities, we decode those as React treats the thing as an
|
|
|
|
// opaque string. This does not allow any HTML to be injected into the DOM.
|
|
|
|
const description = AllHtmlEntities.decode(p["og:description"] || "");
|
|
|
|
|
2019-12-13 17:02:50 +03:00
|
|
|
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
|
2016-03-31 20:38:01 +03:00
|
|
|
return (
|
2020-08-19 12:38:26 +03:00
|
|
|
<div className="mx_LinkPreviewWidget">
|
2016-07-15 18:54:56 +03:00
|
|
|
{ img }
|
2016-04-01 04:16:11 +03:00
|
|
|
<div className="mx_LinkPreviewWidget_caption">
|
2020-02-24 01:14:29 +03:00
|
|
|
<div className="mx_LinkPreviewWidget_title"><a href={this.props.link} target="_blank" rel="noreferrer noopener">{ p["og:title"] }</a></div>
|
2016-07-15 18:54:56 +03:00
|
|
|
<div className="mx_LinkPreviewWidget_siteName">{ p["og:site_name"] ? (" - " + p["og:site_name"]) : null }</div>
|
2019-12-08 15:16:17 +03:00
|
|
|
<div className="mx_LinkPreviewWidget_description" ref={this._description}>
|
2020-01-06 03:18:24 +03:00
|
|
|
{ description }
|
2016-07-15 18:54:56 +03:00
|
|
|
</div>
|
2016-03-31 20:38:01 +03:00
|
|
|
</div>
|
2021-02-23 15:03:08 +03:00
|
|
|
<AccessibleButton className="mx_LinkPreviewWidget_cancel" onClick={this.props.onCancelClick} aria-label={_t("Close preview")}>
|
|
|
|
<img className="mx_filterFlipColor" alt="" role="presentation"
|
|
|
|
src={require("../../../../res/img/cancel.svg")} width="18" height="18" />
|
|
|
|
</AccessibleButton>
|
2016-03-31 20:38:01 +03:00
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|