From b2c9b225bdb4dc691e5b315a60a4bc5ee744c707 Mon Sep 17 00:00:00 2001 From: Andrew Kostka Date: Fri, 7 May 2021 12:40:36 +0200 Subject: [PATCH] Add back button to the transclusion dialog Bug: T272354 Change-Id: I08c4a7db6239b485439bf547e3e8b4d6f7aeede8 --- extension.json | 4 ++ includes/VisualEditorHooks.php | 1 + .../dm/models/ve.dm.MWTransclusionModel.js | 10 ++++ .../ui/dialogs/ve.ui.MWTransclusionDialog.js | 60 +++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/extension.json b/extension.json index 53f3560a94..112604e3d3 100644 --- a/extension.json +++ b/extension.json @@ -182,6 +182,10 @@ "description": "Temporary flag to enable inline parameter descriptions in the transclusion dialog.", "value": false }, + "VisualEditorTransclusionDialogBackButton": { + "description": "Temporary flag to enable the back button in the transclusion dialog when inserting a new template. (T272354)", + "value": false + }, "VisualEditorTransclusionDialogNewSidebar": { "description": "Temporary flag to enable the new sidebar in the transclusion dialog.", "value": false diff --git a/includes/VisualEditorHooks.php b/includes/VisualEditorHooks.php index c6a8f78b64..58b60ee01c 100644 --- a/includes/VisualEditorHooks.php +++ b/includes/VisualEditorHooks.php @@ -1050,6 +1050,7 @@ class VisualEditorHooks { 'transclusionDialogSuggestedValues' => $veConfig->get( 'VisualEditorTransclusionDialogSuggestedValues' ), 'transclusionDialogInlineDescriptions' => $veConfig->get( 'VisualEditorTransclusionDialogInlineDescriptions' ), + 'transclusionDialogBackButton' => $veConfig->get( 'VisualEditorTransclusionDialogBackButton' ), 'transclusionDialogNewSidebar' => $veConfig->get( 'VisualEditorTransclusionDialogNewSidebar' ), 'cirrusSearchLookup' => $extensionRegistry->isLoaded( 'CirrusSearch' ) && $veConfig->get( 'VisualEditorTemplateSearchImprovements' ), diff --git a/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js b/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js index 9365e7cadc..e2a638e50f 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js @@ -568,4 +568,14 @@ } }; + /** + * Resets the model's state. + */ + ve.dm.MWTransclusionModel.prototype.reset = function () { + this.parts = []; + this.uid = 0; + this.requests = []; + this.queue = []; + }; + }() ); diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js index 1f4d8c4e93..345abca3d9 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWTransclusionDialog.js @@ -20,6 +20,8 @@ ve.ui.MWTransclusionDialog = function VeUiMWTransclusionDialog( config ) { // Properties this.mode = null; + this.closeButton = null; + this.backButton = null; // Temporary override while feature flag is in place. this.isBigger = mw.config.get( 'wgVisualEditorConfig' ).transclusionDialogInlineDescriptions; @@ -46,6 +48,12 @@ ve.ui.MWTransclusionDialog.static.actions = ve.ui.MWTemplateDialog.static.action // HACK: Will be set later, but we want measurements to be accurate in the mean time, this // will not be needed when T93290 is resolved label: $( document.createTextNode( '\u00a0' ) ) + }, + { + action: 'back', + label: OO.ui.deferMsg( 'visualeditor-dialog-action-goback' ), + modes: [ 'edit', 'insert' ], + flags: [ 'safe', 'back' ] } ] ); @@ -177,6 +185,7 @@ ve.ui.MWTransclusionDialog.prototype.onReplacePart = function ( removed, added ) single = this.isSingleTemplateTransclusion(); this.actions.setAbilities( { mode: single } ); + this.updateActionSet(); }; /** @@ -328,6 +337,12 @@ ve.ui.MWTransclusionDialog.prototype.addPart = function ( part ) { * @inheritdoc */ ve.ui.MWTransclusionDialog.prototype.getActionProcess = function ( action ) { + if ( action === 'back' ) { + return new OO.ui.Process( function () { + this.resetDialog(); + }, this ); + } + if ( action === 'mode' ) { return new OO.ui.Process( function () { this.setMode( this.mode === 'single' ? 'multiple' : 'single' ); @@ -337,6 +352,51 @@ ve.ui.MWTransclusionDialog.prototype.getActionProcess = function ( action ) { return ve.ui.MWTransclusionDialog.super.prototype.getActionProcess.call( this, action ); }; +/** + * Update the widgets in the dialog's action bar. + */ +ve.ui.MWTransclusionDialog.prototype.updateActionSet = function () { + var veConfig = mw.config.get( 'wgVisualEditorConfig' ), + backButton = this.actions.get( { flags: [ 'back' ] } ).pop(); + + if ( backButton ) { + // Todo: this won't be needed if https://gerrit.wikimedia.org/r/c/oojs/ui/+/686439 is resolved + this.backButton = backButton; + } + + if ( veConfig.transclusionDialogBackButton ) { + var closeButton = this.actions.get( { flags: [ 'close' ] } ).pop(), + parts = this.transclusionModel && this.transclusionModel.getParts(), + isInitialPage = parts && parts.length === 1 && parts[ 0 ] instanceof ve.dm.MWTemplatePlaceholderModel, + isInsertMode = this.getMode() === 'insert'; + + if ( closeButton ) { + // Todo: this won't be needed if https://gerrit.wikimedia.org/r/c/oojs/ui/+/686439 is resolved + this.closeButton = closeButton; + } + + this.closeButton.toggle( !isInsertMode || isInitialPage ); + this.backButton.toggle( isInsertMode && !isInitialPage ); + } else { + backButton.toggle( false ); + } +}; + +/** + * Revert the dialog back to its initial state. + */ +ve.ui.MWTransclusionDialog.prototype.resetDialog = function () { + var target = this; + this.transclusionModel.reset(); + this.bookletLayout.clearPages(); + this.transclusionModel + .addPart( new ve.dm.MWTemplatePlaceholderModel( this.transclusionModel ), 0 ) + .done( function () { + target.setMode( 'single' ); + target.updateModeActionState(); + } ); +}; + /** * @inheritdoc */