Ver código fonte

Add alignment commands.

Maciej Gołaszewski 5 anos atrás
pai
commit
87bde84cd7

+ 1 - 1
packages/ckeditor5-table/src/commands/tablecellbackgroundcolorcommand.js

@@ -4,7 +4,7 @@
  */
 
 /**
- * @module table/commands/tablecellbackgroundColorcommand
+ * @module table/commands/tablecellbackgroundcolorcommand
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';

+ 81 - 0
packages/ckeditor5-table/src/commands/tablecellhorizontalalignmentcommand.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/tablecellhorizontalalignmentcommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+
+import { findAncestor } from './utils';
+
+/**
+ * The table cell horizontal alignment command.
+ *
+ * The command is registered by {@link module:table/tablecellpropertiesediting~TableCellPropertiesEditing} as
+ * `'tableCellHorizontalAlignment'` editor command.
+ *
+ * To change cell horizontal alignment of the selected cell, execute the command:
+ *
+ *		editor.execute( 'tableCellHorizontalAlignment', {
+ *			value: '5px'
+ *		} );
+ *
+ * @extends module:core/command~Command
+ */
+export default class TableCellHorizontalAlignmentCommand extends Command {
+	constructor( editor ) {
+		super( editor );
+
+		this.attributeName = 'horizontalAlignment';
+	}
+
+	/**
+	 * @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 );
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @fires execute
+	 * @param {Object} [options]
+	 * @param {Boolean} [options.value] If set the command will set horizontal alignment.
+	 * If horizontal alignment 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 ) );
+			}
+		} );
+	}
+}

+ 81 - 0
packages/ckeditor5-table/src/commands/tablecellverticalalignmentcommand.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/tablecellverticalalignmentcommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+
+import { findAncestor } from './utils';
+
+/**
+ * The table cell vertical alignment command.
+ *
+ * The command is registered by {@link module:table/tablecellpropertiesediting~TableCellPropertiesEditing} as
+ * `'tableCellVerticalAlignment'` editor command.
+ *
+ * To change cell vertical alignment of the selected cell, execute the command:
+ *
+ *		editor.execute( 'tableCellVerticalAlignment', {
+ *			value: '5px'
+ *		} );
+ *
+ * @extends module:core/command~Command
+ */
+export default class TableCellVerticalAlignmentCommand extends Command {
+	constructor( editor ) {
+		super( editor );
+
+		this.attributeName = 'verticalAlignment';
+	}
+
+	/**
+	 * @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 );
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @fires execute
+	 * @param {Object} [options]
+	 * @param {Boolean} [options.value] If set the command will set vertical alignment.
+	 * If vertical alignment 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 ) );
+			}
+		} );
+	}
+}

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

@@ -17,6 +17,8 @@ import TableCellPaddingCommand from './commands/tablecellpaddingcommand';
 import TableCellWidthCommand from './commands/tablecellwidthcommand';
 import TableCellHeightCommand from './commands/tablecellheightcommand';
 import TableCellBackgroundColorCommand from './commands/tablecellbackgroundcolorcommand';
+import TableCellVerticalAlignmentCommand from './commands/tablecellverticalalignmentcommand';
+import TableCellHorizontalAlignmentCommand from './commands/tablecellhorizontalalignmentcommand';
 
 /**
  * The table cell properties editing feature.
@@ -61,6 +63,7 @@ export default class TableCellPropertiesEditing extends Plugin {
 
 		enableBorderProperties( schema, conversion );
 		enableHorizontalAlignmentProperty( schema, conversion );
+		editor.commands.add( 'tableCellHorizontalAlignment', new TableCellHorizontalAlignmentCommand( editor ) );
 
 		enableProperty( schema, conversion, 'width', 'width' );
 		editor.commands.add( 'tableCellWidth', new TableCellWidthCommand( editor ) );
@@ -75,6 +78,7 @@ export default class TableCellPropertiesEditing extends Plugin {
 		editor.commands.add( 'tableCellBackgroundColor', new TableCellBackgroundColorCommand( editor ) );
 
 		enableProperty( schema, conversion, 'verticalAlignment', 'vertical-align' );
+		editor.commands.add( 'tableCellVerticalAlignment', new TableCellVerticalAlignmentCommand( editor ) );
 	}
 }