User:Goadeff/common.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | The accompanying .css page for this skin is at User:Goadeff/common.css. |
/*
Any JavaScript here will be loaded for all users on every page load.
*/
/***************************************************************************
Lifted from: https://github.com/Kasheftin/jquery-textarea-caret:
***************************************************************************/
jQuery.fn.extend({
insertAtCursor: function(myValue) {
return this.each(function(i) {
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else if (this.selectionStart || this.selectionStart == "0") {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0,startPos) + myValue + this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
}
else {
this.value += myValue;
this.focus();
}
});
},
insertAroundCursor: function(myValueBefore,myValueAfter) {
return this.each(function(i) {
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValueBefore + sel.text + myValueAfter;
this.focus();
}
else if (this.selectionStart || this.selectionStart == "0") {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0,startPos) + myValueBefore + this.value.substring(startPos,endPos) + myValueAfter + this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValueBefore.length;
this.selectionEnd = endPos + myValueBefore.length;
this.scrollTop = scrollTop;
}
else {
this.value += myValueBefore + myValueAfter;
this.focus();
}
});
}
});
/***************************************************************************
My own bit of tinkering...
***************************************************************************/
function selectionMagic()
{
oldText = $('#wpTextbox1').textSelection('getSelection');
newText = $('<div/>').text(oldText).html();
$('#wpTextbox1').insertAtCursor(newText);
}
/***************************************************************************
Modifying newer WikiEditor toolbar
ref: http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization/Library
***************************************************************************/
var customizeToolbar = function() {
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'main', group: 'format', tools: { "strikethrough": { label: 'Strike', type: 'button',
icon: mw.config.get('stylepath')+'/common/images/Vector_strikeout.png',
action: { type: 'encapsulate', options: { pre: "<s>", post: "</s>" } }
}
}
});
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'main', group: 'format', tools: { "comment": { label: 'Comment', type: 'button',
icon: mw.config.get('stylepath')+'/common/images/Toolbaricon_italics_C.jpg',
action: { type: 'encapsulate', options: { pre: "<!--", post: "-->" } }
}
}
});
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'main', group: 'format', tools: { "template": { label: 'Template', type: 'button',
icon: mw.config.get('stylepath')+'/common/images/Template_icon.svg',
action: { type: 'encapsulate', options: { pre: "{{", post: "}}", peri: "Template" } }
}
}
});
/*
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'main', group: 'format', tools: { "pre": { label: 'Pre', type: 'button',
icon: mw.config.get('stylepath')+'/common/images/Toolbar_pre_vector.png',
action: { type: 'encapsulate', options: { pre: "<pre>", post: "</pre>", peri: "preformatted text" } }
}
}
});
*/
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'main', group: 'format', tools: { "pre": { label: 'Pre', type: 'button',
icon: mw.config.get('stylepath')+'/common/images/Toolbar_pre_vector.png',
action: { type: 'callback', execute: function(context) { selectionMagic(); } }
}
}
});
};
/* Check if we are in edit mode and the required modules are available and then customize the toolbar */
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 ) {
mw.loader.using( 'user.options', function () {
if ( mw.user.options.get('usebetatoolbar') ) {
mw.loader.using( 'ext.wikiEditor.toolbar', function () {
$(document).ready( customizeToolbar );
} );
}
} );
}