0
votes

I am trying to build a small plugin for CKeditor. I want to add a function that when I press one the the generated buttons that in the addcommand function the Lable for that button gets printed to the editor

example : if List array contains "Name", "State", "country" so this plugin code below will add 3 button to the menu called "Name", "State" and "country" if I click anyone of these buttons they will activate insertMM command , I want that command to do a inserthtml with the string "you pressed [Label]" so if I pressed "Name" button it would print "you pressed Name" in the Editor window

CKEDITOR.plugins.add( 'MM', {
    init: function( editor ) {
        editor.addCommand( 'insertMM', {
            exec: function( editor) {
                editor.insertHtml( "you pressed "+ {{ADD LABEL STRING HERE}} );
            }
        });
        for (i = 0; i < List.length; i++) { 
            editor.ui.addButton( List[i], {
                label: List[i], 
                command: 'insertMM',
                toolbar: 'insert'
            });
        }   
    }
});

so the question is, is there some way of knowing which button is pressed and getting its details when running the addcommand or is there a way of passing a know value to the addcommmand when the button is pushed

1
Do you mean that you want a new button that when clicked prints its own label or do you want a new plugin that prints the label of any button whenever it is pressed? Reread the question and maybe try to clarify a little :) - Joel Peltonen

1 Answers

3
votes

Yo got to create a separate command for each button, i.e.

function genCommand( name ) {
    return {
        exec: function( editor ) {
            editor.insertHtml( "you pressed " + name );
        }
    };  
}

editor.addCommand( 'insertMMfoo', genCommand( 'foo' ) );
editor.addCommand( 'insertMMbar', genCommand( 'bar' ) );

editor.ui.addButton( List[i], {
    label: List[i], 
    command: 'insertMMfoo',
    toolbar: 'insert'
});

I'm not quite sure what is in List array, but it could (actually, should) look like this:

function genCommand( name ) {
    return {
        exec: function( editor ) {
            editor.insertHtml( "you pressed " + name );
        }
    };  
}

for (i = 0; i < List.length; i++) { 
    var commandName = 'insertMM' + i;

    editor.addCommand( commandName, genCommand( i ) );

    editor.ui.addButton( List[i], {
        label: List[i], 
        command: commandName,
        toolbar: 'insert'
    });
}