Merge "[refactor] Merge some ReferencePreviews code"

This commit is contained in:
jenkins-bot 2024-12-18 10:19:10 +00:00 committed by Gerrit Code Review
commit 175ad76a08
5 changed files with 47 additions and 51 deletions

View file

@ -1,4 +0,0 @@
module.exports = {
TYPE_REFERENCE: 'reference',
FETCH_DELAY_REFERENCE_TYPE: 150
};

View file

@ -2,7 +2,7 @@
* @module gateway/reference * @module gateway/reference
*/ */
const { TYPE_REFERENCE } = require( './constants.js' ); const TYPE_REFERENCE = 'reference';
/** /**
* @return {Gateway} * @return {Gateway}

View file

@ -1,15 +1,36 @@
const isReferencePreviewsEnabled = require( './isReferencePreviewsEnabled.js' );
const { initReferencePreviewsInstrumentation, LOGGING_SCHEMA } = require( './referencePreviewsInstrumentation.js' ); const { initReferencePreviewsInstrumentation, LOGGING_SCHEMA } = require( './referencePreviewsInstrumentation.js' );
const createReferenceGateway = require( './createReferenceGateway.js' ); const createReferenceGateway = require( './createReferenceGateway.js' );
const renderFn = require( './createReferencePreview.js' ); const createReferencePreview = require( './createReferencePreview.js' );
const { TYPE_REFERENCE, FETCH_DELAY_REFERENCE_TYPE } = require( './constants.js' ); const TYPE_REFERENCE = 'reference';
/**
* Given the global state of the application, creates a function that gets
* whether or not the user should have Reference Previews enabled.
*
* @param {mw.user} user The `mw.user` singleton instance
* @param {Function} isPreviewTypeEnabled check whether preview has been disabled or enabled.
* @param {mw.Map} config
*
* @return {boolean|null} Null when there is no way the popup type can be enabled at run-time.
* @memberof module:ext.cite.referencePreviews
*/
function isReferencePreviewsEnabled( user, isPreviewTypeEnabled, config ) {
if ( !config.get( 'wgCiteReferencePreviewsActive' ) ) {
return null;
}
if ( user.isAnon() ) {
return isPreviewTypeEnabled( TYPE_REFERENCE );
}
return true;
}
const referencePreviewsState = isReferencePreviewsEnabled( const referencePreviewsState = isReferencePreviewsEnabled(
mw.user, mw.user,
mw.popups.isEnabled, mw.popups.isEnabled,
mw.config mw.config
); );
const gateway = createReferenceGateway();
// For tracking baseline stats in the Cite extension https://phabricator.wikimedia.org/T353798 // For tracking baseline stats in the Cite extension https://phabricator.wikimedia.org/T353798
// FIXME: This might be obsolete when the code moves to the Cite extension and the tracking there // FIXME: This might be obsolete when the code moves to the Cite extension and the tracking there
@ -22,22 +43,32 @@ mw.trackSubscribe( 'Popups.SettingChange', ( data ) => {
} }
} ); } );
module.exports = referencePreviewsState !== null ? { /**
type: TYPE_REFERENCE, * Create the relevant config to register the preview type in the Popups extension.
selector: '#mw-content-text .reference a[ href*="#" ]', *
delay: FETCH_DELAY_REFERENCE_TYPE, * @see mw.popups.register()
gateway, * @memberof module:ext.cite.referencePreviews
renderFn, */
init: () => { function createReferencePreviewsType() {
initReferencePreviewsInstrumentation(); return {
} type: TYPE_REFERENCE,
} : null; selector: '#mw-content-text .reference a[ href*="#" ]',
delay: 150,
gateway: createReferenceGateway(),
renderFn: createReferencePreview,
init: () => {
initReferencePreviewsInstrumentation();
}
};
}
module.exports = referencePreviewsState !== null ? createReferencePreviewsType() : null;
// Expose private methods for QUnit tests // Expose private methods for QUnit tests
if ( typeof QUnit !== 'undefined' ) { if ( typeof QUnit !== 'undefined' ) {
module.exports = { private: { module.exports = { private: {
createReferenceGateway: require( './createReferenceGateway.js' ), createReferenceGateway: require( './createReferenceGateway.js' ),
createReferencePreview: require( './createReferencePreview.js' ), createReferencePreview: require( './createReferencePreview.js' ),
isReferencePreviewsEnabled: require( './isReferencePreviewsEnabled.js' ) isReferencePreviewsEnabled
} }; } };
} }

View file

@ -1,29 +0,0 @@
const { TYPE_REFERENCE } = require( './constants.js' );
/**
* @module isReferencePreviewsEnabled
*/
/**
* Given the global state of the application, creates a function that gets
* whether or not the user should have Reference Previews enabled.
*
* @param {mw.user} user The `mw.user` singleton instance
* @param {Function} isPreviewTypeEnabled check whether preview has been disabled or enabled.
* @param {mw.Map} config
*
* @return {boolean|null} Null when there is no way the popup type can be enabled at run-time.
*/
function isReferencePreviewsEnabled( user, isPreviewTypeEnabled, config ) {
if ( !config.get( 'wgCiteReferencePreviewsActive' ) ) {
return null;
}
if ( user.isAnon() ) {
return isPreviewTypeEnabled( TYPE_REFERENCE );
}
return true;
}
module.exports = isReferencePreviewsEnabled;

View file

@ -125,10 +125,8 @@ class CiteHooks implements
], ],
'packageFiles' => [ 'packageFiles' => [
'index.js', 'index.js',
'constants.js',
'createReferenceGateway.js', 'createReferenceGateway.js',
'createReferencePreview.js', 'createReferencePreview.js',
'isReferencePreviewsEnabled.js',
'referencePreviewsInstrumentation.js' 'referencePreviewsInstrumentation.js'
] ]
] ]