From 120e48e886d0c14e00c2ead245bd6563fcecca2e Mon Sep 17 00:00:00 2001 From: Steve Whiteley Date: Mon, 19 Oct 2015 21:19:57 +0100 Subject: [PATCH] Bug fix for new lines. --- README.md | 36 ++++++++++++++++++------------------ jquery.teletype.js | 17 +++++++++-------- jquery.teletype.min.js | 2 +- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index b2a21a9..8843739 100644 --- a/README.md +++ b/README.md @@ -27,20 +27,21 @@ Installation Options ------- - Option | Default | Description ---------------|-------------------------------------|------------ - text | `['one','two','three']` (array) | List of strings to output. - typeDelay | `100` (integer) | Minimum delay, in ms, between typing characters. - backDelay | `50` (integer) | Minimum delay, in ms, between deleting characters. - blinkSpeed | `1000` (integer) | Interval, in ms, that the cursor will flash. - cursor | "|" (string) | Character used to represent the cursor. - delay | `2000` (int) | Time in ms to pause before deleting the current text. - preserve | `false` (boolean) | Prevent auto delete of the current string and begin outputting the next string. - prefix | `""` (string) | Begin each string with this prefix value. - loop | `0` (int) | Number of times to loop through the output strings, for unlimited use `0`. - humanise | `true` (boolean) | Add a random delay before each character to represent human interaction. - callbackNext | `null` (function) | Callback function called every text item. See `Callback functions` below. - callbackType | `null` (function) | Callback function called every 'letter'. See `Callback functions` below. + Option | Default | Description +----------------------|-------------------------------------|------------ + text | `['one','two','three']` (array) | List of strings to output. + typeDelay | `100` (integer) | Minimum delay, in ms, between typing characters. + backDelay | `50` (integer) | Minimum delay, in ms, between deleting characters. + blinkSpeed | `1000` (integer) | Interval, in ms, that the cursor will flash. + cursor | "|" (string) | Character used to represent the cursor. + delay | `2000` (int) | Time in ms to pause before deleting the current text. + preserve | `false` (boolean) | Prevent auto delete of the current string and begin outputting the next string. + prefix | `""` (string) | Begin each string with this prefix value. + loop | `0` (int) | Number of times to loop through the output strings, for unlimited use `0`. + humanise | `true` (boolean) | Add a random delay before each character to represent human interaction. + callbackNext | `null` (function) | Callback function called every text item. See `Callback functions` below. + callbackType | `null` (function) | Callback function called every 'letter'. See `Callback functions` below. + callbackFinished | `null` (function) | Callback function called once everything is finished. See `Callback functions` below. Methods ------- @@ -150,11 +151,11 @@ callbackType: function( letter, current, teletype ) { Callback function called immediately once the teletype process is entirely finished. -There are no parameters. +The single parameter `teletype` returns the teletype object itself. ``` -callbackFinished: function() { - alert('Nothing left to type!'); +callbackFinished: function( teletype ) { + alert( 'Nothing left to type!' ); } ``` @@ -162,4 +163,3 @@ Minification --- The Minified version of this script was provided by UglifyJS 2 - an online version can be found at . - diff --git a/jquery.teletype.js b/jquery.teletype.js index 7a0bb46..d2dd67b 100644 --- a/jquery.teletype.js +++ b/jquery.teletype.js @@ -1,12 +1,12 @@ /* * Teletype jQuery Plugin -* @version 0.1.5 +* @version 0.1.6 * * @author Steve Whiteley * @see http://teletype.rocks * @see https://github.com/stvwhtly/jquery-teletype-plugin * -* Copyright (c) 2014 Steve Whiteley +* Copyright (c) 2015 Steve Whiteley * Dual licensed under the MIT or GPL Version 2 licenses. * */ @@ -32,7 +32,7 @@ } } current.position = 0; - current.string = settings.text[current.index]; + setCurrentString(); if ( typeof( settings.callbackNext ) == 'function' ) { settings.callbackNext( current, object ); } @@ -91,10 +91,8 @@ window.setTimeout( function() { window.setTimeout( type, delay( settings.typeDelay ) ); }, settings.delay ); - } else { - if ( typeof( settings.callbackFinished ) == 'function' ) { - settings.callbackFinished(); - } + } else if ( typeof( settings.callbackFinished ) == 'function' ) { + settings.callbackFinished( object ); } } if ( typeof( settings.callbackType ) == 'function' ) { @@ -127,11 +125,14 @@ } return time; }; + var setCurrentString = function() { + current.string = settings.text[current.index].replace(/\n/g, "\\n"); + } this.setCursor = function( cursor ) { $('.teletype-cursor', self).text( cursor ); }; return this.each( function() { - current.string = settings.text[current.index]; + setCurrentString(); self.addClass( 'teletype' ).empty(); output = $( '' ).addClass( 'teletype-text' ).appendTo( self ); if ( settings.cursor ) { diff --git a/jquery.teletype.min.js b/jquery.teletype.min.js index 8282865..71e06a5 100644 --- a/jquery.teletype.min.js +++ b/jquery.teletype.min.js @@ -1 +1 @@ -!function(e){e.fn.teletype=function(t){var i=e.extend({},e.fn.teletype.defaults,t),n=this,o=e(this),s=null,l={string:"",index:0,position:0,loop:0},a=function(){return l.index++,l.index>=i.text.length&&(l.index=0,l.loop++,i.loop!==!1&&i.loop==l.loop)?!1:(l.position=0,l.string=i.text[l.index],"function"==typeof i.callbackNext&&i.callbackNext(l,n),!0)},r=function(){i.prefix&&0===l.position&&0===l.loop&&0===l.index&&e("").addClass("teletype-prefix").html(i.prefix).prependTo(o);var t=l.string.split(""),c=t[l.position],d=l.position+1;if("^"==c||"~"==c){var f=l.string.substr(d).search(/[^0-9]/);-1==f&&(f=l.string.length);var y=l.string.substr(d,f);if(e.isNumeric(y)){if(l.string=l.string.replace(c+y,""),"^"==c)window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},y);else{var h=l.position-y;l.string=l.string.substr(0,h-1)+l.string.substr(l.position-1),window.setTimeout(function(){p(Math.max(h,0))},u(i.backDelay))}return}}else if("\\"==c){var m=l.string.substr(d,1);"n"==m&&(l.position++,c="
")}void 0!==c&&s.html(s.html()+c),l.position++,l.position'+i.prefix+"
"),a()?window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},i.delay):"function"==typeof i.callbackFinished&&i.callbackFinished()),"function"==typeof i.callbackType&&i.callbackType(c,l,n)},p=function(e){if(e||(e=0),l.position>e)s.html(s.html().slice(0,-1)),window.setTimeout(function(){p(e)},u(i.backDelay)),l.position--;else{if(0===e&&a()===!1)return;window.setTimeout(r,u(i.typeDelay))}},u=function(e){var t=parseInt(e);return i.humanise&&(t+=Math.floor(200*Math.random())),t};return this.setCursor=function(t){e(".teletype-cursor",o).text(t)},this.each(function(){if(l.string=i.text[l.index],o.addClass("teletype").empty(),s=e("").addClass("teletype-text").appendTo(o),i.cursor){var t=e("").addClass("teletype-cursor").appendTo(o);n.setCursor(i.cursor),setInterval(function(){t.animate({opacity:0}).animate({opacity:1})},i.blinkSpeed)}r()})},e.fn.teletype.defaults={text:["one","two","three"],typeDelay:100,backDelay:50,blinkSpeed:1e3,delay:2e3,cursor:"|",preserve:!1,prefix:"",loop:0,humanise:!0,callbackNext:null,callbackType:null,callbackFinished:null}}(jQuery); \ No newline at end of file +!function(e){e.fn.teletype=function(t){var i=e.extend({},e.fn.teletype.defaults,t),n=this,o=e(this),s=null,l={string:"",index:0,position:0,loop:0},a=function(){return l.index++,l.index>=i.text.length&&(l.index=0,l.loop++,i.loop!==!1&&i.loop==l.loop)?!1:(l.position=0,c(),"function"==typeof i.callbackNext&&i.callbackNext(l,n),!0)},r=function(){i.prefix&&0===l.position&&0===l.loop&&0===l.index&&e("").addClass("teletype-prefix").html(i.prefix).prependTo(o);var t=l.string.split(""),c=t[l.position],d=l.position+1;if("^"==c||"~"==c){var f=l.string.substr(d).search(/[^0-9]/);-1==f&&(f=l.string.length);var y=l.string.substr(d,f);if(e.isNumeric(y)){if(l.string=l.string.replace(c+y,""),"^"==c)window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},y);else{var h=l.position-y;l.string=l.string.substr(0,h-1)+l.string.substr(l.position-1),window.setTimeout(function(){p(Math.max(h,0))},u(i.backDelay))}return}}else if("\\"==c){var m=l.string.substr(d,1);"n"==m&&(l.position++,c="
")}void 0!==c&&s.html(s.html()+c),l.position++,l.position'+i.prefix+"
"),a()?window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},i.delay):"function"==typeof i.callbackFinished&&i.callbackFinished(n)),"function"==typeof i.callbackType&&i.callbackType(c,l,n)},p=function(e){if(e||(e=0),l.position>e)s.html(s.html().slice(0,-1)),window.setTimeout(function(){p(e)},u(i.backDelay)),l.position--;else{if(0===e&&a()===!1)return;window.setTimeout(r,u(i.typeDelay))}},u=function(e){var t=parseInt(e);return i.humanise&&(t+=Math.floor(200*Math.random())),t},c=function(){l.string=i.text[l.index].replace(/\n/g,"\\n")};return this.setCursor=function(t){e(".teletype-cursor",o).text(t)},this.each(function(){if(c(),o.addClass("teletype").empty(),s=e("").addClass("teletype-text").appendTo(o),i.cursor){var t=e("").addClass("teletype-cursor").appendTo(o);n.setCursor(i.cursor),setInterval(function(){t.animate({opacity:0}).animate({opacity:1})},i.blinkSpeed)}r()})},e.fn.teletype.defaults={text:["one","two","three"],typeDelay:100,backDelay:50,blinkSpeed:1e3,delay:2e3,cursor:"|",preserve:!1,prefix:"",loop:0,humanise:!0,callbackNext:null,callbackType:null,callbackFinished:null}}(jQuery); \ No newline at end of file