From 6eb8f5680c7144e3400054fc28643183dcbd8fb5 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 2 Jan 2019 11:56:33 -0800 Subject: [PATCH] Preserve links with embedded emoji --- ts/components/conversation/Emojify.tsx | 5 ++- ts/components/conversation/MessageBody.md | 6 +++ ts/components/conversation/MessageBody.tsx | 50 ++++++++++++++++++---- ts/util/emoji.ts | 4 +- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/ts/components/conversation/Emojify.tsx b/ts/components/conversation/Emojify.tsx index 1250a4697..0fb36b8ec 100644 --- a/ts/components/conversation/Emojify.tsx +++ b/ts/components/conversation/Emojify.tsx @@ -8,6 +8,7 @@ import { getRegex, getReplacementData, getTitle, + SizeClassType, } from '../../util/emoji'; import { Localizer, RenderTextCallback } from '../../types/Util'; @@ -20,7 +21,7 @@ function getImageTag({ i18n, }: { match: any; - sizeClass: string | undefined; + sizeClass?: SizeClassType; key: string | number; i18n: Localizer; }) { @@ -51,7 +52,7 @@ function getImageTag({ interface Props { text: string; /** A class name to be added to the generated emoji images */ - sizeClass?: '' | 'small' | 'medium' | 'large' | 'jumbo'; + sizeClass?: SizeClassType; /** Allows you to customize now non-newlines are rendered. Simplest is just a . */ renderNonEmoji?: RenderTextCallback; i18n: Localizer; diff --git a/ts/components/conversation/MessageBody.md b/ts/components/conversation/MessageBody.md index ce568c81e..2c9a87b51 100644 --- a/ts/components/conversation/MessageBody.md +++ b/ts/components/conversation/MessageBody.md @@ -44,3 +44,9 @@ ```jsx ``` + +### Emoji in link + +```jsx + +``` diff --git a/ts/components/conversation/MessageBody.tsx b/ts/components/conversation/MessageBody.tsx index 24f99c3ee..2c601a83d 100644 --- a/ts/components/conversation/MessageBody.tsx +++ b/ts/components/conversation/MessageBody.tsx @@ -1,6 +1,6 @@ import React from 'react'; -import { getSizeClass } from '../../util/emoji'; +import { getSizeClass, SizeClassType } from '../../util/emoji'; import { Emojify } from './Emojify'; import { AddNewLines } from './AddNewLines'; import { Linkify } from './Linkify'; @@ -21,8 +21,26 @@ const renderNewLines: RenderTextCallback = ({ key, }) => ; -const renderLinks: RenderTextCallback = ({ text: textWithLinks, key }) => ( - +const renderEmoji = ({ + i18n, + text, + key, + sizeClass, + renderNonEmoji, +}: { + i18n: Localizer; + text: string; + key: number; + sizeClass?: SizeClassType; + renderNonEmoji: RenderTextCallback; +}) => ( + ); /** @@ -34,14 +52,30 @@ const renderLinks: RenderTextCallback = ({ text: textWithLinks, key }) => ( export class MessageBody extends React.Component { public render() { const { text, disableJumbomoji, disableLinks, i18n } = this.props; - const sizeClass = disableJumbomoji ? '' : getSizeClass(text); + const sizeClass = disableJumbomoji ? undefined : getSizeClass(text); + + if (disableLinks) { + return renderEmoji({ + i18n, + text, + sizeClass, + key: 0, + renderNonEmoji: renderNewLines, + }); + } return ( - { + return renderEmoji({ + i18n, + text: nonLinkText, + sizeClass, + key, + renderNonEmoji: renderNewLines, + }); + }} /> ); } diff --git a/ts/util/emoji.ts b/ts/util/emoji.ts index f15e75282..1242e44f2 100644 --- a/ts/util/emoji.ts +++ b/ts/util/emoji.ts @@ -10,6 +10,8 @@ instance.include_title = true; instance.replace_mode = 'img'; instance.supports_css = false; // needed to avoid spans with background-image +export type SizeClassType = '' | 'small' | 'medium' | 'large' | 'jumbo'; + export function getRegex(): RegExp { return instance.rx_unified; } @@ -56,7 +58,7 @@ function hasNormalCharacters(str: string) { return noEmoji.length > 0; } -export function getSizeClass(str: string) { +export function getSizeClass(str: string): SizeClassType { if (hasNormalCharacters(str)) { return ''; }