Utente:Ruthven/AccessKeys.js

'A Wikisource.

Notarella: Aroppo pubbreca putisse necessità 'e pulezzà 'a caché d' 'o navigatóre pe vedé 'e cagnamienti.

  • Firefox / Safari: Sprémme 'o buttóne maiuscole e ffà clic ncopp'a Recarreca, o pure spremme Ctrl-F5 o Ctrl-R (⌘-R ncopp'a Mac)
  • Google Chrome: spremme Ctrl-Shift-R (⌘-Shift-R ncopp'a nu Mac)
  • Internet Explorer/edge: Spremme 'o buttóne Ctrl pe' tramente ca faie click ncopp'a Refresh, o pure spremmere Ctrl-F5
  • Opera: Vaje addò 'o Menu → Mpustaziune (Opera → Mpustaziune ncopp' 'o Mac) e po' ncopp'a Privacy & sicurezza → Pulezza date d' 'o browser → Immaggene e file d' 'a cache.
// ==UserScript==
// @name        Wikisource AccessKeys
// @description Wikisource hotkeys in editing mode
//              Italic: ctrl + i
//              Bold: ctrl + b
//              {{Sc}}: ctrl + y
//              ’: ctrl + '
//              <ref>: alt  + z
//              {{v}}: alt + v
//              [[...]]: alt + k
//              {{gap}}: alt + g
// @include     http://*.wikisource.org/*
// @author      Ruthven
// @date		2023
// @copyright   CC0
// @namespace   Page
// ==/UserScript==

var textarea = document.getElementById("wpTextbox1");

// Make sure the utilities module is loaded (will only load if not already)
mw.loader.using( [ 'mediawiki.util',  'mediawiki.api' ], function () {

    // Only for NS Paggena
    if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Page' ) {
        $( document ).ready( function () { 
        	
        	if (mw.config.get('skin') === 'vector-2022') {
        		mw.loader.load('//nap.wikisource.org/w/index.php?title=Utente:Ruthven/De-duplicateAccessKeys.js&action=raw&ctype=text/javascript','text/javascript')
        	} else {
			// Calls removeAccessKeys to remove MediaWiki accesskeys so to avoid overlap
				mw.loader.load('//en.wikipedia.org/w/index.php?title=MediaWiki:Gadget-removeAccessKeys.js&action=raw&ctype=text/javascript','text/javascript');
        	}
			var selectedText = ""
			// Listen for keyboard events
			textarea.addEventListener('keydown', function(event) {
				
			  // Check if the correct key combination has been pressed
			  if (event.ctrlKey && event.key === 'i') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var italicElement = document.createTextNode('\'\'' + selectedText + '\'\'');
			    display(selectedText, italicElement.textContent, 2);
			  } // i

			  if (event.ctrlKey && event.key === 'b') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var boldElement = document.createTextNode('\'\'\'' + selectedText + '\'\'\'');
			    display(selectedText, boldElement.textContent, 3);
			  } // b
			  
			  if (event.ctrlKey && event.key === 'y') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var scElement = document.createTextNode('{{Sc|' + selectedText + '}}');
			    display(selectedText, scElement.textContent, 5);
			  } // y
			  
			  if (event.ctrlKey && event.key === "'") {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var scElement = document.createTextNode('’');
			    display(selectedText, scElement.textContent, 1);
			  } // '

			  if (event.altKey && event.key === 'z') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var refElement = document.createTextNode('<ref>' + selectedText + '</ref>');
			    display(selectedText, refElement.textContent, 5);
			    textarea.setSelectionRange(textarea.selectionStart, textarea.selectionEnd); // I want it all included
			  } // z 
			  
			  if (event.altKey && event.key === 'v') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var refElement = document.createTextNode('{{v}}' + selectedText);
			    display(selectedText, refElement.textContent, 5);
			  } // v

			  if (event.altKey && event.key === 'g') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var refElement = document.createTextNode('{{gap}}' + selectedText);
			    display(selectedText, refElement.textContent, 7);
			  } // g
			  
			  if (event.altKey && event.key === 'k') {
			    // Get the selected text
			    selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			    // Create a new element to wrap the selected text in
			    var refElement = document.createTextNode('[[' + selectedText + ']]');
			    display(selectedText, refElement.textContent, 2);
			  } // k
			  
			}); // event listener
        } );
    } // ns verification
} );

/**
 * Displays a text.
 * 
 * @param (string) wtext Selected text.
 * @param {HTMLElement} container The HTML element where to put the content.
 * @param (text) textContent Content to display.
 * @param (int) offset Starting point of selected text.
 */
display = function(wtext, textContent, offset) {
// Replace the selected text with the new text node
    var startIndex = textarea.selectionStart;
    var endIndex = textarea.selectionEnd;
    textarea.value = textarea.value.substring(0, startIndex) + textContent + textarea.value.substring(endIndex);
    
    // Set the new selection range
    var newStartIndex = startIndex + offset;
    var newEndIndex = newStartIndex + wtext.length;
    textarea.setSelectionRange(newStartIndex, newEndIndex);
} // end display