@@ -114,6 +114,40 @@ module.exports =
114
114
} ) ;
115
115
} ;
116
116
117
+ _this . typeLine = function ( line , lineIdx ) {
118
+ if ( ! _this . mounted ) {
119
+ return Promise . resolve ( ) ;
120
+ }
121
+ var onLineTyped = _this . props . onLineTyped ;
122
+
123
+
124
+ return new Promise ( function ( resolve , reject ) {
125
+ _this . setState ( { text : _this . state . text . concat ( [ '' ] ) } , function ( ) {
126
+ utils . eachPromise ( line , _this . typeCharacter , line , lineIdx ) . then ( function ( ) {
127
+ return onLineTyped ( line , lineIdx ) ;
128
+ } ) . then ( resolve ) . catch ( reject ) ;
129
+ } ) ;
130
+ } ) ;
131
+ } ;
132
+
133
+ _this . typeCharacter = function ( character , charIdx , line , lineIdx ) {
134
+ if ( ! _this . mounted ) {
135
+ return Promise . resolve ( ) ;
136
+ }
137
+ var onCharacterTyped = _this . props . onCharacterTyped ;
138
+
139
+
140
+ return new Promise ( function ( resolve ) {
141
+ var text = _this . state . text . slice ( ) ;
142
+ text [ lineIdx ] += character ;
143
+ _this . setState ( { text : text } , function ( ) {
144
+ onCharacterTyped ( character , charIdx ) ;
145
+ var delay = _this . delayGenerator ( line , lineIdx , character , charIdx ) ;
146
+ setTimeout ( resolve , delay ) ;
147
+ } ) ;
148
+ } ) ;
149
+ } ;
150
+
117
151
_this . mounted = false ;
118
152
_this . linesToType = [ ] ;
119
153
@@ -163,38 +197,10 @@ module.exports =
163
197
164
198
var lines = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : this . linesToType ;
165
199
166
- return utils . eachPromise ( lines , function ( line , idx ) {
167
- if ( ! _this2 . mounted ) {
168
- return Promise . resolve ( ) ;
169
- }
170
- return new Promise ( function ( resolve ) {
171
- _this2 . setState ( { text : _this2 . state . text . concat ( [ '' ] ) } , function ( ) {
172
- _this2 . typeLine ( line , idx ) . then ( resolve ) ;
173
- } ) ;
174
- } ) ;
175
- } ) . then ( function ( ) {
200
+ return utils . eachPromise ( lines , this . typeLine ) . then ( function ( ) {
176
201
return _this2 . onTypingDone ( ) ;
177
202
} ) ;
178
203
}
179
- } , {
180
- key : 'typeLine' ,
181
- value : function typeLine ( line , lineIdx ) {
182
- var _this3 = this ;
183
-
184
- return utils . eachPromise ( line , function ( character , charIdx ) {
185
- if ( ! _this3 . mounted ) {
186
- return Promise . resolve ( ) ;
187
- }
188
- return new Promise ( function ( resolve ) {
189
- var text = _this3 . state . text . slice ( ) ;
190
- text [ lineIdx ] += character ;
191
- _this3 . setState ( { text : text } , function ( ) {
192
- var delay = _this3 . delayGenerator ( line , lineIdx , character , charIdx ) ;
193
- setTimeout ( resolve , delay ) ;
194
- } ) ;
195
- } ) ;
196
- } ) ;
197
- }
198
204
} , {
199
205
key : 'render' ,
200
206
value : function render ( ) {
@@ -224,6 +230,8 @@ module.exports =
224
230
stdTypingDelay : _react . PropTypes . number ,
225
231
startDelay : _react . PropTypes . number ,
226
232
cursor : _react . PropTypes . object ,
233
+ onCharacterTyped : _react . PropTypes . func ,
234
+ onLineTyped : _react . PropTypes . func ,
227
235
onTypingDone : _react . PropTypes . func ,
228
236
delayGenerator : _react . PropTypes . func
229
237
} ;
@@ -233,6 +241,8 @@ module.exports =
233
241
stdTypingDelay : 25 ,
234
242
startDelay : 0 ,
235
243
cursor : { } ,
244
+ onCharacterTyped : function onCharacterTyped ( ) { } ,
245
+ onLineTyped : function onLineTyped ( ) { } ,
236
246
onTypingDone : function onTypingDone ( ) { } ,
237
247
delayGenerator : utils . gaussianRnd
238
248
} ;
@@ -273,20 +283,16 @@ module.exports =
273
283
var Cursor = function ( _Component ) {
274
284
_inherits ( Cursor , _Component ) ;
275
285
276
- function Cursor ( ) {
277
- var _ref ;
278
-
279
- var _temp , _this , _ret ;
280
-
286
+ function Cursor ( props ) {
281
287
_classCallCheck ( this , Cursor ) ;
282
288
283
- for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
284
- args [ _key ] = arguments [ _key ] ;
285
- }
289
+ var _this = _possibleConstructorReturn ( this , ( Cursor . __proto__ || Object . getPrototypeOf ( Cursor ) ) . call ( this , props ) ) ;
286
290
287
- return _ret = ( _temp = ( _this = _possibleConstructorReturn ( this , ( _ref = Cursor . __proto__ || Object . getPrototypeOf ( Cursor ) ) . call . apply ( _ref , [ this ] . concat ( args ) ) ) , _this ) , _this . state = {
291
+ _this . _isReRenderingCursor = false ;
292
+ _this . state = {
288
293
shouldRender : _this . props . show
289
- } , _temp ) , _possibleConstructorReturn ( _this , _ret ) ;
294
+ } ;
295
+ return _this ;
290
296
}
291
297
292
298
_createClass ( Cursor , [ {
@@ -301,6 +307,49 @@ module.exports =
301
307
} , this . props . hideWhenDoneDelay ) ;
302
308
}
303
309
}
310
+ } , {
311
+ key : 'componentDidUpdate' ,
312
+ value : function componentDidUpdate ( ) {
313
+ var _props = this . props ;
314
+ var show = _props . show ;
315
+ var isDone = _props . isDone ;
316
+
317
+ if ( ! show ) {
318
+ return ;
319
+ }
320
+ if ( isDone ) {
321
+ return ;
322
+ }
323
+ if ( this . _isReRenderingCursor ) {
324
+ return ;
325
+ }
326
+
327
+ // In webkit and blink, rendering the cursor alongside the text as it
328
+ // animates sometimes causes the text to stop rendering when it reaches
329
+ // a new line break, even though the underlying DOM /does/ contain
330
+ // the text. This seems to happen when the space available for the text is
331
+ // at a specific width that makes it so the line break happens within a
332
+ // word.
333
+ // Using dev tools, when in this state, if you modify the dom or any style,
334
+ // it immediately renders all of the text in its correct position.
335
+ // Given that observation, this is a hackish solutions that re-renders the
336
+ // cursor every time a character is rendered, just to ensure that the text
337
+ // is rendered correctly.
338
+ // See #13 and #15 for more details
339
+ this . _reRenderCursor ( ) ;
340
+ }
341
+ } , {
342
+ key : '_reRenderCursor' ,
343
+ value : function _reRenderCursor ( ) {
344
+ var _this3 = this ;
345
+
346
+ this . _isReRenderingCursor = true ;
347
+ this . setState ( { shouldRender : false } , function ( ) {
348
+ _this3 . setState ( { shouldRender : true } , function ( ) {
349
+ _this3 . _isReRenderingCursor = false ;
350
+ } ) ;
351
+ } ) ;
352
+ }
304
353
} , {
305
354
key : 'render' ,
306
355
value : function render ( ) {
@@ -388,15 +437,16 @@ module.exports =
388
437
}
389
438
390
439
function eachPromise ( arr , iterator ) {
391
- var length = arr . length ;
440
+ for ( var _len = arguments . length , extraArgs = Array ( _len > 2 ? _len - 2 : 0 ) , _key = 2 ; _key < _len ; _key ++ ) {
441
+ extraArgs [ _key - 2 ] = arguments [ _key ] ;
442
+ }
392
443
393
- return Array . from ( arr ) . reduce ( function ( prev , current , idx ) {
444
+ var promiseReducer = function promiseReducer ( prev , current , idx ) {
394
445
return prev . then ( function ( ) {
395
- return Promise . resolve ( current ) . then ( function ( val ) {
396
- return iterator ( val , idx , length ) ;
397
- } ) ;
446
+ return iterator . apply ( undefined , [ current , idx ] . concat ( extraArgs ) ) ;
398
447
} ) ;
399
- } , Promise . resolve ( ) ) ;
448
+ } ;
449
+ return Array . from ( arr ) . reduce ( promiseReducer , Promise . resolve ( ) ) ;
400
450
}
401
451
402
452
function exclude ( obj , keys ) {
@@ -465,8 +515,8 @@ module.exports =
465
515
}
466
516
467
517
function extractTreeWithText ( ) {
468
- for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
469
- args [ _key ] = arguments [ _key ] ;
518
+ for ( var _len2 = arguments . length , args = Array ( _len2 ) , _key2 = 0 ; _key2 < _len2 ; _key2 ++ ) {
519
+ args [ _key2 ] = arguments [ _key2 ] ;
470
520
}
471
521
472
522
if ( ! args [ 0 ] ) return void 0 ;
0 commit comments