Ticket #3372: 3372_7.patch
File 3372_7.patch, 4.5 KB (added by , 15 years ago) |
---|
-
_source/plugins/undo/plugin.js
82 82 { 83 83 // Do not capture CTRL hotkeys. 84 84 if ( !event.data.$.ctrlKey && !event.data.$.metaKey ) 85 undoManager.type( );85 undoManager.type( event ); 86 86 }); 87 87 88 88 // Being this the first call, let's get an undo snapshot. … … 167 167 function UndoManager( editor ) 168 168 { 169 169 this.typesCount = 0; 170 this.modifiersCount = 0; 170 171 171 172 this.editor = editor; 172 173 … … 182 183 this.index = -1; 183 184 184 185 this.limit = editor.config.undoStackSize; 186 187 /** 188 * Remember last pressed key. 189 */ 190 this.lastKeystroke = 0; 185 191 } 186 192 187 193 UndoManager.prototype = 188 194 { 189 type : function() 195 /** 196 * Process undo system regard keystrikes. 197 * @param {CKEDITOR.dom.event} event 198 */ 199 type : function( event ) 190 200 { 191 if ( !this.typing ) 201 var keystroke = event && event.data.getKeystroke(), 202 203 // Backspace, Delete 204 modifierCodes = { 8:1, 46:1 }, 205 // Keystrokes which will modify the contents. 206 isModifier = keystroke in modifierCodes, 207 wasModifier = this.lastKeystroke in modifierCodes, 208 lastWasSameModifier = isModifier && keystroke == this.lastKeystroke, 209 210 // Arrows: L, T, R, B 211 resetTypingCodes = { 37:1, 38:1, 39:1, 40:1 }, 212 // Keystrokes which navigation through contents. 213 isReset = keystroke in resetTypingCodes, 214 wasReset = this.lastKeystroke in resetTypingCodes, 215 216 // Keystrokes which just introduce new contents. 217 isContent = ( !isModifier && !isReset ), 218 219 // Create undo snap for every different modifier key. 220 modifierSnapshot = ( isModifier && !lastWasSameModifier ), 221 // Create undo snap on the following cases: 222 // 1. Just start to type. 223 // 2. Typing some content after a modifier. 224 // 3. Typing some content after make a visible selection. 225 startedTyping = !this.typing 226 || ( isContent && ( wasModifier || wasReset ) ); 227 228 if ( startedTyping || modifierSnapshot ) 192 229 { 193 230 var beforeTypeImage = new Image( this.editor ); 194 231 … … 204 241 205 242 if ( beforeTypeImage.contents != currentSnapshot ) 206 243 { 207 if ( !this.save( false, beforeTypeImage ) ) 208 { 244 // This's a special save, with specified snapshot 245 // and without auto 'fireChange'. 246 if ( !this.save( false, beforeTypeImage, false ) ) 209 247 // Drop future snapshots. 210 248 this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 ); 211 }212 249 213 250 this.hasUndo = true; 214 251 this.hasRedo = false; 215 252 216 253 this.typesCount = 1; 217 this. typing = true;254 this.modifiersCount = 1; 218 255 219 256 this.onChange(); 220 257 } 221 258 }, 222 0, this ); 223 224 return; 259 0, this 260 ); 225 261 } 226 262 227 this.typesCount++; 263 this.lastKeystroke = keystroke; 264 // Create undo snap after typed too much (over 25 times). 265 if ( isModifier ) 266 { 267 this.typesCount = 0; 268 this.modifiersCount++; 269 270 if ( this.modifiersCount > 25 ) 271 { 272 this.save(); 273 this.modifiersCount = 1; 274 } 275 } 276 else if ( !isReset ) 277 { 278 this.modifiersCount = 0; 279 this.typesCount++; 228 280 229 if ( this.typesCount > 25 ) 230 { 231 this.save(); 232 this.typesCount = 1; 233 } 281 if ( this.typesCount > 25 ) 282 { 283 this.save(); 284 this.typesCount = 1; 285 } 286 } 234 287 235 288 this.typing = true; 236 289 }, 237 290 291 /** 292 * Reset all states about typing. 293 * @see UndoManager.type 294 */ 295 resetType : function() 296 { 297 this.typing = false; 298 delete this.lastKeystroke; 299 this.typesCount = 0; 300 this.modifiersCount = 0; 301 }, 238 302 fireChange : function() 239 303 { 240 304 this.hasUndo = !!this.getNextImage( true ); 241 305 this.hasRedo = !!this.getNextImage( false ); 242 243 this.typing = false; 244 this.typesCount = 0; 245 306 // Reset typing 307 this.resetType(); 246 308 this.onChange(); 247 309 }, 248 310 249 311 /** 250 312 * Save a snapshot of document image for later retrieve. 251 313 */ 252 save : function( onContentOnly, image )314 save : function( onContentOnly, image, autoFireChange ) 253 315 { 254 316 var snapshots = this.snapshots; 255 317 … … 273 335 274 336 this.currentImage = image; 275 337 276 this.fireChange();277 338 if ( autoFireChange !== false ) 339 this.fireChange(); 278 340 return true; 279 341 }, 280 342