1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
mw.editcheck = {
config: require( './config.json' ),
ecenable: !!( new URL( location.href ).searchParams.get( 'ecenable' ) || window.MWVE_FORCE_EDIT_CHECK_ENABLED )
};
require( './EditCheckContextItem.js' );
require( './EditCheckInspector.js' );
require( './EditCheckDialog.js' );
require( './EditCheckFactory.js' );
require( './EditCheckAction.js' );
require( './BaseEditCheck.js' );
// TODO: Load these checks behind feature flags
// require( './ConvertReferenceEditCheck.js' );
// require( './TextMatchEditCheck.js' );
require( './AddReferenceEditCheck.js' );
/**
* Return the content ranges (content branch node interiors) contained within a range
*
* For a content branch node entirely contained within the range, its entire interior
* range will be included. For a content branch node overlapping with the range boundary,
* only the covered part of its interior range will be included.
*
* @param {ve.dm.Document} documentModel The documentModel to search
* @param {ve.Range} range The range to include
* @param {boolean} covers Only include ranges which cover the whole of their node
* @return {ve.Range[]} The contained content ranges (content branch node interiors)
*/
mw.editcheck.getContentRanges = function ( documentModel, range, covers ) {
const ranges = [];
documentModel.selectNodes( range, 'branches' ).forEach( ( spec ) => {
if (
spec.node.canContainContent() && (
!covers || (
!spec.range || // an empty range means the node is covered
spec.range.equalsSelection( spec.nodeRange )
)
)
) {
ranges.push( spec.range || spec.nodeRange );
}
} );
return ranges;
};
mw.editcheck.hasAddedContentNeedingReference = function ( documentModel, includeReferencedContent ) {
// helper for ve.init.mw.ArticleTarget save-tagging, keep logic below in-sync with AddReferenceEditCheck.
// This is bypassing the normal "should this check apply?" logic for creation, so we need to manually
// apply the "only the main namespace" rule.
if ( mw.config.get( 'wgNamespaceNumber' ) !== mw.config.get( 'wgNamespaceIds' )[ '' ] ) {
return false;
}
const check = mw.editcheck.editCheckFactory.create( 'addReference', mw.editcheck.config.addReference );
return check.findAddedContent( documentModel, includeReferencedContent ).length > 0;
};
mw.editcheck.getModifiedRanges = function ( documentModel, coveredNodesOnly ) {
if ( !documentModel.completeHistory.getLength() ) {
return [];
}
const operations = documentModel.completeHistory.squash().transactions[ 0 ].operations;
const ranges = [];
let offset = 0;
const endOffset = documentModel.getDocumentRange().end;
operations.every( ( op ) => {
if ( op.type === 'retain' ) {
offset += op.length;
} else if ( op.type === 'replace' ) {
const insertedRange = new ve.Range( offset, offset + op.insert.length );
offset += op.insert.length;
// 1. Only trigger if the check is a pure insertion, with no adjacent content removed (T340088)
if ( op.remove.length === 0 ) {
ve.batchPush(
ranges,
// 2. Only fully inserted paragraphs (ranges that cover the whole node) (T345121)
mw.editcheck.getContentRanges( documentModel, insertedRange, coveredNodesOnly )
);
}
}
// Reached the end of the doc / start of internal list, stop searching
return offset < endOffset;
} );
return ranges;
};
mw.editcheck.rejections = [];
mw.editcheck.getRejectionReasons = function () {
return mw.editcheck.rejections;
};
mw.editcheck.refCheckShown = false;
if ( mw.config.get( 'wgVisualEditorConfig' ).editCheckTagging ) {
mw.hook( 've.activationComplete' ).add( () => {
const target = ve.init.target;
function getRefNodes() {
// The firstNodes list is a numerically indexed array of reference nodes in the document.
// The list is append only, and removed references are set to undefined in place.
// To check if a new reference is being published, we just need to know if a reference
// with an index beyond the initial list (initLength) is still set.
const internalList = target.getSurface().getModel().getDocument().getInternalList();
const group = internalList.getNodeGroup( 'mwReference/' );
return group ? group.firstNodes || [] : [];
}
const initLength = getRefNodes().length;
target.saveFields.vetags = function () {
const refNodes = getRefNodes();
const newLength = refNodes.length;
let newNodesInDoc = false;
for ( let i = initLength; i < newLength; i++ ) {
if ( refNodes[ i ] ) {
newNodesInDoc = true;
break;
}
}
const tags = [];
if ( newNodesInDoc ) {
tags.push( 'editcheck-newreference' );
}
if ( mw.editcheck.refCheckShown ) {
tags.push( 'editcheck-references-activated' );
}
return tags.join( ',' );
};
} );
mw.hook( 've.deactivationComplete' ).add( () => {
const target = ve.init.target;
delete target.saveFields.vetags;
} );
}
if ( mw.config.get( 'wgVisualEditorConfig' ).editCheck || mw.editcheck.ecenable ) {
let saveProcessDeferred;
mw.hook( 've.preSaveProcess' ).add( ( saveProcess, target ) => {
const surface = target.getSurface();
if ( surface.getMode() !== 'visual' ) {
// Some checks will entirely work in source mode for most cases.
// But others will fail spectacularly -- e.g. reference check
// isn't aware of <ref> tags and so will suggest that all content
// has references added. As such, disable in source mode for now.
return;
}
ve.track( 'counter.editcheck.preSaveChecksAvailable' );
// clear rejection-reasons between runs of the save process, so only the last one counts
mw.editcheck.rejections.length = 0;
let checks = mw.editcheck.editCheckFactory.createAllByListener( 'onBeforeSave', surface.getModel() );
if ( checks.length ) {
ve.track( 'counter.editcheck.preSaveChecksShown' );
mw.editcheck.refCheckShown = true;
const surfaceView = surface.getView();
const toolbar = target.getToolbar();
const reviewToolbar = new ve.ui.PositionedTargetToolbar( target, target.toolbarConfig );
reviewToolbar.setup( [
{
name: 'back',
type: 'bar',
include: [ 'editCheckBack' ]
},
// Placeholder toolbar groups
// TODO: Make a proper TitleTool?
{
name: 'title',
type: 'bar',
include: []
},
{
name: 'save',
// TODO: MobileArticleTarget should ignore 'align'
align: OO.ui.isMobile() ? 'before' : 'after',
type: 'bar',
include: [ 'showSaveDisabled' ]
}
], surface );
reviewToolbar.$element.addClass( 've-ui-editCheck-toolbar' );
reviewToolbar.items[ 1 ].$element.removeClass( 'oo-ui-toolGroup-empty' );
reviewToolbar.items[ 1 ].$group.append(
$( '<span>' ).addClass( 've-ui-editCheck-toolbar-title' ).text( ve.msg( 'editcheck-dialog-title' ) )
);
if ( OO.ui.isMobile() ) {
reviewToolbar.$element.addClass( 've-init-mw-mobileArticleTarget-toolbar' );
}
target.toolbar.$element.before( reviewToolbar.$element );
target.toolbar = reviewToolbar;
saveProcessDeferred = ve.createDeferred();
const context = surface.getContext();
// TODO: Allow multiple checks to be shown when multicheck is enabled
checks = checks.slice( 0, 1 );
// eslint-disable-next-line no-shadow
const drawSelections = ( checks ) => {
const highlightNodes = [];
const selections = [];
checks.forEach( ( check ) => {
highlightNodes.push.apply( highlightNodes, surfaceView.getDocument().selectNodes( check.highlight.getSelection().getCoveringRange(), 'branches' ).map( ( spec ) => spec.node ) );
const selection = ve.ce.Selection.static.newFromModel( check.highlight.getSelection(), surfaceView );
selections.push( selection );
} );
// TODO: Make selections clickable when multicheck is enabled
surfaceView.drawSelections(
'editCheck',
checks.map( ( check ) => ve.ce.Selection.static.newFromModel( check.highlight.getSelection(), surfaceView ) )
);
surfaceView.setReviewMode( true, highlightNodes );
};
const contextDone = ( responseData, contextData ) => {
if ( !responseData ) {
// this is the back button
return saveProcessDeferred.resolve();
}
const selectionIndex = checks.indexOf( contextData.action );
if ( responseData.action !== 'reject' ) {
mw.notify( ve.msg( 'editcheck-dialog-addref-success-notify' ), { type: 'success' } );
} else if ( responseData.reason ) {
mw.editcheck.rejections.push( responseData.reason );
}
// TODO: Move on to the next issue, when multicheck is enabled
// checks = mw.editcheck.editCheckFactory.createAllByListener( 'onBeforeSave', surface.getModel() );
checks = [];
if ( checks.length ) {
context.removePersistentSource( 'editCheckReferences' );
setTimeout( () => {
// timeout needed to wait out the newly added content being focused
surface.getModel().setNullSelection();
drawSelections( checks );
setTimeout( () => {
// timeout needed to allow the context to reposition
showCheckContext( checks[ Math.min( selectionIndex, checks.length - 1 ) ] );
} );
}, 500 );
} else {
saveProcessDeferred.resolve( true );
}
};
// eslint-disable-next-line no-inner-declarations
function showCheckContext( check ) {
const fragment = check.highlight;
// Select the found content to correctly position the context on desktop
fragment.select();
context.addPersistentSource( {
embeddable: false,
data: {
action: check,
fragment: fragment,
callback: contextDone,
saveProcessDeferred: saveProcessDeferred
},
name: 'editCheckReferences'
} );
// Deactivate to prevent selection suppressing mobile context
surface.getView().deactivate();
// Once the context is positioned, clear the selection
setTimeout( () => {
surface.getModel().setNullSelection();
} );
}
drawSelections( checks );
toolbar.toggle( false );
target.onContainerScroll();
saveProcess.next( () => {
showCheckContext( checks[ 0 ] );
return saveProcessDeferred.promise().then( ( data ) => {
context.removePersistentSource( 'editCheckReferences' );
surfaceView.drawSelections( 'editCheck', [] );
surfaceView.setReviewMode( false );
reviewToolbar.$element.remove();
toolbar.toggle( true );
target.toolbar = toolbar;
target.onContainerScroll();
// Check the user inserted a citation
if ( data ) {
const delay = ve.createDeferred();
// If they inserted, wait 2 seconds on desktop before showing save dialog
setTimeout( () => {
ve.track( 'counter.editcheck.preSaveChecksCompleted' );
delay.resolve();
}, !OO.ui.isMobile() && data.action !== 'reject' ? 2000 : 0 );
return delay.promise();
} else {
ve.track( 'counter.editcheck.preSaveChecksAbandoned' );
return ve.createDeferred().reject().promise();
}
} );
} );
} else {
// Counterpart to earlier preSaveChecksShown, for use in tracking
// errors in check-generation:
ve.track( 'counter.editcheck.preSaveChecksNotShown' );
}
} );
mw.hook( 've.deactivationComplete' ).add( () => {
if ( saveProcessDeferred ) {
saveProcessDeferred.reject();
}
} );
}
ve.ui.EditCheckBack = function VeUiEditCheckBack() {
// Parent constructor
ve.ui.EditCheckBack.super.apply( this, arguments );
this.setDisabled( false );
};
OO.inheritClass( ve.ui.EditCheckBack, ve.ui.Tool );
ve.ui.EditCheckBack.static.name = 'editCheckBack';
ve.ui.EditCheckBack.static.icon = 'previous';
ve.ui.EditCheckBack.static.autoAddToCatchall = false;
ve.ui.EditCheckBack.static.autoAddToGroup = false;
ve.ui.EditCheckBack.static.title =
OO.ui.deferMsg( 'visualeditor-backbutton-tooltip' );
ve.ui.EditCheckBack.prototype.onSelect = function () {
const context = this.toolbar.getSurface().getContext();
if ( context.inspector ) {
context.inspector.close();
} else {
context.items[ 0 ].close();
}
this.setActive( false );
};
ve.ui.EditCheckBack.prototype.onUpdateState = function () {
this.setDisabled( false );
};
ve.ui.toolFactory.register( ve.ui.EditCheckBack );
ve.ui.EditCheckSaveDisabled = function VeUiEditCheckSaveDisabled() {
// Parent constructor
ve.ui.EditCheckSaveDisabled.super.apply( this, arguments );
};
OO.inheritClass( ve.ui.EditCheckSaveDisabled, ve.ui.MWSaveTool );
ve.ui.EditCheckSaveDisabled.static.name = 'showSaveDisabled';
ve.ui.EditCheckSaveDisabled.static.autoAddToCatchall = false;
ve.ui.EditCheckSaveDisabled.static.autoAddToGroup = false;
ve.ui.EditCheckSaveDisabled.prototype.onUpdateState = function () {
this.setDisabled( true );
};
ve.ui.toolFactory.register( ve.ui.EditCheckSaveDisabled );
|