Skip to content

Commit dd14aa3

Browse files
committed
Feat: Added onCharAppended support
1 parent a8c8686 commit dd14aa3

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Intention of this fork
2+
3+
This fork is strictly exists to replicate the solution for:
4+
https://stackoverflow.com/questions/49291904/play-sound-at-each-char-with-typedjs
5+
6+
Where a callback ("onCharAppended") is exposes for when each character is typed to allowing for playing a sound or other events on character typed.
7+
8+
# Original Repo's Readme
9+
110
[![Build Status](https://travis-ci.org/mattboldt/typed.js.svg?branch=typed-2.0)](https://travis-ci.org/mattboldt/typed.js)
211
[![Code Climate](https://codeclimate.com/github/mattboldt/typed.js/badges/gpa.svg)](https://codeclimate.com/github/mattboldt/typed.js)
312
[![GitHub release](https://img.shields.io/github/release/mattboldt/typed.js.svg)]()
@@ -38,7 +47,7 @@ import Typed from 'typed.js';
3847

3948
var options = {
4049
strings: ['<i>First</i> sentence.', '&amp; a second sentence.'],
41-
typeSpeed: 40
50+
typeSpeed: 40,
4251
};
4352

4453
var typed = new Typed('.element', options);
@@ -116,7 +125,7 @@ You can pause in the middle of a string for a given amount of time by including
116125
```javascript
117126
var typed = new Typed('.element', {
118127
// Waits 1000ms after typing "First"
119-
strings: ['First ^1000 sentence.', 'Second sentence.']
128+
strings: ['First ^1000 sentence.', 'Second sentence.'],
120129
});
121130
```
122131

@@ -127,7 +136,7 @@ In the following example, this would only backspace the words after "This is a"
127136
```javascript
128137
var typed = new Typed('.element', {
129138
strings: ['This is a JavaScript library', 'This is an ES6 module'],
130-
smartBackspace: true // Default value
139+
smartBackspace: true, // Default value
131140
});
132141
```
133142

@@ -137,7 +146,7 @@ The following example would emulate how a terminal acts when typing a command an
137146

138147
```javascript
139148
var typed = new Typed('.element', {
140-
strings: ['git push --force ^1000\n `pushed to origin with option force`']
149+
strings: ['git push --force ^1000\n `pushed to origin with option force`'],
141150
});
142151
```
143152

@@ -167,7 +176,7 @@ var typed = new Typed('.element', {
167176
'These are the default values...',
168177
'You know what you should do?',
169178
'Use your own!',
170-
'Have a great day!'
179+
'Have a great day!',
171180
],
172181
stringsElement: null,
173182

@@ -312,7 +321,7 @@ var typed = new Typed('.element', {
312321
* After destroy
313322
* @param {Typed} self
314323
*/
315-
onDestroy: (self) => {}
324+
onDestroy: (self) => {},
316325
});
317326
```
318327

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ declare module 'typed.js' {
121121
* After destroy
122122
*/
123123
onDestroy?(self: Typed): void;
124+
125+
onCharAppended: (char: string, self: Typed) => {};
124126
}
125127

126128
export default class Typed {

src/defaults.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,73 +92,79 @@ const defaults = {
9292
* Before it begins typing
9393
* @param {Typed} self
9494
*/
95-
onBegin: (self) => {},
95+
onBegin: (self) => { },
9696

9797
/**
9898
* All typing is complete
9999
* @param {Typed} self
100100
*/
101-
onComplete: (self) => {},
101+
onComplete: (self) => { },
102102

103103
/**
104104
* Before each string is typed
105105
* @param {number} arrayPos
106106
* @param {Typed} self
107107
*/
108-
preStringTyped: (arrayPos, self) => {},
108+
preStringTyped: (arrayPos, self) => { },
109109

110110
/**
111111
* After each string is typed
112112
* @param {number} arrayPos
113113
* @param {Typed} self
114114
*/
115-
onStringTyped: (arrayPos, self) => {},
115+
onStringTyped: (arrayPos, self) => { },
116116

117117
/**
118118
* During looping, after last string is typed
119119
* @param {Typed} self
120120
*/
121-
onLastStringBackspaced: (self) => {},
121+
onLastStringBackspaced: (self) => { },
122122

123123
/**
124124
* Typing has been stopped
125125
* @param {number} arrayPos
126126
* @param {Typed} self
127127
*/
128-
onTypingPaused: (arrayPos, self) => {},
128+
onTypingPaused: (arrayPos, self) => { },
129129

130130
/**
131131
* Typing has been started after being stopped
132132
* @param {number} arrayPos
133133
* @param {Typed} self
134134
*/
135-
onTypingResumed: (arrayPos, self) => {},
135+
onTypingResumed: (arrayPos, self) => { },
136136

137137
/**
138138
* After reset
139139
* @param {Typed} self
140140
*/
141-
onReset: (self) => {},
141+
onReset: (self) => { },
142142

143143
/**
144144
* After stop
145145
* @param {number} arrayPos
146146
* @param {Typed} self
147147
*/
148-
onStop: (arrayPos, self) => {},
148+
onStop: (arrayPos, self) => { },
149149

150150
/**
151151
* After start
152152
* @param {number} arrayPos
153153
* @param {Typed} self
154154
*/
155-
onStart: (arrayPos, self) => {},
155+
onStart: (arrayPos, self) => { },
156156

157157
/**
158158
* After destroy
159159
* @param {Typed} self
160160
*/
161-
onDestroy: (self) => {}
161+
onDestroy: (self) => { },
162+
/**
163+
* After a character has been appended.
164+
* @param {*} char The character that has been appended
165+
* @param {*} self
166+
*/
167+
onCharAppended: (char, self) => { },
162168
};
163169

164170
export default defaults;

src/typed.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export default class Typed {
187187

188188
// humanized value for typing
189189
}, humanize);
190+
this.options.onCharAppended(substr.charAt(0), this);
190191
}
191192

192193
/**

0 commit comments

Comments
 (0)