1 | /*
|
---|
2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
|
---|
3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
4 | */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @file Justify commands.
|
---|
8 | */
|
---|
9 |
|
---|
10 | (function()
|
---|
11 | {
|
---|
12 | var alignRemoveRegex = /(-moz-|-webkit-|start|auto)/i;
|
---|
13 |
|
---|
14 | function getState( editor, path )
|
---|
15 | {
|
---|
16 | var firstBlock = path.block || path.blockLimit;
|
---|
17 |
|
---|
18 | if ( !firstBlock || firstBlock.getName() == 'body' )
|
---|
19 | return CKEDITOR.TRISTATE_OFF;
|
---|
20 |
|
---|
21 | var currentAlign = firstBlock.getComputedStyle( 'text-align' ).replace( alignRemoveRegex, '' );
|
---|
22 | if ( ( !currentAlign && this.isDefaultAlign ) || currentAlign == this.value )
|
---|
23 | return CKEDITOR.TRISTATE_ON;
|
---|
24 | return CKEDITOR.TRISTATE_OFF;
|
---|
25 | }
|
---|
26 |
|
---|
27 | function onSelectionChange( evt )
|
---|
28 | {
|
---|
29 | var command = evt.editor.getCommand( this.name );
|
---|
30 | command.state = getState.call( this, evt.editor, evt.data.path );
|
---|
31 | command.fire( 'state' );
|
---|
32 | }
|
---|
33 |
|
---|
34 | function justifyCommand( editor, name, value )
|
---|
35 | {
|
---|
36 | this.name = name;
|
---|
37 | this.value = value;
|
---|
38 |
|
---|
39 | var contentDir = editor.config.contentsLangDirection;
|
---|
40 | this.isDefaultAlign = ( value == 'left' && contentDir == 'ltr' ) ||
|
---|
41 | ( value == 'right' && contentDir == 'rtl' );
|
---|
42 |
|
---|
43 | var classes = editor.config.justifyClasses;
|
---|
44 | if ( classes )
|
---|
45 | {
|
---|
46 | switch ( value )
|
---|
47 | {
|
---|
48 | case 'left' :
|
---|
49 | this.cssClassName = classes[0];
|
---|
50 | break;
|
---|
51 | case 'center' :
|
---|
52 | this.cssClassName = classes[1];
|
---|
53 | break;
|
---|
54 | case 'right' :
|
---|
55 | this.cssClassName = classes[2];
|
---|
56 | break;
|
---|
57 | case 'justify' :
|
---|
58 | this.cssClassName = classes[3];
|
---|
59 | break;
|
---|
60 | }
|
---|
61 |
|
---|
62 | this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | justifyCommand.prototype = {
|
---|
67 | exec : function( editor )
|
---|
68 | {
|
---|
69 | var selection = editor.getSelection(),
|
---|
70 | enterMode = editor.config.enterMode;
|
---|
71 |
|
---|
72 | if ( !selection )
|
---|
73 | return;
|
---|
74 |
|
---|
75 | var bookmarks = selection.createBookmarks(),
|
---|
76 | ranges = selection.getRanges();
|
---|
77 |
|
---|
78 |
|
---|
79 | var cssClassName = this.cssClassName,
|
---|
80 | iterator,
|
---|
81 | block;
|
---|
82 | for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
|
---|
83 | {
|
---|
84 | iterator = ranges[ i ].createIterator();
|
---|
85 | iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
|
---|
86 |
|
---|
87 | while ( ( block = iterator.getNextParagraph() ) )
|
---|
88 | {
|
---|
89 | block.removeAttribute( 'align' );
|
---|
90 |
|
---|
91 | if ( cssClassName )
|
---|
92 | {
|
---|
93 | // Remove any of the alignment classes from the className.
|
---|
94 | var className = block.$.className =
|
---|
95 | CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) );
|
---|
96 |
|
---|
97 | // Append the desired class name.
|
---|
98 | if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
|
---|
99 | block.addClass( cssClassName );
|
---|
100 | else if ( !className )
|
---|
101 | block.removeAttribute( 'class' );
|
---|
102 | }
|
---|
103 | else
|
---|
104 | {
|
---|
105 | if ( this.state == CKEDITOR.TRISTATE_OFF && !this.isDefaultAlign )
|
---|
106 | block.setStyle( 'text-align', this.value );
|
---|
107 | else
|
---|
108 | block.removeStyle( 'text-align' );
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 |
|
---|
114 | editor.focus();
|
---|
115 | editor.forceNextSelectionCheck();
|
---|
116 | selection.selectBookmarks( bookmarks );
|
---|
117 | }
|
---|
118 | };
|
---|
119 |
|
---|
120 | CKEDITOR.plugins.add( 'justify',
|
---|
121 | {
|
---|
122 | init : function( editor )
|
---|
123 | {
|
---|
124 | var left = new justifyCommand( editor, 'justifyleft', 'left' ),
|
---|
125 | center = new justifyCommand( editor, 'justifycenter', 'center' ),
|
---|
126 | right = new justifyCommand( editor, 'justifyright', 'right' ),
|
---|
127 | justify = new justifyCommand( editor, 'justifyblock', 'justify' );
|
---|
128 |
|
---|
129 | editor.addCommand( 'justifyleft', left );
|
---|
130 | editor.addCommand( 'justifycenter', center );
|
---|
131 | editor.addCommand( 'justifyright', right );
|
---|
132 | editor.addCommand( 'justifyblock', justify );
|
---|
133 |
|
---|
134 | editor.ui.addButton( 'JustifyLeft',
|
---|
135 | {
|
---|
136 | label : editor.lang.justify.left,
|
---|
137 | command : 'justifyleft'
|
---|
138 | } );
|
---|
139 | editor.ui.addButton( 'JustifyCenter',
|
---|
140 | {
|
---|
141 | label : editor.lang.justify.center,
|
---|
142 | command : 'justifycenter'
|
---|
143 | } );
|
---|
144 | editor.ui.addButton( 'JustifyRight',
|
---|
145 | {
|
---|
146 | label : editor.lang.justify.right,
|
---|
147 | command : 'justifyright'
|
---|
148 | } );
|
---|
149 | editor.ui.addButton( 'JustifyBlock',
|
---|
150 | {
|
---|
151 | label : editor.lang.justify.block,
|
---|
152 | command : 'justifyblock'
|
---|
153 | } );
|
---|
154 |
|
---|
155 | editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
|
---|
156 | editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
|
---|
157 | editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
|
---|
158 | editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
|
---|
159 | },
|
---|
160 |
|
---|
161 | requires : [ 'domiterator' ]
|
---|
162 | });
|
---|
163 | })();
|
---|
164 |
|
---|
165 | CKEDITOR.tools.extend( CKEDITOR.config,
|
---|
166 | {
|
---|
167 | justifyClasses : null
|
---|
168 | } );
|
---|