Skip to content

Commit

Permalink
Changed easyimage_class config option, so that it supports null (actu…
Browse files Browse the repository at this point in the history
…ally any falsy value).

Setting null should cause any figure to be upcasted into a Easy Image widget.
  • Loading branch information
mlewand authored and Comandeer committed Feb 6, 2018
1 parent 71e311b commit cd82217
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
64 changes: 37 additions & 27 deletions plugins/easyimage/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,45 @@
}

function registerWidget( editor ) {
var config = editor.config;
var config = editor.config,
figureClass = config.easyimage_class,
widgetDefinition = {
allowedContent: {
figure: {
classes: config.easyimage_sideClass
}
},

CKEDITOR.plugins.imagebase.addImageWidget( editor, 'easyimage', {
allowedContent: {
figure: {
classes: '!' + config.easyimage_class + ',' + config.easyimage_sideClass
}
},
requiredContent: 'figure',

requiredContent: 'figure(!' + config.easyimage_class + ')',
upcasts: {
figure: function( element ) {
// http://dev.ckeditor.com/ticket/11110 Don't initialize on pasted fake objects.
if ( element.attributes[ 'data-cke-realelement' ] ) {
return;
}

upcasts: {
figure: function( element ) {
// http://dev.ckeditor.com/ticket/11110 Don't initialize on pasted fake objects.
if ( element.attributes[ 'data-cke-realelement' ] ) {
return;
if ( element.name === 'figure' && ( !figureClass || element.hasClass( figureClass ) ) ) {
return element;
}
}
},

if ( element.name === 'figure' && element.hasClass( config.easyimage_class ) ) {
return element;
}
init: function() {
this.on( 'contextMenu', function( evt ) {
evt.data.easyimageFull = editor.getCommand( 'easyimageFull' ).state;
evt.data.easyimageSide = editor.getCommand( 'easyimageSide' ).state;
evt.data.easyimageAlt = editor.getCommand( 'easyimageAlt' ).state;
} );
}
},
};

init: function() {
this.on( 'contextMenu', function( evt ) {
evt.data.easyimageFull = editor.getCommand( 'easyimageFull' ).state;
evt.data.easyimageSide = editor.getCommand( 'easyimageSide' ).state;
evt.data.easyimageAlt = editor.getCommand( 'easyimageAlt' ).state;
} );
}
} );
if ( figureClass ) {
widgetDefinition.requiredContent += '(!' + figureClass + ')';
widgetDefinition.allowedContent.figure.classes = '!' + figureClass + ',' + widgetDefinition.allowedContent.figure.classes;
}

CKEDITOR.plugins.imagebase.addImageWidget( editor, 'easyimage', widgetDefinition );
}

function loadStyles( editor, plugin ) {
Expand Down Expand Up @@ -156,13 +163,16 @@
} );

/**
* A CSS class applied to all Easy Image widgets
* A CSS class applied to all Easy Image widgets. If set to `null` all `<figure>` elements are converted into widgets.
*
* // Changes the class to "my-image".
* config.easyimage_class = 'my-image';
*
* // This will cause plugin to convert any figure into a widget.
* config.easyimage_class = null;
*
* @since 4.8.0
* @cfg {String} [easyimage_class='easyimage']
* @cfg {String/null} [easyimage_class='easyimage']
* @member CKEDITOR.config
*/
CKEDITOR.config.easyimage_class = 'easyimage';
Expand Down
21 changes: 21 additions & 0 deletions tests/plugins/easyimage/manual/figuresnoclass.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

<div id="classic">
<p>Both figures should become widgets:</p>
<figure class="easyimage">
<img src="../../image2/_assets/foo.png" alt="foo">
<figcaption>Test image</figcaption>
</figure>
<p>Figure two:</p>
<figure>
<img src="../../image2/_assets/foo.png" alt="foo">
<figcaption>Test image</figcaption>
</figure>
</div>

<script>
CKEDITOR.replace( 'classic', {
easyimage_class: null,
height: 500,
extraAllowedContent: 'figure(*)'
} );
</script>
13 changes: 13 additions & 0 deletions tests/plugins/easyimage/manual/figuresnoclass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@bender-tags: 4.8.0, feature, config, 932
@bender-ui: collapsed
@bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, easyimage, htmlwriter, elementspath

`config.easyimage_class` customization

1. Check if both figures become widgets.
1. Click "Source" button to check output.

## Expected

* First `<figure>` element has `easyimage` class.
* Second `<figure>` element has no class.

0 comments on commit cd82217

Please sign in to comment.