Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 0739eaf

Browse files
committed
Add body link fallback handler for in-app navigation
This adds a fallback click handler for all links in the message to ensure that links that were ignored as part of linkification (e.g. pills, links in message content) will still navigate in-app where possible. This approach preserves a side-benefit of #7453, which kept all link hrefs as matrix.to style links (so that they can be easily copied and shared). Fixes element-hq/element-web#20715
1 parent dbae45e commit 0739eaf

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/components/views/messages/TextualBody.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React, { createRef, SyntheticEvent } from 'react';
17+
import React, { createRef, SyntheticEvent, MouseEvent } from 'react';
1818
import ReactDOM from 'react-dom';
1919
import highlight from 'highlight.js';
2020
import { MsgType } from "matrix-js-sdk/src/@types/event";
@@ -31,7 +31,7 @@ import SettingsStore from "../../../settings/SettingsStore";
3131
import ReplyChain from "../elements/ReplyChain";
3232
import { pillifyLinks, unmountPills } from '../../../utils/pillify';
3333
import { IntegrationManagers } from "../../../integrations/IntegrationManagers";
34-
import { isPermalinkHost } from "../../../utils/permalinks/Permalinks";
34+
import { isPermalinkHost, tryTransformPermalinkToLocalHref } from "../../../utils/permalinks/Permalinks";
3535
import { copyPlaintext } from "../../../utils/strings";
3636
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
3737
import { replaceableComponent } from "../../../utils/replaceableComponent";
@@ -418,6 +418,23 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
418418
});
419419
};
420420

421+
/**
422+
* This acts as a fallback in-app navigation handler for any body links that
423+
* were ignored as part of linkification because they were already links
424+
* to start with (e.g. pills, links in the content).
425+
*/
426+
private onBodyLinkClick = (e: MouseEvent): void => {
427+
const target = e.target as Element;
428+
if (target.nodeName !== "A" || target.classList.contains("linkified")) return;
429+
const { href } = target as HTMLLinkElement;
430+
const localHref = tryTransformPermalinkToLocalHref(href);
431+
if (localHref !== href) {
432+
// it could be converted to a localHref -> therefore handle locally
433+
e.preventDefault();
434+
window.location.hash = localHref;
435+
}
436+
};
437+
421438
public getEventTileOps = () => ({
422439
isWidgetHidden: () => {
423440
return this.state.widgetHidden;
@@ -606,7 +623,9 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
606623

607624
if (isEmote) {
608625
return (
609-
<div className="mx_MEmoteBody mx_EventTile_content">
626+
<div className="mx_MEmoteBody mx_EventTile_content"
627+
onClick={this.onBodyLinkClick}
628+
>
610629
*&nbsp;
611630
<span
612631
className="mx_MEmoteBody_sender"
@@ -622,14 +641,18 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
622641
}
623642
if (isNotice) {
624643
return (
625-
<div className="mx_MNoticeBody mx_EventTile_content">
644+
<div className="mx_MNoticeBody mx_EventTile_content"
645+
onClick={this.onBodyLinkClick}
646+
>
626647
{ body }
627648
{ widgets }
628649
</div>
629650
);
630651
}
631652
return (
632-
<div className="mx_MTextBody mx_EventTile_content">
653+
<div className="mx_MTextBody mx_EventTile_content"
654+
onClick={this.onBodyLinkClick}
655+
>
633656
{ body }
634657
{ widgets }
635658
</div>

0 commit comments

Comments
 (0)