2016-03-31 17:38:01 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const React = require('react');
|
2016-03-31 17:38:01 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const sdk = require('../../../index');
|
|
|
|
const MatrixClientPeg = require('../../../MatrixClientPeg');
|
|
|
|
const ImageUtils = require('../../../ImageUtils');
|
|
|
|
const Modal = require('../../../Modal');
|
2016-03-31 17:38:01 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const linkify = require('linkifyjs');
|
|
|
|
const linkifyElement = require('linkifyjs/element');
|
|
|
|
const linkifyMatrix = require('../../../linkify-matrix');
|
2016-04-01 01:23:29 +00:00
|
|
|
linkifyMatrix(linkify);
|
|
|
|
|
2016-03-31 17:38:01 +00:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'LinkPreviewWidget',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-04-07 17:58:50 +00:00
|
|
|
link: React.PropTypes.string.isRequired, // the URL being previewed
|
|
|
|
mxEvent: React.PropTypes.object.isRequired, // the Event associated with the preview
|
|
|
|
onCancelClick: React.PropTypes.func, // called when the preview's cancel ('hide') button is clicked
|
|
|
|
onWidgetLoad: React.PropTypes.func, // called when the preview's contents has loaded
|
2016-03-31 17:38:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2017-10-11 16:56:17 +00:00
|
|
|
preview: null,
|
2016-03-31 17:38:01 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
2016-04-18 13:51:17 +00:00
|
|
|
this.unmounted = false;
|
2016-04-03 01:50:36 +00:00
|
|
|
MatrixClientPeg.get().getUrlPreview(this.props.link, this.props.mxEvent.getTs()).then((res)=>{
|
2016-04-18 13:51:17 +00:00
|
|
|
if (this.unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-07 17:58:50 +00:00
|
|
|
this.setState(
|
|
|
|
{ preview: res },
|
2017-10-11 16:56:17 +00:00
|
|
|
this.props.onWidgetLoad,
|
2016-04-07 17:58:50 +00:00
|
|
|
);
|
2016-03-31 17:38:01 +00:00
|
|
|
}, (error)=>{
|
2016-04-01 01:16:11 +00:00
|
|
|
console.error("Failed to get preview for " + this.props.link + " " + error);
|
2016-04-18 13:51:17 +00:00
|
|
|
}).done();
|
2016-03-31 17:38:01 +00:00
|
|
|
},
|
|
|
|
|
2016-04-01 01:23:29 +00:00
|
|
|
componentDidMount: function() {
|
2017-01-20 14:22:27 +00:00
|
|
|
if (this.refs.description) {
|
2016-04-01 01:23:29 +00:00
|
|
|
linkifyElement(this.refs.description, linkifyMatrix.options);
|
2017-01-20 14:22:27 +00:00
|
|
|
}
|
2016-04-01 01:23:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate: function() {
|
2017-01-20 14:22:27 +00:00
|
|
|
if (this.refs.description) {
|
2016-04-01 01:23:29 +00:00
|
|
|
linkifyElement(this.refs.description, linkifyMatrix.options);
|
2017-01-20 14:22:27 +00:00
|
|
|
}
|
2016-04-01 01:23:29 +00:00
|
|
|
},
|
|
|
|
|
2016-04-18 13:51:17 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
this.unmounted = true;
|
|
|
|
},
|
|
|
|
|
2016-07-15 15:54:56 +00:00
|
|
|
onImageClick: function(ev) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const p = this.state.preview;
|
2016-07-15 15:54:56 +00:00
|
|
|
if (ev.button != 0 || ev.metaKey) return;
|
|
|
|
ev.preventDefault();
|
2017-10-11 16:56:17 +00:00
|
|
|
const ImageView = sdk.getComponent("elements.ImageView");
|
2016-07-15 15:54:56 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let src = p["og:image"];
|
2016-07-15 15:54:56 +00:00
|
|
|
if (src && src.startsWith("mxc://")) {
|
|
|
|
src = MatrixClientPeg.get().mxcUrlToHttp(src);
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const params = {
|
2016-07-15 15:54:56 +00: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");
|
|
|
|
},
|
|
|
|
|
2016-03-31 17:38:01 +00:00
|
|
|
render: function() {
|
2017-10-11 16:56:17 +00:00
|
|
|
const p = this.state.preview;
|
2017-05-08 13:01:44 +00:00
|
|
|
if (!p || Object.keys(p).length === 0) {
|
2017-10-11 16:56:17 +00:00
|
|
|
return <div />;
|
2017-05-08 13:01:44 +00:00
|
|
|
}
|
2016-04-01 23:36:19 +00:00
|
|
|
|
2016-04-02 01:46:19 +00:00
|
|
|
// FIXME: do we want to factor out all image displaying between this and MImageBody - especially for lightboxing?
|
2017-10-11 16:56:17 +00:00
|
|
|
let image = p["og:image"];
|
|
|
|
let imageMaxWidth = 100, imageMaxHeight = 100;
|
2016-04-01 23:36:19 +00:00
|
|
|
if (image && image.startsWith("mxc://")) {
|
|
|
|
image = MatrixClientPeg.get().mxcUrlToHttp(image, imageMaxWidth, imageMaxHeight);
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let thumbHeight = imageMaxHeight;
|
2016-04-01 23:36:19 +00:00
|
|
|
if (p["og:image:width"] && p["og:image:height"]) {
|
|
|
|
thumbHeight = ImageUtils.thumbHeight(p["og:image:width"], p["og:image:height"], imageMaxWidth, imageMaxHeight);
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let img;
|
2016-04-01 23:36:19 +00:00
|
|
|
if (image) {
|
2016-07-15 15:54:56 +00:00
|
|
|
img = <div className="mx_LinkPreviewWidget_image" style={{ height: thumbHeight }}>
|
2017-10-11 16:56:17 +00:00
|
|
|
<img style={{ maxWidth: imageMaxWidth, maxHeight: imageMaxHeight }} src={image} onClick={this.onImageClick} />
|
2017-01-20 14:22:27 +00:00
|
|
|
</div>;
|
2016-04-01 23:36:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:38:01 +00:00
|
|
|
return (
|
2016-04-01 23:36:19 +00:00
|
|
|
<div className="mx_LinkPreviewWidget" >
|
2016-07-15 15:54:56 +00:00
|
|
|
{ img }
|
2016-04-01 01:16:11 +00:00
|
|
|
<div className="mx_LinkPreviewWidget_caption">
|
2017-10-11 16:56:17 +00:00
|
|
|
<div className="mx_LinkPreviewWidget_title"><a href={this.props.link} target="_blank" rel="noopener">{ p["og:title"] }</a></div>
|
2016-07-15 15:54:56 +00:00
|
|
|
<div className="mx_LinkPreviewWidget_siteName">{ p["og:site_name"] ? (" - " + p["og:site_name"]) : null }</div>
|
|
|
|
<div className="mx_LinkPreviewWidget_description" ref="description">
|
|
|
|
{ p["og:description"] }
|
|
|
|
</div>
|
2016-03-31 17:38:01 +00:00
|
|
|
</div>
|
2016-07-15 15:54:56 +00:00
|
|
|
<img className="mx_LinkPreviewWidget_cancel" src="img/cancel.svg" width="18" height="18"
|
2017-10-11 16:56:17 +00:00
|
|
|
onClick={this.props.onCancelClick} />
|
2016-03-31 17:38:01 +00:00
|
|
|
</div>
|
|
|
|
);
|
2017-10-11 16:56:17 +00:00
|
|
|
},
|
2016-03-31 17:38:01 +00:00
|
|
|
});
|