Add 100 character buffer to read more

This commit is contained in:
Josh Perez 2021-11-29 10:42:26 -05:00 committed by GitHub
parent c9678c4877
commit 29b4148889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 22 deletions

View file

@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import React, { useState } from 'react';
import { action } from '@storybook/addon-actions';
import { text } from '@storybook/addon-knobs';
@ -27,34 +27,49 @@ const createProps = (overrideProps: Partial<Props> = {}): Props => ({
text: text('text', overrideProps.text || ''),
});
story.add('Lots of cake with a cherry on top', () => (
<MessageBodyReadMore
{...createProps({
text: `x${'🍰'.repeat(399)}🍒`,
})}
function MessageBodyReadMoreTest({
text: messageBodyText,
}: {
text: string;
}): JSX.Element {
const [displayLimit, setDisplayLimit] = useState<number | undefined>();
return (
<MessageBodyReadMore
{...createProps({ text: messageBodyText })}
displayLimit={displayLimit}
messageExpanded={(_, newDisplayLimit) => setDisplayLimit(newDisplayLimit)}
/>
);
}
story.add('Long text + 100 more', () => (
<MessageBodyReadMoreTest
text={`${'test '.repeat(160)}${'extra '.repeat(10)}`}
/>
));
story.add('Cherry overflow', () => (
<MessageBodyReadMore
{...createProps({
text: `x${'🍰'.repeat(400)}🍒`,
})}
story.add('Lots of cake with some cherries on top', () => (
<MessageBodyReadMoreTest text={`x${'🍰'.repeat(399)}${'🍒'.repeat(100)}`} />
));
story.add('Leafy not buffered', () => (
<MessageBodyReadMoreTest text={`x${'🌿'.repeat(450)}`} />
));
story.add('Links', () => (
<MessageBodyReadMoreTest
text={`${'test '.repeat(176)}https://www.signal.org`}
/>
));
story.add('Excessive amounts of cake', () => (
<MessageBodyReadMore
{...createProps({
text: `x${'🍰'.repeat(20000)}`,
})}
/>
<MessageBodyReadMoreTest text={`x${'🍰'.repeat(20000)}`} />
));
story.add('Long text', () => (
<MessageBodyReadMore
{...createProps({
text: `
<MessageBodyReadMoreTest
text={`
SCENE I. Rome. A street.
Enter FLAVIUS, MARULLUS, and certain Commoners
FLAVIUS
@ -151,7 +166,6 @@ story.add('Long text', () => (
Will make him fly an ordinary pitch,
Who else would soar above the view of men
And keep us all in servile fearfulness.
`,
})}
`}
/>
));

View file

@ -25,6 +25,7 @@ export type Props = Pick<
const INITIAL_LENGTH = 800;
const INCREMENT_COUNT = 3000;
const BUFFER = 100;
function graphemeAwareSlice(
str: string,
@ -33,7 +34,7 @@ function graphemeAwareSlice(
hasReadMore: boolean;
text: string;
} {
if (str.length <= length) {
if (str.length <= length + BUFFER) {
return { text: str, hasReadMore: false };
}