summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/jquery/jquery.textSelection.js
blob: da882708b9642f3483791d04c7a116d00ed75e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*!
 * These plugins provide extra functionality for interaction with textareas.
 *
 * - encapsulateSelection: Ported from skins/common/edit.js by Trevor Parscal
 *   © 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org
 * - getCaretPosition, scrollToCaretPosition: Ported from Wikia's LinkSuggest extension
 *   https://github.com/Wikia/app/blob/c0cd8b763/extensions/wikia/LinkSuggest/js/jquery.wikia.linksuggest.js
 *   © 2010 Inez Korczyński (korczynski@gmail.com) & Jesús Martínez Novo (martineznovo@gmail.com) (GPLv2)
 */

/**
 * @class jQuery.plugin.textSelection
 *
 * Do things to the selection in a `<textarea>`, or a textarea-like editable element.
 *
 *     var $textbox = $( '#wpTextbox1' );
 *     $textbox.textSelection( 'setContents', 'This is bold!' );
 *     $textbox.textSelection( 'setSelection', { start: 8, end: 12 } );
 *     $textbox.textSelection( 'encapsulateSelection', { pre: '<b>', post: '</b>' } );
 *     // Result: Textbox contains 'This is <b>bold</b>!', with cursor before the '!'
 */
( function ( $ ) {
	/**
	 * Do things to the selection in a `<textarea>`, or a textarea-like editable element.
	 *
	 *     var $textbox = $( '#wpTextbox1' );
	 *     $textbox.textSelection( 'setContents', 'This is bold!' );
	 *     $textbox.textSelection( 'setSelection', { start: 8, end: 12 } );
	 *     $textbox.textSelection( 'encapsulateSelection', { pre: '<b>', post: '</b>' } );
	 *     // Result: Textbox contains 'This is <b>bold</b>!', with cursor before the '!'
	 *
	 * @param {string} command Command to execute, one of:
	 *
	 *  - {@link jQuery.plugin.textSelection#getContents getContents}
	 *  - {@link jQuery.plugin.textSelection#setContents setContents}
	 *  - {@link jQuery.plugin.textSelection#getSelection getSelection}
	 *  - {@link jQuery.plugin.textSelection#replaceSelection replaceSelection}
	 *  - {@link jQuery.plugin.textSelection#encapsulateSelection encapsulateSelection}
	 *  - {@link jQuery.plugin.textSelection#getCaretPosition getCaretPosition}
	 *  - {@link jQuery.plugin.textSelection#setSelection setSelection}
	 *  - {@link jQuery.plugin.textSelection#scrollToCaretPosition scrollToCaretPosition}
	 *  - {@link jQuery.plugin.textSelection#register register}
	 *  - {@link jQuery.plugin.textSelection#unregister unregister}
	 * @param {Mixed} [options] Options to pass to the command
	 * @return {Mixed} Depending on the command
	 */
	$.fn.textSelection = function ( command, options ) {
		var fn,
			alternateFn,
			retval;

		fn = {
			/**
			 * Get the contents of the textarea.
			 *
			 * @private
			 * @return {string}
			 */
			getContents: function () {
				return this.val();
			},

			/**
			 * Set the contents of the textarea, replacing anything that was there before.
			 *
			 * @private
			 * @param {string} content
			 * @return {jQuery}
			 * @chainable
			 */
			setContents: function ( content ) {
				return this.each( function () {
					var scrollTop = this.scrollTop;
					$( this ).val( content );
					// Setting this.value may scroll the textarea, restore the scroll position
					this.scrollTop = scrollTop;
				} );
			},

			/**
			 * Get the currently selected text in this textarea.
			 *
			 * @private
			 * @return {string}
			 */
			getSelection: function () {
				var retval,
					el = this.get( 0 );

				if ( !el ) {
					retval = '';
				} else {
					retval = el.value.substring( el.selectionStart, el.selectionEnd );
				}

				return retval;
			},

			/**
			 * Replace the selected text in the textarea with the given text, or insert it at the cursor.
			 *
			 * @private
			 * @param {string} value
			 * @return {jQuery}
			 * @chainable
			 */
			replaceSelection: function ( value ) {
				return this.each( function () {
					var allText, currSelection, startPos, endPos;

					allText = $( this ).textSelection( 'getContents' );
					currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
					startPos = currSelection[ 0 ];
					endPos = currSelection[ 1 ];

					$( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
						allText.slice( endPos ) );
					$( this ).textSelection( 'setSelection', {
						start: startPos,
						end: startPos + value.length
					} );
				} );
			},

			/**
			 * Insert text at the beginning and end of a text selection, optionally
			 * inserting text at the caret when selection is empty.
			 *
			 * Also focusses the textarea.
			 *
			 * @private
			 * @param {Object} [options]
			 * @param {string} [options.pre] Text to insert before the cursor/selection
			 * @param {string} [options.peri] Text to insert between pre and post and select afterwards
			 * @param {string} [options.post] Text to insert after the cursor/selection
			 * @param {boolean} [options.ownline=false] Put the inserted text on a line of its own
			 * @param {boolean} [options.replace=false] If there is a selection, replace it with peri
			 *  instead of leaving it alone
			 * @param {boolean} [options.selectPeri=true] Select the peri text if it was inserted (but not
			 *  if there was a selection and replace==false, or if splitlines==true)
			 * @param {boolean} [options.splitlines=false] If multiple lines are selected, encapsulate
			 *  each line individually
			 * @param {number} [options.selectionStart] Position to start selection at
			 * @param {number} [options.selectionEnd=options.selectionStart] Position to end selection at
			 * @return {jQuery}
			 * @chainable
			 */
			encapsulateSelection: function ( options ) {
				return this.each( function () {
					var selText, allText, currSelection, insertText,
						combiningCharSelectionBug = false,
						isSample, startPos, endPos,
						pre = options.pre,
						post = options.post;

					/**
					 * @ignore
					 * Check if the selected text is the same as the insert text
					 */
					function checkSelectedText() {
						if ( !selText ) {
							selText = options.peri;
							isSample = true;
						} else if ( options.replace ) {
							selText = options.peri;
						} else {
							while ( selText.charAt( selText.length - 1 ) === ' ' ) {
								// Exclude ending space char
								selText = selText.slice( 0, -1 );
								post += ' ';
							}
							while ( selText.charAt( 0 ) === ' ' ) {
								// Exclude prepending space char
								selText = selText.slice( 1 );
								pre = ' ' + pre;
							}
						}
					}

					/**
					 * @ignore
					 * Do the splitlines stuff.
					 *
					 * Wrap each line of the selected text with pre and post
					 *
					 * @param {string} selText Selected text
					 * @param {string} pre Text before
					 * @param {string} post Text after
					 * @return {string} Wrapped text
					 */
					function doSplitLines( selText, pre, post ) {
						var i,
							insertText = '',
							selTextArr = selText.split( '\n' );
						for ( i = 0; i < selTextArr.length; i++ ) {
							insertText += pre + selTextArr[ i ] + post;
							if ( i !== selTextArr.length - 1 ) {
								insertText += '\n';
							}
						}
						return insertText;
					}

					isSample = false;
					$( this ).focus();
					if ( options.selectionStart !== undefined ) {
						$( this ).textSelection( 'setSelection', { start: options.selectionStart, end: options.selectionEnd } );
					}

					selText = $( this ).textSelection( 'getSelection' );
					allText = $( this ).textSelection( 'getContents' );
					currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
					startPos = currSelection[ 0 ];
					endPos = currSelection[ 1 ];
					checkSelectedText();
					if (
						options.selectionStart !== undefined &&
						endPos - startPos !== options.selectionEnd - options.selectionStart
					) {
						// This means there is a difference in the selection range returned by browser and what we passed.
						// This happens for Safari 5.1, Chrome 12 in the case of composite characters. Ref T32130
						// Set the startPos to the correct position.
						startPos = options.selectionStart;
						combiningCharSelectionBug = true;
						// TODO: The comment above is from 2011. Is this still a problem for browsers we support today?
						// Minimal test case: https://jsfiddle.net/z4q7a2ko/
					}

					insertText = pre + selText + post;
					if ( options.splitlines ) {
						insertText = doSplitLines( selText, pre, post );
					}
					if ( options.ownline ) {
						if ( startPos !== 0 && allText.charAt( startPos - 1 ) !== '\n' && allText.charAt( startPos - 1 ) !== '\r' ) {
							insertText = '\n' + insertText;
							pre += '\n';
						}
						if ( allText.charAt( endPos ) !== '\n' && allText.charAt( endPos ) !== '\r' ) {
							insertText += '\n';
							post += '\n';
						}
					}
					if ( combiningCharSelectionBug ) {
						$( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + insertText +
							allText.slice( endPos ) );
					} else {
						$( this ).textSelection( 'replaceSelection', insertText );
					}
					if ( isSample && options.selectPeri && ( !options.splitlines || ( options.splitlines && selText.indexOf( '\n' ) === -1 ) ) ) {
						$( this ).textSelection( 'setSelection', {
							start: startPos + pre.length,
							end: startPos + pre.length + selText.length
						} );
					} else {
						$( this ).textSelection( 'setSelection', {
							start: startPos + insertText.length
						} );
					}
					$( this ).trigger( 'encapsulateSelection', [ options.pre, options.peri, options.post, options.ownline,
						options.replace, options.splitlines ] );
				} );
			},

			/**
			 * Get the current cursor position (in UTF-16 code units) in a textarea.
			 *
			 * @private
			 * @param {Object} [options]
			 * @param {Object} [options.startAndEnd=false] Return range of the selection rather than just start
			 * @return {Mixed}
			 *  - When `startAndEnd` is `false`: number
			 *  - When `startAndEnd` is `true`: array with two numbers, for start and end of selection
			 */
			getCaretPosition: function ( options ) {
				function getCaret( e ) {
					var caretPos = 0,
						endPos = 0;
					if ( e ) {
						caretPos = e.selectionStart;
						endPos = e.selectionEnd;
					}
					return options.startAndEnd ? [ caretPos, endPos ] : caretPos;
				}
				return getCaret( this.get( 0 ) );
			},

			/**
			 * Set the current cursor position (in UTF-16 code units) in a textarea.
			 *
			 * @private
			 * @param {Object} [options]
			 * @param {number} options.start
			 * @param {number} [options.end=options.start]
			 * @return {jQuery}
			 * @chainable
			 */
			setSelection: function ( options ) {
				return this.each( function () {
					// Opera 9.0 doesn't allow setting selectionStart past
					// selectionEnd; any attempts to do that will be ignored
					// Make sure to set them in the right order
					if ( options.start > this.selectionEnd ) {
						this.selectionEnd = options.end;
						this.selectionStart = options.start;
					} else {
						this.selectionStart = options.start;
						this.selectionEnd = options.end;
					}
				} );
			},

			/**
			 * Scroll a textarea to the current cursor position. You can set the cursor
			 * position with #setSelection.
			 *
			 * @private
			 * @param {Object} [options]
			 * @param {string} [options.force=false] Whether to force a scroll even if the caret position
			 *  is already visible.
			 * @return {jQuery}
			 * @chainable
			 */
			scrollToCaretPosition: function ( options ) {
				return this.each( function () {
					var
						clientHeight = this.clientHeight,
						origValue = this.value,
						origSelectionStart = this.selectionStart,
						origSelectionEnd = this.selectionEnd,
						origScrollTop = this.scrollTop,
						calcScrollTop;

					// Delete all text after the selection and scroll the textarea to the end.
					// This ensures the selection is visible (aligned to the bottom of the textarea).
					// Then restore the text we deleted without changing scroll position.
					this.value = this.value.slice( 0, this.selectionEnd );
					this.scrollTop = this.scrollHeight;
					// Chrome likes to adjust scroll position when changing value, so save and re-set later.
					// Note that this is not equal to scrollHeight, it's scrollHeight minus clientHeight.
					calcScrollTop = this.scrollTop;
					this.value = origValue;
					this.selectionStart = origSelectionStart;
					this.selectionEnd = origSelectionEnd;

					if ( !options.force ) {
						// Check if all the scrolling was unnecessary and if so, restore previous position.
						// If the current position is no more than a screenful above the original,
						// the selection was previously visible on the screen.
						if ( calcScrollTop < origScrollTop && origScrollTop - calcScrollTop < clientHeight ) {
							calcScrollTop = origScrollTop;
						}
					}

					this.scrollTop = calcScrollTop;

					$( this ).trigger( 'scrollToPosition' );
				} );
			}
		};

		/**
		 * @method register
		 *
		 * Register an alternative textSelection API for this element.
		 *
		 * @private
		 * @param {Object} functions Functions to replace. Keys are command names (as in #textSelection,
		 *  except 'register' and 'unregister'). Values are functions to execute when a given command is
		 *  called.
		 */

		/**
		 * @method unregister
		 *
		 * Unregister the alternative textSelection API for this element (see #register).
		 *
		 * @private
		 */

		alternateFn = $( this ).data( 'jquery.textSelection' );

		// Apply defaults
		switch ( command ) {
			// case 'getContents': // no params
			// case 'setContents': // no params with defaults
			// case 'getSelection': // no params
			// case 'replaceSelection': // no params with defaults
			case 'encapsulateSelection':
				options = $.extend( {
					pre: '',
					peri: '',
					post: '',
					ownline: false,
					replace: false,
					selectPeri: true,
					splitlines: false,
					selectionStart: undefined,
					selectionEnd: undefined
				}, options );
				break;
			case 'getCaretPosition':
				options = $.extend( {
					startAndEnd: false
				}, options );
				break;
			case 'setSelection':
				options = $.extend( {
					start: undefined,
					end: undefined
				}, options );
				if ( options.end === undefined ) {
					options.end = options.start;
				}
				break;
			case 'scrollToCaretPosition':
				options = $.extend( {
					force: false
				}, options );
				break;
			case 'register':
				if ( alternateFn ) {
					throw new Error( 'Another textSelection API was already registered' );
				}
				$( this ).data( 'jquery.textSelection', options );
				// No need to update alternateFn as this command only stores the options.
				// A command that uses it will set it again.
				return;
			case 'unregister':
				$( this ).removeData( 'jquery.textSelection' );
				return;
		}

		retval = ( alternateFn && alternateFn[ command ] || fn[ command ] ).call( this, options );

		return retval;
	};

	/**
	 * @class jQuery
	 */
	/**
	 * @method textSelection
	 * @inheritdoc jQuery.plugin.textSelection#textSelection
	 */

}( jQuery ) );