signal-desktop/ts/components/conversation/AddNewLines.tsx
Scott Nonnenberg 2988da0981 Turn on all of Microsoft's recommend lint rules
Biggest changes forced by this: alt tags for all images, resulting in
new strings added to messages.json, and a new i18n paramter/prop added
in a plot of places.

Another change of note is that there are two new tslint.json files under
ts/test and ts/styleguide to relax our rules a bit there. This required
a change to our package.json script, as manually specifying the config
file there made it ignore our tslint.json files in subdirectories
2018-05-23 16:26:47 -07:00

56 lines
1.4 KiB
TypeScript

import React from 'react';
import { RenderTextCallback } from '../../types/Util';
interface Props {
text: string;
/** Allows you to customize now non-newlines are rendered. Simplest is just a <span>. */
renderNonNewLine?: RenderTextCallback;
}
export class AddNewLines extends React.Component<Props> {
public static defaultProps: Partial<Props> = {
renderNonNewLine: ({ text, key }) => <span key={key}>{text}</span>,
};
public render() {
const { text, renderNonNewLine } = this.props;
const results: Array<any> = [];
const FIND_NEWLINES = /\n/g;
// We have to do this, because renderNonNewLine is not required in our Props object,
// but it is always provided via defaultProps.
if (!renderNonNewLine) {
return;
}
let match = FIND_NEWLINES.exec(text);
let last = 0;
let count = 1;
if (!match) {
return renderNonNewLine({ text, key: 0 });
}
while (match) {
if (last < match.index) {
const textWithNoNewline = text.slice(last, match.index);
results.push(
renderNonNewLine({ text: textWithNoNewline, key: count++ })
);
}
results.push(<br key={count++} />);
// @ts-ignore
last = FIND_NEWLINES.lastIndex;
match = FIND_NEWLINES.exec(text);
}
if (last < text.length) {
results.push(renderNonNewLine({ text: text.slice(last), key: count++ }));
}
return results;
}
}