Allow checks to be dismissed by fragment or ID

Bug: T379804
Change-Id: I8c275a560971b95e3bbcf4921633e17c0078511f
This commit is contained in:
Ed Sanders 2024-11-19 13:13:06 +00:00
parent 04264c6b6e
commit a26c145574
5 changed files with 57 additions and 15 deletions

View file

@ -274,3 +274,45 @@ mw.editcheck.BaseEditCheck.prototype.isRangeInValidSection = function ( range, d
// If the heading text matches any of ignoreSections, return false.
return !ignoreSections.some( ( section ) => compare( headingText, section ) === 0 );
};
/**
* Dismiss a check action
*
* @param {mw.editCheck.EditCheckAction} action
*/
mw.editcheck.BaseEditCheck.prototype.dismiss = function ( action ) {
const name = this.constructor.static.name;
if ( action.id ) {
const dismissedIds = mw.editcheck.dismissedIds;
dismissedIds[ name ] = dismissedIds[ name ] || [];
dismissedIds[ name ].push( action.id );
} else {
const dismissedFragments = mw.editcheck.dismissedFragments;
dismissedFragments[ name ] = dismissedFragments[ name ] || [];
dismissedFragments[ name ].push( ...action.fragments );
}
};
/**
* Check if this type of check has been dismissed covering a specific range
*
* @param {ve.Range} range
* @return {boolean}
*/
mw.editcheck.BaseEditCheck.prototype.isDismissedRange = function ( range ) {
const fragments = mw.editcheck.dismissedFragments[ this.constructor.static.name ];
return !!fragments && fragments.some(
( fragment ) => fragment.getSelection().getCoveringRange().containsRange( range )
);
};
/**
* Check if an action with a given ID has been dismissed
*
* @param {string} id
* @return {boolean}
*/
mw.editcheck.BaseEditCheck.prototype.isDismissedId = function ( id ) {
const ids = mw.editcheck.dismissedIds[ this.constructor.static.name ];
return ids && ids.indexOf( id ) !== -1;
};

View file

@ -11,6 +11,7 @@ mw.editcheck.EditCheckAction = function MWEditCheckAction( config ) {
this.check = config.check;
this.fragments = config.fragments;
this.message = config.message;
this.id = config.id;
this.title = config.title;
this.icon = config.icon;
this.type = config.type || 'warning';

View file

@ -317,12 +317,7 @@ ve.ui.EditCheckDialog.prototype.onAct = function ( widget, choice, actionChosen,
setTimeout( () => this.surface.getModel().setNullSelection(), 300 );
}
if ( !data ) {
// Nothing happened, just fall back and leave the check
return;
}
if ( this.listener === 'onBeforeSave' ) {
if ( data && this.listener === 'onBeforeSave' ) {
// If an action has been taken, we want to linger for a brief moment
// to show the result of the action before moving away
// TODO: This was written for AddReferenceEditCheck but should be
@ -334,6 +329,8 @@ ve.ui.EditCheckDialog.prototype.onAct = function ( widget, choice, actionChosen,
this.currentOffset = Math.max( 0, this.currentOffset - 1 );
this.update();
}, pause );
} else {
this.updateDebounced();
}
} );
};

View file

@ -17,14 +17,13 @@ mw.editcheck.AddReferenceEditCheck.static.defaultConfig = ve.extendObject( {}, m
} );
mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( surfaceModel ) {
return this.findAddedContent( surfaceModel.getDocument() ).map( ( range ) => {
const fragment = surfaceModel.getLinearFragment( range );
return new mw.editcheck.EditCheckAction( {
fragments: [ fragment ],
check: this
// icon: 'quotes',
} );
} );
return this.findAddedContent( surfaceModel.getDocument() ).filter( ( range ) => !this.isDismissedRange( range ) )
.map(
( range ) => new mw.editcheck.EditCheckAction( {
fragments: [ surfaceModel.getLinearFragment( range ) ],
check: this
} )
);
};
mw.editcheck.AddReferenceEditCheck.prototype.onDocumentChange = mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave;
@ -100,6 +99,7 @@ mw.editcheck.AddReferenceEditCheck.prototype.act = function ( choice, action, su
} ).done( ( data ) => {
if ( data && data.action === 'reject' && data.reason ) {
mw.editcheck.rejections.push( data.reason );
this.dismiss( action );
}
} );
}

View file

@ -1,6 +1,8 @@
mw.editcheck = {
config: require( './config.json' ),
ecenable: !!( new URL( location.href ).searchParams.get( 'ecenable' ) || window.MWVE_FORCE_EDIT_CHECK_ENABLED )
ecenable: !!( new URL( location.href ).searchParams.get( 'ecenable' ) || window.MWVE_FORCE_EDIT_CHECK_ENABLED ),
dismissedFragments: {},
dismissedIds: {}
};
require( './EditCheckContextItem.js' );