Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event handler for clicks on parserfunction hightlight class #16

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading