From d8061868103be70c40bd1415d2f884a78f6fcdb9 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Fri, 22 Nov 2024 10:34:06 -0600 Subject: [PATCH] Edit check: move config defaults to the client-side This lets us encapsulate everything about a check in its JS definition, which makes it much simpler for other extensions or gadgets to create checks. Change-Id: Ica23fc26a576d55addb001e4baf78b3702927208 --- editcheck/includes/ResourceLoaderData.php | 15 +-------------- editcheck/modules/BaseEditCheck.js | 9 ++++++++- .../modules/editchecks/AddReferenceEditCheck.js | 5 +++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/editcheck/includes/ResourceLoaderData.php b/editcheck/includes/ResourceLoaderData.php index 2cb80026f8..912502f341 100644 --- a/editcheck/includes/ResourceLoaderData.php +++ b/editcheck/includes/ResourceLoaderData.php @@ -13,19 +13,6 @@ use MessageLocalizer; class ResourceLoaderData { - private const DEFAULTS = [ - 'addReference' => [ - 'minimumCharacters' => 50, - 'beforePunctuation' => false, - // TODO: when we have multiple edit checks this will likely be a generic block: - // account: loggedin, loggedout, anything non-truthy means allow either - 'account' => false, - 'maximumEditcount' => 100, - 'ignoreSections' => [], - 'ignoreLeadSection' => false, - ], - ]; - /** * Return configuration data for edit checks, fetched from an on-wiki JSON message * @@ -35,6 +22,6 @@ class ResourceLoaderData { public static function getConfig( MessageLocalizer $context ): array { $raw_config = json_decode( $context->msg( 'editcheck-config.json' )->inContentLanguage()->plain(), true ); - return array_replace_recursive( self::DEFAULTS, $raw_config ?? [] ); + return $raw_config ?? []; } } diff --git a/editcheck/modules/BaseEditCheck.js b/editcheck/modules/BaseEditCheck.js index e5d8a81a51..84da82d225 100644 --- a/editcheck/modules/BaseEditCheck.js +++ b/editcheck/modules/BaseEditCheck.js @@ -1,5 +1,5 @@ mw.editcheck.BaseEditCheck = function MWBaseEditCheck( config ) { - this.config = config; + this.config = ve.extendObject( {}, this.constructor.static.defaultConfig, config ); }; OO.initClass( mw.editcheck.BaseEditCheck ); @@ -19,6 +19,13 @@ mw.editcheck.BaseEditCheck.static.choices = [ } ]; +mw.editcheck.BaseEditCheck.static.defaultConfig = { + account: false, // 'loggedin', 'loggedout', anything non-truthy means allow either + maximumEditcount: 100, + ignoreSections: [], + ignoreLeadSection: false +}; + mw.editcheck.BaseEditCheck.static.description = ve.msg( 'editcheck-dialog-addref-description' ); /** diff --git a/editcheck/modules/editchecks/AddReferenceEditCheck.js b/editcheck/modules/editchecks/AddReferenceEditCheck.js index db3ddece86..caf8828085 100644 --- a/editcheck/modules/editchecks/AddReferenceEditCheck.js +++ b/editcheck/modules/editchecks/AddReferenceEditCheck.js @@ -9,6 +9,11 @@ mw.editcheck.AddReferenceEditCheck.static.name = 'addReference'; mw.editcheck.AddReferenceEditCheck.static.description = ve.msg( 'editcheck-dialog-addref-description' ); +mw.editcheck.AddReferenceEditCheck.static.defaultConfig = ve.extendObject( {}, mw.editcheck.BaseEditCheck.static.defaultConfig, { + minimumCharacters: 50, + beforePunctuation: false +} ); + mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( surfaceModel ) { return this.findAddedContent( surfaceModel.getDocument() ).map( ( range ) => { const fragment = surfaceModel.getLinearFragment( range );