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 // Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import React from 'react'; import React, { useState } from 'react';
import { action } from '@storybook/addon-actions'; import { action } from '@storybook/addon-actions';
import { text } from '@storybook/addon-knobs'; import { text } from '@storybook/addon-knobs';
@ -27,34 +27,49 @@ const createProps = (overrideProps: Partial<Props> = {}): Props => ({
text: text('text', overrideProps.text || ''), text: text('text', overrideProps.text || ''),
}); });
story.add('Lots of cake with a cherry on top', () => ( function MessageBodyReadMoreTest({
<MessageBodyReadMore text: messageBodyText,
{...createProps({ }: {
text: `x${'🍰'.repeat(399)}🍒`, 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', () => ( story.add('Lots of cake with some cherries on top', () => (
<MessageBodyReadMore <MessageBodyReadMoreTest text={`x${'🍰'.repeat(399)}${'🍒'.repeat(100)}`} />
{...createProps({ ));
text: `x${'🍰'.repeat(400)}🍒`,
})} 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', () => ( story.add('Excessive amounts of cake', () => (
<MessageBodyReadMore <MessageBodyReadMoreTest text={`x${'🍰'.repeat(20000)}`} />
{...createProps({
text: `x${'🍰'.repeat(20000)}`,
})}
/>
)); ));
story.add('Long text', () => ( story.add('Long text', () => (
<MessageBodyReadMore <MessageBodyReadMoreTest
{...createProps({ text={`
text: `
SCENE I. Rome. A street. SCENE I. Rome. A street.
Enter FLAVIUS, MARULLUS, and certain Commoners Enter FLAVIUS, MARULLUS, and certain Commoners
FLAVIUS FLAVIUS
@ -151,7 +166,6 @@ story.add('Long text', () => (
Will make him fly an ordinary pitch, Will make him fly an ordinary pitch,
Who else would soar above the view of men Who else would soar above the view of men
And keep us all in servile fearfulness. And keep us all in servile fearfulness.
`, `}
})}
/> />
)); ));

View file

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