X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fdiv%2Fdialogs%2Fdiv.js;h=e63bd4b880e2563fb147d1b900c0eb911c267a1c;hb=3fe9cac293e090ea459a3ee10d78cbe9e1dd0e03;hp=619a3f883b70c17e6e7aec45bf599f34bf3cbd9f;hpb=941b0a9ba4e673e292510d80a5a86806994b8ea6;p=ckeditor.git diff --git a/_source/plugins/div/dialogs/div.js b/_source/plugins/div/dialogs/div.js index 619a3f8..e63bd4b 100644 --- a/_source/plugins/div/dialogs/div.js +++ b/_source/plugins/div/dialogs/div.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. + * Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -27,10 +27,10 @@ { var retval = []; var children = element.getChildren(); - for( var i = 0 ; i < children.count() ; i++ ) + for ( var i = 0 ; i < children.count() ; i++ ) { var child = children.getItem( i ); - if( ! ( child.type === CKEDITOR.NODE_TEXT + if ( ! ( child.type === CKEDITOR.NODE_TEXT && ( /^[ \t\n\r]+$/ ).test( child.getText() ) ) ) retval.push( child ); } @@ -55,7 +55,7 @@ delete definition.div; // Exclude 'td' and 'th' when 'wrapping table' - if( editor.config.div_wrapTable ) + if ( editor.config.div_wrapTable ) { delete definition.td; delete definition.th; @@ -94,7 +94,7 @@ this.foreach( function( field ) { // Exclude layout container elements - if( /^(?!vbox|hbox)/.test( field.type ) ) + if ( /^(?!vbox|hbox)/.test( field.type ) ) { if ( !field.setup ) { @@ -140,8 +140,8 @@ var containedBlocks = [], block; // Get all ranges from the selection. - var selection = editor.document.getSelection(); - var ranges = selection.getRanges(); + var selection = editor.document.getSelection(), + ranges = selection.getRanges(); var bookmarks = selection.createBookmarks(); var i, iterator; @@ -149,13 +149,13 @@ var blockTag = editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'; // collect all included elements from dom-iterator - for( i = 0 ; i < ranges.length ; i++ ) + for ( i = 0 ; i < ranges.length ; i++ ) { iterator = ranges[ i ].createIterator(); - while( ( block = iterator.getNextParagraph() ) ) + while ( ( block = iterator.getNextParagraph() ) ) { // include contents of blockLimit elements. - if( block.getName() in divLimitDefinition ) + if ( block.getName() in divLimitDefinition ) { var j, childNodes = block.getChildren(); for ( j = 0 ; j < childNodes.count() ; j++ ) @@ -164,7 +164,7 @@ else { // Bypass dtd disallowed elements. - while( !dtd[ block.getName() ] && block.getName() != 'body' ) + while ( !dtd[ block.getName() ] && block.getName() != 'body' ) block = block.getParent(); addSafely( containedBlocks, block, database ); } @@ -176,7 +176,7 @@ var blockGroups = groupByDivLimit( containedBlocks ); var ancestor, blockEl, divElement; - for( i = 0 ; i < blockGroups.length ; i++ ) + for ( i = 0 ; i < blockGroups.length ; i++ ) { var currentNode = blockGroups[ i ][ 0 ]; @@ -188,11 +188,11 @@ divElement = new CKEDITOR.dom.element( 'div', editor.document ); // Normalize the blocks in each group to a common parent. - for( j = 0; j < blockGroups[ i ].length ; j++ ) + for ( j = 0; j < blockGroups[ i ].length ; j++ ) { currentNode = blockGroups[ i ][ j ]; - while( !currentNode.getParent().equals( ancestor ) ) + while ( !currentNode.getParent().equals( ancestor ) ) currentNode = currentNode.getParent(); // This could introduce some duplicated elements in array. @@ -259,6 +259,29 @@ return groups; } + // Synchronous field values to other impacted fields is required, e.g. div styles + // change should also alter inline-style text. + function commitInternally( targetFields ) + { + var dialog = this.getDialog(), + element = dialog._element && dialog._element.clone() + || new CKEDITOR.dom.element( 'div', editor.document ); + + // Commit this field and broadcast to target fields. + this.commit( element, true ); + + targetFields = [].concat( targetFields ); + var length = targetFields.length, field; + for ( var i = 0; i < length; i++ ) + { + field = dialog.getContentElement.apply( dialog, targetFields[ i ].split( ':' ) ); + field && field.setup && field.setup( element, true ); + } + } + + + // Registered 'CKEDITOR.style' instances. + var styles = {} ; /** * Hold a collection of created block container elements. */ @@ -289,17 +312,31 @@ style :'width: 100%;', label :editor.lang.div.styleSelectLabel, 'default' : '', - items : [], + // Options are loaded dynamically. + items : + [ + [ editor.lang.common.notSet , '' ] + ], + onChange : function() + { + commitInternally.call( this, [ 'info:class', 'advanced:dir', 'advanced:style' ] ); + }, setup : function( element ) { - this.setValue( element.$.style.cssText || '' ); + for ( var name in styles ) + styles[ name ].checkElementRemovable( element, true ) && this.setValue( name ); }, commit: function( element ) { - if ( this.getValue() ) - element.$.style.cssText = this.getValue(); - else - element.removeAttribute( 'style' ); + var styleName; + if ( ( styleName = this.getValue() ) ) + { + var style = styles[ styleName ]; + var customData = element.getCustomData( 'elementStyle' ) || ''; + + style.applyToObject( element ); + element.setCustomData( 'elementStyle', customData + style._.definition.attributes.style ); + } } }, { @@ -351,7 +388,13 @@ id :'style', style :'width: 100%;', label :editor.lang.common.cssStyle, - 'default' : '' + 'default' : '', + commit : function( element ) + { + // Merge with 'elementStyle', which is of higher priority. + var merged = this.getValue() + ( element.getCustomData( 'elementStyle' ) || '' ); + element.setAttribute( 'style', merged ); + } } ] }, @@ -376,6 +419,7 @@ 'default' : '', items : [ + [ editor.lang.common.notSet , '' ], [ editor.lang.common.langDirLtr, 'ltr' @@ -393,7 +437,42 @@ ], onLoad : function() { - setupFields.call(this); + setupFields.call( this ); + + // Preparing for the 'elementStyle' field. + var dialog = this, + stylesField = this.getContentElement( 'info', 'elementStyle' ); + + // Reuse the 'stylescombo' plugin's styles definition. + editor.getStylesSet( function( stylesDefinitions ) + { + var styleName; + + if ( stylesDefinitions ) + { + // Digg only those styles that apply to 'div'. + for ( var i = 0 ; i < stylesDefinitions.length ; i++ ) + { + var styleDefinition = stylesDefinitions[ i ]; + if ( styleDefinition.element && styleDefinition.element == 'div' ) + { + styleName = styleDefinition.name; + styles[ styleName ] = new CKEDITOR.style( styleDefinition ); + + // Populate the styles field options with style name. + stylesField.items.push( [ styleName, styleName ] ); + stylesField.add( styleName, styleName ); + } + } + } + + // We should disable the content element + // it if no options are available at all. + stylesField[ stylesField.items.length > 1 ? 'enable' : 'disable' ](); + + // Now setup the field value manually. + setTimeout( function() { stylesField.setup( dialog._element ); }, 0 ); + } ); }, onShow : function() { @@ -410,15 +489,29 @@ }, onOk : function() { - if( command == 'editdiv' ) + if ( command == 'editdiv' ) containers = [ this._element ]; else containers = createDiv( editor, true ); // Update elements attributes - for( var i = 0 ; i < containers.length ; i++ ) + var size = containers.length; + for ( var i = 0; i < size; i++ ) + { this.commitContent( containers[ i ] ); + + // Remove empty 'style' attribute. + !containers[ i ].getAttribute( 'style' ) && containers[ i ].removeAttribute( 'style' ); + } + this.hide(); + }, + onHide : function() + { + // Remove style only when editing existing DIV. (#6315) + if ( command == 'editdiv' ) + this._element.removeCustomData( 'elementStyle' ); + delete this._element; } }; }