Que al insertar una etiqueta me la inserte donde está situado el cursor, y que si no hay una selección a la que añadir la etiqueta, por ej-> <b>inserte el texto en negrita aquí</b> que el texto "inserte el texto en negrita aquí" quede seleccionado.
Mi problema es que no comprendo bien el funcionamiento del caretPos y createTextRange. Ambos me devuelven undefined aún estando alguna zona seleccionado y juraría que los nombres están correctos. les dejo aquí parte del codigo.
Formulario
Código:
Funcion storecaret<form action="" method="post" name="editor" onsubmit="return checkForm(this)" id="editor"> ..... <textarea name="message" id="message" cols="35" rows="15" wrap="VIRTUAL" style="width:359px" tabindex="3" onselect ="storeCaret(this);" onclick ="storeCaret(this);" onkeyup ="storeCaret(this);" onchange ="storeCaret(this);" ><?php if(isset($message) && strcmp($op,"prev")==0) echo $message;?> </textarea>
Código:
Función meter (la cogí de un ejemplo de este foro)// Remember the current position. function storeCaret(textEl) { // Only bother if it will be useful. if (typeof(textEl.createTextRange) != 'undefined') text.caretPos = document.selection.createRange().duplicate(); }
Código:
El resto de la información se la dejo aquí por si quieren descargarse el ejemplo completo:function meter(text) { //documento.nombre_formulario.nombre_campo var postopic = document.editor.message; if (postopic.createTextRange && postopic.caretPos) { var caretPos= postopic.caretPos; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text; } else postopic.value += text; postopic.focus(caretPos) }
Plataforma que estamos desarollando: https://swad.ugr.es
Editor en pruebas: http://quickwebdeluxe.com/editor/editor.php
Archivo JS: http://quickwebdeluxe.com/editor/funciones_editor.js
Un saludo y gracias por adelantado.
Raúl