Skip to content

Commit

Permalink
Add event handler for clicks on parserfunction hightlight class (#16)
Browse files Browse the repository at this point in the history
* Add event handler for clicks on parserfunction hightlight class

Allows to alt-click on Module names to open them

* Apply eslint fixes
  • Loading branch information
mbergen authored Nov 25, 2024
1 parent 0d35c2a commit 264b5cb
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions resources/scripts/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ if ( !String.prototype.includes ) {
// case 'setContents': // no params with defaults
// case 'getSelection': // no params
case 'encapsulateSelection':
options = $.extend( {
options = Object.assign( {
// Text to insert before the cursor/selection
pre: '',
// Text to insert between pre and post and select afterwards
Expand All @@ -232,15 +232,15 @@ if ( !String.prototype.includes ) {
}, options );
break;
case 'getCaretPosition':
options = $.extend( {
options = Object.assign( {
// Return [start, end] instead of just start
startAndEnd: false
}, options );
// FIXME: We may not need character position-based functions
// if we insert markers in the right places
break;
case 'setSelection':
options = $.extend( {
options = Object.assign( {
// Position to start selection at
start: undefined,
// Position to end selection at. Defaults to start
Expand All @@ -261,7 +261,7 @@ if ( !String.prototype.includes ) {
// functions if we insert markers in the right places
break;
case 'scrollToCaretPosition':
options = $.extend( {
options = Object.assign( {
force: false // Force a scroll even if the caret position is already visible
}, options );
break;
Expand Down 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 @@ -384,6 +401,12 @@ if ( !String.prototype.includes ) {
}
} );

$( '.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';
function attemptLineChangeOnHash() {
Expand All @@ -400,12 +423,12 @@ if ( !String.prototype.includes ) {

attemptLineChangeOnHash();

window.addEventListener( 'hashchange', function() {
window.addEventListener( 'hashchange', () => {
attemptLineChangeOnHash();
} );

document.querySelectorAll( '.CodeMirror' ).forEach( function( el ) {
el.addEventListener( 'click', function( ev ) {
document.querySelectorAll( '.CodeMirror' ).forEach( ( el ) => {
el.addEventListener( 'click', ( ev ) => {
const targetElement = ev.target;
if ( targetElement.classList.contains( 'CodeMirror-linenumber' ) ) {
window.location.hash = magicHashPrefix + targetElement.innerHTML;
Expand All @@ -423,7 +446,7 @@ if ( !String.prototype.includes ) {
codeMirror = codeMirrorDesktop;
}
if ( codeMirror ) {
mw.hook( 'wikiEditor.toolbarReady' ).add( function() {
mw.hook( 'wikiEditor.toolbarReady' ).add( () => {
enableCodeMirror();

// define JQuery hook for searching and replacing text using
Expand Down

0 comments on commit 264b5cb

Please sign in to comment.