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
37 tabIndex : CKEDITOR.env.webkit ? -1 : editor.tabIndex,
\r
39 'aria-label' : editor.lang.editorTitle.replace( '%1', editor.name )
\r
41 textarea.addClass( 'cke_source' );
\r
42 textarea.addClass( 'cke_enable_context_menu' );
\r
46 // IE7 has overflow the <textarea> from wrapping table cell.
\r
47 width : CKEDITOR.env.ie7Compat ? '99%' : '100%',
\r
51 'text-align' : 'left'
\r
54 // Having to make <textarea> fixed sized to conque the following bugs:
\r
55 // 1. The textarea height/width='100%' doesn't constraint to the 'td' in IE6/7.
\r
56 // 2. Unexpected vertical-scrolling behavior happens whenever focus is moving out of editor
\r
57 // if text content within it has overflowed. (#4762)
\r
58 if ( CKEDITOR.env.ie )
\r
60 onResize = function()
\r
62 // Holder rectange size is stretched by textarea,
\r
63 // so hide it just for a moment.
\r
65 textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );
\r
66 textarea.setStyle( 'width', holderElement.$.clientWidth + 'px' );
\r
67 // When we have proper holder size, show textarea again.
\r
71 editor.on( 'resize', onResize );
\r
72 win.on( 'resize', onResize );
\r
73 setTimeout( onResize, 0 );
\r
77 // By some yet unknown reason, we must stop the
\r
78 // mousedown propagation for the textarea,
\r
79 // otherwise it's not possible to place the caret
\r
80 // inside of it (non IE).
\r
81 textarea.on( 'mousedown', function( evt )
\r
83 evt.data.stopPropagation();
\r
87 // Reset the holder element and append the
\r
88 // <textarea> to it.
\r
89 holderElement.setHtml( '' );
\r
90 holderElement.append( textarea );
\r
91 textarea.setStyles( styles );
\r
93 editor.fire( 'ariaWidget', textarea );
\r
95 textarea.on( 'blur', function()
\r
97 editor.focusManager.blur();
\r
100 textarea.on( 'focus', function()
\r
102 editor.focusManager.focus();
\r
105 // The editor data "may be dirty" after this point.
\r
106 editor.mayBeDirty = true;
\r
108 // Set the <textarea> value.
\r
109 this.loadData( data );
\r
111 var keystrokeHandler = editor.keystrokeHandler;
\r
112 if ( keystrokeHandler )
\r
113 keystrokeHandler.attach( textarea );
\r
115 setTimeout( function()
\r
117 editor.mode = 'source';
\r
118 editor.fire( 'mode' );
\r
120 ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );
\r
123 loadData : function( data )
\r
125 textarea.setValue( data );
\r
126 editor.fire( 'dataReady' );
\r
129 getData : function()
\r
131 return textarea.getValue();
\r
134 getSnapshotData : function()
\r
136 return textarea.getValue();
\r
139 unload : function( holderElement )
\r
141 textarea.clearCustomData();
\r
142 editor.textarea = textarea = null;
\r
146 editor.removeListener( 'resize', onResize );
\r
147 win.removeListener( 'resize', onResize );
\r
150 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
151 holderElement.removeStyle( 'position' );
\r
161 editor.addCommand( 'source', sourcearea.commands.source );
\r
163 if ( editor.ui.addButton )
\r
165 editor.ui.addButton( 'Source',
\r
167 label : editor.lang.source,
\r
172 editor.on( 'mode', function()
\r
174 editor.getCommand( 'source' ).setState(
\r
175 editor.mode == 'source' ?
\r
176 CKEDITOR.TRISTATE_ON :
\r
177 CKEDITOR.TRISTATE_OFF );
\r
183 * Holds the definition of commands an UI elements included with the sourcearea
\r
187 CKEDITOR.plugins.sourcearea =
\r
193 modes : { wysiwyg:1, source:1 },
\r
194 editorFocus : false,
\r
196 exec : function( editor )
\r
198 if ( editor.mode == 'wysiwyg' )
\r
199 editor.fire( 'saveSnapshot' );
\r
200 editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );
\r
201 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );
\r