Revision: 8942
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=8942&view=rev
Author:   dnaber
Date:     2013-01-10 20:25:55 +0000 (Thu, 10 Jan 2013)
Log Message:
-----------
online check: fix: cursor position was lost on check

Modified Paths:
--------------
    trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/editor_plugin.js
    trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/atd.core.js
    
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/editor_plugin.js

Modified: 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/editor_plugin.js
===================================================================
--- 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/editor_plugin.js    
    2013-01-10 18:57:41 UTC (rev 8941)
+++ 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/editor_plugin.js    
    2013-01-10 20:25:55 UTC (rev 8942)
@@ -23,6 +23,13 @@
 // fixed: text with markup (even bold) messes up everything
 //
 
+String.prototype.insert = function (index, string) {
+  if (index > 0)
+    return this.substring(0, index) + string + this.substring(index, 
this.length);
+  else
+    return string + this;
+};
+
 function AtDCore() {
        /* these are the categories of errors AtD should ignore */
        this.ignore_types = [];
@@ -171,7 +178,10 @@
  * code to manage highlighting of errors
  */
 AtDCore.prototype.markMyWords = function(container_nodes) {
-    var newText = this.getText();
+    var ed = tinyMCE.activeEditor;
+    var textWithCursor = this.getPlainTextWithCursorMarker();
+    var cursorPos = textWithCursor.indexOf("\ufeff");
+    var newText = this.getPlainText();
     
     var previousSpanStart = -1;
     // iterate backwards as we change the text and thus modify positions:
@@ -210,12 +220,45 @@
         }
     }
     
+    // now insert a span into the location of the original cursor position,
+    // only considering real text content of course:
+    newText = this._insertCursorSpan(newText, cursorPos);
+    
     newText = newText.replace(/^\n/, "");
     newText = newText.replace(/^\n/, "");
     newText = newText.replace(/\n/g, "<br/>");
-    tinyMCE.activeEditor.setContent(newText);
+    ed.setContent(newText);
+    // now place the cursor where it was:
+    ed.selection.select(ed.dom.select('span#caret_pos_holder')[0]);
+    ed.dom.remove(ed.dom.select('span#caret_pos_holder')[0]);
 };
 
+AtDCore.prototype._insertCursorSpan = function(text, cursorPos) {
+    var newTextParts = text.split(/([<>])/);
+    var inTag = 0;
+    var textPos = 0;
+    var stringPos = 0;
+    for (var i = 0; i < newTextParts.length; i++) {
+        if (newTextParts[i] == "<" || newTextParts[i] == ">") {
+            if (newTextParts[i] == "<") {
+                inTag++;
+            } else {
+                inTag--;
+            }
+        } else if (inTag == 0) {
+            var partLength = newTextParts[i].length;
+            if (cursorPos >= textPos && cursorPos <= textPos + partLength) {
+                var relativePos = cursorPos - textPos;
+                text = text.insert(stringPos + relativePos, "<span 
id='caret_pos_holder'></span>");
+                break;
+            }
+            textPos += partLength;
+        }
+        stringPos += newTextParts[i].length;
+    }
+    return text;
+}
+
 AtDCore.prototype.getSurrogatePart = function(surrogateString, part) {
     var parts = surrogateString.split(this.surrogateAttributeDelimiter);
     if (part == 'id') {
@@ -230,13 +273,25 @@
     return null;
 };
 
-AtDCore.prototype.getText = function() {
-    return tinyMCE.activeEditor.getContent({ format: 'raw' })
+AtDCore.prototype.getPlainTextWithCursorMarker = function() {
+    return this._getPlainText(false);
+};
+
+AtDCore.prototype.getPlainText = function() {
+    return this._getPlainText(true);
+};
+
+AtDCore.prototype._getPlainText = function(removeCursor) {
+    var plainText = tinyMCE.activeEditor.getContent({ format: 'raw' })
             .replace(/<p>/g, "\n\n")
             .replace(/<br>/g, "\n")
             .replace(/<br\s*\/>/g, "\n")
             .replace(/<.*?>/g, "")
-            .replace(/\ufeff/g, "");  // feff = 65279 = cursor code
+            .replace(/&nbsp;/g, " ");  // for Chrome - no idea where this 
comes from
+    if (removeCursor) {
+        plainText = plainText.replace(/\ufeff/g, "");  // feff = 65279 = 
cursor code
+    }
+    return plainText;
 };
 
 AtDCore.prototype.removeWords = function(node, w) {
@@ -456,7 +511,7 @@
             plugin._removeWords();
 
             /* send request to our service */
-            var textContent = plugin.editor.core.getText();
+            var textContent = plugin.editor.core.getPlainText();
             plugin.sendRequest('checkDocument', textContent, languageCode, 
function(data, request, someObject)
             {
                /* turn off the spinning thingie */

Modified: 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/atd.core.js
===================================================================
--- trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/atd.core.js 
2013-01-10 18:57:41 UTC (rev 8941)
+++ trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/atd.core.js 
2013-01-10 20:25:55 UTC (rev 8942)
@@ -14,15 +14,22 @@
 
 //
 // TODO:
-// 1. cursor position gets lost on check
-// 2. "ignore" and "ignore this kind of error" only works until the next check
-// 3. Ctrl-Z (undo) make the error markers go away
+// 1. "ignore" and "ignore this kind of error" only works until the next check
+// 2. Ctrl-Z (undo) make the error markers go away
 //
+// fixed: cursor position gets lost on check
 // fixed: "ignore all" doesn't work
 // fixed: current cursor position is ignored when incorrect (it has its own 
node)
 // fixed: text with markup (even bold) messes up everything
 //
 
+String.prototype.insert = function (index, string) {
+  if (index > 0)
+    return this.substring(0, index) + string + this.substring(index, 
this.length);
+  else
+    return string + this;
+};
+
 function AtDCore() {
        /* these are the categories of errors AtD should ignore */
        this.ignore_types = [];
@@ -171,7 +178,10 @@
  * code to manage highlighting of errors
  */
 AtDCore.prototype.markMyWords = function(container_nodes) {
-    var newText = this.getText();
+    var ed = tinyMCE.activeEditor;
+    var textWithCursor = this.getPlainTextWithCursorMarker();
+    var cursorPos = textWithCursor.indexOf("\ufeff");
+    var newText = this.getPlainText();
     
     var previousSpanStart = -1;
     // iterate backwards as we change the text and thus modify positions:
@@ -210,12 +220,45 @@
         }
     }
     
+    // now insert a span into the location of the original cursor position,
+    // only considering real text content of course:
+    newText = this._insertCursorSpan(newText, cursorPos);
+    
     newText = newText.replace(/^\n/, "");
     newText = newText.replace(/^\n/, "");
     newText = newText.replace(/\n/g, "<br/>");
-    tinyMCE.activeEditor.setContent(newText);
+    ed.setContent(newText);
+    // now place the cursor where it was:
+    ed.selection.select(ed.dom.select('span#caret_pos_holder')[0]);
+    ed.dom.remove(ed.dom.select('span#caret_pos_holder')[0]);
 };
 
+AtDCore.prototype._insertCursorSpan = function(text, cursorPos) {
+    var newTextParts = text.split(/([<>])/);
+    var inTag = 0;
+    var textPos = 0;
+    var stringPos = 0;
+    for (var i = 0; i < newTextParts.length; i++) {
+        if (newTextParts[i] == "<" || newTextParts[i] == ">") {
+            if (newTextParts[i] == "<") {
+                inTag++;
+            } else {
+                inTag--;
+            }
+        } else if (inTag == 0) {
+            var partLength = newTextParts[i].length;
+            if (cursorPos >= textPos && cursorPos <= textPos + partLength) {
+                var relativePos = cursorPos - textPos;
+                text = text.insert(stringPos + relativePos, "<span 
id='caret_pos_holder'></span>");
+                break;
+            }
+            textPos += partLength;
+        }
+        stringPos += newTextParts[i].length;
+    }
+    return text;
+}
+
 AtDCore.prototype.getSurrogatePart = function(surrogateString, part) {
     var parts = surrogateString.split(this.surrogateAttributeDelimiter);
     if (part == 'id') {
@@ -230,14 +273,25 @@
     return null;
 };
 
-AtDCore.prototype.getText = function() {
-    return tinyMCE.activeEditor.getContent({ format: 'raw' })
+AtDCore.prototype.getPlainTextWithCursorMarker = function() {
+    return this._getPlainText(false);
+};
+
+AtDCore.prototype.getPlainText = function() {
+    return this._getPlainText(true);
+};
+
+AtDCore.prototype._getPlainText = function(removeCursor) {
+    var plainText = tinyMCE.activeEditor.getContent({ format: 'raw' })
             .replace(/<p>/g, "\n\n")
             .replace(/<br>/g, "\n")
             .replace(/<br\s*\/>/g, "\n")
             .replace(/<.*?>/g, "")
-            .replace(/&nbsp;/g, " ")  // for Chrome - no idea where this comes 
from
-            .replace(/\ufeff/g, "");  // feff = 65279 = cursor code
+            .replace(/&nbsp;/g, " ");  // for Chrome - no idea where this 
comes from
+    if (removeCursor) {
+        plainText = plainText.replace(/\ufeff/g, "");  // feff = 65279 = 
cursor code
+    }
+    return plainText;
 };
 
 AtDCore.prototype.removeWords = function(node, w) {

Modified: 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/editor_plugin.js
===================================================================
--- 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/editor_plugin.js
    2013-01-10 18:57:41 UTC (rev 8941)
+++ 
trunk/website/www/online-check/tiny_mce/plugins/atd-tinymce/src/editor_plugin.js
    2013-01-10 20:25:55 UTC (rev 8942)
@@ -135,7 +135,7 @@
             plugin._removeWords();
 
             /* send request to our service */
-            var textContent = plugin.editor.core.getText();
+            var textContent = plugin.editor.core.getPlainText();
             plugin.sendRequest('checkDocument', textContent, languageCode, 
function(data, request, someObject)
             {
                /* turn off the spinning thingie */

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
Languagetool-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-commits

Reply via email to