|
|
@@ -7,6 +7,8 @@
|
|
|
* @module image/imagestyle/converters
|
|
|
*/
|
|
|
|
|
|
+import { isImage } from '../utils';
|
|
|
+
|
|
|
/**
|
|
|
* Returns converter for `imageStyle` attribute. It can be used for adding, changing and removing the attribute.
|
|
|
*
|
|
|
@@ -35,6 +37,57 @@ export function modelToViewSetStyle( styles ) {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Returns view to model converter converting image CSS classes to proper value in the model.
|
|
|
+ *
|
|
|
+ * @param {Array.<module:image/imagestyle/imagestyleengine~ImageStyleFormat>} styles Styles for which converter is created.
|
|
|
+ * @returns {Function} View to model converter.
|
|
|
+ */
|
|
|
+export function viewToModelImageStyles( styles ) {
|
|
|
+ // Convert only styles without `null` value.
|
|
|
+ const filteredStyles = styles.filter( style => style.value !== null );
|
|
|
+
|
|
|
+ return ( evt, data, consumable, conversionApi ) => {
|
|
|
+ for ( let style of filteredStyles ) {
|
|
|
+ viewToModelImageStyle( style, data, consumable, conversionApi );
|
|
|
+ }
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+// Converter from view to model converting single style.
|
|
|
+// For more information see {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher};
|
|
|
+//
|
|
|
+// @param {module:image/imagestyle/imagestyleengine~ImageStyleFormat} style
|
|
|
+// @param {Object} data
|
|
|
+// @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable
|
|
|
+// @param {Object} conversionApi
|
|
|
+function viewToModelImageStyle( style, data, consumable, conversionApi ) {
|
|
|
+ const viewFigureElement = data.input;
|
|
|
+ const modelImageElement = data.output;
|
|
|
+
|
|
|
+ // *** Step 1: Validate conversion.
|
|
|
+ // Check if view element has proper class to consume.
|
|
|
+ if ( !consumable.test( viewFigureElement, { class: style.className } ) ) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if figure is converted to image.
|
|
|
+ if ( !isImage( modelImageElement ) ) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if image element can be placed in current context wit additional attribute.
|
|
|
+ const attributes = [ ...modelImageElement.getAttributeKeys(), 'imageStyle' ];
|
|
|
+
|
|
|
+ if ( !conversionApi.schema.check( { name: 'image', inside: data.context, attributes } ) ) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // *** Step2: Convert to model.
|
|
|
+ consumable.consume( viewFigureElement, { class: style.className } );
|
|
|
+ modelImageElement.setAttribute( 'imageStyle', style.value );
|
|
|
+}
|
|
|
+
|
|
|
// Returns style with given `value` from array of styles.
|
|
|
//
|
|
|
// @param {String} value
|