0

I met some trouble with javascript and ckeditor.

I've done a function that will replace in a textarea all abreviations by their expression, for example tel -> telephone, etc....

The trouble is with ckeditor. on a normal textarea this function works fine, like that:

var abbreviations = {
    <?php $rqt="SELECT * FROM `glossaire` WHERE `id_company` ='".$societe['id']."'";
    $result=mysql_query($rqt);
    while($data=mysql_fetch_assoc($result))
    {?>
    "<?php echo ($data['libelle']) ; ?>": "<?php echo ($data['texte']) ; ?>",
     <?php } ?>    



};

function abbrReplace(el, abbrs) {
    if (!el || !abbrs) {
        return false;
    }
    else {
        var val = el.value,
            words = val.split(/\s/);
        for (var i = 0, len = words.length; i<len; i++) {
            if (abbrs[words[i]]) {
                words[i] = abbrs[words[i]];
            }
        }
        return words.join(' ');
    }
}

document.getElementById("suivi").onkeyup = function(){
    this.value = abbrReplace(this, abbreviations);
};

So on ckeditor, I've tried this : CKEDITOR.instances.commentaire.insertText('abbreviations');

Like that:

var abbreviations = {
    <?php $rqt="SELECT * FROM `glossaire` WHERE `id_company` ='".$societe['id']."'";
    $result=mysql_query($rqt);
    while($data=mysql_fetch_assoc($result))
    {?>
    "<?php echo ($data['libelle']) ; ?>": "<?php echo ($data['texte']) ; ?>",
     <?php } ?>    



};

function abbrReplace(el, abbrs) {
    if (!el || !abbrs) {
        return false;
    }
    else {
        var val = el.value,
            words = val.split(/\s/);
        for (var i = 0, len = words.length; i<len; i++) {
            if (abbrs[words[i]]) {
                words[i] = abbrs[words[i]];
            }
        }
        return words.join(' ');
    }
}

document.getElementById("suivi").onkeyup = function(){
    this.value = CKEDITOR.instances.commentaire.insertText('abbreviations');
};

I'm really lost, on ckeditor it does not work,

I think I do something wrong, butg I do not know how to correct it.

Anykind of help will be much appreciated.

Kind Regards.

SP.

Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59

1 Answers1

1

http://jsfiddle.net/hooy/DU83p/ — here is working sample.

to bind onKeyUp function on CKEDITOR you cat use ckeditor.on('key', someFunction);​

Also, setFocusToEnd function is necessary because of How to set cursor position to end of text in CKEditor?

Community
  • 1
  • 1
maxwell
  • 857
  • 8
  • 16