JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / editingblock / plugin.js
diff --git a/_source/plugins/editingblock/plugin.js b/_source/plugins/editingblock/plugin.js
new file mode 100644 (file)
index 0000000..4d0885b
--- /dev/null
@@ -0,0 +1,236 @@
+/*\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 default editing block plugin, which holds the editing area\r
+ *             and source view.\r
+ */\r
+\r
+(function()\r
+{\r
+       var getMode = function( editor, mode )\r
+       {\r
+               return editor._.modes && editor._.modes[ mode || editor.mode ];\r
+       };\r
+\r
+       // This is a semaphore used to avoid recursive calls between\r
+       // the following data handling functions.\r
+       var isHandlingData;\r
+\r
+       CKEDITOR.plugins.add( 'editingblock',\r
+       {\r
+               init : function( editor )\r
+               {\r
+                       if ( !editor.config.editingBlock )\r
+                               return;\r
+\r
+                       editor.on( 'themeSpace', function( event )\r
+                               {\r
+                                       if ( event.data.space == 'contents' )\r
+                                               event.data.html += '<br>';\r
+                               });\r
+\r
+                       editor.on( 'themeLoaded', function()\r
+                               {\r
+                                       editor.fireOnce( 'editingBlockReady' );\r
+                               });\r
+\r
+                       editor.on( 'uiReady', function()\r
+                               {\r
+                                       editor.setMode( editor.config.startupMode );\r
+                               });\r
+\r
+                       editor.on( 'afterSetData', function()\r
+                               {\r
+                                       if ( !isHandlingData )\r
+                                       {\r
+                                               function setData()\r
+                                               {\r
+                                                       isHandlingData = true;\r
+                                                       getMode( editor ).loadData( editor.getData() );\r
+                                                       isHandlingData = false;\r
+                                               }\r
+\r
+                                               if ( editor.mode )\r
+                                                       setData();\r
+                                               else\r
+                                               {\r
+                                                       editor.on( 'mode', function()\r
+                                                               {\r
+                                                                       setData();\r
+                                                                       editor.removeListener( 'mode', arguments.callee );\r
+                                                               });\r
+                                               }\r
+                                       }\r
+                               });\r
+\r
+                       editor.on( 'beforeGetData', function()\r
+                               {\r
+                                       if ( !isHandlingData && editor.mode )\r
+                                       {\r
+                                               isHandlingData = true;\r
+                                               editor.setData( getMode( editor ).getData() );\r
+                                               isHandlingData = false;\r
+                                       }\r
+                               });\r
+\r
+                       editor.on( 'getSnapshot', function( event )\r
+                               {\r
+                                       if ( editor.mode )\r
+                                               event.data = getMode( editor ).getSnapshotData();\r
+                               });\r
+\r
+                       editor.on( 'loadSnapshot', function( event )\r
+                               {\r
+                                       if ( editor.mode )\r
+                                               getMode( editor ).loadSnapshotData( event.data );\r
+                               });\r
+\r
+                       // For the first "mode" call, we'll also fire the "instanceReady"\r
+                       // event.\r
+                       editor.on( 'mode', function( event )\r
+                               {\r
+                                       // Do that once only.\r
+                                       event.removeListener();\r
+\r
+                                       // Grab editor focus if the editor container is focused. (#3104)\r
+                                       var focusGrabber = editor.container;\r
+\r
+                                       // Safari 3 can't handle tabindex in all elements, so we do\r
+                                       // a trick to make it move the focus to the editor on TAB.\r
+                                       if ( CKEDITOR.env.webkit && CKEDITOR.env.version < 528 )\r
+                                       {\r
+                                               var tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;\r
+                                               focusGrabber = focusGrabber.append( CKEDITOR.dom.element.createFromHtml(\r
+                                                       '<input' +\r
+                                                               ' tabindex="' + tabIndex + '"' +\r
+                                                               ' style="position:absolute; left:-10000">' ) );\r
+                                       }\r
+\r
+                                       focusGrabber.on( 'focus', function()\r
+                                               {\r
+                                                       editor.focus();\r
+                                               });\r
+\r
+                                       if ( editor.config.startupFocus )\r
+                                               editor.focus();\r
+\r
+                                       // Fire instanceReady for both the editor and CKEDITOR, but\r
+                                       // defer this until the whole execution has completed\r
+                                       // to guarantee the editor is fully responsible.\r
+                                       setTimeout( function(){\r
+                                               editor.fireOnce( 'instanceReady' );\r
+                                               CKEDITOR.fire( 'instanceReady', null, editor );\r
+                                       } );\r
+                               });\r
+               }\r
+       });\r
+\r
+       /**\r
+        * The current editing mode. An editing mode is basically a viewport for\r
+        * editing or content viewing. By default the possible values for this\r
+        * property are "wysiwyg" and "source".\r
+        * @type String\r
+        * @example\r
+        * alert( CKEDITOR.instances.editor1.mode );  // "wysiwyg" (e.g.)\r
+        */\r
+       CKEDITOR.editor.prototype.mode = '';\r
+\r
+       /**\r
+        * Registers an editing mode. This function is to be used mainly by plugins.\r
+        * @param {String} mode The mode name.\r
+        * @param {Object} modeEditor The mode editor definition.\r
+        * @example\r
+        */\r
+       CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )\r
+       {\r
+               modeEditor.name = mode;\r
+               ( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;\r
+       };\r
+\r
+       /**\r
+        * Sets the current editing mode in this editor instance.\r
+        * @param {String} mode A registered mode name.\r
+        * @example\r
+        * // Switch to "source" view.\r
+        * CKEDITOR.instances.editor1.setMode( 'source' );\r
+        */\r
+       CKEDITOR.editor.prototype.setMode = function( mode )\r
+       {\r
+               var data,\r
+                       holderElement = this.getThemeSpace( 'contents' ),\r
+                       isDirty = this.checkDirty();\r
+\r
+               // Unload the previous mode.\r
+               if ( this.mode )\r
+               {\r
+                       if ( mode == this.mode )\r
+                               return;\r
+\r
+                       this.fire( 'beforeModeUnload' );\r
+\r
+                       var currentMode = getMode( this );\r
+                       data = currentMode.getData();\r
+                       currentMode.unload( holderElement );\r
+                       this.mode = '';\r
+               }\r
+\r
+               holderElement.setHtml( '' );\r
+\r
+               // Load required mode.\r
+               var modeEditor = getMode( this, mode );\r
+               if ( !modeEditor )\r
+                       throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';\r
+\r
+               if ( !isDirty )\r
+               {\r
+                       this.on( 'mode', function()\r
+                               {\r
+                                       this.resetDirty();\r
+                                       this.removeListener( 'mode', arguments.callee );\r
+                               });\r
+               }\r
+\r
+               modeEditor.load( holderElement, ( typeof data ) != 'string'  ? this.getData() : data);\r
+       };\r
+\r
+       /**\r
+        * Moves the selection focus to the editing are space in the editor.\r
+        */\r
+       CKEDITOR.editor.prototype.focus = function()\r
+       {\r
+               var mode = getMode( this );\r
+               if ( mode )\r
+                       mode.focus();\r
+       };\r
+})();\r
+\r
+/**\r
+ * The mode to load at the editor startup. It depends on the plugins\r
+ * loaded. By default, the "wysiwyg" and "source" modes are available.\r
+ * @type String\r
+ * @default 'wysiwyg'\r
+ * @example\r
+ * config.startupMode = 'source';\r
+ */\r
+CKEDITOR.config.startupMode = 'wysiwyg';\r
+\r
+/**\r
+ * Sets whether the editor should have the focus when the page loads.\r
+ * @type Boolean\r
+ * @default false\r
+ * @example\r
+ * config.startupFocus = true;\r
+ */\r
+CKEDITOR.config.startupFocus = false;\r
+\r
+/**\r
+ * Whether to render or not the editing block area in the editor interface.\r
+ * @type Boolean\r
+ * @default true\r
+ * @example\r
+ * config.editingBlock = false;\r
+ */\r
+CKEDITOR.config.editingBlock = true;\r