Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWEditSummaryWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWEditSummaryWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Multi line text input for edit summary, with auto completion based on
9 * the user's previous edit summaries.
10 *
11 * @class
12 * @extends OO.ui.MultilineTextInputWidget
13 * @mixes OO.ui.mixin.LookupElement
14 *
15 * @constructor
16 * @param {Object} [config] Configuration options
17 * @param {number} [config.limit=6] Number of suggestions to show
18 */
19 ve.ui.MWEditSummaryWidget = function VeUiMWEditSummaryWidget( config ) {
20 config = config || {};
21
22 // Parent method
23 ve.ui.MWEditSummaryWidget.super.call( this, ve.extendObject( {
24 autosize: true,
25 maxRows: 15,
26 allowLinebreaks: false
27 }, config ) );
28
29 // Mixin method
30 OO.ui.mixin.LookupElement.call( this, ve.extendObject( {
31 showPendingRequest: false,
32 showSuggestionsOnFocus: false,
33 allowSuggestionsWhenEmpty: false,
34 highlightFirst: false
35 }, config ) );
36
37 this.limit = config.limit || 6;
38 };
39
40 /* Inheritance */
41
42 OO.inheritClass( ve.ui.MWEditSummaryWidget, OO.ui.MultilineTextInputWidget );
43
44 OO.mixinClass( ve.ui.MWEditSummaryWidget, OO.ui.mixin.LookupElement );
45
46 /* Static properties */
47
48 ve.ui.MWEditSummaryWidget.static.summarySplitter = /^(\/\*.*?\*\/\s*)?([^]*)$/;
49
50 /* Static methods */
51
52 /**
53 * Split a summary into the section and the actual summary
54 *
55 * @param {string} summary
56 * @return {Object} Object with section and comment string properties
57 */
58 ve.ui.MWEditSummaryWidget.static.splitSummary = function ( summary ) {
59 const result = summary.match( this.summarySplitter );
60 return {
61 section: result[ 1 ] || '',
62 comment: result[ 2 ]
63 };
64 };
65
66 /**
67 * Filter a list of edit summaries to a specific query string
68 *
69 * @param {string[]} summaries Edit summaries
70 * @param {string} query User query
71 * @return {string[]} Filtered edit summaries
72 */
73 ve.ui.MWEditSummaryWidget.static.getMatchingSummaries = function ( summaries, query ) {
74 const summaryPrefixMatches = [], wordPrefixMatches = [], otherMatches = [],
75 lowerQuery = query.toLowerCase();
76
77 if ( !query.trim() ) {
78 // Show no results for empty query
79 return [];
80 }
81
82 summaries.forEach( ( summary ) => {
83 const lowerSummary = summary.toLowerCase(),
84 index = lowerSummary.indexOf( lowerQuery );
85 if ( index === 0 ) {
86 // Exclude exact matches
87 if ( lowerQuery !== summary ) {
88 summaryPrefixMatches.push( summary );
89 }
90 } else if ( index !== -1 ) {
91 if ( /^\s/.test( lowerSummary.charAt( index - 1 ) ) ) {
92 // Character before match is whitespace
93 wordPrefixMatches.push( summary );
94 } else {
95 otherMatches.push( summary );
96 }
97 }
98 } );
99 return summaryPrefixMatches.concat( wordPrefixMatches, otherMatches );
100 };
101
102 /* Methods */
103
104 /**
105 * @inheritdoc
106 */
107 ve.ui.MWEditSummaryWidget.prototype.adjustSize = function () {
108 // To autosize, the widget will render another element beneath the input
109 // with the same text for measuring. This extra element could cause scrollbars
110 // to appear, changing the available width, so if scrollbars are intially
111 // hidden, force them to stay hidden during the adjustment.
112 // TODO: Consider upstreaming this?
113 const scrollContainer = this.getClosestScrollableElementContainer();
114 const hasScrollbar = scrollContainer.offsetWidth > scrollContainer.scrollWidth;
115 let overflowY;
116 if ( !hasScrollbar ) {
117 overflowY = scrollContainer.style.overflowY;
118 scrollContainer.style.overflowY = 'hidden';
119 }
120
121 // Parent method
122 ve.ui.MWEditSummaryWidget.super.prototype.adjustSize.apply( this, arguments );
123
124 if ( !hasScrollbar ) {
125 scrollContainer.style.overflowY = overflowY;
126 }
127
128 return this;
129 };
130
131 /**
132 * Get recent edit summaries for the logged in user
133 *
134 * @return {jQuery.Promise} Promise which resolves with a list of summaries
135 */
136 ve.ui.MWEditSummaryWidget.prototype.getSummaries = function () {
137 const splitSummary = this.constructor.static.splitSummary.bind( this.constructor.static );
138 if ( !this.getSummariesPromise ) {
139 if ( mw.user.isAnon() ) {
140 this.getSummariesPromise = ve.createDeferred().resolve( [] ).promise();
141 } else {
142 // Allow this for temp users as well. The isAnon() check above is just to avoid autocompleting
143 // with someone else's summaries.
144 this.getSummariesPromise = ve.init.target.getLocalApi().get( {
145 action: 'query',
146 list: 'usercontribs',
147 ucuser: mw.user.getName(),
148 ucprop: 'comment',
149 uclimit: 500
150 } ).then( ( response ) => {
151 const usedComments = {},
152 changes = ve.getProp( response, 'query', 'usercontribs' ) || [];
153
154 return changes
155 // Filter out changes without comment (e.g. due to RevisionDelete)
156 .filter( ( change ) => Object.prototype.hasOwnProperty.call( change, 'comment' ) )
157 // Remove section /* headings */
158 .map( ( change ) => splitSummary( change.comment ).comment.trim() )
159 // Filter out duplicates and empty comments
160 .filter( ( comment ) => {
161 if ( !comment || Object.prototype.hasOwnProperty.call( usedComments, comment ) ) {
162 return false;
163 }
164 usedComments[ comment ] = true;
165 return true;
166 } )
167 .sort();
168 } );
169 }
170 }
171 return this.getSummariesPromise;
172 };
173
174 /**
175 * @inheritdoc
176 */
177 ve.ui.MWEditSummaryWidget.prototype.getLookupRequest = function () {
178 const query = this.constructor.static.splitSummary( this.value ),
179 limit = this.limit;
180
181 return this.getSummaries().then( ( allSummaries ) => {
182 const matchingSummaries = this.constructor.static.getMatchingSummaries( allSummaries, query.comment );
183 if ( matchingSummaries.length > limit ) {
184 // Quick in-place truncate
185 matchingSummaries.length = limit;
186 }
187 return { summaries: matchingSummaries, section: query.section };
188 } ).promise( { abort: () => {} } ); // don't abort, the actual request will be the same anyway
189 };
190
191 /**
192 * @inheritdoc
193 */
194 ve.ui.MWEditSummaryWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
195 return response;
196 };
197
198 /**
199 * @inheritdoc
200 */
201 ve.ui.MWEditSummaryWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
202 return data.summaries.map( ( item ) => new OO.ui.MenuOptionWidget( {
203 label: item,
204 data: data.section + item
205 } ) );
206 };