From 92d5213ecee17f06667a82b34439c2cc379a3086 Mon Sep 17 00:00:00 2001 From: Leo Koppelkamm Date: Thu, 21 Apr 2011 08:19:24 +0000 Subject: [PATCH] =?utf8?q?Move=20edit.js=20stuff=20to=20mediawiki.action.e?= =?utf8?q?dit.js,=20and=20remove=20wikibits=20dependency.=20Wrap=20publicl?= =?utf8?q?y=20accessible=20functions=20inside=20mw.toolbar=20&=20provide?= =?utf8?q?=20legacy=20shortcuts=20to=20those.=20Make=20the=20traditional?= =?utf8?q?=20toolbar=20use=20$.fn.textSelection,=20to=20reduce=20code=20du?= =?utf8?q?plication.=20This=20fixes=20Bug=2027116,=20Bug=C2=A015705,=20Bug?= =?utf8?q?=2011011?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- includes/EditPage.php | 6 +- resources/Resources.php | 1 + resources/jquery/jquery.textSelection.js | 15 ++- .../mediawiki.action/mediawiki.action.edit.js | 92 ++++++++++++++++++- skins/common/edit.js | 4 + skins/common/shared.css | 4 +- 6 files changed, 109 insertions(+), 13 deletions(-) diff --git a/includes/EditPage.php b/includes/EditPage.php index 9b6f6a0ef7..5e69bb562b 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -373,7 +373,7 @@ class EditPage { $this->preview = true; } - $wgOut->addModules( array( 'mediawiki.legacy.edit', 'mediawiki.action.edit' ) ); + $wgOut->addModules( array( 'mediawiki.action.edit' ) ); if ( $wgUser->getOption( 'uselivepreview', false ) ) { $wgOut->addModules( 'mediawiki.legacy.preview' ); @@ -2410,9 +2410,9 @@ HTML $paramList = implode( ',', array_map( array( 'Xml', 'encodeJsVar' ), $params ) ); - $script .= "addButton($paramList);\n"; + $script .= "mw.toolbar.addButton($paramList);\n"; } - + $script .= "mw.toolbar.init();\n"; $wgOut->addScript( Html::inlineScript( "if ( window.mediaWiki ) { jQuery(function(){{$script}}); }" ) ); diff --git a/resources/Resources.php b/resources/Resources.php index 7ae0d2285f..adf45135b3 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -412,6 +412,7 @@ return array( ), 'mediawiki.action.edit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.edit.js', + 'dependencies' => 'jquery.textSelection', ), 'mediawiki.action.view.rightClickEdit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js', diff --git a/resources/jquery/jquery.textSelection.js b/resources/jquery/jquery.textSelection.js index 22ee09298c..530485ec6e 100644 --- a/resources/jquery/jquery.textSelection.js +++ b/resources/jquery/jquery.textSelection.js @@ -62,10 +62,17 @@ encapsulateSelection: function( options ) { isSample = true; } else if ( options.replace ) { selText = options.peri; - } else if ( selText.charAt( selText.length - 1 ) == ' ' ) { - // Exclude ending space char - selText = selText.substring(0, selText.length - 1); - options.post += ' '; + } else { + while ( selText.charAt( selText.length - 1 ) == ' ' ) { + // Exclude ending space char + selText = selText.substring(0, selText.length - 1); + options.post += ' '; + } + while ( selText.charAt( 0 ) == ' ' ) { + // Exclude prepending space char + selText = selText.substring(1, selText.length); + options.pre = ' ' + options.pre; + } } } var isSample = false; diff --git a/resources/mediawiki.action/mediawiki.action.edit.js b/resources/mediawiki.action/mediawiki.action.edit.js index 8a785e63c2..6d93e9c6ec 100644 --- a/resources/mediawiki.action/mediawiki.action.edit.js +++ b/resources/mediawiki.action/mediawiki.action.edit.js @@ -1,14 +1,61 @@ -/* Note, there is still stuff in skins/common/edit.js that - * has not been jQuery-ized. - */ - (function( $ ) { + // currentFocus is used to determine where to insert tags + var currentFocused = $( '#wpTextbox1' ); + + mw.toolbar = { + $toolbar : $( '#toolbar' ), + addButton : function( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) { + var image = $('', { + width : 23, + height : 23, + src : imageFile, + alt : speedTip, + title : speedTip, + id : imageId || '', + 'class': 'mw-toolbar-editbutton' + } ).click( function() { + mw.toolbar.insertTags( tagOpen, tagClose, sampleText, selectText ); + return false; + } ); + + this.$toolbar.append( image ); + return true; + }, + + // apply tagOpen/tagClose to selection in textarea, + // use sampleText instead of selection if there is none + insertTags : function( tagOpen, tagClose, sampleText, selectText) { + if ( currentFocused.length ) { + currentFocused.textSelection( + 'encapsulateSelection', { 'pre': tagOpen, 'peri': sampleText, 'post': tagClose } + ); + return; + } + }, + init : function() { + // Legacy + // Print out buttons from mwCustomEditButtons + // If you want to add buttons, use + // $( document ).ready( function () { mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) } ); + var c; + for ( i = 0; i < window.mwCustomEditButtons.length; i++ ) { + c = window.mwCustomEditButtons[i]; + mw.toolbar.addButton( c.imageFile, c.speedTip, c.tagOpen, c.tagClose, c.sampleText, c.imageId, c.selectText ); + } + return true; + } + }; + + //Legacy + window.addButton = mw.toolbar.addButton; + window.insertTags = mw.toolbar.insertTags; + //make sure edit summary does not exceed byte limit $( '#wpSummary' ).attr( 'maxLength', 250 ).keypress( function( e ) { // first check to see if this is actually a character key // being pressed. // Based on key-event info from http://unixpapa.com/js/key.html - // JQuery should also normalize e.which to be consistent cross-browser, + // jQuery should also normalize e.which to be consistent cross-browser, // however the same check is still needed regardless of jQuery. // Note: At the moment, for some older opera versions (~< 10.5) @@ -33,4 +80,39 @@ e.preventDefault(); } }); + + + $( document ).ready( function() { + /** + * Restore the edit box scroll state following a preview operation, + * and set up a form submission handler to remember this state + */ + var scrollEditBox = function() { + var editBox = document.getElementById( 'wpTextbox1' ); + var scrollTop = document.getElementById( 'wpScrolltop' ); + var $editForm = $( '#editform' ); + if( $editForm.length && editBox && scrollTop ) { + if( scrollTop.value ) { + editBox.scrollTop = scrollTop.value; + } + $editForm.submit( function() { + scrollTop.value = editBox.scrollTop; + }); + } + }; + scrollEditBox(); + + $( '#wpSummary, #wpTextbox1' ).focus( function() { + currentFocused = $(this); + }); + + // HACK: make currentFocused work with the usability iframe + // With proper focus detection support (HTML 5!) this'll be much cleaner + var iframe = $( '.wikiEditor-ui-text iframe' ); + if ( iframe.length > 0 ) { + $( iframe.get( 0 ).contentWindow.document ) + .add( iframe.get( 0 ).contentWindow.document.body ) // for IE + .focus( function() { currentFocused = iframe; } ); + } + }); })(jQuery); diff --git a/skins/common/edit.js b/skins/common/edit.js index 35505f4cdb..732aa92152 100644 --- a/skins/common/edit.js +++ b/skins/common/edit.js @@ -1,3 +1,7 @@ +// This file is still referenced from +// tests/selenium/data/SimpleSeleniumTestDB.sql +// includes/specials/SpecialUpload.php +// /extensions/SemanticForms/specials/SF_UploadWindow2.php window.currentFocused = undefined; // this function adds a toolbar button to the mwEditButtons list diff --git a/skins/common/shared.css b/skins/common/shared.css index c9a3bd3ffe..bbb8552dbd 100644 --- a/skins/common/shared.css +++ b/skins/common/shared.css @@ -40,7 +40,9 @@ span.texhtml { font-family: serif; } #editform, #toolbar, #wpTextbox1 { clear: both; } - +#toolbar img { + cursor: pointer; +} div#mw-js-message { margin: 1em 5%; padding: 0.5em 2.5%; -- 2.20.1