tablecellpropertiesediting.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/tablecellproperties/tablecellpropertiesediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import { addBorderRules } from '@ckeditor/ckeditor5-engine/src/view/styles/border';
  10. import { addPaddingRules } from '@ckeditor/ckeditor5-engine/src/view/styles/padding';
  11. import { addBackgroundRules } from '@ckeditor/ckeditor5-engine/src/view/styles/background';
  12. import { downcastAttributeToStyle, upcastStyleToAttribute, upcastBorderStyles } from './../converters/tableproperties';
  13. import TableEditing from './../tableediting';
  14. import TableCellPaddingCommand from './commands/tablecellpaddingcommand';
  15. import TableCellWidthCommand from './commands/tablecellwidthcommand';
  16. import TableCellHeightCommand from './commands/tablecellheightcommand';
  17. import TableCellBackgroundColorCommand from './commands/tablecellbackgroundcolorcommand';
  18. import TableCellVerticalAlignmentCommand from './commands/tablecellverticalalignmentcommand';
  19. import TableCellHorizontalAlignmentCommand from './commands/tablecellhorizontalalignmentcommand';
  20. import TableCellBorderStyleCommand from './commands/tablecellborderstylecommand';
  21. import TableCellBorderColorCommand from './commands/tablecellbordercolorcommand';
  22. import TableCellBorderWidthCommand from './commands/tablecellborderwidthcommand';
  23. const VALIGN_VALUES_REG_EXP = /^(top|bottom)$/;
  24. /**
  25. * The table cell properties editing feature.
  26. *
  27. * Introduces table cell model attributes and their conversion:
  28. *
  29. * - border: `borderStyle`, `borderColor` and `borderWidth`
  30. * - background color: `backgroundColor`
  31. * - cell padding: `padding`
  32. * - horizontal and vertical alignment: `horizontalAlignment`, `verticalAlignment`
  33. * - cell width and height: `width`, `height`
  34. *
  35. * It also registers commands used to manipulate the above attributes:
  36. *
  37. * - border: the `'tableCellBorderStyle'`, `'tableCellBorderColor'` and `'tableCellBorderWidth'` commands
  38. * - background color: the `'tableCellBackgroundColor'` command
  39. * - cell padding: the `'tableCellPadding'` command
  40. * - horizontal and vertical alignment: the `'tableCellHorizontalAlignment'` and `'tableCellVerticalAlignment'` commands
  41. * - width and height: the `'tableCellWidth'` and `'tableCellHeight'` commands
  42. *
  43. * @extends module:core/plugin~Plugin
  44. */
  45. export default class TableCellPropertiesEditing extends Plugin {
  46. /**
  47. * @inheritDoc
  48. */
  49. static get pluginName() {
  50. return 'TableCellPropertiesEditing';
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. static get requires() {
  56. return [ TableEditing ];
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. init() {
  62. const editor = this.editor;
  63. const schema = editor.model.schema;
  64. const conversion = editor.conversion;
  65. editor.data.addStyleProcessorRules( addBorderRules );
  66. enableBorderProperties( schema, conversion );
  67. editor.commands.add( 'tableCellBorderStyle', new TableCellBorderStyleCommand( editor ) );
  68. editor.commands.add( 'tableCellBorderColor', new TableCellBorderColorCommand( editor ) );
  69. editor.commands.add( 'tableCellBorderWidth', new TableCellBorderWidthCommand( editor ) );
  70. enableHorizontalAlignmentProperty( schema, conversion );
  71. editor.commands.add( 'tableCellHorizontalAlignment', new TableCellHorizontalAlignmentCommand( editor ) );
  72. enableProperty( schema, conversion, 'width', 'width' );
  73. editor.commands.add( 'tableCellWidth', new TableCellWidthCommand( editor ) );
  74. enableProperty( schema, conversion, 'height', 'height' );
  75. editor.commands.add( 'tableCellHeight', new TableCellHeightCommand( editor ) );
  76. editor.data.addStyleProcessorRules( addPaddingRules );
  77. enableProperty( schema, conversion, 'padding', 'padding' );
  78. editor.commands.add( 'tableCellPadding', new TableCellPaddingCommand( editor ) );
  79. editor.data.addStyleProcessorRules( addBackgroundRules );
  80. enableProperty( schema, conversion, 'backgroundColor', 'background-color' );
  81. editor.commands.add( 'tableCellBackgroundColor', new TableCellBackgroundColorCommand( editor ) );
  82. enableVerticalAlignmentProperty( schema, conversion );
  83. editor.commands.add( 'tableCellVerticalAlignment', new TableCellVerticalAlignmentCommand( editor ) );
  84. }
  85. }
  86. // Enables the `'borderStyle'`, `'borderColor'` and `'borderWidth'` attributes for table cells.
  87. //
  88. // @param {module:engine/model/schema~Schema} schema
  89. // @param {module:engine/conversion/conversion~Conversion} conversion
  90. function enableBorderProperties( schema, conversion ) {
  91. schema.extend( 'tableCell', {
  92. allowAttributes: [ 'borderWidth', 'borderColor', 'borderStyle' ]
  93. } );
  94. upcastBorderStyles( conversion, 'td' );
  95. upcastBorderStyles( conversion, 'th' );
  96. downcastAttributeToStyle( conversion, 'tableCell', 'borderStyle', 'border-style' );
  97. downcastAttributeToStyle( conversion, 'tableCell', 'borderColor', 'border-color' );
  98. downcastAttributeToStyle( conversion, 'tableCell', 'borderWidth', 'border-width' );
  99. }
  100. // Enables the `'horizontalAlignment'` attribute for table cells.
  101. //
  102. // @param {module:engine/model/schema~Schema} schema
  103. // @param {module:engine/conversion/conversion~Conversion} conversion
  104. function enableHorizontalAlignmentProperty( schema, conversion ) {
  105. schema.extend( 'tableCell', {
  106. allowAttributes: [ 'horizontalAlignment' ]
  107. } );
  108. conversion.attributeToAttribute( {
  109. model: {
  110. name: 'tableCell',
  111. key: 'horizontalAlignment',
  112. values: [ 'right', 'center', 'justify' ]
  113. },
  114. view: {
  115. right: {
  116. key: 'style',
  117. value: {
  118. 'text-align': 'right'
  119. }
  120. },
  121. center: {
  122. key: 'style',
  123. value: {
  124. 'text-align': 'center'
  125. }
  126. },
  127. justify: {
  128. key: 'style',
  129. value: {
  130. 'text-align': 'justify'
  131. }
  132. }
  133. }
  134. } );
  135. }
  136. // Enables the `'verticalAlignment'` attribute for table cells.
  137. //
  138. // @param {module:engine/model/schema~Schema} schema
  139. // @param {module:engine/conversion/conversion~Conversion} conversion
  140. function enableVerticalAlignmentProperty( schema, conversion ) {
  141. schema.extend( 'tableCell', {
  142. allowAttributes: [ 'verticalAlignment' ]
  143. } );
  144. conversion.attributeToAttribute( {
  145. model: {
  146. name: 'tableCell',
  147. key: 'verticalAlignment',
  148. values: [ 'top', 'bottom' ]
  149. },
  150. view: {
  151. top: {
  152. key: 'style',
  153. value: {
  154. 'vertical-align': 'top'
  155. }
  156. },
  157. bottom: {
  158. key: 'style',
  159. value: {
  160. 'vertical-align': 'bottom'
  161. }
  162. }
  163. }
  164. } );
  165. conversion.for( 'upcast' )
  166. // Support for backwards compatibility and pasting from other sources.
  167. .attributeToAttribute( {
  168. view: {
  169. attributes: {
  170. valign: VALIGN_VALUES_REG_EXP
  171. }
  172. },
  173. model: {
  174. name: 'tableCell',
  175. key: 'verticalAlignment',
  176. value: viewElement => viewElement.getAttribute( 'valign' )
  177. }
  178. } );
  179. }
  180. // Enables conversion for an attribute for simple view-model mappings.
  181. //
  182. // @param {String} modelAttribute
  183. // @param {String} styleName
  184. // @param {module:engine/model/schema~Schema} schema
  185. // @param {module:engine/conversion/conversion~Conversion} conversion
  186. function enableProperty( schema, conversion, modelAttribute, styleName ) {
  187. schema.extend( 'tableCell', {
  188. allowAttributes: [ modelAttribute ]
  189. } );
  190. upcastStyleToAttribute( conversion, 'tableCell', modelAttribute, styleName );
  191. downcastAttributeToStyle( conversion, 'tableCell', modelAttribute, styleName );
  192. }