Преглед на файлове

Added: Initial RemoveColumnCommand implementation.

Maciej Gołaszewski преди 7 години
родител
ревизия
a895dae781

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

@@ -0,0 +1,81 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module table/commands/splitcell
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import TableWalker from '../tablewalker';
+import { getColumns } from './utils';
+
+/**
+ * The split cell command.
+ *
+ * @extends module:core/command~Command
+ */
+export default class RemoveColumnCommand extends Command {
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const model = this.editor.model;
+		const doc = model.document;
+
+		const element = doc.selection.getFirstPosition().parent;
+
+		this.isEnabled = element.is( 'tableCell' ) && getColumns( element.parent.parent ) > 1;
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @fires execute
+	 */
+	execute() {
+		const model = this.editor.model;
+		const document = model.document;
+		const selection = document.selection;
+
+		const firstPosition = selection.getFirstPosition();
+		const tableCell = firstPosition.parent;
+		const tableRow = tableCell.parent;
+
+		const table = tableRow.parent;
+
+		const rowIndex = tableRow.index;
+
+		model.change( writer => {
+			const headingColumns = ( table.getAttribute( 'headingColumns' ) || 0 );
+
+			if ( headingColumns && rowIndex <= headingColumns ) {
+				writer.setAttribute( 'headingColumns', headingColumns - 1, table );
+			}
+
+			// Cache the table before removing or updating colspans.
+			const currentTableState = [ ...new TableWalker( table ) ];
+
+			// Get column index of removed column.
+			const removedColumn = currentTableState.filter( value => value.cell === tableCell ).pop().column;
+
+			for ( const tableWalkerValue of currentTableState ) {
+				const column = tableWalkerValue.column;
+				const colspan = tableWalkerValue.colspan;
+
+				if ( column <= removedColumn && colspan > 1 && column + colspan > removedColumn ) {
+					const colspanToSet = tableWalkerValue.colspan - 1;
+
+					if ( colspanToSet > 1 ) {
+						writer.setAttribute( 'colspan', colspanToSet, tableWalkerValue.cell );
+					} else {
+						writer.removeAttribute( 'colspan', tableWalkerValue.cell );
+					}
+				} else if ( column == removedColumn ) {
+					writer.remove( tableWalkerValue.cell );
+				}
+			}
+		} );
+	}
+}

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

@@ -19,6 +19,7 @@ import InsertRowCommand from './commands/insertrowcommand';
 import InsertColumnCommand from './commands/insertcolumncommand';
 import SplitCellCommand from './commands/splitcellcommand';
 import RemoveRowCommand from './commands/removerowcommand';
+import RemoveColumnCommand from './commands/removecolumncommand';
 import { getParentTable } from './commands/utils';
 
 import './../theme/table.css';
@@ -86,6 +87,7 @@ export default class TablesEditing extends Plugin {
 		editor.commands.add( 'insertColumn', new InsertColumnCommand( editor ) );
 		editor.commands.add( 'splitCell', new SplitCellCommand( editor ) );
 		editor.commands.add( 'removeRow', new RemoveRowCommand( editor ) );
+		editor.commands.add( 'removeColumn', new RemoveColumnCommand( editor ) );
 
 		this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabOnSelectedTable( ...args ) );
 		this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabInsideTable( ...args ) );

+ 185 - 0
packages/ckeditor5-table/tests/commands/removecolumncommand.js

@@ -0,0 +1,185 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
+
+import RemoveColumnCommand from '../../src/commands/removecolumncommand';
+import { downcastInsertTable } from '../../src/converters/downcast';
+import upcastTable from '../../src/converters/upcasttable';
+import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
+
+describe( 'RemoveColumnCommand', () => {
+	let editor, model, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				model = editor.model;
+				command = new RemoveColumnCommand( editor );
+
+				const conversion = editor.conversion;
+				const schema = model.schema;
+
+				schema.register( 'table', {
+					allowWhere: '$block',
+					allowAttributes: [ 'headingRows', 'headingColumns' ],
+					isBlock: true,
+					isObject: true
+				} );
+
+				schema.register( 'tableRow', {
+					allowIn: 'table',
+					allowAttributes: [],
+					isBlock: true,
+					isLimit: true
+				} );
+
+				schema.register( 'tableCell', {
+					allowIn: 'tableRow',
+					allowContentOf: '$block',
+					allowAttributes: [ 'colspan', 'rowspan' ],
+					isBlock: true,
+					isLimit: true
+				} );
+
+				model.schema.register( 'p', { inheritAllFrom: '$block' } );
+
+				// Table conversion.
+				conversion.for( 'upcast' ).add( upcastTable() );
+				conversion.for( 'downcast' ).add( downcastInsertTable() );
+
+				// Table row upcast only since downcast conversion is done in `downcastTable()`.
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
+
+				// Table cell conversion.
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
+				conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
+
+				conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
+				conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
+			} );
+	} );
+
+	afterEach( () => {
+		return editor.destroy();
+	} );
+
+	describe( 'isEnabled', () => {
+		it( 'should be true if selection is inside table cell', () => {
+			setData( model, modelTable( [
+				[ '00[]', '01' ],
+				[ '10', '11' ]
+			] ) );
+
+			expect( command.isEnabled ).to.be.true;
+		} );
+
+		it( 'should be false if selection is inside table with one column only', () => {
+			setData( model, modelTable( [
+				[ '00' ],
+				[ '10[]' ],
+				[ '20[]' ]
+			] ) );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
+		it( 'should be false if selection is outside a table', () => {
+			setData( model, '<p>11[]</p>' );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+	} );
+
+	describe( 'execute()', () => {
+		it( 'should remove a given column', () => {
+			setData( model, modelTable( [
+				[ '00', '01', '02' ],
+				[ '10', '[]11', '12' ],
+				[ '20', '21', '22' ]
+			] ) );
+
+			command.execute();
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '00', '02' ],
+				[ '10[]', '12' ],
+				[ '20', '22' ]
+			] ) );
+		} );
+
+		it( 'should remove a given column from a table start', () => {
+			setData( model, modelTable( [
+				[ '[]00', '01' ],
+				[ '10', '11' ],
+				[ '20', '21' ]
+			] ) );
+
+			command.execute();
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '[]01' ],
+				[ '11' ],
+				[ '21' ]
+			] ) );
+		} );
+
+		it( 'should change heading columns if removing a heading column', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			], { headingColumns: 2 } ) );
+
+			command.execute();
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '01' ],
+				[ '[]11' ],
+				[ '21' ]
+			], { headingColumns: 1 } ) );
+		} );
+
+		it( 'should decrease colspan of table cells from previous column', () => {
+			setData( model, modelTable( [
+				[ { colspan: 4, contents: '00' }, '03' ],
+				[ { colspan: 3, contents: '10' }, '13' ],
+				[ { colspan: 2, contents: '20' }, '22[]', '23' ],
+				[ '30', { colspan: 2, contents: '31' }, '33' ],
+				[ '40', '41', '42', '43' ]
+			] ) );
+
+			command.execute();
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ { colspan: 3, contents: '00' }, '03' ],
+				[ { colspan: 2, contents: '10' }, '13' ],
+				[ { colspan: 2, contents: '20[]' }, '23' ],
+				[ '30', '31', '33' ],
+				[ '40', '41', '43' ]
+
+			] ) );
+		} );
+
+		it( 'should move colspaned cells to row below removing it\'s column', () => {
+			setData( model, modelTable( [
+				[ { colspan: 3, contents: '[]00' }, '03' ],
+				[ { colspan: 2, contents: '10' }, '13' ],
+				[ '20', '21', '22', '23' ]
+			] ) );
+
+			command.execute();
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ { colspan: 2, contents: '[]00' }, '03' ],
+				[ '10', '13' ],
+				[ '21', '22', '23' ]
+			] ) );
+		} );
+	} );
+} );

+ 1 - 1
packages/ckeditor5-table/tests/commands/removerowcommand.js

@@ -73,7 +73,7 @@ describe( 'RemoveRowCommand', () => {
 		it( 'should be true if selection is inside table cell', () => {
 			setData( model, modelTable( [
 				[ '00[]', '01' ],
-				[ '10[]', '11' ]
+				[ '10', '11' ]
 			] ) );
 
 			expect( command.isEnabled ).to.be.true;