Jump to content

User:M-le-mot-dit/Shortcuts.js

From Wikisource

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.
// Shortcuts
//		Ctrl-i -> italics
//		Ctrl-b -> bold
//		Ctrl-q -> sc
//		Ctrl-alt-c -> {{Center|}}
//		Ctrl-alt-q -> uc
//		Ctrl-alt-r -> <ref/></ref>
//		Ctrl-alt-s -> {{SIC||}}
//		Ctrl-alt-w -> {{}}
//		Ctrl-alt-space -> {{Nowrap|}}

mw.loader.using('jquery.textSelection');

function raccItal() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (!selection.collapsed) {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "''", 'peri': str, 'post': "''"});
    }
}

function raccGras() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (!selection.collapsed) {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "'''", 'peri': str, 'post': "'''"});
    }
}

function raccSc() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (!selection.collapsed) {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{sc|", 'peri': str, 'post': "}}"});
    }
}

function raccUc() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (!selection.collapsed) {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{uc|", 'peri': str, 'post': "}}"});
    }
}

function raccModel() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (!selection.collapsed) {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{", 'peri': str, 'post': "}}"});
    }
}

function raccCorr() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
	var str = selection.toString();
	$('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{SIC|"+str+"|", 'peri': str, 'post': "|nodash}}", 'replace':true});
	if (str != '') {
	  var posCurseur = $('#wpTextbox1').textSelection('getCaretPosition');
	  $('#wpTextbox1').textSelection('setSelection', {'start':posCurseur - 2});
	}
}

function raccRef() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    var vn = "" ;
    if ( selection[0] == '+' ) { vn = " follow=\"p\"" }
    else if ( selection[0] == '-' ) { vn = " name=\"p\"" }
	$('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "<ref" + vn + ">", 'post': "</ref>"});
}

function raccInsec() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (!selection.collapsed) {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{Nowrap|", 'peri': str, 'post': "}}"});
    }
}

function raccCenter() {
    var selection = $('#wpTextbox1').textSelection('getSelection');
    if (selection != '') {
      var str = selection.toString();
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{Center|", 'peri': str, 'post': "}}"});
    }
    else {
      $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "{{Center|}}", 'post': ''});
    }
}

// Shortcuts (edition mode)
if (mw.config.get("wgAction") == "edit" || mw.config.get("wgAction") == "submit") { 
	document.addEventListener("keydown", 
	    function (event) { 
//		    if (event.key === "i" && event.ctrlKey && !event.altKey) { 
//		        event.preventDefault(); 
//		        raccItal(); 
//		    }
//		    if (event.key === "b" && event.ctrlKey && !event.altKey) { 
//		        event.preventDefault(); 
//		        raccGras(); 
//		    }
		    if (event.key === "q" && event.ctrlKey && !event.altKey) { 
		        event.preventDefault(); 
		        raccSc(); 
		    }
		    if (event.key === "q" && event.ctrlKey && event.altKey) { 
		        event.preventDefault(); 
		        raccUc(); 
		    }
		    if (event.key === "w" && event.altKey  && event.ctrlKey) { 
		        event.preventDefault(); 
		        raccModel(); 
		    }
		    if (event.key === "c" && event.altKey && event.ctrlKey) { 
		        event.preventDefault(); 
		        raccCenter(); 
		    }
		    if (event.key === "s" && event.altKey && event.ctrlKey) { 
		        event.preventDefault(); 
		        raccCorr(); 
		    }
		    if (event.key === "r" && event.altKey && event.ctrlKey) { 
		        event.preventDefault(); 
		        raccRef();
		    }
		    if (event.key === " " && event.ctrlKey) { 
		        event.preventDefault(); 
		        raccInsec(); 
		    }
		});
}