MediaWiki:Gadget-KeyShortcuts.js

From Wikidata
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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* <nowiki>
 *
 * KeyShortcuts.js
 * @author [[User:Ricordisamoa]]
 * provides some useful keyboard shortcuts on the Wikidata GUI
 *
 * based on [[Wikidata talk:Tools#Keyboard shortcuts|this suggestion]] by [[User:Jon Harald Søby]]
*/
$( function () {
	'use strict';
	if ( !mw.config.exists( 'wbEntityId' ) ) {
		return;
	}
	var addStatement = function () {
		$( '.wikibase-toolbar-button-add' ).last().find( 'a' ).first().click();
	},
	editTerms = function () {
		$( '.wikibase-entitytermsview .wikibase-toolbar-button-edit a' ).first().click();
	},
	editDescription = function () {
		$( '.wikibase-entitytermsforlanguagelistview' ).one( 'entitytermsforlanguagelistviewafterstartediting', function () {
			$( '.wikibase-labelview-text textarea' ).one( 'focus', function () {
				$( this ).closest( '.wikibase-entitytermsforlanguageview' )
				.find( '.wikibase-descriptionview-text textarea' ).first().focus();
			} );
		} );
		editTerms();
	},
	scrollHelper = function ( $el ) {
		if ( $el.length > 0 ) {
			$( 'html, body' ).animate( {
				scrollTop: $el.first().offset().top
			}, 400 );
		}
	},
	scrollToStatements = function () {
		scrollHelper( $( '#claims' ) );
	},
	scrollToIdentifiers = function () {
		scrollHelper( $( '#identifiers' ) );
	},
	scrollToSitelinks = function () {
		scrollHelper( $( '.wikibase-sitelinkgrouplistview' ) );
	};
	$( this )
	.keydown( function ( event ) {
		if (
			!event.ctrlKey &&
			!event.altKey &&
			!event.shiftKey &&
			!event.metaKey &&
			( $( ':focus' ).length === 0 || $( 'a:focus' ).length )
		) {
			switch ( event.keyCode ) {
				case 65: addStatement(); return false; // A
				case 68: editDescription(); return false; // D
				case 73: scrollToSitelinks(); return false; // I
				case 74: scrollToIdentifiers(); return false; // J
				case 76: editTerms(); return false; // L
				case 83: scrollToStatements(); return false; // S
			}
		}
	} );
} );