-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
tablewidthscommand.ts
98 lines (82 loc) · 2.82 KB
/
tablewidthscommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tablecolumnresize/tablewidthscommand
*/
import type { Element } from 'ckeditor5/src/engine';
import { Command } from 'ckeditor5/src/core';
import { normalizeColumnWidths } from './utils';
/**
* Command used by the {@link module:table/tablecolumnresize~TableColumnResize Table column resize feature} that
* updates the width of the whole table as well as its individual columns.
*/
export default class TableWidthsCommand extends Command {
/**
* @inheritDoc
*/
public override refresh(): void {
// The command is always enabled as it doesn't care about the actual selection - table can be resized
// even if the selection is elsewhere.
this.isEnabled = true;
}
/**
* Updated the `tableWidth` attribute of the table and the `columnWidth` attribute of the columns of that table.
*/
public override execute( options: TableWidthsCommandOptions = {} ): void {
const { model, plugins } = this.editor;
let {
table = model.document.selection.getSelectedElement()!,
columnWidths,
tableWidth
} = options;
if ( columnWidths ) {
// For backwards compatibility, columnWidths might be an array or a string of comma-separated values.
columnWidths = Array.isArray( columnWidths ) ?
columnWidths :
columnWidths.split( ',' );
}
model.change( writer => {
if ( tableWidth ) {
writer.setAttribute( 'tableWidth', tableWidth, table );
} else {
writer.removeAttribute( 'tableWidth', table );
}
const tableColumnGroup = plugins
.get( 'TableColumnResizeEditing' )
.getColumnGroupElement( table )!;
if ( !columnWidths && !tableColumnGroup ) {
return;
}
if ( !columnWidths ) {
return writer.remove( tableColumnGroup );
}
const widths = normalizeColumnWidths( columnWidths as Array<string> );
if ( !tableColumnGroup ) {
const colGroupElement = writer.createElement( 'tableColumnGroup' );
widths.forEach( columnWidth => writer.appendElement( 'tableColumn', { columnWidth }, colGroupElement ) );
writer.append( colGroupElement, table );
} else {
Array
.from( tableColumnGroup.getChildren() )
.forEach( ( column, index ) => writer.setAttribute( 'columnWidth', widths[ index ], column ) );
}
} );
}
}
export interface TableWidthsCommandOptions {
/**
* New value of the `columnWidths` attribute. Must be array of strings or string with comma-separated values.
* If skipped, the column widths information will be deleted.
*/
columnWidths?: Array<string> | string;
/**
* The new table width. If skipped, the model attribute will be removed.
*/
tableWidth?: string;
/**
* The table that is having the columns resized.
*/
table?: Element;
}