2015-11-27 18:02:32 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-12-10 15:50:41 +03:00
|
|
|
Copyright 2017 New Vector Ltd
|
2019-05-22 17:16:32 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2015-11-27 18:02:32 +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.
|
|
|
|
*/
|
|
|
|
|
2019-12-08 15:16:17 +03:00
|
|
|
import React, {createRef} from 'react';
|
2017-04-06 16:08:59 +03:00
|
|
|
import ReactDOM from 'react-dom';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-04-06 16:08:59 +03:00
|
|
|
import highlight from 'highlight.js';
|
|
|
|
import * as HtmlUtils from '../../../HtmlUtils';
|
2019-05-17 13:36:36 +03:00
|
|
|
import {formatDate} from '../../../DateUtils';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2017-04-06 16:08:59 +03:00
|
|
|
import Modal from '../../../Modal';
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from '../../../dispatcher/dispatcher';
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-12-03 13:53:32 +03:00
|
|
|
import * as ContextMenu from '../../structures/ContextMenu';
|
2017-10-29 10:43:52 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2018-03-04 15:39:34 +03:00
|
|
|
import ReplyThread from "../elements/ReplyThread";
|
2020-02-23 02:51:30 +03:00
|
|
|
import {pillifyLinks, unmountPills} from '../../../utils/pillify';
|
2019-08-10 01:05:05 +03:00
|
|
|
import {IntegrationManagers} from "../../../integrations/IntegrationManagers";
|
2019-10-01 05:39:58 +03:00
|
|
|
import {isPermalinkHost} from "../../../utils/permalinks/Permalinks";
|
2019-12-03 02:21:59 +03:00
|
|
|
import {toRightOf} from "../../structures/ContextMenu";
|
2020-04-15 02:16:11 +03:00
|
|
|
import {copyPlaintext} from "../../../utils/strings";
|
2020-07-14 01:14:00 +03:00
|
|
|
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class TextualBody extends React.Component {
|
|
|
|
static propTypes = {
|
2016-02-17 22:50:04 +03:00
|
|
|
/* the MatrixEvent to show */
|
2017-12-26 04:03:18 +03:00
|
|
|
mxEvent: PropTypes.object.isRequired,
|
2016-02-17 22:50:04 +03:00
|
|
|
|
|
|
|
/* a list of words to highlight */
|
2017-12-26 04:03:18 +03:00
|
|
|
highlights: PropTypes.array,
|
2016-02-17 22:50:04 +03:00
|
|
|
|
|
|
|
/* link URL for the highlights */
|
2017-12-26 04:03:18 +03:00
|
|
|
highlightLink: PropTypes.string,
|
2016-04-02 02:36:19 +03:00
|
|
|
|
2016-07-18 03:35:42 +03:00
|
|
|
/* should show URL previews for this event */
|
2017-12-26 04:03:18 +03:00
|
|
|
showUrlPreview: PropTypes.bool,
|
2016-07-18 03:35:42 +03:00
|
|
|
|
2016-04-02 02:36:19 +03:00
|
|
|
/* callback for when our widget has loaded */
|
2019-03-06 14:27:16 +03:00
|
|
|
onHeightChanged: PropTypes.func,
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2018-01-10 14:51:48 +03:00
|
|
|
/* the shape of the tile, used */
|
2018-01-10 14:54:58 +03:00
|
|
|
tileShape: PropTypes.string,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-02-17 22:50:04 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this._content = createRef();
|
|
|
|
|
|
|
|
this.state = {
|
2016-05-24 02:54:20 +03:00
|
|
|
// the URLs (if any) to be previewed with a LinkPreviewWidget
|
2016-04-08 22:21:12 +03:00
|
|
|
// inside this TextualBody.
|
2016-05-31 21:42:00 +03:00
|
|
|
links: [],
|
2016-04-04 01:30:48 +03:00
|
|
|
|
|
|
|
// track whether the preview widget is hidden
|
|
|
|
widgetHidden: false,
|
2016-03-31 20:38:01 +03:00
|
|
|
};
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-12-08 15:16:17 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidMount() {
|
2016-10-26 20:41:28 +03:00
|
|
|
this._unmounted = false;
|
2020-02-23 02:51:30 +03:00
|
|
|
this._pills = [];
|
2019-06-12 19:52:34 +03:00
|
|
|
if (!this.props.editState) {
|
2019-05-17 18:01:30 +03:00
|
|
|
this._applyFormatting();
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-10-26 20:41:28 +03:00
|
|
|
|
2019-05-15 17:55:03 +03:00
|
|
|
_applyFormatting() {
|
2021-01-21 15:08:55 +03:00
|
|
|
const showLineNumbers = SettingsStore.getValue("showCodeLineNumbers");
|
2019-12-08 04:01:19 +03:00
|
|
|
this.activateSpoilers([this._content.current]);
|
2019-05-22 21:41:27 +03:00
|
|
|
|
2017-07-17 16:50:45 +03:00
|
|
|
// pillifyLinks BEFORE linkifyElement because plain room/user URLs in the composer
|
|
|
|
// are still sent as plaintext URLs. If these are ever pillified in the composer,
|
|
|
|
// we should be pillify them here by doing the linkifying BEFORE the pillifying.
|
2020-02-23 02:51:30 +03:00
|
|
|
pillifyLinks([this._content.current], this.props.mxEvent, this._pills);
|
2019-12-08 15:16:17 +03:00
|
|
|
HtmlUtils.linkifyElement(this._content.current);
|
2016-07-20 14:03:13 +03:00
|
|
|
this.calculateUrlPreview();
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2016-10-26 20:41:28 +03:00
|
|
|
if (this.props.mxEvent.getContent().format === "org.matrix.custom.html") {
|
2021-01-20 10:44:32 +03:00
|
|
|
// Handle expansion and add buttons
|
|
|
|
const pres = ReactDOM.findDOMNode(this).getElementsByTagName("pre");
|
|
|
|
if (pres.length > 0) {
|
|
|
|
for (let i = 0; i < pres.length; i++) {
|
2021-02-09 21:46:30 +03:00
|
|
|
// If there already is a div wrapping the codeblock we want to skip this.
|
|
|
|
// This happens after the codeblock was edited.
|
|
|
|
if (pres[i].parentNode.className == "mx_EventTile_pre_container") continue;
|
2021-02-05 10:22:02 +03:00
|
|
|
// Wrap a div around <pre> so that the copy button can be correctly positioned
|
|
|
|
// when the <pre> overflows and is scrolled horizontally.
|
2021-01-20 10:44:32 +03:00
|
|
|
const div = this._wrapInDiv(pres[i]);
|
|
|
|
this._handleCodeBlockExpansion(pres[i]);
|
2021-01-20 15:03:05 +03:00
|
|
|
this._addCodeExpansionButton(div, pres[i]);
|
2021-01-20 17:01:23 +03:00
|
|
|
this._addCodeCopyButton(div);
|
2021-01-21 15:08:55 +03:00
|
|
|
if (showLineNumbers) {
|
|
|
|
this._addLineNumbers(pres[i]);
|
|
|
|
}
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
2021-01-20 10:44:32 +03:00
|
|
|
}
|
|
|
|
// Highlight code
|
|
|
|
const codes = ReactDOM.findDOMNode(this).getElementsByTagName("code");
|
2021-02-04 19:58:21 +03:00
|
|
|
if (codes.length > 0) {
|
2021-01-20 10:44:32 +03:00
|
|
|
for (let i = 0; i < codes.length; i++) {
|
2021-02-05 10:30:54 +03:00
|
|
|
// Do this asynchronously: parsing code takes time and we don't
|
|
|
|
// need to block the DOM update on it.
|
2021-01-20 10:44:32 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
if (this._unmounted) return;
|
2021-01-20 15:05:23 +03:00
|
|
|
for (let i = 0; i < pres.length; i++) {
|
|
|
|
this._highlightCode(codes[i]);
|
|
|
|
}
|
2021-01-20 10:44:32 +03:00
|
|
|
}, 10);
|
|
|
|
}
|
2016-10-26 20:41:28 +03:00
|
|
|
}
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:03:05 +03:00
|
|
|
_addCodeExpansionButton(div, pre) {
|
2021-02-05 10:30:54 +03:00
|
|
|
// Calculate how many percent does the pre element take up.
|
|
|
|
// If it's less than 30% we don't add the expansion button.
|
2021-01-20 17:01:23 +03:00
|
|
|
const percentageOfViewport = pre.offsetHeight / window.innerHeight * 100;
|
|
|
|
if (percentageOfViewport < 30) return;
|
2021-01-19 23:02:39 +03:00
|
|
|
|
|
|
|
const button = document.createElement("span");
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className = "mx_EventTile_button ";
|
2021-01-19 23:02:39 +03:00
|
|
|
if (pre.className == "mx_EventTile_collapsedCodeBlock") {
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className += "mx_EventTile_expandButton";
|
2021-01-19 23:02:39 +03:00
|
|
|
} else {
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className += "mx_EventTile_collapseButton";
|
2021-01-19 23:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
button.onclick = async () => {
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className = "mx_EventTile_button ";
|
2021-01-19 23:02:39 +03:00
|
|
|
if (pre.className == "mx_EventTile_collapsedCodeBlock") {
|
|
|
|
pre.className = "";
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className += "mx_EventTile_collapseButton";
|
2021-01-19 23:02:39 +03:00
|
|
|
} else {
|
|
|
|
pre.className = "mx_EventTile_collapsedCodeBlock";
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className += "mx_EventTile_expandButton";
|
2021-01-19 23:02:39 +03:00
|
|
|
}
|
2021-02-05 10:29:24 +03:00
|
|
|
|
|
|
|
// By expanding/collapsing we changed
|
|
|
|
// the height, therefore we call this
|
|
|
|
this.props.onHeightChanged();
|
2021-01-19 23:02:39 +03:00
|
|
|
};
|
|
|
|
|
2021-01-20 10:44:32 +03:00
|
|
|
div.appendChild(button);
|
2021-01-19 23:02:39 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 10:44:32 +03:00
|
|
|
_addCodeCopyButton(div) {
|
2021-01-19 18:35:32 +03:00
|
|
|
const button = document.createElement("span");
|
2021-01-20 17:01:23 +03:00
|
|
|
button.className = "mx_EventTile_button mx_EventTile_copyButton ";
|
|
|
|
|
2021-02-05 10:30:54 +03:00
|
|
|
// Check if expansion button exists. If so
|
|
|
|
// we put the copy button to the bottom
|
2021-01-20 17:01:23 +03:00
|
|
|
const expansionButtonExists = div.getElementsByClassName("mx_EventTile_button");
|
|
|
|
if (expansionButtonExists.length > 0) button.className += "mx_EventTile_buttonBottom";
|
|
|
|
|
2021-01-19 18:35:32 +03:00
|
|
|
button.onclick = async () => {
|
2021-01-21 12:53:18 +03:00
|
|
|
const copyCode = button.parentNode.getElementsByTagName("code")[0];
|
2021-01-19 18:35:32 +03:00
|
|
|
const successful = await copyPlaintext(copyCode.textContent);
|
|
|
|
|
|
|
|
const buttonRect = button.getBoundingClientRect();
|
|
|
|
const GenericTextContextMenu = sdk.getComponent('context_menus.GenericTextContextMenu');
|
|
|
|
const {close} = ContextMenu.createMenu(GenericTextContextMenu, {
|
|
|
|
...toRightOf(buttonRect, 2),
|
|
|
|
message: successful ? _t('Copied!') : _t('Failed to copy'),
|
|
|
|
});
|
|
|
|
button.onmouseleave = close;
|
|
|
|
};
|
|
|
|
|
2021-01-20 10:44:32 +03:00
|
|
|
div.appendChild(button);
|
2021-01-19 20:21:59 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 10:44:32 +03:00
|
|
|
_wrapInDiv(pre) {
|
2021-01-19 18:35:32 +03:00
|
|
|
const div = document.createElement("div");
|
|
|
|
div.className = "mx_EventTile_pre_container";
|
|
|
|
|
|
|
|
// Insert containing div in place of <pre> block
|
2021-01-20 10:44:32 +03:00
|
|
|
pre.parentNode.replaceChild(div, pre);
|
2021-01-19 18:35:32 +03:00
|
|
|
// Append <pre> block and copy button to container
|
2021-01-20 10:44:32 +03:00
|
|
|
div.appendChild(pre);
|
2021-01-19 20:21:59 +03:00
|
|
|
|
|
|
|
return div;
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 10:44:32 +03:00
|
|
|
_handleCodeBlockExpansion(pre) {
|
2021-01-19 19:30:02 +03:00
|
|
|
if (!SettingsStore.getValue("expandCodeByDefault")) {
|
2021-01-20 10:44:32 +03:00
|
|
|
pre.className = "mx_EventTile_collapsedCodeBlock";
|
2021-01-19 19:30:02 +03:00
|
|
|
}
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 12:53:18 +03:00
|
|
|
_addLineNumbers(pre) {
|
2021-01-21 15:04:51 +03:00
|
|
|
pre.innerHTML = '<span class="mx_EventTile_lineNumbers"></span>' + pre.innerHTML + '<span></span>';
|
2021-01-21 12:53:18 +03:00
|
|
|
const lineNumbers = pre.getElementsByClassName("mx_EventTile_lineNumbers")[0];
|
|
|
|
// Calculate number of lines in pre
|
|
|
|
const number = pre.innerHTML.split(/\n/).length;
|
|
|
|
// Iterate through lines starting with 1 (number of the first line is 1)
|
|
|
|
for (let i = 1; i < number; i++) {
|
|
|
|
lineNumbers.innerHTML += '<span class="mx_EventTile_lineNumber">' + i + '</span>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 10:44:32 +03:00
|
|
|
_highlightCode(code) {
|
2021-01-19 18:35:32 +03:00
|
|
|
if (SettingsStore.getValue("enableSyntaxHighlightLanguageDetection")) {
|
2021-01-20 10:44:32 +03:00
|
|
|
highlight.highlightBlock(code);
|
2021-01-19 18:35:32 +03:00
|
|
|
} else {
|
|
|
|
// Only syntax highlight if there's a class starting with language-
|
2021-01-20 10:44:32 +03:00
|
|
|
const classes = code.className.split(/\s+/).filter(function(cl) {
|
2021-01-19 18:35:32 +03:00
|
|
|
return cl.startsWith('language-') && !cl.startsWith('language-_');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (classes.length != 0) {
|
2021-01-20 10:44:32 +03:00
|
|
|
highlight.highlightBlock(code);
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
2016-10-26 20:41:28 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-07-20 14:03:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
2019-06-12 19:52:34 +03:00
|
|
|
if (!this.props.editState) {
|
|
|
|
const stoppedEditing = prevProps.editState && !this.props.editState;
|
2019-05-17 18:01:30 +03:00
|
|
|
const messageWasEdited = prevProps.replacingEventId !== this.props.replacingEventId;
|
2019-05-20 11:19:29 +03:00
|
|
|
if (messageWasEdited || stoppedEditing) {
|
2019-05-17 18:01:30 +03:00
|
|
|
this._applyFormatting();
|
|
|
|
}
|
2019-05-15 17:55:03 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-07-20 14:03:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentWillUnmount() {
|
2016-10-26 20:41:28 +03:00
|
|
|
this._unmounted = true;
|
2020-02-23 02:51:30 +03:00
|
|
|
unmountPills(this._pills);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-10-26 20:41:28 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-11-26 04:14:03 +03:00
|
|
|
//console.info("shouldComponentUpdate: ShowUrlPreview for %s is %s", this.props.mxEvent.getId(), this.props.showUrlPreview);
|
2016-07-20 14:03:13 +03:00
|
|
|
|
|
|
|
// exploit that events are immutable :)
|
|
|
|
return (nextProps.mxEvent.getId() !== this.props.mxEvent.getId() ||
|
|
|
|
nextProps.highlights !== this.props.highlights ||
|
2019-05-15 13:49:43 +03:00
|
|
|
nextProps.replacingEventId !== this.props.replacingEventId ||
|
2016-07-20 14:03:13 +03:00
|
|
|
nextProps.highlightLink !== this.props.highlightLink ||
|
|
|
|
nextProps.showUrlPreview !== this.props.showUrlPreview ||
|
2019-06-12 19:52:34 +03:00
|
|
|
nextProps.editState !== this.props.editState ||
|
2016-07-20 14:03:13 +03:00
|
|
|
nextState.links !== this.state.links ||
|
|
|
|
nextState.widgetHidden !== this.state.widgetHidden);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-07-20 14:03:13 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
calculateUrlPreview() {
|
2019-11-26 04:14:03 +03:00
|
|
|
//console.info("calculateUrlPreview: ShowUrlPreview for %s is %s", this.props.mxEvent.getId(), this.props.showUrlPreview);
|
2016-07-20 14:03:13 +03:00
|
|
|
|
2019-05-21 17:56:26 +03:00
|
|
|
if (this.props.showUrlPreview) {
|
2019-12-08 04:01:19 +03:00
|
|
|
// pass only the first child which is the event tile otherwise this recurses on edited events
|
|
|
|
let links = this.findLinks([this._content.current]);
|
2016-07-18 03:35:42 +03:00
|
|
|
if (links.length) {
|
2017-07-06 12:47:15 +03:00
|
|
|
// de-dup the links (but preserve ordering)
|
2017-07-06 12:02:25 +03:00
|
|
|
const seen = new Set();
|
|
|
|
links = links.filter((link) => {
|
|
|
|
if (seen.has(link)) return false;
|
|
|
|
seen.add(link);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({ links: links });
|
2016-04-04 01:30:48 +03:00
|
|
|
|
2016-07-18 03:35:42 +03:00
|
|
|
// lazy-load the hidden state of the preview widget from localstorage
|
|
|
|
if (global.localStorage) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const hidden = global.localStorage.getItem("hide_preview_" + this.props.mxEvent.getId());
|
2016-07-18 03:35:42 +03:00
|
|
|
this.setState({ widgetHidden: hidden });
|
|
|
|
}
|
2020-08-19 12:38:26 +03:00
|
|
|
} else if (this.state.links.length) {
|
|
|
|
this.setState({ links: [] });
|
2016-04-04 01:30:48 +03:00
|
|
|
}
|
2016-03-31 20:38:01 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-04-04 01:30:48 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
activateSpoilers(nodes) {
|
2019-05-22 21:41:27 +03:00
|
|
|
let node = nodes[0];
|
|
|
|
while (node) {
|
|
|
|
if (node.tagName === "SPAN" && typeof node.getAttribute("data-mx-spoiler") === "string") {
|
|
|
|
const spoilerContainer = document.createElement('span');
|
|
|
|
|
|
|
|
const reason = node.getAttribute("data-mx-spoiler");
|
|
|
|
const Spoiler = sdk.getComponent('elements.Spoiler');
|
|
|
|
node.removeAttribute("data-mx-spoiler"); // we don't want to recurse
|
|
|
|
const spoiler = <Spoiler
|
|
|
|
reason={reason}
|
|
|
|
contentHtml={node.outerHTML}
|
|
|
|
/>;
|
|
|
|
|
|
|
|
ReactDOM.render(spoiler, spoilerContainer);
|
|
|
|
node.parentNode.replaceChild(spoilerContainer, node);
|
|
|
|
|
|
|
|
node = spoilerContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.childNodes && node.childNodes.length) {
|
|
|
|
this.activateSpoilers(node.childNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.nextSibling;
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-05-22 21:41:27 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
findLinks(nodes) {
|
2017-10-11 19:56:17 +03:00
|
|
|
let links = [];
|
2017-07-06 12:02:25 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
const node = nodes[i];
|
|
|
|
if (node.tagName === "A" && node.getAttribute("href")) {
|
2016-05-24 02:54:20 +03:00
|
|
|
if (this.isLinkPreviewable(node)) {
|
2017-07-06 12:02:25 +03:00
|
|
|
links.push(node.getAttribute("href"));
|
2016-05-24 02:54:20 +03:00
|
|
|
}
|
2017-10-11 19:56:17 +03:00
|
|
|
} else if (node.tagName === "PRE" || node.tagName === "CODE" ||
|
2017-03-18 13:43:35 +03:00
|
|
|
node.tagName === "BLOCKQUOTE") {
|
2016-05-24 02:54:20 +03:00
|
|
|
continue;
|
2017-10-11 19:56:17 +03:00
|
|
|
} else if (node.children && node.children.length) {
|
2016-05-24 02:54:20 +03:00
|
|
|
links = links.concat(this.findLinks(node.children));
|
2016-04-07 20:58:50 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 02:54:20 +03:00
|
|
|
return links;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-04-07 20:58:50 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
isLinkPreviewable(node) {
|
2016-04-21 20:00:39 +03:00
|
|
|
// don't try to preview relative links
|
|
|
|
if (!node.getAttribute("href").startsWith("http://") &&
|
2017-10-11 19:56:17 +03:00
|
|
|
!node.getAttribute("href").startsWith("https://")) {
|
2016-04-21 20:00:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// as a random heuristic to avoid highlighting things like "foo.pl"
|
|
|
|
// we require the linked text to either include a / (either from http://
|
|
|
|
// or from a full foo.bar/baz style schemeless URL) - or be a markdown-style
|
|
|
|
// link, in which case we check the target text differs from the link value.
|
|
|
|
// TODO: make this configurable?
|
2017-10-11 19:56:17 +03:00
|
|
|
if (node.textContent.indexOf("/") > -1) {
|
2016-11-07 14:45:55 +03:00
|
|
|
return true;
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
|
|
|
const url = node.getAttribute("href");
|
|
|
|
const host = url.match(/^https?:\/\/(.*?)(\/|$)/)[1];
|
2016-11-07 13:57:08 +03:00
|
|
|
|
2019-10-01 05:38:03 +03:00
|
|
|
// never preview permalinks (if anything we should give a smart
|
2016-11-07 13:57:08 +03:00
|
|
|
// preview of the room/user they point to: nobody needs to be reminded
|
|
|
|
// what the matrix.to site looks like).
|
2019-09-30 21:53:45 +03:00
|
|
|
if (isPermalinkHost(host)) return false;
|
2016-11-07 13:57:08 +03:00
|
|
|
|
2016-05-27 12:09:07 +03:00
|
|
|
if (node.textContent.toLowerCase().trim().startsWith(host.toLowerCase())) {
|
2016-04-21 20:00:39 +03:00
|
|
|
// it's a "foo.pl" style link
|
2016-11-07 14:45:55 +03:00
|
|
|
return false;
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
2016-04-21 20:00:39 +03:00
|
|
|
// it's a [foo bar](http://foo.com) style link
|
2016-11-07 14:45:55 +03:00
|
|
|
return true;
|
2016-04-21 20:00:39 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-04-21 20:00:39 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onCancelClick = event => {
|
2016-04-04 01:30:48 +03:00
|
|
|
this.setState({ widgetHidden: true });
|
|
|
|
// FIXME: persist this somewhere smarter than local storage
|
|
|
|
if (global.localStorage) {
|
|
|
|
global.localStorage.setItem("hide_preview_" + this.props.mxEvent.getId(), "1");
|
|
|
|
}
|
|
|
|
this.forceUpdate();
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onEmoteSenderClick = event => {
|
2017-04-06 16:08:59 +03:00
|
|
|
const mxEvent = this.props.mxEvent;
|
|
|
|
dis.dispatch({
|
2017-07-20 20:02:54 +03:00
|
|
|
action: 'insert_mention',
|
2017-07-20 17:46:36 +03:00
|
|
|
user_id: mxEvent.getSender(),
|
2017-04-06 16:08:59 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-04-06 16:08:59 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
getEventTileOps = () => ({
|
|
|
|
isWidgetHidden: () => {
|
|
|
|
return this.state.widgetHidden;
|
|
|
|
},
|
2016-04-08 23:42:29 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
unhideWidget: () => {
|
|
|
|
this.setState({widgetHidden: false});
|
|
|
|
if (global.localStorage) {
|
|
|
|
global.localStorage.removeItem("hide_preview_" + this.props.mxEvent.getId());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2016-04-08 23:42:29 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onStarterLinkClick = (starterLink, ev) => {
|
2016-09-05 11:10:15 +03:00
|
|
|
ev.preventDefault();
|
2016-09-02 18:36:43 +03:00
|
|
|
// We need to add on our scalar token to the starter link, but we may not have one!
|
|
|
|
// In addition, we can't fetch one on click and then go to it immediately as that
|
|
|
|
// is then treated as a popup!
|
|
|
|
// We can get around this by fetching one now and showing a "confirmation dialog" (hurr hurr)
|
|
|
|
// which requires the user to click through and THEN we can open the link in a new tab because
|
|
|
|
// the window.open command occurs in the same stack frame as the onClick callback.
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
const managers = IntegrationManagers.sharedInstance();
|
|
|
|
if (!managers.hasManager()) {
|
|
|
|
managers.openNoManagerDialog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 18:36:43 +03:00
|
|
|
// Go fetch a scalar token
|
2019-08-10 01:05:05 +03:00
|
|
|
const integrationManager = managers.getPrimaryManager();
|
|
|
|
const scalarClient = integrationManager.getScalarClient();
|
2016-09-02 18:36:43 +03:00
|
|
|
scalarClient.connect().then(() => {
|
2017-10-11 19:56:17 +03:00
|
|
|
const completeUrl = scalarClient.getStarterLink(starterLink);
|
|
|
|
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
2019-08-10 01:05:05 +03:00
|
|
|
const integrationsUrl = integrationManager.uiUrl;
|
2017-07-27 19:19:18 +03:00
|
|
|
Modal.createTrackedDialog('Add an integration', '', QuestionDialog, {
|
2017-05-30 17:09:57 +03:00
|
|
|
title: _t("Add an Integration"),
|
2016-09-02 18:36:43 +03:00
|
|
|
description:
|
|
|
|
<div>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ _t("You are about to be taken to a third-party site so you can " +
|
2017-05-30 22:32:12 +03:00
|
|
|
"authenticate your account for use with %(integrationsUrl)s. " +
|
2017-10-11 19:56:17 +03:00
|
|
|
"Do you wish to continue?", { integrationsUrl: integrationsUrl }) }
|
2016-09-02 18:36:43 +03:00
|
|
|
</div>,
|
2017-05-30 17:09:57 +03:00
|
|
|
button: _t("Continue"),
|
2020-08-29 14:14:16 +03:00
|
|
|
onFinished(confirmed) {
|
2016-09-02 18:36:43 +03:00
|
|
|
if (!confirmed) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-11 19:56:17 +03:00
|
|
|
const width = window.screen.width > 1024 ? 1024 : window.screen.width;
|
|
|
|
const height = window.screen.height > 800 ? 800 : window.screen.height;
|
|
|
|
const left = (window.screen.width - width) / 2;
|
|
|
|
const top = (window.screen.height - height) / 2;
|
2020-02-24 01:22:12 +03:00
|
|
|
const features = `height=${height}, width=${width}, top=${top}, left=${left},`;
|
|
|
|
const wnd = window.open(completeUrl, '_blank', features);
|
|
|
|
wnd.opener = null;
|
2016-09-02 18:36:43 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2016-09-02 18:03:24 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_openHistoryDialog = async () => {
|
2019-05-29 19:23:18 +03:00
|
|
|
const MessageEditHistoryDialog = sdk.getComponent("views.dialogs.MessageEditHistoryDialog");
|
|
|
|
Modal.createDialog(MessageEditHistoryDialog, {mxEvent: this.props.mxEvent});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-05-29 19:23:18 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_renderEditedMarker() {
|
2020-07-14 01:14:00 +03:00
|
|
|
const date = this.props.mxEvent.replacingEventDate();
|
|
|
|
const dateString = date && formatDate(date);
|
2019-12-08 00:01:21 +03:00
|
|
|
|
2020-07-16 17:15:00 +03:00
|
|
|
const tooltip = <div>
|
|
|
|
<div className="mx_Tooltip_title">
|
|
|
|
{_t("Edited at %(date)s", {date: dateString})}
|
|
|
|
</div>
|
|
|
|
<div className="mx_Tooltip_sub">
|
|
|
|
{_t("Click to view edits")}
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
|
2019-05-17 13:36:36 +03:00
|
|
|
return (
|
2020-07-14 01:14:00 +03:00
|
|
|
<AccessibleTooltipButton
|
2019-12-08 00:01:21 +03:00
|
|
|
className="mx_EventTile_edited"
|
2019-05-29 19:23:18 +03:00
|
|
|
onClick={this._openHistoryDialog}
|
2020-07-14 01:14:00 +03:00
|
|
|
title={_t("Edited at %(date)s. Click to view edits.", {date: dateString})}
|
2020-07-16 17:15:00 +03:00
|
|
|
tooltip={tooltip}
|
2019-12-08 00:01:21 +03:00
|
|
|
>
|
2020-07-14 01:14:00 +03:00
|
|
|
<span>{`(${_t("edited")})`}</span>
|
|
|
|
</AccessibleTooltipButton>
|
2019-05-17 13:36:36 +03:00
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-05-17 13:36:36 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2019-06-12 19:52:34 +03:00
|
|
|
if (this.props.editState) {
|
2019-08-05 16:27:40 +03:00
|
|
|
const EditMessageComposer = sdk.getComponent('rooms.EditMessageComposer');
|
|
|
|
return <EditMessageComposer editState={this.props.editState} className="mx_EventTile_content" />;
|
2019-05-17 17:30:07 +03:00
|
|
|
}
|
2017-10-11 19:56:17 +03:00
|
|
|
const mxEvent = this.props.mxEvent;
|
|
|
|
const content = mxEvent.getContent();
|
2016-09-02 18:03:24 +03:00
|
|
|
|
2020-10-07 13:08:43 +03:00
|
|
|
// only strip reply if this is the original replying event, edits thereafter do not have the fallback
|
|
|
|
const stripReply = !mxEvent.replacingEvent() && ReplyThread.getParentEventId(mxEvent);
|
2017-10-14 21:40:45 +03:00
|
|
|
let body = HtmlUtils.bodyToHtml(content, this.props.highlights, {
|
2019-03-19 02:25:11 +03:00
|
|
|
disableBigEmoji: content.msgtype === "m.emote" || !SettingsStore.getValue('TextualBody.enableBigEmoji'),
|
2018-03-04 15:39:34 +03:00
|
|
|
// Part of Replies fallback support
|
2018-03-10 02:12:13 +03:00
|
|
|
stripReplyFallback: stripReply,
|
2019-12-08 04:01:19 +03:00
|
|
|
ref: this._content,
|
2017-10-14 21:40:45 +03:00
|
|
|
});
|
2019-05-17 13:36:36 +03:00
|
|
|
if (this.props.replacingEventId) {
|
2021-01-15 14:21:52 +03:00
|
|
|
body = <>
|
|
|
|
{body}
|
|
|
|
{this._renderEditedMarker()}
|
|
|
|
</>;
|
2019-05-17 13:36:36 +03:00
|
|
|
}
|
2015-11-27 18:02:32 +03:00
|
|
|
|
2016-07-18 03:35:42 +03:00
|
|
|
if (this.props.highlightLink) {
|
2017-10-11 19:56:17 +03:00
|
|
|
body = <a href={this.props.highlightLink}>{ body }</a>;
|
|
|
|
} else if (content.data && typeof content.data["org.matrix.neb.starter_link"] === "string") {
|
2020-10-30 21:18:17 +03:00
|
|
|
body = <a href="#"
|
|
|
|
onClick={this.onStarterLinkClick.bind(this, content.data["org.matrix.neb.starter_link"])}
|
|
|
|
>{ body }</a>;
|
2016-09-02 18:03:24 +03:00
|
|
|
}
|
2016-03-31 20:38:01 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let widgets;
|
2016-07-20 14:03:13 +03:00
|
|
|
if (this.state.links.length && !this.state.widgetHidden && this.props.showUrlPreview) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const LinkPreviewWidget = sdk.getComponent('rooms.LinkPreviewWidget');
|
2016-05-24 02:54:20 +03:00
|
|
|
widgets = this.state.links.map((link)=>{
|
|
|
|
return <LinkPreviewWidget
|
2017-10-11 19:56:17 +03:00
|
|
|
key={link}
|
|
|
|
link={link}
|
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
onCancelClick={this.onCancelClick}
|
2019-03-06 14:27:16 +03:00
|
|
|
onHeightChanged={this.props.onHeightChanged} />;
|
2016-05-24 02:54:20 +03:00
|
|
|
});
|
2016-03-31 20:38:01 +03:00
|
|
|
}
|
|
|
|
|
2015-11-29 00:12:02 +03:00
|
|
|
switch (content.msgtype) {
|
|
|
|
case "m.emote":
|
|
|
|
return (
|
2019-12-08 04:01:19 +03:00
|
|
|
<span className="mx_MEmoteBody mx_EventTile_content">
|
2017-04-06 16:08:59 +03:00
|
|
|
*
|
2019-05-19 17:23:43 +03:00
|
|
|
<span
|
2017-04-06 16:08:59 +03:00
|
|
|
className="mx_MEmoteBody_sender"
|
|
|
|
onClick={this.onEmoteSenderClick}
|
|
|
|
>
|
2019-12-08 04:01:19 +03:00
|
|
|
{ mxEvent.sender ? mxEvent.sender.name : mxEvent.getSender() }
|
2019-05-19 17:23:43 +03:00
|
|
|
</span>
|
2017-04-06 16:08:59 +03:00
|
|
|
|
|
|
|
{ body }
|
2016-05-24 02:54:20 +03:00
|
|
|
{ widgets }
|
2015-11-29 00:12:02 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
case "m.notice":
|
|
|
|
return (
|
2019-12-08 04:01:19 +03:00
|
|
|
<span className="mx_MNoticeBody mx_EventTile_content">
|
2015-11-29 00:12:02 +03:00
|
|
|
{ body }
|
2016-05-24 02:54:20 +03:00
|
|
|
{ widgets }
|
2015-11-29 00:12:02 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
default: // including "m.text"
|
|
|
|
return (
|
2021-01-19 18:35:32 +03:00
|
|
|
<span className="mx_MTextBody mx_EventTile_content">
|
2015-11-29 00:12:02 +03:00
|
|
|
{ body }
|
2016-05-24 02:54:20 +03:00
|
|
|
{ widgets }
|
2015-11-29 00:12:02 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|