All files / mobile.editor.overlay SourceEditorOverlay.js

41.08% Statements 83/202
27.27% Branches 18/66
31.7% Functions 13/41
41.29% Lines 83/201

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 5951x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               5x                   5x 5x                   5x 5x 5x 5x     5x       5x   5x 5x 5x                     5x                                                                                       5x 5x   5x                                                                         5x     5x 5x   5x 5x         5x     5x     5x       5x           5x                                             5x           5x       5x                                             1x       1x 1x 1x   1x       1x 1x 1x 1x     1x 1x 1x   1x   1x 1x         1x                     1x                                                     4x 4x       4x   4x 4x           4x                               4x     4x                 1x                 5x   5x   4x   4x     4x       4x 4x   4x 1x 1x 1x 1x     1x     4x         4x                                                                                                                                                                                                                                                                                                                                                                                                             4x       1x  
const mobile = require( 'mobile.startup' ),
	EditorOverlayBase = require( './EditorOverlayBase.js' ),
	util = mobile.util,
	spinner = mobile.spinner,
	Section = mobile.Section,
	saveFailureMessage = require( './saveFailureMessage.js' ),
	EditorGateway = require( './EditorGateway.js' ),
	MessageBox = mobile.MessageBox,
	setPreferredEditor = require( './setPreferredEditor.js' ),
	VisualEditorOverlay = require( './VisualEditorOverlay.js' ),
	currentPage = mobile.currentPage;
 
/**
 * Overlay that shows an editor
 *
 * @uses Section
 * @uses EditorGateway
 * @uses VisualEditorOverlay
 * @private
 */
class SourceEditorOverlay extends EditorOverlayBase {
	/**
	 * @param {Object} options Configuration options
	 * @param {jQuery.Promise} [dataPromise] Optional promise for loading content
	 */
	constructor( options, dataPromise ) {
		super(
			util.extend( true,
				{ events: { 'input .wikitext-editor': 'onInputWikitextEditor' } },
				options,
				{ dataPromise }
			)
		);
	}
 
	initialize( options ) {
		this.isFirefox = /firefox/i.test( window.navigator.userAgent );
		this.gateway = new EditorGateway( {
			api: options.api,
			title: options.title,
			sectionId: options.sectionId,
			oldId: options.oldId,
			fromModified: !!options.dataPromise,
			preload: options.preload,
			preloadparams: options.preloadparams,
			editintro: options.editintro
		} );
		this.readOnly = !!options.oldId; // If old revision, readOnly mode
		this.dataPromise = options.dataPromise || this.gateway.getContent();
		this.currentPage = currentPage();
		Iif ( this.currentPage.isVEVisualAvailable() ) {
			options.editSwitcher = true;
		}
		Iif ( this.readOnly ) {
			options.readOnly = true;
			options.editingMsg = mw.msg( 'mobile-frontend-editor-viewing-source-page', options.title );
		} else {
			options.editingMsg = mw.msg( 'mobile-frontend-editor-editing-page', options.title );
		}
		options.previewingMsg = mw.msg( 'mobile-frontend-editor-previewing-page', options.title );
		this.sectionLine = '';
		super.initialize( options );
	}
 
	get editor() {
		return 'wikitext';
	}
 
	/**
	 * @inheritdoc
	 */
	get templatePartials() {
		return util.extend(
			{},
			super.templatePartials, {
				content: util.template( `
		<div lang="{{contentLang}}" dir="{{contentDir}}" class="editor-container content">
			<textarea class="wikitext-editor" id="wikitext-editor" cols="40" rows="10" placeholder="{{placeholder}}"></textarea>
			<div class="preview collapsible-headings-expanded mw-body-content"></div>
		</div>
				` )
			}
		);
	}
 
	/**
	 * @inheritdoc
	 */
	show() {
		super.show();
		// Ensure we do this after showing the overlay, otherwise it doesn't work.
		this._resizeEditor();
	}
 
	/**
	 * Wikitext Editor input handler
	 *
	 */
	onInputWikitextEditor() {
		this.gateway.setContent( this.$el.find( '.wikitext-editor' ).val() );
		this.$el.find( '.continue, .submit' ).prop( 'disabled', false );
	}
 
	/**
	 * @inheritdoc
	 */
	onClickBack() {
		super.onClickBack();
		this._hidePreview();
	}
 
	/**
	 * @inheritdoc
	 */
	postRender() {
		// log edit attempt
		this.log( { action: 'ready' } );
		this.log( { action: 'loaded' } );
 
		Iif ( this.currentPage.isVEVisualAvailable() ) {
			mw.loader.using( 'ext.visualEditor.switching' ).then( () => {
				const toolFactory = new OO.ui.ToolFactory(),
					toolGroupFactory = new OO.ui.ToolGroupFactory();
 
				toolFactory.register( mw.libs.ve.MWEditModeVisualTool );
				toolFactory.register( mw.libs.ve.MWEditModeSourceTool );
				const switchToolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory, {
					classes: [ 'editor-switcher' ]
				} );
 
				switchToolbar.on( 'switchEditor', ( mode ) => {
					if ( mode === 'visual' ) {
						if ( !this.gateway.hasChanged ) {
							this._switchToVisualEditor();
						} else {
							// Pass wikitext if there are changes.
							this._switchToVisualEditor( this.gateway.content );
						}
					}
				} );
 
				switchToolbar.setup( [
					{
						name: 'editMode',
						type: 'list',
						icon: 'edit',
						title: mw.msg( 'visualeditor-mweditmode-tooltip' ),
						include: [ 'editModeVisual', 'editModeSource' ]
					}
				] );
 
				this.$el.find( '.switcher-container' ).html( switchToolbar.$element );
				switchToolbar.emit( 'updateState' );
			} );
		}
 
		super.postRender();
 
		// This spinner is still used when displaying save/preview panel
		this.$el.find( '.overlay-content' ).append( spinner().$el );
		this.hideSpinner();
 
		this.$preview = this.$el.find( '.preview' );
		this.$content = this.$el.find( '.wikitext-editor' );
		// The following classes can be used here:
		// * mw-editfont-monospace
		// * mw-editfont-sans-serif
		// * mw-editfont-serif
		this.$content.addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) );
 
		// make license links open in separate tabs
		this.$el.find( '.license a' ).attr( 'target', '_blank' );
 
		// If in readOnly mode, make textarea readonly
		Iif ( this.readOnly ) {
			this.$content.prop( 'readonly', true );
		}
 
		this.$content
			.on( 'input', () => this._resizeEditor() )
			.one( 'input', () => {
				this.log( { action: 'firstChange' } );
			} );
 
		Iif ( this.isFirefox ) {
			this.$content.on( 'mousedown', () => {
				// Support: Firefox Mobile
				// Firefox scrolls back to the top of the page *every time*
				// you tap on the textarea. This makes things slightly
				// more usable by restoring your scroll offset every time
				// the page scrolls for the next 1000ms.
				// The page will still flicker every time the user touches
				// to place the cursor, but this is better than completely
				// losing your scroll offset. (T214880)
				const docEl = document.documentElement,
					scrollTop = docEl.scrollTop;
				function blockScroll() {
					docEl.scrollTop = scrollTop;
				}
				window.addEventListener( 'scroll', blockScroll );
				setTimeout( () => {
					window.removeEventListener( 'scroll', blockScroll );
				}, 1000 );
			} );
		}
 
		// Render edit summary
		this.summaryTextArea = new OO.ui.MultilineTextInputWidget( {
			placeholder: this.options.summaryMsg,
			classes: [ 'summary' ],
			value: '',
			maxRows: 2
		} );
		this.$el.find( '.summary-input' ).append(
			this.summaryTextArea.$element
		);
 
		this._loadContent();
	}
 
	/**
	 * Handles click on "Edit without login" in anonymous editing warning.
	 *
	 * @private
	 */
	onClickAnonymous() {
		this.$anonWarning.hide();
		this.$anonTalkWarning.hide();
		// reenable "Next" button
		this.$anonHiddenButtons.show();
		this.$content.show();
		this._resizeEditor();
	}
 
	/**
	 * Prepares the preview interface and reveals the save screen of the overlay
	 *
	 * @inheritdoc
	 */
	onStageChanges() {
		const params = {
			text: this.getContent()
		};
 
		this.scrollTop = util.getDocument().find( 'body' ).scrollTop();
		this.$content.hide();
		this.showSpinner();
 
		Iif ( mw.config.get( 'wgIsMainPage' ) ) {
			params.mainpage = 1; // Setting it to 0 will have the same effect
		}
 
		const hideSpinnerAndShowPreview = () => {
			this.hideSpinner();
			this.$preview.show();
			mw.hook( 'wikipage.content' ).fire( this.$preview );
		};
 
		this.gateway.getPreview( params ).then( ( result ) => {
			const parsedText = result.text,
				parsedSectionLine = result.line;
 
			this.sectionId = result.id;
			// On desktop edit summaries strip tags. Mimic this behavior on mobile devices
			this.sectionLine = this.parseHTML( '<div>' ).html( parsedSectionLine ).text();
			new Section( {
				el: this.$preview,
				text: parsedText
			} ).$el.find( 'a' ).on( 'click', false );
 
			hideSpinnerAndShowPreview();
		}, () => {
			this.$preview.replaceWith(
				new MessageBox( {
					type: 'error',
					msg: mw.msg( 'mobile-frontend-editor-error-preview' )
				} ).$el
			);
			hideSpinnerAndShowPreview();
		} );
 
		super.onStageChanges();
	}
 
	/**
	 * Hides the preview and reverts back to initial screen.
	 *
	 * @private
	 */
	_hidePreview() {
		this.gateway.abortPreview();
		this.hideSpinner();
		// FIXME: Don't rely on internals - we re-render template instead.
		this.$preview.removeClass(
			'cdx-message--error'
		).hide();
		this.$content.show();
		window.scrollTo( 0, this.scrollTop );
		this.showHidden( '.initial-header' );
	}
 
	/**
	 * Resize the editor textarea, maintaining scroll position in iOS
	 *
	 */
	_resizeEditor() {
		let scrollTop, container, $scrollContainer;
 
		Eif ( !this.$scrollContainer ) {
			container = OO.ui.Element.static
				.getClosestScrollableContainer( this.$content[ 0 ] );
			// The scroll container will be either within the view
			// or the document element itself.
			$scrollContainer = this.$el.find( container ).length ?
				this.$el.find( container ) : util.getDocument();
			this.$scrollContainer = $scrollContainer;
			this.$content.css( 'padding-bottom', this.$scrollContainer.height() * 0.6 );
		} else {
			$scrollContainer = this.$scrollContainer;
		}
 
		// Only do this if scroll container exists
		Iif ( this.$content.prop( 'scrollHeight' ) && $scrollContainer.length ) {
			scrollTop = $scrollContainer.scrollTop();
			this.$content
				.css( 'height', 'auto' )
				// can't reuse prop( 'scrollHeight' ) because we need the current value
				.css( 'height', ( this.$content.prop( 'scrollHeight' ) + 2 ) + 'px' );
			$scrollContainer.scrollTop( scrollTop );
		}
	}
 
	/**
	 * Set content to the user input field.
	 *
	 * @param {string} content The content to set.
	 */
	setContent( content ) {
		this.$content
			.show()
			.val( content );
		this._resizeEditor();
	}
 
	/**
	 * Returns the content of the user input field.
	 *
	 * @return {string}
	 */
	getContent() {
		return this.$content.val();
	}
 
	/**
	 * Requests content from the API and reveals it in UI.
	 *
	 * @private
	 */
	_loadContent() {
		this.$content.hide();
 
		this.getLoadingPromise()
			.then( ( result ) => {
				const content = result.text;
 
				this.setContent( content );
 
				// If the loaded content is not the default content, enable the save button
				Iif ( this.hasChanged() ) {
					this.$el.find( '.continue, .submit' ).prop( 'disabled', false );
				}
 
				const options = this.options;
				const showAnonWarning = options.isAnon && !options.switched;
 
				if ( showAnonWarning ) {
					this.$anonWarning = this.createAnonWarning( options );
					this.$anonTalkWarning = this.createAnonTalkWarning();
					this.$el.find( '.editor-container' ).append( [ this.$anonTalkWarning, this.$anonWarning ] );
					this.$content.hide();
					// the user has to click login, signup or edit without login,
					// disable "Next" button on top right
					this.$anonHiddenButtons = this.$el.find( '.overlay-header .continue' ).hide();
				}
 
				Iif ( this.gateway.fromModified ) {
					// Trigger intial EditorGateway#setContent and update save button
					this.onInputWikitextEditor();
				}
 
				this.showEditNotices();
			} );
	}
 
	/**
	 * Loads a {VisualEditorOverlay} and replaces the existing SourceEditorOverlay with it
	 * based on the current option values.
	 *
	 * @private
	 * @param {string} [wikitext] Wikitext to pass to VE
	 */
	_switchToVisualEditor( wikitext ) {
		this.log( {
			action: 'abort',
			type: 'switchnochange',
			mechanism: 'navigate'
		} );
		this.logFeatureUse( {
			feature: 'editor-switch',
			action: 'visual-mobile'
		} );
 
		// Save a user setting indicating that this user prefers using the VisualEditor
		setPreferredEditor( 'VisualEditor' );
 
		this.$el.addClass( 'switching' );
		this.$el.find( '.overlay-header-container' ).hide();
		this.$el.append( this.fakeToolbar() );
		this.$content.prop( 'readonly', true );
 
		mw.loader.using( 'ext.visualEditor.targetLoader' ).then( () => {
			mw.libs.ve.targetLoader.addPlugin( 'ext.visualEditor.mobileArticleTarget' );
			return mw.libs.ve.targetLoader.loadModules( 'visual' );
		} ).then(
			() => {
				const options = this.getOptionsForSwitch();
				options.SourceEditorOverlay = SourceEditorOverlay;
				if ( wikitext ) {
					options.dataPromise = mw.libs.ve.targetLoader.requestPageData( 'visual', mw.config.get( 'wgRelevantPageName' ), {
						section: options.sectionId,
						oldId: options.oldId || mw.config.get( 'wgRevisionId' ),
						targetName: 'mobile',
						modified: true,
						wikitext: wikitext
					} );
				} else {
					delete options.dataPromise;
				}
 
				const newOverlay = new VisualEditorOverlay( options );
				newOverlay.getLoadingPromise().then( () => {
					this.switching = true;
					this.overlayManager.replaceCurrent( newOverlay );
					this.switching = false;
				} );
			},
			() => {
				this.$el.removeClass( 'switching' );
				this.$el.find( '.overlay-header-container' ).show();
				this.$el.find( '.ve-mobile-fakeToolbar-container' ).remove();
				this.$content.prop( 'readonly', false );
				// FIXME: We should show an error notification, but right now toast
				// notifications are not dismissible when shown within the editor.
			}
		);
	}
 
	/**
	 * Get the current edit summary.
	 *
	 * @return {string}
	 */
	getEditSummary() {
		return this.summaryTextArea.getValue();
	}
 
	/**
	 * Executed when the editor clicks the save/publish button. Handles logging and submitting
	 * the save action to the editor API.
	 *
	 * @inheritdoc
	 */
	onSaveBegin() {
		const options = {
			summary: this.getEditSummary()
		};
 
		if ( this.sectionLine !== '' ) {
			options.summary = '/* ' + this.sectionLine + ' */' + options.summary;
		}
		super.onSaveBegin();
		if ( this.confirmAborted ) {
			return;
		}
		if ( this.captchaId ) {
			options.captchaId = this.captchaId;
			options.captchaWord = this.$el.find( '.captcha-word' ).val();
		}
 
		this.showHidden( '.saving-header' );
 
		this.gateway.save( options )
			.then( ( newRevId, redirectUrl, tempUserCreated ) => {
				const title = this.options.title;
				// Special case behaviour of main page
				if ( mw.config.get( 'wgIsMainPage' ) && !redirectUrl ) {
					// FIXME: Blocked on T189173
					// eslint-disable-next-line no-restricted-properties
					window.location = mw.util.getUrl( title );
					return;
				}
 
				this.onSaveComplete( newRevId, redirectUrl, tempUserCreated );
 
				if ( redirectUrl && tempUserCreated ) {
					// eslint-disable-next-line no-restricted-properties
					window.location.href = redirectUrl;
				}
			}, ( data ) => {
				this.onSaveFailure( data );
			} );
	}
 
	/**
	 * @inheritdoc
	 * @param {number|null} newRevId ID of the newly created revision, or null if it was a
	 * null edit.
	 * @param {string} [redirectUrl] URL to redirect to, if different than the current URL.
	 * @param {boolean} [tempUserCreated] Whether a temporary user was created
	 */
	onSaveComplete( newRevId, redirectUrl, tempUserCreated ) {
		super.onSaveComplete( newRevId, redirectUrl, tempUserCreated );
 
		// The parent class changes the location hash in a setTimeout, so wait
		// for that to happen before reloading.
		setTimeout( function () {
			if ( redirectUrl ) {
				// eslint-disable-next-line no-restricted-properties
				window.location.href = redirectUrl;
			} else if ( newRevId ) {
				// Set a notify parameter similar to venotify in VisualEditor.
				const url = new URL( location.href );
				url.searchParams.set( 'mfnotify', this.isNewPage ? 'created' : 'saved' );
				// eslint-disable-next-line no-restricted-properties
				window.location.search = url.search;
			} else {
				// Null edit; do not add notify parameter.
				// Note the "#" may be in the URL.
				// If so, using window.location alone will not reload the page
				// we need to forcefully refresh
				// eslint-disable-next-line no-restricted-properties
				window.location.reload();
			}
		} );
	}
 
	/**
	 * @inheritdoc
	 */
	showSaveCompleteMsg( action, tempUserCreated ) {
		require( 'mediawiki.action.view.postEdit' ).fireHookOnPageReload( action, tempUserCreated );
	}
 
	/**
	 * Executed when page save fails. Handles error display and bookkeeping,
	 * passes logging duties to the parent.
	 *
	 * @inheritdoc
	 */
	onSaveFailure( data ) {
		let msg, noRetry;
 
		if ( data.edit && data.edit.captcha ) {
			this.captchaId = data.edit.captcha.id;
			this.handleCaptcha( data.edit.captcha );
		} else {
			msg = saveFailureMessage( data );
			this.reportError( msg );
			this.showHidden( '.save-header, .save-panel' );
 
			// Some errors may be temporary, but for others we know for sure that the save will
			// never succeed, so don't confuse the user by giving them the option to retry.
			noRetry = data.errors && data.errors.some( ( error ) => error.code === 'abusefilter-disallowed' );
 
			if ( noRetry ) {
				// disable continue and save buttons, reenabled when user changes content
				this.$el.find( '.continue, .submit' ).prop( 'disabled', true );
			}
		}
 
		super.onSaveFailure( data );
	}
 
	/**
	 * Checks whether the existing content has changed.
	 *
	 * @return {boolean}
	 */
	hasChanged() {
		return this.gateway.hasChanged;
	}
}
 
module.exports = SourceEditorOverlay;