[MediaWiki-commits] [Gerrit] Detect outdated pending post KeyPress handler - change (mediawiki...VisualEditor)
jenkins-bot has submitted this change and it was merged. Change subject: Detect outdated pending post KeyPress handler .. Detect outdated pending post KeyPress handler modules/ve/ce/ve.ce.Surface.js * Schedule the post-keypress async handler in a way that can be cancelled. * Cancel it and run the handler immediately if another key event happens. Bug: 53079 Change-Id: If139ff3230c10caa616743f71659c4606d290310 --- M modules/ve/ce/ve.ce.Surface.js 1 file changed, 36 insertions(+), 2 deletions(-) Approvals: Catrope: Looks good to me, approved jenkins-bot: Verified diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js index cbc5ba1..f4b52e6 100644 --- a/modules/ve/ce/ve.ce.Surface.js +++ b/modules/ve/ce/ve.ce.Surface.js @@ -33,6 +33,7 @@ this.documentView = new ve.ce.Document( model.getDocument(), this ); this.surfaceObserver = new ve.ce.SurfaceObserver( this.documentView ); this.selectionTimeout = null; + this.keyPressTimeout = null; this.$document = $( this.getElementDocument() ); this.clipboard = {}; this.renderingEnabled = true; @@ -450,6 +451,7 @@ */ ve.ce.Surface.prototype.onDocumentKeyDown = function ( e ) { var trigger; + this.forceKeyPressTimeout(); // Ignore keydowns while in IME mode but do not preventDefault them (so text actually appear on // the screen). @@ -514,6 +516,8 @@ ve.ce.Surface.prototype.onDocumentKeyPress = function ( e ) { var selection, prevNode, documentModel = this.model.getDocument(); + this.forceKeyPressTimeout(); + // Prevent IE from editing Aliens/Entities // TODO: Better comment about what's going on here is needed. if ( $.browser.msie === true ) { @@ -539,12 +543,42 @@ } this.handleInsertion(); - setTimeout( ve.bind( function () { - this.surfaceObserver.start( false, true ); + this.setKeyPressTimeout(); +}; + +/** + * Append a call to onKeyPressTimeout to the event queue. + * @method + */ +ve.ce.Surface.prototype.setKeyPressTimeout = function () { + this.keyPressTimeout = setTimeout( ve.bind( function() { + this.keyPressTimeout = null; + this.onKeyPressTimeout(); }, this ) ); }; /** + * If there is a pending call to onKeyPressTimeout in the event queue, delete it and call now + * @method + */ +ve.ce.Surface.prototype.forceKeyPressTimeout = function () { + if ( this.keyPressTimeout === null ) { + return; + } + clearTimeout( this.keyPressTimeout ); + this.keyPressTimeout = null; + this.onKeyPressTimeout(); +}; + +/** + * post-keypress handler: re-sync the surface and model + * @method + */ +ve.ce.Surface.prototype.onKeyPressTimeout = function () { + this.surfaceObserver.start( false, true ); +}; + +/** * Handle document key up events. * * @method -- To view, visit https://gerrit.wikimedia.org/r/81213 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: If139ff3230c10caa616743f71659c4606d290310 Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Divec Gerrit-Reviewer: Catrope Gerrit-Reviewer: Inez Gerrit-Reviewer: jenkins-bot ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
[MediaWiki-commits] [Gerrit] Detect outdated pending post KeyPress handler - change (mediawiki...VisualEditor)
Divec has uploaded a new change for review. https://gerrit.wikimedia.org/r/81213 Change subject: Detect outdated pending post KeyPress handler .. Detect outdated pending post KeyPress handler modules/ve/ce/ve.ce.Surface.js * KeyPress handler: save id for the post-event setTimeout * KeyDown handler: cancel the poller and poll immediately if pending. Bug: 53079 Change-Id: If139ff3230c10caa616743f71659c4606d290310 --- M modules/ve/ce/ve.ce.Surface.js 1 file changed, 15 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor refs/changes/13/81213/1 diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js index cbc5ba1..ffa25f6 100644 --- a/modules/ve/ce/ve.ce.Surface.js +++ b/modules/ve/ce/ve.ce.Surface.js @@ -33,6 +33,7 @@ this.documentView = new ve.ce.Document( model.getDocument(), this ); this.surfaceObserver = new ve.ce.SurfaceObserver( this.documentView ); this.selectionTimeout = null; + this.keyPressTimeout = null; this.$document = $( this.getElementDocument() ); this.clipboard = {}; this.renderingEnabled = true; @@ -450,6 +451,13 @@ */ ve.ce.Surface.prototype.onDocumentKeyDown = function ( e ) { var trigger; + if ( this.keyPressTimeout !== null ) { + // Cancel the pending poll, and do a synchronous one instead + clearTimeout( this.keyPressTimeout ); + this.keyPressTimeout = null; + ve.log( 'keydown clash ' + document.getElementsByClassName( 've-ce-documentNode' )[0].innerHTML.substring(0, 100) ); + this.surfaceObserver.start( false, true ); + } // Ignore keydowns while in IME mode but do not preventDefault them (so text actually appear on // the screen). @@ -514,6 +522,11 @@ ve.ce.Surface.prototype.onDocumentKeyPress = function ( e ) { var selection, prevNode, documentModel = this.model.getDocument(); + // I *think* there's no need to check for a pending keyPressTimeout here, because it + // will have already been cancelled by the onDocumentKeyDown. This assumes that there's + // no way we can ever get two consecutive KeyPress events without an intervening + // KeyDown event. Perhaps some IME/browser combination will violate this :-/ + // Prevent IE from editing Aliens/Entities // TODO: Better comment about what's going on here is needed. if ( $.browser.msie === true ) { @@ -539,7 +552,8 @@ } this.handleInsertion(); - setTimeout( ve.bind( function () { + this.keyPressTimeout = setTimeout( ve.bind( function () { + this.keyPressTimeout = null; this.surfaceObserver.start( false, true ); }, this ) ); }; -- To view, visit https://gerrit.wikimedia.org/r/81213 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If139ff3230c10caa616743f71659c4606d290310 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Divec ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits