Участник:Pj Uran/jump2text.js
Перейти к навигации
Перейти к поиску
var editSearch = new function(){
var txtarea
var popup = null
var selText = ''
this.onLoad = function(){
var wikiPreview = document.getElementById('wikiPreview')
txtarea = document.getElementById('wpTextbox1')
if (!wikiPreview || !txtarea) return
addEvent(wikiPreview, 'mouseup', editSearch.onMouseUp)
}
this.onMouseUp = function(e){
hidePopup()
if (! (selText = getSelectedText()) ) return
selText = selText.replace(/^ +/, "").replace(/ +$/, "") //trim spaces
if (selText.length < 1 || txtarea.value.indexOf(selText) == -1) return //empty or not found selection
//show popup
if (!popup) createPopup()
var coords=getMousePos(e || window.event)
popup.style.left=coords[0]+5+'px'
popup.style.top=coords[1]+10+'px'
popup.style.display='block'
}
this.onGo = function(){
hidePopup()
selectInTextArea(selText)
}
function createPopup(){
popup=document.createElement('div')
popup.className = 'popup'
popup.title='Найти выделенный текст в окошке редактирования'
with (popup.style){ position='absolute'; padding='2px'; border='1px outset gray';
zIndex='50'; backgroundColor='#EEEEEE'; cursor='pointer' }
popup.onclick=editSearch.onGo
popup.appendChild(document.createTextNode(' ↓ '))
document.body.appendChild(popup)
}
function hidePopup(){
if (popup) popup.style.display='none'
}
function selectInTextArea(text){
if (txtarea.setSelectionRange) {//Mozilla/Opera
var selPos = txtarea.value.indexOf(text)
if (selPos < 0) return
txtarea.focus()
txtarea.setSelectionRange(selPos, selPos + text.length)
//try to scroll texarea next to cursor
txtarea.scrollTop = txtarea.scrollHeight * selPos / txtarea.value.length - 50
//try to scroll the window to textarea better
if (window.pageYOffset + window.innerHeight < txtarea.offsetHeight + 200) window.scrollTo(0,txtarea.offsetHeight - 50)
}else if (txtarea.createTextRange){ //IE
var oRange = txtarea.createTextRange()
if (oRange.findText(text)) oRange.select()
}
}
}//obj
//Common methods
function addEvent(obj, event, func) {
if (obj.addEventListener) obj.addEventListener( event, func, false )
else if (obj.attachEvent) obj.attachEvent ('on'+event, func)
}
function getSelectedText(){
if (window.getSelection) return window.getSelection().toString()
else if (document.getSelection) return document.getSelection().toString()
else if (document.selection && document.selection.createRange) return document.selection.createRange().text
else return null
}
function getMousePos(ev){
var posx = 0, posy = 0
if (ev.pageX)
return [ev.pageX, ev.pageY]
else if (ev.clientX)
return [ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
ev.clientY + document.body.scrollTop + document.documentElement.scrollTop]
else return null
}
//load object
if (mw.config.get('wgAction')=='edit' || mw.config.get('wgAction')=='submit')
$(editSearch.onLoad)