converters.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import first from '@ckeditor/ckeditor5-utils/src/first';
  6. /**
  7. * @module image/imagestyle/converters
  8. */
  9. /**
  10. * Returns a converter for the `imageStyle` attribute. It can be used for adding, changing and removing the attribute.
  11. *
  12. * @param {Object} styles An object containing available styles. See {@link module:image/imagestyle/imagestyleediting~ImageStyleFormat}
  13. * for more details.
  14. * @returns {Function} A model-to-view attribute converter.
  15. */
  16. export function modelToViewStyleAttribute( styles ) {
  17. return ( evt, data, conversionApi ) => {
  18. if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
  19. return;
  20. }
  21. // Check if there is class name associated with given value.
  22. const newStyle = getStyleByName( data.attributeNewValue, styles );
  23. const oldStyle = getStyleByName( data.attributeOldValue, styles );
  24. const viewElement = conversionApi.mapper.toViewElement( data.item );
  25. const viewWriter = conversionApi.writer;
  26. if ( oldStyle ) {
  27. viewWriter.removeClass( oldStyle.className, viewElement );
  28. }
  29. if ( newStyle ) {
  30. viewWriter.addClass( newStyle.className, viewElement );
  31. }
  32. };
  33. }
  34. /**
  35. * Returns a view-to-model converter converting image CSS classes to a proper value in the model.
  36. *
  37. * @param {Array.<module:image/imagestyle/imagestyleediting~ImageStyleFormat>} styles The styles for which the converter is created.
  38. * @returns {Function} A view-to-model converter.
  39. */
  40. export function viewToModelStyleAttribute( styles ) {
  41. // Convert only non–default styles.
  42. const filteredStyles = styles.filter( style => !style.isDefault );
  43. return ( evt, data, conversionApi ) => {
  44. if ( !data.modelRange ) {
  45. return;
  46. }
  47. const viewFigureElement = data.viewItem;
  48. const modelImageElement = first( data.modelRange.getItems() );
  49. // Check if `imageStyle` attribute is allowed for current element.
  50. if ( !conversionApi.schema.checkAttribute( modelImageElement, 'imageStyle' ) ) {
  51. return;
  52. }
  53. // Convert style one by one.
  54. for ( const style of filteredStyles ) {
  55. // Try to consume class corresponding with style.
  56. if ( conversionApi.consumable.consume( viewFigureElement, { classes: style.className } ) ) {
  57. // And convert this style to model attribute.
  58. conversionApi.writer.setAttribute( 'imageStyle', style.name, modelImageElement );
  59. }
  60. }
  61. };
  62. }
  63. // Returns the style with a given `name` from an array of styles.
  64. //
  65. // @param {String} name
  66. // @param {Array.<module:image/imagestyle/imagestyleediting~ImageStyleFormat> } styles
  67. // @return {module:image/imagestyle/imagestyleediting~ImageStyleFormat|undefined}
  68. function getStyleByName( name, styles ) {
  69. for ( const style of styles ) {
  70. if ( style.name === name ) {
  71. return style;
  72. }
  73. }
  74. }