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

Add body link fallback handler for in-app navigation #7627

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/HtmlUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ const transformTags: IExtendedSanitizeOptions["transformTags"] = { // custom to
attribs.target = '_blank'; // by default

const transformed = tryTransformPermalinkToLocalHref(attribs.href); // only used to check if it is a link that can be handled locally
if (transformed !== attribs.href || // it could be converted so handle locally symbols e.g. @user:server.tdl, matrix: and matrix.to
attribs.href.match(ELEMENT_URL_PATTERN) // for https:vector|riot...
if (
transformed !== attribs.href || // it could be converted so handle locally symbols e.g. @user:server.tdl, matrix: and matrix.to
attribs.href.match(ELEMENT_URL_PATTERN) // for https links to Element domains
) {
delete attribs.target;
}
Expand Down
33 changes: 28 additions & 5 deletions src/components/views/messages/TextualBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { createRef, SyntheticEvent } from 'react';
import React, { createRef, SyntheticEvent, MouseEvent } from 'react';
import ReactDOM from 'react-dom';
import highlight from 'highlight.js';
import { MsgType } from "matrix-js-sdk/src/@types/event";
Expand All @@ -31,7 +31,7 @@ import SettingsStore from "../../../settings/SettingsStore";
import ReplyChain from "../elements/ReplyChain";
import { pillifyLinks, unmountPills } from '../../../utils/pillify';
import { IntegrationManagers } from "../../../integrations/IntegrationManagers";
import { isPermalinkHost } from "../../../utils/permalinks/Permalinks";
import { isPermalinkHost, tryTransformPermalinkToLocalHref } from "../../../utils/permalinks/Permalinks";
import { copyPlaintext } from "../../../utils/strings";
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
import { replaceableComponent } from "../../../utils/replaceableComponent";
Expand Down Expand Up @@ -418,6 +418,23 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
});
};

/**
* This acts as a fallback in-app navigation handler for any body links that
* were ignored as part of linkification because they were already links
* to start with (e.g. pills, links in the content).
*/
private onBodyLinkClick = (e: MouseEvent): void => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if they worked before, but what about ctrl + clicking links in messages?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah hmm... IIRC, we have always forced in-app navigation (at least in recent times), but it does seem reasonable to support Ctrl/Cmd + Click to force a separate tab for those who want it. Since this is aimed at fixing a regression and that's effectively an enhancement, I'd say it's best to file an issue track that separately.

const target = e.target as Element;
if (target.nodeName !== "A" || target.classList.contains("linkified")) return;
jryans marked this conversation as resolved.
Show resolved Hide resolved
const { href } = target as HTMLLinkElement;
const localHref = tryTransformPermalinkToLocalHref(href);
if (localHref !== href) {
// it could be converted to a localHref -> therefore handle locally
e.preventDefault();
window.location.hash = localHref;
}
};

public getEventTileOps = () => ({
isWidgetHidden: () => {
return this.state.widgetHidden;
Expand Down Expand Up @@ -606,7 +623,9 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {

if (isEmote) {
return (
<div className="mx_MEmoteBody mx_EventTile_content">
<div className="mx_MEmoteBody mx_EventTile_content"
onClick={this.onBodyLinkClick}
>
*&nbsp;
<span
className="mx_MEmoteBody_sender"
Expand All @@ -622,14 +641,18 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
}
if (isNotice) {
return (
<div className="mx_MNoticeBody mx_EventTile_content">
<div className="mx_MNoticeBody mx_EventTile_content"
onClick={this.onBodyLinkClick}
>
{ body }
{ widgets }
</div>
);
}
return (
<div className="mx_MTextBody mx_EventTile_content">
<div className="mx_MTextBody mx_EventTile_content"
onClick={this.onBodyLinkClick}
>
{ body }
{ widgets }
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/linkify-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import linkifyString from '@matrix-org/linkify-string';
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
import { registerCustomProtocol, registerPlugin } from '@matrix-org/linkifyjs';

//linkifyjs/src/core/fsm
import { baseUrl } from "./utils/permalinks/MatrixToPermalinkConstructor";
import {
parsePermalink,
Expand Down Expand Up @@ -227,8 +226,9 @@ export const options = {
if (type === Type.URL) {
try {
const transformed = tryTransformPermalinkToLocalHref(href);
if (transformed !== href || // if it could be converted to handle locally for matrix symbols e.g. @user:server.tdl and matrix.to
decodeURIComponent(href).match(ELEMENT_URL_PATTERN) // for https:vector|riot...
if (
transformed !== href || // if it could be converted to handle locally for matrix symbols e.g. @user:server.tdl and matrix.to
decodeURIComponent(href).match(ELEMENT_URL_PATTERN) // for https links to Element domains
) {
return null;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/permalinks/Permalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export function tryTransformEntityToPermalink(entity: string): string {
if (!entity) return null;

// Check to see if it is a bare entity for starters
{if (entity[0] === '#' || entity[0] === '!') return makeRoomPermalink(entity);}
if (entity[0] === '#' || entity[0] === '!') return makeRoomPermalink(entity);
if (entity[0] === '@') return makeUserPermalink(entity);
if (entity[0] === '+') return makeGroupPermalink(entity);

Expand Down