Ticket #3372: 3372_6.patch

File 3372_6.patch, 4.6 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                                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
     211                                // Arrows: L, T, R, B
     212                                resetTypingCodes = { 37:1, 38:1, 39:1, 40:1 },
     213                                // Keystrokes which navigation through contents.
     214                                isReset = keystroke in resetTypingCodes,
     215                                wasReset = this.lastKeystroke in resetTypingCodes,
     216
     217                                // Keystrokes which just introduce new contents.
     218                                isContent = ( !isModifier && !isReset ),
     219
     220                                // Create undo snap for every different modifier key.
     221                                modifierSnapshot = ( isModifier && !lastWasSameModifier ),
     222                                // Create undo snap on the following cases:
     223                                // 1. Just start to type.
     224                                // 2. Typing some content after a modifier.
     225                                // 3. Typing some content after make a visible selection.
     226                                startedTyping = !this.typing
     227                                        || ( isContent && ( wasModifier || wasReset ) )
     228                                        || ( isContent && isSelected );
     229
     230                        if ( startedTyping || modifierSnapshot )
    192231                        {
    193232                                var beforeTypeImage = new Image( this.editor );
    194233
     
    204243
    205244                                                if ( beforeTypeImage.contents != currentSnapshot )
    206245                                                {
    207                                                         if ( !this.save( false, beforeTypeImage ) )
    208                                                         {
     246                                                        // This's a special save, with specified snapshot
     247                                                        // and without auto 'fireChange'.
     248                                                        if ( !this.save( false, beforeTypeImage, false ) )
    209249                                                                // Drop future snapshots.
    210250                                                                this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 );
    211                                                         }
    212251
    213252                                                        this.hasUndo = true;
    214253                                                        this.hasRedo = false;
    215254
    216255                                                        this.typesCount = 1;
    217                                                         this.typing = true;
     256                                                        this.modifiersCount = 1;
    218257
    219258                                                        this.onChange();
    220259                                                }
    221260                                        },
    222                                         0, this );
    223 
    224                                 return;
     261                                        0, this
     262                                );
    225263                        }
    226264
    227                         this.typesCount++;
     265                        this.lastKeystroke = keystroke;
     266                        // Create undo snap after typed too much (over 25 times).
     267                        if ( isModifier )
     268                        {
     269                                this.typesCount = 0;
     270                                this.modifiersCount++;
     271
     272                                if ( this.modifiersCount > 25 )
     273                                {
     274                                        this.save();
     275                                        this.modifiersCount = 1;
     276                                }
     277                        }
     278                        else if ( !isReset )
     279                        {
     280                                this.modifiersCount = 0;
     281                                this.typesCount++;
    228282
    229                         if ( this.typesCount > 25 )
    230                         {
    231                                 this.save();
    232                                 this.typesCount = 1;
    233                         }
     283                                if ( this.typesCount > 25 )
     284                                {
     285                                        this.save();
     286                                        this.typesCount = 1;
     287                                }
     288                        }
    234289
    235290                        this.typing = true;
    236291                },
    237292
     293                /**
     294                 * Reset all states about typing.
     295                 * @see  UndoManager.type
     296                 */
     297                resetType : function()
     298                {
     299                        this.typing = false;
     300                        delete this.lastKeystroke;
     301                        this.typesCount = 0;
     302                        this.modifiersCount = 0;
     303                },
    238304                fireChange : function()
    239305                {
    240306                        this.hasUndo = !!this.getNextImage( true );
    241307                        this.hasRedo = !!this.getNextImage( false );
    242 
    243                         this.typing = false;
    244                         this.typesCount = 0;
    245 
     308                        // Reset typing
     309                        this.resetType();
    246310                        this.onChange();
    247311                },
    248312
    249313                /**
    250314                 * Save a snapshot of document image for later retrieve.
    251315                 */
    252                 save : function( onContentOnly, image )
     316                save : function( onContentOnly, image, autoFireChange )
    253317                {
    254318                        var snapshots = this.snapshots;
    255319
     
    273337
    274338                        this.currentImage = image;
    275339
    276                         this.fireChange();
    277 
     340                        if ( autoFireChange !== false )
     341                                this.fireChange();
    278342                        return true;
    279343                },
    280344
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy