Skip to content

Commit d3ee1a9

Browse files
authored
Add onCharacterTyped and onLineTyped callback options (#22)
This commit adds 2 new callback options to hook into when Typist is typing a character and a line It also refactors and cleans up the code a bit
1 parent ac7aa41 commit d3ee1a9

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Refer to [`examples/`](/examples) for more examples.
7979
* [`stdTypingDelay`](#stdTypingDelay)
8080
* [`startDelay`](#startDelay)
8181
* [`cursor`](#cursor)
82+
* [`onCharacterTyped`](#onCharacterTyped)
83+
* [`onLineTyped`](#onLineTyped)
8284
* [`onTypingDone`](#onTypingDone)
8385
* [`delayGenerator`](#delayGenerator)
8486

@@ -142,6 +144,26 @@ animation is complete.
142144
* `hideWhenDoneDelay (int)`: delay in ms to be applied before hiding cursor when
143145
typing animation is complete.
144146

147+
<a name="onCharacterTyped"></a>
148+
#### onCharacterTyped
149+
Function to be called every time a character is typed on the screen.
150+
151+
```js
152+
function(character, charIdx) {
153+
...
154+
}
155+
```
156+
157+
<a name="onLineTyped"></a>
158+
#### onLineTyped
159+
Function to be called every time a line is typed on the screen.
160+
161+
```js
162+
function(line, lineIdx) {
163+
...
164+
}
165+
```
166+
145167
<a name="onTypingDone"></a>
146168
#### onTypingDone
147169
Function to be called when typing animation is complete.

src/Typist.jsx

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default class Typist extends Component {
1212
stdTypingDelay: PropTypes.number,
1313
startDelay: PropTypes.number,
1414
cursor: PropTypes.object,
15+
onCharacterTyped: PropTypes.func,
16+
onLineTyped: PropTypes.func,
1517
onTypingDone: PropTypes.func,
1618
delayGenerator: PropTypes.func,
1719
}
@@ -22,6 +24,8 @@ export default class Typist extends Component {
2224
stdTypingDelay: 25,
2325
startDelay: 0,
2426
cursor: {},
27+
onCharacterTyped: () => {},
28+
onLineTyped: () => {},
2529
onTypingDone: () => {},
2630
delayGenerator: utils.gaussianRnd,
2731
}
@@ -91,27 +95,35 @@ export default class Typist extends Component {
9195
}
9296

9397
typeAllLines(lines = this.linesToType) {
94-
return utils.eachPromise(lines, (line, idx) => {
95-
if (!this.mounted) { return Promise.resolve(); }
96-
return new Promise((resolve) => {
97-
this.setState({ text: this.state.text.concat(['']) }, () => {
98-
this.typeLine(line, idx).then(resolve);
99-
});
100-
});
101-
})
98+
return utils.eachPromise(lines, this.typeLine)
10299
.then(() => this.onTypingDone());
103100
}
104101

105-
typeLine(line, lineIdx) {
106-
return utils.eachPromise(line, (character, charIdx) => {
107-
if (!this.mounted) { return Promise.resolve(); }
108-
return new Promise((resolve) => {
109-
const text = this.state.text.slice();
110-
text[lineIdx] += character;
111-
this.setState({ text }, () => {
112-
const delay = this.delayGenerator(line, lineIdx, character, charIdx);
113-
setTimeout(resolve, delay);
114-
});
102+
typeLine = (line, lineIdx) => {
103+
if (!this.mounted) { return Promise.resolve(); }
104+
const { onLineTyped } = this.props;
105+
106+
return new Promise((resolve, reject) => {
107+
this.setState({ text: this.state.text.concat(['']) }, () => {
108+
utils.eachPromise(line, this.typeCharacter, line, lineIdx)
109+
.then(() => onLineTyped(line, lineIdx))
110+
.then(resolve)
111+
.catch(reject);
112+
});
113+
});
114+
}
115+
116+
typeCharacter = (character, charIdx, line, lineIdx) => {
117+
if (!this.mounted) { return Promise.resolve(); }
118+
const { onCharacterTyped } = this.props;
119+
120+
return new Promise((resolve) => {
121+
const text = this.state.text.slice();
122+
text[lineIdx] += character;
123+
this.setState({ text }, () => {
124+
onCharacterTyped(character, charIdx);
125+
const delay = this.delayGenerator(line, lineIdx, character, charIdx);
126+
setTimeout(resolve, delay);
115127
});
116128
});
117129
}

src/utils.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ export function gaussianRnd(mean, std) {
1212
return Math.round((sum) * std) + mean;
1313
}
1414

15-
export function eachPromise(arr, iterator) {
16-
const { length } = arr;
17-
return Array.from(arr).reduce((prev, current, idx) =>
18-
prev.then(() =>
19-
Promise.resolve(current)
20-
.then((val) => iterator(val, idx, length))
21-
)
22-
, Promise.resolve());
15+
export function eachPromise(arr, iterator, ...extraArgs) {
16+
const promiseReducer = (prev, current, idx) => (
17+
prev.then(() => iterator(current, idx, ...extraArgs))
18+
);
19+
return Array.from(arr).reduce(promiseReducer, Promise.resolve());
2320
}
2421

2522
export function exclude(obj, keys) {

0 commit comments

Comments
 (0)