...

View Full Version : How to move boundaries of a selection?



Krupski
12-27-2010, 04:05 PM
Hi all,

I'm trying to do something with a WYSIWYG editor. Below is a piece of code that takes selected text and wraps various BBCodes around it. The code almost works... but I want a few changes and I hope someone has an idea for me.

Note: the var range = ed.selection.getRng(true); line gets a W3C compatible range containing the current selection - even under MSIE. The editor takes care of this.
Note: The var elem = ed.dom.create('span', false, ' '); line creates a SPAN element with the initial content " " and returns it's element in "elem".



var ed = (tinymce.activeEditor || opener.tinymce.activeEditor);
if(ed)
{
var sel = ed.selection.getContent();
var range = ed.selection.getRng(true);
var elem = ed.dom.create('span', false, ' ');
elem.innerHTML = bbopen + sel + bbclose;
range.deleteContents();
range.insertNode(elem);
ed.selection.select(elem);
ed.focus();
return;
}
This code works, but not exactly as I want. For example, if I type "This is a test" and wrap code tags around it, I get this:

http://three-dog.homelinux.com/images/selection0.jpg

But what I WANT is this:

http://three-dog.homelinux.com/images/selection1.jpg

Also, if I select no text and hit the BBCode button, I get the open tag and the close tag, all highlighted.

What I want in this case is the open tag, the close tag and the cursor IN BETWEEN them ready for text insertion.

I'm thinking that I can take the range start and end offsets and move them to get what I want, but so far I've failed.

One thing I DON'T want to have to do is create THREE spans... one for the open, one for the selection and one for the close. Trying to avoid that if possible.

Any help will be greatly appreciated!

Thanks!

-- Roger



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum