User:Magnus Manske/less edit clutter.js: Difference between revisions

Content deleted Content added
minor updates
experimental reference management, deactivated
Line 2:
addOnloadHook ( less_edit_clutter_init ) ;
}
 
var lec_parse_references = 0 ;
 
var lec_parent ;
Line 26 ⟶ 28:
var b = document.getElementById ( id ) ;
b.onclick = function () { less_edit_clutter_rejoin() ; return true ; } ;
}
 
function less_edit_clutter_mark_text_range ( ctrl , from , to ) {
if(ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange ( from , to ) ;
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse ( true ) ;
range.moveEnd ( 'character', from ) ;
range.moveStart ( 'character', to ) ;
range.select();
}
}
 
function less_edit_clutter_tb1_select ( ta ) {
var selection = ta.value.substr(ta.selectionStart, ta.selectionEnd - ta.selectionStart);
if ( ! /^\<*REFERENCE\d+\>*$/.test ( selection ) ) return ;
alert ( selection ) ;
}
 
function less_edit_clutter_find_and_mark ( text , ctrl ) {
var pos = ctrl.value.search ( text ) ;
if ( pos < 0 ) return true ; // Continue search
less_edit_clutter_mark_text_range ( ctrl , pos , pos + text.length ) ;
return false ;
}
 
function less_edit_clutter_refsel_changed () {
var si = lec_tb_ref.selectedIndex + 1 ;
var key = '<<REFERENCE' + si + '>>' ;
var cont = true ;
if ( cont ) cont = less_edit_clutter_find_and_mark ( key , lec_tb_top ) ;
if ( cont ) cont = less_edit_clutter_find_and_mark ( key , lec_tb1 ) ;
if ( cont ) cont = less_edit_clutter_find_and_mark ( key , lec_tb_bt ) ;
if ( cont ) alert ( "Something's wrong - could not find reference " + key + "! Better reload this edit in the standard edit box!" ) ;
}
 
function less_edit_clutter_refsel_double_clicked () {
var si = lec_tb_ref.selectedIndex ;
var text = lec_tb_ref[si].firstChild.nodeValue ;
text = text.split ( ' : ' ) ;
var value = text.shift() ;
text = text.join ( ' : ' ) ;
text = text.split ( ' | ' ) ;
var refstuff = text.shift() ;
text = text.join ( ' | ' ) ;
refstuff = prompt ( "Enter reference name:" , refstuff ) ;
text = prompt ( "Enter reference contents:" , text ) ;
lec_tb_ref[si].firstChild.nodeValue = value + ' : ' + refstuff + ' | ' + text ;
}
 
Line 60 ⟶ 113:
}
v = v.replace(/^\s+|\s+$/g,"") ;
if ( lec_parse_references ) {
for ( var n = lec_tb_ref.firstChild ; n ; n = n.nextSibling ) {
var text = n.firstChild.nodeValue ;
text = text.split ( ' : ' ) ;
var value = text.shift() ;
text = text.join ( ' : ' ) ;
text = text.split ( ' | ' ) ;
var refstuff = text.shift() ;
text = text.join ( ' | ' ) ;
text = '<ref' + refstuff + '>' + text + '</ref>' ;
var key = '<<REFERENCE' + value + '>>' ;
v = v.split(key).join(text) ;
}
}
 
lec_tb1.value = v ;
Line 91 ⟶ 159:
lec_tb_bt.cols = 30 ;
lec_tb_bt.rows = 5 ;
 
lec_bottom_table = document.createElement ( 'table' ) ;
lec_bottom_tbody = document.createElement ( 'tbody' ) ;
lec_bottom_tr = document.createElement ( 'tr' ) ;
lec_bottom_tr2 = document.createElement ( 'tr' ) ;
lec_bottom_td1 = document.createElement ( 'td' ) ;
lec_bottom_td2 = document.createElement ( 'td' ) ;
Line 115 ⟶ 184:
lec_bottom_td3.appendChild ( lec_tb_ill ) ;
 
if ( lec_parse_references ) {
lec_tb1.onselect = function () { less_edit_clutter_tb1_select ( lec_tb1 ) ; } ;
lec_tb_top.onselect = function () { less_edit_clutter_tb1_select ( lec_tb_top ) ; } ;
lec_tb_bt.onselect = function () { less_edit_clutter_tb1_select ( lec_tb_bt ) ; } ;
lec_tb_ref = document.createElement ( 'select' ) ;
lec_tb_ref.id = 'lec_tb_ref' ;
lec_tb_ref.name = 'lec_tb_ref' ;
lec_tb_ref.size = 5 ;
lec_tb_ref.style.width = '100%' ;
lec_tb_ref.onchange = less_edit_clutter_refsel_changed ;
lec_tb_ref.ondblclick = less_edit_clutter_refsel_double_clicked ;
 
lec_bottom_td4 = document.createElement ( 'td' ) ;
lec_bottom_td4.appendChild ( document.createTextNode ( 'References' ) ) ;
lec_bottom_td4.appendChild ( document.createElement ( 'br' ) ) ;
lec_bottom_td4.appendChild ( lec_tb_ref ) ;
lec_bottom_td4.colSpan = '3' ;
 
lec_bottom_tr2.appendChild ( lec_bottom_td4 ) ;
lec_bottom_tbody.appendChild ( lec_bottom_tr2 ) ;
}
lec_bottom_tr.appendChild ( lec_bottom_td1 ) ;
lec_bottom_tr.appendChild ( lec_bottom_td2 ) ;
Line 123 ⟶ 214:
var a = document.createElement ( 'a' ) ;
a.href = '#' ;
a.appendChild ( document.createTextNode ( "resetreload toin standard edit box" ) ) ;
a.onclick = function () {
u = document.getElementById('lec_div_top') ;
Line 212 ⟶ 303:
var in_template = 0 ;
var begin_templates = "" ;
// References
if ( lec_parse_references ) {
cnt = 1 ;
v = lec_original ;
v = v.split ( /\<ref/ig ) ;
o = v.shift () ;
refs = new Array () ;
while ( lec_tb_ref.firstChild ) lec_tb_ref.removeChild ( lec_tb_ref.firstChild ) ;
for ( i = 0 ; i < v.length ; i++ ) {
if ( v[i].substr(0,1) != ' ' && v[i].substr(0,1) != '>' ) { // Something that is not a reference tag
o += '<ref' + v[i] ;
continue ;
}
n = v[i].split ( '>' ) ;
if ( n.length < 2 ) { o += '<ref' + v[i] ; continue ; }
refstuff = n.shift() ;
n = n.join ( '>' ) ;
n = n.split ( '</ref>' ) ;
if ( n.length < 2 ) { o += '<ref' + v[i] ; continue ; }
refcontents = n.shift() ;
// if ( n.length > 1 ) { o += '<ref' + v[i] ; continue ; }
n = n.join ( '</ref>' ) ;
o += '<<REFERENCE' + cnt + '>>' + n ;
refs.push ( cnt + ' : ' + refstuff + ' | ' + refcontents ) ;
cnt++ ;
}
for ( i = 0 ; i < refs.length ; i++ ) {
var opt = document.createElement ( 'option' ) ;
opt.value = i ;
opt.appendChild ( document.createTextNode ( refs[i] ) ) ;
lec_tb_ref.appendChild ( opt ) ;
}
lines = o.split("\n") ;
}
 
// Begin of text