render matrix room and event link content (#1938)
This commit is contained in:
parent
96df140153
commit
5482f8e72e
1 changed files with 24 additions and 7 deletions
|
@ -14,7 +14,12 @@ import { IntermediateRepresentation, Opts as LinkifyOpts, OptFn } from 'linkifyj
|
||||||
import Linkify from 'linkify-react';
|
import Linkify from 'linkify-react';
|
||||||
import { ErrorBoundary } from 'react-error-boundary';
|
import { ErrorBoundary } from 'react-error-boundary';
|
||||||
import * as css from '../styles/CustomHtml.css';
|
import * as css from '../styles/CustomHtml.css';
|
||||||
import { getMxIdLocalPart, getCanonicalAliasRoomId, isRoomAlias, mxcUrlToHttp } from '../utils/matrix';
|
import {
|
||||||
|
getMxIdLocalPart,
|
||||||
|
getCanonicalAliasRoomId,
|
||||||
|
isRoomAlias,
|
||||||
|
mxcUrlToHttp,
|
||||||
|
} from '../utils/matrix';
|
||||||
import { getMemberDisplayName } from '../utils/room';
|
import { getMemberDisplayName } from '../utils/room';
|
||||||
import { EMOJI_PATTERN, URL_NEG_LB } from '../utils/regex';
|
import { EMOJI_PATTERN, URL_NEG_LB } from '../utils/regex';
|
||||||
import { getHexcodeForEmoji, getShortcodeFor } from './emoji';
|
import { getHexcodeForEmoji, getShortcodeFor } from './emoji';
|
||||||
|
@ -44,7 +49,8 @@ export const LINKIFY_OPTS: LinkifyOpts = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const makeMentionCustomProps = (
|
export const makeMentionCustomProps = (
|
||||||
handleMentionClick?: ReactEventHandler<HTMLElement>
|
handleMentionClick?: ReactEventHandler<HTMLElement>,
|
||||||
|
content?: string
|
||||||
): ComponentPropsWithoutRef<'a'> => ({
|
): ComponentPropsWithoutRef<'a'> => ({
|
||||||
style: { cursor: 'pointer' },
|
style: { cursor: 'pointer' },
|
||||||
target: '_blank',
|
target: '_blank',
|
||||||
|
@ -53,6 +59,7 @@ export const makeMentionCustomProps = (
|
||||||
tabIndex: handleMentionClick ? 0 : -1,
|
tabIndex: handleMentionClick ? 0 : -1,
|
||||||
onKeyDown: handleMentionClick ? onEnterOrSpace(handleMentionClick) : undefined,
|
onKeyDown: handleMentionClick ? onEnterOrSpace(handleMentionClick) : undefined,
|
||||||
onClick: handleMentionClick,
|
onClick: handleMentionClick,
|
||||||
|
children: content,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const renderMatrixMention = (
|
export const renderMatrixMention = (
|
||||||
|
@ -72,8 +79,9 @@ export const renderMatrixMention = (
|
||||||
className={css.Mention({ highlight: mx.getUserId() === userId })}
|
className={css.Mention({ highlight: mx.getUserId() === userId })}
|
||||||
data-mention-id={userId}
|
data-mention-id={userId}
|
||||||
>
|
>
|
||||||
{`@${(currentRoom && getMemberDisplayName(currentRoom, userId)) ?? getMxIdLocalPart(userId)
|
{`@${
|
||||||
}`}
|
(currentRoom && getMemberDisplayName(currentRoom, userId)) ?? getMxIdLocalPart(userId)
|
||||||
|
}`}
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -85,6 +93,8 @@ export const renderMatrixMention = (
|
||||||
isRoomAlias(roomIdOrAlias) ? getCanonicalAliasRoomId(mx, roomIdOrAlias) : roomIdOrAlias
|
isRoomAlias(roomIdOrAlias) ? getCanonicalAliasRoomId(mx, roomIdOrAlias) : roomIdOrAlias
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const fallbackContent = mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<a
|
<a
|
||||||
href={href}
|
href={href}
|
||||||
|
@ -95,7 +105,7 @@ export const renderMatrixMention = (
|
||||||
data-mention-id={mentionRoom?.roomId ?? roomIdOrAlias}
|
data-mention-id={mentionRoom?.roomId ?? roomIdOrAlias}
|
||||||
data-mention-via={viaServers?.join(',')}
|
data-mention-via={viaServers?.join(',')}
|
||||||
>
|
>
|
||||||
{mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias}
|
{customProps.children ? customProps.children : fallbackContent}
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -118,7 +128,9 @@ export const renderMatrixMention = (
|
||||||
data-mention-event-id={eventId}
|
data-mention-event-id={eventId}
|
||||||
data-mention-via={viaServers?.join(',')}
|
data-mention-via={viaServers?.join(',')}
|
||||||
>
|
>
|
||||||
Message: {mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias}
|
{customProps.children
|
||||||
|
? customProps.children
|
||||||
|
: `Message: ${mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias}`}
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -327,12 +339,17 @@ export const getReactCustomHtmlParser = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name === 'a' && testMatrixTo(tryDecodeURIComponent(props.href))) {
|
if (name === 'a' && testMatrixTo(tryDecodeURIComponent(props.href))) {
|
||||||
|
const content = children.find((child) => !(child instanceof DOMText))
|
||||||
|
? undefined
|
||||||
|
: children.map((c) => (c instanceof DOMText ? c.data : '')).join();
|
||||||
|
|
||||||
const mention = renderMatrixMention(
|
const mention = renderMatrixMention(
|
||||||
mx,
|
mx,
|
||||||
roomId,
|
roomId,
|
||||||
tryDecodeURIComponent(props.href),
|
tryDecodeURIComponent(props.href),
|
||||||
makeMentionCustomProps(params.handleMentionClick)
|
makeMentionCustomProps(params.handleMentionClick, content)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (mention) return mention;
|
if (mention) return mention;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue