X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fplugins%2Fimage%2Fplugin.js;h=f3c6d6dc1706590238ec6eb03d4601a94d899f3a;hp=7285b0fade645479550663ad4abd805e8302e4e9;hb=2f22c0c38f17e75be5541089076885442aaa2377;hpb=e73319a12b56100b29ef456fd74114fe5519e01c diff --git a/_source/plugins/image/plugin.js b/_source/plugins/image/plugin.js index 7285b0f..f3c6d6d 100644 --- a/_source/plugins/image/plugin.js +++ b/_source/plugins/image/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -7,6 +7,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license * @file Image plugin */ +(function() +{ + CKEDITOR.plugins.add( 'image', { init : function( editor ) @@ -53,14 +56,94 @@ CKEDITOR.plugins.add( 'image', { editor.contextMenu.addListener( function( element, selection ) { - if ( !element || !element.is( 'img' ) || element.data( 'cke-realelement' ) || element.isReadOnly() ) - return null; - - return { image : CKEDITOR.TRISTATE_OFF }; + if ( getSelectedImage( editor, element ) ) + return { image : CKEDITOR.TRISTATE_OFF }; }); } + }, + afterInit : function( editor ) + { + // Customize the behavior of the alignment commands. (#7430) + setupAlignCommand( 'left' ); + setupAlignCommand( 'right' ); + setupAlignCommand( 'center' ); + setupAlignCommand( 'block' ); + + function setupAlignCommand( value ) + { + var command = editor.getCommand( 'justify' + value ); + if ( command ) + { + if ( value == 'left' || value == 'right' ) + { + command.on( 'exec', function( evt ) + { + var img = getSelectedImage( editor ), align; + if ( img ) + { + align = getImageAlignment( img ); + if ( align == value ) + { + img.removeStyle( 'float' ); + + // Remove "align" attribute when necessary. + if ( value == getImageAlignment( img ) ) + img.removeAttribute( 'align' ); + } + else + img.setStyle( 'float', value ); + + evt.cancel(); + } + }); + } + + command.on( 'refresh', function( evt ) + { + var img = getSelectedImage( editor ), align; + if ( img ) + { + align = getImageAlignment( img ); + + this.setState( + ( align == value ) ? CKEDITOR.TRISTATE_ON : + ( value == 'right' || value == 'left' ) ? CKEDITOR.TRISTATE_OFF : + CKEDITOR.TRISTATE_DISABLED ); + + evt.cancel(); + } + }); + } + } } -} ); +}); + +function getSelectedImage( editor, element ) +{ + if ( !element ) + { + var sel = editor.getSelection(); + element = ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && sel.getSelectedElement(); + } + + if ( element && element.is( 'img' ) && !element.data( 'cke-realelement' ) && !element.isReadOnly() ) + return element; +} + +function getImageAlignment( element ) +{ + var align = element.getStyle( 'float' ); + + if ( align == 'inherit' || align == 'none' ) + align = 0; + + if ( !align ) + align = element.getAttribute( 'align' ); + + return align; +} + +})(); /** * Whether to remove links when emptying the link URL field in the image dialog.