Jump to content

User:Goadeff/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Goadeff (talk | contribs) at 00:09, 26 June 2012. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/***************************************************************************
 BEGIN jQuery extensions
   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();
            }
        });
    }
});
/***************************************************************************
END jQuery extensions
***************************************************************************/
  
/***************************************************************************
BEGIN My own bit of tinkering...
***************************************************************************/
function selectionMagic()
{
    oldText = $('#wpTextbox1').textSelection('getSelection');
    newText = $('<div/>').text(oldText).html();
    $('#wpTextbox1').insertAtCursor(newText);
}
/***************************************************************************
END of my own bit of tinkering...
***************************************************************************/
 
/***************************************************************************
   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: '/media/wikipedia/commons/6/6d/Vector_strikeout.png',
                action: { type: 'encapsulate', options: { pre: "<s>", post: "</s>" } }
            }
        }
    });
   $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "pre": { label: 'Pre', type: 'button', 
            icon: '/media/wikipedia/commons/b/b9/Toolbar_pre_vector.png',
                action: { type: 'encapsulate', options: { pre: "<pre>", post: "</pre>", peri: "preformatted text" } }
            }
        }
    });
};
 
/* 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 );
                        } );
                }
        } );
}