Dopo un sacco di ricerca ed un sacco di studio sono riuscito a trovare e modificare questo script...
Codice PHP:
System.useCodepage = true;
my_txt = prova;
my_txt_simple = provatesto;
my_txt.border = true;
my_txt.wordWrap = true;
my_txt.multiline = true;
my_txt.type = "input";
my_txt.html = true;
//CSS
//var styles:TextField.StyleSheet = new TextField.StyleSheet();
//styles.onLoad = function(success:Boolean):Void {
// if (success) {
// visualizza i nomi stile.
//trace(this.getStyleNames());
//my_txt.styleSheet = styles;
//} else {
//trace("Error loading CSS file.");
//}
//};
//styles.load("html_styles.css");
my_txt.htmlText = "[url='#']Prova di formattazione[/url] speriamo[b]che funzioni tutto[/b] mamma mia...";
var my_cm:ContextMenu = new ContextMenu();
my_cm.customItems.push(new ContextMenuItem("Grassetto", formattaHTML));
my_cm.customItems.push(new ContextMenuItem("Corsivo", formattaHTML));
my_cm.customItems.push(new ContextMenuItem("Sottolineato", formattaHTML));
my_cm.customItems.push(new ContextMenuItem("Link", formattaHTML));
//my_cm.customItems.push(new ContextMenuItem("Inserisci immagine...", formattaHTML));
function formattaHTML(obj:Object, menuItem:ContextMenuItem) {
var mnuItem:String = menuItem.caption;
var testoHtml:String = my_txt.htmlText;
var testo:String = my_txt.text;
var ltxth:Number = testoHtml.length;
var ltxt:Number = testo.length;
var inizioSel:Number = Selection.getBeginIndex();//Inizio selezione;
var fineSel:Number = Selection.getEndIndex();//Fine selezione;
var selezione:String = testo.substring(inizioSel,fineSel);//testo selezionato NON HTML
var inizioSelh:Number=testoHtml.indexOf(selezione);//Inizio selezione nel testo HTML
var fineSelh:Number=inizioSelh+selezione.length;
var selezioneh:String = testoHtml.substring(inizioSelh,fineSelh)//testo selezionato HTML
var prima:String = testoHtml.substring(0,inizioSelh);//Testo HTML prima della selezione
var dopo:String = testoHtml.substring(fineSelh);//Testo HTML dopo della selezione
switch (mnuItem){
case "Sottolineato":
currentFormat = my_txt.getTextFormat(inizioSel, fineSel);
if (currentFormat.underline == true) {
tagA="</u>";
tagB="<u>";
} else {
tagA="<u>";
tagB="</u>";
}
break;
case "Corsivo":
currentFormat = my_txt.getTextFormat(inizioSel, fineSel);
if (currentFormat.italic == true) {
tagA="[/i]";
tagB="[i]";
} else {
tagA="[i]";
tagB="[/i]";
}
break;
case "Link":
currentFormat = my_txt.getTextFormat(inizioSel, fineSel);
if (currentFormat.underline == true) {
tagA="";
tagB="";
} else {
tagA="<a href='testo da apporre' target='_blank'>";
tagB="</a>";
}
break;
case "Grassetto":
currentFormat = my_txt.getTextFormat(inizioSel, fineSel);
if (currentFormat.bold == true) {
tagA="[/b]";
tagB="[b]";
} else {
tagA="[b]";
tagB="[/b]";
}
break;
}
my_txt.htmlText = prima + tagA + selezioneh + tagB + dopo;
my_txt_simple.text = my_txt.htmlText;
}
my_txt.menu = my_cm;
Riuscirei a fare quello che mi serve, ma ho visto che a volte va, a volta fa casino... per esempio:
se il testo è:
e cioè: "Prova di formattazione speriamo che funzioni tutto mamma mia..."
se seleziono precisamente "che funzioni tutto " (con lo spazio alla fine) e do un comando qualsiasi (o bold di nuovo, o sottoelineato per esempio) mi scompare tutto il testo della textarea.
E lo stesso succede se seleziono "Prova di formattazione s"
Mentre se sto molto attento a quello che clicco, con molto ordine, per il 95% dei casi funziona... perchè e come risolvo?