Skip to content

Commit

Permalink
Added loop flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xSentry committed Jul 1, 2024
1 parent 6ad57b7 commit 9de1e80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
5 changes: 4 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ typewriter.typeWriteEffect(['Hello', 'World'])
## API

```typescript
typeWriteEffect(words: string[], writeSpeed?: number, deleteDelay?: number, writeDelay?: number): Observable<string>
typeWriteEffect(words: string[], loop?: boolean, writeSpeed?: number, deleteDelay?: number, writeDelay?: number): Observable<string>
```

Starts the typewriter effect.

- `words`: An array of strings to be typed.
- `loop`: (optional): If set to false, the last word in the words array will stay.
- `writeSpeed` (optional): The speed of typing each character in milliseconds. Default is 40.
- `deleteDelay` (optional): The delay before starting to delete the typed word in milliseconds. Default is 3500.
- `writeDelay` (optional): The delay before starting to type the next word after deleting the current word in milliseconds. Default is 50.
Expand All @@ -52,6 +53,7 @@ const typewriter = new TypeWriter();

typewriter.typeWriteEffect(
['Hello', 'World'],
true, // loop (default true)
100, // write speed
2000, // delete delay
100 // write delay
Expand All @@ -70,6 +72,7 @@ const typewriter = new TypeWriter();

typewriter.typeWriteEffect(
['Hello', 'World'],
true, // loop (default true)
100, // Custom write speed
2000, // Custom delete delay
100 // Custom write delay
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@omnedia/rxjs-typewriter",
"version": "1.0.0",
"version": "1.1.0",
"description": "A simple typewriter effect to animate text.",
"private": false,
"main": "./dist/index.js",
Expand Down
26 changes: 22 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,42 @@ export class TypeWriter {
private writeSpeed = 40;
private waitAfterTyped = 3500;
private waitAfterDeleted = 50;
private loop = true;
private wordsCount = 0;

typeWriteEffect(
words: string[],
loop?: boolean,
writeSpeed?: number,
deleteDelay?: number,
writeDelay?: number
): Observable<string> {
this.writeSpeed = writeSpeed ?? this.writeSpeed;
this.waitAfterTyped = deleteDelay ?? this.waitAfterTyped;
this.waitAfterDeleted = writeDelay ?? this.waitAfterDeleted;
this.loop = loop ?? this.loop;

this.wordsCount = words.length;

return this.startTypewriter(words);
}

private startTypewriter(words: string[]): Observable<string> {
if (this.loop === false) {
return from(words).pipe(concatMap((word) => this.typeEffect(word)));
}

return from(words).pipe(
concatMap((word) => this.typeEffect(word)),
repeat()
);
}

private typeEffect(word: string): Observable<string> {
if (this.loop === false) {
this.wordsCount -= 1;
}

return concat(
this.typeWord(word),
of("").pipe(delay(this.waitAfterTyped), ignoreElements()),
Expand All @@ -47,12 +61,16 @@ export class TypeWriter {
}

private typeWord(word: string, backwards?: boolean): Observable<string> {
if (this.loop === false && this.wordsCount <= 0 && backwards) {
return of(word);
}

return interval(this.writeSpeed).pipe(
map((x) =>
backwards
map((x) => {
return backwards
? word.substring(0, word.length - x)
: word.substring(0, x + 1)
),
: word.substring(0, x + 1);
}),
take(word.length + 1)
);
}
Expand Down

0 comments on commit 9de1e80

Please sign in to comment.