From b7da08f31d4ac3988db057350b786268c23fe3f1 Mon Sep 17 00:00:00 2001 From: Simon Grim Date: Thu, 9 Apr 2015 22:09:05 +0500 Subject: [PATCH] fix of post splitting, $.fn.caret, licenses for caret, franc and notify modules --- js/interface_common.js | 97 ++++++++++++++++++++++++++------------ js/jQueryPlugins.js | 82 +++++++++++++++++++++++++++++++- licenses/caret.license | 30 ++++++++++++ licenses/franc.license | 25 ++++++++++ licenses/notify.js.license | 9 ++++ 5 files changed, 213 insertions(+), 30 deletions(-) create mode 100644 licenses/caret.license create mode 100644 licenses/franc.license create mode 100644 licenses/notify.js.license diff --git a/js/interface_common.js b/js/interface_common.js index a32f384..8f4267f 100644 --- a/js/interface_common.js +++ b/js/interface_common.js @@ -759,12 +759,12 @@ function replyTextKeypress(e) { if( tweetForm != undefined ) { if ($.Options.getUnicodeConversionOpt() !== "disable") $this.val(convert2Unicodes($this.val(), $this)); - var c = 140 - $this.val().length; + if (usePostSpliting && !$this.parents('.directMessages').length) { var $tas = tweetForm.find("textarea"); splitedPostsCount = $tas.length; - if ($this.hasClass('splited-post')) - $this.css('height', '28px'); + var icurrentta = $tas.index(this); // current textarea $tas index + var caretPos = $this.caret(); var reply_to = $this.attr('data-reply-to'); for (var i = 0; i < $tas.length; i++) { @@ -785,27 +785,39 @@ function replyTextKeypress(e) { if (i < splitedPostsCount - 1) { $tas[i + 1].value = $tas[i].value.substr(ci) + $tas[i + 1].value; $tas[i].value = $tas[i].value.substr(0, ci); + if (caretPos > ci) { + caretPos -= ci; + icurrentta += 1; + var $targetta = $($tas[icurrentta]); + } else if (i === icurrentta) + $($tas[i]).caret(caretPos); } else { var $oldta = $($tas[i]); - if (typeof($.fn.textcomplete) === 'function') + if (typeof($.fn.textcomplete) === 'function') { $oldta.textcomplete('destroy'); + e.stopImmediatePropagation(); // something goes wrong in $.fn.textcomplete if we don't stop this immediately + } var $newta = $($oldta).clone(true); var cp = $oldta.val(); $oldta.val(cp.substr(0, ci)); - $oldta.on("click", function(e) { - e.stopPropagation(); + $oldta.on('focus', function() { this.style.height = '80px'; }); - e.stopImmediatePropagation(); - $oldta.off('keyup'); - $oldta.on('focusout', function() {this.style.height = '28px';}); // FIXME move this to CSS - $oldta[0].style.height = '28px'; // FIXME move this to CSS $oldta.addClass('splited-post'); + $oldta.on('focusout', function() {this.style.height = '28px';}); // FIXME move this to CSS $oldta.after($newta); $newta.val(cp.substr(ci)); + if (caretPos > ci) { + caretPos -= ci; + icurrentta += 1; + var $targetta = $newta; + $oldta[0].style.height = '28px'; // FIXME move this to CSS + } else if (i === icurrentta) + $($tas[i]).caret(caretPos); + $tas = tweetForm.find("textarea"); splitedPostsCount = $tas.length; } @@ -819,28 +831,19 @@ function replyTextKeypress(e) { } $tas = tweetForm.find("textarea"); splitedPostsCount = $tas.length; + caretPos = -1; + if (icurrentta >= i && icurrentta > 0) + icurrentta -= 1; + var $targetta = $($tas[icurrentta]); } } - if (typeof($newta) !== 'undefined' && $newta[0] !== document.activeElement) - $newta.focus(); - - c = 140 - $tas[$tas.length - 1].value.length - (2 * splitedPostsCount.toString().length) - 4; - if (typeof(reply_to) !== 'undefined' && - !checkPostForMentions($tas[$tas.length - 1].value, reply_to, 140 - c - reply_to.length)) - c -= reply_to.length; + if (typeof($targetta) !== 'undefined' && $targetta[0] !== document.activeElement) { + $targetta.focus(); + $targetta.caret(caretPos); + } } - var remainingCount = tweetForm.find(".post-area-remaining"); - - if( c < 0 ) - remainingCount.addClass("warn"); - else - remainingCount.removeClass("warn"); - - if (usePostSpliting && !$this.parents('.directMessages').length) - remainingCount.text(splitedPostsCount.toString() + ". post: " + c.toString()); - else - remainingCount.text(c.toString()); + var c = replyTextCount(this); var tweetAction = tweetForm.find(".post-submit"); if( !tweetAction.length ) tweetAction = tweetForm.find(".dm-submit"); if( c >= 0 && c < 140 && @@ -868,6 +871,41 @@ function replyTextKeypress(e) { } } +function replyTextUpdateRemaining() { + if (this === document.activeElement ) { + var $this = $( this ); + var tweetForm = $this.parents('form'); + if( tweetForm != undefined ) { + var remainingCount = tweetForm.find(".post-area-remaining"); + var c = replyTextCount(this); + + if (usePostSpliting && !$this.parents('.directMessages').length) + remainingCount.text((tweetForm.find("textarea").index(this)+1).toString() +'/' +splitedPostsCount.toString() + ": " + c.toString()); + else + remainingCount.text(c.toString()); + + if( c < 0 ) + remainingCount.addClass("warn"); + else + remainingCount.removeClass("warn"); + } + } +} + +function replyTextCount(ta) { + var c; + + if (usePostSpliting && !$(ta).parents('.directMessages').length) { + c = 140 - ta.value.length - (2 * splitedPostsCount.toString().length) - 4; + if (typeof(reply_to) !== 'undefined' && + !checkPostForMentions(ta.value, reply_to, 140 - c - reply_to.length)) + c -= reply_to.length; + } else + c = 140 - ta.value.length; + + return c; +} + /* * unicode convertion list * k: original string to be replaced @@ -1510,7 +1548,8 @@ function initInterfaceCommon() { $( ".undo-unicode" ).click( undoLastUnicode ); var $replyText = $( ".post-area-new textarea" ); - $replyText.on("keyup", replyTextKeypress ); + $replyText.on('input', replyTextKeypress ); + $replyText.on('input focus', replyTextUpdateRemaining); $( ".open-profile-modal").bind( "click", function(e){ e.stopPropagation(); } ); //$( ".open-hashtag-modal").bind( "click", openHashtagModal ); diff --git a/js/jQueryPlugins.js b/js/jQueryPlugins.js index 205ab12..7601966 100644 --- a/js/jQueryPlugins.js +++ b/js/jQueryPlugins.js @@ -11,4 +11,84 @@ }); return $(this); } -})(jQuery); \ No newline at end of file +})(jQuery); + +(function($) { + $.fn.caret = function(pos) { + var target = this[0]; + var isContentEditable = target.contentEditable === 'true'; + //get + if (arguments.length == 0) { + //HTML5 + if (window.getSelection) { + //contenteditable + if (isContentEditable) { + target.focus(); + var range1 = window.getSelection().getRangeAt(0), + range2 = range1.cloneRange(); + range2.selectNodeContents(target); + range2.setEnd(range1.endContainer, range1.endOffset); + return range2.toString().length; + } + //textarea + return target.selectionStart; + } + //IE<9 + if (document.selection) { + target.focus(); + //contenteditable + if (isContentEditable) { + var range1 = document.selection.createRange(), + range2 = document.body.createTextRange(); + range2.moveToElementText(target); + range2.setEndPoint('EndToEnd', range1); + return range2.text.length; + } + //textarea + var pos = 0, + range = target.createTextRange(), + range2 = document.selection.createRange().duplicate(), + bookmark = range2.getBookmark(); + range.moveToBookmark(bookmark); + while (range.moveStart('character', -1) !== 0) pos++; + return pos; + } + // Addition for jsdom support + if (target.selectionStart) + return target.selectionStart; + //not supported + return 0; + } + //set + if (pos == -1) + pos = this[isContentEditable? 'text' : 'val']().length; + //HTML5 + if (window.getSelection) { + //contenteditable + if (isContentEditable) { + target.focus(); + window.getSelection().collapse(target.firstChild, pos); + } + //textarea + else + target.setSelectionRange(pos, pos); + } + //IE<9 + else if (document.body.createTextRange) { + if (isContentEditable) { + var range = document.body.createTextRange(); + range.moveToElementText(target); + range.moveStart('character', pos); + range.collapse(true); + range.select(); + } else { + var range = target.createTextRange(); + range.move('character', pos); + range.select(); + } + } + if (!isContentEditable) + target.focus(); + return pos; + } +})(jQuery); diff --git a/licenses/caret.license b/licenses/caret.license new file mode 100644 index 0000000..0a66b06 --- /dev/null +++ b/licenses/caret.license @@ -0,0 +1,30 @@ +Copyright (c) 2009, Gideon Sireling + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Gideon Sireling nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/licenses/franc.license b/licenses/franc.license new file mode 100644 index 0000000..1dab815 --- /dev/null +++ b/licenses/franc.license @@ -0,0 +1,25 @@ +(The MIT License) + +Copyright (c) 2014-2015 Titus Wormer +Copyright (c) 2008 Kent S Johnson +Copyright (c) 2006 Jacob R Rideout +Copyright (c) 2004 Maciej Ceglowski + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/licenses/notify.js.license b/licenses/notify.js.license new file mode 100644 index 0000000..b5cf4a5 --- /dev/null +++ b/licenses/notify.js.license @@ -0,0 +1,9 @@ +Copyright (c) 2014 Alex Gibson + +http://alxgbsn.co.uk/ + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction except as noted below, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sublicense, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE