Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts

    How to move boundaries of a selection?

    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".

    Code:
    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:



    But what I WANT is this:



    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
    Last edited by Krupski; 12-27-2010 at 03:08 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •