|
77 | 77 |
|
78 | 78 | // Registering keydown on every document recreation.(#3844)
|
79 | 79 | editor.on( 'contentDom', function() {
|
80 |
| - editor.editable().on( 'keydown', function( event ) { |
81 |
| - var keystroke = event.data.getKey(); |
82 | 80 |
|
83 |
| - if ( keystroke == 8 /*Backspace*/ || keystroke == 46 /*Delete*/ ) |
84 |
| - undoManager.type( keystroke, 0 ); |
85 |
| - } ); |
| 81 | + if ( CKEDITOR.env.ie ) { |
| 82 | + // Old solution for IE, because IE11 and earlier does not fire input |
| 83 | + // event for `contenteditable=true` elements. |
| 84 | + editor.editable().on( 'keypress', function( event ) { |
| 85 | + undoManager.type( event.data.getKey(), 1 ); |
| 86 | + } ); |
86 | 87 |
|
87 |
| - editor.editable().on( 'keypress', function( event ) { |
88 |
| - undoManager.type( event.data.getKey(), 1 ); |
89 |
| - } ); |
| 88 | + editor.editable().on( 'keydown', function( event ) { |
| 89 | + var keystroke = event.data.getKey(); |
| 90 | + |
| 91 | + if ( keystroke == 8 /*Backspace*/ || keystroke == 46 /*Delete*/ ) |
| 92 | + undoManager.type( keystroke, 0 ); |
| 93 | + } ); |
| 94 | + } else { |
| 95 | + |
| 96 | + editor.on( 'instanceReady', function() { |
| 97 | + console.log( 'isntR' ); |
| 98 | + // Save initial image. |
| 99 | + // haxes, haxes needed for initial store. |
| 100 | + var img = new Image( editor ); |
| 101 | + undoManager.snapshots.push( img ); |
| 102 | + undoManager.currentSnapshot = img; |
| 103 | + undoManager.index = 0; |
| 104 | + undoManager.onChange(); |
| 105 | + // Index: |
| 106 | + // 0 - stays for characters input |
| 107 | + // 1 - functional keys (delete/backspace) |
| 108 | + undoManager.strokesRecorded = [ 0, 0 ]; |
| 109 | + } ); |
| 110 | + |
| 111 | + var tmpInputFired = false, |
| 112 | + ignoreInputEvent = false, |
| 113 | + ignoreInputEventListener = function() { |
| 114 | + console.log( 'input event canceled' ); |
| 115 | + ignoreInputEvent = true; |
| 116 | + }; |
| 117 | + |
| 118 | + editor.editable().on( 'input', function() { |
| 119 | + tmpInputFired = true; |
| 120 | + |
| 121 | + if ( ignoreInputEvent ) { |
| 122 | + tmpInputFired = false; |
| 123 | + ignoreInputEvent = false; // Reset flag. |
| 124 | + } |
| 125 | + } ); |
| 126 | + |
| 127 | + editor.editable().on( 'keyup', function( evt ) { |
| 128 | + if ( tmpInputFired ) { |
| 129 | + console.log( 'input flag detected, processing'); |
| 130 | + |
| 131 | + undoManager.newType( evt.data.getKey() ); |
| 132 | + // Reset temporary flag. |
| 133 | + tmpInputFired = false; |
| 134 | + } |
| 135 | + } ); |
| 136 | + |
| 137 | + // On paste and drop we need to cancel tmpInputFired variable. |
| 138 | + // It would result with calling undoManager.newType() on any following key. |
| 139 | + editor.editable().on( 'paste', ignoreInputEventListener ); |
| 140 | + editor.editable().on( 'drop', ignoreInputEventListener ); |
| 141 | + } |
90 | 142 | } );
|
91 | 143 |
|
92 | 144 | // Always save an undo snapshot - the previous mode might have
|
|
379 | 431 |
|
380 | 432 | },
|
381 | 433 |
|
| 434 | + newType: function( keyCode ) { |
| 435 | + // Backspace and delete. |
| 436 | + var functionalKey = Number( keyCode == 8 || keyCode == 46 ), |
| 437 | + // Note that his count does not consider current count, so you might want |
| 438 | + // to increase it by 1. |
| 439 | + strokesRecorded = this.strokesRecorded[ functionalKey ] + 1; |
| 440 | + |
| 441 | + if ( functionalKey !== this.wasFunctionalKey ) { |
| 442 | + console.log( 'Key group changed' ); |
| 443 | + // Reset the other key group recorded count. |
| 444 | + this.strokesRecorded[ functionalKey ? 0 : 1 ] = 0; |
| 445 | + } else if ( strokesRecorded >= 5 ) { |
| 446 | + console.log( 'We have 5 or more keys recorded.' ); |
| 447 | + //this.save( false, null, true ); |
| 448 | + // Rather than using save directly we should use saveSnapshot event |
| 449 | + this.editor.fire( 'saveSnapshot' ); |
| 450 | + // Reset the count of strokes, so it will be later assing to this.strokesRecorded. |
| 451 | + strokesRecorded = 0; |
| 452 | + } |
| 453 | + |
| 454 | + console.log( 'Already recorded: ' + strokesRecorded ); |
| 455 | + // Increase recorded strokes count. |
| 456 | + this.strokesRecorded[ functionalKey ] = strokesRecorded; |
| 457 | + //this.strokesRecorded[ functionalKey ]++; |
| 458 | + // This prop will tell in next itaration what kind of group was processed previously. |
| 459 | + this.wasFunctionalKey = functionalKey; |
| 460 | + |
| 461 | + // Eventually we need to fire change event manually. |
| 462 | + // Event was already fired if strokesRecorded == 0 ( snapshot has been created ), |
| 463 | + // so we need to take care of any other case. |
| 464 | + // @todo: mhmm seems that `this.save( false, null, true );` does not fire change |
| 465 | + // event here. |
| 466 | + //if ( strokesRecorded != 0 ) |
| 467 | + this.editor.fire( 'change' ); |
| 468 | + }, |
| 469 | + |
382 | 470 | /**
|
383 | 471 | * Resets the undo stack.
|
384 | 472 | */
|
|
414 | 502 | delete this.lastKeystroke;
|
415 | 503 | this.typesCount = 0;
|
416 | 504 | this.modifiersCount = 0;
|
| 505 | + |
| 506 | + // Reseting newType() variables. |
| 507 | + this.strokesRecorded = [ 0, 0 ]; |
417 | 508 | },
|
418 | 509 |
|
419 | 510 | fireChange: function() {
|
|
0 commit comments