@ckeditor/ckeditor5-table 40.0.0 → 40.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.
- package/CHANGELOG.md +15 -15
- package/LICENSE.md +3 -3
- package/build/table.js +1 -1
- package/build/translations/fi.js +1 -1
- package/lang/translations/fi.po +3 -3
- package/package.json +2 -2
- package/src/augmentation.d.ts +76 -76
- package/src/augmentation.js +5 -5
- package/src/commands/insertcolumncommand.d.ts +55 -55
- package/src/commands/insertcolumncommand.js +67 -67
- package/src/commands/insertrowcommand.d.ts +54 -54
- package/src/commands/insertrowcommand.js +66 -66
- package/src/commands/inserttablecommand.d.ts +44 -44
- package/src/commands/inserttablecommand.js +69 -69
- package/src/commands/mergecellcommand.d.ts +68 -68
- package/src/commands/mergecellcommand.js +198 -198
- package/src/commands/mergecellscommand.d.ts +28 -28
- package/src/commands/mergecellscommand.js +94 -94
- package/src/commands/removecolumncommand.d.ts +29 -29
- package/src/commands/removecolumncommand.js +109 -109
- package/src/commands/removerowcommand.d.ts +29 -29
- package/src/commands/removerowcommand.js +82 -82
- package/src/commands/selectcolumncommand.d.ts +33 -33
- package/src/commands/selectcolumncommand.js +60 -60
- package/src/commands/selectrowcommand.d.ts +33 -33
- package/src/commands/selectrowcommand.js +56 -56
- package/src/commands/setheadercolumncommand.d.ts +50 -50
- package/src/commands/setheadercolumncommand.js +71 -71
- package/src/commands/setheaderrowcommand.d.ts +53 -53
- package/src/commands/setheaderrowcommand.js +79 -79
- package/src/commands/splitcellcommand.d.ts +43 -43
- package/src/commands/splitcellcommand.js +54 -54
- package/src/converters/downcast.d.ts +63 -63
- package/src/converters/downcast.js +146 -146
- package/src/converters/table-caption-post-fixer.d.ts +20 -20
- package/src/converters/table-caption-post-fixer.js +53 -53
- package/src/converters/table-cell-paragraph-post-fixer.d.ts +32 -32
- package/src/converters/table-cell-paragraph-post-fixer.js +107 -107
- package/src/converters/table-cell-refresh-handler.d.ts +18 -18
- package/src/converters/table-cell-refresh-handler.js +45 -45
- package/src/converters/table-headings-refresh-handler.d.ts +17 -17
- package/src/converters/table-headings-refresh-handler.js +49 -49
- package/src/converters/table-layout-post-fixer.d.ts +226 -226
- package/src/converters/table-layout-post-fixer.js +367 -367
- package/src/converters/tableproperties.d.ts +54 -54
- package/src/converters/tableproperties.js +159 -159
- package/src/converters/upcasttable.d.ts +49 -49
- package/src/converters/upcasttable.js +243 -243
- package/src/index.d.ts +60 -60
- package/src/index.js +30 -30
- package/src/plaintableoutput.d.ts +26 -26
- package/src/plaintableoutput.js +123 -123
- package/src/table.d.ts +40 -40
- package/src/table.js +44 -44
- package/src/tablecaption/tablecaptionediting.d.ts +63 -63
- package/src/tablecaption/tablecaptionediting.js +122 -122
- package/src/tablecaption/tablecaptionui.d.ts +21 -21
- package/src/tablecaption/tablecaptionui.js +57 -57
- package/src/tablecaption/toggletablecaptioncommand.d.ts +68 -68
- package/src/tablecaption/toggletablecaptioncommand.js +105 -104
- package/src/tablecaption/utils.d.ts +38 -42
- package/src/tablecaption/utils.js +57 -67
- package/src/tablecaption.d.ts +24 -24
- package/src/tablecaption.js +28 -28
- package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.d.ts +32 -32
- package/src/tablecellproperties/commands/tablecellbackgroundcolorcommand.js +30 -30
- package/src/tablecellproperties/commands/tablecellbordercolorcommand.d.ts +37 -37
- package/src/tablecellproperties/commands/tablecellbordercolorcommand.js +44 -44
- package/src/tablecellproperties/commands/tablecellborderstylecommand.d.ts +37 -37
- package/src/tablecellproperties/commands/tablecellborderstylecommand.js +44 -44
- package/src/tablecellproperties/commands/tablecellborderwidthcommand.d.ts +51 -51
- package/src/tablecellproperties/commands/tablecellborderwidthcommand.js +64 -64
- package/src/tablecellproperties/commands/tablecellheightcommand.d.ts +46 -46
- package/src/tablecellproperties/commands/tablecellheightcommand.js +51 -51
- package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.d.ts +32 -32
- package/src/tablecellproperties/commands/tablecellhorizontalalignmentcommand.js +30 -30
- package/src/tablecellproperties/commands/tablecellpaddingcommand.d.ts +51 -51
- package/src/tablecellproperties/commands/tablecellpaddingcommand.js +64 -64
- package/src/tablecellproperties/commands/tablecellpropertycommand.d.ts +62 -62
- package/src/tablecellproperties/commands/tablecellpropertycommand.js +92 -92
- package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.d.ts +40 -40
- package/src/tablecellproperties/commands/tablecellverticalalignmentcommand.js +38 -38
- package/src/tablecellproperties/tablecellpropertiesediting.d.ts +43 -43
- package/src/tablecellproperties/tablecellpropertiesediting.js +241 -241
- package/src/tablecellproperties/tablecellpropertiesui.d.ts +112 -112
- package/src/tablecellproperties/tablecellpropertiesui.js +330 -330
- package/src/tablecellproperties/ui/tablecellpropertiesview.d.ts +228 -228
- package/src/tablecellproperties/ui/tablecellpropertiesview.js +548 -548
- package/src/tablecellproperties.d.ts +30 -30
- package/src/tablecellproperties.js +34 -34
- package/src/tablecellwidth/commands/tablecellwidthcommand.d.ts +46 -46
- package/src/tablecellwidth/commands/tablecellwidthcommand.js +51 -51
- package/src/tablecellwidth/tablecellwidthediting.d.ts +29 -29
- package/src/tablecellwidth/tablecellwidthediting.js +45 -45
- package/src/tableclipboard.d.ts +65 -65
- package/src/tableclipboard.js +450 -450
- package/src/tablecolumnresize/constants.d.ts +20 -20
- package/src/tablecolumnresize/constants.js +20 -20
- package/src/tablecolumnresize/converters.d.ts +18 -18
- package/src/tablecolumnresize/converters.js +46 -46
- package/src/tablecolumnresize/tablecolumnresizeediting.d.ts +139 -139
- package/src/tablecolumnresize/tablecolumnresizeediting.js +583 -583
- package/src/tablecolumnresize/tablewidthscommand.d.ts +38 -38
- package/src/tablecolumnresize/tablewidthscommand.js +61 -61
- package/src/tablecolumnresize/utils.d.ts +148 -148
- package/src/tablecolumnresize/utils.js +358 -358
- package/src/tablecolumnresize.d.ts +26 -26
- package/src/tablecolumnresize.js +30 -30
- package/src/tableconfig.d.ts +343 -343
- package/src/tableconfig.js +5 -5
- package/src/tableediting.d.ts +98 -98
- package/src/tableediting.js +191 -191
- package/src/tablekeyboard.d.ts +68 -68
- package/src/tablekeyboard.js +279 -279
- package/src/tablemouse/mouseeventsobserver.d.ts +62 -62
- package/src/tablemouse/mouseeventsobserver.js +35 -35
- package/src/tablemouse.d.ts +48 -48
- package/src/tablemouse.js +172 -172
- package/src/tableproperties/commands/tablealignmentcommand.d.ts +32 -32
- package/src/tableproperties/commands/tablealignmentcommand.js +30 -30
- package/src/tableproperties/commands/tablebackgroundcolorcommand.d.ts +32 -32
- package/src/tableproperties/commands/tablebackgroundcolorcommand.js +30 -30
- package/src/tableproperties/commands/tablebordercolorcommand.d.ts +37 -37
- package/src/tableproperties/commands/tablebordercolorcommand.js +44 -44
- package/src/tableproperties/commands/tableborderstylecommand.d.ts +37 -37
- package/src/tableproperties/commands/tableborderstylecommand.js +44 -44
- package/src/tableproperties/commands/tableborderwidthcommand.d.ts +51 -51
- package/src/tableproperties/commands/tableborderwidthcommand.js +64 -64
- package/src/tableproperties/commands/tableheightcommand.d.ts +46 -46
- package/src/tableproperties/commands/tableheightcommand.js +54 -54
- package/src/tableproperties/commands/tablepropertycommand.d.ts +61 -61
- package/src/tableproperties/commands/tablepropertycommand.js +81 -80
- package/src/tableproperties/commands/tablewidthcommand.d.ts +46 -46
- package/src/tableproperties/commands/tablewidthcommand.js +54 -54
- package/src/tableproperties/tablepropertiesediting.d.ts +40 -40
- package/src/tableproperties/tablepropertiesediting.js +206 -206
- package/src/tableproperties/tablepropertiesui.d.ts +114 -114
- package/src/tableproperties/tablepropertiesui.js +321 -321
- package/src/tableproperties/ui/tablepropertiesview.d.ts +207 -207
- package/src/tableproperties/ui/tablepropertiesview.js +466 -466
- package/src/tableproperties.d.ts +30 -30
- package/src/tableproperties.js +34 -34
- package/src/tableselection.d.ts +107 -107
- package/src/tableselection.js +297 -297
- package/src/tabletoolbar.d.ts +32 -32
- package/src/tabletoolbar.js +57 -57
- package/src/tableui.d.ts +53 -53
- package/src/tableui.js +309 -309
- package/src/tableutils.d.ts +448 -448
- package/src/tableutils.js +1055 -1055
- package/src/tablewalker.d.ts +362 -362
- package/src/tablewalker.js +393 -393
- package/src/ui/colorinputview.d.ts +140 -140
- package/src/ui/colorinputview.js +271 -271
- package/src/ui/formrowview.d.ts +61 -61
- package/src/ui/formrowview.js +57 -57
- package/src/ui/inserttableview.d.ts +77 -77
- package/src/ui/inserttableview.js +169 -169
- package/src/utils/common.d.ts +46 -42
- package/src/utils/common.js +68 -57
- package/src/utils/structure.d.ts +245 -245
- package/src/utils/structure.js +426 -426
- package/src/utils/table-properties.d.ts +67 -67
- package/src/utils/table-properties.js +86 -86
- package/src/utils/ui/contextualballoon.d.ts +34 -34
- package/src/utils/ui/contextualballoon.js +110 -106
- package/src/utils/ui/table-properties.d.ts +195 -195
- package/src/utils/ui/table-properties.js +362 -362
- package/src/utils/ui/widget.d.ts +20 -16
- package/src/utils/ui/widget.js +48 -38
- package/build/table.js.map +0 -1
|
@@ -1,583 +1,583 @@
|
|
|
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/tablecolumnresize/tablecolumnresizeediting
|
|
7
|
-
*/
|
|
8
|
-
import { throttle, isEqual } from 'lodash-es';
|
|
9
|
-
import { global, DomEmitterMixin } from 'ckeditor5/src/utils';
|
|
10
|
-
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
-
import MouseEventsObserver from '../../src/tablemouse/mouseeventsobserver';
|
|
12
|
-
import TableEditing from '../tableediting';
|
|
13
|
-
import TableUtils from '../tableutils';
|
|
14
|
-
import TableWalker from '../tablewalker';
|
|
15
|
-
import TableWidthsCommand from './tablewidthscommand';
|
|
16
|
-
import { downcastTableResizedClass, upcastColgroupElement } from './converters';
|
|
17
|
-
import { clamp, createFilledArray, sumArray, getColumnEdgesIndexes, getChangedResizedTables, getColumnMinWidthAsPercentage, getElementWidthInPixels, getTableWidthInPixels, normalizeColumnWidths, toPrecision, getDomCellOuterWidth, updateColumnElements, getColumnGroupElement, getTableColumnElements, getTableColumnsWidths } from './utils';
|
|
18
|
-
import { COLUMN_MIN_WIDTH_IN_PIXELS } from './constants';
|
|
19
|
-
/**
|
|
20
|
-
* The table column resize editing plugin.
|
|
21
|
-
*/
|
|
22
|
-
export default class TableColumnResizeEditing extends Plugin {
|
|
23
|
-
/**
|
|
24
|
-
* @inheritDoc
|
|
25
|
-
*/
|
|
26
|
-
static get requires() {
|
|
27
|
-
return [TableEditing, TableUtils];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @inheritDoc
|
|
31
|
-
*/
|
|
32
|
-
static get pluginName() {
|
|
33
|
-
return 'TableColumnResizeEditing';
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @inheritDoc
|
|
37
|
-
*/
|
|
38
|
-
constructor(editor) {
|
|
39
|
-
super(editor);
|
|
40
|
-
this._isResizingActive = false;
|
|
41
|
-
this.set('_isResizingAllowed', true);
|
|
42
|
-
this._resizingData = null;
|
|
43
|
-
this._domEmitter = new (DomEmitterMixin())();
|
|
44
|
-
this._tableUtilsPlugin = editor.plugins.get('TableUtils');
|
|
45
|
-
this.on('change:_isResizingAllowed', (evt, name, value) => {
|
|
46
|
-
// Toggling the `ck-column-resize_disabled` class shows and hides the resizers through CSS.
|
|
47
|
-
const classAction = value ? 'removeClass' : 'addClass';
|
|
48
|
-
editor.editing.view.change(writer => {
|
|
49
|
-
for (const root of editor.editing.view.document.roots) {
|
|
50
|
-
writer[classAction]('ck-column-resize_disabled', editor.editing.view.document.getRoot(root.rootName));
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* @inheritDoc
|
|
57
|
-
*/
|
|
58
|
-
init() {
|
|
59
|
-
this._extendSchema();
|
|
60
|
-
this._registerPostFixer();
|
|
61
|
-
this._registerConverters();
|
|
62
|
-
this._registerResizingListeners();
|
|
63
|
-
this._registerResizerInserter();
|
|
64
|
-
const editor = this.editor;
|
|
65
|
-
const columnResizePlugin = editor.plugins.get('TableColumnResize');
|
|
66
|
-
const tableEditing = editor.plugins.get('TableEditing');
|
|
67
|
-
tableEditing.registerAdditionalSlot({
|
|
68
|
-
filter: element => element.is('element', 'tableColumnGroup'),
|
|
69
|
-
positionOffset: 0
|
|
70
|
-
});
|
|
71
|
-
const tableWidthsCommand = new TableWidthsCommand(editor);
|
|
72
|
-
// For backwards compatibility we have two commands that perform exactly the same operation.
|
|
73
|
-
editor.commands.add('resizeTableWidth', tableWidthsCommand);
|
|
74
|
-
editor.commands.add('resizeColumnWidths', tableWidthsCommand);
|
|
75
|
-
// Currently the states of column resize and table resize (which is actually the last column resize) features
|
|
76
|
-
// are bound together. They can be separated in the future by adding distinct listeners and applying
|
|
77
|
-
// different CSS classes (e.g. `ck-column-resize_disabled` and `ck-table-resize_disabled`) to the editor root.
|
|
78
|
-
// See #12148 for the details.
|
|
79
|
-
this.bind('_isResizingAllowed').to(editor, 'isReadOnly', columnResizePlugin, 'isEnabled', tableWidthsCommand, 'isEnabled', (isEditorReadOnly, isPluginEnabled, isTableWidthsCommandCommandEnabled) => !isEditorReadOnly && isPluginEnabled && isTableWidthsCommandCommandEnabled);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* @inheritDoc
|
|
83
|
-
*/
|
|
84
|
-
destroy() {
|
|
85
|
-
this._domEmitter.stopListening();
|
|
86
|
-
super.destroy();
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Returns a 'tableColumnGroup' element from the 'table'.
|
|
90
|
-
*
|
|
91
|
-
* @param element A 'table' or 'tableColumnGroup' element.
|
|
92
|
-
* @returns A 'tableColumnGroup' element.
|
|
93
|
-
*/
|
|
94
|
-
getColumnGroupElement(element) {
|
|
95
|
-
return getColumnGroupElement(element);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Returns an array of 'tableColumn' elements.
|
|
99
|
-
*
|
|
100
|
-
* @param element A 'table' or 'tableColumnGroup' element.
|
|
101
|
-
* @returns An array of 'tableColumn' elements.
|
|
102
|
-
*/
|
|
103
|
-
getTableColumnElements(element) {
|
|
104
|
-
return getTableColumnElements(element);
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Returns an array of table column widths.
|
|
108
|
-
*
|
|
109
|
-
* @param element A 'table' or 'tableColumnGroup' element.
|
|
110
|
-
* @returns An array of table column widths.
|
|
111
|
-
*/
|
|
112
|
-
getTableColumnsWidths(element) {
|
|
113
|
-
return getTableColumnsWidths(element);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Registers new attributes for a table model element.
|
|
117
|
-
*/
|
|
118
|
-
_extendSchema() {
|
|
119
|
-
this.editor.model.schema.extend('table', {
|
|
120
|
-
allowAttributes: ['tableWidth']
|
|
121
|
-
});
|
|
122
|
-
this.editor.model.schema.register('tableColumnGroup', {
|
|
123
|
-
allowIn: 'table',
|
|
124
|
-
isLimit: true
|
|
125
|
-
});
|
|
126
|
-
this.editor.model.schema.register('tableColumn', {
|
|
127
|
-
allowIn: 'tableColumnGroup',
|
|
128
|
-
allowAttributes: ['columnWidth', 'colSpan'],
|
|
129
|
-
isLimit: true
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Registers table column resize post-fixer.
|
|
134
|
-
*
|
|
135
|
-
* It checks if the change from the differ concerns a table-related element or attribute. For detected changes it:
|
|
136
|
-
* * Adjusts the `columnWidths` attribute to guarantee that the sum of the widths from all columns is 100%.
|
|
137
|
-
* * Checks if the `columnWidths` attribute gets updated accordingly after columns have been added or removed.
|
|
138
|
-
*/
|
|
139
|
-
_registerPostFixer() {
|
|
140
|
-
const editor = this.editor;
|
|
141
|
-
const model = editor.model;
|
|
142
|
-
model.document.registerPostFixer(writer => {
|
|
143
|
-
let changed = false;
|
|
144
|
-
for (const table of getChangedResizedTables(model)) {
|
|
145
|
-
const tableColumnGroup = this.getColumnGroupElement(table);
|
|
146
|
-
const columns = this.getTableColumnElements(tableColumnGroup);
|
|
147
|
-
const columnWidths = this.getTableColumnsWidths(tableColumnGroup);
|
|
148
|
-
// Adjust the `columnWidths` attribute to guarantee that the sum of the widths from all columns is 100%.
|
|
149
|
-
let normalizedWidths = normalizeColumnWidths(columnWidths);
|
|
150
|
-
// If the number of columns has changed, then we need to adjust the widths of the affected columns.
|
|
151
|
-
normalizedWidths = adjustColumnWidths(normalizedWidths, table, this);
|
|
152
|
-
if (isEqual(columnWidths, normalizedWidths)) {
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
updateColumnElements(columns, tableColumnGroup, normalizedWidths, writer);
|
|
156
|
-
changed = true;
|
|
157
|
-
}
|
|
158
|
-
return changed;
|
|
159
|
-
});
|
|
160
|
-
/**
|
|
161
|
-
* Adjusts if necessary the `columnWidths` in case if the number of column has changed.
|
|
162
|
-
*
|
|
163
|
-
* @param columnWidths Note: this array **may be modified** by the function.
|
|
164
|
-
* @param table Table to be checked.
|
|
165
|
-
*/
|
|
166
|
-
function adjustColumnWidths(columnWidths, table, plugin) {
|
|
167
|
-
const newTableColumnsCount = plugin._tableUtilsPlugin.getColumns(table);
|
|
168
|
-
const columnsCountDelta = newTableColumnsCount - columnWidths.length;
|
|
169
|
-
if (columnsCountDelta === 0) {
|
|
170
|
-
return columnWidths;
|
|
171
|
-
}
|
|
172
|
-
const widths = columnWidths.map(width => Number(width.replace('%', '')));
|
|
173
|
-
// Collect all cells that are affected by the change.
|
|
174
|
-
const cellSet = getAffectedCells(plugin.editor.model.document.differ, table);
|
|
175
|
-
for (const cell of cellSet) {
|
|
176
|
-
const currentColumnsDelta = newTableColumnsCount - widths.length;
|
|
177
|
-
if (currentColumnsDelta === 0) {
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
// If the column count in the table changed, adjust the widths of the affected columns.
|
|
181
|
-
const hasMoreColumns = currentColumnsDelta > 0;
|
|
182
|
-
const currentColumnIndex = plugin._tableUtilsPlugin.getCellLocation(cell).column;
|
|
183
|
-
if (hasMoreColumns) {
|
|
184
|
-
const columnMinWidthAsPercentage = getColumnMinWidthAsPercentage(table, plugin.editor);
|
|
185
|
-
const columnWidthsToInsert = createFilledArray(currentColumnsDelta, columnMinWidthAsPercentage);
|
|
186
|
-
widths.splice(currentColumnIndex, 0, ...columnWidthsToInsert);
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
// Moves the widths of the removed columns to the preceding one.
|
|
190
|
-
// Other editors either reduce the width of the whole table or adjust the widths
|
|
191
|
-
// proportionally, so change of this behavior can be considered in the future.
|
|
192
|
-
const removedColumnWidths = widths.splice(currentColumnIndex, Math.abs(currentColumnsDelta));
|
|
193
|
-
widths[currentColumnIndex] += sumArray(removedColumnWidths);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return widths.map(width => width + '%');
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Returns a set of cells that have been changed in a given table.
|
|
200
|
-
*/
|
|
201
|
-
function getAffectedCells(differ, table) {
|
|
202
|
-
const cellSet = new Set();
|
|
203
|
-
for (const change of differ.getChanges()) {
|
|
204
|
-
if (change.type == 'insert' &&
|
|
205
|
-
change.position.nodeAfter &&
|
|
206
|
-
change.position.nodeAfter.name == 'tableCell' &&
|
|
207
|
-
change.position.nodeAfter.getAncestors().includes(table)) {
|
|
208
|
-
cellSet.add(change.position.nodeAfter);
|
|
209
|
-
}
|
|
210
|
-
else if (change.type == 'remove') {
|
|
211
|
-
// If the first cell was removed, use the node after the change position instead.
|
|
212
|
-
const referenceNode = (change.position.nodeBefore || change.position.nodeAfter);
|
|
213
|
-
if (referenceNode.name == 'tableCell' && referenceNode.getAncestors().includes(table)) {
|
|
214
|
-
cellSet.add(referenceNode);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return cellSet;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Registers table column resize converters.
|
|
223
|
-
*/
|
|
224
|
-
_registerConverters() {
|
|
225
|
-
const editor = this.editor;
|
|
226
|
-
const conversion = editor.conversion;
|
|
227
|
-
// Table width style
|
|
228
|
-
conversion.for('upcast').attributeToAttribute({
|
|
229
|
-
view: {
|
|
230
|
-
name: 'figure',
|
|
231
|
-
key: 'style',
|
|
232
|
-
value: {
|
|
233
|
-
width: /[\s\S]+/
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
model: {
|
|
237
|
-
name: 'table',
|
|
238
|
-
key: 'tableWidth',
|
|
239
|
-
value: (viewElement) => viewElement.getStyle('width')
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
conversion.for('downcast').attributeToAttribute({
|
|
243
|
-
model: {
|
|
244
|
-
name: 'table',
|
|
245
|
-
key: 'tableWidth'
|
|
246
|
-
},
|
|
247
|
-
view: (width) => ({
|
|
248
|
-
name: 'figure',
|
|
249
|
-
key: 'style',
|
|
250
|
-
value: {
|
|
251
|
-
width
|
|
252
|
-
}
|
|
253
|
-
})
|
|
254
|
-
});
|
|
255
|
-
conversion.elementToElement({ model: 'tableColumnGroup', view: 'colgroup' });
|
|
256
|
-
conversion.elementToElement({ model: 'tableColumn', view: 'col' });
|
|
257
|
-
conversion.for('downcast').add(downcastTableResizedClass());
|
|
258
|
-
conversion.for('upcast').add(upcastColgroupElement(this._tableUtilsPlugin));
|
|
259
|
-
conversion.for('upcast').attributeToAttribute({
|
|
260
|
-
view: {
|
|
261
|
-
name: 'col',
|
|
262
|
-
styles: {
|
|
263
|
-
width: /.*/
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
model: {
|
|
267
|
-
key: 'columnWidth',
|
|
268
|
-
value: (viewElement) => {
|
|
269
|
-
const viewColWidth = viewElement.getStyle('width');
|
|
270
|
-
// 'pt' is the default unit for table column width pasted from MS Office.
|
|
271
|
-
// See https://github.com/ckeditor/ckeditor5/issues/14521#issuecomment-1662102889 for more details.
|
|
272
|
-
if (!viewColWidth || (!viewColWidth.endsWith('%') && !viewColWidth.endsWith('pt'))) {
|
|
273
|
-
return 'auto';
|
|
274
|
-
}
|
|
275
|
-
return viewColWidth;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
// The `col[span]` attribute is present in tables pasted from MS Excel. We use it to set the temporary `colSpan` model attribute,
|
|
280
|
-
// which is consumed during the `colgroup` element upcast.
|
|
281
|
-
// See https://github.com/ckeditor/ckeditor5/issues/14521#issuecomment-1662102889 for more details.
|
|
282
|
-
conversion.for('upcast').attributeToAttribute({
|
|
283
|
-
view: {
|
|
284
|
-
name: 'col',
|
|
285
|
-
key: 'span'
|
|
286
|
-
},
|
|
287
|
-
model: 'colSpan'
|
|
288
|
-
});
|
|
289
|
-
conversion.for('downcast').attributeToAttribute({
|
|
290
|
-
model: {
|
|
291
|
-
name: 'tableColumn',
|
|
292
|
-
key: 'columnWidth'
|
|
293
|
-
},
|
|
294
|
-
view: width => ({ key: 'style', value: { width } })
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Registers listeners to handle resizing process.
|
|
299
|
-
*/
|
|
300
|
-
_registerResizingListeners() {
|
|
301
|
-
const editingView = this.editor.editing.view;
|
|
302
|
-
editingView.addObserver(MouseEventsObserver);
|
|
303
|
-
editingView.document.on('mousedown', this._onMouseDownHandler.bind(this), { priority: 'high' });
|
|
304
|
-
this._domEmitter.listenTo(global.window.document, 'mousemove', throttle(this._onMouseMoveHandler.bind(this), 50));
|
|
305
|
-
this._domEmitter.listenTo(global.window.document, 'mouseup', this._onMouseUpHandler.bind(this));
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Handles the `mousedown` event on column resizer element:
|
|
309
|
-
* * calculates the initial column pixel widths,
|
|
310
|
-
* * inserts the `<colgroup>` element if it is not present in the `<table>`,
|
|
311
|
-
* * puts the necessary data in the temporary storage,
|
|
312
|
-
* * applies the attributes to the `<table>` view element.
|
|
313
|
-
*
|
|
314
|
-
* @param eventInfo An object containing information about the fired event.
|
|
315
|
-
* @param domEventData The data related to the DOM event.
|
|
316
|
-
*/
|
|
317
|
-
_onMouseDownHandler(eventInfo, domEventData) {
|
|
318
|
-
const target = domEventData.target;
|
|
319
|
-
if (!target.hasClass('ck-table-column-resizer')) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
if (!this._isResizingAllowed) {
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
const editor = this.editor;
|
|
326
|
-
const modelTable = editor.editing.mapper.toModelElement(target.findAncestor('figure'));
|
|
327
|
-
// Do not resize if table model is in non-editable place.
|
|
328
|
-
if (!editor.model.canEditAt(modelTable)) {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
domEventData.preventDefault();
|
|
332
|
-
eventInfo.stop();
|
|
333
|
-
// The column widths are calculated upon mousedown to allow lazy applying the `columnWidths` attribute on the table.
|
|
334
|
-
const columnWidthsInPx = _calculateDomColumnWidths(modelTable, this._tableUtilsPlugin, editor);
|
|
335
|
-
const viewTable = target.findAncestor('table');
|
|
336
|
-
const editingView = editor.editing.view;
|
|
337
|
-
// Insert colgroup for the table that is resized for the first time.
|
|
338
|
-
if (!Array.from(viewTable.getChildren()).find(viewCol => viewCol.is('element', 'colgroup'))) {
|
|
339
|
-
editingView.change(viewWriter => {
|
|
340
|
-
_insertColgroupElement(viewWriter, columnWidthsInPx, viewTable);
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
this._isResizingActive = true;
|
|
344
|
-
this._resizingData = this._getResizingData(domEventData, columnWidthsInPx);
|
|
345
|
-
// At this point we change only the editor view - we don't want other users to see our changes yet,
|
|
346
|
-
// so we can't apply them in the model.
|
|
347
|
-
editingView.change(writer => _applyResizingAttributesToTable(writer, viewTable, this._resizingData));
|
|
348
|
-
/**
|
|
349
|
-
* Calculates the DOM columns' widths. It is done by taking the width of the widest cell
|
|
350
|
-
* from each table column (we rely on the {@link module:table/tablewalker~TableWalker}
|
|
351
|
-
* to determine which column the cell belongs to).
|
|
352
|
-
*
|
|
353
|
-
* @param modelTable A table which columns should be measured.
|
|
354
|
-
* @param tableUtils The Table Utils plugin instance.
|
|
355
|
-
* @param editor The editor instance.
|
|
356
|
-
* @returns Columns' widths expressed in pixels (without unit).
|
|
357
|
-
*/
|
|
358
|
-
function _calculateDomColumnWidths(modelTable, tableUtilsPlugin, editor) {
|
|
359
|
-
const columnWidthsInPx = Array(tableUtilsPlugin.getColumns(modelTable));
|
|
360
|
-
const tableWalker = new TableWalker(modelTable);
|
|
361
|
-
for (const cellSlot of tableWalker) {
|
|
362
|
-
const viewCell = editor.editing.mapper.toViewElement(cellSlot.cell);
|
|
363
|
-
const domCell = editor.editing.view.domConverter.mapViewToDom(viewCell);
|
|
364
|
-
const domCellWidth = getDomCellOuterWidth(domCell);
|
|
365
|
-
if (!columnWidthsInPx[cellSlot.column] || domCellWidth < columnWidthsInPx[cellSlot.column]) {
|
|
366
|
-
columnWidthsInPx[cellSlot.column] = toPrecision(domCellWidth);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
return columnWidthsInPx;
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Creates a `<colgroup>` element with `<col>`s and inserts it into a given view table.
|
|
373
|
-
*
|
|
374
|
-
* @param viewWriter A writer instance.
|
|
375
|
-
* @param columnWidthsInPx Column widths.
|
|
376
|
-
* @param viewTable A table view element.
|
|
377
|
-
*/
|
|
378
|
-
function _insertColgroupElement(viewWriter, columnWidthsInPx, viewTable) {
|
|
379
|
-
const colgroup = viewWriter.createContainerElement('colgroup');
|
|
380
|
-
for (let i = 0; i < columnWidthsInPx.length; i++) {
|
|
381
|
-
const viewColElement = viewWriter.createEmptyElement('col');
|
|
382
|
-
const columnWidthInPc = `${toPrecision(columnWidthsInPx[i] / sumArray(columnWidthsInPx) * 100)}%`;
|
|
383
|
-
viewWriter.setStyle('width', columnWidthInPc, viewColElement);
|
|
384
|
-
viewWriter.insert(viewWriter.createPositionAt(colgroup, 'end'), viewColElement);
|
|
385
|
-
}
|
|
386
|
-
viewWriter.insert(viewWriter.createPositionAt(viewTable, 0), colgroup);
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Applies the style and classes to the view table as the resizing begun.
|
|
390
|
-
*
|
|
391
|
-
* @param viewWriter A writer instance.
|
|
392
|
-
* @param viewTable A table containing the clicked resizer.
|
|
393
|
-
* @param resizingData Data related to the resizing.
|
|
394
|
-
*/
|
|
395
|
-
function _applyResizingAttributesToTable(viewWriter, viewTable, resizingData) {
|
|
396
|
-
const figureInitialPcWidth = resizingData.widths.viewFigureWidth / resizingData.widths.viewFigureParentWidth;
|
|
397
|
-
viewWriter.addClass('ck-table-resized', viewTable);
|
|
398
|
-
viewWriter.addClass('ck-table-column-resizer__active', resizingData.elements.viewResizer);
|
|
399
|
-
viewWriter.setStyle('width', `${toPrecision(figureInitialPcWidth * 100)}%`, viewTable.findAncestor('figure'));
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Handles the `mousemove` event.
|
|
404
|
-
* * If resizing process is not in progress, it does nothing.
|
|
405
|
-
* * If resizing is active but not allowed, it stops the resizing process instantly calling the `mousedown` event handler.
|
|
406
|
-
* * Otherwise it dynamically updates the widths of the resized columns.
|
|
407
|
-
*
|
|
408
|
-
* @param eventInfo An object containing information about the fired event.
|
|
409
|
-
* @param mouseEventData The native DOM event.
|
|
410
|
-
*/
|
|
411
|
-
_onMouseMoveHandler(eventInfo, mouseEventData) {
|
|
412
|
-
if (!this._isResizingActive) {
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
|
-
if (!this._isResizingAllowed) {
|
|
416
|
-
this._onMouseUpHandler();
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
const { columnPosition, flags: { isRightEdge, isTableCentered, isLtrContent }, elements: { viewFigure, viewLeftColumn, viewRightColumn }, widths: { viewFigureParentWidth, tableWidth, leftColumnWidth, rightColumnWidth } } = this._resizingData;
|
|
420
|
-
const dxLowerBound = -leftColumnWidth + COLUMN_MIN_WIDTH_IN_PIXELS;
|
|
421
|
-
const dxUpperBound = isRightEdge ?
|
|
422
|
-
viewFigureParentWidth - tableWidth :
|
|
423
|
-
rightColumnWidth - COLUMN_MIN_WIDTH_IN_PIXELS;
|
|
424
|
-
// The multiplier is needed for calculating the proper movement offset:
|
|
425
|
-
// - it should negate the sign if content language direction is right-to-left,
|
|
426
|
-
// - it should double the offset if the table edge is resized and table is centered.
|
|
427
|
-
const multiplier = (isLtrContent ? 1 : -1) * (isRightEdge && isTableCentered ? 2 : 1);
|
|
428
|
-
const dx = clamp((mouseEventData.clientX - columnPosition) * multiplier, Math.min(dxLowerBound, 0), Math.max(dxUpperBound, 0));
|
|
429
|
-
if (dx === 0) {
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
this.editor.editing.view.change(writer => {
|
|
433
|
-
const leftColumnWidthAsPercentage = toPrecision((leftColumnWidth + dx) * 100 / tableWidth);
|
|
434
|
-
writer.setStyle('width', `${leftColumnWidthAsPercentage}%`, viewLeftColumn);
|
|
435
|
-
if (isRightEdge) {
|
|
436
|
-
const tableWidthAsPercentage = toPrecision((tableWidth + dx) * 100 / viewFigureParentWidth);
|
|
437
|
-
writer.setStyle('width', `${tableWidthAsPercentage}%`, viewFigure);
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
const rightColumnWidthAsPercentage = toPrecision((rightColumnWidth - dx) * 100 / tableWidth);
|
|
441
|
-
writer.setStyle('width', `${rightColumnWidthAsPercentage}%`, viewRightColumn);
|
|
442
|
-
}
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
/**
|
|
446
|
-
* Handles the `mouseup` event.
|
|
447
|
-
* * If resizing process is not in progress, it does nothing.
|
|
448
|
-
* * If resizing is active but not allowed, it cancels the resizing process restoring the original widths.
|
|
449
|
-
* * Otherwise it propagates the changes from view to the model by executing the adequate commands.
|
|
450
|
-
*/
|
|
451
|
-
_onMouseUpHandler() {
|
|
452
|
-
if (!this._isResizingActive) {
|
|
453
|
-
return;
|
|
454
|
-
}
|
|
455
|
-
const { viewResizer, modelTable, viewFigure, viewColgroup } = this._resizingData.elements;
|
|
456
|
-
const editor = this.editor;
|
|
457
|
-
const editingView = editor.editing.view;
|
|
458
|
-
const tableColumnGroup = this.getColumnGroupElement(modelTable);
|
|
459
|
-
const viewColumns = Array
|
|
460
|
-
.from(viewColgroup.getChildren())
|
|
461
|
-
.filter((column) => column.is('view:element'));
|
|
462
|
-
const columnWidthsAttributeOld = tableColumnGroup ?
|
|
463
|
-
this.getTableColumnsWidths(tableColumnGroup) :
|
|
464
|
-
null;
|
|
465
|
-
const columnWidthsAttributeNew = viewColumns.map(column => column.getStyle('width'));
|
|
466
|
-
const isColumnWidthsAttributeChanged = !isEqual(columnWidthsAttributeOld, columnWidthsAttributeNew);
|
|
467
|
-
const tableWidthAttributeOld = modelTable.getAttribute('tableWidth');
|
|
468
|
-
const tableWidthAttributeNew = viewFigure.getStyle('width');
|
|
469
|
-
const isTableWidthAttributeChanged = tableWidthAttributeOld !== tableWidthAttributeNew;
|
|
470
|
-
if (isColumnWidthsAttributeChanged || isTableWidthAttributeChanged) {
|
|
471
|
-
if (this._isResizingAllowed) {
|
|
472
|
-
editor.execute('resizeTableWidth', {
|
|
473
|
-
table: modelTable,
|
|
474
|
-
tableWidth: `${toPrecision(tableWidthAttributeNew)}%`,
|
|
475
|
-
columnWidths: columnWidthsAttributeNew
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
else {
|
|
479
|
-
// In read-only mode revert all changes in the editing view. The model is not touched so it does not need to be restored.
|
|
480
|
-
// This case can occur if the read-only mode kicks in during the resizing process.
|
|
481
|
-
editingView.change(writer => {
|
|
482
|
-
// If table had resized columns before, restore the previous column widths.
|
|
483
|
-
// Otherwise clean up the view from the temporary column resizing markup.
|
|
484
|
-
if (columnWidthsAttributeOld) {
|
|
485
|
-
for (const viewCol of viewColumns) {
|
|
486
|
-
writer.setStyle('width', columnWidthsAttributeOld.shift(), viewCol);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
else {
|
|
490
|
-
writer.remove(viewColgroup);
|
|
491
|
-
}
|
|
492
|
-
if (isTableWidthAttributeChanged) {
|
|
493
|
-
// If the whole table was already resized before, restore the previous table width.
|
|
494
|
-
// Otherwise clean up the view from the temporary table resizing markup.
|
|
495
|
-
if (tableWidthAttributeOld) {
|
|
496
|
-
writer.setStyle('width', tableWidthAttributeOld, viewFigure);
|
|
497
|
-
}
|
|
498
|
-
else {
|
|
499
|
-
writer.removeStyle('width', viewFigure);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
// If a table and its columns weren't resized before,
|
|
503
|
-
// prune the remaining common resizing markup.
|
|
504
|
-
if (!columnWidthsAttributeOld && !tableWidthAttributeOld) {
|
|
505
|
-
writer.removeClass('ck-table-resized', [...viewFigure.getChildren()].find(element => element.name === 'table'));
|
|
506
|
-
}
|
|
507
|
-
});
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
editingView.change(writer => {
|
|
511
|
-
writer.removeClass('ck-table-column-resizer__active', viewResizer);
|
|
512
|
-
});
|
|
513
|
-
this._isResizingActive = false;
|
|
514
|
-
this._resizingData = null;
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Retrieves and returns required data needed for the resizing process.
|
|
518
|
-
*
|
|
519
|
-
* @param domEventData The data of the `mousedown` event.
|
|
520
|
-
* @param columnWidths The current widths of the columns.
|
|
521
|
-
* @returns The data needed for the resizing process.
|
|
522
|
-
*/
|
|
523
|
-
_getResizingData(domEventData, columnWidths) {
|
|
524
|
-
const editor = this.editor;
|
|
525
|
-
const columnPosition = domEventData.domEvent.clientX;
|
|
526
|
-
const viewResizer = domEventData.target;
|
|
527
|
-
const viewLeftCell = viewResizer.findAncestor('td') || viewResizer.findAncestor('th');
|
|
528
|
-
const modelLeftCell = editor.editing.mapper.toModelElement(viewLeftCell);
|
|
529
|
-
const modelTable = modelLeftCell.findAncestor('table');
|
|
530
|
-
const leftColumnIndex = getColumnEdgesIndexes(modelLeftCell, this._tableUtilsPlugin).rightEdge;
|
|
531
|
-
const lastColumnIndex = this._tableUtilsPlugin.getColumns(modelTable) - 1;
|
|
532
|
-
const isRightEdge = leftColumnIndex === lastColumnIndex;
|
|
533
|
-
const isTableCentered = !modelTable.hasAttribute('tableAlignment');
|
|
534
|
-
const isLtrContent = editor.locale.contentLanguageDirection !== 'rtl';
|
|
535
|
-
const viewTable = viewLeftCell.findAncestor('table');
|
|
536
|
-
const viewFigure = viewTable.findAncestor('figure');
|
|
537
|
-
const viewColgroup = [...viewTable.getChildren()]
|
|
538
|
-
.find(viewCol => viewCol.is('element', 'colgroup'));
|
|
539
|
-
const viewLeftColumn = viewColgroup.getChild(leftColumnIndex);
|
|
540
|
-
const viewRightColumn = isRightEdge ? undefined : viewColgroup.getChild(leftColumnIndex + 1);
|
|
541
|
-
const viewFigureParentWidth = getElementWidthInPixels(editor.editing.view.domConverter.mapViewToDom(viewFigure.parent));
|
|
542
|
-
const viewFigureWidth = getElementWidthInPixels(editor.editing.view.domConverter.mapViewToDom(viewFigure));
|
|
543
|
-
const tableWidth = getTableWidthInPixels(modelTable, editor);
|
|
544
|
-
const leftColumnWidth = columnWidths[leftColumnIndex];
|
|
545
|
-
const rightColumnWidth = isRightEdge ? undefined : columnWidths[leftColumnIndex + 1];
|
|
546
|
-
return {
|
|
547
|
-
columnPosition,
|
|
548
|
-
flags: {
|
|
549
|
-
isRightEdge,
|
|
550
|
-
isTableCentered,
|
|
551
|
-
isLtrContent
|
|
552
|
-
},
|
|
553
|
-
elements: {
|
|
554
|
-
viewResizer,
|
|
555
|
-
modelTable,
|
|
556
|
-
viewFigure,
|
|
557
|
-
viewColgroup,
|
|
558
|
-
viewLeftColumn,
|
|
559
|
-
viewRightColumn
|
|
560
|
-
},
|
|
561
|
-
widths: {
|
|
562
|
-
viewFigureParentWidth,
|
|
563
|
-
viewFigureWidth,
|
|
564
|
-
tableWidth,
|
|
565
|
-
leftColumnWidth,
|
|
566
|
-
rightColumnWidth
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* Registers a listener ensuring that each resizable cell have a resizer handle.
|
|
572
|
-
*/
|
|
573
|
-
_registerResizerInserter() {
|
|
574
|
-
this.editor.conversion.for('editingDowncast').add(dispatcher => {
|
|
575
|
-
dispatcher.on('insert:tableCell', (evt, data, conversionApi) => {
|
|
576
|
-
const modelElement = data.item;
|
|
577
|
-
const viewElement = conversionApi.mapper.toViewElement(modelElement);
|
|
578
|
-
const viewWriter = conversionApi.writer;
|
|
579
|
-
viewWriter.insert(viewWriter.createPositionAt(viewElement, 'end'), viewWriter.createUIElement('div', { class: 'ck-table-column-resizer' }));
|
|
580
|
-
}, { priority: 'lowest' });
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
}
|
|
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/tablecolumnresize/tablecolumnresizeediting
|
|
7
|
+
*/
|
|
8
|
+
import { throttle, isEqual } from 'lodash-es';
|
|
9
|
+
import { global, DomEmitterMixin } from 'ckeditor5/src/utils';
|
|
10
|
+
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
+
import MouseEventsObserver from '../../src/tablemouse/mouseeventsobserver';
|
|
12
|
+
import TableEditing from '../tableediting';
|
|
13
|
+
import TableUtils from '../tableutils';
|
|
14
|
+
import TableWalker from '../tablewalker';
|
|
15
|
+
import TableWidthsCommand from './tablewidthscommand';
|
|
16
|
+
import { downcastTableResizedClass, upcastColgroupElement } from './converters';
|
|
17
|
+
import { clamp, createFilledArray, sumArray, getColumnEdgesIndexes, getChangedResizedTables, getColumnMinWidthAsPercentage, getElementWidthInPixels, getTableWidthInPixels, normalizeColumnWidths, toPrecision, getDomCellOuterWidth, updateColumnElements, getColumnGroupElement, getTableColumnElements, getTableColumnsWidths } from './utils';
|
|
18
|
+
import { COLUMN_MIN_WIDTH_IN_PIXELS } from './constants';
|
|
19
|
+
/**
|
|
20
|
+
* The table column resize editing plugin.
|
|
21
|
+
*/
|
|
22
|
+
export default class TableColumnResizeEditing extends Plugin {
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/
|
|
26
|
+
static get requires() {
|
|
27
|
+
return [TableEditing, TableUtils];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get pluginName() {
|
|
33
|
+
return 'TableColumnResizeEditing';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
constructor(editor) {
|
|
39
|
+
super(editor);
|
|
40
|
+
this._isResizingActive = false;
|
|
41
|
+
this.set('_isResizingAllowed', true);
|
|
42
|
+
this._resizingData = null;
|
|
43
|
+
this._domEmitter = new (DomEmitterMixin())();
|
|
44
|
+
this._tableUtilsPlugin = editor.plugins.get('TableUtils');
|
|
45
|
+
this.on('change:_isResizingAllowed', (evt, name, value) => {
|
|
46
|
+
// Toggling the `ck-column-resize_disabled` class shows and hides the resizers through CSS.
|
|
47
|
+
const classAction = value ? 'removeClass' : 'addClass';
|
|
48
|
+
editor.editing.view.change(writer => {
|
|
49
|
+
for (const root of editor.editing.view.document.roots) {
|
|
50
|
+
writer[classAction]('ck-column-resize_disabled', editor.editing.view.document.getRoot(root.rootName));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @inheritDoc
|
|
57
|
+
*/
|
|
58
|
+
init() {
|
|
59
|
+
this._extendSchema();
|
|
60
|
+
this._registerPostFixer();
|
|
61
|
+
this._registerConverters();
|
|
62
|
+
this._registerResizingListeners();
|
|
63
|
+
this._registerResizerInserter();
|
|
64
|
+
const editor = this.editor;
|
|
65
|
+
const columnResizePlugin = editor.plugins.get('TableColumnResize');
|
|
66
|
+
const tableEditing = editor.plugins.get('TableEditing');
|
|
67
|
+
tableEditing.registerAdditionalSlot({
|
|
68
|
+
filter: element => element.is('element', 'tableColumnGroup'),
|
|
69
|
+
positionOffset: 0
|
|
70
|
+
});
|
|
71
|
+
const tableWidthsCommand = new TableWidthsCommand(editor);
|
|
72
|
+
// For backwards compatibility we have two commands that perform exactly the same operation.
|
|
73
|
+
editor.commands.add('resizeTableWidth', tableWidthsCommand);
|
|
74
|
+
editor.commands.add('resizeColumnWidths', tableWidthsCommand);
|
|
75
|
+
// Currently the states of column resize and table resize (which is actually the last column resize) features
|
|
76
|
+
// are bound together. They can be separated in the future by adding distinct listeners and applying
|
|
77
|
+
// different CSS classes (e.g. `ck-column-resize_disabled` and `ck-table-resize_disabled`) to the editor root.
|
|
78
|
+
// See #12148 for the details.
|
|
79
|
+
this.bind('_isResizingAllowed').to(editor, 'isReadOnly', columnResizePlugin, 'isEnabled', tableWidthsCommand, 'isEnabled', (isEditorReadOnly, isPluginEnabled, isTableWidthsCommandCommandEnabled) => !isEditorReadOnly && isPluginEnabled && isTableWidthsCommandCommandEnabled);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @inheritDoc
|
|
83
|
+
*/
|
|
84
|
+
destroy() {
|
|
85
|
+
this._domEmitter.stopListening();
|
|
86
|
+
super.destroy();
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns a 'tableColumnGroup' element from the 'table'.
|
|
90
|
+
*
|
|
91
|
+
* @param element A 'table' or 'tableColumnGroup' element.
|
|
92
|
+
* @returns A 'tableColumnGroup' element.
|
|
93
|
+
*/
|
|
94
|
+
getColumnGroupElement(element) {
|
|
95
|
+
return getColumnGroupElement(element);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns an array of 'tableColumn' elements.
|
|
99
|
+
*
|
|
100
|
+
* @param element A 'table' or 'tableColumnGroup' element.
|
|
101
|
+
* @returns An array of 'tableColumn' elements.
|
|
102
|
+
*/
|
|
103
|
+
getTableColumnElements(element) {
|
|
104
|
+
return getTableColumnElements(element);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns an array of table column widths.
|
|
108
|
+
*
|
|
109
|
+
* @param element A 'table' or 'tableColumnGroup' element.
|
|
110
|
+
* @returns An array of table column widths.
|
|
111
|
+
*/
|
|
112
|
+
getTableColumnsWidths(element) {
|
|
113
|
+
return getTableColumnsWidths(element);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Registers new attributes for a table model element.
|
|
117
|
+
*/
|
|
118
|
+
_extendSchema() {
|
|
119
|
+
this.editor.model.schema.extend('table', {
|
|
120
|
+
allowAttributes: ['tableWidth']
|
|
121
|
+
});
|
|
122
|
+
this.editor.model.schema.register('tableColumnGroup', {
|
|
123
|
+
allowIn: 'table',
|
|
124
|
+
isLimit: true
|
|
125
|
+
});
|
|
126
|
+
this.editor.model.schema.register('tableColumn', {
|
|
127
|
+
allowIn: 'tableColumnGroup',
|
|
128
|
+
allowAttributes: ['columnWidth', 'colSpan'],
|
|
129
|
+
isLimit: true
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Registers table column resize post-fixer.
|
|
134
|
+
*
|
|
135
|
+
* It checks if the change from the differ concerns a table-related element or attribute. For detected changes it:
|
|
136
|
+
* * Adjusts the `columnWidths` attribute to guarantee that the sum of the widths from all columns is 100%.
|
|
137
|
+
* * Checks if the `columnWidths` attribute gets updated accordingly after columns have been added or removed.
|
|
138
|
+
*/
|
|
139
|
+
_registerPostFixer() {
|
|
140
|
+
const editor = this.editor;
|
|
141
|
+
const model = editor.model;
|
|
142
|
+
model.document.registerPostFixer(writer => {
|
|
143
|
+
let changed = false;
|
|
144
|
+
for (const table of getChangedResizedTables(model)) {
|
|
145
|
+
const tableColumnGroup = this.getColumnGroupElement(table);
|
|
146
|
+
const columns = this.getTableColumnElements(tableColumnGroup);
|
|
147
|
+
const columnWidths = this.getTableColumnsWidths(tableColumnGroup);
|
|
148
|
+
// Adjust the `columnWidths` attribute to guarantee that the sum of the widths from all columns is 100%.
|
|
149
|
+
let normalizedWidths = normalizeColumnWidths(columnWidths);
|
|
150
|
+
// If the number of columns has changed, then we need to adjust the widths of the affected columns.
|
|
151
|
+
normalizedWidths = adjustColumnWidths(normalizedWidths, table, this);
|
|
152
|
+
if (isEqual(columnWidths, normalizedWidths)) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
updateColumnElements(columns, tableColumnGroup, normalizedWidths, writer);
|
|
156
|
+
changed = true;
|
|
157
|
+
}
|
|
158
|
+
return changed;
|
|
159
|
+
});
|
|
160
|
+
/**
|
|
161
|
+
* Adjusts if necessary the `columnWidths` in case if the number of column has changed.
|
|
162
|
+
*
|
|
163
|
+
* @param columnWidths Note: this array **may be modified** by the function.
|
|
164
|
+
* @param table Table to be checked.
|
|
165
|
+
*/
|
|
166
|
+
function adjustColumnWidths(columnWidths, table, plugin) {
|
|
167
|
+
const newTableColumnsCount = plugin._tableUtilsPlugin.getColumns(table);
|
|
168
|
+
const columnsCountDelta = newTableColumnsCount - columnWidths.length;
|
|
169
|
+
if (columnsCountDelta === 0) {
|
|
170
|
+
return columnWidths;
|
|
171
|
+
}
|
|
172
|
+
const widths = columnWidths.map(width => Number(width.replace('%', '')));
|
|
173
|
+
// Collect all cells that are affected by the change.
|
|
174
|
+
const cellSet = getAffectedCells(plugin.editor.model.document.differ, table);
|
|
175
|
+
for (const cell of cellSet) {
|
|
176
|
+
const currentColumnsDelta = newTableColumnsCount - widths.length;
|
|
177
|
+
if (currentColumnsDelta === 0) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
// If the column count in the table changed, adjust the widths of the affected columns.
|
|
181
|
+
const hasMoreColumns = currentColumnsDelta > 0;
|
|
182
|
+
const currentColumnIndex = plugin._tableUtilsPlugin.getCellLocation(cell).column;
|
|
183
|
+
if (hasMoreColumns) {
|
|
184
|
+
const columnMinWidthAsPercentage = getColumnMinWidthAsPercentage(table, plugin.editor);
|
|
185
|
+
const columnWidthsToInsert = createFilledArray(currentColumnsDelta, columnMinWidthAsPercentage);
|
|
186
|
+
widths.splice(currentColumnIndex, 0, ...columnWidthsToInsert);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
// Moves the widths of the removed columns to the preceding one.
|
|
190
|
+
// Other editors either reduce the width of the whole table or adjust the widths
|
|
191
|
+
// proportionally, so change of this behavior can be considered in the future.
|
|
192
|
+
const removedColumnWidths = widths.splice(currentColumnIndex, Math.abs(currentColumnsDelta));
|
|
193
|
+
widths[currentColumnIndex] += sumArray(removedColumnWidths);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return widths.map(width => width + '%');
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns a set of cells that have been changed in a given table.
|
|
200
|
+
*/
|
|
201
|
+
function getAffectedCells(differ, table) {
|
|
202
|
+
const cellSet = new Set();
|
|
203
|
+
for (const change of differ.getChanges()) {
|
|
204
|
+
if (change.type == 'insert' &&
|
|
205
|
+
change.position.nodeAfter &&
|
|
206
|
+
change.position.nodeAfter.name == 'tableCell' &&
|
|
207
|
+
change.position.nodeAfter.getAncestors().includes(table)) {
|
|
208
|
+
cellSet.add(change.position.nodeAfter);
|
|
209
|
+
}
|
|
210
|
+
else if (change.type == 'remove') {
|
|
211
|
+
// If the first cell was removed, use the node after the change position instead.
|
|
212
|
+
const referenceNode = (change.position.nodeBefore || change.position.nodeAfter);
|
|
213
|
+
if (referenceNode.name == 'tableCell' && referenceNode.getAncestors().includes(table)) {
|
|
214
|
+
cellSet.add(referenceNode);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return cellSet;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Registers table column resize converters.
|
|
223
|
+
*/
|
|
224
|
+
_registerConverters() {
|
|
225
|
+
const editor = this.editor;
|
|
226
|
+
const conversion = editor.conversion;
|
|
227
|
+
// Table width style
|
|
228
|
+
conversion.for('upcast').attributeToAttribute({
|
|
229
|
+
view: {
|
|
230
|
+
name: 'figure',
|
|
231
|
+
key: 'style',
|
|
232
|
+
value: {
|
|
233
|
+
width: /[\s\S]+/
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
model: {
|
|
237
|
+
name: 'table',
|
|
238
|
+
key: 'tableWidth',
|
|
239
|
+
value: (viewElement) => viewElement.getStyle('width')
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
conversion.for('downcast').attributeToAttribute({
|
|
243
|
+
model: {
|
|
244
|
+
name: 'table',
|
|
245
|
+
key: 'tableWidth'
|
|
246
|
+
},
|
|
247
|
+
view: (width) => ({
|
|
248
|
+
name: 'figure',
|
|
249
|
+
key: 'style',
|
|
250
|
+
value: {
|
|
251
|
+
width
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
});
|
|
255
|
+
conversion.elementToElement({ model: 'tableColumnGroup', view: 'colgroup' });
|
|
256
|
+
conversion.elementToElement({ model: 'tableColumn', view: 'col' });
|
|
257
|
+
conversion.for('downcast').add(downcastTableResizedClass());
|
|
258
|
+
conversion.for('upcast').add(upcastColgroupElement(this._tableUtilsPlugin));
|
|
259
|
+
conversion.for('upcast').attributeToAttribute({
|
|
260
|
+
view: {
|
|
261
|
+
name: 'col',
|
|
262
|
+
styles: {
|
|
263
|
+
width: /.*/
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
model: {
|
|
267
|
+
key: 'columnWidth',
|
|
268
|
+
value: (viewElement) => {
|
|
269
|
+
const viewColWidth = viewElement.getStyle('width');
|
|
270
|
+
// 'pt' is the default unit for table column width pasted from MS Office.
|
|
271
|
+
// See https://github.com/ckeditor/ckeditor5/issues/14521#issuecomment-1662102889 for more details.
|
|
272
|
+
if (!viewColWidth || (!viewColWidth.endsWith('%') && !viewColWidth.endsWith('pt'))) {
|
|
273
|
+
return 'auto';
|
|
274
|
+
}
|
|
275
|
+
return viewColWidth;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
// The `col[span]` attribute is present in tables pasted from MS Excel. We use it to set the temporary `colSpan` model attribute,
|
|
280
|
+
// which is consumed during the `colgroup` element upcast.
|
|
281
|
+
// See https://github.com/ckeditor/ckeditor5/issues/14521#issuecomment-1662102889 for more details.
|
|
282
|
+
conversion.for('upcast').attributeToAttribute({
|
|
283
|
+
view: {
|
|
284
|
+
name: 'col',
|
|
285
|
+
key: 'span'
|
|
286
|
+
},
|
|
287
|
+
model: 'colSpan'
|
|
288
|
+
});
|
|
289
|
+
conversion.for('downcast').attributeToAttribute({
|
|
290
|
+
model: {
|
|
291
|
+
name: 'tableColumn',
|
|
292
|
+
key: 'columnWidth'
|
|
293
|
+
},
|
|
294
|
+
view: width => ({ key: 'style', value: { width } })
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Registers listeners to handle resizing process.
|
|
299
|
+
*/
|
|
300
|
+
_registerResizingListeners() {
|
|
301
|
+
const editingView = this.editor.editing.view;
|
|
302
|
+
editingView.addObserver(MouseEventsObserver);
|
|
303
|
+
editingView.document.on('mousedown', this._onMouseDownHandler.bind(this), { priority: 'high' });
|
|
304
|
+
this._domEmitter.listenTo(global.window.document, 'mousemove', throttle(this._onMouseMoveHandler.bind(this), 50));
|
|
305
|
+
this._domEmitter.listenTo(global.window.document, 'mouseup', this._onMouseUpHandler.bind(this));
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Handles the `mousedown` event on column resizer element:
|
|
309
|
+
* * calculates the initial column pixel widths,
|
|
310
|
+
* * inserts the `<colgroup>` element if it is not present in the `<table>`,
|
|
311
|
+
* * puts the necessary data in the temporary storage,
|
|
312
|
+
* * applies the attributes to the `<table>` view element.
|
|
313
|
+
*
|
|
314
|
+
* @param eventInfo An object containing information about the fired event.
|
|
315
|
+
* @param domEventData The data related to the DOM event.
|
|
316
|
+
*/
|
|
317
|
+
_onMouseDownHandler(eventInfo, domEventData) {
|
|
318
|
+
const target = domEventData.target;
|
|
319
|
+
if (!target.hasClass('ck-table-column-resizer')) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (!this._isResizingAllowed) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const editor = this.editor;
|
|
326
|
+
const modelTable = editor.editing.mapper.toModelElement(target.findAncestor('figure'));
|
|
327
|
+
// Do not resize if table model is in non-editable place.
|
|
328
|
+
if (!editor.model.canEditAt(modelTable)) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
domEventData.preventDefault();
|
|
332
|
+
eventInfo.stop();
|
|
333
|
+
// The column widths are calculated upon mousedown to allow lazy applying the `columnWidths` attribute on the table.
|
|
334
|
+
const columnWidthsInPx = _calculateDomColumnWidths(modelTable, this._tableUtilsPlugin, editor);
|
|
335
|
+
const viewTable = target.findAncestor('table');
|
|
336
|
+
const editingView = editor.editing.view;
|
|
337
|
+
// Insert colgroup for the table that is resized for the first time.
|
|
338
|
+
if (!Array.from(viewTable.getChildren()).find(viewCol => viewCol.is('element', 'colgroup'))) {
|
|
339
|
+
editingView.change(viewWriter => {
|
|
340
|
+
_insertColgroupElement(viewWriter, columnWidthsInPx, viewTable);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
this._isResizingActive = true;
|
|
344
|
+
this._resizingData = this._getResizingData(domEventData, columnWidthsInPx);
|
|
345
|
+
// At this point we change only the editor view - we don't want other users to see our changes yet,
|
|
346
|
+
// so we can't apply them in the model.
|
|
347
|
+
editingView.change(writer => _applyResizingAttributesToTable(writer, viewTable, this._resizingData));
|
|
348
|
+
/**
|
|
349
|
+
* Calculates the DOM columns' widths. It is done by taking the width of the widest cell
|
|
350
|
+
* from each table column (we rely on the {@link module:table/tablewalker~TableWalker}
|
|
351
|
+
* to determine which column the cell belongs to).
|
|
352
|
+
*
|
|
353
|
+
* @param modelTable A table which columns should be measured.
|
|
354
|
+
* @param tableUtils The Table Utils plugin instance.
|
|
355
|
+
* @param editor The editor instance.
|
|
356
|
+
* @returns Columns' widths expressed in pixels (without unit).
|
|
357
|
+
*/
|
|
358
|
+
function _calculateDomColumnWidths(modelTable, tableUtilsPlugin, editor) {
|
|
359
|
+
const columnWidthsInPx = Array(tableUtilsPlugin.getColumns(modelTable));
|
|
360
|
+
const tableWalker = new TableWalker(modelTable);
|
|
361
|
+
for (const cellSlot of tableWalker) {
|
|
362
|
+
const viewCell = editor.editing.mapper.toViewElement(cellSlot.cell);
|
|
363
|
+
const domCell = editor.editing.view.domConverter.mapViewToDom(viewCell);
|
|
364
|
+
const domCellWidth = getDomCellOuterWidth(domCell);
|
|
365
|
+
if (!columnWidthsInPx[cellSlot.column] || domCellWidth < columnWidthsInPx[cellSlot.column]) {
|
|
366
|
+
columnWidthsInPx[cellSlot.column] = toPrecision(domCellWidth);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return columnWidthsInPx;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Creates a `<colgroup>` element with `<col>`s and inserts it into a given view table.
|
|
373
|
+
*
|
|
374
|
+
* @param viewWriter A writer instance.
|
|
375
|
+
* @param columnWidthsInPx Column widths.
|
|
376
|
+
* @param viewTable A table view element.
|
|
377
|
+
*/
|
|
378
|
+
function _insertColgroupElement(viewWriter, columnWidthsInPx, viewTable) {
|
|
379
|
+
const colgroup = viewWriter.createContainerElement('colgroup');
|
|
380
|
+
for (let i = 0; i < columnWidthsInPx.length; i++) {
|
|
381
|
+
const viewColElement = viewWriter.createEmptyElement('col');
|
|
382
|
+
const columnWidthInPc = `${toPrecision(columnWidthsInPx[i] / sumArray(columnWidthsInPx) * 100)}%`;
|
|
383
|
+
viewWriter.setStyle('width', columnWidthInPc, viewColElement);
|
|
384
|
+
viewWriter.insert(viewWriter.createPositionAt(colgroup, 'end'), viewColElement);
|
|
385
|
+
}
|
|
386
|
+
viewWriter.insert(viewWriter.createPositionAt(viewTable, 0), colgroup);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Applies the style and classes to the view table as the resizing begun.
|
|
390
|
+
*
|
|
391
|
+
* @param viewWriter A writer instance.
|
|
392
|
+
* @param viewTable A table containing the clicked resizer.
|
|
393
|
+
* @param resizingData Data related to the resizing.
|
|
394
|
+
*/
|
|
395
|
+
function _applyResizingAttributesToTable(viewWriter, viewTable, resizingData) {
|
|
396
|
+
const figureInitialPcWidth = resizingData.widths.viewFigureWidth / resizingData.widths.viewFigureParentWidth;
|
|
397
|
+
viewWriter.addClass('ck-table-resized', viewTable);
|
|
398
|
+
viewWriter.addClass('ck-table-column-resizer__active', resizingData.elements.viewResizer);
|
|
399
|
+
viewWriter.setStyle('width', `${toPrecision(figureInitialPcWidth * 100)}%`, viewTable.findAncestor('figure'));
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Handles the `mousemove` event.
|
|
404
|
+
* * If resizing process is not in progress, it does nothing.
|
|
405
|
+
* * If resizing is active but not allowed, it stops the resizing process instantly calling the `mousedown` event handler.
|
|
406
|
+
* * Otherwise it dynamically updates the widths of the resized columns.
|
|
407
|
+
*
|
|
408
|
+
* @param eventInfo An object containing information about the fired event.
|
|
409
|
+
* @param mouseEventData The native DOM event.
|
|
410
|
+
*/
|
|
411
|
+
_onMouseMoveHandler(eventInfo, mouseEventData) {
|
|
412
|
+
if (!this._isResizingActive) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (!this._isResizingAllowed) {
|
|
416
|
+
this._onMouseUpHandler();
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const { columnPosition, flags: { isRightEdge, isTableCentered, isLtrContent }, elements: { viewFigure, viewLeftColumn, viewRightColumn }, widths: { viewFigureParentWidth, tableWidth, leftColumnWidth, rightColumnWidth } } = this._resizingData;
|
|
420
|
+
const dxLowerBound = -leftColumnWidth + COLUMN_MIN_WIDTH_IN_PIXELS;
|
|
421
|
+
const dxUpperBound = isRightEdge ?
|
|
422
|
+
viewFigureParentWidth - tableWidth :
|
|
423
|
+
rightColumnWidth - COLUMN_MIN_WIDTH_IN_PIXELS;
|
|
424
|
+
// The multiplier is needed for calculating the proper movement offset:
|
|
425
|
+
// - it should negate the sign if content language direction is right-to-left,
|
|
426
|
+
// - it should double the offset if the table edge is resized and table is centered.
|
|
427
|
+
const multiplier = (isLtrContent ? 1 : -1) * (isRightEdge && isTableCentered ? 2 : 1);
|
|
428
|
+
const dx = clamp((mouseEventData.clientX - columnPosition) * multiplier, Math.min(dxLowerBound, 0), Math.max(dxUpperBound, 0));
|
|
429
|
+
if (dx === 0) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
this.editor.editing.view.change(writer => {
|
|
433
|
+
const leftColumnWidthAsPercentage = toPrecision((leftColumnWidth + dx) * 100 / tableWidth);
|
|
434
|
+
writer.setStyle('width', `${leftColumnWidthAsPercentage}%`, viewLeftColumn);
|
|
435
|
+
if (isRightEdge) {
|
|
436
|
+
const tableWidthAsPercentage = toPrecision((tableWidth + dx) * 100 / viewFigureParentWidth);
|
|
437
|
+
writer.setStyle('width', `${tableWidthAsPercentage}%`, viewFigure);
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
const rightColumnWidthAsPercentage = toPrecision((rightColumnWidth - dx) * 100 / tableWidth);
|
|
441
|
+
writer.setStyle('width', `${rightColumnWidthAsPercentage}%`, viewRightColumn);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Handles the `mouseup` event.
|
|
447
|
+
* * If resizing process is not in progress, it does nothing.
|
|
448
|
+
* * If resizing is active but not allowed, it cancels the resizing process restoring the original widths.
|
|
449
|
+
* * Otherwise it propagates the changes from view to the model by executing the adequate commands.
|
|
450
|
+
*/
|
|
451
|
+
_onMouseUpHandler() {
|
|
452
|
+
if (!this._isResizingActive) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const { viewResizer, modelTable, viewFigure, viewColgroup } = this._resizingData.elements;
|
|
456
|
+
const editor = this.editor;
|
|
457
|
+
const editingView = editor.editing.view;
|
|
458
|
+
const tableColumnGroup = this.getColumnGroupElement(modelTable);
|
|
459
|
+
const viewColumns = Array
|
|
460
|
+
.from(viewColgroup.getChildren())
|
|
461
|
+
.filter((column) => column.is('view:element'));
|
|
462
|
+
const columnWidthsAttributeOld = tableColumnGroup ?
|
|
463
|
+
this.getTableColumnsWidths(tableColumnGroup) :
|
|
464
|
+
null;
|
|
465
|
+
const columnWidthsAttributeNew = viewColumns.map(column => column.getStyle('width'));
|
|
466
|
+
const isColumnWidthsAttributeChanged = !isEqual(columnWidthsAttributeOld, columnWidthsAttributeNew);
|
|
467
|
+
const tableWidthAttributeOld = modelTable.getAttribute('tableWidth');
|
|
468
|
+
const tableWidthAttributeNew = viewFigure.getStyle('width');
|
|
469
|
+
const isTableWidthAttributeChanged = tableWidthAttributeOld !== tableWidthAttributeNew;
|
|
470
|
+
if (isColumnWidthsAttributeChanged || isTableWidthAttributeChanged) {
|
|
471
|
+
if (this._isResizingAllowed) {
|
|
472
|
+
editor.execute('resizeTableWidth', {
|
|
473
|
+
table: modelTable,
|
|
474
|
+
tableWidth: `${toPrecision(tableWidthAttributeNew)}%`,
|
|
475
|
+
columnWidths: columnWidthsAttributeNew
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
// In read-only mode revert all changes in the editing view. The model is not touched so it does not need to be restored.
|
|
480
|
+
// This case can occur if the read-only mode kicks in during the resizing process.
|
|
481
|
+
editingView.change(writer => {
|
|
482
|
+
// If table had resized columns before, restore the previous column widths.
|
|
483
|
+
// Otherwise clean up the view from the temporary column resizing markup.
|
|
484
|
+
if (columnWidthsAttributeOld) {
|
|
485
|
+
for (const viewCol of viewColumns) {
|
|
486
|
+
writer.setStyle('width', columnWidthsAttributeOld.shift(), viewCol);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
writer.remove(viewColgroup);
|
|
491
|
+
}
|
|
492
|
+
if (isTableWidthAttributeChanged) {
|
|
493
|
+
// If the whole table was already resized before, restore the previous table width.
|
|
494
|
+
// Otherwise clean up the view from the temporary table resizing markup.
|
|
495
|
+
if (tableWidthAttributeOld) {
|
|
496
|
+
writer.setStyle('width', tableWidthAttributeOld, viewFigure);
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
writer.removeStyle('width', viewFigure);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// If a table and its columns weren't resized before,
|
|
503
|
+
// prune the remaining common resizing markup.
|
|
504
|
+
if (!columnWidthsAttributeOld && !tableWidthAttributeOld) {
|
|
505
|
+
writer.removeClass('ck-table-resized', [...viewFigure.getChildren()].find(element => element.name === 'table'));
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
editingView.change(writer => {
|
|
511
|
+
writer.removeClass('ck-table-column-resizer__active', viewResizer);
|
|
512
|
+
});
|
|
513
|
+
this._isResizingActive = false;
|
|
514
|
+
this._resizingData = null;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Retrieves and returns required data needed for the resizing process.
|
|
518
|
+
*
|
|
519
|
+
* @param domEventData The data of the `mousedown` event.
|
|
520
|
+
* @param columnWidths The current widths of the columns.
|
|
521
|
+
* @returns The data needed for the resizing process.
|
|
522
|
+
*/
|
|
523
|
+
_getResizingData(domEventData, columnWidths) {
|
|
524
|
+
const editor = this.editor;
|
|
525
|
+
const columnPosition = domEventData.domEvent.clientX;
|
|
526
|
+
const viewResizer = domEventData.target;
|
|
527
|
+
const viewLeftCell = viewResizer.findAncestor('td') || viewResizer.findAncestor('th');
|
|
528
|
+
const modelLeftCell = editor.editing.mapper.toModelElement(viewLeftCell);
|
|
529
|
+
const modelTable = modelLeftCell.findAncestor('table');
|
|
530
|
+
const leftColumnIndex = getColumnEdgesIndexes(modelLeftCell, this._tableUtilsPlugin).rightEdge;
|
|
531
|
+
const lastColumnIndex = this._tableUtilsPlugin.getColumns(modelTable) - 1;
|
|
532
|
+
const isRightEdge = leftColumnIndex === lastColumnIndex;
|
|
533
|
+
const isTableCentered = !modelTable.hasAttribute('tableAlignment');
|
|
534
|
+
const isLtrContent = editor.locale.contentLanguageDirection !== 'rtl';
|
|
535
|
+
const viewTable = viewLeftCell.findAncestor('table');
|
|
536
|
+
const viewFigure = viewTable.findAncestor('figure');
|
|
537
|
+
const viewColgroup = [...viewTable.getChildren()]
|
|
538
|
+
.find(viewCol => viewCol.is('element', 'colgroup'));
|
|
539
|
+
const viewLeftColumn = viewColgroup.getChild(leftColumnIndex);
|
|
540
|
+
const viewRightColumn = isRightEdge ? undefined : viewColgroup.getChild(leftColumnIndex + 1);
|
|
541
|
+
const viewFigureParentWidth = getElementWidthInPixels(editor.editing.view.domConverter.mapViewToDom(viewFigure.parent));
|
|
542
|
+
const viewFigureWidth = getElementWidthInPixels(editor.editing.view.domConverter.mapViewToDom(viewFigure));
|
|
543
|
+
const tableWidth = getTableWidthInPixels(modelTable, editor);
|
|
544
|
+
const leftColumnWidth = columnWidths[leftColumnIndex];
|
|
545
|
+
const rightColumnWidth = isRightEdge ? undefined : columnWidths[leftColumnIndex + 1];
|
|
546
|
+
return {
|
|
547
|
+
columnPosition,
|
|
548
|
+
flags: {
|
|
549
|
+
isRightEdge,
|
|
550
|
+
isTableCentered,
|
|
551
|
+
isLtrContent
|
|
552
|
+
},
|
|
553
|
+
elements: {
|
|
554
|
+
viewResizer,
|
|
555
|
+
modelTable,
|
|
556
|
+
viewFigure,
|
|
557
|
+
viewColgroup,
|
|
558
|
+
viewLeftColumn,
|
|
559
|
+
viewRightColumn
|
|
560
|
+
},
|
|
561
|
+
widths: {
|
|
562
|
+
viewFigureParentWidth,
|
|
563
|
+
viewFigureWidth,
|
|
564
|
+
tableWidth,
|
|
565
|
+
leftColumnWidth,
|
|
566
|
+
rightColumnWidth
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Registers a listener ensuring that each resizable cell have a resizer handle.
|
|
572
|
+
*/
|
|
573
|
+
_registerResizerInserter() {
|
|
574
|
+
this.editor.conversion.for('editingDowncast').add(dispatcher => {
|
|
575
|
+
dispatcher.on('insert:tableCell', (evt, data, conversionApi) => {
|
|
576
|
+
const modelElement = data.item;
|
|
577
|
+
const viewElement = conversionApi.mapper.toViewElement(modelElement);
|
|
578
|
+
const viewWriter = conversionApi.writer;
|
|
579
|
+
viewWriter.insert(viewWriter.createPositionAt(viewElement, 'end'), viewWriter.createUIElement('div', { class: 'ck-table-column-resizer' }));
|
|
580
|
+
}, { priority: 'lowest' });
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
}
|