Ki Kim
Ki Kim

Reputation: 45

CKEditor Link dialog modification

I am trying to add a drop down to CKEditor's link dialog. When selected, the drop down should insert corresponding class name to the link.

CKEDITOR.on( 'dialogDefinition', function( ev ) {
  var dialogName = ev.data.name;
  var dialogDefinition = ev.data.definition;

  if ( dialogName == 'link' ) {
    var infoTab = dialogDefinition.getContents( 'info' );

    infoTab.add({
      type: 'select',
      label: 'Display link as a button',
      id: 'buttonType',
      'default': '',
      items: [
        ['- Not button -', ''],
        ['Button one', 'btn-primary'],
        ['Button two', 'btn-success'],
        ['Button three', 'btn-danger']
      ],

      commit: function(data) {
        data.className = this.getValue();
      }
    });
  }
});

I have a feeling commit function is not doing the job, but cannot figure out how to make it work. I saw a code that almost does the same thing as I want at http://www.lxg.de/code/simplify-ckeditors-link-dialog. I tried it and it does not work either.

I am using CKEditor 4.3.2.

I appreciate your help in advance.

Upvotes: 0

Views: 1115

Answers (1)

oleq
oleq

Reputation: 15895

If you console.log the data object in link dialog's onOk, you'll find quite a different hierarchy. Element classes are in data.advanced.advCSSClasses. But even if you decide to override (or extend) the value of this property in your commit, your string will be nuked by the original commit of advCSSClasses input field ("Advanced" tab) anyway. So the approach got to be a little bit different:

  1. Always store the value of the select in data.
  2. Override commit of advCSSClasses input field to consider stored value.
  3. Remember to execute the original commit of advCSSClasses input.

Here we go:

CKEDITOR.on( 'dialogDefinition', function( ev ) {
  var dialogName = ev.data.name;
  var dialogDefinition = ev.data.definition;

  if ( dialogName == 'link' ) {
    var infoTab = dialogDefinition.getContents( 'info' ),
        advTab =  dialogDefinition.getContents( 'advanced' ),
        advCSSClasses = advTab.get( 'advCSSClasses' );

    infoTab.add( {
      type: 'select',
      label: 'Display link as a button',
      id: 'buttonType',
      'default': '',
      items: [
        ['- Not button -', ''],
        ['Button one', 'btn-primary'],
        ['Button two', 'btn-success'],
        ['Button three', 'btn-danger']
      ],

      commit: function( data ) {
        data.buttonType = this.getValue();
      }
    });

    var orgAdvCSSClassesCommit = advCSSClasses.commit;
    advCSSClasses.commit = function( data ) {
        orgAdvCSSClassesCommit.apply( this, arguments );

        if ( data.buttonType && data.advanced.advCSSClasses.indexOf( data.buttonType ) == -1 )
            data.advanced.advCSSClasses += ' ' + data.buttonType;
    };
  }
});

Now you got to only write a setup function which will detect whether one of your button classes is present to set a proper value of your select field once the dialog is open.

Upvotes: 1

Related Questions