MWImageModel#insertImageNode: Separate out linmod generation

Change-Id: Idee01e2e59f2a68481476f3d784502d3ebdf015b
This commit is contained in:
Ed Sanders 2015-08-02 19:25:35 +01:00 committed by Jforrester
parent 322d1f54c6
commit a4b250c790

View file

@ -435,34 +435,15 @@ ve.dm.MWImageModel.prototype.updateImageNode = function ( node, surfaceModel ) {
* @throws {Error} Unknown image node type * @throws {Error} Unknown image node type
*/ */
ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) { ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) {
var editAttributes, captionDoc, var captionDoc, offset, contentToInsert,
offset,
contentToInsert = [],
nodeType = this.getImageNodeType(), nodeType = this.getImageNodeType(),
originalAttrs = ve.copy( this.getOriginalImageAttributes() ),
surfaceModel = fragment.getSurface(); surfaceModel = fragment.getSurface();
if ( !( fragment.getSelection() instanceof ve.dm.LinearSelection ) ) { if ( !( fragment.getSelection() instanceof ve.dm.LinearSelection ) ) {
return fragment; return fragment;
} }
editAttributes = $.extend( originalAttrs, this.getUpdatedAttributes() ); contentToInsert = this.getData();
// Remove old classes
delete editAttributes.originalClasses;
delete editAttributes.unrecognizedClasses;
// Newly created images must have valid URLs, so remove the error attribute
if ( this.isChangedImageSource() ) {
delete editAttributes.isError;
}
contentToInsert = [
{
type: nodeType,
attributes: editAttributes
},
{ type: '/' + nodeType }
];
switch ( nodeType ) { switch ( nodeType ) {
case 'mwInlineImage': case 'mwInlineImage':
@ -475,7 +456,6 @@ ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) {
return fragment; return fragment;
case 'mwBlockImage': case 'mwBlockImage':
contentToInsert.splice( 1, 0, { type: 'mwImageCaption' }, { type: '/mwImageCaption' } );
// Try to put the image in front of the structural node // Try to put the image in front of the structural node
offset = fragment.getDocument().data.getNearestStructuralOffset( fragment.getSelection().getRange().start, -1 ); offset = fragment.getDocument().data.getNearestStructuralOffset( fragment.getSelection().getRange().start, -1 );
if ( offset > -1 ) { if ( offset > -1 ) {
@ -501,6 +481,38 @@ ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) {
} }
}; };
/**
* Get linear data representation of the image
* @return {Array} Linear data
*/
ve.dm.MWImageModel.prototype.getData = function () {
var data,
originalAttrs = ve.copy( this.getOriginalImageAttributes() ),
editAttributes = $.extend( originalAttrs, this.getUpdatedAttributes() ),
nodeType = this.getImageNodeType();
// Remove old classes
delete editAttributes.originalClasses;
delete editAttributes.unrecognizedClasses;
// Newly created images must have valid URLs, so remove the error attribute
if ( this.isChangedImageSource() ) {
delete editAttributes.isError;
}
data = [
{
type: nodeType,
attributes: editAttributes
},
{ type: '/' + nodeType }
];
if ( nodeType === 'mwBlockImage' ) {
data.splice( 1, 0, { type: 'mwImageCaption' }, { type: '/mwImageCaption' } );
}
return data;
};
/** /**
* Return all updated attributes that belong to the node. * Return all updated attributes that belong to the node.
* @return {Object} Updated attributes * @return {Object} Updated attributes