JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / sourcearea / plugin.js
diff --git a/_source/plugins/sourcearea/plugin.js b/_source/plugins/sourcearea/plugin.js
new file mode 100644 (file)
index 0000000..54f9d54
--- /dev/null
@@ -0,0 +1,185 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @fileOverview The "sourcearea" plugin. It registers the "source" editing\r
+ *             mode, which displays the raw data being edited in the editor.\r
+ */\r
+\r
+CKEDITOR.plugins.add( 'sourcearea',\r
+{\r
+       requires : [ 'editingblock' ],\r
+\r
+       init : function( editor )\r
+       {\r
+               var sourcearea = CKEDITOR.plugins.sourcearea;\r
+\r
+               editor.on( 'editingBlockReady', function()\r
+                       {\r
+                               var textarea,\r
+                                       onResize;\r
+\r
+                               editor.addMode( 'source',\r
+                                       {\r
+                                               load : function( holderElement, data )\r
+                                               {\r
+                                                       if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
+                                                               holderElement.setStyle( 'position', 'relative' );\r
+\r
+                                                       // Create the source area <textarea>.\r
+                                                       editor.textarea = textarea = new CKEDITOR.dom.element( 'textarea' );\r
+                                                       textarea.setAttributes(\r
+                                                               {\r
+                                                                       dir : 'ltr',\r
+                                                                       tabIndex : -1\r
+                                                               });\r
+                                                       textarea.addClass( 'cke_source' );\r
+\r
+                                                       var styles =\r
+                                                       {\r
+                                                               // IE7 has overflow the <textarea> from wrapping table cell.\r
+                                                               width   : CKEDITOR.env.ie7Compat ?  '99%' : '100%',\r
+                                                               height  : '100%',\r
+                                                               resize  : 'none',\r
+                                                               outline : 'none',\r
+                                                               'text-align' : 'left'\r
+                                                       };\r
+\r
+                                                       // The textarea height/width='100%' doesn't\r
+                                                       // constraint to the 'td' in IE strick mode\r
+                                                       if ( CKEDITOR.env.ie )\r
+                                                       {\r
+                                                               if ( !CKEDITOR.env.ie8Compat )\r
+                                                               {\r
+                                                                       onResize = function()\r
+                                                                               {\r
+                                                                                       // Holder rectange size is stretched by textarea,\r
+                                                                                       // so hide it just for a moment.\r
+                                                                                       textarea.hide();\r
+                                                                                       textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );\r
+                                                                                       // When we have proper holder size, show textarea again.\r
+                                                                                       textarea.show();\r
+                                                                               };\r
+                                                                       editor.on( 'resize', onResize );\r
+                                                                       styles.height = holderElement.$.clientHeight + 'px';\r
+                                                               }\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               // By some yet unknown reason, we must stop the\r
+                                                               // mousedown propagation for the textarea,\r
+                                                               // otherwise it's not possible to place the caret\r
+                                                               // inside of it (non IE).\r
+                                                               textarea.on( 'mousedown', function( evt )\r
+                                                                       {\r
+                                                                               evt = evt.data.$;\r
+                                                                               if ( evt.stopPropagation )\r
+                                                                                       evt.stopPropagation();\r
+                                                                       } );\r
+                                                       }\r
+\r
+                                                       // Reset the holder element and append the\r
+                                                       // <textarea> to it.\r
+                                                       holderElement.setHtml( '' );\r
+                                                       holderElement.append( textarea );\r
+                                                       textarea.setStyles( styles );\r
+\r
+                                                       // The editor data "may be dirty" after this point.\r
+                                                       editor.mayBeDirty = true;\r
+\r
+                                                       // Set the <textarea> value.\r
+                                                       this.loadData( data );\r
+\r
+                                                       var keystrokeHandler = editor.keystrokeHandler;\r
+                                                       if ( keystrokeHandler )\r
+                                                               keystrokeHandler.attach( textarea );\r
+\r
+                                                       setTimeout( function()\r
+                                                       {\r
+                                                               editor.mode = 'source';\r
+                                                               editor.fire( 'mode' );\r
+                                                       },\r
+                                                       ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );\r
+                                               },\r
+\r
+                                               loadData : function( data )\r
+                                               {\r
+                                                       textarea.setValue( data );\r
+                                               },\r
+\r
+                                               getData : function()\r
+                                               {\r
+                                                       return textarea.getValue();\r
+                                               },\r
+\r
+                                               getSnapshotData : function()\r
+                                               {\r
+                                                       return textarea.getValue();\r
+                                               },\r
+\r
+                                               unload : function( holderElement )\r
+                                               {\r
+                                                       editor.textarea = textarea = null;\r
+\r
+                                                       if ( onResize )\r
+                                                               editor.removeListener( 'resize', onResize );\r
+\r
+                                                       if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
+                                                               holderElement.removeStyle( 'position' );\r
+                                               },\r
+\r
+                                               focus : function()\r
+                                               {\r
+                                                       textarea.focus();\r
+                                               }\r
+                                       });\r
+                       });\r
+\r
+               editor.addCommand( 'source', sourcearea.commands.source );\r
+\r
+               if ( editor.ui.addButton )\r
+               {\r
+                       editor.ui.addButton( 'Source',\r
+                               {\r
+                                       label : editor.lang.source,\r
+                                       command : 'source'\r
+                               });\r
+               }\r
+\r
+               editor.on( 'mode', function()\r
+                       {\r
+                               editor.getCommand( 'source' ).setState(\r
+                                       editor.mode == 'source' ?\r
+                                               CKEDITOR.TRISTATE_ON :\r
+                                               CKEDITOR.TRISTATE_OFF );\r
+                       });\r
+       }\r
+});\r
+\r
+/**\r
+ * Holds the definition of commands an UI elements included with the sourcearea\r
+ * plugin.\r
+ * @example\r
+ */\r
+CKEDITOR.plugins.sourcearea =\r
+{\r
+       commands :\r
+       {\r
+               source :\r
+               {\r
+                       modes : { wysiwyg:1, source:1 },\r
+\r
+                       exec : function( editor )\r
+                       {\r
+                               if ( editor.mode == 'wysiwyg' )\r
+                                       editor.fire( 'saveSnapshot' );\r
+                               editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );\r
+                               editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );\r
+                       },\r
+\r
+                       canUndo : false\r
+               }\r
+       }\r
+};\r