Ticket #3372: 3372_6_ref.patch

File 3372_6_ref.patch, 4.5 KB (added by Garry Yao, 15 years ago)
  • 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                                // 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                            // Keystrokes which just introduce new contents.
     216                                isContent = ( !isModifier && !isReset ),
     217                            // Create undo snap for every different modifier key.
     218                                modifierSnapshot = ( isModifier && !lastWasSameModifier ),
     219                                // Create undo snap on the following cases:
     220                                // 1. Just start to type.
     221                                // 2. Typing some content after a modifier.
     222                                // 3. Typing some content after make a visible selection.
     223                                startedTyping = !this.typing
     224                                        || ( isContent && ( wasModifier || wasReset ) )
     225                                        || ( isContent && isSelected );
     226
     227                        if ( startedTyping || modifierSnapshot )
    192228                        {
    193229                                var beforeTypeImage = new Image( this.editor );
    194230
     
    204240
    205241                                                if ( beforeTypeImage.contents != currentSnapshot )
    206242                                                {
    207                                                         if ( !this.save( false, beforeTypeImage ) )
    208                                                         {
     243                                                        // This's a special save, with specified snapshot
     244                                                        // and without auto 'fireChange'.
     245                                                        if ( !this.save( false, beforeTypeImage, false ) )
    209246                                                                // Drop future snapshots.
    210247                                                                this.snapshots.splice( this.index + 1, this.snapshots.length - this.index - 1 );
    211                                                         }
    212248
    213249                                                        this.hasUndo = true;
    214250                                                        this.hasRedo = false;
    215251
    216252                                                        this.typesCount = 1;
    217                                                         this.typing = true;
     253                                                        this.modifiersCount = 1;
    218254
    219255                                                        this.onChange();
    220256                                                }
    221257                                        },
    222                                         0, this );
    223 
    224                                 return;
     258                                        0, this
     259                                );
    225260                        }
    226261
    227                         this.typesCount++;
     262                        this.lastKeystroke = keystroke;
     263                        // Create undo snap after typed too much (over 25 times).
     264                        if ( isModifier )
     265                        {
     266                                this.typesCount = 0;
     267                                this.modifiersCount++;
    228268
    229                         if ( this.typesCount > 25 )
     269                                if ( this.modifiersCount > 25 )
     270                                {
     271                                        this.save();
     272                                        this.modifiersCount = 1;
     273                                }
     274                        }
     275                        else if ( !isReset )
    230276                        {
    231                                 this.save();
    232                                 this.typesCount = 1;
     277                                this.modifiersCount = 0;
     278                                this.typesCount++;
     279
     280                                if ( this.typesCount > 25 )
     281                                {
     282                                        this.save();
     283                                        this.typesCount = 1;
     284                                }
    233285                        }
    234286
    235287                        this.typing = true;
    236288                },
    237289
     290                /**
     291                 * Reset all states about typing.
     292                 * @see  UndoManager.type
     293                 */
     294                resetType : function()
     295                {
     296                        this.typing = false;
     297                        delete this.lastKeystroke;
     298                        this.typesCount = 0;
     299                        this.modifiersCount = 0;
     300                },
    238301                fireChange : function()
    239302                {
    240303                        this.hasUndo = !!this.getNextImage( true );
    241304                        this.hasRedo = !!this.getNextImage( false );
    242 
    243                         this.typing = false;
    244                         this.typesCount = 0;
    245 
     305                        // Reset typing
     306                        this.resetType();
    246307                        this.onChange();
    247308                },
    248309
    249310                /**
    250311                 * Save a snapshot of document image for later retrieve.
    251312                 */
    252                 save : function( onContentOnly, image )
     313                save : function( onContentOnly, image, autoFireChange )
    253314                {
    254315                        var snapshots = this.snapshots;
    255316
     
    273334
    274335                        this.currentImage = image;
    275336
    276                         this.fireChange();
    277 
     337                        if ( autoFireChange !== false )
     338                                this.fireChange();
    278339                        return true;
    279340                },
    280341
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy