tablecellpropertiesediting.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  24. * The table cell properties editing feature.
  25. *
  26. * Introduces table cells's model attributes and their conversion:
  27. *
  28. * - border: `borderStyle`, `borderColor` and `borderWidth`
  29. * - background color: `backgroundColor`
  30. * - cell padding: `padding`
  31. * - horizontal and vertical alignment: `horizontalAlignment`, `verticalAlignment`
  32. * - cell width & height: `width` & `height`
  33. *
  34. * It also registers commands used to manipulate the above attributes:
  35. *
  36. * - border: `'tableCellBorderStyle'`, `'tableCellBorderColor'` and `'tableCellBorderWidth'` commands
  37. * - background color: `'tableCellBackgroundColor'`
  38. * - cell padding: `'tableCellPadding'`
  39. * - horizontal and vertical alignment: `'tableCellHorizontalAlignment'`, `'tableCellVerticalAlignment'`
  40. * - width & height: `'tableCellWidth'` & `'tableCellHeight'`
  41. *
  42. * @extends module:core/plugin~Plugin
  43. */
  44. export default class TableCellPropertiesEditing extends Plugin {
  45. /**
  46. * @inheritDoc
  47. */
  48. static get pluginName() {
  49. return 'TableCellPropertiesEditing';
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. static get requires() {
  55. return [ TableEditing ];
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. init() {
  61. const editor = this.editor;
  62. const schema = editor.model.schema;
  63. const conversion = editor.conversion;
  64. const viewDoc = editor.editing.view.document;
  65. viewDoc.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. viewDoc.addStyleProcessorRules( addPaddingRules );
  77. enableProperty( schema, conversion, 'padding', 'padding' );
  78. editor.commands.add( 'tableCellPadding', new TableCellPaddingCommand( editor ) );
  79. viewDoc.addStyleProcessorRules( addBackgroundRules );
  80. enableProperty( schema, conversion, 'backgroundColor', 'background-color' );
  81. editor.commands.add( 'tableCellBackgroundColor', new TableCellBackgroundColorCommand( editor ) );
  82. enableProperty( schema, conversion, 'verticalAlignment', 'vertical-align' );
  83. editor.commands.add( 'tableCellVerticalAlignment', new TableCellVerticalAlignmentCommand( editor ) );
  84. }
  85. }
  86. // Enables `'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: [ 'left', 'right', 'center', 'justify' ]
  113. },
  114. view: {
  115. // TODO: controversial one but I don't know if we want "default".
  116. left: {
  117. key: 'style',
  118. value: {
  119. 'text-align': 'left'
  120. }
  121. },
  122. right: {
  123. key: 'style',
  124. value: {
  125. 'text-align': 'right'
  126. }
  127. },
  128. center: {
  129. key: 'style',
  130. value: {
  131. 'text-align': 'center'
  132. }
  133. },
  134. justify: {
  135. key: 'style',
  136. value: {
  137. 'text-align': 'justify'
  138. }
  139. }
  140. }
  141. } );
  142. }
  143. // Enables conversion for an attribute for simple view-model mappings.
  144. //
  145. // @param {String} modelAttribute
  146. // @param {String} styleName
  147. // @param {module:engine/model/schema~Schema} schema
  148. // @param {module:engine/conversion/conversion~Conversion} conversion
  149. function enableProperty( schema, conversion, modelAttribute, styleName ) {
  150. schema.extend( 'tableCell', {
  151. allowAttributes: [ modelAttribute ]
  152. } );
  153. upcastStyleToAttribute( conversion, 'tableCell', modelAttribute, styleName );
  154. downcastAttributeToStyle( conversion, 'tableCell', modelAttribute, styleName );
  155. }