Skip to content

Commit

Permalink
Add animated text
Browse files Browse the repository at this point in the history
  • Loading branch information
millerf committed Jan 20, 2023
1 parent 69828a5 commit f5b43ca
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions projects/plugins/jetpack/extensions/blocks/ai-paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,33 @@ import { sprintf, __ } from '@wordpress/i18n';

const numberOfCharactersNeeded = 36;

function ShowLittleByLittle( { html, showAnimation, attributes, setAttributes } ) {
if ( showAnimation ) {
// This is to animate text input. I think this will give an idea of a "better" AI.
// At this point this is an established pattern.
const tokens = html.split( ' ' );
for ( let i = 1; i < tokens.length; i++ ) {
const output = tokens.slice( 0, i ).join( ' ' );
setTimeout( () => setAttributes( { content: output } ), 50 * i );
}
setTimeout( () => setAttributes( { content: html } ), 50 * tokens.length );
} else {
setAttributes( { content: html } );
}

return (
<div { ...useBlockProps() }>
<div className="content">
<RawHTML>{ attributes.content }</RawHTML>
</div>
</div>
);
}

export default function Edit( { attributes, setAttributes, clientId } ) {
const [ loadingCompletion, setLoadingCompletion ] = useState( false );
const [ loadingCategories, setLoadingCategories ] = useState( false );
const [ showAnimation, setShowAnimation ] = useState( false );
const [ needsMoreCharacters, setNeedsMoreCharacters ] = useState( 0 );
const [ showRetry, setShowRetry ] = useState( false );
const [ completionFinished, setCompletionFinished ] = useState( !! attributes.content );
Expand Down Expand Up @@ -157,6 +181,7 @@ export default function Edit( { attributes, setAttributes, clientId } ) {

// We set it up so it doesn't start with nothing
setCompletionFinished( true );
setShowAnimation( true );
setLoadingCompletion( false );
setAttributes( { content: result } );
} )
Expand Down Expand Up @@ -219,11 +244,10 @@ export default function Edit( { attributes, setAttributes, clientId } ) {
) }

{ attributes.content && (
<div>
<div className="content">
<RawHTML>{ attributes.content.trim().replaceAll( '\n', '<br/>' ) }</RawHTML>
</div>
</div>
<ShowLittleByLittle
showAnimation={ showAnimation }
html={ attributes.content.trim().replaceAll( '\n', '<br/>' ) }
/>
) }

{ ! attributes.content && ( loadingCompletion || loadingCategories ) && (
Expand Down

0 comments on commit f5b43ca

Please sign in to comment.