http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89847

Revision: 89847
Author:   brion
Date:     2011-06-10 20:04:17 +0000 (Fri, 10 Jun 2011)
Log Message:
-----------
CodeEditor: now integrates with WikiEditor's search-replace

Modified Paths:
--------------
    trunk/extensions/CodeEditor/modules/jquery.codeEditor.js

Modified: trunk/extensions/CodeEditor/modules/jquery.codeEditor.js
===================================================================
--- trunk/extensions/CodeEditor/modules/jquery.codeEditor.js    2011-06-10 
19:58:08 UTC (rev 89846)
+++ trunk/extensions/CodeEditor/modules/jquery.codeEditor.js    2011-06-10 
20:04:17 UTC (rev 89847)
@@ -114,7 +114,7 @@
                                }
                        ];
                        box.closest('form').submit(function(event) {
-                               
box.val(context.codeEditor.getSession().getValue());
+                               box.val(context.fn.getContents());
                        });
                        context.codeEditor.getSession().setMode(new 
(require("ace/mode/" + lang).Mode));
 
@@ -141,6 +141,11 @@
                }
        },
 
+       /* Needed for search/replace */
+       'getContents': function() {
+               return context.codeEditor.getSession().getValue();
+       },
+
        /*
         * Compatibility with the $.textSelection jQuery plug-in. When the 
iframe is in use, these functions provide
         * equivilant functionality to the otherwise textarea-based 
functionality.
@@ -183,6 +188,7 @@
                        range.setEnd( range.start.row, range.start.column + 
selText.length );
                        sel.setSelectionRange(range);
                }
+               return context.$textarea;
        },
        /**
         * Gets the position (in resolution of bytes not nessecarily 
characters) in a textarea
@@ -200,6 +206,31 @@
         * @param endContainer Element in iframe to end selection in. If not 
set, end is a character offset
         */
        'setSelection': function( options ) {
+               // Ace stores positions for ranges as row/column pairs.
+               // To convert from character offsets, we'll need to iterate 
through the document
+               var doc = context.codeEditor.getSession().getDocument();
+               var lines = doc.getAllLines();
+
+               var offsetToPos = function( offset ) {
+                       var row = 0, col = 0;
+                       var pos = 0;
+                       while ( row < lines.length && pos + lines[row].length < 
offset) {
+                               pos += lines[row].length;
+                               pos++; // for the newline
+                               row++;
+                       }
+                       col = offset - pos;
+                       return {row: row, column: col};
+               }
+               var start = offsetToPos( options.start ),
+                       end = offsetToPos( options.end );
+
+               var sel = context.codeEditor.getSelection();
+               var range = sel.getRange();
+               range.setStart( start.row, start.column );
+               range.setEnd( end.row, end.column );
+               sel.setSelectionRange( range );
+               return context.$textarea;
        },
        /**
         * Scroll a textarea to the current cursor position. You can set the 
cursor position with setSelection()
@@ -207,6 +238,7 @@
         */
        'scrollToCaretPosition': function( options ) {
                //context.fn.scrollToTop( context.fn.getElementAtCursor(), true 
);
+               return context.$textarea;
        },
        /**
         * Scroll an element to the top of the iframe


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

Reply via email to