r89847 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89846‎ | r89847 | r89848 >
Date:20:04, 10 June 2011
Author:brion
Status:deferred
Tags:
Comment:
CodeEditor: now integrates with WikiEditor's search-replace
Modified paths:
  • /trunk/extensions/CodeEditor/modules/jquery.codeEditor.js (modified) (history)

Diff [purge]

Index: trunk/extensions/CodeEditor/modules/jquery.codeEditor.js
@@ -114,7 +114,7 @@
115115 }
116116 ];
117117 box.closest('form').submit(function(event) {
118 - box.val(context.codeEditor.getSession().getValue());
 118+ box.val(context.fn.getContents());
119119 });
120120 context.codeEditor.getSession().setMode(new (require("ace/mode/" + lang).Mode));
121121
@@ -141,6 +141,11 @@
142142 }
143143 },
144144
 145+ /* Needed for search/replace */
 146+ 'getContents': function() {
 147+ return context.codeEditor.getSession().getValue();
 148+ },
 149+
145150 /*
146151 * Compatibility with the $.textSelection jQuery plug-in. When the iframe is in use, these functions provide
147152 * equivilant functionality to the otherwise textarea-based functionality.
@@ -183,6 +188,7 @@
184189 range.setEnd( range.start.row, range.start.column + selText.length );
185190 sel.setSelectionRange(range);
186191 }
 192+ return context.$textarea;
187193 },
188194 /**
189195 * Gets the position (in resolution of bytes not nessecarily characters) in a textarea
@@ -200,6 +206,31 @@
201207 * @param endContainer Element in iframe to end selection in. If not set, end is a character offset
202208 */
203209 'setSelection': function( options ) {
 210+ // Ace stores positions for ranges as row/column pairs.
 211+ // To convert from character offsets, we'll need to iterate through the document
 212+ var doc = context.codeEditor.getSession().getDocument();
 213+ var lines = doc.getAllLines();
 214+
 215+ var offsetToPos = function( offset ) {
 216+ var row = 0, col = 0;
 217+ var pos = 0;
 218+ while ( row < lines.length && pos + lines[row].length < offset) {
 219+ pos += lines[row].length;
 220+ pos++; // for the newline
 221+ row++;
 222+ }
 223+ col = offset - pos;
 224+ return {row: row, column: col};
 225+ }
 226+ var start = offsetToPos( options.start ),
 227+ end = offsetToPos( options.end );
 228+
 229+ var sel = context.codeEditor.getSelection();
 230+ var range = sel.getRange();
 231+ range.setStart( start.row, start.column );
 232+ range.setEnd( end.row, end.column );
 233+ sel.setSelectionRange( range );
 234+ return context.$textarea;
204235 },
205236 /**
206237 * Scroll a textarea to the current cursor position. You can set the cursor position with setSelection()
@@ -207,6 +238,7 @@
208239 */
209240 'scrollToCaretPosition': function( options ) {
210241 //context.fn.scrollToTop( context.fn.getElementAtCursor(), true );
 242+ return context.$textarea;
211243 },
212244 /**
213245 * Scroll an element to the top of the iframe

Status & tagging log