Ver Mensaje Individual
  #13 (permalink)  
Antiguo 06/08/2007, 13:35
Avatar de Adler
Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 19 años, 9 meses
Puntos: 126
Re: Problemas con bbcode en textarea

Hola, en estos días de vacaciones he terminado este código para editar texto. Aún no está lo suficientemente probado, pero las prubas que he hecho en IE 6 y 7, FF 2 y opera 9 han funcionado. Este último con algunas limitaciones. Pruebalo y ya me diras.

Esta es la primera parte, la segunda está en el siguiente post.

Código PHP:
<html>
<
head>
<
title></title>
<!
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
script language="JavaScript" type="text/javascript">
// Comprueba Navegador y Plataforma del pc
var clientPC = navigator.userAgent.toLowerCase(); // Coge info cliente
var clientVer = parseInt(navigator.appVersion); // Coge versión navegador
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var 
is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (
clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (
clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var 
is_moz = 0;
var 
is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var 
is_mac = (clientPC.indexOf("mac")!=-1);
var 
valor_boton = new Array('Arial','Times New Roman','Verdana','9','10','11','#ff0000','#000000','#ffffff','left','center','right');
var 
recortar_posAtras = new Array('7','7','7','7','7','7','8','8','8','8','8','8','4','4','4');
var 
num_caracteres = new Array('19','29','21','15','16','16','23','23','23','20','22','21','7','7','7','12');
var 
num_caracteresAtras = new Array('12','22','14','8','9','9','15','15','15','12','14','13','4','4','4');
var 
valor_tag = new Array('FACE','SIZE','COLOR','ALIGN','B','U','I','URL');
// Insertar vinculo
function inserta_url(boton,tag) {
var 
txtarea = document.getElementById("firma");
var 
reg = new RegExp();
reg =  "(www+\.tuweb+\.com)([\/\?\._-]+\w*)";
var 
vinculo = null;
vinculo= prompt("Ingresa una dirección","http://");
var 
IniCursor = 0
if ((clientVer >= 4) && is_ie && is_win) {
var 
theSelection = document.selection.createRange().text;
 
 if (
document.selection) {
  if (!
theSelection) {
   if ((
vinculo==null) || (vinculo== "undefined") || (vinculo=="") || (vinculo==' ') || (vinculo =="http://")) {
   
txtarea.focus(); 
   var 
Sel = document.selection.createRange ();
   
Sel.moveStart ('character', -txtarea.value.length);
   
iniCursor = Sel.text.length; //caracteres que deja atras
   
alert("No has introducido una dirección válida");
   return(
iniCursor);
   }
   else if (
vinculo.match(reg)) {
   
txtarea.focus();
   var 
Sel = document.selection.createRange ();
   
document.selection.createRange().text = "[" + valor_tag[tag] + "=" + vinculo + "]" + vinculo.substring(7,vinculo.length) +"[/" + valor_tag[tag] + "]";
   
Sel.moveStart ('character', -txtarea.value.length);
   
iniCursor = Sel.text.length; //caracteres que deja atras
   
var  sumarPos= parseInt(num_caracteres[boton]);
   var 
LargoVinHTTP = vinculo.length;
   var 
LargoVin = vinculo.length -7; // Largo del vinculo sin http://
   
var Cursor = iniCursor +  sumarPos + LargoVinHTTP + LargoVin;
   return(
Cursor);
   }
   else {
   
txtarea.focus(); 
   var 
Sel = document.selection.createRange ();
   
Sel.moveStart ('character', -txtarea.value.length);
   
iniCursor = Sel.text.length; //caracteres que deja atras
   
alert("Solo se admiten vinculos directos que apunten hacía esta página");
   return(
iniCursor);
   }
  }
  if (
theSelection) {
   if ((
vinculo==null) || (vinculo== "undefined") || (vinculo=="") || (vinculo==' ') || (vinculo =="http://")) {
   
txtarea.focus(); 
   var 
Sel = document.selection.createRange ();
   
Sel.moveStart ('character', -txtarea.value.length);
   
iniCursor = Sel.text.length; //caracteres que deja atras
   
alert("No has introducido una dirección válida");
   return(
iniCursor);
   }
   else if (
vinculo.match(reg)) {
   
txtarea.focus();
   var 
Sel = document.selection.createRange ();
   
document.selection.createRange().text = "[" + valor_tag[tag] + "=" + vinculo + "]" + theSelection +"[/" + valor_tag[tag] + "]";
   
Sel.moveStart ('character', -txtarea.value.length);
   
iniCursor = Sel.text.length; //caracteres que deja atras
   
var  sumarPos= parseInt(num_caracteres[boton]);
   var 
LargoVinHTTP = vinculo.length;
   var 
LargoSeleccion = theSelection.length;
   var 
Cursor = iniCursor +  sumarPos + LargoVinHTTP + LargoSeleccion;
   return(
Cursor);
   }
   else {
   
txtarea.focus(); 
   var 
Sel = document.selection.createRange ();
   
Sel.moveStart ('character', -txtarea.value.length);
   
iniCursor = Sel.text.length; //caracteres que deja atras
   
alert("Solo se admiten vinculos directos que apunten hacía esta página");
   return(
iniCursor);
   }
  }
 }
}
else if (
txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
{
 var 
selLargo = txtarea.textLength;
 var 
selEmpz = txtarea.selectionStart;
 var 
selFin = txtarea.selectionEnd;
 var 
s1 = (txtarea.value).substring(0,selEmpz);
 var 
s2 = (txtarea.value).substring(selEmpz, selFin);
 var 
s3 = (txtarea.value).substring(selFin, selLargo);
 var 
iniText = s1.length;
 var 
textSel = s2.length;
 
 if ((
vinculo==null) || (vinculo== "undefined") || (vinculo=="") || (vinculo==' ') || (vinculo =="http://")) {
 var 
Cursor = iniText + textSel;
 
alert("No has introducido una dirección válida");
 return(
Cursor);
 }
 else if (
vinculo.match(reg)) {
 var 
vinLength = vinculo.length;
 var 
sumarPos= parseInt(num_caracteres[boton]);
 
txtarea.value =  s1 + "[" + valor_tag[tag] + "=" +vinculo+ "]" +s2 + "[/" +valor_tag[tag] + "]" + s3;
 var 
Cursor = iniText + textSel + vinLength + sumarPos;
 return(
Cursor);
 }
 else {
 var 
Cursor = iniText;
 
alert("Solo se admiten vinculos directos que apunten hacía esta página");
 return(
Cursor);
 } 
}
else
{
 if (
txtarea.selectionStart || txtarea.selectionStart == '0') {
 var 
selLargo = txtarea.textLength;
 var 
selEmpz = txtarea.selectionStart;
 var 
selFin = txtarea.selectionEnd;
 var 
s1 = (txtarea.value).substring(0,selEmpz);
 var 
s2 = (txtarea.value).substring(selEmpz, selFin);
 var 
s3 = (txtarea.value).substring(selFin, selLargo);
 var 
iniText = s1.length;
  if ((
vinculo==null) || (vinculo== "undefined") || (vinculo=="") || (vinculo==' ') || (vinculo =="http://")) {
  var 
Cursor = iniText;
  
alert("No has introducido una dirección válida");
  return(
Cursor);
  }
  else if (
vinculo.match(reg)) {
  var 
vinLength = vinculo.length;
  var 
sumarPos= parseInt(num_caracteres[boton]);
  
txtarea.value =  s1 + "[" + valor_tag[tag] + "=" +vinculo+ "]" + vinculo.substring(7,vinculo.length) + "[/" +valor_tag[tag] + "]" + s3;
  var 
Cursor = iniText + vinLength + vinLength + sumarPos - 7;
  return(
Cursor); 
  }
  else {
  var 
Cursor = iniText;
  
alert("Solo se admiten vinculos directos que apunten hacía esta página");
  return(
Cursor);
  }
 }
 }
 
almznaCursor(txtarea);
}
 
 
//Lista Ordenada
function listaord () {
var 
txtarea = document.getElementById("firma");
var 
opcion= new Array();
var 
i,j
var suma = 0
if ((clientVer >= 4) && is_ie && is_win) {
var 
theSelection = document.selection.createRange().text;
 
 if (
document.selection) {
  if (!
theSelection) {
  
txtarea.focus();
  var 
Sel = document.selection.createRange ();
  var 
PosSel1 = Sel.moveStart ('character', -txtarea.value.length); 
  var 
SelLargo = txtarea.value.length;
  
// La linea siguiente es necesaria por que -- PosSel1 -- da un valor negativo
  
var ValorPositivo = PosSel1 + SelLargo + 1
  
var SelEmpz = SelLargo - ValorPositivo
  
var PosSel2 = Sel.moveStart ('character', -txtarea.value.length); 
  var 
SelFin = SelLargo - PosSel2
  
var s1 = (txtarea.value).substring(0,SelEmpz);
  var 
s2 = (txtarea.value).substring(SelEmpz,SelFin);
  
//var s3 = (txtarea.value).substring(selFin, selLargo);
  
var abre = "[list=1]\n";
  var 
cierra = "[/LIST=]";
  
txtarea.value =  s1 +  abre + cierra + s2;
  for (
i=0; i<=opcion.length; i++) {
  
opcion[i]=prompt("Ingresa los elementos de la lista de uno en uno","");
  if ((
opcion[i]== null) || (opcion[i]== "undefined") || (opcion[i] == "") || (opcion[i] ==' ')) {
  var 
Cursor =  s1.length + abre.length + suma + cierra.length;
  return(
Cursor); 
  }
  
txtarea.value =  s1 +  abre;
  for (
j=0; j<=i; j++) {
  
txtarea.value +=  "[*]" + opcion[j] + "\n"; 
  }
  
txtarea.value += cierra + s2;
  
suma += (opcion[i].length+4);
  }
 }
 
  if (
theSelection) {
  
txtarea.focus();
  var 
Sel = document.selection.createRange ();
  var 
PosSel1 = Sel.moveStart ('character', -txtarea.value.length); 
  var 
SelLargo = txtarea.value.length;
  
// La linea siguiente es necesaria por que -- PosSel1 -- da un valor negativo
  
var ValorPositivo = PosSel1 + SelLargo + 1
  
var SelEmpz = SelLargo - ValorPositivo
  
var s1 = (txtarea.value).substring(0,SelEmpz);
  var 
selFin = s1.length + theSelection.length
  
var s2 = (txtarea.value).substring(selFin, selLargo);
  var 
abre = "[list=1]\n";
  var 
cierra = "[/LIST=]";
  
txtarea.value =  s1 + abre;
  
opcion = theSelection.split(" ");
  for (
j=0; j<opcion.length; j++) {
  
txtarea.value +=  "[*]" + opcion[j] + "\n"; 
  
suma += (opcion[j].length+4);
  }
  
txtarea.value += cierra + s2;
  var 
Cursor =  s1.length + abre.length + suma + cierra.length;
  return(
Cursor); 
  }
 }
}
 
 
else if (
txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
{
 var 
selLargo = txtarea.textLength;
 var 
selEmpz = txtarea.selectionStart;
 var 
selFin = txtarea.selectionEnd;
 var 
s1 = (txtarea.value).substring(0,selEmpz);
 var 
s2 = (txtarea.value).substring(selEmpz, selFin);
 var 
s3 = (txtarea.value).substring(selFin, selLargo);
 var 
abre = "[list=1]\n";
 var 
cierra = "[/LIST=]";
 
opcion = s2.split(" ");
 
txtarea.value =  s1 +  abre;
 for (
j=0; j<opcion.length; j++) {
 
txtarea.value +=  "[*]" + opcion[j] + "\n"; 
 
suma += (opcion[j].length+4);
 }
 
txtarea.value += cierra + s3;
 var 
Cursor =  s1.length + abre.length +  suma + cierra.length;
 return(
Cursor); 
}
else
{
 var 
selLargo = txtarea.textLength;
 var 
selEmpz = txtarea.selectionStart;
 var 
selFin = txtarea.selectionEnd; 
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />

Última edición por Adler; 07/08/2007 a las 13:11