UNPKG

3.46 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, 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/tablecellproperties/commands/tablecellpropertycommand
7 */
8import { Command } from 'ckeditor5/src/core.js';
9/**
10 * The table cell attribute command.
11 *
12 * The command is a base command for other table cell property commands.
13 */
14export default class TableCellPropertyCommand extends Command {
15 /**
16 * Creates a new `TableCellPropertyCommand` instance.
17 *
18 * @param editor An editor in which this command will be used.
19 * @param attributeName Table cell attribute name.
20 * @param defaultValue The default value of the attribute.
21 */
22 constructor(editor, attributeName, defaultValue) {
23 super(editor);
24 this.attributeName = attributeName;
25 this._defaultValue = defaultValue;
26 }
27 /**
28 * @inheritDoc
29 */
30 refresh() {
31 const editor = this.editor;
32 const tableUtils = this.editor.plugins.get('TableUtils');
33 const selectedTableCells = tableUtils.getSelectionAffectedTableCells(editor.model.document.selection);
34 this.isEnabled = !!selectedTableCells.length;
35 this.value = this._getSingleValue(selectedTableCells);
36 }
37 /**
38 * Executes the command.
39 *
40 * @fires execute
41 * @param options.value If set, the command will set the attribute on selected table cells.
42 * If it is not set, the command will remove the attribute from the selected table cells.
43 * @param options.batch Pass the model batch instance to the command to aggregate changes,
44 * for example to allow a single undo step for multiple executions.
45 */
46 execute(options = {}) {
47 const { value, batch } = options;
48 const model = this.editor.model;
49 const tableUtils = this.editor.plugins.get('TableUtils');
50 const tableCells = tableUtils.getSelectionAffectedTableCells(model.document.selection);
51 const valueToSet = this._getValueToSet(value);
52 model.enqueueChange(batch, writer => {
53 if (valueToSet) {
54 tableCells.forEach(tableCell => writer.setAttribute(this.attributeName, valueToSet, tableCell));
55 }
56 else {
57 tableCells.forEach(tableCell => writer.removeAttribute(this.attributeName, tableCell));
58 }
59 });
60 }
61 /**
62 * Returns the attribute value for a table cell.
63 */
64 _getAttribute(tableCell) {
65 if (!tableCell) {
66 return;
67 }
68 const value = tableCell.getAttribute(this.attributeName);
69 if (value === this._defaultValue) {
70 return;
71 }
72 return value;
73 }
74 /**
75 * Returns the proper model value. It can be used to add a default unit to numeric values.
76 */
77 _getValueToSet(value) {
78 if (value === this._defaultValue) {
79 return;
80 }
81 return value;
82 }
83 /**
84 * Returns a single value for all selected table cells. If the value is the same for all cells,
85 * it will be returned (`undefined` otherwise).
86 */
87 _getSingleValue(tableCells) {
88 const firstCellValue = this._getAttribute(tableCells[0]);
89 const everyCellHasAttribute = tableCells.every(tableCells => this._getAttribute(tableCells) === firstCellValue);
90 return everyCellHasAttribute ? firstCellValue : undefined;
91 }
92}