Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.WhitespacePreservingTextInputWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface WhitespacePreservingTextInputWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Text input widget which hides but preserves leading and trailing whitespace
9 *
10 * @class
11 * @extends OO.ui.MultilineTextInputWidget
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 * @param {string} [config.valueAndWhitespace=''] Initial value and whitespace
16 * @param {number} [config.limit] Maximum number of characters to preserve at each end
17 */
18 ve.ui.WhitespacePreservingTextInputWidget = function VeUiWhitespacePreservingTextInputWidget( config ) {
19 // Configuration
20 config = config || {};
21
22 // Parent constructor
23 ve.ui.WhitespacePreservingTextInputWidget.super.call( this, config );
24
25 this.limit = config.limit;
26
27 this.setWhitespace( [ '', '' ] );
28 this.setValueAndWhitespace( config.valueAndWhitespace || '' );
29
30 this.$element.addClass( 've-ui-whitespacePreservingTextInputWidget' );
31 };
32
33 /* Inheritance */
34
35 OO.inheritClass( ve.ui.WhitespacePreservingTextInputWidget, OO.ui.MultilineTextInputWidget );
36
37 /* Methods */
38
39 /**
40 * Set the value of the widget and extract whitespace.
41 *
42 * @param {string} value
43 */
44 ve.ui.WhitespacePreservingTextInputWidget.prototype.setValueAndWhitespace = function ( value ) {
45 const leftValue = this.limit ? value.slice( 0, this.limit ) : value;
46 this.whitespace[ 0 ] = leftValue.match( /^\s*/ )[ 0 ];
47 value = value.slice( this.whitespace[ 0 ].length );
48
49 const rightValue = this.limit ? value.slice( -this.limit ) : value;
50 this.whitespace[ 1 ] = rightValue.match( /\s*$/ )[ 0 ];
51 value = value.slice( 0, value.length - this.whitespace[ 1 ].length );
52
53 this.setValue( value );
54 };
55
56 /**
57 * Set the value of the widget and extract whitespace.
58 *
59 * @param {string[]} whitespace Outer whitespace
60 */
61 ve.ui.WhitespacePreservingTextInputWidget.prototype.setWhitespace = function ( whitespace ) {
62 this.whitespace = whitespace;
63 };
64
65 /**
66 * Get the value of text widget, including hidden outer whitespace
67 *
68 * @return {string} Text widget value including whitespace
69 */
70 ve.ui.WhitespacePreservingTextInputWidget.prototype.getValueAndWhitespace = function () {
71 if ( !this.whitespace ) {
72 // In case getValue() is called from a parent constructor
73 return this.value;
74 }
75 return this.whitespace[ 0 ] + this.value + this.whitespace[ 1 ];
76 };