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

/** * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor. * @see https://meta.wikimedia.org/wiki/TemplateScript * @update-token */ // /* CharInsert specific */ // window.charinsertDontMove = true; window.charinsertMoveTop = true; // window.editToolsRecall = true;

window.charinsertCustom = { "User": ' – — · • ′ ″ Ç Á É ĺ Ó Ú á í ó ú ō       ' };

$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function {	pathoschild.TemplateScript.add([ // Spaces {			name: 'Spaces', position: 'cursor', script: function(editor) { editor.replace(/ +$/mg, '') .replace(/ +/g, ' ') .replace(/( |\ +/g, '$1')				.replace(/ +(<\/ref>|\))/g, '$1') .replace(/ ([?!;:,])/g, '$1') .replace(/ *— */g, '—') .replace(/[-¬] *(\n)([^ \|\n]+)[ \n]/g, '$2$1') .replace(/([;:]+)—/g, '$1—') .replace(/ +/g, ' '); }			},

// straight quotes {			name: 'Quotes', position: 'cursor', script: function(editor) { editor.replace(/[«“] */g, '\"')				.replace(/ *[»”]/g, '\"') .replace(/[‘] */g, '\'') .replace(/[`’]/g, '\'') .replace(/&quot;/g, '\"')				.replace(/^(["']) /mg, '$1') .replace(/ (["']) *$/mg, '$1');			}			},

// replace _ or -- by — {			name: 'Bars', position: 'cursor', script: function(editor) { editor.replace(/ *___ */g, '') .replace(/ *(__|——) */g, '') .replace(/ *(_|--) */g, '—') .replace(/^——$/gm, ''); }			},

// Ellipses {			name: 'Ellipses', position: 'cursor', script: function(editor) { editor.replace(/( *)([\.?!]*) *\. *\. *\./g, '$1$2…'); }			},

// Acute accents /*			{			name: 'Accents', position: 'cursor', script: function(editor) { editor.replace(/\*a/g, 'á') .replace(/\*i/g, 'í') .replace(/\*o/g, 'ó') .replace(/\*u/g, 'ú') .replace(/\*A/g, 'Á') .replace(/\*E/g, 'É') .replace(/\*I/g, 'ĺ') .replace(/\*O/g, 'Ó') .replace(/\*U/g, 'Ú'); }			}, */

// Special chars /*			{			name: 'Math', position: 'cursor', script: function(editor) { editor.replace(/\*-/g, '−') .replace(/\*x/g, '×') .replace(/\*\//g, '÷') .replace(/\*'/g, '′') .replace(/\*"/g, '″')				.replace(/\*\./g, '·');			}			}, */

// No wrap /*			{			name: 'Nowrap', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return  + selected + ;				}); }			}, */

// Sic {			name: 'Sic', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return  + selected +  + selected + ' [sic]';				}); }			},

// Convert to lower case {			name: 'To lower', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return selected.toLowerCase;				}); }			},

// Convert to upper case {			name: 'To upper', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return selected.toUpperCase;				}); }			},

// Underline {			name: 'Underline', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return  + selected + ;				}); }			},

// Right {			name: 'Right', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return  + selected + ;				}); }			},

// Cite {			name: 'Cite', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return '';				}); }			},

// Rule {			name: 'Rule (5em)', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return selected + '\n\n';				}); }			},

// Inclose with p tag - paragraph break {			name: 'Paragraph break', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					return selected + '\n';				}); }			},

// Author {			name: 'Author', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {					selected = selected.trim;					return  + selected + ;				}); }			},

// add refs to footer {			name: 'Footer—Smallrefs', position: 'cursor', script: function(editor) { editor.forField('#wpFooterTextbox') .append(''); }			},

// add ref block to footer {			name: 'Footer—Ref block', position: 'cursor', script: function(editor) { editor.forField('#wpFooterTextbox') .append(''); }			},

// add smaller center to footer {			name: 'Footer—Center', position: 'after', script: function(editor) { editor.forField('#wpFooterTextbox') .append(''); }			},

// add Category to footer {			name: 'Footer—Not trans.', position: 'after', editSummary: 'Category', script: function(editor) { editor.forField('#wpFooterTextbox') .append(''); }			},

// Main namespace, smallrefs {			name: 'Main—Refs', template: '\n\n', position: 'after', forNamespaces: 0 },

// Join paragraph {			name: 'Join', position: 'cursor', script: function(editor) { editor.replaceSelection(function(selected) {			 		selected = selected.replace(/ *(\r\n|\n|\r)/gm, ' ');			 		return selected;			 	}); }			},

// End of page: nop, peh {			name: 'End of page', position: 'cursor', script: function(editor) { editor.replace(/-[ \n]*$/, '') .replace(/—[ \n]*$/, '') .replace(/\+[ \n]*$/, '') .replace(/([^}])[ \n]*$/, '$1\n'); }			},			// Form paragraphs {			name: 'Parag.', position: 'cursor', script: function(editor) { editor.replace(/([^>}\n]) *\n([^<{\|\n])/g, '$1 $2') .replace(/ *([-—]) */g, '$1') .replace(/ {2,}/g, ' ') .replace(/[ \n]*$/, ''); }			},

]); }); //

// Pop-up options window.popupFixRedirs = true; window.popupFixDabs = true;

// Special characters gadget //mw.loader.load('//wikisource.org/w/index.php?title=MediaWiki:SpecialChars.js&action=raw&ctype=text/javascript'); function keycheck { tb = document.getElementById("wpTextbox1"); if(tb && mw.config.get('wgNamespaceNumber')!=8) { tb.onkeyup = function(e) { var key = (window.event) ? event.key : e.key; var charCode = key.charCodeAt(0); // pour compatibilité sur divers navigateurs startPos = tb.selectionStart; endPos = tb.selectionEnd; if(endPos == startPos && (charCode == 34 || charCode == 36 || charCode == 39 || (charCode >= 44 && charCode <= 46) || (charCode >= 48 && charCode <= 62) || (charCode >= 65 && charCode <= 90) || charCode == 94 || charCode == 95 || (charCode >= 97 && charCode <= 122)) ) { m2 = tb.value.substring(startPos - 2, startPos); m3 = tb.value.substring(startPos - 3, startPos); m4 = tb.value.substring(startPos - 4, startPos); m2_pre = tb.value.substring(startPos - 1, startPos+1); f = tb.value.substring(startPos); todo = false;

// Raccourcis de trois caractères if(!todo) { i = 3; m = m3; todo = true; switch(m) { // Ponctuation case "..." : m = "…"; break; case "---" : m = "—"; break; // Ligatures et lettres latines exotiques case "a~e" : m = "æ"; break; case "A~E" : m = "Æ"; break; case "o~e" : m = "œ"; break; case "O~E" : m = "Œ"; break; case "s~s" : m = "ß"; break; case "t~h" : m = "þ"; break; case "T~H" : m = "Þ"; break; case "d~h" : m = "ð"; break; case "D~H" : m = "Ð"; break; // Barres obliques inscrites case "o~/" : m = "ø"; break; case "O~/" : m = "Ø"; break; case "l~/" : m = "ł"; break; case "L~/" : m = "Ł"; break; // Accents aigus case "a^'" : m = "á"; break; case "A^'" : m = "Á"; break; case "c^'" : m = "ć"; break; case "C^'" : m = "Ć"; break; case "e^'" : m = "é"; break; case "E^'" : m = "É"; break; case "i^'" : m = "í"; break; case "I^'" : m = "Í"; break; case "n^'" : m = "ń"; break; case "N^'" : m = "Ń"; break; case "o^'" : m = "ó"; break; case "O^'" : m = "Ó"; break; case "s^'" : m = "ś"; break; case "S^'" : m = "Ś"; break; case "u^'" : m = "ú"; break; case "U^'" : m = "Ú"; break; case "z^'" : m = "ź"; break; case "Z^'" : m = "Ź"; break; // Carons case "a^v" : m = "ǎ"; break; case "A^v" : m = "Ǎ"; break; case "c^v" : m = "č"; break; case "C^v" : m = "Č"; break; case "d^v" : m = "ď"; break; case "D^v" : m = "Ď"; break; case "e^v" : m = "ě"; break; case "E^v" : m = "Ě"; break; case "g^v" : m = "ǧ"; break; case "G^v" : m = "Ǧ"; break; case "i^v" : m = "ǐ"; break; case "I^v" : m = "Ǐ"; break; case "l^v" : m = "ľ"; break; case "L^v" : m = "Ľ"; break; case "n^v" : m = "ň"; break; case "N^v" : m = "Ň"; break; case "o^v" : m = "ǒ"; break; case "O^v" : m = "Ǒ"; break; case "r^v" : m = "ř"; break; case "R^v" : m = "Ř"; break; case "s^v" : m = "š"; break; case "S^v" : m = "Š"; break; case "t^v" : m = "ť"; break; case "T^v" : m = "Ť"; break; case "u^v" : m = "ǔ"; break; case "U^v" : m = "Ǔ"; break; case "z^v" : m = "ž"; break; case "Z^v" : m = "Ž"; break; // Macrons case "a^_" : m = "ā"; break; case "A^_" : m = "Ā"; break; case "e^_" : m = "ē"; break; case "E^_" : m = "Ē"; break; case "i^_" : m = "ī"; break; case "I^_" : m = "Ī"; break; case "o^_" : m = "ō"; break; case "O^_" : m = "Ō"; break; case "u^_" : m = "ū"; break; case "U^_" : m = "Ū"; break; case "y^_" : m = "ȳ"; break; case "Y^_" : m = "Ȳ"; break; // Brèves case "a^-" : m = "ă"; break; case "A^-" : m = "Ă"; break; case "e^-" : m = "ĕ"; break; case "E^-" : m = "Ĕ"; break; case "g^-" : m = "ğ"; break; case "G^-" : m = "Ğ"; break; case "i^-" : m = "ĭ"; break; case "I^-" : m = "Ĭ"; break; case "o^-" : m = "ŏ"; break; case "O^-" : m = "Ŏ"; break; case "u^-" : m = "ŭ"; break; case "U^-" : m = "Ŭ"; break; // Ronds en chef case "a^°" : m = "å"; break; case "A^°" : m = "Å"; break; case "u^°" : m = "ů"; break; case "U^°" : m = "Ů"; break; // Cédilles et virgules souscrites case "c_," : m = "ç"; break; case "C_," : m = "Ç"; break; case "s_," : m = "ş"; break; case "S_," : m = "Ş"; break; // Monnaies case "c_$" : m = "¢"; break; case "C_$" : m = "₵"; break; case "Y_$" : m = "¥"; break; default : todo = false; }				}

// Raccourcis de deux caractères if(!todo) { i = 2; m = m2; todo = true; switch(m) { // Ponctuation case "<\"" : m = "“"; break;           			case ">\"" : m = "”"; break; case "<'" : m = "‘"; break; case ">'" : m = "’"; break; case "<<" : m = "«"; break; case ">>" : m = "»"; break; case "^?" : m = "¿"; break; case "^!" : m = "¡"; break; case "^_" : m = "–"; break; // Lettres additionnelles case "^s" : m = "ſ"; break; case "^0" : m = "⁰"; break; case "^1" : m = "¹"; break; case "^2" : m = "²"; break; case "^3" : m = "³"; break; case "^4" : m = "⁴"; break; case "^5" : m = "⁵"; break; case "^6" : m = "⁶"; break; case "^7" : m = "⁷"; break; case "^8" : m = "⁸"; break; case "^9" : m = "⁹"; break; // Math case "^-" : m = "−"; break; case "^x" : m = "×"; break; case "^/" : m = "÷"; break; case "=/" : m = "≠"; break; case "=/" : m = "≠"; break; case "+-" : m = "±"; break; case "^'" : m = "′"; break; case '^"' : m = "″"; break;           			case "^." : m = "·"; break;						default : todo = false;					}				}

if(todo) { var textScroll = tb.scrollTop; h = tb.value.substring(0, startPos - i); tb.value = h + m + f;					tb.selectionStart = startPos - i + m.length; tb.selectionEnd = tb.selectionStart; tb.scrollTop = textScroll; }			}		};	} }

$(document).ready(keycheck);

// Shortcuts //		Ctrl-i -> italics //		Ctrl-b -> bold //		Ctrl-q -> sc //		Ctrl-alt-q -> uc //		Ctrl-alt-w -> – //		Ctrl-alt-c -> [sic] //		Ctrl-alt-r -> //		Ctrl-alt-space ->

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': ""}); } }

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

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

function raccCorr { var selection = $('#wpTextbox1').textSelection('getSelection'); var str = selection.toString; $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': ""+str+"", 'peri': str, 'post': " [sic]", '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': "", 'post': " "}); }

function raccInsec { var selection = $('#wpTextbox1').textSelection('getSelection'); if (!selection.collapsed) { var str = selection.toString; $('#wpTextbox1').textSelection('encapsulateSelection', {'pre': "", 'peri': str, '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; 		        raccCorr; 		    }		    if (event.key === "r" && event.altKey && event.ctrlKey) { 		        event.preventDefault; raccRef; }		   if (event.key === " " && event.ctrlKey) { event.preventDefault; raccInsec; }		}); }