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