Skip to content

Commit

Permalink
factor out a function for each key action, phetsims/chipper#1319
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jan 25, 2023
1 parent 9ac8847 commit f6c5c74
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 deletions js/DynamicStringTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,66 +36,78 @@ export default class DynamicStringTest {
const words = WORD_SOURCE.split( ' ' );

function setStride( newStride: number ): void {

// Handle wraparound.
if ( newStride > words.length - 1 ) {
newStride = 0;
}
else if ( newStride < 0 ) {
newStride = words.length - 1;
}

stride = newStride;
console.log( 'stride = ' + stride );

localizedStrings.forEach( ( localizedString, index ) => {
localizedString.property.value = words[ ( index + stride ) % words.length ];
} );
}

window.addEventListener( 'keydown', event => {

// Left Arrow: halve string
// Right Arrow: double string
if ( event.keyCode === LEFT_ARROW || event.keyCode === RIGHT_ARROW ) {
function doubleStrings(): void {
stringFactor = stringFactor * 2;
applyToStrings( stringFactor );
}

// Set the string factor based on left (halve) or right (double) arrow keys.
stringFactor = event.keyCode === LEFT_ARROW ? Math.max( stringFactor * 0.5, 0.01 ) : stringFactor * 2;
console.log( `stringFactor = ${stringFactor}` );
function halveStrings(): void {
stringFactor = Math.max( stringFactor * 0.5, 0.01 );
applyToStrings( stringFactor );
}

localizedStrings.forEach( localizedString => {
localizedString.restoreInitialValue( 'en' );
function reset(): void {
stringFactor = 1;
console.log( `stringFactor = ${stringFactor}` );
stride = 0;
console.log( `stride = ${stride}` );
localizedStrings.forEach( localizedString => localizedString.restoreInitialValue( 'en' ) );
}

// Strip out all RTL (U+202A), LTR (U+202B), and PDF (U+202C) characters from string.
const strippedString = localizedString.property.value.replace( /[\u202A\u202B\u202C]/g, '' );
localizedString.property.value = applyStringFactor( strippedString, stringFactor );
} );
window.addEventListener( 'keydown', event => {
if ( event.keyCode === LEFT_ARROW ) {
halveStrings();
}
else if ( event.keyCode === RIGHT_ARROW ) {
doubleStrings();
}

// Space Bar: reset
else if ( event.keyCode === SPACE_BAR ) {
stringFactor = 1;
console.log( `stringFactor = ${stringFactor}` );
stride = 0;
console.log( `stride = ${stride}` );
localizedStrings.forEach( localizedString => localizedString.restoreInitialValue( 'en' ) );
reset();
}

// Up Arrow: increase stride, with wraparound
else if ( event.keyCode === UP_ARROW ) {
let newStride = stride + 1;
if ( newStride > words.length - 1 ) {
newStride = 0;
}
setStride( newStride );
setStride( stride + 1 );
}

// Down Arrow: decrease stride, with wraparound
else if ( event.keyCode === DOWN_ARROW ) {
let newStride = stride - 1;
if ( newStride < 0 ) {
newStride = words.length - 1;
}
setStride( newStride );
setStride( stride - 1 );
}
} );
}
}

/**
* Returns a string with its length based on the string factor.
* Applies the string factor to all strings.
*/
function applyToStrings( factor: number ): void {
localizedStrings.forEach( localizedString => {
localizedString.restoreInitialValue( 'en' );

// Strip out all RTL (U+202A), LTR (U+202B), and PDF (U+202C) characters from string.
const strippedString = localizedString.property.value.replace( /[\u202A\u202B\u202C]/g, '' );
localizedString.property.value = applyToString( strippedString, factor );
} );
}

/**
* Applies the string factor to one string.
*/
function applyStringFactor( string: string, factor: number ): string {
function applyToString( string: string, factor: number ): string {
assert && assert( factor > 0, `factor must be > 0: ${factor}` );

if ( factor > 1 ) {
Expand Down

0 comments on commit f6c5c74

Please sign in to comment.