2 // currentFocus is used to determine where to insert tags
3 var currentFocused
= $( '#wpTextbox1' );
6 $toolbar
: $( '#toolbar' ),
7 addButton : function( imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
, selectText
) {
8 var image
= $('<img>', {
15 'class': 'mw-toolbar-editbutton'
16 } ).click( function() {
17 mw
.toolbar
.insertTags( tagOpen
, tagClose
, sampleText
, selectText
);
21 this.$toolbar
.append( image
);
25 // apply tagOpen/tagClose to selection in textarea,
26 // use sampleText instead of selection if there is none
27 insertTags : function( tagOpen
, tagClose
, sampleText
, selectText
) {
28 if ( currentFocused
.length
) {
29 currentFocused
.textSelection(
30 'encapsulateSelection', { 'pre': tagOpen
, 'peri': sampleText
, 'post': tagClose
}
37 // Print out buttons from mwCustomEditButtons
38 // If you want to add buttons, use
39 // $( document ).ready( function () { mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) } );
41 for ( i
= 0; i
< window
.mwCustomEditButtons
.length
; i
++ ) {
42 c
= window
.mwCustomEditButtons
[i
];
43 mw
.toolbar
.addButton( c
.imageFile
, c
.speedTip
, c
.tagOpen
, c
.tagClose
, c
.sampleText
, c
.imageId
, c
.selectText
);
50 window
.addButton
= mw
.toolbar
.addButton
;
51 window
.insertTags
= mw
.toolbar
.insertTags
;
53 //make sure edit summary does not exceed byte limit
54 $( '#wpSummary' ).attr( 'maxLength', 250 ).keypress( function( e
) {
55 // first check to see if this is actually a character key
57 // Based on key-event info from http://unixpapa.com/js/key.html
58 // jQuery should also normalize e.which to be consistent cross-browser,
59 // however the same check is still needed regardless of jQuery.
61 // Note: At the moment, for some older opera versions (~< 10.5)
62 // some special keys won't be recognized (aka left arrow key).
63 // Backspace will be, so not big issue.
65 if ( e
.which
=== 0 || e
.charCode
=== 0 || e
.which
=== 8 ||
66 e
.ctrlKey
|| e
.altKey
|| e
.metaKey
)
68 return true; //a special key (backspace, etc) so don't interfere.
71 // This basically figures out how many bytes a UTF-16 string (which is what js sees)
72 // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
73 // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
74 // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in edge cases
75 // such as illegal sequences, but that should never happen.
77 var len
= this.value
.replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ).replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ).length
;
78 //247 as this doesn't count character about to be inserted.
85 $( document
).ready( function() {
87 * Restore the edit box scroll state following a preview operation,
88 * and set up a form submission handler to remember this state
90 var scrollEditBox = function() {
91 var editBox
= document
.getElementById( 'wpTextbox1' );
92 var scrollTop
= document
.getElementById( 'wpScrolltop' );
93 var $editForm
= $( '#editform' );
94 if( $editForm
.length
&& editBox
&& scrollTop
) {
95 if( scrollTop
.value
) {
96 editBox
.scrollTop
= scrollTop
.value
;
98 $editForm
.submit( function() {
99 scrollTop
.value
= editBox
.scrollTop
;
105 $( '#wpSummary, #wpTextbox1' ).focus( function() {
106 currentFocused
= $(this);
109 // HACK: make currentFocused work with the usability iframe
110 // With proper focus detection support (HTML 5!) this'll be much cleaner
111 var iframe
= $( '.wikiEditor-ui-text iframe' );
112 if ( iframe
.length
> 0 ) {
113 $( iframe
.get( 0 ).contentWindow
.document
)
114 .add( iframe
.get( 0 ).contentWindow
.document
.body
) // for IE
115 .focus( function() { currentFocused
= iframe
; } );