MediaWiki:Gadget-shortcuts.js

Z Wikiźródeł, wolnej biblioteki

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
/**
 * Skróty klawiszowe (shortcuts).
 * 
 * Skrypt umożliwia dodanie skrótów klawiszowych, które przyspieszają pracę z tekstami.
 * Więcej: [[Wikiźródła:Narzędzia/Skróty klawiszowe]]
 * 
 * Author: Beau, Nux.
 * License: CC-BY-SA or GFDL.
 * 
 * Dev version: [[MediaWiki:Gadget-shortcuts-dev.js]]
 * Main version: [[MediaWiki:Gadget-shortcuts.js]]
 */
// required libraries
// should add those as gadget dependency (when in gadget env.)
if ( ( typeof window.shortcut ) !== 'object' ) {
	//console.log('[shortcuts]', 'shortcut missing');
	// could probably use mw.loader.using... but also won't need that in a gadget
	mw.loader.getScript( 'https://pl.wikisource.org/wiki/MediaWiki:Gadget-lib-shortcuts.js?action=raw&ctype=text/javascript' )
	.then (function() {
		mw.hook('userjs.shortcutsGadget.libshortcuts').fire();
	});
} else {
	//console.log('[shortcuts]', 'shortcut lib already loaded');
	mw.hook('userjs.shortcutsGadget.libshortcuts').fire();
}
if ( ( typeof window.sel_t3 ) !== 'object' ) {
	//console.log('[shortcuts]', 'sel_t3 missing');
	mw.loader.load( 'https://pl.wikipedia.org/wiki/MediaWiki:Gadget-sel_t3.js?action=raw&ctype=text/javascript' );
}

window.shortcutsGadget = {
	version: '1.1.1',
	pageLoaded: false,
	queue: [],

	addTextShortcut: function( keystroke, tagOpen, tagClose, sampleText ) {
		var callback = function() {
			//console.log('[shortcuts]', 'insert', tagOpen, tagClose, sampleText);
			var inserted = sel_t3.insertText(tagOpen, tagClose, sampleText)
			return false;
		};

		var item = {
			keystroke: keystroke,
			callback: callback
		};

		if ( this.pageLoaded ) {
			this.addTextShortcutNow( item );
		} else {
			this.queue.push( item );
		}
	},

	addTextShortcutNow: function( item ) {
		shortcut.add( item.keystroke, item.callback, {
			target: document
		} );
	},

	removeAll: function() {
		this.queue = [];
	},

	bindShortcuts: function() {
		for ( var i = 0; i < this.queue.length; i++ ) {
			this.addTextShortcutNow( this.queue[i] );
		}
		this.removeAll();
	},

	init: function() {
		this.pageLoaded = true;
		this.bindShortcuts();
	}
};

// wait and init
$(function () {
	mw.hook('userjs.shortcutsGadget.libshortcuts').add(function(){
		mw.hook('userjs.sel_t3.ready').add(function (sel_t3) {
			mw.hook('userjs.shortcutsGadget.preinit').fire(shortcutsGadget);
			shortcutsGadget.init();
			mw.hook('userjs.shortcutsGadget.postinit').fire(shortcutsGadget);
		});
	});
});

// default shortcut(s)
shortcutsGadget.addTextShortcut( 'ctrl+.', 'é', '', '' );

// usage: mw.hook('userjs.shortcutsGadget.ready').add(function (shortcutsGadget) {...});
mw.hook('userjs.shortcutsGadget.ready').fire(shortcutsGadget);