From 0ace77081df7e23441a2534126045c341b4b640b Mon Sep 17 00:00:00 2001 From: Stephan Eckardt Date: Thu, 24 Mar 2011 16:14:10 +0100 Subject: [PATCH 1/2] Backported changes to encloseSelection from latest DotClear release, less bugs in IE8 --- public/javascripts/jstoolbar/jstoolbar.js | 181 ++++++++++------------ 1 file changed, 79 insertions(+), 102 deletions(-) diff --git a/public/javascripts/jstoolbar/jstoolbar.js b/public/javascripts/jstoolbar/jstoolbar.js index 3c3a3433..bfef733e 100644 --- a/public/javascripts/jstoolbar/jstoolbar.js +++ b/public/javascripts/jstoolbar/jstoolbar.js @@ -242,108 +242,85 @@ jsToolBar.prototype = { this.encloseSelection(stag,etag); }, - - encloseLineSelection: function(prefix, suffix, fn) { - this.textarea.focus(); - - prefix = prefix || ''; - suffix = suffix || ''; - - var start, end, sel, scrollPos, subst, res; - - if (typeof(document["selection"]) != "undefined") { - sel = document.selection.createRange().text; - } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { - start = this.textarea.selectionStart; - end = this.textarea.selectionEnd; - scrollPos = this.textarea.scrollTop; - // go to the start of the line - start = this.textarea.value.substring(0, start).replace(/[^\r\n]*$/g,'').length; - // go to the end of the line - end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length; - sel = this.textarea.value.substring(start, end); - } - - if (sel.match(/ $/)) { // exclude ending space char, if any - sel = sel.substring(0, sel.length - 1); - suffix = suffix + " "; - } - - if (typeof(fn) == 'function') { - res = (sel) ? fn.call(this,sel) : fn(''); - } else { - res = (sel) ? sel : ''; - } - - subst = prefix + res + suffix; - - if (typeof(document["selection"]) != "undefined") { - document.selection.createRange().text = subst; - var range = this.textarea.createTextRange(); - range.collapse(false); - range.move('character', -suffix.length); - range.select(); - } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { - this.textarea.value = this.textarea.value.substring(0, start) + subst + - this.textarea.value.substring(end); - if (sel) { - this.textarea.setSelectionRange(start + subst.length, start + subst.length); - } else { - this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); - } - this.textarea.scrollTop = scrollPos; - } - }, - - encloseSelection: function(prefix, suffix, fn) { - this.textarea.focus(); - - prefix = prefix || ''; - suffix = suffix || ''; - - var start, end, sel, scrollPos, subst, res; - - if (typeof(document["selection"]) != "undefined") { - sel = document.selection.createRange().text; - } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { - start = this.textarea.selectionStart; - end = this.textarea.selectionEnd; - scrollPos = this.textarea.scrollTop; - sel = this.textarea.value.substring(start, end); - } - - if (sel.match(/ $/)) { // exclude ending space char, if any - sel = sel.substring(0, sel.length - 1); - suffix = suffix + " "; - } - - if (typeof(fn) == 'function') { - res = (sel) ? fn.call(this,sel) : fn(''); - } else { - res = (sel) ? sel : ''; - } - - subst = prefix + res + suffix; - - if (typeof(document["selection"]) != "undefined") { - document.selection.createRange().text = subst; - var range = this.textarea.createTextRange(); - range.collapse(false); - range.move('character', -suffix.length); - range.select(); -// this.textarea.caretPos -= suffix.length; - } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { - this.textarea.value = this.textarea.value.substring(0, start) + subst + - this.textarea.value.substring(end); - if (sel) { - this.textarea.setSelectionRange(start + subst.length, start + subst.length); - } else { - this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); - } - this.textarea.scrollTop = scrollPos; - } - }, - + + encloseLineSelection: function (prefix, suffix, fn) { + this.textarea.focus(); + prefix = prefix || ''; + suffix = suffix || ''; + var start, end, sel, scrollPos, subst, res; + if (typeof(document["selection"]) != "undefined") { + sel = document.selection.createRange().text; + } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { + start = this.textarea.selectionStart; + end = this.textarea.selectionEnd; + scrollPos = this.textarea.scrollTop; + // go to the start of the line + start = this.textarea.value.substring(0, start).replace(/[^\r\n]*$/g,'').length; + // go to the end of the line + end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length; + sel = this.textarea.value.substring(start, end); + } + if (sel.match(/ $/)) { + sel = sel.substring(0, sel.length - 1); + suffix = suffix + " "; + } + if (typeof(fn) == 'function') { + res = (sel) ? fn.call(this, sel) : fn(''); + } else { + res = (sel) ? sel : ''; + } + subst = prefix + res + suffix; + if (typeof(document["selection"]) != "undefined") { + var range = document.selection.createRange().text = subst; + this.textarea.caretPos -= suffix.length; + } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { + this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end); + if (sel) { + this.textarea.setSelectionRange(start + subst.length, start + subst.length); + } else { + this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); + } + this.textarea.scrollTop = scrollPos; + } + }, + + encloseSelection: function (prefix, suffix, fn) { + this.textarea.focus(); + prefix = prefix || ''; + suffix = suffix || ''; + var start, end, sel, scrollPos, subst, res; + if (typeof(document["selection"]) != "undefined") { + sel = document.selection.createRange().text; + } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { + start = this.textarea.selectionStart; + end = this.textarea.selectionEnd; + scrollPos = this.textarea.scrollTop; + sel = this.textarea.value.substring(start, end); + } + if (sel.match(/ $/)) { + sel = sel.substring(0, sel.length - 1); + suffix = suffix + " "; + } + if (typeof(fn) == 'function') { + res = (sel) ? fn.call(this, sel) : fn(''); + } else { + res = (sel) ? sel : ''; + } + subst = prefix + res + suffix; + if (typeof(document["selection"]) != "undefined") { + var range = document.selection.createRange().text = subst; + this.textarea.caretPos -= suffix.length; + } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { + this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end); + if (sel) { + this.textarea.setSelectionRange(start + subst.length, start + subst.length); + } else { + this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); + } + this.textarea.scrollTop = scrollPos; + } + }, + stripBaseURL: function(url) { if (this.base_url != '') { var pos = url.indexOf(this.base_url); From 9e04bf83c05428b83c5b842cef7f3249eabd979e Mon Sep 17 00:00:00 2001 From: Stephan Eckardt Date: Thu, 24 Mar 2011 19:35:49 +0100 Subject: [PATCH 2/2] Fix jstoolbar to make buttons for h1, h2,
 etc. work
 in IE8

---
 public/javascripts/jstoolbar/jstoolbar.js | 26 +++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/public/javascripts/jstoolbar/jstoolbar.js b/public/javascripts/jstoolbar/jstoolbar.js
index bfef733e..dbc973fe 100644
--- a/public/javascripts/jstoolbar/jstoolbar.js
+++ b/public/javascripts/jstoolbar/jstoolbar.js
@@ -249,7 +249,29 @@ jsToolBar.prototype = {
       suffix = suffix || '';
       var start, end, sel, scrollPos, subst, res;
       if (typeof(document["selection"]) != "undefined") {
-          sel = document.selection.createRange().text;
+        // just makes it work in IE8 somehow
+        var range = document.selection.createRange();
+        var bookmark = range.getBookmark();
+        var origParent = range.parentElement();
+        // we move the starting point of the selection to the last newline
+        try {
+          while (range.text[0] != "\n" && range.text[0] != "\r") {
+            bookmark = range.getBookmark();
+            range.moveStart("character", -1);
+            if (origParent != range.parentElement()) {
+              throw "Outside of Textarea";
+            }
+          }
+          range.moveStart("character", 1);
+        } catch(err) {
+          if (err == "Outside of Textarea")
+            range.moveToBookmark(bookmark);
+          else
+            throw err;
+        }
+        if (range.text.match(/ $/))
+          range.moveEnd("character", -1);
+        sel = range.text;
       } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
           start = this.textarea.selectionStart;
           end = this.textarea.selectionEnd;
@@ -271,7 +293,7 @@ jsToolBar.prototype = {
       }
       subst = prefix + res + suffix;
       if (typeof(document["selection"]) != "undefined") {
-          var range = document.selection.createRange().text = subst;
+          range.text = subst;
           this.textarea.caretPos -= suffix.length;
       } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
           this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end);