|
|
@@ -7,9 +7,7 @@
|
|
|
* @module table/tablecellproperties/commands/tablecellbackgroundcolorcommand
|
|
|
*/
|
|
|
|
|
|
-import Command from '@ckeditor/ckeditor5-core/src/command';
|
|
|
-
|
|
|
-import { findAncestor } from '../../commands/utils';
|
|
|
+import TableCellPropertyCommand from './tablecellpropertycommand';
|
|
|
|
|
|
/**
|
|
|
* The table cell background color command.
|
|
|
@@ -17,65 +15,21 @@ import { findAncestor } from '../../commands/utils';
|
|
|
* The command is registered by {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing} as
|
|
|
* `'tableCellBackgroundColor'` editor command.
|
|
|
*
|
|
|
- * To change cell backgroundColor of the selected cell, execute the command:
|
|
|
+ * To change cell `backgroundColor` attribute of the selected cells, execute the command:
|
|
|
*
|
|
|
* editor.execute( 'tableCellBackgroundColor', {
|
|
|
- * value: '5px'
|
|
|
+ * value: '#f00'
|
|
|
* } );
|
|
|
*
|
|
|
* @extends module:core/command~Command
|
|
|
*/
|
|
|
-export default class TableCellBackgroundColorCommand extends Command {
|
|
|
- constructor( editor ) {
|
|
|
- super( editor );
|
|
|
-
|
|
|
- this.attributeName = 'backgroundColor';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- refresh() {
|
|
|
- const editor = this.editor;
|
|
|
- const selection = editor.model.document.selection;
|
|
|
-
|
|
|
- const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
|
|
|
-
|
|
|
- this.isEnabled = !!tableCell;
|
|
|
- this.value = this._getValue( tableCell );
|
|
|
- }
|
|
|
-
|
|
|
- _getValue( tableCell ) {
|
|
|
- if ( !tableCell ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- return tableCell.getAttribute( this.attributeName );
|
|
|
- }
|
|
|
-
|
|
|
+export default class TableCellBackgroundColorCommand extends TableCellPropertyCommand {
|
|
|
/**
|
|
|
- * Executes the command.
|
|
|
+ * Creates a new `TableCellBackgroundColorCommand` instance.
|
|
|
*
|
|
|
- * @fires execute
|
|
|
- * @param {Object} [options]
|
|
|
- * @param {Boolean} [options.value] If set the command will set backgroundColor.
|
|
|
- * If backgroundColor is not set the command will remove the attribute.
|
|
|
+ * @param {module:core/editor/editor~Editor} editor Editor on which this command will be used.
|
|
|
*/
|
|
|
- execute( options = {} ) {
|
|
|
- const model = this.editor.model;
|
|
|
- const selection = model.document.selection;
|
|
|
-
|
|
|
- const { value, batch } = options;
|
|
|
-
|
|
|
- const tableCells = Array.from( selection.getSelectedBlocks() )
|
|
|
- .map( element => findAncestor( 'tableCell', model.createPositionAt( element, 0 ) ) );
|
|
|
-
|
|
|
- model.enqueueChange( batch || 'default', writer => {
|
|
|
- if ( value ) {
|
|
|
- tableCells.forEach( tableCell => writer.setAttribute( this.attributeName, value, tableCell ) );
|
|
|
- } else {
|
|
|
- tableCells.forEach( tableCell => writer.removeAttribute( this.attributeName, tableCell ) );
|
|
|
- }
|
|
|
- } );
|
|
|
+ constructor( editor ) {
|
|
|
+ super( editor, 'backgroundColor' );
|
|
|
}
|
|
|
}
|