/* 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'); var ImageUtils = require('../../../ImageUtils'); var linkify = require('linkifyjs'); var linkifyElement = require('linkifyjs/element'); var linkifyMatrix = require('../../../linkify-matrix'); linkifyMatrix(linkify); module.exports = React.createClass({ displayName: 'LinkPreviewWidget', propTypes: { link: React.PropTypes.string.isRequired, mxEvent: React.PropTypes.object.isRequired, onWidgetLoad: React.PropTypes.func, }, getInitialState: function() { return { preview: null }; }, componentWillMount: function() { if (global.localStorage) { if (global.localStorage.getItem("hide_preview_" + this.props.mxEvent.getId()) === "1") { return; } } MatrixClientPeg.get().getUrlPreview(this.props.link, this.props.mxEvent.getTs()).then((res)=>{ this.setState({ preview: res }); this.props.onWidgetLoad(); }, (error)=>{ console.error("Failed to get preview for " + this.props.link + " " + error); }); }, componentDidMount: function() { if (this.refs.description) linkifyElement(this.refs.description, linkifyMatrix.options); }, componentDidUpdate: function() { if (this.refs.description) linkifyElement(this.refs.description, linkifyMatrix.options); }, onCancelClick: function(event) { this.setState({ preview: null }); // FIXME: persist this somewhere smarter than local storage // FIXME: add to event contextual menu ability to unhide hidden previews if (global.localStorage) { global.localStorage.setItem("hide_preview_" + this.props.mxEvent.getId(), "1"); } }, render: function() { var p = this.state.preview; if (!p) return
; // FIXME: do we want to factor out all image displaying between this and MImageBody - especially for lightboxing? 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 = } return (