2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview The "sourcearea" plugin. It registers the "source" editing
\r
8 * mode, which displays the raw data being edited in the editor.
\r
11 CKEDITOR.plugins.add( 'sourcearea',
\r
13 requires : [ 'editingblock' ],
\r
15 init : function( editor )
\r
17 var sourcearea = CKEDITOR.plugins.sourcearea,
\r
18 win = CKEDITOR.document.getWindow();
\r
20 editor.on( 'editingBlockReady', function()
\r
25 editor.addMode( 'source',
\r
27 load : function( holderElement, data )
\r
29 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
30 holderElement.setStyle( 'position', 'relative' );
\r
32 // Create the source area <textarea>.
\r
33 editor.textarea = textarea = new CKEDITOR.dom.element( 'textarea' );
\r
34 textarea.setAttributes(
\r
39 textarea.addClass( 'cke_source' );
\r
40 textarea.addClass( 'cke_enable_context_menu' );
\r
44 // IE7 has overflow the <textarea> from wrapping table cell.
\r
45 width : CKEDITOR.env.ie7Compat ? '99%' : '100%',
\r
49 'text-align' : 'left'
\r
52 // Having to make <textarea> fixed sized to conque the following bugs:
\r
53 // 1. The textarea height/width='100%' doesn't constraint to the 'td' in IE6/7.
\r
54 // 2. Unexpected vertical-scrolling behavior happens whenever focus is moving out of editor
\r
55 // if text content within it has overflowed. (#4762)
\r
56 if ( CKEDITOR.env.ie )
\r
58 onResize = function()
\r
60 // Holder rectange size is stretched by textarea,
\r
61 // so hide it just for a moment.
\r
63 textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );
\r
64 textarea.setStyle( 'width', holderElement.$.clientWidth + 'px' );
\r
65 // When we have proper holder size, show textarea again.
\r
69 editor.on( 'resize', onResize );
\r
70 win.on( 'resize', onResize );
\r
71 setTimeout( onResize, 0 );
\r
75 // By some yet unknown reason, we must stop the
\r
76 // mousedown propagation for the textarea,
\r
77 // otherwise it's not possible to place the caret
\r
78 // inside of it (non IE).
\r
79 textarea.on( 'mousedown', function( evt )
\r
81 evt.data.stopPropagation();
\r
85 // Reset the holder element and append the
\r
86 // <textarea> to it.
\r
87 holderElement.setHtml( '' );
\r
88 holderElement.append( textarea );
\r
89 textarea.setStyles( styles );
\r
91 textarea.on( 'blur', function()
\r
93 editor.focusManager.blur();
\r
96 textarea.on( 'focus', function()
\r
98 editor.focusManager.focus();
\r
101 // The editor data "may be dirty" after this point.
\r
102 editor.mayBeDirty = true;
\r
104 // Set the <textarea> value.
\r
105 this.loadData( data );
\r
107 var keystrokeHandler = editor.keystrokeHandler;
\r
108 if ( keystrokeHandler )
\r
109 keystrokeHandler.attach( textarea );
\r
111 setTimeout( function()
\r
113 editor.mode = 'source';
\r
114 editor.fire( 'mode' );
\r
116 ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );
\r
119 loadData : function( data )
\r
121 textarea.setValue( data );
\r
122 editor.fire( 'dataReady' );
\r
125 getData : function()
\r
127 return textarea.getValue();
\r
130 getSnapshotData : function()
\r
132 return textarea.getValue();
\r
135 unload : function( holderElement )
\r
137 editor.textarea = textarea = null;
\r
141 editor.removeListener( 'resize', onResize );
\r
142 win.removeListener( 'resize', onResize );
\r
145 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
146 holderElement.removeStyle( 'position' );
\r
156 editor.addCommand( 'source', sourcearea.commands.source );
\r
158 if ( editor.ui.addButton )
\r
160 editor.ui.addButton( 'Source',
\r
162 label : editor.lang.source,
\r
167 editor.on( 'mode', function()
\r
169 editor.getCommand( 'source' ).setState(
\r
170 editor.mode == 'source' ?
\r
171 CKEDITOR.TRISTATE_ON :
\r
172 CKEDITOR.TRISTATE_OFF );
\r
178 * Holds the definition of commands an UI elements included with the sourcearea
\r
182 CKEDITOR.plugins.sourcearea =
\r
188 modes : { wysiwyg:1, source:1 },
\r
190 exec : function( editor )
\r
192 if ( editor.mode == 'wysiwyg' )
\r
193 editor.fire( 'saveSnapshot' );
\r
194 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );
\r
195 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );
\r