| | 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 Undo/Redo system for saving shapshot for document modification |
| | 8 | * and other recordable changes. |
| | 9 | */ |
| | 10 | |
| | 11 | (function() |
| | 12 | { |
| | 13 | CKEDITOR.plugins.add( 'undo', |
| | 14 | { |
| | 15 | requires : [ 'selection', 'wysiwygarea' ], |
| | 16 | |
| | 17 | init : function( editor ) |
| | 18 | { |
| | 19 | var undoManager = new UndoManager( editor ); |
| | 20 | |
| | 21 | var undoCommand = editor.addCommand( 'undo', |
| | 22 | { |
| | 23 | exec : function() |
| | 24 | { |
| | 25 | if ( undoManager.undo() ) |
| | 26 | { |
| | 27 | editor.selectionChange(); |
| | 28 | this.fire( 'afterUndo' ); |
| | 29 | } |
| | 30 | }, |
| | 31 | state : CKEDITOR.TRISTATE_DISABLED, |
| | 32 | canUndo : false |
| | 33 | }); |
| | 34 | |
| | 35 | var redoCommand = editor.addCommand( 'redo', |
| | 36 | { |
| | 37 | exec : function() |
| | 38 | { |
| | 39 | if ( undoManager.redo() ) |
| | 40 | { |
| | 41 | editor.selectionChange(); |
| | 42 | this.fire( 'afterRedo' ); |
| | 43 | } |
| | 44 | }, |
| | 45 | state : CKEDITOR.TRISTATE_DISABLED, |
| | 46 | canUndo : false |
| | 47 | }); |
| | 48 | |
| | 49 | undoManager.onChange = function() |
| | 50 | { |
| | 51 | undoCommand.setState( undoManager.undoable() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED ); |
| | 52 | redoCommand.setState( undoManager.redoable() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED ); |
| | 53 | }; |
| | 54 | |
| | 55 | function recordCommand( event ) |
| | 56 | { |
| | 57 | // If the command hasn't been marked to not support undo. |
| | 58 | if ( undoManager.enabled && event.data.command.canUndo !== false ) |
| | 59 | undoManager.save( event.name == 'afterCommandExec' ? 'R' : 'U' ); |
| | 60 | } |
| | 61 | |
| | 62 | // We'll save snapshots before and after executing a command. |
| | 63 | editor.on( 'beforeCommandExec', recordCommand ); |
| | 64 | editor.on( 'afterCommandExec', recordCommand ); |
| | 65 | |
| | 66 | // Make the undo manager available only in wysiwyg mode. |
| | 67 | editor.on( 'mode', function() |
| | 68 | { |
| | 69 | if ( !undoManager.enabled && editor.mode == 'wysiwyg' ) |
| | 70 | { |
| | 71 | undoManager.enabled = true; |
| | 72 | |
| | 73 | editor.document.on( 'keydown', function() |
| | 74 | { |
| | 75 | undoManager.type(); |
| | 76 | }); |
| | 77 | |
| | 78 | // Being this the first call, let's get an undo snapshot. |
| | 79 | if ( undoManager.index == -1 ) |
| | 80 | undoManager.save(); |
| | 81 | } |
| | 82 | |
| | 83 | undoManager.onChange(); |
| | 84 | }); |
| | 85 | |
| | 86 | editor.ui.addButton( 'Undo', |
| | 87 | { |
| | 88 | label : editor.lang.undo, |
| | 89 | command : 'undo' |
| | 90 | }); |
| | 91 | |
| | 92 | editor.ui.addButton( 'Redo', |
| | 93 | { |
| | 94 | label : editor.lang.redo, |
| | 95 | command : 'redo' |
| | 96 | }); |
| | 97 | } |
| | 98 | }); |
| | 99 | |
| | 100 | // Gets a snapshot image which represent the current document status. |
| | 101 | function Image( editor ) |
| | 102 | { |
| | 103 | this.contents = editor.getSnapshot(); |
| | 104 | this.bookmarks = editor.getSelection().createBookmarks2(); |
| | 105 | } |
| | 106 | |
| | 107 | Image.prototype = |
| | 108 | { |
| | 109 | equals : function( otherImage, contentOnly ) |
| | 110 | { |
| | 111 | if ( this.contents != otherImage.contents ) |
| | 112 | return false; |
| | 113 | |
| | 114 | if ( contentOnly ) |
| | 115 | return true; |
| | 116 | |
| | 117 | var bookmarksA = this.bookmarks, |
| | 118 | bookmarksB = otherImage.bookmarks; |
| | 119 | |
| | 120 | if ( bookmarksA.length != bookmarksB.length ) |
| | 121 | return false; |
| | 122 | |
| | 123 | for ( var i = 0 ; i < bookmarksA.length ; i++ ) |
| | 124 | { |
| | 125 | var bookmarkA = bookmarksA[ i ], |
| | 126 | bookmarkB = bookmarksB[ i ]; |
| | 127 | |
| | 128 | if ( |
| | 129 | bookmarkA.startOffset != bookmarkB.startOffset || |
| | 130 | bookmarkA.endOffset != bookmarkB.endOffset || |
| | 131 | !CKEDITOR.tools.arrayCompare( bookmarkA.start, bookmarkB.start ) || |
| | 132 | !CKEDITOR.tools.arrayCompare( bookmarkA.end, bookmarkB.end ) ) |
| | 133 | { |
| | 134 | return false; |
| | 135 | } |
| | 136 | } |
| | 137 | |
| | 138 | return true; |
| | 139 | } |
| | 140 | }; |
| | 141 | |
| | 142 | /** |
| | 143 | * @constructor Main logic for Redo/Undo feature. |
| | 144 | */ |
| | 145 | function UndoManager( editor ) |
| | 146 | { |
| | 147 | /** |
| | 148 | * Indicates that the Undo/Redo features are avaialable and effective. |
| | 149 | */ |
| | 150 | this.enabled = false; |
| | 151 | |
| | 152 | this.typing = false; |
| | 153 | this.typesCount = 0; |
| | 154 | |
| | 155 | this.editor = editor; |
| | 156 | |
| | 157 | /** |
| | 158 | * Stack for all the undo and redo snapshots, they're always created/removed |
| | 159 | * in consistency. |
| | 160 | */ |
| | 161 | this.snapshots = []; |
| | 162 | |
| | 163 | /** |
| | 164 | * Current snapshot history index. |
| | 165 | */ |
| | 166 | this.index = -1; |
| | 167 | |
| | 168 | this.limit = editor.config.maxUndoLevels; |
| | 169 | } |
| | 170 | |
| | 171 | UndoManager.prototype = |
| | 172 | { |
| | 173 | type : function() |
| | 174 | { |
| | 175 | if ( !this.typing ) |
| | 176 | { |
| | 177 | this.save( 'U' ); |
| | 178 | this.typing = true; |
| | 179 | } |
| | 180 | |
| | 181 | this.typesCount++; |
| | 182 | |
| | 183 | if ( this.typesCount > 25 ) |
| | 184 | { |
| | 185 | this.typesCount = 0; |
| | 186 | this.save(); |
| | 187 | } |
| | 188 | }, |
| | 189 | |
| | 190 | /** |
| | 191 | * Save a snapshot of document image for later retrieve. |
| | 192 | */ |
| | 193 | save : function( type, onContentOnly ) |
| | 194 | { |
| | 195 | var snapshots = this.snapshots; |
| | 196 | |
| | 197 | // Get a content image. |
| | 198 | var image = new Image( this.editor ); |
| | 199 | |
| | 200 | // Check if this is a duplicate. In such case, do nothing. |
| | 201 | if ( this.index > -1 && image.equals( snapshots[ this.index ], onContentOnly ) ) |
| | 202 | return; |
| | 203 | |
| | 204 | image.type = type || 'UR'; |
| | 205 | |
| | 206 | // Drop future snapshots. |
| | 207 | snapshots.splice( this.index + 1, snapshots.length - this.index - 1 ); |
| | 208 | |
| | 209 | // If we have reached the limit, remove the oldest one. |
| | 210 | if ( snapshots.length == this.limit ) |
| | 211 | snapshots.shift(); |
| | 212 | |
| | 213 | // Add the new image, updating the current index. |
| | 214 | this.index = snapshots.push( image ) - 1; |
| | 215 | |
| | 216 | this.onChange(); |
| | 217 | }, |
| | 218 | |
| | 219 | restoreImage : function( index ) |
| | 220 | { |
| | 221 | var image = this.snapshots[ index ]; |
| | 222 | |
| | 223 | this.editor.loadSnapshot( image.contents ); |
| | 224 | this.editor.getSelection().selectBookmarks( image.bookmarks ); |
| | 225 | |
| | 226 | this.index = index; |
| | 227 | |
| | 228 | this.onChange(); |
| | 229 | }, |
| | 230 | |
| | 231 | /** |
| | 232 | * Check the current redo state. |
| | 233 | * @return {Boolean} Whether the document has previous state to |
| | 234 | * retrieve. |
| | 235 | */ |
| | 236 | redoable : function() |
| | 237 | { |
| | 238 | return this.enabled && ( this.index < this.snapshots.length - 1 ); |
| | 239 | }, |
| | 240 | |
| | 241 | /** |
| | 242 | * Check the current undo state. |
| | 243 | * @return {Boolean} Whether the document has future state to restore. |
| | 244 | */ |
| | 245 | undoable : function() |
| | 246 | { |
| | 247 | return this.enabled && ( this.index > 0 ); |
| | 248 | }, |
| | 249 | |
| | 250 | /** |
| | 251 | * Perform undo on current index. |
| | 252 | */ |
| | 253 | undo : function() |
| | 254 | { |
| | 255 | if ( this.undoable() ) |
| | 256 | { |
| | 257 | this.save( 'UR', true ); |
| | 258 | |
| | 259 | var index = this.index; |
| | 260 | |
| | 261 | while ( --index >= 0 ) |
| | 262 | { |
| | 263 | var image = this.snapshots[ index ]; |
| | 264 | if ( image.type == 'U' || image.type == 'UR' ) |
| | 265 | return this.restoreImage( index ), true; |
| | 266 | } |
| | 267 | } |
| | 268 | |
| | 269 | return false; |
| | 270 | }, |
| | 271 | |
| | 272 | /** |
| | 273 | * Perform redo on current index. |
| | 274 | */ |
| | 275 | redo : function() |
| | 276 | { |
| | 277 | if ( this.redoable() ) |
| | 278 | { |
| | 279 | // Try to save. If no changes have been made, the redo stack |
| | 280 | // will not change, so it will still be redoable. |
| | 281 | this.save( 'UR', true ); |
| | 282 | |
| | 283 | // If instead we had changes, we can't redo anymore. |
| | 284 | if ( this.redoable() ) |
| | 285 | { |
| | 286 | var index = this.index; |
| | 287 | |
| | 288 | while ( ++index < this.snapshots.length ) |
| | 289 | { |
| | 290 | var image = this.snapshots[ index ]; |
| | 291 | if ( image.type == 'R' || image.type == 'UR' ) |
| | 292 | return this.restoreImage( index ), true; |
| | 293 | } |
| | 294 | } |
| | 295 | } |
| | 296 | |
| | 297 | return false; |
| | 298 | } |
| | 299 | }; |
| | 300 | })(); |
| | 301 | |
| | 302 | CKEDITOR.config.maxUndoLevels = 20; |
| | 303 | No newline at end of file |