UNPKG

@bigfishtv/cockpit

Version:

124 lines (101 loc) 4.49 kB
var _class, _class2, _temp; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import throttle from 'lodash/throttle'; import { post } from 'axios'; import encodeFormData from '../../utils/encodeFormData'; import windowVisible from '../../decorators/windowVisible'; // try and grab cspNonce from script in current document var cspNonce = document.scripts && document.scripts[0].nonce; var PreviewFrame = windowVisible(_class = (_temp = _class2 = function (_Component) { _inherits(PreviewFrame, _Component); function PreviewFrame() { _classCallCheck(this, PreviewFrame); var _this = _possibleConstructorReturn(this, _Component.call(this)); _this.updatePreview = throttle(_this.updatePreview, 1000); _this.state = { dirty: true }; return _this; } PreviewFrame.prototype.componentWillUnmount = function componentWillUnmount() { this.updatePreview.cancel(); }; PreviewFrame.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { var dataChanged = nextProps.data !== this.props.data || nextProps.url !== this.props.url || nextProps.date !== this.props.date; if (dataChanged && !this.state.dirty) { this.setState({ dirty: true }); } }; PreviewFrame.prototype.componentDidUpdate = function componentDidUpdate() { if (this.props.windowVisible && this.state.dirty && this.props.enabled && (this.props.url || this.props.data)) { this.updatePreview(); } }; PreviewFrame.prototype.updatePreview = function updatePreview() { var _this2 = this; this.setState({ dirty: false }); this.saveScrollPosition(); if (typeof this.props.data == 'string') { this.updateIframe(this.props.data); } else { this.requestHtml().then(function (res) { return _this2.updateIframe(res.data); }); } }; PreviewFrame.prototype.saveScrollPosition = function saveScrollPosition() { if (this.refs.frame.contentDocument) { this.scrollTop = this.refs.frame.contentDocument.defaultView.scrollY; this.scrollLeft = this.refs.frame.contentDocument.defaultView.scrollX; } else { console.warn("Couldn't save scroll position", this.refs.frame); } }; PreviewFrame.prototype.requestHtml = function requestHtml() { var data = { data: JSON.stringify(this.props.data), preview: 1, date: this.props.date, cspNonce: cspNonce }; return post(this.props.url, data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, responseType: 'text', transformRequest: [encodeFormData] }); }; PreviewFrame.prototype.updateIframe = function updateIframe(html) { html += '<script>window.scrollTo(' + this.scrollLeft + ', ' + this.scrollTop + ');</script>'; this.refs.frame.contentDocument.open(); this.refs.frame.contentDocument.write(html); this.refs.frame.contentDocument.close(); var title = html.match(/<title>(.+?)<\/title>/); if (title) { this.props.onTitleChange(title[1]); } }; PreviewFrame.prototype.render = function render() { return React.createElement( 'div', { className: 'preview-frame' }, React.createElement( 'div', { className: 'preview-frame-inner', style: { maxWidth: this.props.deviceWidth, flexBasis: this.props.deviceWidth } }, React.createElement('iframe', { className: 'preview-frame-iframe', ref: 'frame' }) ) ); }; return PreviewFrame; }(Component), _class2.propTypes = { data: PropTypes.oneOfType([PropTypes.object, PropTypes.string]), enabled: PropTypes.bool, url: PropTypes.string, date: PropTypes.string }, _temp)) || _class; export { PreviewFrame as default };