@@ -206,10 +206,10 @@ class WriteItNode {
206206 // TODO: Check that matching closing bracket is present!
207207 i = waitTempIndex ;
208208 let secToWait = this . text . substring ( i + 2 , this . text . indexOf ( '}' , i ) ) ;
209- this . waitIndex [ "default" ] [ i ] = secToWait ;
209+ this . waitIndex [ "default" ] [ i ] = Number ( secToWait ) ;
210210 // this might return -1
211211 this . text = this . text . replace ( this . text . substring ( i , this . text . indexOf ( "}" , i ) + 1 ) , "" ) ;
212- temp += 2 ;
212+ temp += 1 ;
213213 } else if ( writeTempIndex > - 1 && ( writeTempIndex < waitTempIndex || waitTempIndex == - 1 ) ) {
214214 // set write.
215215 i = writeTempIndex ;
@@ -223,10 +223,13 @@ class WriteItNode {
223223 this . writeAllTextAtOnceIndex [ "default" ] [ i + length ] = i ;
224224 }
225225 this . text = this . text . replace ( this . text . substring ( i , endIndex + 1 ) , this . text . substring ( i + 2 , endIndex ) ) ;
226- temp += endIndex + 1 ;
226+ temp += endIndex ;
227227 }
228228 }
229229 }
230+ console . log ( this . text ) ;
231+ console . log ( this . waitIndex ) ;
232+ console . log ( this . writeAllTextAtOnceIndex ) ;
230233 if ( this . node . hasAttribute ( WriteItJS . WRITEIT_REPLACE_NEXT ) ) {
231234 // Loop for all texts in WRITEIT_REPLACE_NEXT and also parse them.
232235 for ( let iterator = 0 ; iterator < this . originalTexts . length ; iterator ++ ) {
@@ -352,44 +355,58 @@ class WriteItNode {
352355 * Adds/Removes a letter from "html";
353356 */
354357 animate ( ) {
358+ // return if animation is paused or stopped.
355359 if ( this . timeout == - 1 ) {
356360 return ;
357361 }
358- if ( this . waitIndex [ this . textsIndex == - 1 ? "default" : this . textsIndex ] [ this . index ] != undefined && this . wait == false && ( ! this . reverse || this . node . hasAttribute ( WriteItJS . WRITEIT_WAIT_IN_REVERSE ) ) ) {
359- let waitingTime = this . waitIndex [ this . textsIndex == - 1 ? "default" : this . textsIndex ] [ this . index ] ;
360- this . wait = true ;
361- this . node . innerHTML = this . text . substring ( 0 , this . index + ( this . reverse ? - 1 : 1 ) ) + this . writeitChar ;
362- this . index += this . reverse ? - 1 : 1 ;
362+ // Wait if we need to wait at these position.
363+ if ( this . waitIndex [ this . textsIndex < 0 ? "default" : this . textsIndex ] [ this . index ] != undefined && ! this . wait ) {
364+ let secsToWait = this . waitIndex [ this . textsIndex < 0 ? "default" : this . textsIndex ] [ this . index ] ;
365+ this . node . innerHTML = this . text . substring ( 0 , this . index ) ; // + this.writeitChar;
366+ if ( this . reverse && this . node . hasAttribute ( WriteItJS . WRITEIT_WRITE_ALL_IN_REVERSE ) ) {
367+ secsToWait = secsToWait * 1000 ;
368+ this . index -- ;
369+ } else if ( ! this . reverse ) {
370+ secsToWait = secsToWait * 1000 ;
371+ this . index ++ ;
372+ } else {
373+ secsToWait = 0 ;
374+ }
363375 if ( this . index <= 0 || this . index >= this . text . length ) {
364376 this . handleIterationEnd ( ) ;
365377 return ;
366378 } else {
367379 // this.node.innerHTML += this.writeitChar;
368380 }
369- this . timeout = this . setTimeout ( ( ) => { this . wait = false ; this . animate ( ) ; } , waitingTime * 1000 ) ;
370- return ;
371- } else if ( this . wait == true ) {
381+ this . timeout = this . setTimeout ( ( ) => { this . wait = false ; this . animate ( ) ; } , secsToWait ) ;
372382 return ;
373383 }
384+ // return if already waiting!
385+ if ( this . wait ) return ;
386+
387+ // Write all text at once.
374388 if ( this . writeAllTextAtOnceIndex [ this . textsIndex < 0 ? "default" : this . textsIndex ] [ this . index ] != undefined ) {
375389 let destinationIndex = this . writeAllTextAtOnceIndex [ this . textsIndex < 0 ? "default" : this . textsIndex ] [ this . index ] ;
376390 if ( this . reverse && destinationIndex < this . index ) {
377391 this . node . innerHTML = this . text . substring ( 0 , destinationIndex ) ;
378- this . index = this . node . innerHTML . length - 1 ;
392+ this . index = destinationIndex - 1 ;
379393 } else if ( ! this . reverse && destinationIndex > this . index ) {
380394 this . node . innerHTML = this . text . substring ( 0 , destinationIndex ) ;
381- this . index = this . node . innerHTML . length ;
395+ this . index = destinationIndex + 1 ;
382396 }
383397 if ( this . index <= 0 || this . index >= this . text . length ) {
384398 this . handleIterationEnd ( ) ;
385399 return ;
386400 } else {
387401 this . node . innerHTML += this . writeitChar ;
388402 }
403+ this . nextIteration ( ) ;
404+ return ;
389405 }
390406
407+
391408 // Browser may have added ending tag so ignore it.
392- let str = this . node . innerHTML . substring ( 0 , this . index ) ;
409+ let str = this . text . substring ( 0 , this . index ) ;
393410
394411 // Add HTML without writeit.
395412 this . node . innerHTML = str ;
0 commit comments