From 7739b1c79bee4f7adc5b24049ff61e0e1184b312 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Mon, 15 Apr 2019 23:39:04 +0100 Subject: [PATCH] Fix switching without changes * Remove 'discardChanges' from switchToWikitext. This was intended to discard changes even when the document was modified, but it is no longer used as we always keep changes if we can. * Remove 'leaveVE' param, it was only used once and has been replaced with a direct call to switchToFallbackWikitextEditor. * Don't reset 'section' if there are no changes. Bug: T221981 Change-Id: Ia39345da44d203ba67ae331917c8d5ece7d42ef7 --- .../ve.init.mw.DesktopArticleTarget.js | 16 +++++----- .../targets/ve.init.mw.MobileArticleTarget.js | 4 +-- .../ve-mw/init/ve.init.mw.ArticleTarget.js | 29 ++++++++----------- .../ve.ui.MWWikitextSwitchConfirmDialog.js | 2 +- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js index ec6f1d6843..8718bb5621 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js +++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js @@ -321,7 +321,7 @@ ve.init.mw.DesktopArticleTarget.prototype.loadSuccess = function () { windowManager.destroy(); if ( data && data.action === 'prefer-wt' ) { - target.switchToWikitextEditor( true, false ); + target.switchToWikitextEditor( false ); } else if ( data && data.action === 'multi-tab' ) { location.reload(); } @@ -715,7 +715,7 @@ ve.init.mw.DesktopArticleTarget.prototype.loadFail = function ( code, errorDetai } else { // TODO: Some sort of progress bar? target.wikitextFallbackLoading = true; - target.switchToWikitextEditor( true, false ); + target.switchToWikitextEditor( false ); } } ); } else { @@ -1512,7 +1512,7 @@ ve.init.mw.DesktopArticleTarget.prototype.onUnload = function () { /** * @inheritdoc */ -ve.init.mw.DesktopArticleTarget.prototype.switchToFallbackWikitextEditor = function ( discardChanges, modified ) { +ve.init.mw.DesktopArticleTarget.prototype.switchToFallbackWikitextEditor = function ( modified ) { var uri, oldId, prefPromise, target = this; @@ -1522,16 +1522,14 @@ ve.init.mw.DesktopArticleTarget.prototype.switchToFallbackWikitextEditor = funct oldId = mw.config.get( 'wgRevisionId' ) || $( 'input[name=parentRevId]' ).val(); prefPromise = mw.libs.ve.setEditorPreference( 'wikitext' ); - if ( discardChanges ) { - if ( modified ) { - ve.track( 'mwedit.abort', { type: 'switchwithout', mechanism: 'navigate', mode: 'visual' } ); - } else { - ve.track( 'mwedit.abort', { type: 'switchnochange', mechanism: 'navigate', mode: 'visual' } ); - } + if ( !modified ) { + ve.track( 'mwedit.abort', { type: 'switchnochange', mechanism: 'navigate', mode: 'visual' } ); this.submitting = true; prefPromise.done( function () { uri = target.viewUri.clone().extend( { action: 'edit', + // No changes, safe to stay in section mode + section: target.section, veswitched: 1 } ); if ( oldId ) { diff --git a/modules/ve-mw/init/targets/ve.init.mw.MobileArticleTarget.js b/modules/ve-mw/init/targets/ve.init.mw.MobileArticleTarget.js index 82ae56e4e3..fa1a2cbd81 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.MobileArticleTarget.js +++ b/modules/ve-mw/init/targets/ve.init.mw.MobileArticleTarget.js @@ -379,13 +379,13 @@ ve.init.mw.MobileArticleTarget.prototype.loadFail = function ( key, text ) { ve.init.mw.MobileArticleTarget.prototype.editSource = function () { var modified = this.fromEditedState || this.getSurface().getModel().hasBeenModified(); - this.switchToWikitextEditor( false, modified ); + this.switchToWikitextEditor( modified ); }; /** * @inheritdoc */ -ve.init.mw.MobileArticleTarget.prototype.switchToWikitextEditor = function ( discardChanges, modified ) { +ve.init.mw.MobileArticleTarget.prototype.switchToWikitextEditor = function ( modified ) { var dataPromise; if ( modified ) { dataPromise = this.getWikitextDataPromiseForDoc( modified ).then( function ( response ) { diff --git a/modules/ve-mw/init/ve.init.mw.ArticleTarget.js b/modules/ve-mw/init/ve.init.mw.ArticleTarget.js index d6890a7798..5ad3d6cb50 100644 --- a/modules/ve-mw/init/ve.init.mw.ArticleTarget.js +++ b/modules/ve-mw/init/ve.init.mw.ArticleTarget.js @@ -1188,10 +1188,8 @@ ve.init.mw.ArticleTarget.prototype.clearState = function () { ve.init.mw.ArticleTarget.prototype.editSource = function () { var modified = this.fromEditedState || this.getSurface().getModel().hasBeenModified(); - if ( ve.init.target.isModeAvailable( 'source' ) ) { - this.switchToWikitextEditor( false, modified ); - } else if ( !modified ) { - this.switchToWikitextEditor( true, modified ); + if ( ve.init.target.isModeAvailable( 'source' ) || !modified ) { + this.switchToWikitextEditor( modified ); } else { ve.ui.actionFactory.create( 'window', this.getSurface() ) .open( 'wikitextswitchconfirm', { target: this } ); @@ -2267,13 +2265,11 @@ ve.init.mw.ArticleTarget.prototype.maybeShowWelcomeDialog = function () { .closed.then( function ( data ) { target.welcomeDialogPromise.resolve(); target.welcomeDialog = null; - // switchToWikitextEditor and switchToVisualEditor are actually - // only defined in subclasses :/ if ( data && data.action === 'switch-wte' ) { // TODO: Make this work on mobile - right now we can only // get away with it because the button which triggers this // action is hidden on mobile - target.switchToWikitextEditor( true, true ); + target.switchToWikitextEditor( false ); } else if ( data && data.action === 'switch-ve' ) { target.switchToVisualEditor(); } @@ -2302,20 +2298,20 @@ ve.init.mw.ArticleTarget.prototype.maybeShowWelcomeDialog = function () { /** * Switches to the wikitext editor, either keeping (default) or discarding changes. * - * @param {boolean} [discardChanges] Whether to discard changes or not. * @param {boolean} [modified] Whether there were any changes at all. - * @param {boolean} [leaveVE] Leave VE, even if source mode is available */ -ve.init.mw.ArticleTarget.prototype.switchToWikitextEditor = function ( discardChanges, modified, leaveVE ) { +ve.init.mw.ArticleTarget.prototype.switchToWikitextEditor = function ( modified ) { var dataPromise, target = this; - // When switching we always pass the full page as changes in visual section mode + // When switching with changes we always pass the full page as changes in visual section mode // can still affect the whole document (e.g. removing a reference) - this.section = null; + if ( modified ) { + this.section = null; + } - if ( ve.init.target.isModeAvailable( 'source' ) && !leaveVE ) { - if ( discardChanges ) { + if ( ve.init.target.isModeAvailable( 'source' ) ) { + if ( !modified ) { dataPromise = mw.libs.ve.targetLoader.requestPageData( 'source', this.getPageName(), { sessionStore: true, section: this.section, @@ -2325,7 +2321,7 @@ ve.init.mw.ArticleTarget.prototype.switchToWikitextEditor = function ( discardCh function ( response ) { return response; }, function () { // TODO: Some sort of progress bar? - target.switchToWikitextEditor( discardChanges, modified, true ); + target.switchToFallbackWikitextEditor( modified ); // Keep everything else waiting so our error handler can do its business return $.Deferred().promise(); } @@ -2335,7 +2331,7 @@ ve.init.mw.ArticleTarget.prototype.switchToWikitextEditor = function ( discardCh } this.reloadSurface( 'source', dataPromise ); } else { - this.switchToFallbackWikitextEditor( discardChanges, modified ); + this.switchToFallbackWikitextEditor( modified ); } }; @@ -2366,7 +2362,6 @@ ve.init.mw.ArticleTarget.prototype.getWikitextDataPromiseForDoc = function ( mod /** * Switches to the fallback wikitext editor, either keeping (default) or discarding changes. * - * @param {boolean} [discardChanges] Whether to discard changes or not. * @param {boolean} [modified] Whether there were any changes at all. */ ve.init.mw.ArticleTarget.prototype.switchToFallbackWikitextEditor = function () { diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWWikitextSwitchConfirmDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWWikitextSwitchConfirmDialog.js index 0d00da3f45..9f05e2348e 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWWikitextSwitchConfirmDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWWikitextSwitchConfirmDialog.js @@ -56,7 +56,7 @@ ve.ui.MWWikitextSwitchConfirmDialog.prototype.getActionProcess = function ( acti return new OO.ui.Process( function () { this.getActions().setAbilities( { cancel: false } ); this.getActions().get()[ 1 ].pushPending(); - this.target.switchToWikitextEditor( false, true ); + this.target.switchToWikitextEditor( true ); }, this ); } else if ( action === 'cancel' ) { return new OO.ui.Process( function () {