tableproperties.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/converters/tableproperites
  7. */
  8. /**
  9. * Conversion helper for upcasting attribute using normalized styles.
  10. *
  11. * @param {module:engine/conversion/conversion~Conversion} conversion
  12. * @param {String} modelElement
  13. * @param {String} modelAttribute
  14. * @param {String} styleName
  15. */
  16. export function upcastStyleToAttribute( conversion, modelElement, modelAttribute, styleName ) {
  17. conversion.for( 'upcast' ).attributeToAttribute( {
  18. view: {
  19. styles: {
  20. [ styleName ]: /[\s\S]+/
  21. }
  22. },
  23. model: {
  24. name: modelElement,
  25. key: modelAttribute,
  26. value: viewElement => viewElement.getNormalizedStyle( styleName )
  27. }
  28. } );
  29. }
  30. /**
  31. * Conversion helper for upcasting border styles for view element.
  32. *
  33. * @param {module:engine/conversion/conversion~Conversion} conversion
  34. * @param {String} viewElementName
  35. */
  36. export function upcastBorderStyles( conversion, viewElementName ) {
  37. conversion.for( 'upcast' ).add( dispatcher => dispatcher.on( 'element:' + viewElementName, ( evt, data, conversionApi ) => {
  38. // TODO: this is counter-intuitive: ie.: if only `border-top` is defined then `hasStyle( 'border' )` also returns true.
  39. // TODO: this might needs to be fixed in styles normalizer.
  40. const stylesToConsume = [
  41. 'border-top',
  42. 'border-right',
  43. 'border-bottom',
  44. 'border-left'
  45. ].filter( styleName => data.viewItem.hasStyle( styleName ) );
  46. if ( !stylesToConsume.length ) {
  47. return;
  48. }
  49. const matcherPattern = {
  50. styles: stylesToConsume
  51. };
  52. // Try to consume appropriate values from consumable values list.
  53. if ( !conversionApi.consumable.test( data.viewItem, matcherPattern ) ) {
  54. return;
  55. }
  56. const modelElement = [ ...data.modelRange.getItems( { shallow: true } ) ].pop();
  57. conversionApi.consumable.consume( data.viewItem, matcherPattern );
  58. conversionApi.writer.setAttribute( 'borderStyle', data.viewItem.getNormalizedStyle( 'border-style' ), modelElement );
  59. conversionApi.writer.setAttribute( 'borderColor', data.viewItem.getNormalizedStyle( 'border-color' ), modelElement );
  60. conversionApi.writer.setAttribute( 'borderWidth', data.viewItem.getNormalizedStyle( 'border-width' ), modelElement );
  61. } ) );
  62. }
  63. /**
  64. * Conversion helper for downcasting attribute to a style.
  65. *
  66. * @param {module:engine/conversion/conversion~Conversion} conversion
  67. * @param {String} modelElement
  68. * @param {String} modelAttribute
  69. * @param {String} styleName
  70. */
  71. export function downcastAttributeToStyle( conversion, modelElement, modelAttribute, styleName ) {
  72. conversion.for( 'downcast' ).attributeToAttribute( {
  73. model: {
  74. name: modelElement,
  75. key: modelAttribute
  76. },
  77. view: modelAttributeValue => ( {
  78. key: 'style',
  79. value: {
  80. [ styleName ]: modelAttributeValue
  81. }
  82. } )
  83. } );
  84. }
  85. /**
  86. * Conversion helper for downcasting attributes from model's table to a view table (not to figure).
  87. *
  88. * @param {module:engine/conversion/conversion~Conversion} conversion
  89. * @param {String} modelAttribute
  90. * @param {String} styleName
  91. */
  92. export function downcastTableAttribute( conversion, modelAttribute, styleName ) {
  93. conversion.for( 'downcast' ).add( dispatcher => dispatcher.on( `attribute:${ modelAttribute }:table`, ( evt, data, conversionApi ) => {
  94. const { item, attributeNewValue } = data;
  95. const { mapper, writer } = conversionApi;
  96. if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
  97. return;
  98. }
  99. const table = [ ...mapper.toViewElement( item ).getChildren() ].find( child => child.is( 'table' ) );
  100. if ( attributeNewValue ) {
  101. writer.setStyle( styleName, attributeNewValue, table );
  102. } else {
  103. writer.removeStyle( styleName, table );
  104. }
  105. } ) );
  106. }