2 Copyright (c) 2003-2009, 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
135 equals : function( otherImage, contentOnly )
\r
137 if ( this.contents != otherImage.contents )
\r
143 var bookmarksA = this.bookmarks,
\r
144 bookmarksB = otherImage.bookmarks;
\r
146 if ( bookmarksA || bookmarksB )
\r
148 if ( !bookmarksA || !bookmarksB || bookmarksA.length != bookmarksB.length )
\r
151 for ( var i = 0 ; i < bookmarksA.length ; i++ )
\r
153 var bookmarkA = bookmarksA[ i ],
\r
154 bookmarkB = bookmarksB[ i ];
\r
157 bookmarkA.startOffset != bookmarkB.startOffset ||
\r
158 bookmarkA.endOffset != bookmarkB.endOffset ||
\r
159 !CKEDITOR.tools.arrayCompare( bookmarkA.start, bookmarkB.start ) ||
\r
160 !CKEDITOR.tools.arrayCompare( bookmarkA.end, bookmarkB.end ) )
\r
172 * @constructor Main logic for Redo/Undo feature.
\r
174 function UndoManager( editor )
\r
176 this.editor = editor;
\r
178 // Reset the undo stack.
\r
182 UndoManager.prototype =
\r
185 * Process undo system regard keystrikes.
\r
186 * @param {CKEDITOR.dom.event} event
\r
188 type : function( event )
\r
190 var keystroke = event && event.data.getKeystroke(),
\r
192 // Backspace, Delete
\r
193 modifierCodes = { 8:1, 46:1 },
\r
194 // Keystrokes which will modify the contents.
\r
195 isModifier = keystroke in modifierCodes,
\r
196 wasModifier = this.lastKeystroke in modifierCodes,
\r
197 lastWasSameModifier = isModifier && keystroke == this.lastKeystroke,
\r
199 // Arrows: L, T, R, B
\r
200 resetTypingCodes = { 37:1, 38:1, 39:1, 40:1 },
\r
201 // Keystrokes which navigation through contents.
\r
202 isReset = keystroke in resetTypingCodes,
\r
203 wasReset = this.lastKeystroke in resetTypingCodes,
\r
205 // Keystrokes which just introduce new contents.
\r
206 isContent = ( !isModifier && !isReset ),
\r
208 // Create undo snap for every different modifier key.
\r
209 modifierSnapshot = ( isModifier && !lastWasSameModifier ),
\r
210 // Create undo snap on the following cases:
\r
211 // 1. Just start to type.
\r
212 // 2. Typing some content after a modifier.
\r
213 // 3. Typing some content after make a visible selection.
\r
214 startedTyping = !this.typing
\r
215 || ( isContent && ( wasModifier || wasReset ) );
\r
217 if ( startedTyping || modifierSnapshot )
\r
219 var beforeTypeImage = new Image( this.editor );
\r
221 // Use setTimeout, so we give the necessary time to the
\r
222 // browser to insert the character into the DOM.
\r
223 CKEDITOR.tools.setTimeout( function()
\r
225 var currentSnapshot = this.editor.getSnapshot();
\r
227 // In IE, we need to remove the expando attributes.
\r
228 if ( CKEDITOR.env.ie )
\r
229 currentSnapshot = currentSnapshot.replace( /\s+_cke_expando=".*?"/g, '' );
\r
231 if ( beforeTypeImage.contents != currentSnapshot )
\r
233 // This's a special save, with specified snapshot
\r
234 // and without auto 'fireChange'.
\r
235 if ( !this.save( false, beforeTypeImage, false ) )
\r
236 // Drop future snapshots.
\r
237 this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 );
\r
239 this.hasUndo = true;
\r
240 this.hasRedo = false;
\r
242 this.typesCount = 1;
\r
243 this.modifiersCount = 1;
\r
252 this.lastKeystroke = keystroke;
\r
253 // Create undo snap after typed too much (over 25 times).
\r
256 this.typesCount = 0;
\r
257 this.modifiersCount++;
\r
259 if ( this.modifiersCount > 25 )
\r
262 this.modifiersCount = 1;
\r
265 else if ( !isReset )
\r
267 this.modifiersCount = 0;
\r
270 if ( this.typesCount > 25 )
\r
273 this.typesCount = 1;
\r
277 this.typing = true;
\r
280 reset : function() // Reset the undo stack.
\r
283 * Remember last pressed key.
\r
285 this.lastKeystroke = 0;
\r
288 * Stack for all the undo and redo snapshots, they're always created/removed
\r
291 this.snapshots = [];
\r
294 * Current snapshot history index.
\r
298 this.limit = this.editor.config.undoStackSize;
\r
300 this.currentImage = null;
\r
302 this.hasUndo = false;
\r
303 this.hasRedo = false;
\r
309 * Reset all states about typing.
\r
310 * @see UndoManager.type
\r
312 resetType : function()
\r
314 this.typing = false;
\r
315 delete this.lastKeystroke;
\r
316 this.typesCount = 0;
\r
317 this.modifiersCount = 0;
\r
319 fireChange : function()
\r
321 this.hasUndo = !!this.getNextImage( true );
\r
322 this.hasRedo = !!this.getNextImage( false );
\r
329 * Save a snapshot of document image for later retrieve.
\r
331 save : function( onContentOnly, image, autoFireChange )
\r
333 var snapshots = this.snapshots;
\r
335 // Get a content image.
\r
337 image = new Image( this.editor );
\r
339 // Check if this is a duplicate. In such case, do nothing.
\r
340 if ( this.currentImage && image.equals( this.currentImage, onContentOnly ) )
\r
343 // Drop future snapshots.
\r
344 snapshots.splice( this.index + 1, snapshots.length - this.index - 1 );
\r
346 // If we have reached the limit, remove the oldest one.
\r
347 if ( snapshots.length == this.limit )
\r
350 // Add the new image, updating the current index.
\r
351 this.index = snapshots.push( image ) - 1;
\r
353 this.currentImage = image;
\r
355 if ( autoFireChange !== false )
\r
360 restoreImage : function( image )
\r
362 this.editor.loadSnapshot( image.contents );
\r
364 if ( image.bookmarks )
\r
365 this.editor.getSelection().selectBookmarks( image.bookmarks );
\r
366 else if ( CKEDITOR.env.ie )
\r
368 // IE BUG: If I don't set the selection to *somewhere* after setting
\r
369 // document contents, then IE would create an empty paragraph at the bottom
\r
370 // the next time the document is modified.
\r
371 var $range = this.editor.document.getBody().$.createTextRange();
\r
372 $range.collapse( true );
\r
376 this.index = image.index;
\r
378 this.currentImage = image;
\r
383 // Get the closest available image.
\r
384 getNextImage : function( isUndo )
\r
386 var snapshots = this.snapshots,
\r
387 currentImage = this.currentImage,
\r
390 if ( currentImage )
\r
394 for ( i = this.index - 1 ; i >= 0 ; i-- )
\r
396 image = snapshots[ i ];
\r
397 if ( !currentImage.equals( image, true ) )
\r
406 for ( i = this.index + 1 ; i < snapshots.length ; i++ )
\r
408 image = snapshots[ i ];
\r
409 if ( !currentImage.equals( image, true ) )
\r
422 * Check the current redo state.
\r
423 * @return {Boolean} Whether the document has previous state to
\r
426 redoable : function()
\r
428 return this.enabled && this.hasRedo;
\r
432 * Check the current undo state.
\r
433 * @return {Boolean} Whether the document has future state to restore.
\r
435 undoable : function()
\r
437 return this.enabled && this.hasUndo;
\r
441 * Perform undo on current index.
\r
445 if ( this.undoable() )
\r
449 var image = this.getNextImage( true );
\r
451 return this.restoreImage( image ), true;
\r
458 * Perform redo on current index.
\r
462 if ( this.redoable() )
\r
464 // Try to save. If no changes have been made, the redo stack
\r
465 // will not change, so it will still be redoable.
\r
468 // If instead we had changes, we can't redo anymore.
\r
469 if ( this.redoable() )
\r
471 var image = this.getNextImage( false );
\r
473 return this.restoreImage( image ), true;
\r
483 * The number of undo steps to be saved. The higher this setting value the more
\r
484 * memory is used for it.
\r
488 * config.undoStackSize = 50;
\r
490 CKEDITOR.config.undoStackSize = 20;
\r