Preserve links with embedded emoji

This commit is contained in:
Scott Nonnenberg 2019-01-02 11:56:33 -08:00
parent dc9f9dabfd
commit 6eb8f5680c
4 changed files with 54 additions and 11 deletions

View file

@ -8,6 +8,7 @@ import {
getRegex, getRegex,
getReplacementData, getReplacementData,
getTitle, getTitle,
SizeClassType,
} from '../../util/emoji'; } from '../../util/emoji';
import { Localizer, RenderTextCallback } from '../../types/Util'; import { Localizer, RenderTextCallback } from '../../types/Util';
@ -20,7 +21,7 @@ function getImageTag({
i18n, i18n,
}: { }: {
match: any; match: any;
sizeClass: string | undefined; sizeClass?: SizeClassType;
key: string | number; key: string | number;
i18n: Localizer; i18n: Localizer;
}) { }) {
@ -51,7 +52,7 @@ function getImageTag({
interface Props { interface Props {
text: string; text: string;
/** A class name to be added to the generated emoji images */ /** 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 <span>. */ /** Allows you to customize now non-newlines are rendered. Simplest is just a <span>. */
renderNonEmoji?: RenderTextCallback; renderNonEmoji?: RenderTextCallback;
i18n: Localizer; i18n: Localizer;

View file

@ -44,3 +44,9 @@
```jsx ```jsx
<MessageBody text="http://somewhere.com" disableLinks i18n={util.i18n} /> <MessageBody text="http://somewhere.com" disableLinks i18n={util.i18n} />
``` ```
### Emoji in link
```jsx
<MessageBody text="http://somewhere.com?s=🔥\nCool, huh?" i18n={util.i18n} />
```

View file

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { getSizeClass } from '../../util/emoji'; import { getSizeClass, SizeClassType } from '../../util/emoji';
import { Emojify } from './Emojify'; import { Emojify } from './Emojify';
import { AddNewLines } from './AddNewLines'; import { AddNewLines } from './AddNewLines';
import { Linkify } from './Linkify'; import { Linkify } from './Linkify';
@ -21,8 +21,26 @@ const renderNewLines: RenderTextCallback = ({
key, key,
}) => <AddNewLines key={key} text={textWithNewLines} />; }) => <AddNewLines key={key} text={textWithNewLines} />;
const renderLinks: RenderTextCallback = ({ text: textWithLinks, key }) => ( const renderEmoji = ({
<Linkify key={key} text={textWithLinks} renderNonLink={renderNewLines} /> i18n,
text,
key,
sizeClass,
renderNonEmoji,
}: {
i18n: Localizer;
text: string;
key: number;
sizeClass?: SizeClassType;
renderNonEmoji: RenderTextCallback;
}) => (
<Emojify
i18n={i18n}
key={key}
text={text}
sizeClass={sizeClass}
renderNonEmoji={renderNonEmoji}
/>
); );
/** /**
@ -34,14 +52,30 @@ const renderLinks: RenderTextCallback = ({ text: textWithLinks, key }) => (
export class MessageBody extends React.Component<Props> { export class MessageBody extends React.Component<Props> {
public render() { public render() {
const { text, disableJumbomoji, disableLinks, i18n } = this.props; 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 (
<Emojify <Linkify
text={text} text={text}
sizeClass={sizeClass} renderNonLink={({ key, text: nonLinkText }) => {
renderNonEmoji={disableLinks ? renderNewLines : renderLinks} return renderEmoji({
i18n={i18n} i18n,
text: nonLinkText,
sizeClass,
key,
renderNonEmoji: renderNewLines,
});
}}
/> />
); );
} }

View file

@ -10,6 +10,8 @@ instance.include_title = true;
instance.replace_mode = 'img'; instance.replace_mode = 'img';
instance.supports_css = false; // needed to avoid spans with background-image instance.supports_css = false; // needed to avoid spans with background-image
export type SizeClassType = '' | 'small' | 'medium' | 'large' | 'jumbo';
export function getRegex(): RegExp { export function getRegex(): RegExp {
return instance.rx_unified; return instance.rx_unified;
} }
@ -56,7 +58,7 @@ function hasNormalCharacters(str: string) {
return noEmoji.length > 0; return noEmoji.length > 0;
} }
export function getSizeClass(str: string) { export function getSizeClass(str: string): SizeClassType {
if (hasNormalCharacters(str)) { if (hasNormalCharacters(str)) {
return ''; return '';
} }