8
0
Quellcode durchsuchen

Add table background color command.

Maciej Gołaszewski vor 5 Jahren
Ursprung
Commit
7df094d6bb

+ 81 - 0
packages/ckeditor5-table/src/commands/tablebackgroundcolorcommand.js

@@ -0,0 +1,81 @@
+/**
+ * @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/commands/tablebackgroundcolorcommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+
+import { findAncestor } from './utils';
+
+/**
+ * The table background color command.
+ *
+ * The command is registered by {@link module:table/tablepropertiesediting~TablePropertiesEditing} as
+ * `'tableBackgroundColor'` editor command.
+ *
+ * To change backgroundColor of the selected, execute the command:
+ *
+ *		editor.execute( 'tableBackgroundColor', {
+ *			value: '5px'
+ *		} );
+ *
+ * @extends module:core/command~Command
+ */
+export default class TableBackgroundColorCommand extends Command {
+	constructor( editor ) {
+		super( editor );
+
+		this.attributeName = 'backgroundColor';
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const editor = this.editor;
+		const selection = editor.model.document.selection;
+
+		const table = Array.from( selection.getSelectedBlocks() ).find( block => block.is( 'table' ) );
+
+		this.isEnabled = !!table;
+		this.value = table && this._getValue( table );
+	}
+
+	_getValue( table ) {
+		if ( !table ) {
+			return;
+		}
+
+		return table.getAttribute( this.attributeName );
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @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.
+	 */
+	execute( options = {} ) {
+		const model = this.editor.model;
+		const selection = model.document.selection;
+
+		const { value } = options;
+
+		const tables = Array.from( selection.getSelectedBlocks() )
+			.map( element => findAncestor( 'table', model.createPositionAt( element, 0 ) ) );
+
+		model.change( writer => {
+			if ( value ) {
+				tables.forEach( table => writer.setAttribute( this.attributeName, value, table ) );
+			} else {
+				tables.forEach( table => writer.removeAttribute( this.attributeName, table ) );
+			}
+		} );
+	}
+}

+ 3 - 0
packages/ckeditor5-table/src/tablepropertiesediting.js

@@ -13,6 +13,7 @@ import { addBackgroundRules } from '@ckeditor/ckeditor5-engine/src/view/styles/b
 
 import { downcastTableAttribute, upcastStyleToAttribute, upcastBorderStyles } from './converters/tableproperties';
 import TableEditing from './tableediting';
+import TableBackgroundColorCommand from './commands/tablebackgroundcolorcommand';
 
 /**
  * The table properties editing feature.
@@ -50,7 +51,9 @@ export default class TablePropertiesEditing extends Plugin {
 		enableAlignmentProperty( schema, conversion );
 		enableProperty( schema, conversion, 'width', 'width' );
 		enableProperty( schema, conversion, 'height', 'height' );
+
 		enableProperty( schema, conversion, 'backgroundColor', 'background-color' );
+		editor.commands.add( 'tableBackgroundColor', new TableBackgroundColorCommand( editor ) );
 	}
 }