Hello,
I am attempting to extend the image plugin to create a parent, caption element around images. I can successfully create parent elements, but I'm having trouble loading pre-existing parents so I can prepopulate the caption field's value. When I console.log the element objects, the parent element references are all shown as null, which I assume is because the element reference I am given is the one in the preview. But the strangest thing is, even under this condition, the check for $(element.$).parent().parent().hasClass('image-container') works and modifies the existing element. Any points in the right direction would be appreciated.
Additionally, parent elements won't be created on the initial image elment generaton, only during modifications to existing image elements.
*Edit: Sorry, should have been more specific. I'm using CKEditor 3.4.2 in Drupal.
Here's my code.
CKEDITOR.on( 'dialogDefinition', function( ev ) { var dialogName = ev.data.name; var dialogDefinition = ev.data.definition; var IMAGE = 1, //Adding image into wysiwug? LINK = 2, //?? Between loading and cleanup PREVIEW = 4,//Loading image in dialog. CLEANUP = 8; //Closing the dialog, either cancel or ok. // Check if the definition is from the dialog we're if ( dialogName == 'image' ) { // Get a reference to the "Link Info" tab. var infoTab = dialogDefinition.getContents( 'info' ); // Add a text field to the "info" tab. infoTab.add( { type : 'text', label : 'Image Caption', id : 'imageCaption', 'default' : '', validate : function(){}, setup : function( type, element ){ console.log(type); if(type == IMAGE){ //Set the value with the current here, but how do we access the parent element??? } }, commit : function( type, element, internalCommit ) { if(this.getValue()){ var floats = element.getStyle('float'); $(element.$).css({'float' : ''}); //Handle floated images. if(floats){ floats = {'float' : floats}; } else{ floats = ''; } //set max-width var imgWidth = Number(element.getStyle('width').replace(/\D/g,'')); var borderWidth = Number(element.getStyle('border-width').replace(/\D/g,'')); var margin = Number(element.getStyle('margin-left').replace(/\D/g,'')); var maxWidth = {'max-width' : imgWidth+(borderWidth*2)+(margin*2) } if(type == IMAGE){ if($(element.$).parent().parent().hasClass('image-container')){ //Modify an existing caption $(element.$).parent().children('.image-caption').remove(); $(element.$).parent().append('<p class="image-caption">'+this.getValue()+'</p>'); $(element.$).parent().css(floats); $(element.$).parent().css(maxWidth); } else{ $(element.$).wrap('<div class="image-container"></div>'); $(element.$).parent().append('<p class="image-caption">'+this.getValue()+'</p>'); $(element.$).parent().css(floats); $(element.$).parent().css(maxWidth); } } } } }); } }); });
Never did figure it out....
Not sure if I like it, but I ended up modifying the source code for the image plugin. Just dumped this ugly looking block in the onOk function. Then inserted Parent element in place of the image element. Altering the text still seems to be handled by my original plugin code.