MediaWiki:Gadget-veHotkeys.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/*
* VisualEditor keyboard shortcuts in WikiEditor
* Script aims for feature parity, as long as it improves usability.
* List for comparison:
* https://www.mediawiki.org/wiki/VisualEditor/Portal/Keyboard_shortcuts
*/
$( function() {
// List of shortcuts
var ctrlShortcuts = {
'bold': {
key: 'B',
fn: function( e ) {
return ( e.which === 66 );
}
},
'italic': {
key: 'I',
fn: function( e ) {
return ( e.which === 73 );
}
},
'replace': {
key: 'F',
fn: function( e ) {
return ( e.which === 70 );
}
},
'reference': {
key: 'Shift+K',
fn: function( e ) {
return ( e.shiftKey && e.which === 75 );
}
},
'link': {
key: 'K',
fn: function( e ) {
return ( !e.shiftKey && e.which === 75 );
}
},
'nowiki': {
key: '\\',
fn: function( e ) {
return ( e.key === '\\' );
}
},
'superscript': {
key: '.',
fn: function( e ) {
return ( e.key === '.' );
}
},
'subscript': {
key: ',',
fn: function( e ) {
return ( e.key === ',' );
}
},
'heading-2': {
key: '2',
fn: function( e ) {
return ( e.which === 50 );
}
},
'heading-3': {
key: '3',
fn: function( e ) {
return ( e.which === 51 );
}
},
'heading-4': {
key: '4',
fn: function( e ) {
return ( e.which === 52 );
}
},
'heading-5': {
key: '5',
fn: function( e ) {
return ( e.which === 53 );
}
}
};
// Dependencies
var deps = [
'ext.wikiEditor',
'jquery.client',
'jquery.textSelection'
];
// Current platform
var platform;
// Test if shortcut is triggered
function testShortcut( e ) {
if ( platform === 'mac' && !e.metaKey ) {
return false;
}
if ( platform !== 'mac' && !e.ctrlKey ) {
return false;
}
var result = false;
for ( var tool in ctrlShortcuts ) {
var isCorrect = ctrlShortcuts[ tool ].fn( e );
if ( isCorrect ) {
result = tool;
break;
}
}
return result;
}
// Get correct toolbar button
function getButton( name ) {
var $button = $( '.tool[rel="' + name + '"] a' );
if ( $button.length ) {
return $button;
}
$button = $( '.option[rel="' + name + '"]' );
if ( $button.length ) {
return $button;
}
return false;
}
// Insert on keydown
function insertOnKeydown( e ) {
var shortcut = testShortcut( e, platform );
if ( shortcut ) {
e.preventDefault();
var $button = getButton( shortcut );
if ( $button.length ) {
$button[ 0 ].click();
}
}
}
// Initialise the shortcuts
function init( editor ) {
platform = $.client.profile().platform;
var $textbox = ( editor ? $( editor ) : $( '#wpTextbox1' ) );
if ( !$textbox.length ) {
return;
}
// Support Ctrl shortcuts
$textbox.on( 'keydown', insertOnKeydown );
// Inform about shortcuts
for ( var tool in ctrlShortcuts ) {
var $button = getButton( tool );
if ( !$button.length ) {
return;
}
// Get shortcut text
var shortcut = ctrlShortcuts[ tool ].key;
shortcut = ( platform === 'mac' ? 'Cmd+' : 'Ctrl+' ) + shortcut;
// Set shortcut as tooltip
var title = $button.attr( 'title' );
title = ( title ? title : $button.text() );
$button.attr( 'title', title + ' [' + shortcut + ']' );
}
}
// Wait for dependencies and initialise
$.when(
mw.loader.using( deps ),
$.ready
).then( function() {
// Do not transfer promise to this function
init();
} );
// [[User:JWBTH/CD]] support
mw.hook( 'convenientDiscussions.commentFormCreated' ).add( function ( commentForm ) {
mw.loader.using( deps );
var editor = commentForm.commentInput.$input;
editor.on( 'wikiEditor-toolbar-doneInitialSections', function() {
init( editor );
} );
} );
} );