source: sandbox/3.0/phpgwapi/js/ckeditor/_source/plugins/enterkey/plugin.js @ 2862

Revision 2862, 9.8 KB checked in by rodsouza, 14 years ago (diff)

Ticket #663 - Atualizando e centralizando o CKEditor (v. 3.2.1)

Line 
1/*
2Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6(function()
7{
8        CKEDITOR.plugins.add( 'enterkey',
9        {
10                requires : [ 'keystrokes', 'indent' ],
11
12                init : function( editor )
13                {
14                        var specialKeys = editor.specialKeys;
15                        specialKeys[ 13 ] = enter;
16                        specialKeys[ CKEDITOR.SHIFT + 13 ] = shiftEnter;
17                }
18        });
19
20        CKEDITOR.plugins.enterkey =
21        {
22                enterBlock : function( editor, mode, range, forceMode )
23                {
24                        // Get the range for the current selection.
25                        range = range || getRange( editor );
26
27                        var doc = range.document;
28
29                        // Exit the list when we're inside an empty list item block. (#5376)
30                        if ( range.checkStartOfBlock() && range.checkEndOfBlock() )
31                        {
32                                var path = new CKEDITOR.dom.elementPath( range.startContainer ),
33                                                block = path.block;
34
35                                if ( block.is( 'li' ) || block.getParent().is( 'li' ) )
36                                {
37                                        editor.execCommand( 'outdent' );
38                                        return;
39                                }
40                        }
41
42                        // Determine the block element to be used.
43                        var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
44
45                        // Split the range.
46                        var splitInfo = range.splitBlock( blockTag );
47
48                        if ( !splitInfo )
49                                return;
50
51                        // Get the current blocks.
52                        var previousBlock       = splitInfo.previousBlock,
53                                nextBlock               = splitInfo.nextBlock;
54
55                        var isStartOfBlock      = splitInfo.wasStartOfBlock,
56                                isEndOfBlock    = splitInfo.wasEndOfBlock;
57
58                        var node;
59
60                        // If this is a block under a list item, split it as well. (#1647)
61                        if ( nextBlock )
62                        {
63                                node = nextBlock.getParent();
64                                if ( node.is( 'li' ) )
65                                {
66                                        nextBlock.breakParent( node );
67                                        nextBlock.move( nextBlock.getNext(), true );
68                                }
69                        }
70                        else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )
71                        {
72                                previousBlock.breakParent( node );
73                                range.moveToElementEditStart( previousBlock.getNext() );
74                                previousBlock.move( previousBlock.getPrevious() );
75                        }
76
77                        // If we have both the previous and next blocks, it means that the
78                        // boundaries were on separated blocks, or none of them where on the
79                        // block limits (start/end).
80                        if ( !isStartOfBlock && !isEndOfBlock )
81                        {
82                                // If the next block is an <li> with another list tree as the first
83                                // child, we'll need to append a filler (<br>/NBSP) or the list item
84                                // wouldn't be editable. (#1420)
85                                if ( nextBlock.is( 'li' )
86                                         && ( node = nextBlock.getFirst( CKEDITOR.dom.walker.invisible( true ) ) )
87                                         && node.is && node.is( 'ul', 'ol' ) )
88                                        ( CKEDITOR.env.ie ? doc.createText( '\xa0' ) : doc.createElement( 'br' ) ).insertBefore( node );
89
90                                // Move the selection to the end block.
91                                if ( nextBlock )
92                                        range.moveToElementEditStart( nextBlock );
93                        }
94                        else
95                        {
96                                var newBlock;
97
98                                if ( previousBlock )
99                                {
100                                        // Do not enter this block if it's a header tag, or we are in
101                                        // a Shift+Enter (#77). Create a new block element instead
102                                        // (later in the code).
103                                        if ( previousBlock.is( 'li' ) ||
104                                                 !( forceMode || headerTagRegex.test( previousBlock.getName() ) ) )
105                                        {
106                                                // Otherwise, duplicate the previous block.
107                                                newBlock = previousBlock.clone();
108                                        }
109                                }
110                                else if ( nextBlock )
111                                        newBlock = nextBlock.clone();
112
113                                if ( !newBlock )
114                                        newBlock = doc.createElement( blockTag );
115
116                                // Recreate the inline elements tree, which was available
117                                // before hitting enter, so the same styles will be available in
118                                // the new block.
119                                var elementPath = splitInfo.elementPath;
120                                if ( elementPath )
121                                {
122                                        for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )
123                                        {
124                                                var element = elementPath.elements[ i ];
125
126                                                if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )
127                                                        break;
128
129                                                if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )
130                                                {
131                                                        element = element.clone();
132                                                        newBlock.moveChildren( element );
133                                                        newBlock.append( element );
134                                                }
135                                        }
136                                }
137
138                                if ( !CKEDITOR.env.ie )
139                                        newBlock.appendBogus();
140
141                                range.insertNode( newBlock );
142
143                                // This is tricky, but to make the new block visible correctly
144                                // we must select it.
145                                // The previousBlock check has been included because it may be
146                                // empty if we have fixed a block-less space (like ENTER into an
147                                // empty table cell).
148                                if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )
149                                {
150                                        // Move the selection to the new block.
151                                        range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );
152                                        range.select();
153                                }
154
155                                // Move the selection to the new block.
156                                range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );
157                }
158
159                        if ( !CKEDITOR.env.ie )
160                        {
161                                if ( nextBlock )
162                                {
163                                        // If we have split the block, adds a temporary span at the
164                                        // range position and scroll relatively to it.
165                                        var tmpNode = doc.createElement( 'span' );
166
167                                        // We need some content for Safari.
168                                        tmpNode.setHtml( '&nbsp;' );
169
170                                        range.insertNode( tmpNode );
171                                        tmpNode.scrollIntoView();
172                                        range.deleteContents();
173                                }
174                                else
175                                {
176                                        // We may use the above scroll logic for the new block case
177                                        // too, but it gives some weird result with Opera.
178                                        newBlock.scrollIntoView();
179                                }
180                        }
181
182                        range.select();
183                },
184
185                enterBr : function( editor, mode, range, forceMode )
186                {
187                        // Get the range for the current selection.
188                        range = range || getRange( editor );
189
190                        var doc = range.document;
191
192                        // Determine the block element to be used.
193                        var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
194
195                        var isEndOfBlock = range.checkEndOfBlock();
196
197                        var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );
198
199                        var startBlock = elementPath.block,
200                                startBlockTag = startBlock && elementPath.block.getName();
201
202                        var isPre = false;
203
204                        if ( !forceMode && startBlockTag == 'li' )
205                        {
206                                enterBlock( editor, mode, range, forceMode );
207                                return;
208                        }
209
210                        // If we are at the end of a header block.
211                        if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )
212                        {
213                                // Insert a <br> after the current paragraph.
214                                doc.createElement( 'br' ).insertAfter( startBlock );
215
216                                // A text node is required by Gecko only to make the cursor blink.
217                                if ( CKEDITOR.env.gecko )
218                                        doc.createText( '' ).insertAfter( startBlock );
219
220                                // IE has different behaviors regarding position.
221                                range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );
222                        }
223                        else
224                        {
225                                var lineBreak;
226
227                                isPre = ( startBlockTag == 'pre' );
228
229                                // Gecko prefers <br> as line-break inside <pre> (#4711).
230                                if ( isPre && !CKEDITOR.env.gecko )
231                                        lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );
232                                else
233                                        lineBreak = doc.createElement( 'br' );
234
235                                range.deleteContents();
236                                range.insertNode( lineBreak );
237
238                                // A text node is required by Gecko only to make the cursor blink.
239                                // We need some text inside of it, so the bogus <br> is properly
240                                // created.
241                                if ( !CKEDITOR.env.ie )
242                                        doc.createText( '\ufeff' ).insertAfter( lineBreak );
243
244                                // If we are at the end of a block, we must be sure the bogus node is available in that block.
245                                if ( isEndOfBlock && !CKEDITOR.env.ie )
246                                        lineBreak.getParent().appendBogus();
247
248                                // Now we can remove the text node contents, so the caret doesn't
249                                // stop on it.
250                                if ( !CKEDITOR.env.ie )
251                                        lineBreak.getNext().$.nodeValue = '';
252                                // IE has different behavior regarding position.
253                                if ( CKEDITOR.env.ie )
254                                        range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );
255                                else
256                                        range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );
257
258                                // Scroll into view, for non IE.
259                                if ( !CKEDITOR.env.ie )
260                                {
261                                        var dummy = null;
262
263                                        // BR is not positioned in Opera and Webkit.
264                                        if ( !CKEDITOR.env.gecko )
265                                        {
266                                                dummy = doc.createElement( 'span' );
267                                                // We need have some contents for Webkit to position it
268                                                // under parent node. ( #3681)
269                                                dummy.setHtml('&nbsp;');
270                                        }
271                                        else
272                                                dummy = doc.createElement( 'br' );
273
274                                        dummy.insertBefore( lineBreak.getNext() );
275                                        dummy.scrollIntoView();
276                                        dummy.remove();
277                                }
278                        }
279
280                        // This collapse guarantees the cursor will be blinking.
281                        range.collapse( true );
282
283                        range.select( isPre );
284                }
285        };
286
287        var plugin = CKEDITOR.plugins.enterkey,
288                enterBr = plugin.enterBr,
289                enterBlock = plugin.enterBlock,
290                headerTagRegex = /^h[1-6]$/;
291
292        function shiftEnter( editor )
293        {
294                // On SHIFT+ENTER we want to enforce the mode to be respected, instead
295                // of cloning the current block. (#77)
296                return enter( editor, editor.config.shiftEnterMode, true );
297        }
298
299        function enter( editor, mode, forceMode )
300        {
301                forceMode = editor.config.forceEnterMode || forceMode;
302
303                // Only effective within document.
304                if ( editor.mode != 'wysiwyg' )
305                        return false;
306
307                if ( !mode )
308                        mode = editor.config.enterMode;
309
310                // Use setTimout so the keys get cancelled immediatelly.
311                setTimeout( function()
312                        {
313                                editor.fire( 'saveSnapshot' );  // Save undo step.
314                                if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )
315                                        enterBr( editor, mode, null, forceMode );
316                                else
317                                        enterBlock( editor, mode, null, forceMode );
318
319                        }, 0 );
320
321                return true;
322        }
323
324
325        function getRange( editor )
326        {
327                // Get the selection ranges.
328                var ranges = editor.getSelection().getRanges();
329
330                // Delete the contents of all ranges except the first one.
331                for ( var i = ranges.length - 1 ; i > 0 ; i-- )
332                {
333                        ranges[ i ].deleteContents();
334                }
335
336                // Return the first range.
337                return ranges[ 0 ];
338        }
339})();
Note: See TracBrowser for help on using the repository browser.