User:Yair rand/usexeditor.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// This is an example sentence editor, to be used to modify existing example sentences. It can't actually add new example sentences, you'll need to see [[User:Yair rand/editor.js]] for that :) 

function setupusexeditor()
{
  if(wgAction=='view' && (wgNamespaceNumber==0 || wgPageName=="Wiktionary:Sandbox")&&!/&printable=yes|&diff=|&oldid=/.test(window.location.search)){
    mw.util.addCSS("dd>.usexeditbutton{cursor:pointer; margin-left: -10px;margin-right:0px;padding-right:3px;display:none;}dd:hover>.usexeditbutton{display:inline}")
    var dls=document.getElementsByTagName('dl'), dl, dds, i, ii
    for(i=0;i<dls.length;i++){
      if(dls[i].parentNode.parentNode.nodeName.toLowerCase() == "ol"){
        dds = dls[i].getElementsByTagName('dd');
        for(ii=0;ii<dds.length;ii++){
          setupusexeditor2(dds[ii])
          }
        }
      }
    }
  function setupusexeditor2(dd)
  {
  dd.insertBefore(newNode('a','±',{'class':'usexeditbutton', title:"Edit example sentence.", click:function(){
    if(dd.hiddenusex){
      if(dd.getElementsByTagName('form')[0].style.display=='inline'){
        dd.insertBefore(dd.hiddenusex, dd.getElementsByTagName('form')[0]);
        dd.getElementsByTagName('form')[0].style.display='none'
        }
      else{
        dd.removeChild(dd.hiddenusex)
        dd.getElementsByTagName('form')[0].style.display='inline'
        }
      }
    else
      setupusexeditor3(dd);
    }}), dd.firstChild)
  }

  function setupusexeditor3(dd)
  {


  editor = new Editor()
  editor.withCurrentText(function(res){
  
    dd.hiddenusex = newNode('span')
    while(dd.childNodes[1]&&!/form|dl/i.test(dd.childNodes[1].nodeName))
      dd.hiddenusex.appendChild(dd.childNodes[1]);
    var theform, findspot, torender='blah', newwikitext, editbutton=dd.firstChild;
    new AdderWrapper(editor, {
      createForm:function(){findspot=findusexlocation(dd,res);return theform=newNode('form',{style:'display:inline;'},newNode('input',{size:100,value:res.substr(findspot[0],findspot[1]),name:'examplesentence'}),newNode('input',{type:'submit',value:'Preview'}))},
      fields:{examplesentence:function(txt){return txt}},
      onsubmit: function(values, render){
        var newspan = newNode('span');
        editor.withCurrentText(function(res){
          findspot = findusexlocation(dd, res);
          newwikitext = res.substr(0,findspot[0]) + values.examplesentence + res.substr(findspot[0]+findspot[1]);
          torender = newwikitext.substr(newwikitext.substr(0,findspot[0]).lastIndexOf("#")).match(/^.*/)[0].replace(/^#*:*\ */,'')
          })
        render(torender, function(res){
          newspan.innerHTML = res;
          editor.addEdit({
            edit:function(wikitext){return ccc=newwikitext},
            redo:function(){
              theform.style.display='none'; if(dd.hiddenusex.parentNode == dd){dd.removeChild(dd.hiddenusex)}
              dd.insertBefore(newspan, editbutton); dd.removeChild(editbutton)},
            undo:function(){
              theform.style.display='inline'; dd.removeChild(newspan); dd.insertBefore(editbutton, theform)},
            summary: "±usex: "+values.examplesentence
            },newspan)
          })
        }
      }, dd, dd.getElementsByTagName('dl')[0])
    })
  }
}

function findusexlocation(tosearchfrom, res) // todo: get rid of the enormous amount of repetition
{
  function qwe(c)
  {
  for(var r=1; tosearchfrom.previousSibling; tosearchfrom = tosearchfrom.previousSibling){
    if(tosearchfrom.previousSibling.nodeName.toLowerCase() == c)
      r++;
    }
  return r;
  }
  var findnumberofheaders, findnumberofdefs, findnumberofsubdefs=0, findnumberofpredefs=0, findnumberofsubusexes=[], indents='', findspot, findend;
  while(tosearchfrom.nodeName.toLowerCase() != "li"){
    findnumberofsubusexes.push(qwe('dd')); 
    tosearchfrom=tosearchfrom.parentNode.parentNode;
    }
  if(tosearchfrom.parentNode.parentNode.nodeName.toLowerCase() == "li"){
    indents += "#";
    findnumberofsubdefs = qwe('li')
    tosearchfrom = tosearchfrom.parentNode.parentNode
    }
  findnumberofdefs = qwe('li')
  tosearchfrom = tosearchfrom.parentNode.previousSibling
  while(!/h\d/i.test(tosearchfrom.nodeName)){
    if(tosearchfrom.nodeName.toLowerCase() == "ol")
      findnumberofpredefs++;
    tosearchfrom = tosearchfrom.previousSibling
    }
  findnumberofheaders = Number(tosearchfrom.firstChild.getElementsByTagName('a')[0].href.split("&section=")[1])

  findspot=res.search(/(^|\n)=/)+1;
  while(findnumberofheaders-=1)
    findspot += res.substr(findspot).indexOf("\n=")+1;
  while(findnumberofpredefs--)
    findspot += res.substr(findspot+=res.substr(findspot).indexOf("\n#")).search(/\n(?!#)/)+1;
  while(findnumberofdefs--)
    findspot += res.substr(findspot).search(/\n#(?![#\:\*])/)+1;
  while(findnumberofsubdefs--)
    findspot += res.substr(findspot).search(/\n##(?![\:\*])/)+1;
  while(findnumberofsubusexes.length){
    indents += ":";
    while(findnumberofsubusexes[findnumberofsubusexes.length-1]--)
      findspot += res.substr(findspot).search(RegExp("\n#"+indents+"(?!:)"))+1;
    findnumberofsubusexes.pop()
    }
  findspot+=1+indents.length;
  if(res.substr(findspot).indexOf(" ") == 0)
    findspot++;
  findend=res.substr(findspot).search(/\n|$/)
  if(/^''.*''$/.test(res.substr(findspot, findend))){
    findspot+=2; findend-=4
    }
  if(/^\{\{[A-Z][a-z]{3}\|.*\}\}$/.test(res.substr(findspot, findend))){
    findspot+=7; findend-=9;d=res.substr(findspot,findend)
    if(res.substr(findspot).indexOf("lang=") == 0){
      findend-=res.substr(findspot).indexOf("|")+1;
      findspot+=res.substr(findspot).indexOf("|")+1;
      }
    if(res.substr(findspot, findend).indexOf("|lang=") > 0)
      findend=res.substr(findspot).indexOf("|lang=");
    }

return [findspot,findend]

}

$(setupusexeditor)