From cfaf754a3a4e8907605b521e25169801cbb2ee07 Mon Sep 17 00:00:00 2001 From: Ilan Bar-Magen Date: Wed, 28 Aug 2013 13:39:40 +0300 Subject: [PATCH] substructing a diff in caretPos to support connected languages --- textarea-helper.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/textarea-helper.js b/textarea-helper.js index 6393325..571d541 100644 --- a/textarea-helper.js +++ b/textarea-helper.js @@ -2,6 +2,7 @@ 'use strict'; var caretClass = 'textarea-helper-caret' + , endClass = 'textarea-helper-end' , dataKey = 'textarea-helper' // Styles that could influence size of the mirrored element. @@ -32,6 +33,7 @@ , 'top' : 0 , 'left' : -9999 }).insertAfter(this.$text); + this.$mirrorfull = this.$mirror.clone().insertAfter(this.$mirror); }; (function () { @@ -43,19 +45,26 @@ styles[style] = this.$text.css(style); } this.$mirror.css(styles).empty(); - + this.$mirrorfull.css(styles).empty(); + // Update content and insert caret. var caretPos = this.getOriginalCaretPos() , str = this.$text.val() , pre = document.createTextNode(str.substring(0, caretPos)) , post = document.createTextNode(str.substring(caretPos)) - , $car = $('').addClass(caretClass).css('position', 'absolute').html(' '); - this.$mirror.append(pre, $car, post) + , $car = $('').addClass(caretClass).css('position', 'absolute').html('\u0338') + , $end = $('').addClass(endClass).css('position', 'absolute').html('\u0338') + , full = document.createTextNode(str); + + this.$mirror.append(pre, $car, post, $end) .scrollTop(this.$text.scrollTop()); + this.$mirrorfull.append(full, $end.clone()) + .scrollTop(this.$text.scrollTop()); }; this.destroy = function () { this.$mirror.remove(); + this.$mirrorfull.remove(); this.$text.removeData(dataKey); return null; }; @@ -64,8 +73,9 @@ this.update(); var $caret = this.$mirror.find('.' + caretClass) , pos = $caret.position(); + var diff = this.$mirrorfull.find('.' + endClass).position().left - this.$mirror.find('.' + endClass).position().left; if (this.$text.css('direction') === 'rtl') { - pos.right = this.$mirror.innerWidth() - pos.left - $caret.width(); + pos.right = this.$mirror.innerWidth() - pos.left - $caret.width() - diff; pos.left = 'auto'; } @@ -75,6 +85,7 @@ this.height = function () { this.update(); this.$mirror.css('height', ''); + this.$mirrorfull.css('height', ''); return this.$mirror.height(); };