From eb2ee52de094d39975c465db721836b34b96c28b Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Thu, 24 Aug 2017 15:56:22 +0100 Subject: [PATCH] Create wrapper paragraph when converting block image to inline This prevents the image jumping into another paragraph, e.g. in a different table cell. Move the logic for removing the old image out of MWMediaDialog and into MWImageModel#insertImageNode. Bug: T121449 Change-Id: Ibd7c92f3f90c382ceffd3e0defb12ba36a3786d2 --- modules/ve-mw/dm/models/ve.dm.MWImageModel.js | 20 ++++++++++++++++--- .../ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js | 10 ---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js index e7c0e9f42f..cdcf1a6806 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js @@ -438,7 +438,7 @@ ve.dm.MWImageModel.prototype.updateImageNode = function ( node, surfaceModel ) { * @throws {Error} Unknown image node type */ ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) { - var captionDoc, offset, contentToInsert, + var captionDoc, offset, contentToInsert, selectedNode, nodeType = this.getImageNodeType(), surfaceModel = fragment.getSurface(); @@ -446,12 +446,26 @@ ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) { return fragment; } + selectedNode = fragment.getSelectedNode(); + + // If there was a previous node, remove it first + if ( selectedNode ) { + // Remove the old image + fragment.removeContent(); + } + contentToInsert = this.getData(); switch ( nodeType ) { case 'mwInlineImage': - // Try to put the image inside the nearest content node - offset = fragment.getDocument().data.getNearestContentOffset( fragment.getSelection().getRange().start ); + if ( selectedNode.type === 'mwBlockImage' ) { + // If converting from a block image, create a wrapper paragraph for the inline image to go in. + fragment.insertContent( [ { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' } ] ); + offset = fragment.getSelection().getRange().start + 1; + } else { + // Try to put the image inside the nearest content node + offset = fragment.getDocument().data.getNearestContentOffset( fragment.getSelection().getRange().start ); + } if ( offset > -1 ) { fragment = fragment.clone( new ve.dm.LinearSelection( fragment.getDocument(), new ve.Range( offset ) ) ); } diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js index 36ca56d0ab..0f150285f0 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js @@ -1393,16 +1393,6 @@ ve.ui.MWMediaDialog.prototype.getActionProcess = function ( action ) { this.imageModel.updateImageNode( this.selectedNode, surfaceModel ); } else { // Replacing an image or inserting a brand new one - - // If there was a previous node, remove it first - if ( this.selectedNode ) { - // Remove the old image - this.fragment = this.getFragment().clone( - new ve.dm.LinearSelection( this.fragment.getDocument(), this.selectedNode.getOuterRange() ) - ); - this.fragment.removeContent(); - } - // Insert the new image this.fragment = this.imageModel.insertImageNode( this.getFragment() ); }