2018-04-12 01:58:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2019-02-07 13:33:03 +03:00
|
|
|
Copyright 2019 New Vector Ltd
|
2018-04-12 01:58:04 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
2018-04-18 12:23:32 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2018-04-12 01:58:04 +03:00
|
|
|
import request from 'browser-request';
|
2018-04-13 02:43:44 +03:00
|
|
|
import { _t } from '../../languageHandler';
|
2018-04-12 01:58:04 +03:00
|
|
|
import sanitizeHtml from 'sanitize-html';
|
2019-06-05 01:08:32 +03:00
|
|
|
import dis from '../../dispatcher';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../MatrixClientPeg';
|
2019-02-02 00:33:05 +03:00
|
|
|
import classnames from 'classnames';
|
2019-12-17 20:26:12 +03:00
|
|
|
import MatrixClientContext from "../../contexts/MatrixClientContext";
|
2020-03-14 02:39:42 +03:00
|
|
|
import AutoHideScrollbar from "./AutoHideScrollbar";
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-02-07 14:12:28 +03:00
|
|
|
export default class EmbeddedPage extends React.PureComponent {
|
2018-04-18 12:23:32 +03:00
|
|
|
static propTypes = {
|
2019-02-07 14:12:28 +03:00
|
|
|
// URL to request embedded page content from
|
|
|
|
url: PropTypes.string,
|
|
|
|
// Class name prefix to apply for a given instance
|
|
|
|
className: PropTypes.string,
|
2019-02-07 19:25:09 +03:00
|
|
|
// Whether to wrap the page in a scrollbar
|
|
|
|
scrollbar: PropTypes.bool,
|
2020-03-27 17:02:32 +03:00
|
|
|
// Map of keys to replace with values, e.g {$placeholder: "value"}
|
|
|
|
replaceMap: PropTypes.object,
|
2018-04-18 12:23:32 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-12-17 20:26:12 +03:00
|
|
|
static contextType = MatrixClientContext;
|
2018-11-05 13:25:42 +03:00
|
|
|
|
2019-02-07 13:33:03 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2019-06-10 17:27:59 +03:00
|
|
|
this._dispatcherRef = null;
|
|
|
|
|
2019-02-07 13:33:03 +03:00
|
|
|
this.state = {
|
2018-04-12 01:58:04 +03:00
|
|
|
page: '',
|
2019-02-07 13:33:03 +03:00
|
|
|
};
|
|
|
|
}
|
2018-04-18 12:23:32 +03:00
|
|
|
|
|
|
|
translate(s) {
|
|
|
|
// default implementation - skins may wish to extend this
|
|
|
|
return sanitizeHtml(_t(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2018-04-12 01:58:04 +03:00
|
|
|
this._unmounted = false;
|
|
|
|
|
2019-02-07 19:31:44 +03:00
|
|
|
if (!this.props.url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:32:33 +03:00
|
|
|
// we use request() to inline the page into the react component
|
2019-01-26 01:10:54 +03:00
|
|
|
// so that it can inherit CSS and theming easily rather than mess around
|
|
|
|
// with iframes and trying to synchronise document.stylesheets.
|
|
|
|
|
|
|
|
request(
|
2019-02-07 14:12:28 +03:00
|
|
|
{ method: "GET", url: this.props.url },
|
2019-01-26 01:10:54 +03:00
|
|
|
(err, response, body) => {
|
|
|
|
if (this._unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err || response.status < 200 || response.status >= 300) {
|
2019-02-07 19:32:33 +03:00
|
|
|
console.warn(`Error loading page: ${err}`);
|
|
|
|
this.setState({ page: _t("Couldn't load page") });
|
2019-01-26 01:10:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
body = body.replace(/_t\(['"]([\s\S]*?)['"]\)/mg, (match, g1)=>this.translate(g1));
|
2020-03-27 17:02:32 +03:00
|
|
|
|
|
|
|
if (this.props.replaceMap) {
|
|
|
|
Object.keys(this.props.replaceMap).forEach(key => {
|
|
|
|
body = body.split(key).join(this.props.replaceMap[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:10:54 +03:00
|
|
|
this.setState({ page: body });
|
|
|
|
},
|
|
|
|
);
|
2019-06-05 01:08:32 +03:00
|
|
|
|
|
|
|
this._dispatcherRef = dis.register(this.onAction);
|
2018-04-18 12:23:32 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-04-18 12:23:32 +03:00
|
|
|
componentWillUnmount() {
|
2018-04-12 01:58:04 +03:00
|
|
|
this._unmounted = true;
|
2019-06-10 17:27:59 +03:00
|
|
|
if (this._dispatcherRef !== null) dis.unregister(this._dispatcherRef);
|
2018-04-18 12:23:32 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-06-05 01:08:32 +03:00
|
|
|
onAction = (payload) => {
|
|
|
|
// HACK: Workaround for the context's MatrixClient not being set up at render time.
|
|
|
|
if (payload.action === 'client_started') {
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-18 12:23:32 +03:00
|
|
|
render() {
|
2019-06-05 01:08:32 +03:00
|
|
|
// HACK: Workaround for the context's MatrixClient not updating.
|
2019-12-17 20:26:12 +03:00
|
|
|
const client = this.context || MatrixClientPeg.get();
|
2019-02-07 19:25:09 +03:00
|
|
|
const isGuest = client ? client.isGuest() : true;
|
2019-02-07 14:12:28 +03:00
|
|
|
const className = this.props.className;
|
2019-02-02 00:33:05 +03:00
|
|
|
const classes = classnames({
|
2019-02-07 14:12:28 +03:00
|
|
|
[className]: true,
|
|
|
|
[`${className}_guest`]: isGuest,
|
2019-06-05 01:08:32 +03:00
|
|
|
[`${className}_loggedIn`]: !!client,
|
2019-02-02 00:33:05 +03:00
|
|
|
});
|
|
|
|
|
2019-02-07 19:25:09 +03:00
|
|
|
const content = <div className={`${className}_body`}
|
|
|
|
dangerouslySetInnerHTML={{ __html: this.state.page }}
|
|
|
|
>
|
|
|
|
</div>;
|
|
|
|
|
|
|
|
if (this.props.scrollbar) {
|
2020-03-14 02:39:42 +03:00
|
|
|
return <AutoHideScrollbar className={classes}>
|
2019-02-07 19:25:09 +03:00
|
|
|
{content}
|
2020-03-14 02:39:42 +03:00
|
|
|
</AutoHideScrollbar>;
|
2019-02-07 19:25:09 +03:00
|
|
|
} else {
|
|
|
|
return <div className={classes}>
|
|
|
|
{content}
|
|
|
|
</div>;
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
2018-04-18 12:23:32 +03:00
|
|
|
}
|