Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/243166

Change subject: Pass staging state through Document#commit to ve.dm.Node update 
event
......................................................................

Pass staging state through Document#commit to ve.dm.Node update event

This allows GeneratedContentNodes to know if the update was a
staged update, and choose whether to render the error in the view.

Staged updates only cause the node to change the colour of the focus
highlight, whereas full updates will show the full error in the view.

This makes generatedContentsFirstRender redundant as we call
update with staged=false on initialize.

Bug: T114480
Change-Id: I8ebb26d4bcd69e611c5bc2527efb335098938172
---
M src/ce/nodes/ve.ce.GeneratedContentNode.js
M src/dm/ve.dm.Document.js
M src/dm/ve.dm.DocumentSynchronizer.js
M src/dm/ve.dm.Node.js
M src/dm/ve.dm.Surface.js
M src/dm/ve.dm.TransactionProcessor.js
6 files changed, 30 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/66/243166/1

diff --git a/src/ce/nodes/ve.ce.GeneratedContentNode.js 
b/src/ce/nodes/ve.ce.GeneratedContentNode.js
index 3bb5cb3..42eedc2 100644
--- a/src/ce/nodes/ve.ce.GeneratedContentNode.js
+++ b/src/ce/nodes/ve.ce.GeneratedContentNode.js
@@ -16,7 +16,6 @@
        // Properties
        this.generatingPromise = null;
        this.generatedContentsValid = false;
-       this.generatedContentsFirstRender = true;
 
        // Events
        this.model.connect( this, { update: 'onGeneratedContentNodeUpdate' } );
@@ -79,9 +78,11 @@
 
 /**
  * Handler for the update event
+ *
+ * @param {boolean} staged Update happened in staging mode
  */
-ve.ce.GeneratedContentNode.prototype.onGeneratedContentNodeUpdate = function 
() {
-       this.update();
+ve.ce.GeneratedContentNode.prototype.onGeneratedContentNodeUpdate = function ( 
staged ) {
+       this.update( undefined, staged );
 };
 
 /**
@@ -136,19 +137,17 @@
  * Rerender the contents of this node.
  *
  * @param {Object|string|Array} generatedContents Generated contents, in the 
default case an HTMLElement array
+ * @param {boolean} [staged] Update happened in staging mode
  * @fires setup
  * @fires teardown
  */
-ve.ce.GeneratedContentNode.prototype.render = function ( generatedContents ) {
+ve.ce.GeneratedContentNode.prototype.render = function ( generatedContents, 
staged ) {
        var $newElements;
        if ( this.live ) {
                this.emit( 'teardown' );
        }
        $newElements = $( this.getRenderedDomElements( ve.copyDomElements( 
generatedContents ) ) );
-       // Render if this is the first time rendering, regardless of whether 
there is an error; otherwise only
-       // render if there is no error
-       if ( this.generatedContentsFirstRender || 
this.validateGeneratedContents( $( generatedContents ) ) ) {
-               this.generatedContentsFirstRender = false;
+       if ( !staged || this.validateGeneratedContents( $( generatedContents ) 
) ) {
                this.generatedContentsValid = true;
                if ( !this.$element[ 0 ].parentNode ) {
                        // this.$element hasn't been attached yet, so just 
overwrite it
@@ -206,14 +205,15 @@
  * model and config data has been rendered before, the cached rendering in the 
store will be used.
  *
  * @param {Object} [config] Optional additional data to pass to 
generateContents()
+ * @param {boolean} [staged] Update happened in staging mode
  */
-ve.ce.GeneratedContentNode.prototype.update = function ( config ) {
+ve.ce.GeneratedContentNode.prototype.update = function ( config, staged ) {
        var store = this.model.doc.getStore(),
                index = store.indexOfHash( OO.getHash( [ this.model, config ] ) 
);
        if ( index !== null ) {
-               this.render( store.value( index ) );
+               this.render( store.value( index ), staged );
        } else {
-               this.forceUpdate( config );
+               this.forceUpdate( config, staged );
        }
 };
 
@@ -221,8 +221,9 @@
  * Force the contents to be updated. Like update(), but bypasses the store.
  *
  * @param {Object} [config] Optional additional data to pass to 
generateContents()
+ * @param {boolean} [staged] Update happened in staging mode
  */
-ve.ce.GeneratedContentNode.prototype.forceUpdate = function ( config ) {
+ve.ce.GeneratedContentNode.prototype.forceUpdate = function ( config, staged ) 
{
        var promise, node = this;
 
        if ( this.generatingPromise ) {
@@ -239,7 +240,7 @@
                // If this promise is no longer the currently pending one, 
ignore it completely
                .done( function ( generatedContents ) {
                        if ( node.generatingPromise === promise ) {
-                               node.doneGenerating( generatedContents, config 
);
+                               node.doneGenerating( generatedContents, config, 
staged );
                        }
                } )
                .fail( function () {
@@ -287,8 +288,9 @@
  * @method
  * @param {Object|string|Array} generatedContents Generated contents
  * @param {Object} [config] Config object passed to forceUpdate()
+ * @param {boolean} [staged] Update happened in staging mode
  */
-ve.ce.GeneratedContentNode.prototype.doneGenerating = function ( 
generatedContents, config ) {
+ve.ce.GeneratedContentNode.prototype.doneGenerating = function ( 
generatedContents, config, staged ) {
        var store, hash;
 
        // Because doneGenerating is invoked asynchronously, the model node may 
have become detached
@@ -301,7 +303,7 @@
 
        this.$element.removeClass( 've-ce-generatedContentNode-generating' );
        this.generatingPromise = null;
-       this.render( generatedContents );
+       this.render( generatedContents, staged );
 };
 
 /**
diff --git a/src/dm/ve.dm.Document.js b/src/dm/ve.dm.Document.js
index d1a8c46..eb96129 100644
--- a/src/dm/ve.dm.Document.js
+++ b/src/dm/ve.dm.Document.js
@@ -324,16 +324,17 @@
  *
  * @method
  * @param {ve.dm.Transaction} transaction Transaction to apply
+ * @param {boolean} isStaging Transaction is being applied in staging mode
  * @fires transact
  * @throws {Error} Cannot commit a transaction that has already been committed
  */
-ve.dm.Document.prototype.commit = function ( transaction ) {
+ve.dm.Document.prototype.commit = function ( transaction, isStaging ) {
        var doc = this;
        if ( transaction.hasBeenApplied() ) {
                throw new Error( 'Cannot commit a transaction that has already 
been committed' );
        }
        this.emit( 'precommit' );
-       new ve.dm.TransactionProcessor( this, transaction ).process( function 
() {
+       new ve.dm.TransactionProcessor( this, transaction, isStaging ).process( 
function () {
                doc.emit( 'presynchronize', transaction );
        } );
        this.completeHistory.push( transaction );
diff --git a/src/dm/ve.dm.DocumentSynchronizer.js 
b/src/dm/ve.dm.DocumentSynchronizer.js
index 5309a46..b8df616 100644
--- a/src/dm/ve.dm.DocumentSynchronizer.js
+++ b/src/dm/ve.dm.DocumentSynchronizer.js
@@ -21,14 +21,16 @@
  * @constructor
  * @param {ve.dm.Document} doc Document to synchronize
  * @param {ve.dm.Transaction} transaction The transaction being synchronized 
for
+ * @param {boolean} isStaging Transaction is being applied in staging mode
  */
-ve.dm.DocumentSynchronizer = function VeDmDocumentSynchronizer( doc, 
transaction ) {
+ve.dm.DocumentSynchronizer = function VeDmDocumentSynchronizer( doc, 
transaction, isStaging ) {
        // Properties
        this.document = doc;
        this.actionQueue = [];
        this.eventQueue = [];
        this.adjustment = 0;
        this.transaction = transaction;
+       this.isStaging = isStaging;
 };
 
 /* Static Properties */
@@ -64,7 +66,7 @@
                // No tree synchronization needed
                // Queue events
                this.queueEvent( selection[ i ].node, 'annotation' );
-               this.queueEvent( selection[ i ].node, 'update' );
+               this.queueEvent( selection[ i ].node, 'update', this.isStaging 
);
        }
 };
 
@@ -81,7 +83,7 @@
        // No tree synchronization needed
        // Queue events
        this.queueEvent( action.node, 'attributeChange', action.key, 
action.from, action.to );
-       this.queueEvent( action.node, 'update' );
+       this.queueEvent( action.node, 'update', this.isStaging );
 };
 
 /**
diff --git a/src/dm/ve.dm.Node.js b/src/dm/ve.dm.Node.js
index ab93b79..9d69801 100644
--- a/src/dm/ve.dm.Node.js
+++ b/src/dm/ve.dm.Node.js
@@ -35,6 +35,7 @@
 
 /**
  * @event update
+ * @param {boolean} staged Transaction was applied in staging mode
  */
 
 /* Inheritance */
diff --git a/src/dm/ve.dm.Surface.js b/src/dm/ve.dm.Surface.js
index c8cb5bb..829d295 100644
--- a/src/dm/ve.dm.Surface.js
+++ b/src/dm/ve.dm.Surface.js
@@ -801,7 +801,7 @@
                                        }
                                }
                                // The .commit() call below indirectly invokes 
setSelection()
-                               this.getDocument().commit( transactions[ i ] );
+                               this.getDocument().commit( transactions[ i ], 
this.isStaging() );
                                if ( transactions[ i 
].hasElementAttributeOperations() ) {
                                        contextChange = true;
                                }
diff --git a/src/dm/ve.dm.TransactionProcessor.js 
b/src/dm/ve.dm.TransactionProcessor.js
index d72174e..b0a09f7 100644
--- a/src/dm/ve.dm.TransactionProcessor.js
+++ b/src/dm/ve.dm.TransactionProcessor.js
@@ -15,16 +15,17 @@
  * @class
  * @param {ve.dm.Document} doc Document
  * @param {ve.dm.Transaction} transaction Transaction
+ * @param {boolean} isStaging Transaction is being applied in staging mode
  * @constructor
  */
-ve.dm.TransactionProcessor = function VeDmTransactionProcessor( doc, 
transaction ) {
+ve.dm.TransactionProcessor = function VeDmTransactionProcessor( doc, 
transaction, isStaging ) {
        // Properties
        this.document = doc;
        this.transaction = transaction;
        this.operations = transaction.getOperations();
        this.modificationQueue = [];
        this.rollbackQueue = [];
-       this.synchronizer = new ve.dm.DocumentSynchronizer( doc, transaction );
+       this.synchronizer = new ve.dm.DocumentSynchronizer( doc, transaction, 
isStaging );
        // Linear model offset that we're currently at. Operations in the 
transaction are ordered, so
        // the cursor only ever moves forward.
        this.cursor = 0;

-- 
To view, visit https://gerrit.wikimedia.org/r/243166
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8ebb26d4bcd69e611c5bc2527efb335098938172
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to