EditCheck: Replace highlights/selection with fragments

'selection' is only used by AddReferenceEditCheck but this can
be done in the act method.

Provide a method for getting highlight selections from
these 'affected fragments' that can be overridden.

Change-Id: Ida661682efd8ae99d945f0e310ea3ce12efc8770
This commit is contained in:
Ed Sanders 2024-12-05 12:10:28 +00:00
parent 13a949e74d
commit e62327efe0
7 changed files with 25 additions and 20 deletions

View file

@ -3,14 +3,12 @@
*
* @param {Object} config
* @param {mw.editcheck.BaseEditCheck} check
* @param {ve.dm.SurfaceFragment[]} highlights Fragments to highlight
* @param {ve.dm.SurfaceFragment} selection Fragment to select when acting
* @param {ve.dm.SurfaceFragment[]} fragments Affected fragments
* @param {jQuery|string|Function|OO.ui.HtmlSnippet} message Check message body
*/
mw.editcheck.EditCheckAction = function MWEditCheckAction( config ) {
this.check = config.check;
this.highlights = config.highlights;
this.selection = config.selection;
this.fragments = config.fragments;
this.message = config.message;
};
@ -25,6 +23,15 @@ mw.editcheck.EditCheckAction.prototype.getChoices = function () {
return this.check.getChoices( this );
};
/**
* Get selections to highlight for this check
*
* @return {ve.dm.Selection[]}
*/
mw.editcheck.EditCheckAction.prototype.getHighlightSelections = function () {
return this.fragments.map( ( fragment ) => fragment.getSelection() );
};
/**
* Get a description of the check
*

View file

@ -60,8 +60,8 @@ ve.ui.EditCheckDialog.prototype.update = function () {
label: check.message,
framed: false
} ).$element );
check.highlights.forEach( ( highlight ) => {
selections.push( ve.ce.Selection.static.newFromModel( highlight.getSelection(), surfaceView ) );
check.getHighlightSelections().forEach( ( selection ) => {
selections.push( ve.ce.Selection.static.newFromModel( selection, surfaceView ) );
} );
} );
surfaceView.getSelectionManager().drawSelections( 'editCheckWarning', selections );

View file

@ -78,7 +78,7 @@ mw.editcheck.EditCheckFactory.prototype.createAllByListener = function ( listene
}
} );
newChecks.sort(
( a, b ) => a.highlights[ 0 ].getSelection().getCoveringRange().start - b.highlights[ 0 ].getSelection().getCoveringRange().start
( a, b ) => a.getHighlightSelections()[ 0 ].getCoveringRange().start - b.getHighlightSelections()[ 0 ].getCoveringRange().start
);
return newChecks;
};

View file

@ -18,8 +18,7 @@ mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( surfaceMo
return this.findAddedContent( surfaceModel.getDocument() ).map( ( range ) => {
const fragment = surfaceModel.getLinearFragment( range );
return new mw.editcheck.EditCheckAction( {
highlights: [ fragment ],
selection: this.adjustForPunctuation( fragment.collapseToEnd() ),
fragments: [ fragment ],
check: this
} );
} );
@ -60,7 +59,8 @@ mw.editcheck.AddReferenceEditCheck.prototype.act = function ( choice, action, co
switch ( choice ) {
case 'accept':
ve.track( 'activity.editCheckReferences', { action: 'edit-check-confirm' } );
action.selection.select();
this.adjustForPunctuation( action.fragments[ 0 ].collapseToEnd() ).select();
return windowAction.open( 'citoid' ).then( ( instance ) => instance.closing ).then( ( citoidData ) => {
const citoidOrCiteDataDeferred = ve.createDeferred();
@ -95,7 +95,7 @@ mw.editcheck.AddReferenceEditCheck.prototype.act = function ( choice, action, co
return windowAction.open(
'editCheckReferencesInspector',
{
fragment: action.highlights[ 0 ],
fragment: action.fragments[ 0 ],
callback: contextItem.data.callback,
saveProcessDeferred: contextItem.data.saveProcessDeferred
}

View file

@ -22,8 +22,7 @@ mw.editcheck.ConvertReferenceEditCheck.prototype.onDocumentChange = function ( s
if ( href ) {
const fragment = surfaceModel.getLinearFragment( node.getOuterRange() );
return new mw.editcheck.EditCheckAction( {
highlights: [ fragment ],
selection: fragment,
fragments: [ fragment ],
message: ve.msg( 'citoid-referencecontextitem-convert-message' ),
check: this
} );

View file

@ -22,8 +22,7 @@ mw.editcheck.TextMatchEditCheck.prototype.onDocumentChange = function ( surfaceM
const fragment = surfaceModel.getLinearFragment( range );
actions.push(
new mw.editcheck.EditCheckAction( {
highlights: [ fragment ],
selection: fragment,
fragments: [ fragment ],
message: replacer.message,
check: this
} )

View file

@ -153,10 +153,10 @@ if ( mw.config.get( 'wgVisualEditorConfig' ).editCheck || mw.editcheck.ecenable
const highlightNodes = [];
const selections = [];
checks.forEach( ( check ) => {
check.highlights.forEach( ( highlight ) => {
highlightNodes.push.apply( highlightNodes, surfaceView.getDocument().selectNodes( highlight.getSelection().getCoveringRange(), 'branches' ).map( ( spec ) => spec.node ) );
const selection = ve.ce.Selection.static.newFromModel( highlight.getSelection(), surfaceView );
selections.push( selection );
check.getHighlightSelections().forEach( ( selection ) => {
highlightNodes.push.apply( highlightNodes, surfaceView.getDocument().selectNodes( selection.getCoveringRange(), 'branches' ).map( ( spec ) => spec.node ) );
const selectionView = ve.ce.Selection.static.newFromModel( selection, surfaceView );
selections.push( selectionView );
} );
} );
// TODO: Make selections clickable when multicheck is enabled
@ -201,7 +201,7 @@ if ( mw.config.get( 'wgVisualEditorConfig' ).editCheck || mw.editcheck.ecenable
// eslint-disable-next-line no-inner-declarations
function showCheckContext( check ) {
const fragment = check.highlights[ 0 ];
const fragment = check.fragments[ 0 ];
// Select the found content to correctly position the context on desktop
fragment.select();