@ckeditor/ckeditor5-table 35.0.1 → 35.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,35 +7,34 @@
7
7
  * @module table/tablecolumnresize/converters
8
8
  */
9
9
 
10
- /* istanbul ignore file */
11
-
12
- import { getNumberOfColumn } from './utils';
10
+ import { normalizeColumnWidths } from './utils';
13
11
 
14
12
  /**
15
13
  * Returns a helper for converting a view `<colgroup>` and `<col>` elements to the model table `columnWidths` attribute.
16
14
  *
17
15
  * Only the inline width, provided as a percentage value, in the `<col>` element is taken into account. If there are not enough `<col>`
18
- * elements matching this condition, the special value 'auto' is returned. It indicates that the width of a column will be automatically
16
+ * elements matching this condition, the special value `auto` is returned. It indicates that the width of a column will be automatically
19
17
  * calculated in the
20
- * {@link module:table/tablecolumnresize/tablecolumnresizeediting~TableColumnResizeEditing#_setupPostFixer post-fixer}, depending
18
+ * {@link module:table/tablecolumnresize/tablecolumnresizeediting~TableColumnResizeEditing#_registerPostFixer post-fixer}, depending
21
19
  * on the available table space.
22
20
  *
23
- * @param {module:core/editor/editor~Editor} editor The editor instance.
21
+ * @param {module:core/plugin~Plugin} tableUtilsPlugin The {@link module:table/tableutils~TableUtils} plugin instance.
24
22
  * @returns {Function} Conversion helper.
25
23
  */
26
- export function upcastColgroupElement( editor ) {
24
+ export function upcastColgroupElement( tableUtilsPlugin ) {
27
25
  return dispatcher => dispatcher.on( 'element:colgroup', ( evt, data, conversionApi ) => {
28
- const modelTable = data.modelCursor.findAncestor( 'table' );
26
+ const viewColgroupElement = data.viewItem;
29
27
 
30
- if ( !modelTable ) {
28
+ if ( !conversionApi.consumable.test( viewColgroupElement, { name: true } ) ) {
31
29
  return;
32
30
  }
33
31
 
34
- const modelWriter = conversionApi.writer;
35
- const viewColgroupElement = data.viewItem;
36
- const numberOfColumns = getNumberOfColumn( modelTable, editor );
32
+ conversionApi.consumable.consume( viewColgroupElement, { name: true } );
37
33
 
38
- const columnWidthsAttribute = [ ...Array( numberOfColumns ).keys() ]
34
+ const modelTable = data.modelCursor.findAncestor( 'table' );
35
+ const numberOfColumns = tableUtilsPlugin.getColumns( modelTable );
36
+
37
+ let columnWidths = [ ...Array( numberOfColumns ).keys() ]
39
38
  .map( columnIndex => {
40
39
  const viewChild = viewColgroupElement.getChild( columnIndex );
41
40
 
@@ -50,10 +49,13 @@ export function upcastColgroupElement( editor ) {
50
49
  }
51
50
 
52
51
  return viewColWidth;
53
- } )
54
- .join( ',' );
52
+ } );
53
+
54
+ if ( columnWidths.includes( 'auto' ) ) {
55
+ columnWidths = normalizeColumnWidths( columnWidths ).map( width => width + '%' );
56
+ }
55
57
 
56
- modelWriter.setAttribute( 'columnWidths', columnWidthsAttribute, modelTable );
58
+ conversionApi.writer.setAttribute( 'columnWidths', columnWidths.join( ',' ), modelTable );
57
59
  } );
58
60
  }
59
61
 
@@ -71,11 +73,14 @@ export function downcastTableColumnWidthsAttribute() {
71
73
  .find( viewChild => viewChild.is( 'element', 'table' ) );
72
74
 
73
75
  if ( data.attributeNewValue ) {
74
- if ( data.attributeNewValue !== data.attributeOldValue ) {
75
- insertColgroupElement( viewWriter, viewTable, data.attributeNewValue );
76
- }
76
+ // If new value is the same as the old, the operation is not applied (see the `writer.setAttributeOnItem()`).
77
+ // OTOH the model element has the attribute already applied, so we can't compare the values.
78
+ // Hence we need to just recreate the <colgroup> element every time.
79
+ insertColgroupElement( viewWriter, viewTable, data.attributeNewValue );
80
+ viewWriter.addClass( 'ck-table-resized', viewTable );
77
81
  } else {
78
82
  removeColgroupElement( viewWriter, viewTable );
83
+ viewWriter.removeClass( 'ck-table-resized', viewTable );
79
84
  }
80
85
  } );
81
86
  }
@@ -86,7 +91,7 @@ export function downcastTableColumnWidthsAttribute() {
86
91
  // @private
87
92
  // @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter View writer instance.
88
93
  // @param {module:engine/view/element~Element} viewTable View table.
89
- // @param {String} columnWidthsAttribute Column width attribute from model table.
94
+ // @param {String} columnWidthsAttribute Column widths attribute from model table.
90
95
  function insertColgroupElement( viewWriter, viewTable, columnWidthsAttribute ) {
91
96
  const columnWidths = columnWidthsAttribute.split( ',' );
92
97
 
@@ -94,10 +99,10 @@ function insertColgroupElement( viewWriter, viewTable, columnWidthsAttribute ) {
94
99
 
95
100
  if ( !viewColgroupElement ) {
96
101
  viewColgroupElement = viewWriter.createContainerElement( 'colgroup' );
97
- }
98
-
99
- for ( const viewChild of [ ...viewColgroupElement.getChildren() ] ) {
100
- viewWriter.remove( viewChild );
102
+ } else {
103
+ for ( const viewChild of [ ...viewColgroupElement.getChildren() ] ) {
104
+ viewWriter.remove( viewChild );
105
+ }
101
106
  }
102
107
 
103
108
  for ( const columnIndex of Array( columnWidths.length ).keys() ) {
@@ -118,9 +123,5 @@ function insertColgroupElement( viewWriter, viewTable, columnWidthsAttribute ) {
118
123
  function removeColgroupElement( viewWriter, viewTable ) {
119
124
  const viewColgroupElement = [ ...viewTable.getChildren() ].find( viewElement => viewElement.is( 'element', 'colgroup' ) );
120
125
 
121
- if ( !viewColgroupElement ) {
122
- return;
123
- }
124
-
125
126
  viewWriter.remove( viewColgroupElement );
126
127
  }