2017-04-25 11:01:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWEditSummaryWidget class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2017-04-25 11:01:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multi line text input for edit summary, with auto completion based on
|
|
|
|
* the user's previous edit summaries.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.MultilineTextInputWidget
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes OO.ui.mixin.LookupElement
|
2017-04-25 11:01:41 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @cfg {number} [limit=6] Number of suggestions to show
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget = function VeUiMWEditSummaryWidget( config ) {
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent method
|
2021-01-13 21:07:30 +00:00
|
|
|
ve.ui.MWEditSummaryWidget.super.call( this, ve.extendObject( {
|
2020-05-31 16:31:04 +00:00
|
|
|
autosize: true,
|
|
|
|
maxRows: 15,
|
2023-10-25 11:39:13 +00:00
|
|
|
allowLinebreaks: false
|
2021-01-13 21:07:30 +00:00
|
|
|
}, config ) );
|
2017-04-25 11:01:41 +00:00
|
|
|
|
|
|
|
// Mixin method
|
2019-04-24 16:58:46 +00:00
|
|
|
OO.ui.mixin.LookupElement.call( this, ve.extendObject( {
|
2019-05-23 19:29:34 +00:00
|
|
|
showPendingRequest: false,
|
2019-05-24 10:08:57 +00:00
|
|
|
showSuggestionsOnFocus: false,
|
2019-04-24 16:58:46 +00:00
|
|
|
allowSuggestionsWhenEmpty: false,
|
|
|
|
highlightFirst: false
|
|
|
|
}, config ) );
|
2017-04-25 11:01:41 +00:00
|
|
|
|
|
|
|
this.limit = config.limit || 6;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWEditSummaryWidget, OO.ui.MultilineTextInputWidget );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ui.MWEditSummaryWidget, OO.ui.mixin.LookupElement );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
2022-02-18 18:48:11 +00:00
|
|
|
ve.ui.MWEditSummaryWidget.static.summarySplitter = /^(\/\*.*?\*\/\s*)?([^]*)$/;
|
2017-04-25 11:01:41 +00:00
|
|
|
|
|
|
|
/* Static methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a summary into the section and the actual summary
|
2020-06-02 20:30:00 +00:00
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {string} summary
|
2017-04-25 11:01:41 +00:00
|
|
|
* @return {Object} Object with section and comment string properties
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.static.splitSummary = function ( summary ) {
|
|
|
|
var result = summary.match( this.summarySplitter );
|
|
|
|
return {
|
|
|
|
section: result[ 1 ] || '',
|
|
|
|
comment: result[ 2 ]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-08-05 08:00:54 +00:00
|
|
|
* Filter a list of edit summaries to a specific query string
|
2017-04-25 11:01:41 +00:00
|
|
|
*
|
|
|
|
* @param {string[]} summaries Edit summaries
|
|
|
|
* @param {string} query User query
|
|
|
|
* @return {string[]} Filtered edit summaries
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.static.getMatchingSummaries = function ( summaries, query ) {
|
|
|
|
var summaryPrefixMatches = [], wordPrefixMatches = [], otherMatches = [],
|
|
|
|
lowerQuery = query.toLowerCase();
|
|
|
|
|
|
|
|
if ( !query.trim() ) {
|
|
|
|
// Show no results for empty query
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
summaries.forEach( function ( summary ) {
|
|
|
|
var lowerSummary = summary.toLowerCase(),
|
|
|
|
index = lowerSummary.indexOf( lowerQuery );
|
|
|
|
if ( index === 0 ) {
|
|
|
|
// Exclude exact matches
|
|
|
|
if ( lowerQuery !== summary ) {
|
|
|
|
summaryPrefixMatches.push( summary );
|
|
|
|
}
|
|
|
|
} else if ( index !== -1 ) {
|
2022-03-24 10:20:01 +00:00
|
|
|
if ( /^\s/.test( lowerSummary.charAt( index - 1 ) ) ) {
|
2017-04-25 11:01:41 +00:00
|
|
|
// Character before match is whitespace
|
|
|
|
wordPrefixMatches.push( summary );
|
|
|
|
} else {
|
|
|
|
otherMatches.push( summary );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return summaryPrefixMatches.concat( wordPrefixMatches, otherMatches );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2020-05-31 16:31:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.prototype.adjustSize = function () {
|
|
|
|
// To autosize, the widget will render another element beneath the input
|
|
|
|
// with the same text for measuring. This extra element could cause scrollbars
|
|
|
|
// to appear, changing the available width, so if scrollbars are intially
|
|
|
|
// hidden, force them to stay hidden during the adjustment.
|
|
|
|
// TODO: Consider upstreaming this?
|
|
|
|
var scrollContainer = this.getClosestScrollableElementContainer();
|
|
|
|
var hasScrollbar = scrollContainer.offsetWidth > scrollContainer.scrollWidth;
|
|
|
|
var overflowY;
|
|
|
|
if ( !hasScrollbar ) {
|
|
|
|
overflowY = scrollContainer.style.overflowY;
|
|
|
|
scrollContainer.style.overflowY = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWEditSummaryWidget.super.prototype.adjustSize.apply( this, arguments );
|
|
|
|
|
|
|
|
if ( !hasScrollbar ) {
|
|
|
|
scrollContainer.style.overflowY = overflowY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-04-25 11:01:41 +00:00
|
|
|
/**
|
|
|
|
* Get recent edit summaries for the logged in user
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise} Promise which resolves with a list of summaries
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.prototype.getSummaries = function () {
|
|
|
|
var splitSummary = this.constructor.static.splitSummary.bind( this.constructor.static );
|
|
|
|
if ( !this.getSummariesPromise ) {
|
|
|
|
if ( mw.user.isAnon() ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
this.getSummariesPromise = ve.createDeferred().resolve( [] ).promise();
|
2017-04-25 11:01:41 +00:00
|
|
|
} else {
|
2023-04-28 13:57:46 +00:00
|
|
|
// Allow this for temp users as well. The isAnon() check above is just to avoid autocompleting
|
|
|
|
// with someone else's summaries.
|
2019-04-30 18:30:50 +00:00
|
|
|
this.getSummariesPromise = ve.init.target.getLocalApi().get( {
|
2017-04-25 11:01:41 +00:00
|
|
|
action: 'query',
|
|
|
|
list: 'usercontribs',
|
|
|
|
ucuser: mw.user.getName(),
|
2021-03-13 12:43:13 +00:00
|
|
|
ucprop: 'comment',
|
2021-07-07 10:09:22 +00:00
|
|
|
uclimit: 500
|
2017-04-25 11:01:41 +00:00
|
|
|
} ).then( function ( response ) {
|
|
|
|
var usedComments = {},
|
|
|
|
changes = ve.getProp( response, 'query', 'usercontribs' ) || [];
|
|
|
|
|
|
|
|
return changes
|
2021-03-13 12:42:34 +00:00
|
|
|
// Filter out changes without comment (e.g. due to RevisionDelete)
|
|
|
|
.filter( function ( change ) {
|
|
|
|
return Object.prototype.hasOwnProperty.call( change, 'comment' );
|
|
|
|
} )
|
2017-04-25 11:01:41 +00:00
|
|
|
// Remove section /* headings */
|
|
|
|
.map( function ( change ) {
|
|
|
|
return splitSummary( change.comment ).comment.trim();
|
|
|
|
} )
|
|
|
|
// Filter out duplicates and empty comments
|
|
|
|
.filter( function ( comment ) {
|
|
|
|
if ( !comment || Object.prototype.hasOwnProperty.call( usedComments, comment ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
usedComments[ comment ] = true;
|
|
|
|
return true;
|
|
|
|
} )
|
|
|
|
.sort();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.getSummariesPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.prototype.getLookupRequest = function () {
|
|
|
|
var query = this.constructor.static.splitSummary( this.value ),
|
|
|
|
limit = this.limit,
|
|
|
|
widget = this;
|
|
|
|
|
|
|
|
return this.getSummaries().then( function ( allSummaries ) {
|
|
|
|
var matchingSummaries = widget.constructor.static.getMatchingSummaries( allSummaries, query.comment );
|
|
|
|
if ( matchingSummaries.length > limit ) {
|
|
|
|
// Quick in-place truncate
|
|
|
|
matchingSummaries.length = limit;
|
|
|
|
}
|
|
|
|
return { summaries: matchingSummaries, section: query.section };
|
|
|
|
} ).promise( { abort: function () {} } ); // don't abort, the actual request will be the same anyway
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWEditSummaryWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
|
|
|
|
return data.summaries.map( function ( item ) {
|
|
|
|
return new OO.ui.MenuOptionWidget( {
|
|
|
|
label: item,
|
|
|
|
data: data.section + item
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|