Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Good

JS de BBCode com bugs

Recommended Posts

Eu tenho um arquivo JS do bbcode, e ele está com alguns erros:

 

1- Ele está com erros no IE.

2- Ele está com problema ao clicar no botão para adicionar a tag.

 

Exemplo do erro 2:

 

[|u][|u]MEU TEXTO A[|/u]QUI[|/u]

 

[|i][|i]AAAAAAAAAAAAAA[|/i]AAA[|/i]

 

 

ATENÇÂO: Eu coloquei o "|" para a tag não ser usada.

 

Código do arquivo JS:

$(document).ready(function() {

	var campo_texto = document.getElementById("texto");
	var selecao;
	var selecao_inicio;
	var selecao_fim
	var texto;

	$(".btt_negrito").click(function() {
		selecao = altera_selecao("[b]", "[/b]");
		campo_texto.value = selecao;
	});
	
	$(".btt_italico").click(function() {
		selecao = altera_selecao("[i]", "[/i]");
		campo_texto.value = selecao;
	});
	
	$(".btt_sublinhado").click(function() {
		selecao = altera_selecao("[u]", "[/u]");
		campo_texto.value = selecao;
	});
	
	$(".btt_tachado").click(function() {
		selecao = altera_selecao("[s]", "[/s]");
		campo_texto.value = selecao;
	});
	
	$(".btt_img").click(function() {
		selecao = altera_selecao("[img=", "]");
		campo_texto.value = selecao;
	});
	
	$(".btt_url").click(function() {
		selecao = altera_selecao("[url]", "[/url]");
		campo_texto.value = selecao;
	});
	
	$(".btt_quote").click(function() {
		selecao = altera_selecao("[quote]", "[/quote]");
		campo_texto.value = selecao;
	});
	
	$(".btt_youtube").click(function() {
		selecao = altera_selecao("[youtube]", "[/youtube]");
		campo_texto.value = selecao;
	});
	
	function altera_selecao(tag_inicio, tag_fim) {
		
		texto = campo_texto.value;
		
		if (campo_texto.selectionEnd) {
			selecao_inicio = campo_texto.selectionStart;
			selecao_fim = campo_texto.selectionEnd;
		} else if (document.selection) {
			// IE - Com bug, necessita correção
            var txtarea = campo_texto;
			var range = document.selection.createRange();
            var stored_range = range.duplicate();
            stored_range.moveToElementText(range);
            stored_range.setEndPoint('EndToEnd', range);
            txtarea.selectionStart = stored_range.text.length - range.text.length;
            txtarea.selectionEnd = txtarea.selectionStart + range.text.length;

            selecao_inicio = txtarea.selectionStart;
            selecao_fim = txtarea.selectionEnd;
            
		}
		
		novo_texto = texto.substring(0, selecao_inicio);
		novo_texto += tag_inicio + texto.substring(selecao_inicio, selecao_fim) + tag_fim;
		novo_texto += texto.substring(selecao_fim, texto.length);
		
		$("#reverter_acao").attr("disabled", false);

        return novo_texto;
		
	}

});

Compartilhar este post


Link para o post
Compartilhar em outros sites

Num tem ninguém pra dar uma revisada neste código não?

 

Pq ele está dando problemas, e preciso muito.

 

Obrigado.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.