User:Lectrician1/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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 *
 * KeyShortcuts.js
 * @author [[User:Lectrician1]]
 * provides some useful keyboard shortcuts on the Wikidata GUI
 *
 * based on [[MediaWiki talk:Gadget-KeyShortcuts.js]] by [[User:Ricordisamoa]] and 
 * [[Wikidata talk:Tools#Keyboard shortcuts|this suggestion]] by [[User:Jon Harald Søby]]
*/
$( function () {
	'use strict';
	$( this )
	.keydown( function ( event ) {
		if (event.altKey) {
			switch ( event.keyCode ) {
				case 73: window.open('https://www.wikidata.org/wiki/Special:NewItem'); return false; // I
				case 76: window.open('https://www.wikidata.org/wiki/Special:NewLexeme'); return false; // L
				case 80: window.open('https://www.wikidata.org/wiki/Wikidata:Property_proposal'); return false; // P
				case 81: window.open('https://query.wikidata.org/'); return false; // Q
			}
		}
	});
	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
		) {
			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
			}
		}
	} );
} );