Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki image node.
10 *
11 * @class
12 * @abstract
13 * @extends ve.dm.GeneratedContentNode
14 * @mixes ve.dm.FocusableNode
15 * @mixes ve.dm.ResizableNode
16 *
17 * @constructor
18 */
19 ve.dm.MWImageNode = function VeDmMWImageNode() {
20 // Parent constructor
21 ve.dm.GeneratedContentNode.call( this );
22
23 // Mixin constructors
24 ve.dm.FocusableNode.call( this );
25 // ve.dm.MWResizableNode doesn't exist
26 ve.dm.ResizableNode.call( this );
27
28 this.scalablePromise = null;
29
30 // Use 'bitmap' as default media type until we can
31 // fetch the actual media type from the API
32 this.mediaType = 'BITMAP';
33
34 // Initialize
35 this.constructor.static.syncScalableToType(
36 this.getAttribute( 'type' ),
37 this.mediaType,
38 this.getScalable()
39 );
40
41 // Events
42 this.connect( this, { attributeChange: 'onAttributeChange' } );
43 };
44
45 /* Inheritance */
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 /* Static methods */
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 // Block image only:
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 * Get RDFa type
71 *
72 * @static
73 * @param {string} mediaClass Media class, one of 'File', 'Image', 'Video' or 'Audio'
74 * @param {string} frameType Frame type, one of 'none', 'frameless', 'thumb' or 'frame'
75 * @param {boolean} isError Whether the included media file is missing
76 * @return {string} RDFa type
77 */
78 ve.dm.MWImageNode.static.getRdfa = function ( mediaClass, frameType, isError ) {
79 return ( isError ? 'mw:Error ' : '' ) + 'mw:' + mediaClass + {
80 none: '',
81 frameless: '/Frameless',
82 // Block image only:
83 thumb: '/Thumb',
84 frame: '/Frame'
85 }[ frameType ];
86 };
87
88 /**
89 * Map media tags to source attributes
90 *
91 * @type {Object}
92 */
93 ve.dm.MWImageNode.static.tagsToSrcAttrs = {
94 img: 'src',
95 audio: null,
96 video: 'poster',
97 span: null
98 };
99
100 /**
101 * @inheritdoc ve.dm.GeneratedContentNode
102 */
103 ve.dm.MWImageNode.static.getHashObjectForRendering = function ( dataElement ) {
104 // "Rendering" is just the URL of the thumbnail, so we only
105 // care about src & dimensions
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 // Images with different src's shouldn't be diffed
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 // Skip noise from the data model
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 // The following messages are used here:
176 // * visualeditor-align-desc-left
177 // * visualeditor-align-desc-right
178 // * visualeditor-align-desc-center
179 // * visualeditor-align-desc-default
180 // * visualeditor-align-desc-none
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 // TODO: Handle valign
190 }
191 // Parent method
192 return ve.dm.Node.static.describeChange.apply( this, arguments );
193 };
194
195 /**
196 * Take the given dimensions and scale them to thumbnail size.
197 *
198 * @param {Object} dimensions Width and height of the image
199 * @param {string} [mediaType] Media type 'DRAWING' or 'BITMAP'
200 * @return {Object} The new width and height of the scaled image
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 // Use dimensions
210 // Resize to default thumbnail size, but only if the image itself
211 // isn't smaller than the default size
212 // For svg/drawings, the default wiki size is always applied
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 * Translate the image dimensions into new ones according to the bounding box.
224 *
225 * @param {Object} imageDimensions Width and height of the image
226 * @param {Object} boundingBox The limit of the bounding box
227 * @return {Object} The new width and height of the scaled image.
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 // Scale down
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 * Update image scalable properties according to the image type.
248 *
249 * @param {string} type The new image type
250 * @param {string} mediaType Image media type 'DRAWING' or 'BITMAP'
251 * @param {ve.dm.Scalable} scalable The scalable object to update
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 // We can only set default dimensions if we have the original ones
260 if ( originalDimensions ) {
261 if ( type === 'thumb' || type === 'frameless' ) {
262 // Set the default size to that in the wiki configuration if
263 // 1. The original image width is not smaller than the default
264 // 2. If the image is an SVG drawing
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 // Deal with maximum dimensions for images and drawings
283 if ( mediaType === 'DRAWING' ) {
284 // Vector images are scalable past their original dimensions
285 // EnforcedMax may have previously been set to true
286 scalable.setEnforcedMax( false );
287
288 } else if ( mediaType === 'AUDIO' ) {
289 // Audio files are scalable to any width but have fixed height
290 scalable.fixedRatio = false;
291 scalable.setMinDimensions( { width: 1, height: 32 } );
292 // TODO: No way to enforce max height but not max width
293 scalable.setMaxDimensions( { width: 99999, height: 32 } );
294 scalable.setEnforcedMax( true );
295 scalable.setEnforcedMin( true );
296
297 // Default dimensions for audio files are 0x0, which is no good
298 scalable.setDefaultDimensions( { width: defaultThumbSize, height: 32 } );
299
300 } else {
301 // Raster image files are limited to their original dimensions
302 if ( originalDimensions ) {
303 scalable.setMaxDimensions( originalDimensions );
304 scalable.setEnforcedMax( true );
305 } else {
306 scalable.setEnforcedMax( false );
307 }
308 }
309 // TODO: Some day, when $wgSvgMaxSize works properly in MediaWiki
310 // we can add it back as max dimension consideration.
311 };
312
313 /**
314 * Get the scalable promise which fetches original dimensions from the API
315 *
316 * @param {string} filename The image filename whose details the scalable will represent
317 * @return {jQuery.Promise} Promise which resolves after the image size details are fetched from the API
318 */
319 ve.dm.MWImageNode.static.getScalablePromise = function ( filename ) {
320 // On the first call set off an async call to update the scalable's
321 // original dimensions from the API.
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 /* Methods */
335
336 /**
337 * Respond to attribute change.
338 * Update the rendering of the 'align', src', 'width' and 'height' attributes
339 * when they change in the model.
340 *
341 * @param {string} key Attribute key
342 * @param {string} from Old value
343 * @param {string} to New value
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 * Get the normalised filename of the image
353 *
354 * @return {string} Filename (including namespace)
355 */
356 ve.dm.MWImageNode.prototype.getFilename = function () {
357 return mw.libs.ve.normalizeParsoidResourceName( this.getAttribute( 'resource' ) || '' );
358 };
359
360 /**
361 * @inheritdoc
362 */
363 ve.dm.MWImageNode.prototype.getScalable = function () {
364 if ( !this.scalablePromise ) {
365 this.scalablePromise = ve.dm.MWImageNode.static.getScalablePromise( this.getFilename() );
366 // If the promise was already resolved before getScalablePromise returned, then jQuery will execute the done straight away.
367 // So don't just do getScalablePromise( ... ).done because we need to make sure that this.scalablePromise gets set first.
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 // Update media type
376 this.mediaType = info.mediatype;
377 // Update according to type
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 // Mixin method
388 return ve.dm.ResizableNode.prototype.getScalable.call( this );
389 };
390
391 /**
392 * @inheritdoc
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 * Get symbolic name of media type.
409 *
410 * Example values: "BITMAP" for JPEG or PNG images; "DRAWING" for SVG graphics
411 *
412 * @return {string|undefined} Symbolic media type name, or undefined if empty
413 */
414 ve.dm.MWImageNode.prototype.getMediaType = function () {
415 return this.mediaType;
416 };
417
418 /**
419 * Get RDFa type
420 *
421 * @return {string} RDFa type
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 };