@ckeditor/ckeditor5-table 36.0.1 → 37.0.0-alpha.1

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 (170) hide show
  1. package/README.md +1 -1
  2. package/build/table.js +1 -1
  3. package/ckeditor5-metadata.json +6 -6
  4. package/package.json +34 -29
  5. package/src/augmentation.d.ts +76 -0
  6. package/src/augmentation.js +5 -0
  7. package/src/commands/insertcolumncommand.d.ts +55 -0
  8. package/src/commands/insertcolumncommand.js +45 -60
  9. package/src/commands/insertrowcommand.d.ts +54 -0
  10. package/src/commands/insertrowcommand.js +44 -59
  11. package/src/commands/inserttablecommand.d.ts +44 -0
  12. package/src/commands/inserttablecommand.js +50 -68
  13. package/src/commands/mergecellcommand.d.ts +68 -0
  14. package/src/commands/mergecellcommand.js +169 -244
  15. package/src/commands/mergecellscommand.d.ts +28 -0
  16. package/src/commands/mergecellscommand.js +72 -101
  17. package/src/commands/removecolumncommand.d.ts +29 -0
  18. package/src/commands/removecolumncommand.js +88 -102
  19. package/src/commands/removerowcommand.d.ts +29 -0
  20. package/src/commands/removerowcommand.js +63 -80
  21. package/src/commands/selectcolumncommand.d.ts +33 -0
  22. package/src/commands/selectcolumncommand.js +41 -54
  23. package/src/commands/selectrowcommand.d.ts +33 -0
  24. package/src/commands/selectrowcommand.js +38 -48
  25. package/src/commands/setheadercolumncommand.d.ts +50 -0
  26. package/src/commands/setheadercolumncommand.js +48 -73
  27. package/src/commands/setheaderrowcommand.d.ts +53 -0
  28. package/src/commands/setheaderrowcommand.js +56 -85
  29. package/src/commands/splitcellcommand.d.ts +43 -0
  30. package/src/commands/splitcellcommand.js +35 -49
  31. package/src/converters/downcast.d.ts +63 -0
  32. package/src/converters/downcast.js +98 -130
  33. package/src/converters/table-caption-post-fixer.d.ts +20 -0
  34. package/src/converters/table-caption-post-fixer.js +36 -52
  35. package/src/converters/table-cell-paragraph-post-fixer.d.ts +32 -0
  36. package/src/converters/table-cell-paragraph-post-fixer.js +88 -119
  37. package/src/converters/table-cell-refresh-handler.d.ts +18 -0
  38. package/src/converters/table-cell-refresh-handler.js +29 -48
  39. package/src/converters/table-headings-refresh-handler.d.ts +17 -0
  40. package/src/converters/table-headings-refresh-handler.js +35 -54
  41. package/src/converters/table-layout-post-fixer.d.ts +226 -0
  42. package/src/converters/table-layout-post-fixer.js +276 -313
  43. package/src/converters/tableproperties.d.ts +54 -0
  44. package/src/converters/tableproperties.js +136 -168
  45. package/src/converters/upcasttable.d.ts +49 -0
  46. package/src/converters/upcasttable.js +199 -251
  47. package/src/index.d.ts +60 -0
  48. package/src/index.js +1 -2
  49. package/src/plaintableoutput.d.ts +25 -0
  50. package/src/plaintableoutput.js +107 -135
  51. package/src/table.d.ts +33 -0
  52. package/src/table.js +12 -88
  53. package/src/tablecaption/tablecaptionediting.d.ts +63 -0
  54. package/src/tablecaption/tablecaptionediting.js +104 -135
  55. package/src/tablecaption/tablecaptionui.d.ts +21 -0
  56. package/src/tablecaption/tablecaptionui.js +42 -58
  57. package/src/tablecaption/toggletablecaptioncommand.d.ts +68 -0
  58. package/src/tablecaption/toggletablecaptioncommand.js +77 -92
  59. package/src/tablecaption/utils.d.ts +42 -0
  60. package/src/tablecaption/utils.js +35 -61
  61. package/src/tablecaption.d.ts +22 -0
  62. package/src/tablecaption.js +12 -19
  63. package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.d.ts +32 -0
  64. package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.js +14 -20
  65. package/src/tablecellproperties/commands/tablecellbordercolorcommand.d.ts +37 -0
  66. package/src/tablecellproperties/commands/tablecellbordercolorcommand.js +27 -37
  67. package/src/tablecellproperties/commands/tablecellborderstylecommand.d.ts +37 -0
  68. package/src/tablecellproperties/commands/tablecellborderstylecommand.js +27 -37
  69. package/src/tablecellproperties/commands/tablecellborderwidthcommand.d.ts +51 -0
  70. package/src/tablecellproperties/commands/tablecellborderwidthcommand.js +42 -53
  71. package/src/tablecellproperties/commands/tablecellheightcommand.d.ts +46 -0
  72. package/src/tablecellproperties/commands/tablecellheightcommand.js +29 -36
  73. package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.d.ts +32 -0
  74. package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.js +14 -20
  75. package/src/tablecellproperties/commands/tablecellpaddingcommand.d.ts +51 -0
  76. package/src/tablecellproperties/commands/tablecellpaddingcommand.js +42 -53
  77. package/src/tablecellproperties/commands/tablecellpropertycommand.d.ts +62 -0
  78. package/src/tablecellproperties/commands/tablecellpropertycommand.js +77 -122
  79. package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.d.ts +40 -0
  80. package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.js +14 -20
  81. package/src/tablecellproperties/tablecellpropertiesediting.d.ts +41 -0
  82. package/src/tablecellproperties/tablecellpropertiesediting.js +193 -236
  83. package/src/tablecellproperties/tablecellpropertiesui.d.ts +111 -0
  84. package/src/tablecellproperties/tablecellpropertiesui.js +302 -456
  85. package/src/tablecellproperties/ui/tablecellpropertiesview.d.ts +227 -0
  86. package/src/tablecellproperties/ui/tablecellpropertiesview.js +509 -844
  87. package/src/tablecellproperties.d.ts +28 -0
  88. package/src/tablecellproperties.js +12 -98
  89. package/src/tablecellwidth/commands/tablecellwidthcommand.d.ts +46 -0
  90. package/src/tablecellwidth/commands/tablecellwidthcommand.js +29 -35
  91. package/src/tablecellwidth/tablecellwidthediting.d.ts +28 -0
  92. package/src/tablecellwidth/tablecellwidthediting.js +25 -38
  93. package/src/tableclipboard.d.ts +63 -0
  94. package/src/tableclipboard.js +429 -568
  95. package/src/tablecolumnresize/constants.d.ts +20 -0
  96. package/src/tablecolumnresize/constants.js +0 -10
  97. package/src/tablecolumnresize/converters.d.ts +18 -0
  98. package/src/tablecolumnresize/converters.js +35 -119
  99. package/src/tablecolumnresize/tablecolumnresizeediting.d.ts +137 -0
  100. package/src/tablecolumnresize/tablecolumnresizeediting.js +545 -711
  101. package/src/tablecolumnresize/tablewidthscommand.d.ts +38 -0
  102. package/src/tablecolumnresize/tablewidthscommand.js +61 -0
  103. package/src/tablecolumnresize/utils.d.ts +141 -0
  104. package/src/tablecolumnresize/utils.js +256 -233
  105. package/src/tablecolumnresize.d.ts +24 -0
  106. package/src/tablecolumnresize.js +12 -19
  107. package/src/tableconfig.d.ts +331 -0
  108. package/src/tableconfig.js +5 -0
  109. package/src/tableediting.d.ts +97 -0
  110. package/src/tableediting.js +157 -176
  111. package/src/tablekeyboard.d.ts +63 -0
  112. package/src/tablekeyboard.js +261 -344
  113. package/src/tablemouse/mouseeventsobserver.d.ts +62 -0
  114. package/src/tablemouse/mouseeventsobserver.js +13 -50
  115. package/src/tablemouse.d.ts +46 -0
  116. package/src/tablemouse.js +154 -202
  117. package/src/tableproperties/commands/tablealignmentcommand.d.ts +32 -0
  118. package/src/tableproperties/commands/tablealignmentcommand.js +14 -20
  119. package/src/tableproperties/commands/tablebackgroundcolorcommand.d.ts +32 -0
  120. package/src/tableproperties/commands/tablebackgroundcolorcommand.js +14 -20
  121. package/src/tableproperties/commands/tablebordercolorcommand.d.ts +37 -0
  122. package/src/tableproperties/commands/tablebordercolorcommand.js +27 -37
  123. package/src/tableproperties/commands/tableborderstylecommand.d.ts +37 -0
  124. package/src/tableproperties/commands/tableborderstylecommand.js +27 -37
  125. package/src/tableproperties/commands/tableborderwidthcommand.d.ts +51 -0
  126. package/src/tableproperties/commands/tableborderwidthcommand.js +42 -53
  127. package/src/tableproperties/commands/tableheightcommand.d.ts +46 -0
  128. package/src/tableproperties/commands/tableheightcommand.js +29 -33
  129. package/src/tableproperties/commands/tablepropertycommand.d.ts +61 -0
  130. package/src/tableproperties/commands/tablepropertycommand.js +68 -112
  131. package/src/tableproperties/commands/tablewidthcommand.d.ts +46 -0
  132. package/src/tableproperties/commands/tablewidthcommand.js +29 -33
  133. package/src/tableproperties/tablepropertiesediting.d.ts +39 -0
  134. package/src/tableproperties/tablepropertiesediting.js +163 -210
  135. package/src/tableproperties/tablepropertiesui.d.ts +113 -0
  136. package/src/tableproperties/tablepropertiesui.js +293 -439
  137. package/src/tableproperties/ui/tablepropertiesview.d.ts +203 -0
  138. package/src/tableproperties/ui/tablepropertiesview.js +427 -718
  139. package/src/tableproperties.d.ts +28 -0
  140. package/src/tableproperties.js +12 -95
  141. package/src/tableselection.d.ts +106 -0
  142. package/src/tableselection.js +279 -376
  143. package/src/tabletoolbar.d.ts +31 -0
  144. package/src/tabletoolbar.js +38 -92
  145. package/src/tableui.d.ts +53 -0
  146. package/src/tableui.js +281 -338
  147. package/src/tableutils.d.ts +448 -0
  148. package/src/tableutils.js +1015 -1229
  149. package/src/tablewalker.d.ts +323 -0
  150. package/src/tablewalker.js +308 -548
  151. package/src/ui/colorinputview.d.ts +143 -0
  152. package/src/ui/colorinputview.js +229 -366
  153. package/src/ui/formrowview.d.ts +61 -0
  154. package/src/ui/formrowview.js +38 -84
  155. package/src/ui/inserttableview.d.ts +77 -0
  156. package/src/ui/inserttableview.js +152 -242
  157. package/src/utils/common.d.ts +42 -0
  158. package/src/utils/common.js +33 -57
  159. package/src/utils/structure.d.ts +245 -0
  160. package/src/utils/structure.js +261 -379
  161. package/src/utils/table-properties.d.ts +67 -0
  162. package/src/utils/table-properties.js +60 -81
  163. package/src/utils/ui/contextualballoon.d.ts +34 -0
  164. package/src/utils/ui/contextualballoon.js +70 -89
  165. package/src/utils/ui/table-properties.d.ts +193 -0
  166. package/src/utils/ui/table-properties.js +259 -319
  167. package/src/utils/ui/widget.d.ts +16 -0
  168. package/src/utils/ui/widget.js +24 -46
  169. package/src/tablecolumnresize/tablecolumnwidthscommand.js +0 -55
  170. package/src/tablecolumnresize/tablewidthresizecommand.js +0 -65
@@ -0,0 +1,62 @@
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/tablemouse/mouseeventsobserver
7
+ */
8
+ import { DomEventObserver, type DomEventData } from 'ckeditor5/src/engine';
9
+ /**
10
+ * The mouse selection event observer.
11
+ *
12
+ * It registers listeners for the following DOM events:
13
+ *
14
+ * - `'mousemove'`
15
+ * - `'mouseleave'`
16
+ *
17
+ * Note that this observer is disabled by default. To enable this observer, it needs to be added to
18
+ * {@link module:engine/view/view~View} using the {@link module:engine/view/view~View#addObserver} method.
19
+ *
20
+ * The observer is registered by the {@link module:table/tableselection~TableSelection} plugin.
21
+ */
22
+ export default class MouseEventsObserver extends DomEventObserver<'mousemove' | 'mouseleave'> {
23
+ readonly domEventType: readonly ["mousemove", "mouseleave"];
24
+ /**
25
+ * @inheritDoc
26
+ */
27
+ onDomEvent(domEvent: MouseEvent): void;
28
+ }
29
+ /**
30
+ * Fired when the mouse is moved over one of the editables.
31
+ *
32
+ * Introduced by {@link module:table/tablemouse/mouseeventsobserver~MouseEventsObserver}.
33
+ *
34
+ * Note that this event is not available by default. To make it available,
35
+ * {@link module:table/tablemouse/mouseeventsobserver~MouseEventsObserver} needs to be added
36
+ * to {@link module:engine/view/view~View} using the {@link module:engine/view/view~View#addObserver} method.
37
+ *
38
+ * @see module:table/tablemouse/mouseeventsobserver~MouseEventsObserver
39
+ * @eventName module:engine/view/document~Document#mousemove
40
+ * @param data Event data.
41
+ */
42
+ export type ViewDocumentMouseMoveEvent = {
43
+ name: 'mousemove';
44
+ args: [data: DomEventData<MouseEvent>];
45
+ };
46
+ /**
47
+ * Fired when the mouse is moved out of one of the editables.
48
+ *
49
+ * Introduced by {@link module:table/tablemouse/mouseeventsobserver~MouseEventsObserver}.
50
+ *
51
+ * Note that this event is not available by default. To make it available,
52
+ * {@link module:table/tablemouse/mouseeventsobserver~MouseEventsObserver} needs to be added
53
+ * to {@link module:engine/view/view~View} using the {@link module:engine/view/view~View#addObserver} method.
54
+ *
55
+ * @see module:table/tablemouse/mouseeventsobserver~MouseEventsObserver
56
+ * @eventName module:engine/view/document~Document#mouseleave
57
+ * @param data Event data.
58
+ */
59
+ export type ViewDocumentMouseLeaveEvent = {
60
+ name: 'mouseleave';
61
+ args: [data: DomEventData<MouseEvent>];
62
+ };
@@ -2,71 +2,34 @@
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
- * @module table/tableselection/mouseeventsobserver
6
+ * @module table/tablemouse/mouseeventsobserver
8
7
  */
9
-
10
8
  import { DomEventObserver } from 'ckeditor5/src/engine';
11
-
12
9
  /**
13
10
  * The mouse selection event observer.
14
11
  *
15
12
  * It registers listeners for the following DOM events:
16
13
  *
17
14
  * - `'mousemove'`
18
- * - `'mouseup'`
19
15
  * - `'mouseleave'`
20
16
  *
21
17
  * Note that this observer is disabled by default. To enable this observer, it needs to be added to
22
18
  * {@link module:engine/view/view~View} using the {@link module:engine/view/view~View#addObserver} method.
23
19
  *
24
20
  * The observer is registered by the {@link module:table/tableselection~TableSelection} plugin.
25
- *
26
- * @extends module:engine/view/observer/domeventobserver~DomEventObserver
27
21
  */
28
22
  export default class MouseEventsObserver extends DomEventObserver {
29
- /**
30
- * @inheritDoc
31
- */
32
- constructor( view ) {
33
- super( view );
34
-
35
- this.domEventType = [ 'mousemove', 'mouseleave' ];
36
- }
37
-
38
- /**
39
- * @inheritDoc
40
- */
41
- onDomEvent( domEvent ) {
42
- this.fire( domEvent.type, domEvent );
43
- }
23
+ constructor() {
24
+ super(...arguments);
25
+ this.domEventType = [
26
+ 'mousemove', 'mouseleave'
27
+ ];
28
+ }
29
+ /**
30
+ * @inheritDoc
31
+ */
32
+ onDomEvent(domEvent) {
33
+ this.fire(domEvent.type, domEvent);
34
+ }
44
35
  }
45
-
46
- /**
47
- * Fired when the mouse is moved over one of the editables.
48
- *
49
- * Introduced by {@link module:table/tableselection/mouseeventsobserver~MouseEventsObserver}.
50
- *
51
- * Note that this event is not available by default. To make it available,
52
- * {@link module:table/tableselection/mouseeventsobserver~MouseEventsObserver} needs to be added
53
- * to {@link module:engine/view/view~View} using the {@link module:engine/view/view~View#addObserver} method.
54
- *
55
- * @see module:table/tableselection/mouseeventsobserver~MouseEventsObserver
56
- * @event module:engine/view/document~Document#event:mousemove
57
- * @param {module:engine/view/observer/domeventdata~DomEventData} data Event data.
58
- */
59
-
60
- /**
61
- * Fired when the mouse is moved out of one of the editables.
62
- *
63
- * Introduced by {@link module:table/tableselection/mouseeventsobserver~MouseEventsObserver}.
64
- *
65
- * Note that this event is not available by default. To make it available,
66
- * {@link module:table/tableselection/mouseeventsobserver~MouseEventsObserver} needs to be added
67
- * to {@link module:engine/view/view~View} using the {@link module:engine/view/view~View#addObserver} method.
68
- *
69
- * @see module:table/tableselection/mouseeventsobserver~MouseEventsObserver
70
- * @event module:engine/view/document~Document#event:mouseleave
71
- * @param {module:engine/view/observer/domeventdata~DomEventData} data Event data.
72
- */
@@ -0,0 +1,46 @@
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/tablemouse
7
+ */
8
+ import { Plugin, type PluginDependencies } from 'ckeditor5/src/core';
9
+ /**
10
+ * This plugin enables a table cells' selection with the mouse.
11
+ * It is loaded automatically by the {@link module:table/table~Table} plugin.
12
+ */
13
+ export default class TableMouse extends Plugin {
14
+ /**
15
+ * @inheritDoc
16
+ */
17
+ static get pluginName(): 'TableMouse';
18
+ /**
19
+ * @inheritDoc
20
+ */
21
+ static get requires(): PluginDependencies;
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ init(): void;
26
+ /**
27
+ * Enables making cells selection by <kbd>Shift</kbd>+click. Creates a selection from the cell which previously held
28
+ * the selection to the cell which was clicked. It can be the same cell, in which case it selects a single cell.
29
+ */
30
+ private _enableShiftClickSelection;
31
+ /**
32
+ * Enables making cells selection by dragging.
33
+ *
34
+ * The selection is made only on mousemove. Mouse tracking is started on mousedown.
35
+ * However, the cells selection is enabled only after the mouse cursor left the anchor cell.
36
+ * Thanks to that normal text selection within one cell works just fine. However, you can still select
37
+ * just one cell by leaving the anchor cell and moving back to it.
38
+ */
39
+ private _enableMouseDragSelection;
40
+ /**
41
+ * Returns the model table cell element based on the target element of the passed DOM event.
42
+ *
43
+ * @returns Returns the table cell or `undefined`.
44
+ */
45
+ private _getModelTableCellFromDomEvent;
46
+ }
package/src/tablemouse.js CHANGED
@@ -2,219 +2,171 @@
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/tablemouse
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
-
12
9
  import TableSelection from './tableselection';
13
10
  import MouseEventsObserver from './tablemouse/mouseeventsobserver';
14
11
  import TableUtils from './tableutils';
15
-
16
12
  /**
17
13
  * This plugin enables a table cells' selection with the mouse.
18
14
  * It is loaded automatically by the {@link module:table/table~Table} plugin.
19
- *
20
- * @extends module:core/plugin~Plugin
21
15
  */
22
16
  export default class TableMouse extends Plugin {
23
- /**
24
- * @inheritDoc
25
- */
26
- static get pluginName() {
27
- return 'TableMouse';
28
- }
29
-
30
- /**
31
- * @inheritDoc
32
- */
33
- static get requires() {
34
- return [ TableSelection, TableUtils ];
35
- }
36
-
37
- /**
38
- * @inheritDoc
39
- */
40
- init() {
41
- const editor = this.editor;
42
-
43
- // Currently the MouseObserver only handles `mousedown` and `mouseup` events.
44
- // TODO move to the engine?
45
- editor.editing.view.addObserver( MouseEventsObserver );
46
-
47
- this._enableShiftClickSelection();
48
- this._enableMouseDragSelection();
49
- }
50
-
51
- /**
52
- * Enables making cells selection by <kbd>Shift</kbd>+click. Creates a selection from the cell which previously held
53
- * the selection to the cell which was clicked. It can be the same cell, in which case it selects a single cell.
54
- *
55
- * @private
56
- */
57
- _enableShiftClickSelection() {
58
- const editor = this.editor;
59
- const tableUtils = editor.plugins.get( TableUtils );
60
- let blockSelectionChange = false;
61
-
62
- const tableSelection = editor.plugins.get( TableSelection );
63
-
64
- this.listenTo( editor.editing.view.document, 'mousedown', ( evt, domEventData ) => {
65
- const selection = editor.model.document.selection;
66
-
67
- if ( !this.isEnabled || !tableSelection.isEnabled ) {
68
- return;
69
- }
70
-
71
- if ( !domEventData.domEvent.shiftKey ) {
72
- return;
73
- }
74
-
75
- const anchorCell = tableSelection.getAnchorCell() || tableUtils.getTableCellsContainingSelection( selection )[ 0 ];
76
-
77
- if ( !anchorCell ) {
78
- return;
79
- }
80
-
81
- const targetCell = this._getModelTableCellFromDomEvent( domEventData );
82
-
83
- if ( targetCell && haveSameTableParent( anchorCell, targetCell ) ) {
84
- blockSelectionChange = true;
85
- tableSelection.setCellSelection( anchorCell, targetCell );
86
-
87
- domEventData.preventDefault();
88
- }
89
- } );
90
-
91
- this.listenTo( editor.editing.view.document, 'mouseup', () => {
92
- blockSelectionChange = false;
93
- } );
94
-
95
- // We need to ignore a `selectionChange` event that is fired after we render our new table cells selection.
96
- // When downcasting table cells selection to the view, we put the view selection in the last selected cell
97
- // in a place that may not be natively a "correct" location. This is – we put it directly in the `<td>` element.
98
- // All browsers fire the native `selectionchange` event.
99
- // However, all browsers except Safari return the selection in the exact place where we put it
100
- // (even though it's visually normalized). Safari returns `<td><p>^foo` that makes our selection observer
101
- // fire our `selectionChange` event (because the view selection that we set in the first step differs from the DOM selection).
102
- // Since `selectionChange` is fired, we automatically update the model selection that moves it that paragraph.
103
- // This breaks our dear cells selection.
104
- //
105
- // Theoretically this issue concerns only Safari that is the only browser that do normalize the selection.
106
- // However, to avoid code branching and to have a good coverage for this event blocker, I enabled it for all browsers.
107
- //
108
- // Note: I'm keeping the `blockSelectionChange` state separately for shift+click and mouse drag (exact same logic)
109
- // so I don't have to try to analyze whether they don't overlap in some weird cases. Probably they don't.
110
- // But I have other things to do, like writing this comment.
111
- this.listenTo( editor.editing.view.document, 'selectionChange', evt => {
112
- if ( blockSelectionChange ) {
113
- // @if CK_DEBUG // console.log( 'Blocked selectionChange to avoid breaking table cells selection.' );
114
-
115
- evt.stop();
116
- }
117
- }, { priority: 'highest' } );
118
- }
119
-
120
- /**
121
- * Enables making cells selection by dragging.
122
- *
123
- * The selection is made only on mousemove. Mouse tracking is started on mousedown.
124
- * However, the cells selection is enabled only after the mouse cursor left the anchor cell.
125
- * Thanks to that normal text selection within one cell works just fine. However, you can still select
126
- * just one cell by leaving the anchor cell and moving back to it.
127
- *
128
- * @private
129
- */
130
- _enableMouseDragSelection() {
131
- const editor = this.editor;
132
- let anchorCell, targetCell;
133
- let beganCellSelection = false;
134
- let blockSelectionChange = false;
135
-
136
- const tableSelection = editor.plugins.get( TableSelection );
137
-
138
- this.listenTo( editor.editing.view.document, 'mousedown', ( evt, domEventData ) => {
139
- if ( !this.isEnabled || !tableSelection.isEnabled ) {
140
- return;
141
- }
142
-
143
- // Make sure to not conflict with the shift+click listener and any other possible handler.
144
- if ( domEventData.domEvent.shiftKey || domEventData.domEvent.ctrlKey || domEventData.domEvent.altKey ) {
145
- return;
146
- }
147
-
148
- anchorCell = this._getModelTableCellFromDomEvent( domEventData );
149
- } );
150
-
151
- this.listenTo( editor.editing.view.document, 'mousemove', ( evt, domEventData ) => {
152
- if ( !domEventData.domEvent.buttons ) {
153
- return;
154
- }
155
-
156
- if ( !anchorCell ) {
157
- return;
158
- }
159
-
160
- const newTargetCell = this._getModelTableCellFromDomEvent( domEventData );
161
-
162
- if ( newTargetCell && haveSameTableParent( anchorCell, newTargetCell ) ) {
163
- targetCell = newTargetCell;
164
-
165
- // Switch to the cell selection mode after the mouse cursor left the anchor cell.
166
- // Switch off only on mouseup (makes selecting a single cell possible).
167
- if ( !beganCellSelection && targetCell != anchorCell ) {
168
- beganCellSelection = true;
169
- }
170
- }
171
-
172
- // Yep, not making a cell selection yet. See method docs.
173
- if ( !beganCellSelection ) {
174
- return;
175
- }
176
-
177
- blockSelectionChange = true;
178
- tableSelection.setCellSelection( anchorCell, targetCell );
179
-
180
- domEventData.preventDefault();
181
- } );
182
-
183
- this.listenTo( editor.editing.view.document, 'mouseup', () => {
184
- beganCellSelection = false;
185
- blockSelectionChange = false;
186
- anchorCell = null;
187
- targetCell = null;
188
- } );
189
-
190
- // See the explanation in `_enableShiftClickSelection()`.
191
- this.listenTo( editor.editing.view.document, 'selectionChange', evt => {
192
- if ( blockSelectionChange ) {
193
- // @if CK_DEBUG // console.log( 'Blocked selectionChange to avoid breaking table cells selection.' );
194
-
195
- evt.stop();
196
- }
197
- }, { priority: 'highest' } );
198
- }
199
-
200
- /**
201
- * Returns the model table cell element based on the target element of the passed DOM event.
202
- *
203
- * @private
204
- * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
205
- * @returns {module:engine/model/element~Element|undefined} Returns the table cell or `undefined`.
206
- */
207
- _getModelTableCellFromDomEvent( domEventData ) {
208
- // Note: Work with positions (not element mapping) because the target element can be an attribute or other non-mapped element.
209
- const viewTargetElement = domEventData.target;
210
- const viewPosition = this.editor.editing.view.createPositionAt( viewTargetElement, 0 );
211
- const modelPosition = this.editor.editing.mapper.toModelPosition( viewPosition );
212
- const modelElement = modelPosition.parent;
213
-
214
- return modelElement.findAncestor( 'tableCell', { includeSelf: true } );
215
- }
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ static get pluginName() {
21
+ return 'TableMouse';
22
+ }
23
+ /**
24
+ * @inheritDoc
25
+ */
26
+ static get requires() {
27
+ return [TableSelection, TableUtils];
28
+ }
29
+ /**
30
+ * @inheritDoc
31
+ */
32
+ init() {
33
+ const editor = this.editor;
34
+ // Currently the MouseObserver only handles `mousedown` and `mouseup` events.
35
+ // TODO move to the engine?
36
+ editor.editing.view.addObserver(MouseEventsObserver);
37
+ this._enableShiftClickSelection();
38
+ this._enableMouseDragSelection();
39
+ }
40
+ /**
41
+ * Enables making cells selection by <kbd>Shift</kbd>+click. Creates a selection from the cell which previously held
42
+ * the selection to the cell which was clicked. It can be the same cell, in which case it selects a single cell.
43
+ */
44
+ _enableShiftClickSelection() {
45
+ const editor = this.editor;
46
+ const tableUtils = editor.plugins.get(TableUtils);
47
+ let blockSelectionChange = false;
48
+ const tableSelection = editor.plugins.get(TableSelection);
49
+ this.listenTo(editor.editing.view.document, 'mousedown', (evt, domEventData) => {
50
+ const selection = editor.model.document.selection;
51
+ if (!this.isEnabled || !tableSelection.isEnabled) {
52
+ return;
53
+ }
54
+ if (!domEventData.domEvent.shiftKey) {
55
+ return;
56
+ }
57
+ const anchorCell = tableSelection.getAnchorCell() || tableUtils.getTableCellsContainingSelection(selection)[0];
58
+ if (!anchorCell) {
59
+ return;
60
+ }
61
+ const targetCell = this._getModelTableCellFromDomEvent(domEventData);
62
+ if (targetCell && haveSameTableParent(anchorCell, targetCell)) {
63
+ blockSelectionChange = true;
64
+ tableSelection.setCellSelection(anchorCell, targetCell);
65
+ domEventData.preventDefault();
66
+ }
67
+ });
68
+ this.listenTo(editor.editing.view.document, 'mouseup', () => {
69
+ blockSelectionChange = false;
70
+ });
71
+ // We need to ignore a `selectionChange` event that is fired after we render our new table cells selection.
72
+ // When downcasting table cells selection to the view, we put the view selection in the last selected cell
73
+ // in a place that may not be natively a "correct" location. This is – we put it directly in the `<td>` element.
74
+ // All browsers fire the native `selectionchange` event.
75
+ // However, all browsers except Safari return the selection in the exact place where we put it
76
+ // (even though it's visually normalized). Safari returns `<td><p>^foo` that makes our selection observer
77
+ // fire our `selectionChange` event (because the view selection that we set in the first step differs from the DOM selection).
78
+ // Since `selectionChange` is fired, we automatically update the model selection that moves it that paragraph.
79
+ // This breaks our dear cells selection.
80
+ //
81
+ // Theoretically this issue concerns only Safari that is the only browser that do normalize the selection.
82
+ // However, to avoid code branching and to have a good coverage for this event blocker, I enabled it for all browsers.
83
+ //
84
+ // Note: I'm keeping the `blockSelectionChange` state separately for shift+click and mouse drag (exact same logic)
85
+ // so I don't have to try to analyze whether they don't overlap in some weird cases. Probably they don't.
86
+ // But I have other things to do, like writing this comment.
87
+ this.listenTo(editor.editing.view.document, 'selectionChange', evt => {
88
+ if (blockSelectionChange) {
89
+ // @if CK_DEBUG // console.log( 'Blocked selectionChange to avoid breaking table cells selection.' );
90
+ evt.stop();
91
+ }
92
+ }, { priority: 'highest' });
93
+ }
94
+ /**
95
+ * Enables making cells selection by dragging.
96
+ *
97
+ * The selection is made only on mousemove. Mouse tracking is started on mousedown.
98
+ * However, the cells selection is enabled only after the mouse cursor left the anchor cell.
99
+ * Thanks to that normal text selection within one cell works just fine. However, you can still select
100
+ * just one cell by leaving the anchor cell and moving back to it.
101
+ */
102
+ _enableMouseDragSelection() {
103
+ const editor = this.editor;
104
+ let anchorCell, targetCell;
105
+ let beganCellSelection = false;
106
+ let blockSelectionChange = false;
107
+ const tableSelection = editor.plugins.get(TableSelection);
108
+ this.listenTo(editor.editing.view.document, 'mousedown', (evt, domEventData) => {
109
+ if (!this.isEnabled || !tableSelection.isEnabled) {
110
+ return;
111
+ }
112
+ // Make sure to not conflict with the shift+click listener and any other possible handler.
113
+ if (domEventData.domEvent.shiftKey || domEventData.domEvent.ctrlKey || domEventData.domEvent.altKey) {
114
+ return;
115
+ }
116
+ anchorCell = this._getModelTableCellFromDomEvent(domEventData);
117
+ });
118
+ this.listenTo(editor.editing.view.document, 'mousemove', (evt, domEventData) => {
119
+ if (!domEventData.domEvent.buttons) {
120
+ return;
121
+ }
122
+ if (!anchorCell) {
123
+ return;
124
+ }
125
+ const newTargetCell = this._getModelTableCellFromDomEvent(domEventData);
126
+ if (newTargetCell && haveSameTableParent(anchorCell, newTargetCell)) {
127
+ targetCell = newTargetCell;
128
+ // Switch to the cell selection mode after the mouse cursor left the anchor cell.
129
+ // Switch off only on mouseup (makes selecting a single cell possible).
130
+ if (!beganCellSelection && targetCell != anchorCell) {
131
+ beganCellSelection = true;
132
+ }
133
+ }
134
+ // Yep, not making a cell selection yet. See method docs.
135
+ if (!beganCellSelection) {
136
+ return;
137
+ }
138
+ blockSelectionChange = true;
139
+ tableSelection.setCellSelection(anchorCell, targetCell);
140
+ domEventData.preventDefault();
141
+ });
142
+ this.listenTo(editor.editing.view.document, 'mouseup', () => {
143
+ beganCellSelection = false;
144
+ blockSelectionChange = false;
145
+ anchorCell = null;
146
+ targetCell = null;
147
+ });
148
+ // See the explanation in `_enableShiftClickSelection()`.
149
+ this.listenTo(editor.editing.view.document, 'selectionChange', evt => {
150
+ if (blockSelectionChange) {
151
+ // @if CK_DEBUG // console.log( 'Blocked selectionChange to avoid breaking table cells selection.' );
152
+ evt.stop();
153
+ }
154
+ }, { priority: 'highest' });
155
+ }
156
+ /**
157
+ * Returns the model table cell element based on the target element of the passed DOM event.
158
+ *
159
+ * @returns Returns the table cell or `undefined`.
160
+ */
161
+ _getModelTableCellFromDomEvent(domEventData) {
162
+ // Note: Work with positions (not element mapping) because the target element can be an attribute or other non-mapped element.
163
+ const viewTargetElement = domEventData.target;
164
+ const viewPosition = this.editor.editing.view.createPositionAt(viewTargetElement, 0);
165
+ const modelPosition = this.editor.editing.mapper.toModelPosition(viewPosition);
166
+ const modelElement = modelPosition.parent;
167
+ return modelElement.findAncestor('tableCell', { includeSelf: true });
168
+ }
216
169
  }
217
-
218
- function haveSameTableParent( cellA, cellB ) {
219
- return cellA.parent.parent == cellB.parent.parent;
170
+ function haveSameTableParent(cellA, cellB) {
171
+ return cellA.parent.parent == cellB.parent.parent;
220
172
  }
@@ -0,0 +1,32 @@
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/tableproperties/commands/tablealignmentcommand
7
+ */
8
+ import type { Editor } from 'ckeditor5/src/core';
9
+ import TablePropertyCommand from './tablepropertycommand';
10
+ /**
11
+ * The table alignment command.
12
+ *
13
+ * The command is registered by the {@link module:table/tableproperties/tablepropertiesediting~TablePropertiesEditing} as
14
+ * the `'tableAlignment'` editor command.
15
+ *
16
+ * To change the alignment of the selected table, execute the command:
17
+ *
18
+ * ```ts
19
+ * editor.execute( 'tableAlignment', {
20
+ * value: 'right'
21
+ * } );
22
+ * ```
23
+ */
24
+ export default class TableAlignmentCommand extends TablePropertyCommand {
25
+ /**
26
+ * Creates a new `TableAlignmentCommand` instance.
27
+ *
28
+ * @param editor An editor in which this command will be used.
29
+ * @param defaultValue The default value for the "alignment" attribute.
30
+ */
31
+ constructor(editor: Editor, defaultValue: string);
32
+ }
@@ -2,13 +2,7 @@
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/tableproperties/commands/tablealignmentcommand
8
- */
9
-
10
5
  import TablePropertyCommand from './tablepropertycommand';
11
-
12
6
  /**
13
7
  * The table alignment command.
14
8
  *
@@ -17,20 +11,20 @@ import TablePropertyCommand from './tablepropertycommand';
17
11
  *
18
12
  * To change the alignment of the selected table, execute the command:
19
13
  *
20
- * editor.execute( 'tableAlignment', {
21
- * value: 'right'
22
- * } );
23
- *
24
- * @extends module:table/tableproperties/commands/tablepropertycommand~TablePropertyCommand
14
+ * ```ts
15
+ * editor.execute( 'tableAlignment', {
16
+ * value: 'right'
17
+ * } );
18
+ * ```
25
19
  */
26
20
  export default class TableAlignmentCommand extends TablePropertyCommand {
27
- /**
28
- * Creates a new `TableAlignmentCommand` instance.
29
- *
30
- * @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
31
- * @param {String} defaultValue The default value for the "alignment" attribute.
32
- */
33
- constructor( editor, defaultValue ) {
34
- super( editor, 'tableAlignment', defaultValue );
35
- }
21
+ /**
22
+ * Creates a new `TableAlignmentCommand` instance.
23
+ *
24
+ * @param editor An editor in which this command will be used.
25
+ * @param defaultValue The default value for the "alignment" attribute.
26
+ */
27
+ constructor(editor, defaultValue) {
28
+ super(editor, 'tableAlignment', defaultValue);
29
+ }
36
30
  }