/
tablecellpropertycommand.js
110 lines (96 loc) · 3.2 KB
/
tablecellpropertycommand.js
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
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tablecellproperties/commands/tablecellpropertycommand
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import { getSelectionAffectedTableCells } from '../../utils/selection';
/**
* The table cell attribute command.
*
* The command is a base command for other table cell property commands.
*
* @extends module:core/command~Command
*/
export default class TableCellPropertyCommand extends Command {
/**
* Creates a new `TableCellPropertyCommand` instance.
*
* @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
* @param {String} attributeName Table cell attribute name.
*/
constructor( editor, attributeName ) {
super( editor );
this.attributeName = attributeName;
}
/**
* @inheritDoc
*/
refresh() {
const editor = this.editor;
const selectedTableCells = getSelectionAffectedTableCells( editor.model.document.selection );
this.isEnabled = !!selectedTableCells.length;
this.value = this._getSingleValue( selectedTableCells );
}
/**
* Executes the command.
*
* @fires execute
* @param {Object} [options]
* @param {*} [options.value] If set, the command will set the attribute on selected table cells.
* If it is not set, the command will remove the attribute from the selected table cells.
* @param {module:engine/model/batch~Batch} [options.batch] Pass the model batch instance to the command to aggregate changes,
* for example to allow a single undo step for multiple executions.
*/
execute( options = {} ) {
const { value, batch } = options;
const model = this.editor.model;
const tableCells = getSelectionAffectedTableCells( model.document.selection );
const valueToSet = this._getValueToSet( value );
model.enqueueChange( batch || 'default', writer => {
if ( valueToSet ) {
tableCells.forEach( tableCell => writer.setAttribute( this.attributeName, valueToSet, tableCell ) );
} else {
tableCells.forEach( tableCell => writer.removeAttribute( this.attributeName, tableCell ) );
}
} );
}
/**
* Returns the attribute value for a table cell.
*
* @param {module:engine/model/element~Element} tableCell
* @returns {String|undefined}
* @private
*/
_getAttribute( tableCell ) {
if ( !tableCell ) {
return;
}
return tableCell.getAttribute( this.attributeName );
}
/**
* Returns the proper model value. It can be used to add a default unit to numeric values.
*
* @private
* @param {*} value
* @returns {*}
*/
_getValueToSet( value ) {
return value;
}
/**
* Returns a single value for all selected table cells. If the value is the same for all cells,
* it will be returned (`undefined` otherwise).
*
* @param {Array.<module:engine/model/element~Element>} tableCell
* @returns {*}
* @private
*/
_getSingleValue( tableCell ) {
const firstCellValue = this._getAttribute( tableCell[ 0 ] );
const everyCellHasAttribute = tableCell.every( tableCell => this._getAttribute( tableCell ) === firstCellValue );
return everyCellHasAttribute ? firstCellValue : undefined;
}
}