dc9d4cfc2c7cbac2fbd3d9435c991d419b3c5ff7
2 * These plugins provide extra functionality for interaction with textareas.
4 * - encapsulateSelection: Ported from skins/common/edit.js by Trevor Parscal
5 * © 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org
6 * - getCaretPosition, scrollToCaretPosition: Ported from Wikia's LinkSuggest extension
7 * https://github.com/Wikia/app/blob/c0cd8b763/extensions/wikia/LinkSuggest/js/jquery.wikia.linksuggest.js
8 * © 2010 Inez Korczyński (korczynski@gmail.com) & Jesús Martínez Novo (martineznovo@gmail.com) (GPLv2)
11 $.fn
.textSelection = function ( command
, options
) {
18 * Get the contents of the textarea
22 getContents: function () {
26 * Set the contents of the textarea, replacing anything that was there before
28 * @param {string} content
30 setContents: function ( content
) {
34 * Get the currently selected text in this textarea.
38 getSelection: function () {
42 if ( !el
|| $( el
).is( ':hidden' ) ) {
44 } else if ( el
.selectionStart
|| el
.selectionStart
=== 0 ) {
45 retval
= el
.value
.substring( el
.selectionStart
, el
.selectionEnd
);
51 * Inserts text at the beginning and end of a text selection, optionally
52 * inserting text at the caret when selection is empty.
54 * @param {Object} options Options
55 * FIXME document the options parameters
58 encapsulateSelection: function ( options
) {
59 return this.each( function () {
60 var selText
, scrollTop
, insertText
,
61 isSample
, startPos
, endPos
,
66 * Check if the selected text is the same as the insert text
68 function checkSelectedText() {
70 selText
= options
.peri
;
72 } else if ( options
.replace
) {
73 selText
= options
.peri
;
75 while ( selText
.charAt( selText
.length
- 1 ) === ' ' ) {
76 // Exclude ending space char
77 selText
= selText
.slice( 0, -1 );
80 while ( selText
.charAt( 0 ) === ' ' ) {
81 // Exclude prepending space char
82 selText
= selText
.slice( 1 );
89 * Do the splitlines stuff.
91 * Wrap each line of the selected text with pre and post
93 * @param {string} selText Selected text
94 * @param {string} pre Text before
95 * @param {string} post Text after
96 * @return {string} Wrapped text
98 function doSplitLines( selText
, pre
, post
) {
101 selTextArr
= selText
.split( '\n' );
102 for ( i
= 0; i
< selTextArr
.length
; i
++ ) {
103 insertText
+= pre
+ selTextArr
[ i
] + post
;
104 if ( i
!== selTextArr
.length
- 1 ) {
112 // Do nothing if display none
113 if ( this.style
.display
!== 'none' ) {
114 if ( this.selectionStart
|| this.selectionStart
=== 0 ) {
116 if ( options
.selectionStart
!== undefined ) {
117 $( this ).textSelection( 'setSelection', { start
: options
.selectionStart
, end
: options
.selectionEnd
} );
120 selText
= $( this ).textSelection( 'getSelection' );
121 startPos
= this.selectionStart
;
122 endPos
= this.selectionEnd
;
123 scrollTop
= this.scrollTop
;
126 options
.selectionStart
!== undefined &&
127 endPos
- startPos
!== options
.selectionEnd
- options
.selectionStart
129 // This means there is a difference in the selection range returned by browser and what we passed.
130 // This happens for Chrome in the case of composite characters. Ref bug #30130
131 // Set the startPos to the correct position.
132 startPos
= options
.selectionStart
;
135 insertText
= pre
+ selText
+ post
;
136 if ( options
.splitlines
) {
137 insertText
= doSplitLines( selText
, pre
, post
);
139 if ( options
.ownline
) {
140 if ( startPos
!== 0 && this.value
.charAt( startPos
- 1 ) !== '\n' && this.value
.charAt( startPos
- 1 ) !== '\r' ) {
141 insertText
= '\n' + insertText
;
144 if ( this.value
.charAt( endPos
) !== '\n' && this.value
.charAt( endPos
) !== '\r' ) {
149 this.value
= this.value
.slice( 0, startPos
) + insertText
+
150 this.value
.slice( endPos
);
151 // Setting this.value scrolls the textarea to the top, restore the scroll position
152 this.scrollTop
= scrollTop
;
153 if ( window
.opera
) {
154 pre
= pre
.replace( /\r?\n/g, '\r\n' );
155 selText
= selText
.replace( /\r?\n/g, '\r\n' );
156 post
= post
.replace( /\r?\n/g, '\r\n' );
158 if ( isSample
&& options
.selectPeri
&& ( !options
.splitlines
|| ( options
.splitlines
&& selText
.indexOf( '\n' ) === -1 ) ) ) {
159 this.selectionStart
= startPos
+ pre
.length
;
160 this.selectionEnd
= startPos
+ pre
.length
+ selText
.length
;
162 this.selectionStart
= startPos
+ insertText
.length
;
163 this.selectionEnd
= this.selectionStart
;
167 $( this ).trigger( 'encapsulateSelection', [ options
.pre
, options
.peri
, options
.post
, options
.ownline
,
168 options
.replace
, options
.splitlines
] );
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 * Scroll a textarea to the current cursor position. You can set the cursor
218 * position with setSelection()
220 * @param {Object} options options
221 * @cfg {boolean} [force=false] Whether to force a scroll even if the caret position
222 * is already visible.
223 * FIXME document the options parameters
226 scrollToCaretPosition: function ( options
) {
227 function getLineLength( e
) {
228 return Math
.floor( e
.scrollWidth
/ ( $.client
.profile().platform
=== 'linux' ? 7 : 8 ) );
230 function getCaretScrollPosition( e
) {
231 // FIXME: This functions sucks and is off by a few lines most
232 // of the time. It should be replaced by something decent.
235 text
= e
.value
.replace( /\r/g, '' ),
236 caret
= $( e
).textSelection( 'getCaretPosition' ),
237 lineLength
= getLineLength( e
),
242 for ( i
= 0; i
< caret
; i
++ ) {
244 if ( text
.charAt( i
) === ' ' ) {
245 lastSpaceInLine
= charInLine
;
246 } else if ( text
.charAt( i
) === '\n' ) {
251 if ( charInLine
> lineLength
) {
252 if ( lastSpaceInLine
> 0 ) {
253 charInLine
= charInLine
- lastSpaceInLine
;
260 for ( j
= caret
; j
< caret
+ lineLength
; j
++ ) {
262 text
.charAt( j
) === ' ' ||
263 text
.charAt( j
) === '\n' ||
264 caret
=== text
.length
270 if ( nextSpace
> lineLength
&& caret
<= lineLength
) {
271 charInLine
= caret
- lastSpaceInLine
;
274 return ( $.client
.profile().platform
=== 'mac' ? 13 : ( $.client
.profile().platform
=== 'linux' ? 15 : 16 ) ) * row
;
276 return this.each( function () {
278 // Do nothing if hidden
279 if ( !$( this ).is( ':hidden' ) ) {
280 if ( this.selectionStart
|| this.selectionStart
=== 0 ) {
281 scroll
= getCaretScrollPosition( this );
282 if ( options
.force
|| scroll
< $( this ).scrollTop() ||
283 scroll
> $( this ).scrollTop() + $( this ).height() ) {
284 $( this ).scrollTop( scroll
);
288 $( this ).trigger( 'scrollToPosition' );
293 alternateFn
= $( this ).data( 'jquery.textSelection' );
297 // case 'getContents': // no params
298 // case 'setContents': // no params with defaults
299 // case 'getSelection': // no params
300 case 'encapsulateSelection':
301 options
= $.extend( {
302 pre
: '', // Text to insert before the cursor/selection
303 peri
: '', // Text to insert between pre and post and select afterwards
304 post
: '', // Text to insert after the cursor/selection
305 ownline
: false, // Put the inserted text on a line of its own
306 replace
: false, // If there is a selection, replace it with peri instead of leaving it alone
307 selectPeri
: true, // Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true)
308 splitlines
: false, // If multiple lines are selected, encapsulate each line individually
309 selectionStart
: undefined, // Position to start selection at
310 selectionEnd
: undefined // Position to end selection at. Defaults to start
313 case 'getCaretPosition':
314 options
= $.extend( {
315 // Return [start, end] instead of just start
320 options
= $.extend( {
321 // Position to start selection at
323 // Position to end selection at. Defaults to start
327 if ( options
.end
=== undefined ) {
328 options
.end
= options
.start
;
331 case 'scrollToCaretPosition':
332 options
= $.extend( {
333 force
: false // Force a scroll even if the caret position is already visible
338 throw new Error( 'Another textSelection API was already registered' );
340 $( this ).data( 'jquery.textSelection', options
);
341 // No need to update alternateFn as this command only stores the options.
342 // A command that uses it will set it again.
345 $( this ).removeData( 'jquery.textSelection' );
349 retval
= ( alternateFn
&& alternateFn
[ command
] || fn
[ command
] ).call( this, options
);