JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[ckeditor.git] / _source / plugins / undo / plugin.js
index fcdd2a4..fde581f 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -114,21 +114,49 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // Create the first image.\r
                                editor.fire( 'saveSnapshot' );\r
                        };\r
+\r
+                       /**\r
+                        * Update the undo stacks with any subsequent DOM changes after this call.\r
+                        * @name CKEDITOR.editor#updateUndo\r
+                        * @example\r
+                        * function()\r
+                        * {\r
+                        * editor.fire( 'updateSnapshot' );\r
+                        * ...\r
+                        *  // Ask to include subsequent (in this call stack) DOM changes to be\r
+                        * // considered as part of the first snapshot.\r
+                        *      editor.fire( 'updateSnapshot' );\r
+                        *      editor.document.body.append(...);\r
+                        * ...\r
+                        * }\r
+                        */\r
+                       editor.on( 'updateSnapshot', function()\r
+                       {\r
+                               if ( undoManager.currentImage && new Image( editor ).equals( undoManager.currentImage ) )\r
+                                       setTimeout( function () { undoManager.update(); }, 0 );\r
+                       });\r
                }\r
        });\r
 \r
-       // Gets a snapshot image which represent the current document status.\r
-       function Image( editor )\r
-       {\r
-               var selection = editor.getSelection();\r
+       CKEDITOR.plugins.undo = {};\r
 \r
-               this.contents   = editor.getSnapshot();\r
-               this.bookmarks  = selection && selection.createBookmarks2( true );\r
+       /**\r
+        * Undo snapshot which represents the current document status.\r
+        * @name CKEDITOR.plugins.undo.Image\r
+        * @param editor The editor instance on which the image is created.\r
+        */\r
+       var Image = CKEDITOR.plugins.undo.Image = function( editor )\r
+       {\r
+               this.editor = editor;\r
+               var contents = editor.getSnapshot(),\r
+                       selection       = contents && editor.getSelection();\r
 \r
                // In IE, we need to remove the expando attributes.\r
-               if ( CKEDITOR.env.ie )\r
-                       this.contents = this.contents.replace( /\s+_cke_expando=".*?"/g, '' );\r
-       }\r
+               CKEDITOR.env.ie && contents && ( contents = contents.replace( /\s+_cke_expando=".*?"/g, '' ) );\r
+\r
+               this.contents   = contents;\r
+               this.bookmarks  = selection && selection.createBookmarks2( true );\r
+       };\r
 \r
        // Attributes that browser may changing them when setting via innerHTML.\r
        var protectedAttrs = /\b(?:href|src|name)="[^"]*?"/gi;\r
@@ -137,17 +165,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\r
                equals : function( otherImage, contentOnly )\r
                {\r
+\r
                        var thisContents = this.contents,\r
                                otherContents = otherImage.contents;\r
 \r
                        // For IE6/7 : Comparing only the protected attribute values but not the original ones.(#4522)\r
-                       if( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )\r
+                       if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )\r
                        {\r
                                thisContents = thisContents.replace( protectedAttrs, '' );\r
                                otherContents = otherContents.replace( protectedAttrs, '' );\r
                        }\r
 \r
-                       if( thisContents != otherContents )\r
+                       if ( thisContents != otherContents )\r
                                return false;\r
 \r
                        if ( contentOnly )\r
@@ -242,6 +271,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                if ( beforeTypeImage.contents != currentSnapshot )\r
                                                {\r
+                                                       // It's safe to now indicate typing state.\r
+                                                       this.typing = true;\r
+\r
                                                        // This's a special save, with specified snapshot\r
                                                        // and without auto 'fireChange'.\r
                                                        if ( !this.save( false, beforeTypeImage, false ) )\r
@@ -263,9 +295,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        this.lastKeystroke = keystroke;\r
 \r
-                       // Ignore modifier keys. (#4673)\r
-                       if( isModifierKey )\r
-                               return;\r
                        // Create undo snap after typed too much (over 25 times).\r
                        if ( isEditingKey )\r
                        {\r
@@ -274,7 +303,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                if ( this.modifiersCount > 25 )\r
                                {\r
-                                       this.save();\r
+                                       this.save( false, null, false );\r
                                        this.modifiersCount = 1;\r
                                }\r
                        }\r
@@ -285,12 +314,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                if ( this.typesCount > 25 )\r
                                {\r
-                                       this.save();\r
+                                       this.save( false, null, false );\r
                                        this.typesCount = 1;\r
                                }\r
                        }\r
 \r
-                       this.typing = true;\r
                },\r
 \r
                reset : function()      // Reset the undo stack.\r
@@ -352,6 +380,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        if ( !image )\r
                                image = new Image( this.editor );\r
 \r
+                       // Do nothing if it was not possible to retrieve an image.\r
+                       if ( image.contents === false )\r
+                               return false;\r
+\r
                        // Check if this is a duplicate. In such case, do nothing.\r
                        if ( this.currentImage && image.equals( this.currentImage, onContentOnly ) )\r
                                return false;\r
@@ -391,8 +423,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        this.index = image.index;\r
 \r
-                       this.currentImage = image;\r
-\r
+                       // Update current image with the actual editor\r
+                       // content, since actualy content may differ from\r
+                       // the original snapshot due to dom change. (#4622)\r
+                       this.update();\r
                        this.fireChange();\r
                },\r
 \r
@@ -491,6 +525,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
 \r
                        return false;\r
+               },\r
+\r
+               /**\r
+                * Update the last snapshot of the undo stack with the current editor content.\r
+                */\r
+               update : function()\r
+               {\r
+                       this.snapshots.splice( this.index, 1, ( this.currentImage = new Image( this.editor ) ) );\r
                }\r
        };\r
 })();\r
@@ -504,3 +546,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
  * config.undoStackSize = 50;\r
  */\r
 CKEDITOR.config.undoStackSize = 20;\r
+\r
+/**\r
+ * Fired when the editor is about to save an undo snapshot. This event can be\r
+ * fired by plugins and customizations to make the editor saving undo snapshots.\r
+ * @name CKEDITOR.editor#saveSnapshot\r
+ * @event\r
+ */\r