Ticket #3372: 3372_7.patch

File 3372_7.patch, 4.5 KB (added by Tobiasz Cudnik, 15 years ago)
  • _source/plugins/undo/plugin.js

     
    8282                                                                {
    8383                                                                        // Do not capture CTRL hotkeys.
    8484                                                                        if ( !event.data.$.ctrlKey && !event.data.$.metaKey )
    85                                                                                 undoManager.type();
     85                                                                                undoManager.type( event );
    8686                                                                });
    8787
    8888                                                        // Being this the first call, let's get an undo snapshot.
     
    167167        function UndoManager( editor )
    168168        {
    169169                this.typesCount = 0;
     170                this.modifiersCount = 0;
    170171
    171172                this.editor = editor;
    172173
     
    182183                this.index = -1;
    183184
    184185                this.limit = editor.config.undoStackSize;
     186
     187                /**
     188                 * Remember last pressed key.
     189                 */
     190                this.lastKeystroke = 0;
    185191        }
    186192
    187193        UndoManager.prototype =
    188194        {
    189                 type : function()
     195                /**
     196                 * Process undo system regard keystrikes.
     197                 * @param {CKEDITOR.dom.event} event
     198                 */
     199                type : function( event )
    190200                {
    191                         if ( !this.typing )
     201                        var keystroke = event && event.data.getKeystroke(),
     202
     203                                // Backspace, Delete
     204                                modifierCodes = { 8:1, 46:1 },
     205                                // Keystrokes which will modify the contents.
     206                                isModifier = keystroke in modifierCodes,
     207                                wasModifier = this.lastKeystroke in modifierCodes,
     208                                lastWasSameModifier = isModifier && keystroke == this.lastKeystroke,
     209
     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
     216                                // Keystrokes which just introduce new contents.
     217                                isContent = ( !isModifier && !isReset ),
     218
     219                                // Create undo snap for every different modifier key.
     220                                modifierSnapshot = ( isModifier && !lastWasSameModifier ),
     221                                // Create undo snap on the following cases:
     222                                // 1. Just start to type.
     223                                // 2. Typing some content after a modifier.
     224                                // 3. Typing some content after make a visible selection.
     225                                startedTyping = !this.typing
     226                                        || ( isContent && ( wasModifier || wasReset ) );
     227
     228                        if ( startedTyping || modifierSnapshot )
    192229                        {
    193230                                var beforeTypeImage = new Image( this.editor );
    194231
     
    204241
    205242                                                if ( beforeTypeImage.contents != currentSnapshot )
    206243                                                {
    207                                                         if ( !this.save( false, beforeTypeImage ) )
    208                                                         {
     244                                                        // This's a special save, with specified snapshot
     245                                                        // and without auto 'fireChange'.
     246                                                        if ( !this.save( false, beforeTypeImage, false ) )
    209247                                                                // Drop future snapshots.
    210248                                                                this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 );
    211                                                         }
    212249
    213250                                                        this.hasUndo = true;
    214251                                                        this.hasRedo = false;
    215252
    216253                                                        this.typesCount = 1;
    217                                                         this.typing = true;
     254                                                        this.modifiersCount = 1;
    218255
    219256                                                        this.onChange();
    220257                                                }
    221258                                        },
    222                                         0, this );
    223 
    224                                 return;
     259                                        0, this
     260                                );
    225261                        }
    226262
    227                         this.typesCount++;
     263                        this.lastKeystroke = keystroke;
     264                        // Create undo snap after typed too much (over 25 times).
     265                        if ( isModifier )
     266                        {
     267                                this.typesCount = 0;
     268                                this.modifiersCount++;
     269
     270                                if ( this.modifiersCount > 25 )
     271                                {
     272                                        this.save();
     273                                        this.modifiersCount = 1;
     274                                }
     275                        }
     276                        else if ( !isReset )
     277                        {
     278                                this.modifiersCount = 0;
     279                                this.typesCount++;
    228280
    229                         if ( this.typesCount > 25 )
    230                         {
    231                                 this.save();
    232                                 this.typesCount = 1;
    233                         }
     281                                if ( this.typesCount > 25 )
     282                                {
     283                                        this.save();
     284                                        this.typesCount = 1;
     285                                }
     286                        }
    234287
    235288                        this.typing = true;
    236289                },
    237290
     291                /**
     292                 * Reset all states about typing.
     293                 * @see  UndoManager.type
     294                 */
     295                resetType : function()
     296                {
     297                        this.typing = false;
     298                        delete this.lastKeystroke;
     299                        this.typesCount = 0;
     300                        this.modifiersCount = 0;
     301                },
    238302                fireChange : function()
    239303                {
    240304                        this.hasUndo = !!this.getNextImage( true );
    241305                        this.hasRedo = !!this.getNextImage( false );
    242 
    243                         this.typing = false;
    244                         this.typesCount = 0;
    245 
     306                        // Reset typing
     307                        this.resetType();
    246308                        this.onChange();
    247309                },
    248310
    249311                /**
    250312                 * Save a snapshot of document image for later retrieve.
    251313                 */
    252                 save : function( onContentOnly, image )
     314                save : function( onContentOnly, image, autoFireChange )
    253315                {
    254316                        var snapshots = this.snapshots;
    255317
     
    273335
    274336                        this.currentImage = image;
    275337
    276                         this.fireChange();
    277 
     338                        if ( autoFireChange !== false )
     339                                this.fireChange();
    278340                        return true;
    279341                },
    280342
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy