Mooeypoo has uploaded a new change for review.

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

Change subject: Fix MWImageModel's getImageNodeType()
......................................................................

Fix MWImageModel's getImageNodeType()

* Fixing a typo in the getImageNodeType() method ('frame' was supposed
  to be 'frameless').
* Adding a check for type 'mwBlockImage' before editing/adding captions
* Using the node's getType() method instead of the model's where possible.
* Adjusting default alignment, so that when we check if it is set, it gives
  us the actual answer, which is always useful.

Bug: 65568
Change-Id: I1687d8c7430c723b52448a9e24641e2c30d85d6a
---
M modules/ve-mw/dm/models/ve.dm.MWImageModel.js
1 file changed, 39 insertions(+), 37 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/54/134554/1

diff --git a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js 
b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
index 28a8834..d2e102c 100644
--- a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
+++ b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
@@ -127,7 +127,7 @@
        );
 
        // If this is a block image, get the caption
-       if ( imgModel.getImageNodeType() === 'mwBlockImage' ) {
+       if ( node.getType() === 'mwBlockImage' ) {
                captionNode = node.getCaptionNode();
                if ( captionNode && captionNode.getLength() > 0 ) {
                        imgModel.setCaptionDocument( doc.cloneFromRange( 
captionNode.getRange() ) );
@@ -145,7 +145,7 @@
  */
 ve.dm.MWImageModel.prototype.getImageNodeType = function () {
        if (
-               ( this.getType() === 'frame' || this.getType() === 'none' ) &&
+               ( this.getType() === 'frameless' || this.getType() === 'none' ) 
&&
                ( !this.isAligned() || this.isDefaultAligned() )
        ) {
                return 'mwInlineImage';
@@ -160,46 +160,49 @@
  * @param {ve.dm.Surface} surfaceModel Surface model of main document
  */
 ve.dm.MWImageModel.prototype.updateImageNode = function ( surfaceModel ) {
-       var captionRange,
+       var captionRange, captionNode,
                doc = surfaceModel.getDocument(),
-               captionNode = this.getMediaNode().getCaptionNode();
+               node = this.getMediaNode();
 
        // Update the caption
-       if ( !captionNode ) {
-               // There was no caption before, so insert one now
-               surfaceModel.getFragment()
-                       .adjustRange( 1 )
-                       .collapseRangeToStart()
-                       .insertContent( [ { 'type': 'mwImageCaption' }, { 
'type': '/mwImageCaption' } ] );
-               // Update the caption node
-               captionNode = this.getMediaNode().getCaptionNode();
+       if ( node.getType() === 'mwBlockImage' ) {
+               captionNode = node.getCaptionNode();
+               if ( !captionNode ) {
+                       // There was no caption before, so insert one now
+                       surfaceModel.getFragment()
+                               .adjustRange( 1 )
+                               .collapseRangeToStart()
+                               .insertContent( [ { 'type': 'mwImageCaption' }, 
{ 'type': '/mwImageCaption' } ] );
+                       // Update the caption node
+                       captionNode = this.getMediaNode().getCaptionNode();
+               }
+
+               captionRange = captionNode.getRange();
+
+               // Remove contents of old caption
+               surfaceModel.change(
+                       ve.dm.Transaction.newFromRemoval(
+                               doc,
+                               captionRange,
+                               true
+                       )
+               );
+
+               // Add contents of new caption
+               surfaceModel.change(
+                       ve.dm.Transaction.newFromDocumentInsertion(
+                               doc,
+                               captionRange.start,
+                               this.getCaptionDocument()
+                       )
+               );
        }
-
-       captionRange = captionNode.getRange();
-
-       // Remove contents of old caption
-       surfaceModel.change(
-               ve.dm.Transaction.newFromRemoval(
-                       doc,
-                       captionRange,
-                       true
-               )
-       );
-
-       // Add contents of new caption
-       surfaceModel.change(
-               ve.dm.Transaction.newFromDocumentInsertion(
-                       doc,
-                       captionRange.start,
-                       this.getCaptionDocument()
-               )
-       );
 
        // Update attributes
        surfaceModel.change(
                ve.dm.Transaction.newFromAttributeChanges(
                        doc,
-                       this.mediaNode.getOffset(),
+                       node.getOffset(),
                        this.getUpdatedAttributes()
                )
        );
@@ -232,7 +235,6 @@
                        'attributes': editAttributes
                }
        ];
-
        if ( nodeType === 'mwBlockImage' ) {
                contentToInsert.push( { 'type': 'mwImageCaption' } );
                contentToInsert.push( { 'type': '/mwImageCaption' } );
@@ -259,10 +261,10 @@
        // Select the new node (without extras)
        newFragment = surfaceModel.getFragment( newNodeRange );
        newFragment.select();
+       newNode = newFragment.getSelectedNode();
 
        // Check if there should be a caption
-       if ( nodeType === 'mwBlockImage' ) {
-               newNode = newFragment.getSelectedNode();
+       if ( newNode.getType() === 'mwBlockImage' ) {
 
                if ( this.getCaptionDocument().data.getLength() > 4 ) {
                        // Add contents of new caption
@@ -672,7 +674,7 @@
                align = this.getDefaultDir();
                this.toggleDefaultAlignment( true );
        } else {
-               this.toggleDefaultAlignment( false );
+               this.toggleDefaultAlignment( this.getDefaultDir() === align );
        }
 
        this.alignment = align;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1687d8c7430c723b52448a9e24641e2c30d85d6a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to