Ankita-ks has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/222846

Change subject: Locating errors in the editing area
......................................................................

Locating errors in the editing area

Change-Id: Iddd78852d0fb5b79b468f2d2f55c7ff6eba33b00
---
M modules/ext.LanguageToolAction.js
1 file changed, 58 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/LanguageTool 
refs/changes/46/222846/1

diff --git a/modules/ext.LanguageToolAction.js 
b/modules/ext.LanguageToolAction.js
index 486a5ba..f47c20c 100644
--- a/modules/ext.LanguageToolAction.js
+++ b/modules/ext.LanguageToolAction.js
@@ -17,11 +17,18 @@
 mw.languageToolAction = function VeUiLanguageToolAction( surface ) {
        // Parent constructor
        ve.ui.Action.call( this, surface );
+       this.surfaceModel = this.surface.getModel();
+       //console.log( this.surfaceModel );
        this.surrogateAttribute = "onkeypress";
     this.surrogateAttributeDelimiter = "---#---";
     this.ignoredRulesIds = [];
     this.ignoredSpellingErrors = [];
-    this.suggestions = "";
+    this.$findResults = $( '<div>' ).addClass( 'hiddenSpellError' );
+       this.initialFragment = null;
+       this.fragments = [];
+       this.surface.$selections.append( this.$findResults );
+
+    //this.suggestions = "";
 };
 
 /* Inheritance */
@@ -82,58 +89,79 @@
                        var node = textNodes[nodeI];
                        var nodeRange = node.getRange();
                        var nodeText = 
model.getLinearFragment(nodeRange).getText();
+                       console.log( nodeText );
                        text = text + "\n" + nodeText;
                }
+               console.log( text );
                var lang = mw.config.get( 'wgPageContentLanguage' );
-               var params = "language=" + lang + "&text=" + text;
+               var self = this;
                $.ajax(
                {
                        type: 'POST',
-                       //dataType: 'xml',
+                       dataType: 'xml',
                        url: 'http://127.0.0.1:8081/', 
                        data: {language: lang,  text: text}
                }
                )
                .done( 
-                       this.openDialog
+                       function( responseXML )
+                       {
+                               //console.log( responseXML );
+                               self.openDialog.apply( self, [ responseXML, 
text ] );
+                       }
                        //this.processXML
                 );
                return;
        }
-mw.languageToolAction.prototype.openDialog = function ( responseXML ) {
+mw.languageToolAction.prototype.openDialog = function ( responseXML, text ) {
+       
        //console.log( this.constructor.name );
-       this.processXML = mw.languageToolAction.prototype.processXML.bind( this 
);
+       //this.processXML = mw.languageToolAction.prototype.processXML.bind( 
this );
+       //console.log( responseXML );
        var results = this.processXML( responseXML );
+       var range, fragment;
+       var surfaceModel = this.surface.getModel();
+       this.suggestions = results;
+       //console.log( results );
        var languageCode = mw.config.get( 'wgPageContentLanguage' );
-       var messageDialog = new OO.ui.MessageDialog();
-                       // Create and append a window manager.
-                       var windowManager = new OO.ui.WindowManager();
-                       $( 'body' ).append( windowManager.$element );
-                       windowManager.addWindows( [ messageDialog ] );
-                       
-                       var errors = responseXML.getElementsByTagName( "error" 
);
-                       //console.log(errors);
-                       var i;
-                       var response = "";
-                       for ( i = 0; i < errors.length; i++ ) {
-                               response = response + "ERROR " + i + " :\n";
-                               response = response + "error : " + 
errors[i].getAttribute( 'msg' ) + "\n";
-                               response = response + "context : " + 
errors[i].getAttribute( 'context' ) + "\n";
-                               messageDialog.setData( 'error', 
errors[i].getAttribute( 'msg' ) );
-                               messageDialog.setData( 'context', 
errors[i].getAttribute( 'context' ));
-                       }
-                       //console.log(response);
-                       // Example: Creating and opening a message dialog 
window.
-                       // Open the window.
-                       windowManager.openWindow( messageDialog, {
-                       title: 'LanguageTool Response',
-                       message: response
-                       } );    
+       var previousSpanStart = -1;
+       // iterate backwards as we change the text and thus modify positions:
+    for (var suggestionIndex = this.suggestions.length-1; suggestionIndex >= 
0; suggestionIndex--) {
+       var suggestion = this.suggestions[suggestionIndex];
+       if (!suggestion.used) {
+               var spanStart = suggestion.offset;
+            var spanEnd = spanStart + suggestion.errorlength;
+            if (previousSpanStart != -1 && spanEnd > previousSpanStart) {
+                // overlapping errors - these are not supported by our 
underline approach,
+                // as we would need overlapping <span>s for that, so skip the 
error:
+                continue;
+            }
+            previousSpanStart = spanStart;
+            //console.log( text.substring( spanStart, spanEnd ) );
+            range = new ve.Range( spanStart - 1, spanEnd );
+            fragment = surfaceModel.getLinearFragment( range, true );
+            //console.log( fragment );
+            console.log( fragment.getText() );
+            //fragment.annotate( 'set', 'textStyle/bold' );
+            console.log( spanStart + " , " + spanEnd);
+            var ruleId = suggestion.ruleid;
+            var cssName;
+            if (ruleId.indexOf("SPELLER_RULE") >= 0 || 
ruleId.indexOf("MORFOLOGIK_RULE") == 0 || ruleId == "HUNSPELL_NO_SUGGEST_RULE" 
|| ruleId == "HUNSPELL_RULE") {
+                cssName = "hiddenSpellError";
+            }
+            else {
+                cssName = "hiddenGrammarError";
+            }
+            suggestion.used = true;
+       }
+    }
+
 }
 
 mw.languageToolAction.prototype.processXML = function ( responseXML ) {
        //console.log('entered');
        this.suggestions = [];
+       //console.log( responseXML );
        this._wordwrap = mw.languageToolAction.prototype._wordwrap.bind( this );
     var errors = responseXML.getElementsByTagName('error');
     for (var i = 0; i < errors.length; i++) {

-- 
To view, visit https://gerrit.wikimedia.org/r/222846
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iddd78852d0fb5b79b468f2d2f55c7ff6eba33b00
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/LanguageTool
Gerrit-Branch: master
Gerrit-Owner: Ankita-ks <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to