มีเดียวิกิ:Gadget-Edittools.js
หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง
- ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
- กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
- ไมโครซอฟท์ เอดจ์: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
// <nowiki>
// implicit dependencies: mediawiki.cookie, jquery.textselection
(function charInsertIIFE () {
function CharInsertCookie(key) {
this.key = key;
}
CharInsertCookie.prototype = {
get: function () {
return parseInt(mw.cookie.get(this.key), 10) || 0;
},
set: function (value) {
if (typeof value === 'number')
value = value.toString(10);
if (!(typeof value === 'string' && !isNaN(parseInt(value, 10))))
throw new TypeError("Expected string or number");
return mw.cookie.set(this.key, value);
},
};
var charInsertCookie = new CharInsertCookie('edittoolscharsubset');
/* ===applyCharinserts=== */
/* handle <span class="charinsert"> like <charinsert> */
function applyCharinserts() {
var textbox = $('#wpTextbox1');
textbox.encapsulate = function (left, right) {
return this.textSelection(
'encapsulateSelection', {
pre: left.replace(/^ */, '')
.replace(/\u0640(.)/g, '$1'), // remove ARABIC TATWEEL before Arabic diacritic
peri: '',
post: right
}
);
};
// Must set insertArgs in the element for which this is an event handler.
function clickFunction () {
textbox.encapsulate.apply(textbox, this.insertArgs);
return false;
}
function getInsertArg(string, start, end) {
string = string.substring(start, end);
return string !== ''
? string.replace(/\x22/g, '"').replace(/\x27/g, "\\'").replace(/\x26nbsp;/g, ' ')
: string;
}
function makeInsertArgs(string) {
var index = string.indexOf('+');
if (index === -1)
index = string.length;
var left = getInsertArg(string, 0, index);
var right = getInsertArg(string, index + 1);
return [ left, right ];
}
function makeCharInserter(string) {
var charInserter = document.createElement('a');
charInserter.onclick = clickFunction;
charInserter.href = '#';
charInserter.classList.add("charinserter");
var insertArgs = makeInsertArgs(string);
charInserter.insertArgs = insertArgs;
charInserter.appendChild(document.createTextNode(insertArgs.join('')));
return charInserter;
}
function charInsertify(parent) {
if (parent.charInsertified)
return;
parent.charInsertified = true;
// Go through all child nodes of parent.
for (var childNode = parent.firstChild;
childNode !== null;
childNode = childNode.nextSibling) {
if (childNode.nodeType === 1) { // Element node
charInsertify(childNode);
} else if (childNode.nodeType === 3) { // Text node
// Split text content on whitespace characters (except no-break space);
var strings = childNode.nodeValue
// Replace literal no-break space with HTML character reference.
.replace(/\xA0/g, '\x26nbsp;')
.split(/\s/g);
var addedNew = false;
for (var i = 0; i < strings.length; ++i) {
if (strings[i] !== '') {
if (i > 0)
parent.insertBefore(document.createTextNode(' '), childNode);
parent.insertBefore(makeCharInserter(strings[i]), childNode);
addedNew = true;
}
}
if (addedNew)
parent.removeChild(childNode);
}
}
}
var charInsertSpans = document.querySelectorAll('#editpage-specialchars .charinsert');
Array.prototype.forEach.call(charInsertSpans, charInsertify);
}
/* ===addCharSubsetMenu=== */
/* add menu for selecting subsets of secial characters */
function addCharSubsetMenu() {
var edittools = $('#editpage-specialchars');
if (edittools.length === 0) return;
var menu = $('<select>')
.attr("id", 'charSubsetControl').css("display", "inline")
.on("change", function() {
chooseCharSubset($(this).val());
});
var sections = edittools.find('p');
if (sections.length === 0) return;
sections.each(function(index){
$('<option>')
.text(decodeURIComponent(
($(this).attr("id") || '').replace(/^edittools-/, '')
.replace(/\.([0-9A-F][0-9A-F])/g, '%$1')
.replace(/_/g, '%20')))
.val(index)
.appendTo(menu);
});
/* default subset from cookie */
var sectionIndex = charInsertCookie.get();
/* update dropdown control to value of cookie */
menu.val(sectionIndex);
/* display the subset indicated by the cookie */
chooseCharSubset(sectionIndex);
edittools.prepend(menu);
}
/* ===chooseCharSubsetMenu=== */
/* select subsection of special characters */
function chooseCharSubset(sectionIndex) {
var sections = $('#editpage-specialchars').find('p');
for (var i = 0; i < sections.length; i++) {
var style = sections[i].style;
style.display = i == sectionIndex ? 'inline' : 'none';
}
charInsertCookie.set(sectionIndex);
}
$(function() {
var action = mw.config.get('wgAction');
if (window.testNewEditJs || !(action === 'edit' || action === 'submit'
|| $('#editpage-specialchars').length > 0))
return;
if (!window.doNotUseDefaultEditTools) // [[User:Conrad.Irwin/edittools.js]]
addCharSubsetMenu();
applyCharinserts();
});
})();
// </nowiki>