views:

78

answers:

2

I'm trying to write a function that allows a contenteditable div to do some auto formatting while the user is typing in the div. So far I only manage to make it work in IE. Anyone can help me?

function formatOnKeyUp(){
    if (window.getSelection) {
        // ???????
    } else if (document.selection) {
        cursorPos=document.selection.createRange().duplicate();
        clickx = cursorPos.getBoundingClientRect().left; 
        clicky = cursorPos.getBoundingClientRect().top;
    }

    text = document.getElementById('div1').innerHTML;
    text = text.replace(/this/gm, "<i>this</i>");
    // .... some other formating here...
    document.getElementById('div1').innerHTML = text;

    if (window.getSelection) {
        // ????????
    } else if (document.selection) {
        cursorPos = document.body.createTextRange();
        cursorPos.moveToPoint(clickx, clicky);
        cursorPos.select();
    }
}
A: 

The cursor position and text range stuff looks particular to Microsoft's JScript.

If you're replacing text as someone types, why do you need that code?

mqsoh
Because I need to format the typed text. I'm writing something like a html editor. When someone type, say <title>, I want that to be in different color the moment the user finish typing it <title>.
Kelly Lim
+1  A: 

You could use the selection save and restore module in my Rangy library, which uses invisible marker elements at the selection boundaries. I'd also suggest doing the replacement after a certain period of keboard inactivity rather than on every keyup event:

function formatText(el) {
    // Save the selection
    var savedSel = rangy.saveSelection();

    // Do your formatting here
    var text = el.innerHTML.replace(/this/gm, "<i>this</i>");
    el.innerHTML = text;

    // Restore the original selection 
    rangy.restoreSelection(savedSel);
}

var keyTimer = null, keyDelay = 500;

document.getElementById('div1').onkeyup = function() {
    if (keyTimer) {
        window.clearTimeout(keyTimer);
    }
    window.setTimeout(function() {
        keyTimer = null;
        formatText(document.getElementById('div1'));
    }, keyDelay);
};
Tim Down
It works! Millions of thanks.
Kelly Lim
Looks nice, your library!
Marcel Korpel
@Marcel: Thanks :) Just need to finish documenting and testing the stupid thing now.
Tim Down
Hi Tim, I suspect there are some bugs in the Firefox version. Is there any channel I can feedback my findings to you?
Kelly Lim
Ok, I hope this helps. In Firefox, the script is unable to render the following situation: This is <font color="#00ffff"><span id="selectionBoundary_1288358283457_7547406630610741"></span>some</font> <b>area</b> to type.
Kelly Lim
Notice that your proprietary span tag is right after the <font> tag. Similar problem arise for <b>. The error message is "Index or size is negative or greater than the allowed amount" code: "1[Break on this error] var endNode = ec.childNodes[eo - 1];"
Kelly Lim
@Kelly: The ideal would be to submit a bug on the project bug tracker: http://code.google.com/p/rangy/issues/list. I'll take a look as soon as I can.
Tim Down
You're right, there's a problem there. I'll fix it asap.
Tim Down
@Kelly: I've fixed the problem and uploaded a new version.
Tim Down
Found another potential bug, but it's a complicated one. I've created an issue in Google as per your instruction. I've also added a script for you to recreate the problem. Hope it helps. Thanks.
Kelly Lim