1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
<?php
?>
<script type="text/javascript">
function wysiwyg(_name, _class, _value, _parent)
{
this.vname = _name;
this.parent = this.getNod(_parent);
this.selection = '';
this.commands_edit = { 'g' : '<strong>*</strong>', 'i' : '<i>*</i>', 'g' : '<strong>*</strong>' } ;
this.createTextarea(_class, _value);
this.createCommands();
}
wysiwyg.prototype.createTextarea = function(_class, _value)
{
this.parent.innerHTML = '<input type="hidden" name="'+this.vname+'" id="'+this.vname+'" value="'+_value+'" /><div contenteditable="true" spellcheck="false" class="'+_class+'" id="edit_'+this.vname+'">'+_value+'</div>';
}
wysiwyg.prototype.createCommands = function()
{
var commands = '';
for(var i in this.commands_edit)
{
commands += '<span id="'+i+'" class="'+i+'">'+i+'</span>' ;
}
this.parent.innerHTML = commands + this.parent.innerHTML;
this.addCallback();
}
wysiwyg.prototype.addCallback = function()
{
var object = this;
for(var i in this.commands_edit)
{
this.getNod(i).onclick = function() { object.editText(); };
}
}
wysiwyg.prototype.editText = function()
{
var textarea = this.getNod('edit_'+this.vname);
var selection;
var selection_start;
var selection_end;
var pre_str;
var ap_str;
var retour;
textarea.focus();
var value = textarea.value;
if(typeof textarea.selectionStart != 'undefined')
{
alert("ici");
selection_start = textarea.selectionStart;
selection_end = textarea.selectionEnd;
pre_str = value.substring(0 , selection_start);
ap_str = value.substring(selection_end, textarea.textLength );
selection = value.substring(selection_start, selection_end);
retour = pre_str + '<i>' + selection + '</i>' + ap_str;
}
this.getNod('edit_'+this.vname).value = retour
}
wysiwyg.prototype.getNod = function(_id)
{
return document.getElementById(_id);
}
wysiwyg.prototype.config = function()
{
}
wysiwyg.prototype.getSelected = function()
{
if(window.getSelection)
{
var str = window.getSelection();
}
else if(document.getSelection)
{
var str = document.getSelection();
}
else
{
var str = document.selection.createRange().text;
}
this.selection += str ;
}
</script>
<style type="text/css">
.descrip { width: 500px; height: 300px; border: solid 1px gray; }
</style>
<div id="wysiwyg">
</div>
<script type="text/javascript">
var wy = new wysiwyg('description', 'descrip', '', 'wysiwyg');
</script> |
Partager