Skip to content

Commit

Permalink
Add event handler for clicks on parserfunction hightlight class
Browse files Browse the repository at this point in the history
Allows to alt-click on Module names to open them
  • Loading branch information
mbergen committed Nov 22, 2024
1 parent 0d35c2a commit ae4691b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions resources/scripts/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand All @@ -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';
Expand Down

0 comments on commit ae4691b

Please sign in to comment.