From ae4691b0ae8ea78d7fced79dcf21f3a7d8c108db Mon Sep 17 00:00:00 2001 From: Moritz Bergenthal Date: Fri, 22 Nov 2024 10:23:11 +0100 Subject: [PATCH] Add event handler for clicks on parserfunction hightlight class Allows to alt-click on Module names to open them --- resources/scripts/codemirror.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/resources/scripts/codemirror.js b/resources/scripts/codemirror.js index a0190fc..8012bc7 100644 --- a/resources/scripts/codemirror.js +++ b/resources/scripts/codemirror.js @@ -372,6 +372,23 @@ if ( !String.prototype.includes ) { window.open( mw.config.get( 'wgScriptPath' ) + '/' + pagename ); } + function openModuleOnClick( /** @type { HTMLElement } **/ element ) { + let pagename = element.text(); + + /** @type { HTMLElement | undefined } */ + const parserfunction = element.parent().children().eq( element.index() - 2 ) + + if ( pagename.startsWith( 'module=' ) ) { + pagename = pagename.slice( 7 ); + } else if ( !parserfunction.hasClass( 'cm-mw-parserfunction-name' ) + || parserfunction.text() !== '#invoke' ) { + return; + } + + pagename = 'Module:' + pagename + window.open( mw.config.get( 'wgScriptPath' ) + '/' + pagename ); + } + $( '.CodeMirror' ).on( 'click', '.cm-mw-template-name', function( e ) { if ( e.altKey ) { openPageOnClick( 'cm-mw-template-name', $( this ) ); @@ -383,6 +400,12 @@ if ( !String.prototype.includes ) { openPageOnClick( 'cm-mw-link-pagename', $( this ) ); } } ); + + $( '.CodeMirror' ).on( 'click', '.cm-mw-parserfunction', function( e ) { + if ( e.altKey ) { + openModuleOnClick( $( this ) ); + } + } ); // Jump to correct line number if appropriate hash is given (`#mw-ce-l42`) const magicHashPrefix = '#mw-ce-l';