Участник:Alex Smotrov/jump2text.js

if( /edit|submit/.test(mw.config.get('wgAction')) )
  $('#wikiPreview').mouseup( jumpOnSelection )



function jumpOnSelection(e){

var popup = $('#jump-arrow').hide()
sel = $.trim( getSelText() )
if( sel.length < 1 || $('#wpTextbox1').val().indexOf(sel) == -1 ) return //empty/not found

//create arrow button
if( !popup.length )
  popup = $('<div id="jump-arrow"\
  title="Перейти к этому тексту в окне редактирования"\
  style="position:absolute; padding:2px; border:1px outset gray;\
  z-index:50; background-color:#EEE; cursor:pointer"\
  > ↓ </div>')
  .appendTo(document.body)
  .click(function(){
     popup.hide()
     selectInTextArea(sel)
   })

//show arrow next to mouse click
popup.show().offset({ top: e.pageY, left: e.pageX + 10 })

return



function getSelText(){ //returns selected text (not in textarea)
 if( window.getSelection ) return window.getSelection().toString()
 else if( document.getSelection ) return document.getSelection().toString()
 else if( document.selection ) return document.selection.createRange().text
 else return null
}



function selectInTextArea(text){
 tbox = document.getElementById('wpTextbox1')
 if( tbox.setSelectionRange ) {//Mozilla/Opera
   var selPos = tbox.value.indexOf(text)
   if (selPos < 0) return
   tbox.focus()
   tbox.setSelectionRange(selPos, selPos + text.length)
   //try to scroll texarea next to cursor
   tbox.scrollTop = tbox.scrollHeight * selPos / tbox.value.length - 50
   //try to scroll the window to textarea better
   if( window.pageYOffset + window.innerHeight < tbox.offsetHeight + 200 ) window.scrollTo(0,tbox.offsetHeight - 50)	
 }else if( tbox.createTextRange ){ //IE
   var oRange = tbox.createTextRange()
   if( oRange.findText(text) )  oRange.select()
 }
}



}//main
Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

Portal di Ensiklopedia Dunia

Kembali kehalaman sebelumnya