2016-03-31 20:38:01 +03: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';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
|
|
|
|
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
2016-04-02 02:36:19 +03:00
|
|
|
var ImageUtils = require('../../../ImageUtils');
|
2016-03-31 20:38:01 +03:00
|
|
|
|
2016-04-01 04:23:29 +03:00
|
|
|
var linkify = require('linkifyjs');
|
|
|
|
var linkifyElement = require('linkifyjs/element');
|
|
|
|
var linkifyMatrix = require('../../../linkify-matrix');
|
|
|
|
linkifyMatrix(linkify);
|
|
|
|
|
2016-03-31 20:38:01 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'LinkPreviewWidget',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-04-02 02:36:19 +03:00
|
|
|
link: React.PropTypes.string.isRequired,
|
2016-04-03 04:50:36 +03:00
|
|
|
mxEvent: React.PropTypes.object.isRequired,
|
2016-04-04 01:30:48 +03:00
|
|
|
onCancelClick: React.PropTypes.func,
|
2016-04-02 02:36:19 +03:00
|
|
|
onWidgetLoad: React.PropTypes.func,
|
2016-03-31 20:38:01 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-04-01 04:23:29 +03:00
|
|
|
preview: null
|
2016-03-31 20:38:01 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
2016-04-03 04:50:36 +03:00
|
|
|
MatrixClientPeg.get().getUrlPreview(this.props.link, this.props.mxEvent.getTs()).then((res)=>{
|
2016-03-31 20:38:01 +03:00
|
|
|
this.setState({ preview: res });
|
2016-04-02 02:36:19 +03:00
|
|
|
this.props.onWidgetLoad();
|
2016-03-31 20:38:01 +03:00
|
|
|
}, (error)=>{
|
2016-04-01 04:16:11 +03:00
|
|
|
console.error("Failed to get preview for " + this.props.link + " " + error);
|
2016-03-31 20:38:01 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-04-01 04:23:29 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
if (this.refs.description)
|
|
|
|
linkifyElement(this.refs.description, linkifyMatrix.options);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate: function() {
|
|
|
|
if (this.refs.description)
|
|
|
|
linkifyElement(this.refs.description, linkifyMatrix.options);
|
|
|
|
},
|
|
|
|
|
2016-03-31 20:38:01 +03:00
|
|
|
render: function() {
|
|
|
|
var p = this.state.preview;
|
2016-04-01 04:16:11 +03:00
|
|
|
if (!p) return <div/>;
|
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?
|
2016-04-02 02:36:19 +03:00
|
|
|
var image = p["og:image"];
|
|
|
|
var imageMaxWidth = 100, imageMaxHeight = 100;
|
|
|
|
if (image && image.startsWith("mxc://")) {
|
|
|
|
image = MatrixClientPeg.get().mxcUrlToHttp(image, imageMaxWidth, imageMaxHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbHeight = imageMaxHeight;
|
|
|
|
if (p["og:image:width"] && p["og:image:height"]) {
|
|
|
|
thumbHeight = ImageUtils.thumbHeight(p["og:image:width"], p["og:image:height"], imageMaxWidth, imageMaxHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
var img;
|
|
|
|
if (image) {
|
|
|
|
img = <div className="mx_LinkPreviewWidget_image" style={{ height: thumbHeight }}>
|
|
|
|
<a href={ this.props.link } target="_blank"><img style={{ maxWidth: imageMaxWidth, maxHeight: imageMaxHeight }} src={ image }/></a>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
|
2016-03-31 20:38:01 +03:00
|
|
|
return (
|
2016-04-02 02:36:19 +03:00
|
|
|
<div className="mx_LinkPreviewWidget" >
|
|
|
|
{ img }
|
2016-04-01 04:16:11 +03:00
|
|
|
<div className="mx_LinkPreviewWidget_caption">
|
|
|
|
<div className="mx_LinkPreviewWidget_title"><a href={ this.props.link } target="_blank">{ p["og:title"] }</a></div>
|
|
|
|
<div className="mx_LinkPreviewWidget_siteName">{ p["og:site_name"] ? (" - " + p["og:site_name"]) : null }</div>
|
2016-04-01 04:23:29 +03:00
|
|
|
<div className="mx_LinkPreviewWidget_description" ref="description">
|
2016-04-01 04:16:11 +03:00
|
|
|
{ p["og:description"] }
|
|
|
|
</div>
|
2016-03-31 20:38:01 +03:00
|
|
|
</div>
|
2016-04-03 04:50:36 +03:00
|
|
|
<img className="mx_LinkPreviewWidget_cancel" src="img/cancel.svg" width="18" height="18"
|
2016-04-04 01:30:48 +03:00
|
|
|
onClick={ this.props.onCancelClick }/>
|
2016-03-31 20:38:01 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|