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 * @fileOverview The "sourcearea" plugin. It registers the "source" editing 8 * mode, which displays the raw data being edited in the editor. 9 */ 10 11 CKEDITOR.plugins.add( 'sourcearea', 12 { 13 requires : [ 'editingblock' ], 14 15 init : function( editor, pluginPath ) 16 { 17 var sourcearea = CKEDITOR.plugins.sourcearea; 18 19 editor.on( 'editingBlockReady', function() 20 { 21 var textarea; 22 23 editor.addMode( 'source', 24 { 25 load : function( holderElement, data ) 26 { 27 // Create the source area <textarea>. 28 textarea = new CKEDITOR.dom.element( 'textarea' ); 29 textarea.setAttribute( 'dir', 'ltr' ); 30 textarea.addClass( 'cke_source' ); 31 textarea.setStyles({ 32 width : '100%', 33 height : '100%', 34 resize : 'none', 35 outline : 'none', 36 'text-align' : 'left' }); 37 38 // The textarea height/width='100%' doesn't 39 // constraint to the 'td' in IE strick mode 40 if ( CKEDITOR.env.ie ) 41 { 42 textarea.setStyles({ 43 height : holderElement.$.clientHeight + 'px', 44 width : holderElement.$.clientWidth + 'px' }); 45 } 46 47 // By some yet unknown reason, we must stop the 48 // mousedown propagation for the textarea, 49 // otherwise it's not possible to place the caret 50 // inside of it (non IE). 51 if ( !CKEDITOR.env.ie ) 52 { 53 textarea.on( 'mousedown', function( evt ) 54 { 55 evt = evt.data.$; 56 if ( evt.stopPropagation ) 57 evt.stopPropagation(); 58 } ); 59 } 60 61 // Reset the holder element and append the 62 // <textarea> to it. 63 holderElement.setHtml( '' ); 64 holderElement.append( textarea ); 65 66 // The editor data "may be dirty" after this point. 67 editor.mayBeDirty = true; 68 69 // Set the <textarea> value. 70 this.loadData( data ); 71 72 editor.mode = 'source'; 73 editor.fire( 'mode' ); 74 }, 75 76 loadData : function( data ) 77 { 78 textarea.setValue( data ); 79 }, 80 81 getData : function() 82 { 83 return textarea.getValue(); 84 }, 85 86 getSnapshotData : function() 87 { 88 return textarea.getValue(); 89 }, 90 91 unload : function( holderElement ) 92 { 93 textarea = null; 94 }, 95 96 focus : function() 97 { 98 textarea.focus(); 99 } 100 }); 101 }); 102 103 editor.addCommand( 'source', sourcearea.commands.source ); 104 105 if ( editor.ui.addButton ) 106 { 107 editor.ui.addButton( 'Source', 108 { 109 label : editor.lang.source, 110 command : 'source' 111 }); 112 } 113 114 editor.on( 'mode', function() 115 { 116 var command = editor.getCommand( 'source' ); 117 command.state = ( editor.mode == 'source' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF ); 118 command.fire( 'state' ); 119 }); 120 } 121 }); 122 123 /** 124 * Holds the definition of commands an UI elements included with the sourcearea 125 * plugin. 126 * @example 127 */ 128 CKEDITOR.plugins.sourcearea = 129 { 130 commands : 131 { 132 source : 133 { 134 exec : function( editor ) 135 { 136 editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' ); 137 } 138 } 139 } 140 }; 141