From 8a85f5fb057c9aa903741a8fc5c06e5e450cd306 Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Tue, 17 Sep 2024 10:24:05 +0200 Subject: [PATCH] [refactor] Moving the SetExtendsDialog class from Citoid First step in streamlining the code to create sub-references. Slightly renaming the class to fit the schema and avoid conflicts. Bug: T373849 Change-Id: Iadbdbecbedf4d4aa3f0e0bade88ea020f2570926 --- extension.json | 4 +- .../ve.ui.MWSetExtendsContentDialog.js | 155 ++++++++++++++++++ .../ve.ui.MWSetExtendsContentDialog.less | 11 ++ 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 modules/ve-cite/ve.ui.MWSetExtendsContentDialog.js create mode 100644 modules/ve-cite/ve.ui.MWSetExtendsContentDialog.less diff --git a/extension.json b/extension.json index 5acc10517..a94a00aed 100644 --- a/extension.json +++ b/extension.json @@ -121,6 +121,7 @@ "ve.ui.MWCitationAction.js", "ve.ui.MWReference.init.js", "ve.ui.MWCitationNeededContextItem.js", + "ve.ui.MWSetExtendsContentDialog.js", { "name": "ve.ui.contentLanguage.js", "callback": "Cite\\ResourceLoader\\ContentLanguage::makeScript" @@ -131,7 +132,8 @@ "ve.ui.MWReferenceContextItem.less", "ve.ui.MWReferenceResultWidget.less", "ve.ui.MWReferenceSearchWidget.less", - "ve.ui.MWCitationDialogTool.less" + "ve.ui.MWCitationDialogTool.less", + "ve.ui.MWSetExtendsContentDialog.less" ], "dependencies": [ "oojs-ui.styles.icons-alerts", diff --git a/modules/ve-cite/ve.ui.MWSetExtendsContentDialog.js b/modules/ve-cite/ve.ui.MWSetExtendsContentDialog.js new file mode 100644 index 000000000..f60c4664e --- /dev/null +++ b/modules/ve-cite/ve.ui.MWSetExtendsContentDialog.js @@ -0,0 +1,155 @@ +'use strict'; + +/*! + * VisualEditor UserInterface MWSetExtendsContentDialog class. + * + * @copyright 2024 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt + * @license MIT + */ + +/** + * Dialog for inserting a sub-reference from a main reference + * + * @class + * @extends OO.ui.ProcessDialog + * + * @constructor + * @param {Object} [config] Configuration options + */ +ve.ui.MWSetExtendsContentDialog = function VeUiMWSetExtendsContentDialog( config ) { + // Parent constructor + ve.ui.MWSetExtendsContentDialog.super.call( this, config ); +}; + +/* Inheritance */ + +OO.inheritClass( ve.ui.MWSetExtendsContentDialog, OO.ui.ProcessDialog ); + +/* Static Properties */ + +ve.ui.MWSetExtendsContentDialog.static.name = 'setExtendsContent'; + +ve.ui.MWSetExtendsContentDialog.static.size = 'medium'; + +// TODO extends i18n +ve.ui.MWSetExtendsContentDialog.static.title = 'Extend a reference'; + +ve.ui.MWSetExtendsContentDialog.static.actions = [ + { + action: 'insert', + label: OO.ui.deferMsg( 'visualeditor-dialog-action-insert' ), + flags: [ 'progressive', 'primary' ], + modes: 'insert' + }, + { + label: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ), + flags: [ 'safe', 'close' ], + modes: [ 'insert' ] + } +]; + +/* Methods */ + +/** + * @inheritdoc + */ +ve.ui.MWSetExtendsContentDialog.prototype.initialize = function () { + // Parent method + ve.ui.MWSetExtendsContentDialog.super.prototype.initialize.apply( this, arguments ); + + this.editPanel = new OO.ui.PanelLayout( { + scrollable: true, padded: true + } ); + + // Icon message widget + this.extendsWarning = new OO.ui.MessageWidget( { + icon: 'alert', + inline: true, + classes: [ 've-ui-setExtendsContentDialog-warning' ] + } ); + + this.referenceTarget = ve.init.target.createTargetWidget( + { + includeCommands: null, + excludeCommands: ve.ui.MWReferenceEditPanel.static.getExcludeCommands(), + importRules: ve.ui.MWReferenceEditPanel.static.getImportRules(), + inDialog: this.constructor.static.name, + // TODO extends i18n + placeholder: 'Write or paste the content for the extension here' + } + ); + + this.contentFieldset = new OO.ui.FieldsetLayout(); + this.contentFieldset.$element.append( this.referenceTarget.$element ); + + this.editPanel.$element.append( this.extendsWarning.$element, this.contentFieldset.$element ); + + this.$body.append( this.editPanel.$element ); +}; + +/** + * @inheritdoc + */ +ve.ui.MWSetExtendsContentDialog.prototype.getSetupProcess = function ( data ) { + return ve.ui.MWSetExtendsContentDialog.super.prototype.getSetupProcess.call( this, data ) + .next( () => { + this.originalRef = data.originalRef; + this.newRef = data.newRef; + + const parentItemNode = data.internalList.getItemNode( this.originalRef.getListIndex() ); + const $parentRefPreview = new ve.ui.MWPreviewElement( parentItemNode, { useView: true } ).$element; + this.extendsWarning.setLabel( + $( '

' ) + .text( mw.msg( 'cite-ve-dialog-reference-editing-extends' ) ) + .append( $parentRefPreview ) + ); + + this.referenceTarget.setDocument( this.newRef.getDocument() ); + } ); +}; + +/** + * @override + */ +ve.ui.MWSetExtendsContentDialog.prototype.getActionProcess = function ( action ) { + if ( action === 'insert' ) { + return new OO.ui.Process( () => { + this.close( { action: action } ); + } ); + } + return ve.ui.MWSetExtendsContentDialog.super.prototype.getActionProcess.call( this, action ); +}; + +/** + * @inheritdoc + */ +ve.ui.MWSetExtendsContentDialog.prototype.getReadyProcess = function ( data ) { + return ve.ui.MWSetExtendsContentDialog.super.prototype.getReadyProcess.call( this, data ) + .next( () => { + this.referenceTarget.focus(); + } ); +}; + +/** + * @inheritdoc + */ +ve.ui.MWSetExtendsContentDialog.prototype.getTeardownProcess = function ( data ) { + return ve.ui.MWSetExtendsContentDialog.super.prototype.getTeardownProcess.call( this, data ) + .next( () => { + if ( data && data.action && data.action === 'insert' ) { + this.newRef.setDocument( this.referenceTarget.getContent() ); + } + this.referenceTarget.clear(); + } ); +}; + +/** + * @inheritdoc + */ +ve.ui.MWSetExtendsContentDialog.prototype.getBodyHeight = function () { + return 250; +}; + +/* Registration */ + +ve.ui.windowFactory.register( ve.ui.MWSetExtendsContentDialog ); diff --git a/modules/ve-cite/ve.ui.MWSetExtendsContentDialog.less b/modules/ve-cite/ve.ui.MWSetExtendsContentDialog.less new file mode 100644 index 000000000..a81463bd6 --- /dev/null +++ b/modules/ve-cite/ve.ui.MWSetExtendsContentDialog.less @@ -0,0 +1,11 @@ +/*! + * VisualEditor UserInterface MWSetExtendsContentDialog styles. + * + * @copyright 2024 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt + * @license MIT + */ + +.ve-ui-setExtendsContentDialog-warning { + font-weight: normal; + margin-bottom: 1em; +}