2 * These plugins provide extra functionality for interaction with textareas.
5 $.fn
.textSelection = function ( command
, options
) {
12 * Get the contents of the textarea
16 getContents: function () {
20 * Set the contents of the textarea, replacing anything that was there before
22 * @param {string} content
24 setContents: function ( content
) {
28 * Get the currently selected text in this textarea.
32 getSelection: function () {
36 if ( !el
|| $( el
).is( ':hidden' ) ) {
38 } else if ( el
.selectionStart
|| el
.selectionStart
=== 0 ) {
39 retval
= el
.value
.substring( el
.selectionStart
, el
.selectionEnd
);
45 * Ported from skins/common/edit.js by Trevor Parscal
46 * (c) 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org
48 * Inserts text at the beginning and end of a text selection, optionally
49 * inserting text at the caret when selection is empty.
51 * @param {Object} options Options
52 * FIXME document the options parameters
55 encapsulateSelection: function ( options
) {
56 return this.each( function () {
57 var selText
, scrollTop
, insertText
,
58 isSample
, startPos
, endPos
,
63 * Check if the selected text is the same as the insert text
65 function checkSelectedText() {
67 selText
= options
.peri
;
69 } else if ( options
.replace
) {
70 selText
= options
.peri
;
72 while ( selText
.charAt( selText
.length
- 1 ) === ' ' ) {
73 // Exclude ending space char
74 selText
= selText
.slice( 0, -1 );
77 while ( selText
.charAt( 0 ) === ' ' ) {
78 // Exclude prepending space char
79 selText
= selText
.slice( 1 );
86 * Do the splitlines stuff.
88 * Wrap each line of the selected text with pre and post
90 * @param {string} selText Selected text
91 * @param {string} pre Text before
92 * @param {string} post Text after
93 * @return {string} Wrapped text
95 function doSplitLines( selText
, pre
, post
) {
98 selTextArr
= selText
.split( '\n' );
99 for ( i
= 0; i
< selTextArr
.length
; i
++ ) {
100 insertText
+= pre
+ selTextArr
[ i
] + post
;
101 if ( i
!== selTextArr
.length
- 1 ) {
109 // Do nothing if display none
110 if ( this.style
.display
!== 'none' ) {
111 if ( this.selectionStart
|| this.selectionStart
=== 0 ) {
113 if ( options
.selectionStart
!== undefined ) {
114 $( this ).textSelection( 'setSelection', { start
: options
.selectionStart
, end
: options
.selectionEnd
} );
117 selText
= $( this ).textSelection( 'getSelection' );
118 startPos
= this.selectionStart
;
119 endPos
= this.selectionEnd
;
120 scrollTop
= this.scrollTop
;
123 options
.selectionStart
!== undefined &&
124 endPos
- startPos
!== options
.selectionEnd
- options
.selectionStart
126 // This means there is a difference in the selection range returned by browser and what we passed.
127 // This happens for Chrome in the case of composite characters. Ref bug #30130
128 // Set the startPos to the correct position.
129 startPos
= options
.selectionStart
;
132 insertText
= pre
+ selText
+ post
;
133 if ( options
.splitlines
) {
134 insertText
= doSplitLines( selText
, pre
, post
);
136 if ( options
.ownline
) {
137 if ( startPos
!== 0 && this.value
.charAt( startPos
- 1 ) !== '\n' && this.value
.charAt( startPos
- 1 ) !== '\r' ) {
138 insertText
= '\n' + insertText
;
141 if ( this.value
.charAt( endPos
) !== '\n' && this.value
.charAt( endPos
) !== '\r' ) {
146 this.value
= this.value
.slice( 0, startPos
) + insertText
+
147 this.value
.slice( endPos
);
148 // Setting this.value scrolls the textarea to the top, restore the scroll position
149 this.scrollTop
= scrollTop
;
150 if ( window
.opera
) {
151 pre
= pre
.replace( /\r?\n/g, '\r\n' );
152 selText
= selText
.replace( /\r?\n/g, '\r\n' );
153 post
= post
.replace( /\r?\n/g, '\r\n' );
155 if ( isSample
&& options
.selectPeri
&& ( !options
.splitlines
|| ( options
.splitlines
&& selText
.indexOf( '\n' ) === -1 ) ) ) {
156 this.selectionStart
= startPos
+ pre
.length
;
157 this.selectionEnd
= startPos
+ pre
.length
+ selText
.length
;
159 this.selectionStart
= startPos
+ insertText
.length
;
160 this.selectionEnd
= this.selectionStart
;
164 $( this ).trigger( 'encapsulateSelection', [ options
.pre
, options
.peri
, options
.post
, options
.ownline
,
165 options
.replace
, options
.spitlines
] );
169 * Ported from Wikia's LinkSuggest extension
170 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
172 * Get the position (in resolution of bytes not necessarily characters)
175 * @param {Object} options Options
176 * FIXME document the options parameters
177 * @return {number} Position
179 getCaretPosition: function ( options
) {
180 function getCaret( e
) {
184 if ( e
&& ( e
.selectionStart
|| e
.selectionStart
=== 0 ) ) {
185 caretPos
= e
.selectionStart
;
186 endPos
= e
.selectionEnd
;
188 return options
.startAndEnd
? [ caretPos
, endPos
] : caretPos
;
190 return getCaret( this.get( 0 ) );
193 * @param {Object} options options
194 * FIXME document the options parameters
197 setSelection: function ( options
) {
198 return this.each( function () {
199 // Do nothing if hidden
200 if ( !$( this ).is( ':hidden' ) ) {
201 if ( this.selectionStart
|| this.selectionStart
=== 0 ) {
202 // Opera 9.0 doesn't allow setting selectionStart past
203 // selectionEnd; any attempts to do that will be ignored
204 // Make sure to set them in the right order
205 if ( options
.start
> this.selectionEnd
) {
206 this.selectionEnd
= options
.end
;
207 this.selectionStart
= options
.start
;
209 this.selectionStart
= options
.start
;
210 this.selectionEnd
= options
.end
;
217 * Ported from Wikia's LinkSuggest extension
218 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
220 * Scroll a textarea to the current cursor position. You can set the cursor
221 * position with setSelection()
223 * @param {Object} options options
224 * @cfg {boolean} [force=false] Whether to force a scroll even if the caret position
225 * is already visible.
226 * FIXME document the options parameters
229 scrollToCaretPosition: function ( options
) {
230 function getLineLength( e
) {
231 return Math
.floor( e
.scrollWidth
/ ( $.client
.profile().platform
=== 'linux' ? 7 : 8 ) );
233 function getCaretScrollPosition( e
) {
234 // FIXME: This functions sucks and is off by a few lines most
235 // of the time. It should be replaced by something decent.
238 text
= e
.value
.replace( /\r/g, '' ),
239 caret
= $( e
).textSelection( 'getCaretPosition' ),
240 lineLength
= getLineLength( e
),
245 for ( i
= 0; i
< caret
; i
++ ) {
247 if ( text
.charAt( i
) === ' ' ) {
248 lastSpaceInLine
= charInLine
;
249 } else if ( text
.charAt( i
) === '\n' ) {
254 if ( charInLine
> lineLength
) {
255 if ( lastSpaceInLine
> 0 ) {
256 charInLine
= charInLine
- lastSpaceInLine
;
263 for ( j
= caret
; j
< caret
+ lineLength
; j
++ ) {
265 text
.charAt( j
) === ' ' ||
266 text
.charAt( j
) === '\n' ||
267 caret
=== text
.length
273 if ( nextSpace
> lineLength
&& caret
<= lineLength
) {
274 charInLine
= caret
- lastSpaceInLine
;
277 return ( $.client
.profile().platform
=== 'mac' ? 13 : ( $.client
.profile().platform
=== 'linux' ? 15 : 16 ) ) * row
;
279 return this.each( function () {
281 // Do nothing if hidden
282 if ( !$( this ).is( ':hidden' ) ) {
283 if ( this.selectionStart
|| this.selectionStart
=== 0 ) {
284 scroll
= getCaretScrollPosition( this );
285 if ( options
.force
|| scroll
< $( this ).scrollTop() ||
286 scroll
> $( this ).scrollTop() + $( this ).height() ) {
287 $( this ).scrollTop( scroll
);
291 $( this ).trigger( 'scrollToPosition' );
296 alternateFn
= $( this ).data( 'jquery.textSelection' );
300 // case 'getContents': // no params
301 // case 'setContents': // no params with defaults
302 // case 'getSelection': // no params
303 case 'encapsulateSelection':
304 options
= $.extend( {
305 pre
: '', // Text to insert before the cursor/selection
306 peri
: '', // Text to insert between pre and post and select afterwards
307 post
: '', // Text to insert after the cursor/selection
308 ownline
: false, // Put the inserted text on a line of its own
309 replace
: false, // If there is a selection, replace it with peri instead of leaving it alone
310 selectPeri
: true, // Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true)
311 splitlines
: false, // If multiple lines are selected, encapsulate each line individually
312 selectionStart
: undefined, // Position to start selection at
313 selectionEnd
: undefined // Position to end selection at. Defaults to start
316 case 'getCaretPosition':
317 options
= $.extend( {
318 // Return [start, end] instead of just start
321 // FIXME: We may not need character position-based functions if we insert markers in the right places
324 options
= $.extend( {
325 // Position to start selection at
327 // Position to end selection at. Defaults to start
331 if ( options
.end
=== undefined ) {
332 options
.end
= options
.start
;
334 // FIXME: We may not need character position-based functions if we insert markers in the right places
336 case 'scrollToCaretPosition':
337 options
= $.extend( {
338 force
: false // Force a scroll even if the caret position is already visible
343 throw new Error( 'Another textSelection API was already registered' );
345 $( this ).data( 'jquery.textSelection', options
);
346 // No need to update alternateFn as this command only stores the options.
347 // A command that uses it will set it again.
350 $( this ).removeData( 'jquery.textSelection' );
354 retval
= ( alternateFn
&& alternateFn
[ command
] || fn
[ command
] ).call( this, options
);