      1 /*
      2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
      3 For licensing, see LICENSE.html or http://ckeditor.com/license
      4 */
      5
      6 /**
      7  * @file Horizontal Page Break
      8  */
      9
     10 // Register a plugin named "pagebreak".
     11 CKEDITOR.plugins.add( 'pagebreak',
     12 {
     13 	init : function( editor )
     14 	{
     15 		// Register the command.
     16 		editor.addCommand( 'pagebreak', CKEDITOR.plugins.pagebreakCmd );
     17
     18 		// Register the toolbar button.
     19 		editor.ui.addButton( 'PageBreak',
     20 			{
     21 				label : editor.lang.pagebreak,
     22 				command : 'pagebreak'
     23 			});
     24
     25 		// Add the style that renders our placeholder.
     26 		editor.addCss(
     27 			'img.cke_pagebreak' +
     28 			'{' +
     29 				'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/pagebreak.gif' ) + ');' +
     30 				'background-position: center center;' +
     31 				'background-repeat: no-repeat;' +
     32 				'clear: both;' +
     33 				'display: block;' +
     34 				'float: none;' +
     35 				'width: 100%;' +
     36 				'border-top: #999999 1px dotted;' +
     37 				'border-bottom: #999999 1px dotted;' +
     38 				'height: 5px;' +
     39
     40 			'}' );
     41 	},
     42
     43 	afterInit : function( editor )
     44 	{
     45 		// Register a filter to displaying placeholders after mode change.
     46
     47 		var dataProcessor = editor.dataProcessor,
     48 			dataFilter = dataProcessor && dataProcessor.dataFilter;
     49
     50 		if ( dataFilter )
     51 		{
     52 			dataFilter.addRules(
     53 				{
     54 					elements :
     55 					{
     56 						div : function( element )
     57 						{
     58 							var style = element.attributes.style,
     59 								child = style && element.children.length == 1 && element.children[ 0 ],
     60 								childStyle = child && ( child.name == 'span' ) && child.attributes.style;
     61
     62 							if ( childStyle && ( /page-break-after\s*:\s*always/i ).test( style ) && ( /display\s*:\s*none/i ).test( childStyle ) )
     63 								return editor.createFakeParserElement( element, 'cke_pagebreak', 'div' );
     64 						}
     65 					}
     66 				});
     67 		}
     68 	},
     69
     70 	requires : [ 'fakeobjects' ]
     71 });
     72
     73 CKEDITOR.plugins.pagebreakCmd =
     74 {
     75 	exec : function( editor )
     76 	{
     77 		// Create the element that represents a print break.
     78 		var breakObject = CKEDITOR.dom.element.createFromHtml( '<div style="page-break-after: always;"><span style="display: none;"> </span></div>' );
     79
     80 		// Creates the fake image used for this element.
     81 		breakObject = editor.createFakeElement( breakObject, 'cke_pagebreak', 'div' );
     82
     83 		var ranges = editor.getSelection().getRanges();
     84
     85 		for ( var range, i = 0 ; i < ranges.length ; i++ )
     86 		{
     87 			range = ranges[ i ];
     88
     89 			if ( i > 0 )
     90 				breakObject = breakObject.clone( true );
     91
     92 			range.splitBlock( 'p' );
     93 			range.insertNode( breakObject );
     94 		}
     95 	}
     96 };
     97