Refactor link/emoji/newline components for composability
parent
a5416e42c4
commit
d9e5338dff
@ -0,0 +1,35 @@
|
|||||||
|
### All newlines
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<AddNewLines text="\n\n\n" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Starting and ending with newlines
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<AddNewLines text="\nin between\n" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### With newlines in the middle
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<AddNewLines text="Before \n\n after" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### No newlines
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<AddNewLines text="This is the text" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Providing custom non-newline render function
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
const renderNonNewLine = ({ text, key }) => (
|
||||||
|
<span key={key}>This is my custom content!</span>
|
||||||
|
);
|
||||||
|
<AddNewLines
|
||||||
|
text="\n first \n second \n"
|
||||||
|
renderNonNewLine={renderNonNewLine}
|
||||||
|
/>;
|
||||||
|
```
|
@ -0,0 +1,60 @@
|
|||||||
|
### All emoji
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥🔥🔥" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### With skin color modifier
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="👍🏾" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### With `sizeClass` provided
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥" sizeClass="jumbo" />
|
||||||
|
```
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥" sizeClass="large" />
|
||||||
|
```
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥" sizeClass="medium" />
|
||||||
|
```
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥" sizeClass="small" />
|
||||||
|
```
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥" sizeClass="" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Starting and ending with emoji
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="🔥in between🔥" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### With emoji in the middle
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="Before 🔥🔥 after" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### No emoji
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Emojify text="This is the text" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Providing custom non-link render function
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
const renderNonEmoji = ({ text, key }) => (
|
||||||
|
<span key={key}>This is my custom content</span>
|
||||||
|
);
|
||||||
|
<Emojify text="Before 🔥🔥 after" renderNonEmoji={renderNonEmoji} />;
|
||||||
|
```
|
@ -0,0 +1,44 @@
|
|||||||
|
### All link
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Linkify text="https://somewhere.com" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Starting and ending with link
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Linkify text="https://somewhere.com Yes? No? https://anotherlink.com" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### With a link in the middle
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Linkify text="Before. https://somewhere.com After." />
|
||||||
|
```
|
||||||
|
|
||||||
|
### No link
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Linkify text="Plain text" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Should not render as link
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Linkify text="smailto:someone@somewhere.com - ftp://something.com - //local/share - \\local\share" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Should render as link
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
<Linkify text="github.com - https://blah.com" />
|
||||||
|
```
|
||||||
|
|
||||||
|
### Providing custom non-link render function
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
const renderNonLink = ({ text, key }) => (
|
||||||
|
<span key={key}>This is my custom non-link content!</span>
|
||||||
|
);
|
||||||
|
<Linkify text="Before github.com After" renderNonLink={renderNonLink} />;
|
||||||
|
```
|
@ -0,0 +1,72 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import createLinkify from 'linkify-it';
|
||||||
|
|
||||||
|
import { RenderTextCallback } from '../../types/Util';
|
||||||
|
|
||||||
|
const linkify = createLinkify();
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
text: string;
|
||||||
|
/** Allows you to customize now non-links are rendered. Simplest is just a <span>. */
|
||||||
|
renderNonLink?: RenderTextCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SUPPORTED_PROTOCOLS = /^(http|https):/i;
|
||||||
|
|
||||||
|
export class Linkify extends React.Component<Props, {}> {
|
||||||
|
public static defaultProps: Partial<Props> = {
|
||||||
|
renderNonLink: ({ text, key }) => <span key={key}>{text}</span>,
|
||||||
|
};
|
||||||
|
|
||||||
|
public render() {
|
||||||
|
const { text, renderNonLink } = this.props;
|
||||||
|
const matchData = linkify.match(text) || [];
|
||||||
|
const results: Array<any> = [];
|
||||||
|
let last = 0;
|
||||||
|
let count = 1;
|
||||||
|
|
||||||
|
// We have to do this, because renderNonLink is not required in our Props object,
|
||||||
|
// but it is always provided via defaultProps.
|
||||||
|
if (!renderNonLink) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchData.length === 0) {
|
||||||
|
return renderNonLink({ text, key: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
|
matchData.forEach(
|
||||||
|
(match: {
|
||||||
|
index: number;
|
||||||
|
url: string;
|
||||||
|
lastIndex: number;
|
||||||
|
text: string;
|
||||||
|
}) => {
|
||||||
|
if (last < match.index) {
|
||||||
|
const textWithNoLink = text.slice(last, match.index);
|
||||||
|
results.push(renderNonLink({ text: textWithNoLink, key: count++ }));
|
||||||
|
}
|
||||||
|
|
||||||
|
const { url, text: originalText } = match;
|
||||||
|
if (SUPPORTED_PROTOCOLS.test(url)) {
|
||||||
|
results.push(
|
||||||
|
<a key={count++} href={url}>
|
||||||
|
{originalText}
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
results.push(renderNonLink({ text: originalText, key: count++ }));
|
||||||
|
}
|
||||||
|
|
||||||
|
last = match.lastIndex;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (last < text.length) {
|
||||||
|
results.push(renderNonLink({ text: text.slice(last), key: count++ }));
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
}
|
@ -1,67 +1,46 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import createLinkify from 'linkify-it';
|
|
||||||
|
|
||||||
import { getSizeClass } from '../../util/emoji';
|
import { getSizeClass } from '../../util/emoji';
|
||||||
import { Emojify } from './Emojify';
|
import { Emojify } from './Emojify';
|
||||||
|
import { AddNewLines } from './AddNewLines';
|
||||||
|
import { Linkify } from './Linkify';
|
||||||
|
|
||||||
const linkify = createLinkify();
|
import { RenderTextCallback } from '../../types/Util';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
text: string;
|
text: string;
|
||||||
|
/** If set, all emoji will be the same size. Otherwise, just one emoji will be large. */
|
||||||
disableJumbomoji?: boolean;
|
disableJumbomoji?: boolean;
|
||||||
|
/** If set, links will be left alone instead of turned into clickable `<a>` tags. */
|
||||||
disableLinks?: boolean;
|
disableLinks?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SUPPORTED_PROTOCOLS = /^(http|https):/i;
|
const renderNewLines: RenderTextCallback = ({
|
||||||
|
text: textWithNewLines,
|
||||||
|
key,
|
||||||
|
}) => <AddNewLines key={key} text={textWithNewLines} />;
|
||||||
|
|
||||||
|
const renderLinks: RenderTextCallback = ({ text: textWithLinks, key }) => (
|
||||||
|
<Linkify key={key} text={textWithLinks} renderNonLink={renderNewLines} />
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This component makes it very easy to use all three of our message formatting
|
||||||
|
* components: `Emojify`, `Linkify`, and `AddNewLines`. Because each of them is fully
|
||||||
|
* configurable with their `renderXXX` props, this component will assemble all three of
|
||||||
|
* them for you.
|
||||||
|
*/
|
||||||
export class MessageBody extends React.Component<Props, {}> {
|
export class MessageBody extends React.Component<Props, {}> {
|
||||||
public render() {
|
public render() {
|
||||||
const { text, disableJumbomoji, disableLinks } = this.props;
|
const { text, disableJumbomoji, disableLinks } = this.props;
|
||||||
const matchData = linkify.match(text) || [];
|
|
||||||
const results: Array<any> = [];
|
|
||||||
let last = 0;
|
|
||||||
let count = 1;
|
|
||||||
|
|
||||||
// We only use this sizeClass if there was no link detected, because jumbo emoji
|
|
||||||
// only fire when there's no other text in the message.
|
|
||||||
const sizeClass = disableJumbomoji ? '' : getSizeClass(text);
|
const sizeClass = disableJumbomoji ? '' : getSizeClass(text);
|
||||||
|
|
||||||
if (disableLinks || matchData.length === 0) {
|
return (
|
||||||
return <Emojify text={text} sizeClass={sizeClass} />;
|
<Emojify
|
||||||
}
|
text={text}
|
||||||
|
sizeClass={sizeClass}
|
||||||
matchData.forEach(
|
renderNonEmoji={disableLinks ? renderNewLines : renderLinks}
|
||||||
(match: {
|
/>
|
||||||
index: number;
|
|
||||||
url: string;
|
|
||||||
lastIndex: number;
|
|
||||||
text: string;
|
|
||||||
}) => {
|
|
||||||
if (last < match.index) {
|
|
||||||
const textWithNoLink = text.slice(last, match.index);
|
|
||||||
results.push(<Emojify key={count++} text={textWithNoLink} />);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { url, text: originalText } = match;
|
|
||||||
if (SUPPORTED_PROTOCOLS.test(url)) {
|
|
||||||
results.push(
|
|
||||||
<a key={count++} href={url}>
|
|
||||||
{originalText}
|
|
||||||
</a>
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
results.push(<Emojify key={count++} text={originalText} />);
|
|
||||||
}
|
|
||||||
|
|
||||||
last = match.lastIndex;
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (last < text.length) {
|
|
||||||
results.push(<Emojify key={count++} text={text.slice(last)} />);
|
|
||||||
}
|
|
||||||
|
|
||||||
return <span>{results}</span>;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
export type RenderTextCallback = (
|
||||||
|
options: {
|
||||||
|
text: string;
|
||||||
|
key: number;
|
||||||
|
}
|
||||||
|
) => JSX.Element;
|
||||||
|
|
||||||
|
export type Localizer = (key: string, values?: Array<string>) => string;
|
Loading…
Reference in New Issue