| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.dm.MWImageNode = function VeDmMWImageNode() { |
| 20 |
|
| 21 |
ve.dm.GeneratedContentNode.call( this ); |
| 22 |
|
| 23 |
|
| 24 |
ve.dm.FocusableNode.call( this ); |
| 25 |
|
| 26 |
ve.dm.ResizableNode.call( this ); |
| 27 |
|
| 28 |
this.scalablePromise = null; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
this.mediaType = 'BITMAP'; |
| 33 |
|
| 34 |
|
| 35 |
this.constructor.static.syncScalableToType( |
| 36 |
this.getAttribute( 'type' ), |
| 37 |
this.mediaType, |
| 38 |
this.getScalable() |
| 39 |
); |
| 40 |
|
| 41 |
|
| 42 |
this.connect( this, { attributeChange: 'onAttributeChange' } ); |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
OO.inheritClass( ve.dm.MWImageNode, ve.dm.GeneratedContentNode ); |
| 48 |
|
| 49 |
OO.mixinClass( ve.dm.MWImageNode, ve.dm.FocusableNode ); |
| 50 |
|
| 51 |
OO.mixinClass( ve.dm.MWImageNode, ve.dm.ResizableNode ); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ve.dm.MWImageNode.static.rdfaToTypes = ( function () { |
| 56 |
const rdfaToType = {}; |
| 57 |
|
| 58 |
[ 'File', 'Image', 'Video', 'Audio' ].forEach( ( mediaClass ) => { |
| 59 |
rdfaToType[ 'mw:' + mediaClass ] = { mediaClass: mediaClass, frameType: 'none' }; |
| 60 |
rdfaToType[ 'mw:' + mediaClass + '/Frameless' ] = { mediaClass: mediaClass, frameType: 'frameless' }; |
| 61 |
|
| 62 |
rdfaToType[ 'mw:' + mediaClass + '/Thumb' ] = { mediaClass: mediaClass, frameType: 'thumb' }; |
| 63 |
rdfaToType[ 'mw:' + mediaClass + '/Frame' ] = { mediaClass: mediaClass, frameType: 'frame' }; |
| 64 |
} ); |
| 65 |
|
| 66 |
return rdfaToType; |
| 67 |
}() ); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.dm.MWImageNode.static.getRdfa = function ( mediaClass, frameType, isError ) { |
| 79 |
return ( isError ? 'mw:Error ' : '' ) + 'mw:' + mediaClass + { |
| 80 |
none: '', |
| 81 |
frameless: '/Frameless', |
| 82 |
|
| 83 |
thumb: '/Thumb', |
| 84 |
frame: '/Frame' |
| 85 |
}[ frameType ]; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
ve.dm.MWImageNode.static.tagsToSrcAttrs = { |
| 94 |
img: 'src', |
| 95 |
audio: null, |
| 96 |
video: 'poster', |
| 97 |
span: null |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ve.dm.MWImageNode.static.getHashObjectForRendering = function ( dataElement ) { |
| 104 |
|
| 105 |
|
| 106 |
return { |
| 107 |
type: 'mwImage', |
| 108 |
resource: dataElement.attributes.resource, |
| 109 |
width: dataElement.attributes.width, |
| 110 |
height: dataElement.attributes.height |
| 111 |
}; |
| 112 |
}; |
| 113 |
|
| 114 |
ve.dm.MWImageNode.static.getMatchRdfaTypes = function () { |
| 115 |
return Object.keys( this.rdfaToTypes ); |
| 116 |
}; |
| 117 |
|
| 118 |
ve.dm.MWImageNode.static.allowedRdfaTypes = [ 'mw:Error' ]; |
| 119 |
|
| 120 |
ve.dm.MWImageNode.static.isDiffComparable = function ( element, other ) { |
| 121 |
|
| 122 |
return element.type === other.type && element.attributes.resource === other.attributes.resource; |
| 123 |
}; |
| 124 |
|
| 125 |
ve.dm.MWImageNode.static.describeChanges = function ( attributeChanges, attributes ) { |
| 126 |
const customKeys = [ 'width', 'height', 'defaultSize', 'src', 'href' ], |
| 127 |
descriptions = []; |
| 128 |
|
| 129 |
function describeSize( width, height ) { |
| 130 |
return width + ve.msg( 'visualeditor-dimensionswidget-times' ) + height + ve.msg( 'visualeditor-dimensionswidget-px' ); |
| 131 |
} |
| 132 |
|
| 133 |
if ( 'width' in attributeChanges || 'height' in attributeChanges ) { |
| 134 |
let sizeFrom, sizeTo; |
| 135 |
if ( attributeChanges.defaultSize && attributeChanges.defaultSize.from === true ) { |
| 136 |
sizeFrom = ve.msg( 'visualeditor-mediasizewidget-sizeoptions-default' ); |
| 137 |
} else { |
| 138 |
sizeFrom = describeSize( |
| 139 |
'width' in attributeChanges ? attributeChanges.width.from : attributes.width, |
| 140 |
'height' in attributeChanges ? attributeChanges.height.from : attributes.height |
| 141 |
); |
| 142 |
} |
| 143 |
if ( attributeChanges.defaultSize && attributeChanges.defaultSize.to === true ) { |
| 144 |
sizeTo = ve.msg( 'visualeditor-mediasizewidget-sizeoptions-default' ); |
| 145 |
} else { |
| 146 |
sizeTo = describeSize( |
| 147 |
'width' in attributeChanges ? attributeChanges.width.to : attributes.width, |
| 148 |
'height' in attributeChanges ? attributeChanges.height.to : attributes.height |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
descriptions.push( |
| 153 |
ve.htmlMsg( 'visualeditor-changedesc-image-size', this.wrapText( 'del', sizeFrom ), this.wrapText( 'ins', sizeTo ) ) |
| 154 |
); |
| 155 |
} |
| 156 |
for ( const key in attributeChanges ) { |
| 157 |
if ( customKeys.indexOf( key ) === -1 ) { |
| 158 |
if ( key === 'borderImage' && !attributeChanges.borderImage.from && !attributeChanges.borderImage.to ) { |
| 159 |
|
| 160 |
continue; |
| 161 |
} |
| 162 |
const change = this.describeChange( key, attributeChanges[ key ] ); |
| 163 |
if ( change ) { |
| 164 |
descriptions.push( change ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
return descriptions; |
| 169 |
}; |
| 170 |
|
| 171 |
ve.dm.MWImageNode.static.describeChange = function ( key, change ) { |
| 172 |
switch ( key ) { |
| 173 |
case 'align': |
| 174 |
return ve.htmlMsg( 'visualeditor-changedesc-align', |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
this.wrapText( 'del', ve.msg( 'visualeditor-align-desc-' + change.from ) ), |
| 182 |
this.wrapText( 'ins', ve.msg( 'visualeditor-align-desc-' + change.to ) ) |
| 183 |
); |
| 184 |
case 'originalWidth': |
| 185 |
case 'originalHeight': |
| 186 |
case 'originalClasses': |
| 187 |
case 'unrecognizedClasses': |
| 188 |
return; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
return ve.dm.Node.static.describeChange.apply( this, arguments ); |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
ve.dm.MWImageNode.static.scaleToThumbnailSize = function ( dimensions, mediaType ) { |
| 203 |
const defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' ) |
| 204 |
.thumbLimits[ mw.user.options.get( 'thumbsize' ) ]; |
| 205 |
|
| 206 |
mediaType = mediaType || 'BITMAP'; |
| 207 |
|
| 208 |
if ( dimensions.width && dimensions.height ) { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if ( dimensions.width > defaultThumbSize || mediaType === 'DRAWING' ) { |
| 214 |
return ve.dm.Scalable.static.getDimensionsFromValue( { |
| 215 |
width: defaultThumbSize |
| 216 |
}, dimensions.width / dimensions.height ); |
| 217 |
} |
| 218 |
} |
| 219 |
return dimensions; |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
ve.dm.MWImageNode.static.resizeToBoundingBox = function ( imageDimensions, boundingBox ) { |
| 230 |
const scale = Math.min( |
| 231 |
boundingBox.height / imageDimensions.height, |
| 232 |
boundingBox.width / imageDimensions.width |
| 233 |
); |
| 234 |
|
| 235 |
let newDimensions = ve.copy( imageDimensions ); |
| 236 |
if ( scale < 1 ) { |
| 237 |
|
| 238 |
newDimensions = { |
| 239 |
width: Math.floor( newDimensions.width * scale ), |
| 240 |
height: Math.floor( newDimensions.height * scale ) |
| 241 |
}; |
| 242 |
} |
| 243 |
return newDimensions; |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
ve.dm.MWImageNode.static.syncScalableToType = function ( type, mediaType, scalable ) { |
| 254 |
const defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' ) |
| 255 |
.thumbLimits[ mw.user.options.get( 'thumbsize' ) ]; |
| 256 |
|
| 257 |
const originalDimensions = scalable.getOriginalDimensions(); |
| 258 |
|
| 259 |
|
| 260 |
if ( originalDimensions ) { |
| 261 |
if ( type === 'thumb' || type === 'frameless' ) { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
let dimensions; |
| 266 |
if ( originalDimensions.width >= defaultThumbSize || mediaType === 'DRAWING' ) { |
| 267 |
dimensions = ve.dm.Scalable.static.getDimensionsFromValue( { |
| 268 |
width: defaultThumbSize |
| 269 |
}, scalable.getRatio() ); |
| 270 |
} else { |
| 271 |
dimensions = ve.dm.Scalable.static.getDimensionsFromValue( |
| 272 |
originalDimensions, |
| 273 |
scalable.getRatio() |
| 274 |
); |
| 275 |
} |
| 276 |
scalable.setDefaultDimensions( dimensions ); |
| 277 |
} else { |
| 278 |
scalable.setDefaultDimensions( originalDimensions ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
if ( mediaType === 'DRAWING' ) { |
| 284 |
|
| 285 |
|
| 286 |
scalable.setEnforcedMax( false ); |
| 287 |
|
| 288 |
} else if ( mediaType === 'AUDIO' ) { |
| 289 |
|
| 290 |
scalable.fixedRatio = false; |
| 291 |
scalable.setMinDimensions( { width: 1, height: 32 } ); |
| 292 |
|
| 293 |
scalable.setMaxDimensions( { width: 99999, height: 32 } ); |
| 294 |
scalable.setEnforcedMax( true ); |
| 295 |
scalable.setEnforcedMin( true ); |
| 296 |
|
| 297 |
|
| 298 |
scalable.setDefaultDimensions( { width: defaultThumbSize, height: 32 } ); |
| 299 |
|
| 300 |
} else { |
| 301 |
|
| 302 |
if ( originalDimensions ) { |
| 303 |
scalable.setMaxDimensions( originalDimensions ); |
| 304 |
scalable.setEnforcedMax( true ); |
| 305 |
} else { |
| 306 |
scalable.setEnforcedMax( false ); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
ve.dm.MWImageNode.static.getScalablePromise = function ( filename ) { |
| 320 |
|
| 321 |
|
| 322 |
if ( ve.init.platform.imageInfoCache ) { |
| 323 |
return ve.init.platform.imageInfoCache.get( filename ).then( ( info ) => { |
| 324 |
if ( !info || info.missing ) { |
| 325 |
return ve.createDeferred().reject().promise(); |
| 326 |
} |
| 327 |
return info; |
| 328 |
} ); |
| 329 |
} else { |
| 330 |
return ve.createDeferred().reject().promise(); |
| 331 |
} |
| 332 |
}; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
ve.dm.MWImageNode.prototype.onAttributeChange = function ( key, from, to ) { |
| 346 |
if ( key === 'type' ) { |
| 347 |
this.constructor.static.syncScalableToType( to, this.mediaType, this.getScalable() ); |
| 348 |
} |
| 349 |
}; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
ve.dm.MWImageNode.prototype.getFilename = function () { |
| 357 |
return mw.libs.ve.normalizeParsoidResourceName( this.getAttribute( 'resource' ) || '' ); |
| 358 |
}; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
ve.dm.MWImageNode.prototype.getScalable = function () { |
| 364 |
if ( !this.scalablePromise ) { |
| 365 |
this.scalablePromise = ve.dm.MWImageNode.static.getScalablePromise( this.getFilename() ); |
| 366 |
|
| 367 |
|
| 368 |
this.scalablePromise.done( ( info ) => { |
| 369 |
if ( info ) { |
| 370 |
this.getScalable().setOriginalDimensions( { |
| 371 |
width: info.width, |
| 372 |
height: info.height |
| 373 |
} ); |
| 374 |
const oldMediaType = this.mediaType; |
| 375 |
|
| 376 |
this.mediaType = info.mediatype; |
| 377 |
|
| 378 |
this.constructor.static.syncScalableToType( |
| 379 |
this.getAttribute( 'type' ), |
| 380 |
this.mediaType, |
| 381 |
this.getScalable() |
| 382 |
); |
| 383 |
this.emit( 'attributeChange', 'mediaType', oldMediaType, this.mediaType ); |
| 384 |
} |
| 385 |
} ); |
| 386 |
} |
| 387 |
|
| 388 |
return ve.dm.ResizableNode.prototype.getScalable.call( this ); |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
ve.dm.MWImageNode.prototype.createScalable = function () { |
| 395 |
return new ve.dm.Scalable( { |
| 396 |
currentDimensions: { |
| 397 |
width: this.getAttribute( 'width' ), |
| 398 |
height: this.getAttribute( 'height' ) |
| 399 |
}, |
| 400 |
minDimensions: { |
| 401 |
width: 1, |
| 402 |
height: 1 |
| 403 |
} |
| 404 |
} ); |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
ve.dm.MWImageNode.prototype.getMediaType = function () { |
| 415 |
return this.mediaType; |
| 416 |
}; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
ve.dm.MWImageNode.prototype.getRdfa = function () { |
| 424 |
return this.constructor.static.getRdfa( |
| 425 |
this.getAttribute( 'mediaClass' ), |
| 426 |
this.getAttribute( 'type' ), |
| 427 |
this.getAttribute( 'isError' ) |
| 428 |
); |
| 429 |
}; |