2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Undo/Redo system for saving shapshot for document modification
\r
8 * and other recordable changes.
\r
13 CKEDITOR.plugins.add( 'undo',
\r
15 requires : [ 'selection', 'wysiwygarea' ],
\r
17 init : function( editor )
\r
19 var undoManager = new UndoManager( editor );
\r
21 var undoCommand = editor.addCommand( 'undo',
\r
25 if ( undoManager.undo() )
\r
27 editor.selectionChange();
\r
28 this.fire( 'afterUndo' );
\r
31 state : CKEDITOR.TRISTATE_DISABLED,
\r
35 var redoCommand = editor.addCommand( 'redo',
\r
39 if ( undoManager.redo() )
\r
41 editor.selectionChange();
\r
42 this.fire( 'afterRedo' );
\r
45 state : CKEDITOR.TRISTATE_DISABLED,
\r
49 undoManager.onChange = function()
\r
51 undoCommand.setState( undoManager.undoable() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
\r
52 redoCommand.setState( undoManager.redoable() ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
\r
55 function recordCommand( event )
\r
57 // If the command hasn't been marked to not support undo.
\r
58 if ( undoManager.enabled && event.data.command.canUndo !== false )
\r
62 // We'll save snapshots before and after executing a command.
\r
63 editor.on( 'beforeCommandExec', recordCommand );
\r
64 editor.on( 'afterCommandExec', recordCommand );
\r
66 // Save snapshots before doing custom changes.
\r
67 editor.on( 'saveSnapshot', function()
\r
72 // Registering keydown on every document recreation.(#3844)
\r
73 editor.on( 'contentDom', function()
\r
75 editor.document.on( 'keydown', function( event )
\r
77 // Do not capture CTRL hotkeys.
\r
78 if ( !event.data.$.ctrlKey && !event.data.$.metaKey )
\r
79 undoManager.type( event );
\r
83 // Always save an undo snapshot - the previous mode might have
\r
84 // changed editor contents.
\r
85 editor.on( 'beforeModeUnload', function()
\r
87 editor.mode == 'wysiwyg' && undoManager.save( true );
\r
90 // Make the undo manager available only in wysiwyg mode.
\r
91 editor.on( 'mode', function()
\r
93 undoManager.enabled = editor.mode == 'wysiwyg';
\r
94 undoManager.onChange();
\r
97 editor.ui.addButton( 'Undo',
\r
99 label : editor.lang.undo,
\r
103 editor.ui.addButton( 'Redo',
\r
105 label : editor.lang.redo,
\r
109 editor.resetUndo = function()
\r
111 // Reset the undo stack.
\r
112 undoManager.reset();
\r
114 // Create the first image.
\r
115 editor.fire( 'saveSnapshot' );
\r
120 // Gets a snapshot image which represent the current document status.
\r
121 function Image( editor )
\r
123 var selection = editor.getSelection();
\r
125 this.contents = editor.getSnapshot();
\r
126 this.bookmarks = selection && selection.createBookmarks2( true );
\r
128 // In IE, we need to remove the expando attributes.
\r
129 if ( CKEDITOR.env.ie )
\r
130 this.contents = this.contents.replace( /\s+_cke_expando=".*?"/g, '' );
\r
133 // Attributes that browser may changing them when setting via innerHTML.
\r
134 var protectedAttrs = /\b(?:href|src|name)="[^"]*?"/gi;
\r
138 equals : function( otherImage, contentOnly )
\r
140 var thisContents = this.contents,
\r
141 otherContents = otherImage.contents;
\r
143 // For IE6/7 : Comparing only the protected attribute values but not the original ones.(#4522)
\r
144 if( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
146 thisContents = thisContents.replace( protectedAttrs, '' );
\r
147 otherContents = otherContents.replace( protectedAttrs, '' );
\r
150 if( thisContents != otherContents )
\r
156 var bookmarksA = this.bookmarks,
\r
157 bookmarksB = otherImage.bookmarks;
\r
159 if ( bookmarksA || bookmarksB )
\r
161 if ( !bookmarksA || !bookmarksB || bookmarksA.length != bookmarksB.length )
\r
164 for ( var i = 0 ; i < bookmarksA.length ; i++ )
\r
166 var bookmarkA = bookmarksA[ i ],
\r
167 bookmarkB = bookmarksB[ i ];
\r
170 bookmarkA.startOffset != bookmarkB.startOffset ||
\r
171 bookmarkA.endOffset != bookmarkB.endOffset ||
\r
172 !CKEDITOR.tools.arrayCompare( bookmarkA.start, bookmarkB.start ) ||
\r
173 !CKEDITOR.tools.arrayCompare( bookmarkA.end, bookmarkB.end ) )
\r
185 * @constructor Main logic for Redo/Undo feature.
\r
187 function UndoManager( editor )
\r
189 this.editor = editor;
\r
191 // Reset the undo stack.
\r
196 var editingKeyCodes = { /*Backspace*/ 8:1, /*Delete*/ 46:1 },
\r
197 modifierKeyCodes = { /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1 },
\r
198 navigationKeyCodes = { 37:1, 38:1, 39:1, 40:1 }; // Arrows: L, T, R, B
\r
200 UndoManager.prototype =
\r
203 * Process undo system regard keystrikes.
\r
204 * @param {CKEDITOR.dom.event} event
\r
206 type : function( event )
\r
208 var keystroke = event && event.data.getKey(),
\r
209 isModifierKey = keystroke in modifierKeyCodes,
\r
210 isEditingKey = keystroke in editingKeyCodes,
\r
211 wasEditingKey = this.lastKeystroke in editingKeyCodes,
\r
212 sameAsLastEditingKey = isEditingKey && keystroke == this.lastKeystroke,
\r
213 // Keystrokes which navigation through contents.
\r
214 isReset = keystroke in navigationKeyCodes,
\r
215 wasReset = this.lastKeystroke in navigationKeyCodes,
\r
217 // Keystrokes which just introduce new contents.
\r
218 isContent = ( !isEditingKey && !isReset ),
\r
220 // Create undo snap for every different modifier key.
\r
221 modifierSnapshot = ( isEditingKey && !sameAsLastEditingKey ),
\r
222 // Create undo snap on the following cases:
\r
223 // 1. Just start to type .
\r
224 // 2. Typing some content after a modifier.
\r
225 // 3. Typing some content after make a visible selection.
\r
226 startedTyping = !( isModifierKey || this.typing )
\r
227 || ( isContent && ( wasEditingKey || wasReset ) );
\r
229 if ( startedTyping || modifierSnapshot )
\r
231 var beforeTypeImage = new Image( this.editor );
\r
233 // Use setTimeout, so we give the necessary time to the
\r
234 // browser to insert the character into the DOM.
\r
235 CKEDITOR.tools.setTimeout( function()
\r
237 var currentSnapshot = this.editor.getSnapshot();
\r
239 // In IE, we need to remove the expando attributes.
\r
240 if ( CKEDITOR.env.ie )
\r
241 currentSnapshot = currentSnapshot.replace( /\s+_cke_expando=".*?"/g, '' );
\r
243 if ( beforeTypeImage.contents != currentSnapshot )
\r
245 // It's safe to now indicate typing state.
\r
246 this.typing = true;
\r
248 // This's a special save, with specified snapshot
\r
249 // and without auto 'fireChange'.
\r
250 if ( !this.save( false, beforeTypeImage, false ) )
\r
251 // Drop future snapshots.
\r
252 this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 );
\r
254 this.hasUndo = true;
\r
255 this.hasRedo = false;
\r
257 this.typesCount = 1;
\r
258 this.modifiersCount = 1;
\r
267 this.lastKeystroke = keystroke;
\r
269 // Create undo snap after typed too much (over 25 times).
\r
270 if ( isEditingKey )
\r
272 this.typesCount = 0;
\r
273 this.modifiersCount++;
\r
275 if ( this.modifiersCount > 25 )
\r
277 this.save( false, null, false );
\r
278 this.modifiersCount = 1;
\r
281 else if ( !isReset )
\r
283 this.modifiersCount = 0;
\r
286 if ( this.typesCount > 25 )
\r
288 this.save( false, null, false );
\r
289 this.typesCount = 1;
\r
295 reset : function() // Reset the undo stack.
\r
298 * Remember last pressed key.
\r
300 this.lastKeystroke = 0;
\r
303 * Stack for all the undo and redo snapshots, they're always created/removed
\r
306 this.snapshots = [];
\r
309 * Current snapshot history index.
\r
313 this.limit = this.editor.config.undoStackSize;
\r
315 this.currentImage = null;
\r
317 this.hasUndo = false;
\r
318 this.hasRedo = false;
\r
324 * Reset all states about typing.
\r
325 * @see UndoManager.type
\r
327 resetType : function()
\r
329 this.typing = false;
\r
330 delete this.lastKeystroke;
\r
331 this.typesCount = 0;
\r
332 this.modifiersCount = 0;
\r
334 fireChange : function()
\r
336 this.hasUndo = !!this.getNextImage( true );
\r
337 this.hasRedo = !!this.getNextImage( false );
\r
344 * Save a snapshot of document image for later retrieve.
\r
346 save : function( onContentOnly, image, autoFireChange )
\r
348 var snapshots = this.snapshots;
\r
350 // Get a content image.
\r
352 image = new Image( this.editor );
\r
354 // Check if this is a duplicate. In such case, do nothing.
\r
355 if ( this.currentImage && image.equals( this.currentImage, onContentOnly ) )
\r
358 // Drop future snapshots.
\r
359 snapshots.splice( this.index + 1, snapshots.length - this.index - 1 );
\r
361 // If we have reached the limit, remove the oldest one.
\r
362 if ( snapshots.length == this.limit )
\r
365 // Add the new image, updating the current index.
\r
366 this.index = snapshots.push( image ) - 1;
\r
368 this.currentImage = image;
\r
370 if ( autoFireChange !== false )
\r
375 restoreImage : function( image )
\r
377 this.editor.loadSnapshot( image.contents );
\r
379 if ( image.bookmarks )
\r
380 this.editor.getSelection().selectBookmarks( image.bookmarks );
\r
381 else if ( CKEDITOR.env.ie )
\r
383 // IE BUG: If I don't set the selection to *somewhere* after setting
\r
384 // document contents, then IE would create an empty paragraph at the bottom
\r
385 // the next time the document is modified.
\r
386 var $range = this.editor.document.getBody().$.createTextRange();
\r
387 $range.collapse( true );
\r
391 this.index = image.index;
\r
393 this.currentImage = image;
\r
398 // Get the closest available image.
\r
399 getNextImage : function( isUndo )
\r
401 var snapshots = this.snapshots,
\r
402 currentImage = this.currentImage,
\r
405 if ( currentImage )
\r
409 for ( i = this.index - 1 ; i >= 0 ; i-- )
\r
411 image = snapshots[ i ];
\r
412 if ( !currentImage.equals( image, true ) )
\r
421 for ( i = this.index + 1 ; i < snapshots.length ; i++ )
\r
423 image = snapshots[ i ];
\r
424 if ( !currentImage.equals( image, true ) )
\r
437 * Check the current redo state.
\r
438 * @return {Boolean} Whether the document has previous state to
\r
441 redoable : function()
\r
443 return this.enabled && this.hasRedo;
\r
447 * Check the current undo state.
\r
448 * @return {Boolean} Whether the document has future state to restore.
\r
450 undoable : function()
\r
452 return this.enabled && this.hasUndo;
\r
456 * Perform undo on current index.
\r
460 if ( this.undoable() )
\r
464 var image = this.getNextImage( true );
\r
466 return this.restoreImage( image ), true;
\r
473 * Perform redo on current index.
\r
477 if ( this.redoable() )
\r
479 // Try to save. If no changes have been made, the redo stack
\r
480 // will not change, so it will still be redoable.
\r
483 // If instead we had changes, we can't redo anymore.
\r
484 if ( this.redoable() )
\r
486 var image = this.getNextImage( false );
\r
488 return this.restoreImage( image ), true;
\r
498 * The number of undo steps to be saved. The higher this setting value the more
\r
499 * memory is used for it.
\r
503 * config.undoStackSize = 50;
\r
505 CKEDITOR.config.undoStackSize = 20;
\r