Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Created October 19, 2022 00:44
Show Gist options
  • Save jdlrobson/afbbd705e0ced7aa5c770ba11a3bad5a to your computer and use it in GitHub Desktop.
Save jdlrobson/afbbd705e0ced7aa5c770ba11a3bad5a to your computer and use it in GitHub Desktop.
diff --git a/extension.json b/extension.json
index a8563346..ecd8e5f0 100644
--- a/extension.json
+++ b/extension.json
@@ -164,6 +164,11 @@
}
},
"attributes": {
+ "Popups": {
+ "PluginModules": [
+ "ext.math.popup"
+ ]
+ },
"VisualEditor": {
"PluginModules": [
"ext.math.visualEditor"
@@ -199,10 +204,8 @@
"scripts": "ext.math.js"
},
"ext.math.popup": {
- "scripts": "ext.math.popup.js",
- "dependencies": [
- "ext.popups.main"
- ]
+ "es6": true,
+ "packageFiles": "ext.math.popup.js"
},
"mw.widgets.MathWbEntitySelector": {
"scripts": "mw.widgets.MathWbEntitySelector.js",
diff --git a/modules/ext.math.popup.js b/modules/ext.math.popup.js
index 63ce98fd..a84b2095 100644
--- a/modules/ext.math.popup.js
+++ b/modules/ext.math.popup.js
@@ -1,47 +1,51 @@
-( function () {
- 'use strict';
- var previewType = 'math';
- var api = new mw.Rest();
- var isValidId = function ( qid ) {
- return qid.match( /Q\d+/g ) === null;
- };
- var fetch = function ( qid ) {
- return api.get( '/math/v0/popup/html/' + qid, {}, {
- Accept: 'application/json; charset=utf-8',
- 'Accept-Language': mw.config.language
- } );
- };
- var fetchPreviewForTitle = function ( title, el ) {
- var deferred = $.Deferred();
- var qidstr = el.parentNode.parentNode.dataset.qid;
- if ( isValidId( qidstr ) ) {
- return deferred.reject();
+
+'use strict';
+var previewType = 'math';
+var api = new mw.Rest();
+var isValidId = function ( qid ) {
+ return qid.match( /Q\d+/g ) === null;
+};
+var fetch = function ( qid ) {
+ return api.get( '/math/v0/popup/html/' + qid, {}, {
+ Accept: 'application/json; charset=utf-8',
+ 'Accept-Language': mw.config.language
+ } );
+};
+var fetchPreviewForTitle = function ( title, el ) {
+ var deferred = $.Deferred();
+ var qidstr = el.parentNode.parentNode.dataset.qid;
+ if ( isValidId( qidstr ) ) {
+ return deferred.reject();
+ }
+ qidstr = qidstr.slice( 1 );
+ fetch( qidstr ).then( function ( body ) {
+ var model = {
+ title: body.title,
+ url: body.canonicalurl,
+ languageCode: body.pagelanguagehtmlcode,
+ languageDirection: body.pagelanguagedir,
+ extract: body.extract,
+ type: previewType,
+ thumbnail: undefined,
+ pageId: body.pageId
+ };
+ deferred.resolve( model );
+ } );
+ return deferred.promise();
+};
+// popups require title attributes
+document.querySelectorAll( '.mwe-math-element[data-qid] img' )
+ .forEach( function ( node ) {
+ if ( isValidId( node.parentNode.parentNode.dataset.qid ) ) {
+ node.dataset.title = 'math-unique-identifier';
}
- qidstr = qidstr.slice( 1 );
- fetch( qidstr ).then( function ( body ) {
- var model = {
- title: body.title,
- url: body.canonicalurl,
- languageCode: body.pagelanguagehtmlcode,
- languageDirection: body.pagelanguagedir,
- extract: body.extract,
- type: previewType,
- thumbnail: undefined,
- pageId: body.pageId
- };
- deferred.resolve( model );
- } );
- return deferred.promise();
- };
- // popups require title attributes
- document.querySelectorAll( '.mwe-math-element[data-qid] img' )
- .forEach( function ( node ) {
- if ( isValidId( node.parentNode.parentNode.dataset.qid ) ) {
- node.dataset.title = 'math-unique-identifier';
- }
- } );
- mw.popups.register( previewType, '.mwe-math-element[data-qid] img', {
- fetch: fetch,
- fetchPreviewForTitle: fetchPreviewForTitle
} );
-}() );
+
+module.exports = {
+ type: previewType,
+ selector: '.mwe-math-element[data-qid] img',
+ gateway: {
+ fetch,
+ fetchPreviewForTitle
+ }
+};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment