浏览代码

Add tableCellPaddingCommand.

Maciej Gołaszewski 5 年之前
父节点
当前提交
a4d38798e7

+ 95 - 0
packages/ckeditor5-table/src/commands/tablecellpaddingcommand.js

@@ -0,0 +1,95 @@
+/**
+ * @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/tablecellpaddingcommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import { isObject } from 'lodash-es';
+
+import { findAncestor } from './utils';
+
+/**
+ * The table cell padding command.
+ *
+ * The command is registered by {@link module:table/tablecellpropertiesediting~TableCellPropertiesEditing} as
+ * `'tableCellPadding'` editor command.
+ *
+ * To change cell padding of the selected cell, execute the command:
+ *
+ *		editor.execute( 'tableCellPadding', {
+ *			value: '5px'
+ *		} );
+ *
+ * @extends module:core/command~Command
+ */
+export default class TableCellPaddingCommand extends Command {
+	constructor( editor ) {
+		super( editor );
+
+		this.attributeName = 'padding';
+	}
+
+	/**
+	 * @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;
+		}
+
+		const padding = tableCell.getAttribute( 'padding' );
+
+		return getSingleValue( padding );
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @fires execute
+	 * @param {Object} [options]
+	 * @param {Boolean} [options.value] If set the command will set padding. If padding 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 tableCells = Array.from( selection.getSelectedBlocks() )
+			.map( element => findAncestor( 'tableCell', model.createPositionAt( element, 0 ) ) );
+
+		model.change( writer => {
+			if ( value ) {
+				tableCells.forEach( tableCell => writer.setAttribute( this.attributeName, value, tableCell ) );
+			} else {
+				tableCells.forEach( tableCell => writer.removeAttribute( this.attributeName, tableCell ) );
+			}
+		} );
+	}
+}
+
+function getSingleValue( objectOrString ) {
+	if ( !objectOrString || !isObject( objectOrString ) ) {
+		return objectOrString;
+	}
+
+	const { top, right, bottom, left } = objectOrString;
+
+	if ( top == right && right == bottom && bottom == left ) {
+		return top;
+	}
+}

+ 2 - 0
packages/ckeditor5-table/src/tablecellpropertiesediting.js

@@ -13,6 +13,7 @@ import { addBorderRules } from '@ckeditor/ckeditor5-engine/src/view/styles/borde
 import { addPaddingRules } from '@ckeditor/ckeditor5-engine/src/view/styles/padding';
 import { addBackgroundRules } from '@ckeditor/ckeditor5-engine/src/view/styles/background';
 import TableEditing from './tableediting';
+import TableCellPaddingCommand from './commands/tablecellpaddingcommand';
 
 /**
  * The table cell properties editing feature.
@@ -60,6 +61,7 @@ export default class TableCellPropertiesEditing extends Plugin {
 		enableProperty( schema, conversion, 'width', 'width' );
 		enableProperty( schema, conversion, 'height', 'height' );
 		enableProperty( schema, conversion, 'padding', 'padding' );
+		editor.commands.add( 'tableCellPadding', new TableCellPaddingCommand( editor ) );
 		enableProperty( schema, conversion, 'backgroundColor', 'background-color' );
 		enableProperty( schema, conversion, 'verticalAlignment', 'vertical-align' );
 	}