From 146f55ac68c2b3e6f0c01101a826a6ba94597947 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 30 May 2015 11:12:10 +0100 Subject: [PATCH] Updated the commands syntax. Remove and pause commands no longer require a space character afterwards. --- README.md | 6 +++--- jquery.teletype.js | 16 ++++++++++------ jquery.teletype.min.js | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2829b96..1400446 100644 --- a/README.md +++ b/README.md @@ -56,18 +56,18 @@ Deleting characters `~` It is possible to delete a defined number of characters then proceed with the rest of the text output. -Use `~x ` within the text string, where x is an integer value defining the characters to backspace, followed by a space. +Use `~x` within the text string, where x is an integer value defining the characters to backspace. Example, type "auti", delete 1 character and continue to type "o", resulting in the word "auto": ``` -auti~1 o^` +auti~1o^` ``` Pause / Delay `^` --- -Delay typing the next character using `^x ` where x in an integer value of milliseconds to pause. +Delay typing the next character using `^x` where x in an integer value of milliseconds to pause. To pause for 2 seconds after typing the word "pause" before continuing to type: diff --git a/jquery.teletype.js b/jquery.teletype.js index eaba27e..445a408 100644 --- a/jquery.teletype.js +++ b/jquery.teletype.js @@ -1,6 +1,6 @@ /* * Teletype jQuery Plugin -* @version 0.1.4 +* @version 0.1.5 * * @author Steve Whiteley * @see http://teletype.rocks @@ -45,12 +45,16 @@ } } var letters = current.string.split( '' ), - letter = letters[current.position]; + letter = letters[current.position], + start = current.position + 1; if ( letter == '^' || letter == '~' ) { - var end = current.string.substr( current.position + 1 ).indexOf( ' ' ); - var value = current.string.substr( current.position + 1, end ); + var end = current.string.substr( start ).search( /[^0-9]/ ); + if ( end == -1 ) { + end = current.string.length; + } + var value = current.string.substr( start, end ); if ( $.isNumeric( value ) ) { - current.string = current.string.replace( letter + value + ' ', '' ); + current.string = current.string.replace( letter + value, '' ); if ( letter == '^' ) { window.setTimeout( function() { window.setTimeout( type, delay( settings.typeDelay ) ); @@ -65,7 +69,7 @@ return; } } else if ( letter == '\\' ) { - var nextChar = current.string.substr( current.position + 1, 1 ); + var nextChar = current.string.substr( start, 1 ); if ( nextChar == 'n' ) { current.position++; letter = '
'; diff --git a/jquery.teletype.min.js b/jquery.teletype.min.js index 67da31e..40e2812 100644 --- a/jquery.teletype.min.js +++ b/jquery.teletype.min.js @@ -1 +1 @@ -!function(t){t.fn.teletype=function(e){var i=t.extend({},t.fn.teletype.defaults,e),n=this,o=t(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&&t("").addClass("teletype-prefix").html(i.prefix).prependTo(o);var e=l.string.split(""),c=e[l.position];if("^"==c||"~"==c){var d=l.string.substr(l.position+1).indexOf(" "),f=l.string.substr(l.position+1,d);if(t.isNumeric(f)){if(l.string=l.string.replace(c+f+" ",""),"^"==c)window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},f);else{var y=l.position-f;l.string=l.string.substr(0,y-1)+l.string.substr(l.position-1),window.setTimeout(function(){p(Math.max(y,0))},u(i.backDelay))}return}}else if("\\"==c){var m=l.string.substr(l.position+1,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.callbackType&&i.callbackType(c,l,n)},p=function(t){if(t||(t=0),l.position>t)s.html(s.html().slice(0,-1)),window.setTimeout(function(){p(t)},u(i.backDelay)),l.position--;else{if(0===t&&a()===!1)return;window.setTimeout(r,u(i.typeDelay))}},u=function(t){var e=parseInt(t);return i.humanise&&(e+=Math.floor(200*Math.random())),e};return this.setCursor=function(e){t(".teletype-cursor",o).text(e)},this.each(function(){if(l.string=i.text[l.index],o.addClass("teletype").empty(),s=t("").addClass("teletype-text").appendTo(o),i.cursor){var e=t("").addClass("teletype-cursor").appendTo(o);n.setCursor(i.cursor),setInterval(function(){e.animate({opacity:0}).animate({opacity:1})},i.blinkSpeed)}r()})},t.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}}(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,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 m=l.position-y;l.string=l.string.substr(0,m-1)+l.string.substr(l.position-1),window.setTimeout(function(){p(Math.max(m,0))},u(i.backDelay))}return}}else if("\\"==c){var x=l.string.substr(d,1);"n"==x&&(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.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}}(jQuery); \ No newline at end of file