@ckeditor/ckeditor5-table 36.0.1 → 37.0.0-alpha.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.
Files changed (166) hide show
  1. package/build/table.js +1 -1
  2. package/package.json +34 -29
  3. package/src/commands/insertcolumncommand.d.ts +61 -0
  4. package/src/commands/insertcolumncommand.js +45 -60
  5. package/src/commands/insertrowcommand.d.ts +60 -0
  6. package/src/commands/insertrowcommand.js +44 -59
  7. package/src/commands/inserttablecommand.d.ts +50 -0
  8. package/src/commands/inserttablecommand.js +51 -68
  9. package/src/commands/mergecellcommand.d.ts +76 -0
  10. package/src/commands/mergecellcommand.js +169 -244
  11. package/src/commands/mergecellscommand.d.ts +33 -0
  12. package/src/commands/mergecellscommand.js +72 -101
  13. package/src/commands/removecolumncommand.d.ts +34 -0
  14. package/src/commands/removecolumncommand.js +88 -102
  15. package/src/commands/removerowcommand.d.ts +34 -0
  16. package/src/commands/removerowcommand.js +63 -80
  17. package/src/commands/selectcolumncommand.d.ts +38 -0
  18. package/src/commands/selectcolumncommand.js +41 -54
  19. package/src/commands/selectrowcommand.d.ts +38 -0
  20. package/src/commands/selectrowcommand.js +38 -48
  21. package/src/commands/setheadercolumncommand.d.ts +55 -0
  22. package/src/commands/setheadercolumncommand.js +48 -73
  23. package/src/commands/setheaderrowcommand.d.ts +58 -0
  24. package/src/commands/setheaderrowcommand.js +56 -85
  25. package/src/commands/splitcellcommand.d.ts +49 -0
  26. package/src/commands/splitcellcommand.js +35 -49
  27. package/src/converters/downcast.d.ts +63 -0
  28. package/src/converters/downcast.js +98 -130
  29. package/src/converters/table-caption-post-fixer.d.ts +20 -0
  30. package/src/converters/table-caption-post-fixer.js +36 -52
  31. package/src/converters/table-cell-paragraph-post-fixer.d.ts +32 -0
  32. package/src/converters/table-cell-paragraph-post-fixer.js +88 -119
  33. package/src/converters/table-cell-refresh-handler.d.ts +18 -0
  34. package/src/converters/table-cell-refresh-handler.js +29 -48
  35. package/src/converters/table-headings-refresh-handler.d.ts +17 -0
  36. package/src/converters/table-headings-refresh-handler.js +35 -54
  37. package/src/converters/table-layout-post-fixer.d.ts +226 -0
  38. package/src/converters/table-layout-post-fixer.js +276 -313
  39. package/src/converters/tableproperties.d.ts +54 -0
  40. package/src/converters/tableproperties.js +136 -168
  41. package/src/converters/upcasttable.d.ts +49 -0
  42. package/src/converters/upcasttable.js +196 -251
  43. package/src/index.d.ts +29 -0
  44. package/src/index.js +0 -2
  45. package/src/plaintableoutput.d.ts +30 -0
  46. package/src/plaintableoutput.js +107 -135
  47. package/src/table.d.ts +38 -0
  48. package/src/table.js +12 -88
  49. package/src/tablecaption/tablecaptionediting.d.ts +68 -0
  50. package/src/tablecaption/tablecaptionediting.js +104 -135
  51. package/src/tablecaption/tablecaptionui.d.ts +26 -0
  52. package/src/tablecaption/tablecaptionui.js +42 -58
  53. package/src/tablecaption/toggletablecaptioncommand.d.ts +73 -0
  54. package/src/tablecaption/toggletablecaptioncommand.js +77 -92
  55. package/src/tablecaption/utils.d.ts +42 -0
  56. package/src/tablecaption/utils.js +35 -61
  57. package/src/tablecaption.d.ts +27 -0
  58. package/src/tablecaption.js +12 -19
  59. package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.d.ts +37 -0
  60. package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.js +14 -20
  61. package/src/tablecellproperties/commands/tablecellbordercolorcommand.d.ts +42 -0
  62. package/src/tablecellproperties/commands/tablecellbordercolorcommand.js +27 -37
  63. package/src/tablecellproperties/commands/tablecellborderstylecommand.d.ts +42 -0
  64. package/src/tablecellproperties/commands/tablecellborderstylecommand.js +27 -37
  65. package/src/tablecellproperties/commands/tablecellborderwidthcommand.d.ts +56 -0
  66. package/src/tablecellproperties/commands/tablecellborderwidthcommand.js +42 -53
  67. package/src/tablecellproperties/commands/tablecellheightcommand.d.ts +51 -0
  68. package/src/tablecellproperties/commands/tablecellheightcommand.js +29 -36
  69. package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.d.ts +37 -0
  70. package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.js +14 -20
  71. package/src/tablecellproperties/commands/tablecellpaddingcommand.d.ts +56 -0
  72. package/src/tablecellproperties/commands/tablecellpaddingcommand.js +42 -53
  73. package/src/tablecellproperties/commands/tablecellpropertycommand.d.ts +62 -0
  74. package/src/tablecellproperties/commands/tablecellpropertycommand.js +77 -122
  75. package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.d.ts +45 -0
  76. package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.js +14 -20
  77. package/src/tablecellproperties/tablecellpropertiesediting.d.ts +47 -0
  78. package/src/tablecellproperties/tablecellpropertiesediting.js +194 -236
  79. package/src/tablecellproperties/tablecellpropertiesui.d.ts +117 -0
  80. package/src/tablecellproperties/tablecellpropertiesui.js +303 -456
  81. package/src/tablecellproperties/ui/tablecellpropertiesview.d.ts +227 -0
  82. package/src/tablecellproperties/ui/tablecellpropertiesview.js +509 -844
  83. package/src/tablecellproperties.d.ts +33 -0
  84. package/src/tablecellproperties.js +12 -98
  85. package/src/tablecellwidth/commands/tablecellwidthcommand.d.ts +51 -0
  86. package/src/tablecellwidth/commands/tablecellwidthcommand.js +29 -35
  87. package/src/tablecellwidth/tablecellwidthediting.d.ts +34 -0
  88. package/src/tablecellwidth/tablecellwidthediting.js +26 -38
  89. package/src/tableclipboard.d.ts +68 -0
  90. package/src/tableclipboard.js +429 -568
  91. package/src/tablecolumnresize/constants.d.ts +20 -0
  92. package/src/tablecolumnresize/constants.js +0 -10
  93. package/src/tablecolumnresize/converters.d.ts +18 -0
  94. package/src/tablecolumnresize/converters.js +35 -119
  95. package/src/tablecolumnresize/tablecolumnresizeediting.d.ts +142 -0
  96. package/src/tablecolumnresize/tablecolumnresizeediting.js +545 -711
  97. package/src/tablecolumnresize/tablewidthscommand.d.ts +38 -0
  98. package/src/tablecolumnresize/tablewidthscommand.js +61 -0
  99. package/src/tablecolumnresize/utils.d.ts +141 -0
  100. package/src/tablecolumnresize/utils.js +256 -233
  101. package/src/tablecolumnresize.d.ts +29 -0
  102. package/src/tablecolumnresize.js +12 -19
  103. package/src/tableconfig.d.ts +341 -0
  104. package/src/tableconfig.js +5 -0
  105. package/src/tableediting.d.ts +102 -0
  106. package/src/tableediting.js +157 -176
  107. package/src/tablekeyboard.d.ts +68 -0
  108. package/src/tablekeyboard.js +261 -344
  109. package/src/tablemouse/mouseeventsobserver.d.ts +62 -0
  110. package/src/tablemouse/mouseeventsobserver.js +12 -49
  111. package/src/tablemouse.d.ts +51 -0
  112. package/src/tablemouse.js +154 -202
  113. package/src/tableproperties/commands/tablealignmentcommand.d.ts +37 -0
  114. package/src/tableproperties/commands/tablealignmentcommand.js +14 -20
  115. package/src/tableproperties/commands/tablebackgroundcolorcommand.d.ts +37 -0
  116. package/src/tableproperties/commands/tablebackgroundcolorcommand.js +14 -20
  117. package/src/tableproperties/commands/tablebordercolorcommand.d.ts +42 -0
  118. package/src/tableproperties/commands/tablebordercolorcommand.js +27 -37
  119. package/src/tableproperties/commands/tableborderstylecommand.d.ts +42 -0
  120. package/src/tableproperties/commands/tableborderstylecommand.js +27 -37
  121. package/src/tableproperties/commands/tableborderwidthcommand.d.ts +56 -0
  122. package/src/tableproperties/commands/tableborderwidthcommand.js +42 -53
  123. package/src/tableproperties/commands/tableheightcommand.d.ts +51 -0
  124. package/src/tableproperties/commands/tableheightcommand.js +29 -33
  125. package/src/tableproperties/commands/tablepropertycommand.d.ts +61 -0
  126. package/src/tableproperties/commands/tablepropertycommand.js +68 -112
  127. package/src/tableproperties/commands/tablewidthcommand.d.ts +51 -0
  128. package/src/tableproperties/commands/tablewidthcommand.js +29 -33
  129. package/src/tableproperties/tablepropertiesediting.d.ts +45 -0
  130. package/src/tableproperties/tablepropertiesediting.js +164 -210
  131. package/src/tableproperties/tablepropertiesui.d.ts +119 -0
  132. package/src/tableproperties/tablepropertiesui.js +294 -439
  133. package/src/tableproperties/ui/tablepropertiesview.d.ts +203 -0
  134. package/src/tableproperties/ui/tablepropertiesview.js +427 -718
  135. package/src/tableproperties.d.ts +33 -0
  136. package/src/tableproperties.js +12 -95
  137. package/src/tableselection.d.ts +111 -0
  138. package/src/tableselection.js +279 -376
  139. package/src/tabletoolbar.d.ts +37 -0
  140. package/src/tabletoolbar.js +39 -92
  141. package/src/tableui.d.ts +58 -0
  142. package/src/tableui.js +281 -338
  143. package/src/tableutils.d.ts +453 -0
  144. package/src/tableutils.js +1015 -1229
  145. package/src/tablewalker.d.ts +323 -0
  146. package/src/tablewalker.js +308 -548
  147. package/src/ui/colorinputview.d.ts +143 -0
  148. package/src/ui/colorinputview.js +229 -366
  149. package/src/ui/formrowview.d.ts +61 -0
  150. package/src/ui/formrowview.js +38 -84
  151. package/src/ui/inserttableview.d.ts +77 -0
  152. package/src/ui/inserttableview.js +152 -242
  153. package/src/utils/common.d.ts +42 -0
  154. package/src/utils/common.js +33 -57
  155. package/src/utils/structure.d.ts +245 -0
  156. package/src/utils/structure.js +261 -379
  157. package/src/utils/table-properties.d.ts +67 -0
  158. package/src/utils/table-properties.js +60 -81
  159. package/src/utils/ui/contextualballoon.d.ts +34 -0
  160. package/src/utils/ui/contextualballoon.js +70 -89
  161. package/src/utils/ui/table-properties.d.ts +193 -0
  162. package/src/utils/ui/table-properties.js +259 -319
  163. package/src/utils/ui/widget.d.ts +16 -0
  164. package/src/utils/ui/widget.js +24 -46
  165. package/src/tablecolumnresize/tablecolumnwidthscommand.js +0 -55
  166. package/src/tablecolumnresize/tablewidthresizecommand.js +0 -65
@@ -2,16 +2,10 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module table/commands/mergecellscommand
8
- */
9
-
10
5
  import { Command } from 'ckeditor5/src/core';
11
6
  import TableUtils from '../tableutils';
12
7
  import { updateNumericAttribute } from '../utils/common';
13
8
  import { removeEmptyRowsColumns } from '../utils/structure';
14
-
15
9
  /**
16
10
  * The merge cells command.
17
11
  *
@@ -19,105 +13,82 @@ import { removeEmptyRowsColumns } from '../utils/structure';
19
13
  *
20
14
  * For example, to merge selected table cells:
21
15
  *
22
- * editor.execute( 'mergeTableCells' );
23
- *
24
- * @extends module:core/command~Command
16
+ * ```ts
17
+ * editor.execute( 'mergeTableCells' );
18
+ * ```
25
19
  */
26
20
  export default class MergeCellsCommand extends Command {
27
- /**
28
- * @inheritDoc
29
- */
30
- refresh() {
31
- const tableUtils = this.editor.plugins.get( TableUtils );
32
-
33
- const selectedTableCells = tableUtils.getSelectedTableCells( this.editor.model.document.selection );
34
- this.isEnabled = tableUtils.isSelectionRectangular( selectedTableCells, this.editor.plugins.get( TableUtils ) );
35
- }
36
-
37
- /**
38
- * Executes the command.
39
- *
40
- * @fires execute
41
- */
42
- execute() {
43
- const model = this.editor.model;
44
- const tableUtils = this.editor.plugins.get( TableUtils );
45
-
46
- model.change( writer => {
47
- const selectedTableCells = tableUtils.getSelectedTableCells( model.document.selection );
48
-
49
- // All cells will be merged into the first one.
50
- const firstTableCell = selectedTableCells.shift();
51
-
52
- // Update target cell dimensions.
53
- const { mergeWidth, mergeHeight } = getMergeDimensions( firstTableCell, selectedTableCells, tableUtils );
54
- updateNumericAttribute( 'colspan', mergeWidth, firstTableCell, writer );
55
- updateNumericAttribute( 'rowspan', mergeHeight, firstTableCell, writer );
56
-
57
- for ( const tableCell of selectedTableCells ) {
58
- mergeTableCells( tableCell, firstTableCell, writer );
59
- }
60
-
61
- const table = firstTableCell.findAncestor( 'table' );
62
-
63
- // Remove rows and columns that become empty (have no anchored cells).
64
- removeEmptyRowsColumns( table, tableUtils );
65
-
66
- writer.setSelection( firstTableCell, 'in' );
67
- } );
68
- }
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ refresh() {
25
+ const tableUtils = this.editor.plugins.get(TableUtils);
26
+ const selectedTableCells = tableUtils.getSelectedTableCells(this.editor.model.document.selection);
27
+ this.isEnabled = tableUtils.isSelectionRectangular(selectedTableCells);
28
+ }
29
+ /**
30
+ * Executes the command.
31
+ *
32
+ * @fires execute
33
+ */
34
+ execute() {
35
+ const model = this.editor.model;
36
+ const tableUtils = this.editor.plugins.get(TableUtils);
37
+ model.change(writer => {
38
+ const selectedTableCells = tableUtils.getSelectedTableCells(model.document.selection);
39
+ // All cells will be merged into the first one.
40
+ const firstTableCell = selectedTableCells.shift();
41
+ // Update target cell dimensions.
42
+ const { mergeWidth, mergeHeight } = getMergeDimensions(firstTableCell, selectedTableCells, tableUtils);
43
+ updateNumericAttribute('colspan', mergeWidth, firstTableCell, writer);
44
+ updateNumericAttribute('rowspan', mergeHeight, firstTableCell, writer);
45
+ for (const tableCell of selectedTableCells) {
46
+ mergeTableCells(tableCell, firstTableCell, writer);
47
+ }
48
+ const table = firstTableCell.findAncestor('table');
49
+ // Remove rows and columns that become empty (have no anchored cells).
50
+ removeEmptyRowsColumns(table, tableUtils);
51
+ writer.setSelection(firstTableCell, 'in');
52
+ });
53
+ }
69
54
  }
70
-
71
- // Merges two table cells. It will ensure that after merging cells with empty paragraphs the resulting table cell will only have one
72
- // paragraph. If one of the merged table cells is empty, the merged table cell will have contents of the non-empty table cell.
73
- // If both are empty, the merged table cell will have only one empty paragraph.
74
- //
75
- // @param {module:engine/model/element~Element} cellBeingMerged
76
- // @param {module:engine/model/element~Element} targetCell
77
- // @param {module:engine/model/writer~Writer} writer
78
- function mergeTableCells( cellBeingMerged, targetCell, writer ) {
79
- if ( !isEmpty( cellBeingMerged ) ) {
80
- if ( isEmpty( targetCell ) ) {
81
- writer.remove( writer.createRangeIn( targetCell ) );
82
- }
83
-
84
- writer.move( writer.createRangeIn( cellBeingMerged ), writer.createPositionAt( targetCell, 'end' ) );
85
- }
86
-
87
- // Remove merged table cell.
88
- writer.remove( cellBeingMerged );
55
+ /**
56
+ * Merges two table cells. It will ensure that after merging cells with empty paragraphs the resulting table cell will only have one
57
+ * paragraph. If one of the merged table cells is empty, the merged table cell will have contents of the non-empty table cell.
58
+ * If both are empty, the merged table cell will have only one empty paragraph.
59
+ */
60
+ function mergeTableCells(cellBeingMerged, targetCell, writer) {
61
+ if (!isEmpty(cellBeingMerged)) {
62
+ if (isEmpty(targetCell)) {
63
+ writer.remove(writer.createRangeIn(targetCell));
64
+ }
65
+ writer.move(writer.createRangeIn(cellBeingMerged), writer.createPositionAt(targetCell, 'end'));
66
+ }
67
+ // Remove merged table cell.
68
+ writer.remove(cellBeingMerged);
89
69
  }
90
-
91
- // Checks if the passed table cell contains an empty paragraph.
92
- //
93
- // @param {module:engine/model/element~Element} tableCell
94
- // @returns {Boolean}
95
- function isEmpty( tableCell ) {
96
- return tableCell.childCount == 1 && tableCell.getChild( 0 ).is( 'element', 'paragraph' ) && tableCell.getChild( 0 ).isEmpty;
70
+ /**
71
+ * Checks if the passed table cell contains an empty paragraph.
72
+ */
73
+ function isEmpty(tableCell) {
74
+ const firstTableChild = tableCell.getChild(0);
75
+ return tableCell.childCount == 1 && firstTableChild.is('element', 'paragraph') && firstTableChild.isEmpty;
97
76
  }
98
-
99
- function getMergeDimensions( firstTableCell, selectedTableCells, tableUtils ) {
100
- let maxWidthOffset = 0;
101
- let maxHeightOffset = 0;
102
-
103
- for ( const tableCell of selectedTableCells ) {
104
- const { row, column } = tableUtils.getCellLocation( tableCell );
105
-
106
- maxWidthOffset = getMaxOffset( tableCell, column, maxWidthOffset, 'colspan' );
107
- maxHeightOffset = getMaxOffset( tableCell, row, maxHeightOffset, 'rowspan' );
108
- }
109
-
110
- // Update table cell span attribute and merge set selection on a merged contents.
111
- const { row: firstCellRow, column: firstCellColumn } = tableUtils.getCellLocation( firstTableCell );
112
-
113
- const mergeWidth = maxWidthOffset - firstCellColumn;
114
- const mergeHeight = maxHeightOffset - firstCellRow;
115
-
116
- return { mergeWidth, mergeHeight };
77
+ function getMergeDimensions(firstTableCell, selectedTableCells, tableUtils) {
78
+ let maxWidthOffset = 0;
79
+ let maxHeightOffset = 0;
80
+ for (const tableCell of selectedTableCells) {
81
+ const { row, column } = tableUtils.getCellLocation(tableCell);
82
+ maxWidthOffset = getMaxOffset(tableCell, column, maxWidthOffset, 'colspan');
83
+ maxHeightOffset = getMaxOffset(tableCell, row, maxHeightOffset, 'rowspan');
84
+ }
85
+ // Update table cell span attribute and merge set selection on a merged contents.
86
+ const { row: firstCellRow, column: firstCellColumn } = tableUtils.getCellLocation(firstTableCell);
87
+ const mergeWidth = maxWidthOffset - firstCellColumn;
88
+ const mergeHeight = maxHeightOffset - firstCellRow;
89
+ return { mergeWidth, mergeHeight };
117
90
  }
118
-
119
- function getMaxOffset( tableCell, start, currentMaxOffset, which ) {
120
- const dimensionValue = parseInt( tableCell.getAttribute( which ) || 1 );
121
-
122
- return Math.max( currentMaxOffset, start + dimensionValue );
91
+ function getMaxOffset(tableCell, start, currentMaxOffset, which) {
92
+ const dimensionValue = parseInt(tableCell.getAttribute(which) || '1');
93
+ return Math.max(currentMaxOffset, start + dimensionValue);
123
94
  }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module table/commands/removecolumncommand
7
+ */
8
+ import { Command } from 'ckeditor5/src/core';
9
+ /**
10
+ * The remove column command.
11
+ *
12
+ * The command is registered by {@link module:table/tableediting~TableEditing} as the `'removeTableColumn'` editor command.
13
+ *
14
+ * To remove the column containing the selected cell, execute the command:
15
+ *
16
+ * ```ts
17
+ * editor.execute( 'removeTableColumn' );
18
+ * ```
19
+ */
20
+ export default class RemoveColumnCommand extends Command {
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ refresh(): void;
25
+ /**
26
+ * @inheritDoc
27
+ */
28
+ execute(): void;
29
+ }
30
+ declare module '@ckeditor/ckeditor5-core' {
31
+ interface CommandsMap {
32
+ removeTableColumn: RemoveColumnCommand;
33
+ }
34
+ }
@@ -2,15 +2,11 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module table/commands/removecolumncommand
8
7
  */
9
-
10
8
  import { Command } from 'ckeditor5/src/core';
11
-
12
9
  import TableWalker from '../tablewalker';
13
-
14
10
  /**
15
11
  * The remove column command.
16
12
  *
@@ -18,106 +14,96 @@ import TableWalker from '../tablewalker';
18
14
  *
19
15
  * To remove the column containing the selected cell, execute the command:
20
16
  *
21
- * editor.execute( 'removeTableColumn' );
22
- *
23
- * @extends module:core/command~Command
17
+ * ```ts
18
+ * editor.execute( 'removeTableColumn' );
19
+ * ```
24
20
  */
25
21
  export default class RemoveColumnCommand extends Command {
26
- /**
27
- * @inheritDoc
28
- */
29
- refresh() {
30
- const tableUtils = this.editor.plugins.get( 'TableUtils' );
31
- const selectedCells = tableUtils.getSelectionAffectedTableCells( this.editor.model.document.selection );
32
- const firstCell = selectedCells[ 0 ];
33
-
34
- if ( firstCell ) {
35
- const table = firstCell.findAncestor( 'table' );
36
- const tableColumnCount = tableUtils.getColumns( table );
37
-
38
- const { first, last } = tableUtils.getColumnIndexes( selectedCells );
39
-
40
- this.isEnabled = last - first < ( tableColumnCount - 1 );
41
- } else {
42
- this.isEnabled = false;
43
- }
44
- }
45
-
46
- /**
47
- * @inheritDoc
48
- */
49
- execute() {
50
- const tableUtils = this.editor.plugins.get( 'TableUtils' );
51
- const [ firstCell, lastCell ] = getBoundaryCells( this.editor.model.document.selection, tableUtils );
52
- const table = firstCell.parent.parent;
53
-
54
- // Cache the table before removing or updating colspans.
55
- const tableMap = [ ...new TableWalker( table ) ];
56
-
57
- // Store column indexes of removed columns.
58
- const removedColumnIndexes = {
59
- first: tableMap.find( value => value.cell === firstCell ).column,
60
- last: tableMap.find( value => value.cell === lastCell ).column
61
- };
62
-
63
- const cellToFocus = getCellToFocus( tableMap, firstCell, lastCell, removedColumnIndexes );
64
-
65
- this.editor.model.change( writer => {
66
- const columnsToRemove = removedColumnIndexes.last - removedColumnIndexes.first + 1;
67
-
68
- this.editor.plugins.get( 'TableUtils' ).removeColumns( table, {
69
- at: removedColumnIndexes.first,
70
- columns: columnsToRemove
71
- } );
72
-
73
- writer.setSelection( writer.createPositionAt( cellToFocus, 0 ) );
74
- } );
75
- }
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ refresh() {
26
+ const tableUtils = this.editor.plugins.get('TableUtils');
27
+ const selectedCells = tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection);
28
+ const firstCell = selectedCells[0];
29
+ if (firstCell) {
30
+ const table = firstCell.findAncestor('table');
31
+ const tableColumnCount = tableUtils.getColumns(table);
32
+ const { first, last } = tableUtils.getColumnIndexes(selectedCells);
33
+ this.isEnabled = last - first < (tableColumnCount - 1);
34
+ }
35
+ else {
36
+ this.isEnabled = false;
37
+ }
38
+ }
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ execute() {
43
+ const tableUtils = this.editor.plugins.get('TableUtils');
44
+ const [firstCell, lastCell] = getBoundaryCells(this.editor.model.document.selection, tableUtils);
45
+ const table = firstCell.parent.parent;
46
+ // Cache the table before removing or updating colspans.
47
+ const tableMap = [...new TableWalker(table)];
48
+ // Store column indexes of removed columns.
49
+ const removedColumnIndexes = {
50
+ first: tableMap.find(value => value.cell === firstCell).column,
51
+ last: tableMap.find(value => value.cell === lastCell).column
52
+ };
53
+ const cellToFocus = getCellToFocus(tableMap, firstCell, lastCell, removedColumnIndexes);
54
+ this.editor.model.change(writer => {
55
+ const columnsToRemove = removedColumnIndexes.last - removedColumnIndexes.first + 1;
56
+ tableUtils.removeColumns(table, {
57
+ at: removedColumnIndexes.first,
58
+ columns: columnsToRemove
59
+ });
60
+ writer.setSelection(writer.createPositionAt(cellToFocus, 0));
61
+ });
62
+ }
76
63
  }
77
-
78
- // Returns a proper table cell to focus after removing a column.
79
- // - selection is on last table cell it will return previous cell.
80
- function getCellToFocus( tableMap, firstCell, lastCell, removedColumnIndexes ) {
81
- const colspan = parseInt( lastCell.getAttribute( 'colspan' ) || 1 );
82
-
83
- // If the table cell is spanned over 2+ columns - it will be truncated so the selection should
84
- // stay in that cell.
85
- if ( colspan > 1 ) {
86
- return lastCell;
87
- }
88
- // Normally, look for the cell in the same row that precedes the first cell to put selection there ("column on the left").
89
- // If the deleted column is the first column of the table, there will be no predecessor: use the cell
90
- // from the column that follows then (also in the same row).
91
- else if ( firstCell.previousSibling || lastCell.nextSibling ) {
92
- return lastCell.nextSibling || firstCell.previousSibling;
93
- }
94
- // It can happen that table cells have no siblings in a row, for instance, when there are row spans
95
- // in the table (in the previous row). Then just look for the closest cell that is in a column
96
- // that will not be removed to put the selection there.
97
- else {
98
- // Look for any cell in a column that precedes the first removed column.
99
- if ( removedColumnIndexes.first ) {
100
- return tableMap.reverse().find( ( { column } ) => {
101
- return column < removedColumnIndexes.first;
102
- } ).cell;
103
- }
104
- // If the first removed column is the first column of the table, then
105
- // look for any cell that is in a column that follows the last removed column.
106
- else {
107
- return tableMap.reverse().find( ( { column } ) => {
108
- return column > removedColumnIndexes.last;
109
- } ).cell;
110
- }
111
- }
64
+ /**
65
+ * Returns a proper table cell to focus after removing a column.
66
+ * - selection is on last table cell it will return previous cell.
67
+ */
68
+ function getCellToFocus(tableMap, firstCell, lastCell, removedColumnIndexes) {
69
+ const colspan = parseInt(lastCell.getAttribute('colspan') || '1');
70
+ // If the table cell is spanned over 2+ columns - it will be truncated so the selection should
71
+ // stay in that cell.
72
+ if (colspan > 1) {
73
+ return lastCell;
74
+ }
75
+ // Normally, look for the cell in the same row that precedes the first cell to put selection there ("column on the left").
76
+ // If the deleted column is the first column of the table, there will be no predecessor: use the cell
77
+ // from the column that follows then (also in the same row).
78
+ else if (firstCell.previousSibling || lastCell.nextSibling) {
79
+ return lastCell.nextSibling || firstCell.previousSibling;
80
+ }
81
+ // It can happen that table cells have no siblings in a row, for instance, when there are row spans
82
+ // in the table (in the previous row). Then just look for the closest cell that is in a column
83
+ // that will not be removed to put the selection there.
84
+ else {
85
+ // Look for any cell in a column that precedes the first removed column.
86
+ if (removedColumnIndexes.first) {
87
+ return tableMap.reverse().find(({ column }) => {
88
+ return column < removedColumnIndexes.first;
89
+ }).cell;
90
+ }
91
+ // If the first removed column is the first column of the table, then
92
+ // look for any cell that is in a column that follows the last removed column.
93
+ else {
94
+ return tableMap.reverse().find(({ column }) => {
95
+ return column > removedColumnIndexes.last;
96
+ }).cell;
97
+ }
98
+ }
112
99
  }
113
-
114
- // Returns helper object returning the first and the last cell contained in given selection, based on DOM order.
115
- function getBoundaryCells( selection, tableUtils ) {
116
- const referenceCells = tableUtils.getSelectionAffectedTableCells( selection );
117
- const firstCell = referenceCells[ 0 ];
118
- const lastCell = referenceCells.pop();
119
-
120
- const returnValue = [ firstCell, lastCell ];
121
-
122
- return firstCell.isBefore( lastCell ) ? returnValue : returnValue.reverse();
100
+ /**
101
+ * Returns helper object returning the first and the last cell contained in given selection, based on DOM order.
102
+ */
103
+ function getBoundaryCells(selection, tableUtils) {
104
+ const referenceCells = tableUtils.getSelectionAffectedTableCells(selection);
105
+ const firstCell = referenceCells[0];
106
+ const lastCell = referenceCells.pop();
107
+ const returnValue = [firstCell, lastCell];
108
+ return firstCell.isBefore(lastCell) ? returnValue : returnValue.reverse();
123
109
  }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module table/commands/removerowcommand
7
+ */
8
+ import { Command } from 'ckeditor5/src/core';
9
+ /**
10
+ * The remove row command.
11
+ *
12
+ * The command is registered by {@link module:table/tableediting~TableEditing} as the `'removeTableRow'` editor command.
13
+ *
14
+ * To remove the row containing the selected cell, execute the command:
15
+ *
16
+ * ```ts
17
+ * editor.execute( 'removeTableRow' );
18
+ * ```
19
+ */
20
+ export default class RemoveRowCommand extends Command {
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ refresh(): void;
25
+ /**
26
+ * @inheritDoc
27
+ */
28
+ execute(): void;
29
+ }
30
+ declare module '@ckeditor/ckeditor5-core' {
31
+ interface CommandsMap {
32
+ removeTableRow: RemoveRowCommand;
33
+ }
34
+ }
@@ -2,13 +2,10 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module table/commands/removerowcommand
8
7
  */
9
-
10
8
  import { Command } from 'ckeditor5/src/core';
11
-
12
9
  /**
13
10
  * The remove row command.
14
11
  *
@@ -16,84 +13,70 @@ import { Command } from 'ckeditor5/src/core';
16
13
  *
17
14
  * To remove the row containing the selected cell, execute the command:
18
15
  *
19
- * editor.execute( 'removeTableRow' );
20
- *
21
- * @extends module:core/command~Command
16
+ * ```ts
17
+ * editor.execute( 'removeTableRow' );
18
+ * ```
22
19
  */
23
20
  export default class RemoveRowCommand extends Command {
24
- /**
25
- * @inheritDoc
26
- */
27
- refresh() {
28
- const tableUtils = this.editor.plugins.get( 'TableUtils' );
29
- const selectedCells = tableUtils.getSelectionAffectedTableCells( this.editor.model.document.selection );
30
- const firstCell = selectedCells[ 0 ];
31
-
32
- if ( firstCell ) {
33
- const table = firstCell.findAncestor( 'table' );
34
- const tableRowCount = this.editor.plugins.get( 'TableUtils' ).getRows( table );
35
- const lastRowIndex = tableRowCount - 1;
36
-
37
- const selectedRowIndexes = tableUtils.getRowIndexes( selectedCells );
38
-
39
- const areAllRowsSelected = selectedRowIndexes.first === 0 && selectedRowIndexes.last === lastRowIndex;
40
-
41
- // Disallow selecting whole table -> delete whole table should be used instead.
42
- this.isEnabled = !areAllRowsSelected;
43
- } else {
44
- this.isEnabled = false;
45
- }
46
- }
47
-
48
- /**
49
- * @inheritDoc
50
- */
51
- execute() {
52
- const model = this.editor.model;
53
- const tableUtils = this.editor.plugins.get( 'TableUtils' );
54
-
55
- const referenceCells = tableUtils.getSelectionAffectedTableCells( model.document.selection );
56
- const removedRowIndexes = tableUtils.getRowIndexes( referenceCells );
57
-
58
- const firstCell = referenceCells[ 0 ];
59
- const table = firstCell.findAncestor( 'table' );
60
-
61
- const columnIndexToFocus = tableUtils.getCellLocation( firstCell ).column;
62
-
63
- model.change( writer => {
64
- const rowsToRemove = removedRowIndexes.last - removedRowIndexes.first + 1;
65
-
66
- tableUtils.removeRows( table, {
67
- at: removedRowIndexes.first,
68
- rows: rowsToRemove
69
- } );
70
-
71
- const cellToFocus = getCellToFocus( table, removedRowIndexes.first, columnIndexToFocus, tableUtils.getRows( table ) );
72
-
73
- writer.setSelection( writer.createPositionAt( cellToFocus, 0 ) );
74
- } );
75
- }
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ refresh() {
25
+ const tableUtils = this.editor.plugins.get('TableUtils');
26
+ const selectedCells = tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection);
27
+ const firstCell = selectedCells[0];
28
+ if (firstCell) {
29
+ const table = firstCell.findAncestor('table');
30
+ const tableRowCount = tableUtils.getRows(table);
31
+ const lastRowIndex = tableRowCount - 1;
32
+ const selectedRowIndexes = tableUtils.getRowIndexes(selectedCells);
33
+ const areAllRowsSelected = selectedRowIndexes.first === 0 && selectedRowIndexes.last === lastRowIndex;
34
+ // Disallow selecting whole table -> delete whole table should be used instead.
35
+ this.isEnabled = !areAllRowsSelected;
36
+ }
37
+ else {
38
+ this.isEnabled = false;
39
+ }
40
+ }
41
+ /**
42
+ * @inheritDoc
43
+ */
44
+ execute() {
45
+ const model = this.editor.model;
46
+ const tableUtils = this.editor.plugins.get('TableUtils');
47
+ const referenceCells = tableUtils.getSelectionAffectedTableCells(model.document.selection);
48
+ const removedRowIndexes = tableUtils.getRowIndexes(referenceCells);
49
+ const firstCell = referenceCells[0];
50
+ const table = firstCell.findAncestor('table');
51
+ const columnIndexToFocus = tableUtils.getCellLocation(firstCell).column;
52
+ model.change(writer => {
53
+ const rowsToRemove = removedRowIndexes.last - removedRowIndexes.first + 1;
54
+ tableUtils.removeRows(table, {
55
+ at: removedRowIndexes.first,
56
+ rows: rowsToRemove
57
+ });
58
+ const cellToFocus = getCellToFocus(table, removedRowIndexes.first, columnIndexToFocus, tableUtils.getRows(table));
59
+ writer.setSelection(writer.createPositionAt(cellToFocus, 0));
60
+ });
61
+ }
76
62
  }
77
-
78
- // Returns a cell that should be focused before removing the row, belonging to the same column as the currently focused cell.
79
- // * If the row was not the last one, the cell to focus will be in the row that followed it (before removal).
80
- // * If the row was the last one, the cell to focus will be in the row that preceded it (before removal).
81
- function getCellToFocus( table, removedRowIndex, columnToFocus, tableRowCount ) {
82
- // Don't go beyond last row's index.
83
- const row = table.getChild( Math.min( removedRowIndex, tableRowCount - 1 ) );
84
-
85
- // Default to first table cell.
86
- let cellToFocus = row.getChild( 0 );
87
- let column = 0;
88
-
89
- for ( const tableCell of row.getChildren() ) {
90
- if ( column > columnToFocus ) {
91
- return cellToFocus;
92
- }
93
-
94
- cellToFocus = tableCell;
95
- column += parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
96
- }
97
-
98
- return cellToFocus;
63
+ /**
64
+ * Returns a cell that should be focused before removing the row, belonging to the same column as the currently focused cell.
65
+ * - If the row was not the last one, the cell to focus will be in the row that followed it (before removal).
66
+ * - If the row was the last one, the cell to focus will be in the row that preceded it (before removal).
67
+ */
68
+ function getCellToFocus(table, removedRowIndex, columnToFocus, tableRowCount) {
69
+ // Don't go beyond last row's index.
70
+ const row = table.getChild(Math.min(removedRowIndex, tableRowCount - 1));
71
+ // Default to first table cell.
72
+ let cellToFocus = row.getChild(0);
73
+ let column = 0;
74
+ for (const tableCell of row.getChildren()) {
75
+ if (column > columnToFocus) {
76
+ return cellToFocus;
77
+ }
78
+ cellToFocus = tableCell;
79
+ column += parseInt(tableCell.getAttribute('colspan') || '1');
80
+ }
81
+ return cellToFocus;
99
82
  }