2 Copyright (c) 2003-2011, 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
119 * Update the undo stacks with any subsequent DOM changes after this call.
\r
120 * @name CKEDITOR.editor#updateUndo
\r
124 * editor.fire( 'updateSnapshot' );
\r
126 * // Ask to include subsequent (in this call stack) DOM changes to be
\r
127 * // considered as part of the first snapshot.
\r
128 * editor.fire( 'updateSnapshot' );
\r
129 * editor.document.body.append(...);
\r
133 editor.on( 'updateSnapshot', function()
\r
135 if ( undoManager.currentImage && new Image( editor ).equals( undoManager.currentImage ) )
\r
136 setTimeout( function() { undoManager.update(); }, 0 );
\r
141 CKEDITOR.plugins.undo = {};
\r
144 * Undo snapshot which represents the current document status.
\r
145 * @name CKEDITOR.plugins.undo.Image
\r
146 * @param editor The editor instance on which the image is created.
\r
148 var Image = CKEDITOR.plugins.undo.Image = function( editor )
\r
150 this.editor = editor;
\r
151 var contents = editor.getSnapshot(),
\r
152 selection = contents && editor.getSelection();
\r
154 // In IE, we need to remove the expando attributes.
\r
155 CKEDITOR.env.ie && contents && ( contents = contents.replace( /\s+data-cke-expando=".*?"/g, '' ) );
\r
157 this.contents = contents;
\r
158 this.bookmarks = selection && selection.createBookmarks2( true );
\r
161 // Attributes that browser may changing them when setting via innerHTML.
\r
162 var protectedAttrs = /\b(?:href|src|name)="[^"]*?"/gi;
\r
166 equals : function( otherImage, contentOnly )
\r
169 var thisContents = this.contents,
\r
170 otherContents = otherImage.contents;
\r
172 // For IE6/7 : Comparing only the protected attribute values but not the original ones.(#4522)
\r
173 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) )
\r
175 thisContents = thisContents.replace( protectedAttrs, '' );
\r
176 otherContents = otherContents.replace( protectedAttrs, '' );
\r
179 if ( thisContents != otherContents )
\r
185 var bookmarksA = this.bookmarks,
\r
186 bookmarksB = otherImage.bookmarks;
\r
188 if ( bookmarksA || bookmarksB )
\r
190 if ( !bookmarksA || !bookmarksB || bookmarksA.length != bookmarksB.length )
\r
193 for ( var i = 0 ; i < bookmarksA.length ; i++ )
\r
195 var bookmarkA = bookmarksA[ i ],
\r
196 bookmarkB = bookmarksB[ i ];
\r
199 bookmarkA.startOffset != bookmarkB.startOffset ||
\r
200 bookmarkA.endOffset != bookmarkB.endOffset ||
\r
201 !CKEDITOR.tools.arrayCompare( bookmarkA.start, bookmarkB.start ) ||
\r
202 !CKEDITOR.tools.arrayCompare( bookmarkA.end, bookmarkB.end ) )
\r
214 * @constructor Main logic for Redo/Undo feature.
\r
216 function UndoManager( editor )
\r
218 this.editor = editor;
\r
220 // Reset the undo stack.
\r
225 var editingKeyCodes = { /*Backspace*/ 8:1, /*Delete*/ 46:1 },
\r
226 modifierKeyCodes = { /*Shift*/ 16:1, /*Ctrl*/ 17:1, /*Alt*/ 18:1 },
\r
227 navigationKeyCodes = { 37:1, 38:1, 39:1, 40:1 }; // Arrows: L, T, R, B
\r
229 UndoManager.prototype =
\r
232 * Process undo system regard keystrikes.
\r
233 * @param {CKEDITOR.dom.event} event
\r
235 type : function( event )
\r
237 var keystroke = event && event.data.getKey(),
\r
238 isModifierKey = keystroke in modifierKeyCodes,
\r
239 isEditingKey = keystroke in editingKeyCodes,
\r
240 wasEditingKey = this.lastKeystroke in editingKeyCodes,
\r
241 sameAsLastEditingKey = isEditingKey && keystroke == this.lastKeystroke,
\r
242 // Keystrokes which navigation through contents.
\r
243 isReset = keystroke in navigationKeyCodes,
\r
244 wasReset = this.lastKeystroke in navigationKeyCodes,
\r
246 // Keystrokes which just introduce new contents.
\r
247 isContent = ( !isEditingKey && !isReset ),
\r
249 // Create undo snap for every different modifier key.
\r
250 modifierSnapshot = ( isEditingKey && !sameAsLastEditingKey ),
\r
251 // Create undo snap on the following cases:
\r
252 // 1. Just start to type .
\r
253 // 2. Typing some content after a modifier.
\r
254 // 3. Typing some content after make a visible selection.
\r
255 startedTyping = !( isModifierKey || this.typing )
\r
256 || ( isContent && ( wasEditingKey || wasReset ) );
\r
258 if ( startedTyping || modifierSnapshot )
\r
260 var beforeTypeImage = new Image( this.editor );
\r
262 // Use setTimeout, so we give the necessary time to the
\r
263 // browser to insert the character into the DOM.
\r
264 CKEDITOR.tools.setTimeout( function()
\r
266 var currentSnapshot = this.editor.getSnapshot();
\r
268 // In IE, we need to remove the expando attributes.
\r
269 if ( CKEDITOR.env.ie )
\r
270 currentSnapshot = currentSnapshot.replace( /\s+data-cke-expando=".*?"/g, '' );
\r
272 if ( beforeTypeImage.contents != currentSnapshot )
\r
274 // It's safe to now indicate typing state.
\r
275 this.typing = true;
\r
277 // This's a special save, with specified snapshot
\r
278 // and without auto 'fireChange'.
\r
279 if ( !this.save( false, beforeTypeImage, false ) )
\r
280 // Drop future snapshots.
\r
281 this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 );
\r
283 this.hasUndo = true;
\r
284 this.hasRedo = false;
\r
286 this.typesCount = 1;
\r
287 this.modifiersCount = 1;
\r
296 this.lastKeystroke = keystroke;
\r
298 // Create undo snap after typed too much (over 25 times).
\r
299 if ( isEditingKey )
\r
301 this.typesCount = 0;
\r
302 this.modifiersCount++;
\r
304 if ( this.modifiersCount > 25 )
\r
306 this.save( false, null, false );
\r
307 this.modifiersCount = 1;
\r
310 else if ( !isReset )
\r
312 this.modifiersCount = 0;
\r
315 if ( this.typesCount > 25 )
\r
317 this.save( false, null, false );
\r
318 this.typesCount = 1;
\r
324 reset : function() // Reset the undo stack.
\r
327 * Remember last pressed key.
\r
329 this.lastKeystroke = 0;
\r
332 * Stack for all the undo and redo snapshots, they're always created/removed
\r
335 this.snapshots = [];
\r
338 * Current snapshot history index.
\r
342 this.limit = this.editor.config.undoStackSize || 20;
\r
344 this.currentImage = null;
\r
346 this.hasUndo = false;
\r
347 this.hasRedo = false;
\r
353 * Reset all states about typing.
\r
354 * @see UndoManager.type
\r
356 resetType : function()
\r
358 this.typing = false;
\r
359 delete this.lastKeystroke;
\r
360 this.typesCount = 0;
\r
361 this.modifiersCount = 0;
\r
363 fireChange : function()
\r
365 this.hasUndo = !!this.getNextImage( true );
\r
366 this.hasRedo = !!this.getNextImage( false );
\r
373 * Save a snapshot of document image for later retrieve.
\r
375 save : function( onContentOnly, image, autoFireChange )
\r
377 var snapshots = this.snapshots;
\r
379 // Get a content image.
\r
381 image = new Image( this.editor );
\r
383 // Do nothing if it was not possible to retrieve an image.
\r
384 if ( image.contents === false )
\r
387 // Check if this is a duplicate. In such case, do nothing.
\r
388 if ( this.currentImage && image.equals( this.currentImage, onContentOnly ) )
\r
391 // Drop future snapshots.
\r
392 snapshots.splice( this.index + 1, snapshots.length - this.index - 1 );
\r
394 // If we have reached the limit, remove the oldest one.
\r
395 if ( snapshots.length == this.limit )
\r
398 // Add the new image, updating the current index.
\r
399 this.index = snapshots.push( image ) - 1;
\r
401 this.currentImage = image;
\r
403 if ( autoFireChange !== false )
\r
408 restoreImage : function( image )
\r
410 this.editor.loadSnapshot( image.contents );
\r
412 if ( image.bookmarks )
\r
413 this.editor.getSelection().selectBookmarks( image.bookmarks );
\r
414 else if ( CKEDITOR.env.ie )
\r
416 // IE BUG: If I don't set the selection to *somewhere* after setting
\r
417 // document contents, then IE would create an empty paragraph at the bottom
\r
418 // the next time the document is modified.
\r
419 var $range = this.editor.document.getBody().$.createTextRange();
\r
420 $range.collapse( true );
\r
424 this.index = image.index;
\r
426 // Update current image with the actual editor
\r
427 // content, since actualy content may differ from
\r
428 // the original snapshot due to dom change. (#4622)
\r
433 // Get the closest available image.
\r
434 getNextImage : function( isUndo )
\r
436 var snapshots = this.snapshots,
\r
437 currentImage = this.currentImage,
\r
440 if ( currentImage )
\r
444 for ( i = this.index - 1 ; i >= 0 ; i-- )
\r
446 image = snapshots[ i ];
\r
447 if ( !currentImage.equals( image, true ) )
\r
456 for ( i = this.index + 1 ; i < snapshots.length ; i++ )
\r
458 image = snapshots[ i ];
\r
459 if ( !currentImage.equals( image, true ) )
\r
472 * Check the current redo state.
\r
473 * @return {Boolean} Whether the document has previous state to
\r
476 redoable : function()
\r
478 return this.enabled && this.hasRedo;
\r
482 * Check the current undo state.
\r
483 * @return {Boolean} Whether the document has future state to restore.
\r
485 undoable : function()
\r
487 return this.enabled && this.hasUndo;
\r
491 * Perform undo on current index.
\r
495 if ( this.undoable() )
\r
499 var image = this.getNextImage( true );
\r
501 return this.restoreImage( image ), true;
\r
508 * Perform redo on current index.
\r
512 if ( this.redoable() )
\r
514 // Try to save. If no changes have been made, the redo stack
\r
515 // will not change, so it will still be redoable.
\r
518 // If instead we had changes, we can't redo anymore.
\r
519 if ( this.redoable() )
\r
521 var image = this.getNextImage( false );
\r
523 return this.restoreImage( image ), true;
\r
531 * Update the last snapshot of the undo stack with the current editor content.
\r
533 update : function()
\r
535 this.snapshots.splice( this.index, 1, ( this.currentImage = new Image( this.editor ) ) );
\r
541 * The number of undo steps to be saved. The higher this setting value the more
\r
542 * memory is used for it.
\r
546 * config.undoStackSize = 50;
\r
550 * Fired when the editor is about to save an undo snapshot. This event can be
\r
551 * fired by plugins and customizations to make the editor saving undo snapshots.
\r
552 * @name CKEDITOR.editor#saveSnapshot
\r