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

Add option to display tooltip on link hover #8394

Merged
merged 37 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f18cce8
Add option to display tooltip on link hover
Johennes Apr 21, 2022
7129ba6
Merge branch 'develop' into feature/url-tooltip
Johennes Apr 22, 2022
cf07f54
Gracefully handle missing platform
Johennes Apr 22, 2022
5da4ab8
Use public access modifier
Johennes May 2, 2022
f90805f
Use exact inequality
Johennes May 2, 2022
2374247
Merge branch 'develop' into feature/url-tooltip
Johennes May 2, 2022
1f2808b
Document getAbsoluteUrl
Johennes May 2, 2022
c5ae080
Appease the linter
Johennes May 2, 2022
73dfb56
Clarify performance impact in comment
Johennes May 6, 2022
89ff830
Merge branch 'develop' into feature/url-tooltip
Johennes May 6, 2022
502fab8
Merge branch 'develop' into feature/url-tooltip
Johennes May 9, 2022
e6946ac
Use URL instead of anchor element hack
Johennes May 9, 2022
cd0574b
Wrap anchor in tooltip target and only allow focus on anchor
Johennes May 11, 2022
8cbd220
Merge branch 'develop' into feature/url-tooltip
t3chguy May 13, 2022
0b5d048
Use optional chaining
Johennes May 13, 2022
49ee784
Use double quotes for consistency
Johennes May 13, 2022
1561555
Accumulate and unmount tooltips and extract tooltipify.tsx
Johennes May 13, 2022
d3f5e8d
Merge branch 'develop' into feature/url-tooltip
Johennes May 13, 2022
cfe46f2
Fix indentation
Johennes May 13, 2022
d90964f
Blur tooltip target on click
Johennes May 13, 2022
c7c9bc7
Merge branch 'develop' into feature/url-tooltip
Johennes May 13, 2022
b8f68e0
Remove space
Johennes May 13, 2022
76862a0
Merge branch 'develop' into feature/url-tooltip
Johennes May 17, 2022
7b6f641
Mention platform flag in comment
Johennes May 17, 2022
1216b17
Add (simplistic) tests
Johennes May 17, 2022
9a711f1
Fix lint errors
Johennes May 17, 2022
cdd181d
Fix lint errors ... for real
Johennes May 18, 2022
69f096e
Merge branch 'develop' into feature/url-tooltip
Johennes May 18, 2022
1e4cfac
Replace snapshot tests with structural assertions
Johennes May 18, 2022
837c56a
Add missing semicolon
Johennes May 18, 2022
33c40ac
Merge branch 'develop' into feature/url-tooltip
Johennes May 20, 2022
e892052
Merge branch 'develop' into feature/url-tooltip
Johennes Jun 1, 2022
dc3a5d3
Add tooltips in link previews
Johennes Jun 2, 2022
a76abc6
Fix copyright
Johennes Jun 6, 2022
b83b12f
Merge branch 'develop' into feature/url-tooltip
t3chguy Jun 6, 2022
aa9f454
Merge branch 'develop' into feature/url-tooltip
Johennes Jul 5, 2022
68cf5c0
Merge branch 'develop' into feature/url-tooltip
t3chguy Jul 6, 2022
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
8 changes: 8 additions & 0 deletions src/BasePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ export default abstract class BasePlatform {
}
}

/**
* Returns true if the platform requires URL previews in tooltips, otherwise false.
* @returns {boolean} whether the platform requires URL previews in tooltips
*/
public needsUrlTooltips(): boolean {
return false;
}

/**
* Returns a promise that resolves to a string representing the current version of the application.
*/
Expand Down
63 changes: 63 additions & 0 deletions src/HtmlUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
*/

import React, { ReactNode } from 'react';
import ReactDOM from 'react-dom';
import sanitizeHtml from 'sanitize-html';
import cheerio from 'cheerio';
import classNames from 'classnames';
Expand All @@ -35,6 +36,8 @@ import { getEmojiFromUnicode } from "./emoji";
import { mediaFromMxc } from "./customisations/Media";
import { ELEMENT_URL_PATTERN, options as linkifyMatrixOptions } from './linkify-matrix';
import { stripHTMLReply, stripPlainReply } from './utils/Reply';
import TextWithTooltip from './components/views/elements/TextWithTooltip';
import PlatformPeg from './PlatformPeg';

// Anything outside the basic multilingual plane will be a surrogate pair
const SURROGATE_PAIR_PATTERN = /([\ud800-\udbff])([\udc00-\udfff])/;
Expand Down Expand Up @@ -635,6 +638,66 @@ export function linkifyAndSanitizeHtml(dirtyHtml: string, options = linkifyMatri
return sanitizeHtml(linkifyString(dirtyHtml, options), sanitizeHtmlParams);
}

/**
* Turns a given URL into an absolute one.
* @param {string} url The URL transform
* @returns {string} Absolute URL
*/
const getAbsoluteUrl = (() => {
// Use a single cached anchor element stored in a closure to avoid having to recreate
// a new one on every call and increase performance
Johennes marked this conversation as resolved.
Show resolved Hide resolved
let a: HTMLAnchorElement;

return (url: string) => {
if (!a) {
a = document.createElement('a');
}
a.href = url;
return a.href;
};
})();

/**
* Recurses depth-first through a DOM tree, adding tooltip previews for link elements.
*
* @param {Element[]} rootNodes - a list of sibling DOM nodes to traverse to try
* to add tooltips.
* @param {Element[]} ignoredNodes: a list of nodes to not recurse into.
*/
export function tooltipifyLinks(rootNodes: ArrayLike<Element>, ignoredNodes: Element[]) {
if (!PlatformPeg.get()?.needsUrlTooltips()) {
return;
}

let node = rootNodes[0];

while (node) {
let tooltipified = false;

if (ignoredNodes.indexOf(node) >= 0) {
node = node.nextSibling as Element;
continue;
}

if (node.tagName === "A" && node.getAttribute("href")
&& node.getAttribute("href") !== node.textContent.trim()
) {
const href = node.getAttribute("href");
const tooltip = <TextWithTooltip tooltip={getAbsoluteUrl(href)}>
<span dangerouslySetInnerHTML={{ __html: node.innerHTML }} />
</TextWithTooltip>;
ReactDOM.render(tooltip, node);
tooltipified = true;
}

if (node.childNodes && node.childNodes.length && !tooltipified) {
Johennes marked this conversation as resolved.
Show resolved Hide resolved
tooltipifyLinks(node.childNodes as NodeListOf<Element>, ignoredNodes);
}

node = node.nextSibling as Element;
}
}

/**
* Returns if a node is a block element or not.
* Only takes html nodes into account that are allowed in matrix messages.
Expand Down
9 changes: 9 additions & 0 deletions src/components/views/messages/EditHistoryMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ export default class EditHistoryMessage extends React.PureComponent<IProps, ISta
}
}

private tooltipifyLinks(): void {
// not present for redacted events
if (this.content.current) {
HtmlUtils.tooltipifyLinks(this.content.current.children, this.pills);
}
}

public componentDidMount(): void {
this.pillifyLinks();
this.tooltipifyLinks();
}

public componentWillUnmount(): void {
Expand All @@ -107,6 +115,7 @@ export default class EditHistoryMessage extends React.PureComponent<IProps, ISta

public componentDidUpdate(): void {
this.pillifyLinks();
this.tooltipifyLinks();
}

private renderActionBar(): JSX.Element {
Expand Down
1 change: 1 addition & 0 deletions src/components/views/messages/TextualBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
// we should be pillify them here by doing the linkifying BEFORE the pillifying.
pillifyLinks([this.contentRef.current], this.props.mxEvent, this.pills);
HtmlUtils.linkifyElement(this.contentRef.current);
HtmlUtils.tooltipifyLinks([this.contentRef.current], this.pills);
this.calculateUrlPreview();

if (this.props.mxEvent.getContent().format === "org.matrix.custom.html") {
Expand Down