2015-11-27 18:02:32 +03:00
|
|
|
/*
|
2021-06-30 15:01:26 +03:00
|
|
|
Copyright 2015 - 2021 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.
|
|
|
|
*/
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
import React, { createRef, SyntheticEvent } from 'react';
|
2017-04-06 16:08:59 +03:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import highlight from 'highlight.js';
|
2021-07-01 10:29:53 +03:00
|
|
|
import { MsgType } from "matrix-js-sdk/src/@types/event";
|
2021-06-30 15:01:26 +03:00
|
|
|
|
2017-04-06 16:08:59 +03:00
|
|
|
import * as HtmlUtils from '../../../HtmlUtils';
|
2021-05-26 16:14:55 +03:00
|
|
|
import { formatDate } from '../../../DateUtils';
|
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';
|
2021-06-30 15:01:26 +03:00
|
|
|
import { toRightOf } 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";
|
2021-05-26 16:14:55 +03:00
|
|
|
import { pillifyLinks, unmountPills } from '../../../utils/pillify';
|
|
|
|
import { IntegrationManagers } from "../../../integrations/IntegrationManagers";
|
|
|
|
import { isPermalinkHost } from "../../../utils/permalinks/Permalinks";
|
|
|
|
import { copyPlaintext } from "../../../utils/strings";
|
2020-07-14 01:14:00 +03:00
|
|
|
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
|
2021-05-26 16:14:55 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-05-25 12:25:36 +03:00
|
|
|
import UIStore from "../../../stores/UIStore";
|
2021-05-26 16:14:55 +03:00
|
|
|
import { ComposerInsertPayload } from "../../../dispatcher/payloads/ComposerInsertPayload";
|
|
|
|
import { Action } from "../../../dispatcher/actions";
|
2021-06-30 15:01:26 +03:00
|
|
|
import GenericTextContextMenu from "../context_menus/GenericTextContextMenu";
|
|
|
|
import Spoiler from "../elements/Spoiler";
|
|
|
|
import QuestionDialog from "../dialogs/QuestionDialog";
|
|
|
|
import MessageEditHistoryDialog from "../dialogs/MessageEditHistoryDialog";
|
|
|
|
import EditMessageComposer from '../rooms/EditMessageComposer';
|
2021-07-07 20:04:30 +03:00
|
|
|
import LinkPreviewGroup from '../rooms/LinkPreviewGroup';
|
2021-07-16 19:57:14 +03:00
|
|
|
import { IBodyProps } from "./IBodyProps";
|
2021-06-30 15:01:26 +03:00
|
|
|
|
|
|
|
interface IState {
|
|
|
|
// the URLs (if any) to be previewed with a LinkPreviewWidget inside this TextualBody.
|
|
|
|
links: string[];
|
|
|
|
|
|
|
|
// track whether the preview widget is hidden
|
|
|
|
widgetHidden: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
@replaceableComponent("views.messages.TextualBody")
|
2021-07-16 19:57:14 +03:00
|
|
|
export default class TextualBody extends React.Component<IBodyProps, IState> {
|
2021-06-30 15:01:26 +03:00
|
|
|
private readonly contentRef = createRef<HTMLSpanElement>();
|
|
|
|
|
|
|
|
private unmounted = false;
|
|
|
|
private pills: Element[] = [];
|
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.state = {
|
2016-05-31 21:42:00 +03:00
|
|
|
links: [],
|
2016-04-04 01:30:48 +03:00
|
|
|
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() {
|
2019-06-12 19:52:34 +03:00
|
|
|
if (!this.props.editState) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.applyFormatting();
|
2019-05-17 18:01:30 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-10-26 20:41:28 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private applyFormatting(): void {
|
2021-01-21 15:08:55 +03:00
|
|
|
const showLineNumbers = SettingsStore.getValue("showCodeLineNumbers");
|
2021-06-30 15:01:26 +03:00
|
|
|
this.activateSpoilers([this.contentRef.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.
|
2021-06-30 15:01:26 +03:00
|
|
|
pillifyLinks([this.contentRef.current], this.props.mxEvent, this.pills);
|
|
|
|
HtmlUtils.linkifyElement(this.contentRef.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
|
2021-06-30 15:01:26 +03:00
|
|
|
const pres = (ReactDOM.findDOMNode(this) as Element).getElementsByTagName("pre");
|
2021-01-20 10:44:32 +03:00
|
|
|
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.
|
2021-06-30 15:01:26 +03:00
|
|
|
if (pres[i].parentElement.className == "mx_EventTile_pre_container") continue;
|
2021-02-24 15:07:25 +03:00
|
|
|
// Add code element if it's missing since we depend on it
|
2021-02-23 09:49:26 +03:00
|
|
|
if (pres[i].getElementsByTagName("code").length == 0) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.addCodeElement(pres[i]);
|
2021-02-23 09:46:03 +03:00
|
|
|
}
|
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-06-30 15:01:26 +03:00
|
|
|
const div = this.wrapInDiv(pres[i]);
|
|
|
|
this.handleCodeBlockExpansion(pres[i]);
|
|
|
|
this.addCodeExpansionButton(div, pres[i]);
|
|
|
|
this.addCodeCopyButton(div);
|
2021-01-21 15:08:55 +03:00
|
|
|
if (showLineNumbers) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.addLineNumbers(pres[i]);
|
2021-01-21 15:08:55 +03:00
|
|
|
}
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
2021-01-20 10:44:32 +03:00
|
|
|
}
|
|
|
|
// Highlight code
|
2021-06-30 15:01:26 +03:00
|
|
|
const codes = (ReactDOM.findDOMNode(this) as Element).getElementsByTagName("code");
|
2021-02-04 19:58:21 +03:00
|
|
|
if (codes.length > 0) {
|
2021-02-10 15:41:24 +03:00
|
|
|
// Do this asynchronously: parsing code takes time and we don't
|
2021-02-10 16:02:45 +03:00
|
|
|
// need to block the DOM update on it.
|
2021-02-10 15:18:23 +03:00
|
|
|
setTimeout(() => {
|
2021-06-30 15:01:26 +03:00
|
|
|
if (this.unmounted) return;
|
2021-02-10 15:18:23 +03:00
|
|
|
for (let i = 0; i < codes.length; i++) {
|
2021-02-10 16:20:19 +03:00
|
|
|
// If the code already has the hljs class we want to skip this.
|
|
|
|
// This happens after the codeblock was edited.
|
|
|
|
if (codes[i].className.includes("hljs")) continue;
|
2021-06-30 15:01:26 +03:00
|
|
|
this.highlightCode(codes[i]);
|
2021-02-10 15:18:23 +03:00
|
|
|
}
|
|
|
|
}, 10);
|
2016-10-26 20:41:28 +03:00
|
|
|
}
|
2021-01-19 18:35:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private addCodeElement(pre: HTMLPreElement): void {
|
2021-02-23 09:40:53 +03:00
|
|
|
const code = document.createElement("code");
|
2021-02-24 15:40:37 +03:00
|
|
|
code.append(...pre.childNodes);
|
2021-02-23 09:40:53 +03:00
|
|
|
pre.appendChild(code);
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private addCodeExpansionButton(div: HTMLDivElement, pre: HTMLPreElement): void {
|
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-05-25 12:25:36 +03:00
|
|
|
const percentageOfViewport = pre.offsetHeight / UIStore.instance.windowHeight * 100;
|
2021-01-20 17:01:23 +03:00
|
|
|
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-06-30 15:01:26 +03:00
|
|
|
private addCodeCopyButton(div: HTMLDivElement): void {
|
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-06-30 15:01:26 +03:00
|
|
|
const copyCode = button.parentElement.getElementsByTagName("code")[0];
|
2021-01-19 18:35:32 +03:00
|
|
|
const successful = await copyPlaintext(copyCode.textContent);
|
|
|
|
|
|
|
|
const buttonRect = button.getBoundingClientRect();
|
2021-06-29 15:11:58 +03:00
|
|
|
const { close } = ContextMenu.createMenu(GenericTextContextMenu, {
|
2021-01-19 18:35:32 +03:00
|
|
|
...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-06-30 15:01:26 +03:00
|
|
|
private wrapInDiv(pre: HTMLPreElement): HTMLDivElement {
|
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-06-30 15:01:26 +03:00
|
|
|
private handleCodeBlockExpansion(pre: HTMLPreElement): void {
|
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-06-30 15:01:26 +03:00
|
|
|
private addLineNumbers(pre: HTMLPreElement): void {
|
2021-03-26 00:38:34 +03:00
|
|
|
// Calculate number of lines in pre
|
|
|
|
const number = pre.innerHTML.replace(/\n(<\/code>)?$/, "").split(/\n/).length;
|
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];
|
|
|
|
// Iterate through lines starting with 1 (number of the first line is 1)
|
2021-03-26 00:38:34 +03:00
|
|
|
for (let i = 1; i <= number; i++) {
|
2021-01-21 12:53:18 +03:00
|
|
|
lineNumbers.innerHTML += '<span class="mx_EventTile_lineNumber">' + i + '</span>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private highlightCode(code: HTMLElement): void {
|
2021-07-11 12:18:06 +03:00
|
|
|
// Auto-detect language only if enabled and only for codeblocks
|
|
|
|
if (
|
|
|
|
SettingsStore.getValue("enableSyntaxHighlightLanguageDetection") &&
|
|
|
|
code.parentElement instanceof HTMLPreElement
|
|
|
|
) {
|
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) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.applyFormatting();
|
2019-05-17 18:01:30 +03:00
|
|
|
}
|
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() {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.unmounted = true;
|
|
|
|
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 :)
|
2021-06-19 22:38:19 +03:00
|
|
|
return (nextProps.mxEvent.getId() !== this.props.mxEvent.getId() ||
|
2016-07-20 14:03:13 +03:00
|
|
|
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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private calculateUrlPreview(): void {
|
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
|
2021-06-30 15:01:26 +03:00
|
|
|
let links = this.findLinks([this.contentRef.current]);
|
2016-07-18 03:35:42 +03:00
|
|
|
if (links.length) {
|
2021-07-07 20:04:30 +03:00
|
|
|
// de-duplicate the links using a set here maintains the order
|
|
|
|
links = Array.from(new Set(links));
|
|
|
|
this.setState({ 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
|
2021-06-30 15:01:26 +03:00
|
|
|
if (window.localStorage) {
|
|
|
|
const hidden = !!window.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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private activateSpoilers(nodes: ArrayLike<Element>): void {
|
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");
|
|
|
|
node.removeAttribute("data-mx-spoiler"); // we don't want to recurse
|
2021-06-30 15:01:26 +03:00
|
|
|
const spoiler = <Spoiler reason={reason} contentHtml={node.outerHTML} />;
|
2019-05-22 21:41:27 +03:00
|
|
|
|
|
|
|
ReactDOM.render(spoiler, spoilerContainer);
|
|
|
|
node.parentNode.replaceChild(spoilerContainer, node);
|
|
|
|
|
|
|
|
node = spoilerContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.childNodes && node.childNodes.length) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.activateSpoilers(node.childNodes as NodeListOf<Element>);
|
2019-05-22 21:41:27 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
node = node.nextSibling as Element;
|
2019-05-22 21:41:27 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-05-22 21:41:27 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private findLinks(nodes: ArrayLike<Element>): string[] {
|
|
|
|
let links: string[] = [];
|
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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private isLinkPreviewable(node: Element): boolean {
|
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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onCancelClick = (): void => {
|
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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onEmoteSenderClick = (): void => {
|
2017-04-06 16:08:59 +03:00
|
|
|
const mxEvent = this.props.mxEvent;
|
2021-05-25 00:02:50 +03:00
|
|
|
dis.dispatch<ComposerInsertPayload>({
|
|
|
|
action: Action.ComposerInsert,
|
2021-04-13 17:09:37 +03:00
|
|
|
userId: 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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
public getEventTileOps = () => ({
|
2020-08-29 14:14:16 +03:00
|
|
|
isWidgetHidden: () => {
|
|
|
|
return this.state.widgetHidden;
|
|
|
|
},
|
2016-04-08 23:42:29 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
unhideWidget: () => {
|
2021-06-29 15:11:58 +03:00
|
|
|
this.setState({ widgetHidden: false });
|
2020-08-29 14:14:16 +03:00
|
|
|
if (global.localStorage) {
|
|
|
|
global.localStorage.removeItem("hide_preview_" + this.props.mxEvent.getId());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2016-04-08 23:42:29 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onStarterLinkClick = (starterLink: string, ev: SyntheticEvent): void => {
|
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);
|
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
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private openHistoryDialog = async (): Promise<void> => {
|
2021-06-29 15:11:58 +03:00
|
|
|
Modal.createDialog(MessageEditHistoryDialog, { mxEvent: this.props.mxEvent });
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-05-29 19:23:18 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private 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">
|
2021-07-20 00:43:11 +03:00
|
|
|
{ _t("Edited at %(date)s", { date: dateString }) }
|
2020-07-16 17:15:00 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_Tooltip_sub">
|
2021-07-20 00:43:11 +03:00
|
|
|
{ _t("Click to view edits") }
|
2020-07-16 17:15:00 +03:00
|
|
|
</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"
|
2021-06-30 15:01:26 +03:00
|
|
|
onClick={this.openHistoryDialog}
|
2021-06-29 15:11:58 +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
|
|
|
>
|
2021-07-20 00:43:11 +03:00
|
|
|
<span>{ `(${_t("edited")})` }</span>
|
2020-07-14 01:14:00 +03:00
|
|
|
</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
|
|
|
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
|
2021-06-30 15:01:26 +03:00
|
|
|
const stripReply = !mxEvent.replacingEvent() && !!ReplyThread.getParentEventId(mxEvent);
|
2017-10-14 21:40:45 +03:00
|
|
|
let body = HtmlUtils.bodyToHtml(content, this.props.highlights, {
|
2021-06-30 15:01:26 +03:00
|
|
|
disableBigEmoji: content.msgtype === MsgType.Emote
|
|
|
|
|| !SettingsStore.getValue<boolean>('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,
|
2021-06-30 15:01:26 +03:00
|
|
|
ref: this.contentRef,
|
|
|
|
returnString: false,
|
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 = <>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ body }
|
|
|
|
{ this.renderEditedMarker() }
|
2021-01-15 14:21:52 +03:00
|
|
|
</>;
|
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) {
|
2021-07-07 20:04:30 +03:00
|
|
|
widgets = <LinkPreviewGroup
|
|
|
|
links={this.state.links}
|
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
onCancelClick={this.onCancelClick}
|
|
|
|
onHeightChanged={this.props.onHeightChanged}
|
|
|
|
/>;
|
2016-03-31 20:38:01 +03:00
|
|
|
}
|
|
|
|
|
2015-11-29 00:12:02 +03:00
|
|
|
switch (content.msgtype) {
|
2021-06-30 15:01:26 +03:00
|
|
|
case MsgType.Emote:
|
2015-11-29 00:12:02 +03:00
|
|
|
return (
|
2021-07-22 15:24:49 +03:00
|
|
|
<div 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 }
|
2021-07-22 15:24:49 +03:00
|
|
|
</div>
|
2015-11-29 00:12:02 +03:00
|
|
|
);
|
2021-06-30 15:01:26 +03:00
|
|
|
case MsgType.Notice:
|
2015-11-29 00:12:02 +03:00
|
|
|
return (
|
2021-07-22 15:24:49 +03:00
|
|
|
<div className="mx_MNoticeBody mx_EventTile_content">
|
2015-11-29 00:12:02 +03:00
|
|
|
{ body }
|
2016-05-24 02:54:20 +03:00
|
|
|
{ widgets }
|
2021-07-22 15:24:49 +03:00
|
|
|
</div>
|
2015-11-29 00:12:02 +03:00
|
|
|
);
|
|
|
|
default: // including "m.text"
|
|
|
|
return (
|
2021-07-22 15:24:49 +03:00
|
|
|
<div className="mx_MTextBody mx_EventTile_content">
|
2015-11-29 00:12:02 +03:00
|
|
|
{ body }
|
2016-05-24 02:54:20 +03:00
|
|
|
{ widgets }
|
2021-07-22 15:24:49 +03:00
|
|
|
</div>
|
2015-11-29 00:12:02 +03:00
|
|
|
);
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|