Ticket #3372: 3372_6.patch
File 3372_6.patch, 4.6 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 isSelected = !this.editor.getSelection().getRanges()[0].collapsed, 203 204 // Backspace, Delete 205 modifierCodes = { 8:1, 46:1 }, 206 // Keystrokes which will modify the contents. 207 isModifier = keystroke in modifierCodes, 208 wasModifier = this.lastKeystroke in modifierCodes, 209 lastWasSameModifier = isModifier && keystroke == this.lastKeystroke, 210 211 // Arrows: L, T, R, B 212 resetTypingCodes = { 37:1, 38:1, 39:1, 40:1 }, 213 // Keystrokes which navigation through contents. 214 isReset = keystroke in resetTypingCodes, 215 wasReset = this.lastKeystroke in resetTypingCodes, 216 217 // Keystrokes which just introduce new contents. 218 isContent = ( !isModifier && !isReset ), 219 220 // Create undo snap for every different modifier key. 221 modifierSnapshot = ( isModifier && !lastWasSameModifier ), 222 // Create undo snap on the following cases: 223 // 1. Just start to type. 224 // 2. Typing some content after a modifier. 225 // 3. Typing some content after make a visible selection. 226 startedTyping = !this.typing 227 || ( isContent && ( wasModifier || wasReset ) ) 228 || ( isContent && isSelected ); 229 230 if ( startedTyping || modifierSnapshot ) 192 231 { 193 232 var beforeTypeImage = new Image( this.editor ); 194 233 … … 204 243 205 244 if ( beforeTypeImage.contents != currentSnapshot ) 206 245 { 207 if ( !this.save( false, beforeTypeImage ) ) 208 { 246 // This's a special save, with specified snapshot 247 // and without auto 'fireChange'. 248 if ( !this.save( false, beforeTypeImage, false ) ) 209 249 // Drop future snapshots. 210 250 this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 ); 211 }212 251 213 252 this.hasUndo = true; 214 253 this.hasRedo = false; 215 254 216 255 this.typesCount = 1; 217 this. typing = true;256 this.modifiersCount = 1; 218 257 219 258 this.onChange(); 220 259 } 221 260 }, 222 0, this ); 223 224 return; 261 0, this 262 ); 225 263 } 226 264 227 this.typesCount++; 265 this.lastKeystroke = keystroke; 266 // Create undo snap after typed too much (over 25 times). 267 if ( isModifier ) 268 { 269 this.typesCount = 0; 270 this.modifiersCount++; 271 272 if ( this.modifiersCount > 25 ) 273 { 274 this.save(); 275 this.modifiersCount = 1; 276 } 277 } 278 else if ( !isReset ) 279 { 280 this.modifiersCount = 0; 281 this.typesCount++; 228 282 229 if ( this.typesCount > 25 ) 230 { 231 this.save(); 232 this.typesCount = 1; 233 } 283 if ( this.typesCount > 25 ) 284 { 285 this.save(); 286 this.typesCount = 1; 287 } 288 } 234 289 235 290 this.typing = true; 236 291 }, 237 292 293 /** 294 * Reset all states about typing. 295 * @see UndoManager.type 296 */ 297 resetType : function() 298 { 299 this.typing = false; 300 delete this.lastKeystroke; 301 this.typesCount = 0; 302 this.modifiersCount = 0; 303 }, 238 304 fireChange : function() 239 305 { 240 306 this.hasUndo = !!this.getNextImage( true ); 241 307 this.hasRedo = !!this.getNextImage( false ); 242 243 this.typing = false; 244 this.typesCount = 0; 245 308 // Reset typing 309 this.resetType(); 246 310 this.onChange(); 247 311 }, 248 312 249 313 /** 250 314 * Save a snapshot of document image for later retrieve. 251 315 */ 252 save : function( onContentOnly, image )316 save : function( onContentOnly, image, autoFireChange ) 253 317 { 254 318 var snapshots = this.snapshots; 255 319 … … 273 337 274 338 this.currentImage = image; 275 339 276 this.fireChange();277 340 if ( autoFireChange !== false ) 341 this.fireChange(); 278 342 return true; 279 343 }, 280 344