Rev 15448 | Rev 17510 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
!if $wims_read_parm!=slib_header
!goto proc
!endif
slib_title=WYSIWYG code editor
slib_author=Julien, Lyotard
slib_out=Code display
slib_comment=the available themes are ambiance,neat or zenburn
slib_require=CodeMirror
slib_parms=3\
, list of codes in brackets : - in case of one code: [prog_lang,code] - else, [[prog_lang1,[code1],name_code1],[prog_lang2,[code2],name_code2], ...]\
0,id\
,option: words as init(Initialization) theme=ambiance(default) bystep=[line number of the beginning of the marker,number of the marker's start character,line number of the end of the marker,marker end character number;...] instruction=[if 7<= i <= 14 :, while n<=10 :, instruction 3 ,...]
slib_example=[python,[n=0\nwhile n<10:\n print n,n\n n=n+1]],1,readonly\
[[python,[a=23*797961\nprint a],label1],[python,[n=0\nwhile n<10:\n print n\n n=n+1],label2]],2,theme=neat\
[[python,[a=23*797961\nprint a],label1],[python,[n=0\nwhile n<10:\n print n\n n=n+1],label2]],3,init theme=zenburn\
[[python,[a=23*797961\nprint a],label1],[C,[#include <stdio.h>\n#include <stdlib.h>\nint main()\n{\nprintf(\"Hello world!\n\");\nreturn 0;}],Hello word!,readonly]],4,init\
[python,[n=0\nwhile n<10:\n print n,n\n n=n+1],readonly],5, bystep=[1,1,1,4;2,1,2,6;4,1,4,7;2,1,2,6;4,1,4,7]\
[python,[n=0\nwhile n<10:\n print n,n\n n=n+1]],6,instruction=[if 7<= i <= 14 :, while n<=10 :,instruction 3] theme=neat
/*
CodeMirror : https://codemirror.net/
Téléchargement : https://codemirror.net/codemirror.zip
**rullers : affichage comptable( à regarder)
**Completion automatique testée non implantée <= Bien
**Fullscreen testé non implanté
**sublimtext testé non implanté
**gutters<=permet de configurer les numéros de ligne+debug
**multilangage implanté.
**Buffer implanté avec l'option readonly et l'initialisation
*/
!exit
:proc
!set slib_name_init=Réinitialisation
!set slib_name_bystep=Mode pas à pas
!reset slib_readonly_list
!distribute items $(wims_read_parm) into slib_code,slib_ids,slib_option
slib_ids=!declosing $slib_ids
!distribute item $slib_ids into slib_n,slib_id
!default slib_n=0
slib_prefeditor=wims_editor
!default slib_id=$slib_prefeditor$slib_n
slib_code=!declosing $slib_code
!reset slib_theme$slib_n slib_themecss$slib_n
!if theme isin $(slib_option)
!set slib_theme$slib_n=!getopt theme in $(slib_option)
!endif
!default slib_theme$slib_n=ambiance
!if ($(slib_theme$slib_n) notitemof $(slib_listtheme)) or ($(slib_listtheme)=$empty)
!set slib_themecss$slib_n=<link rel="stylesheet" href="scripts/js/external/codemirror/theme/$(slib_theme$slib_n).css">
!endif
!set slib_listtheme=!append item $(slib_theme$slib_n) to $(slib_listtheme)
!reset slib_instruction$slib_n
!reset slib_instruction_out$slib_n
!if instruction isin $(slib_option)
!set slib_instruction$slib_n=!replace internal = by _equal_ in $(slib_option)
!set slib_instruction$slib_n=!replace internal instruction_equal_ by instruction= in $(slib_instruction$slib_n)
!set slib_instruction$slib_n=!getopt instruction in $(slib_instruction$slib_n)
!set slib_instruction$slib_n=!declosing $(slib_instruction$slib_n)
!set slib_instruction$slib_n=!replace internal _equal_ by = in $(slib_instruction$slib_n)
!set slib_instcnt=!itemcnt $(slib_instruction$slib_n)
!for slib_b =1 to $slib_instcnt
!set slib_c=!item $slib_b of $(slib_instruction$slib_n)
!set slib_instruction_out$slib_n=!append word <span onclick="insertTextAtCursor(editor$(slib_n),'$slib_c');" \
class="wims_button wims_secondary_button">$slib_c</span> to $(slib_instruction_out$slib_n)
!next
!set slib_instruction_out$slib_n=<div id="instruction_id$slib_n">$(slib_instruction_out$slib_n)</div>
!if $slib_instruction_js=$empty
slib_instruction_js=function insertTextAtCursor(editor, text) {\
var selection = editor.getSelection();\
if(selection.length>0){editor.replaceSelection(text);}\
else{var doc = editor.getDoc();\
var cursor = doc.getCursor();\
var pos = {line: cursor.line,ch: cursor.ch}\
doc.replaceRange(text, pos);}}
!endif
!endif
!reset slib_bystep
!if bystep isin $(slib_option)
slib_bystep=!getopt bystep in $(slib_option)
slib_bystep=!declosing $(slib_bystep)
slib_bystep=!rows2lines $slib_bystep
slib_bystep=!lines2items $slib_bystep
slib_bystep=!exec pari v=[$slib_bystep]; v-vector(#v,j,1)
slib_bystep_cnt=!itemcnt $(slib_bystep)
slib_bystep_cnt=$[$(slib_bystep_cnt)/4]
slib_bystep_script=<script src="scripts/js/external/codemirror/addon/edit/matchbrackets.js"></script>\
<script src="scripts/js/external/codemirror/addon/search/searchcursor.js"></script>\
<script src="scripts/js/external/codemirror/addon/selection/mark-selection.js"></script>\
<style>.marker{background-color:rgba(255, 255, 255, 1);color:black;}</style>
!endif
!if $(slib_bystep) !=$empty
slib_bystep_button=<span onclick="modebystep$(slib_n)();" id="bystep$(slib_n)" class="wims_button wims_warning" style="cursor:pointer;">$slib_name_bystep</span>
slib_bystep_fct=function modebystep$(slib_n)(){\
let click=0;\
var tab=[$(slib_bystep)];\
var lo=tab.length/4-1;\
myMarker$(slib_n).forEach(function(element,index,arr){if(typeof(element[index])=="undefined" && index!=lo){click=index+1;}else{click=0;}\
delete arr[index];\
element.clear();\
});\
myMarker$(slib_n)[click]=editor$(slib_n).markText({line: tab[click*4] , ch: tab[click*4+1]}, {line: tab[click*4+2], \
ch: tab[click*4+3]}, {className:"marker",cleaWhenEmpty:false});\
}
!else
slib_bystep_fct=$empty
slib_bystep_button=$empty
!endif
!reset slib_init
!if readonly iswordof $(slib_option)
slib_readonly=true
!reset slib_init
!else
slib_readonly=false
!if init iswordof $(slib_option)
slib_init=init
!else
!reset slib_init
!endif
!endif
!if init iswordof $(slib_option)
slib_init=init
!else
slib_init=
!endif
!default slib_code=$empty
!reset slib_code_select
!if $(slib_code)!=$empty
slib_code=!declosing $(slib_code)
!if __[ isin __$(slib_code)
slib_code_cnt=!itemcnt $(slib_code)
slib_code_select=<div><select id="select_$(slib_id)">
!else
slib_code_cnt=1
slib_code=[$slib_code]
!endif
!else
slib_code_cnt=0
!endif
!reset slib_code_s slib_code_op
!for jj=1 to $(slib_code_cnt)
!if $(slib_code_cnt)!=1
slib_code$jj=!declosing $(slib_code[$jj])
!else
slib_code$jj=!declosing $(slib_code)
!endif
!if readonly iswordof $(slib_code$jj[4])
slib_readonly$jj=true
!else
slib_readonly$jj=false
!endif
slib_readonly_list=!append item $(slib_readonly$jj) to $(slib_readonly_list)
slib_prog_lang=!append item $(slib_code$jj[1]) to $slib_prog_lang
slib_code_code$jj=!declosing $(slib_code$jj[2])
slib_name_code$jj=$(slib_code$jj[3])
slib_code_code$jj=!replace internal $ $ by \n in $(slib_code_code$jj)
slib_code_data$jj=!rows2lines $(slib_code_code$jj)
slib_code_s=!append line <option value="$(slib_name_code$jj)">$(slib_name_code$jj)</option> to $slib_code_s
slib_code_op=!append line openBuffer_$(slib_n)("$(slib_name_code$jj)","$(slib_code_data$jj)" , "$(slib_prog_lang[$jj])"); to $slib_code_op
slib_o=$slib_o\
$(slib_code_code$jj)
!next
!if $(slib_code_cnt)=1
slib_readonly_list=$(slib_readonly)
slib_readonly1=$(slib_readonly)
slib_name_code1=bidon1
slib_code_init=editor$(slib_n).setValue("$(slib_code_data1)");
!else
slib_code_init=$(slib_code_op)\
selectBuffer_$(slib_n)("$(slib_name_code1)");\
document.getElementById("select_$(slib_id)").options[0].selected = true;
!endif
!if init iswordof $(slib_option)
slib_init_button=<span onclick="initialisation$(slib_n)();" class="wims_button" style="cursor:pointer;">$slib_name_init</span>
slib_init_fct= function initialisation$(slib_n)(){ $(slib_code_init)}
!else
slib_init_button=$empty
slib_init_fct=$empty
!endif
slib_prog_lang_uniq=!listuniq $(slib_prog_lang)
!for nimp in $(slib_prog_lang_uniq)
slib_scripts_lang=!append line <script src="scripts/js/external/codemirror/mode/$nimp/$nimp.js"></script> to $(slib_scripts_lang)
!next
!reset slib_code_select_script
!if $(slib_code_cnt)!=1
slib_code_select=$(slib_code_select)\
$(slib_code_s)\
</select></div>
slib_code_select_script= var list_$(slib_n)=[$(slib_readonly_list)];\
var sel_$(slib_n)=document.getElementById("select_$(slib_id)");\
CodeMirror.on(sel_$(slib_n), "change", function() {\
selectBuffer_$(slib_n)(sel_$(slib_n).options[ sel_$(slib_n).selectedIndex].value);\
editor$(slib_n).setOption("readOnly",list_$(slib_n)[sel_$(slib_n).selectedIndex]);\
});\
var buffers_$(slib_n) = {};\
function openBuffer_$(slib_n)(name, text, mode) {buffers_$(slib_n)[name] = CodeMirror.Doc(text, mode);}\
function selectBuffer_$(slib_n)(name) {\
var buf = buffers_$(slib_n)[name];\
if (buf.getEditor()) buf = buf.linkedDoc({sharedHist: true});\
var old = editor$(slib_n).swapDoc(buf);\
var linked = old.iterLinkedDocs(function(doc) {linked = doc;});\
if (linked) {\
for (var name in buffers_$(slib_n)) if (buffers_$(slib_n)[name] == old) buffers_$(slib_n)[name] = linked;\
old.unlinkDoc(linked);\
}\
editor$(slib_n).focus();}
!else
slib_code_op=
!endif
slib_code_select_script= $(slib_code_select_script)\
$(slib_code_op)\
var editor$(slib_n) = CodeMirror(document.getElementById("$(slib_n)"), {\
lineNumbers: true,\
theme:"$(slib_theme$slib_n)",\
matchBrackets: true,\
mode: "$(slib_prog_lang[1])",\
readOnly:$(slib_readonly1),\
styleSelectedText: true,\
viewportMargin: 10,\
indentUnit:2,\
autocapitalize:true\
});
!if $(slib_code_cnt)!=1
slib_code_select_script= $(slib_code_select_script)\
selectBuffer_$(slib_n)("$(slib_name_code1)");
!else
slib_code_select_script= $(slib_code_select_script)\
editor$(slib_n).setValue("$(slib_code_data1)");
!endif
!if $slib_code_js=$empty
slib_code_js=<link rel="stylesheet" href="scripts/js/external/codemirror/codemirror.css">\
<script src="scripts/js/external/codemirror/codemirror.js"></script>\
$(slib_scripts_lang)
slib_out=$slib_code_js
!else
slib_out=
!endif
!if $(slib_bystep_script)!=$empty
slib_out=$slib_out\
$(slib_bystep_script)
!endif
slib_out=$(slib_themecss$slib_n)\
$slib_out\
<div class="$slib_prefeditor" id="$slib_id">\
$(slib_init_button)\
$(slib_bystep_button)\
$(slib_code_select)\
<div id="$(slib_n)"></div>\
$(slib_instruction_out$slib_n)\
</div>\
<script>\
$(slib_code_select_script)\
$(slib_init_fct)\
var myMarker$(slib_n)=[];\
$(slib_bystep_fct)\
$(slib_instruction_js)\
</script>