Преглед изворни кода

Added: Initial SetTableHeadersCommand implementation.

Maciej Gołaszewski пре 7 година
родитељ
комит
7ad4e69784

+ 69 - 0
packages/ckeditor5-table/src/commands/settableheaderscommand.js

@@ -0,0 +1,69 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module table/commands/settableheaderscommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import { getParentTable } from './utils';
+
+/**
+ * The set table headers command.
+ *
+ * @extends module:core/command~Command
+ */
+export default class SetTableHeadersCommand extends Command {
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const model = this.editor.model;
+		const doc = model.document;
+		const selection = doc.selection;
+
+		const tableParent = getParentTable( selection.getFirstPosition() );
+
+		this.isEnabled = !!tableParent;
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @param {Object} [options] Options for the executed command.
+	 * @param {Number} [options.rows] Number of rows to set as headers.
+	 * @param {Number} [options.columns] Number of columns to set as headers.
+	 *
+	 * @fires execute
+	 */
+	execute( options = {} ) {
+		const model = this.editor.model;
+		const doc = model.document;
+		const selection = doc.selection;
+
+		const rows = parseInt( options.rows ) || 0;
+		const columns = parseInt( options.columns ) || 0;
+
+		const table = getParentTable( selection.getFirstPosition() );
+
+		model.change( writer => {
+			updateTableAttribute( table, 'headingRows', rows, writer );
+			updateTableAttribute( table, 'headingColumns', columns, writer );
+		} );
+	}
+}
+
+// @private
+function updateTableAttribute( table, attributeName, newValue, writer ) {
+	const currentValue = parseInt( table.getAttribute( attributeName ) || 0 );
+
+	if ( newValue !== currentValue ) {
+		if ( newValue > 0 ) {
+			writer.setAttribute( attributeName, newValue, table );
+		} else {
+			writer.removeAttribute( attributeName, table );
+		}
+	}
+}

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

@@ -27,6 +27,7 @@ import SplitCellCommand from './commands/splitcellcommand';
 import MergeCellCommand from './commands/mergecellcommand';
 import RemoveRowCommand from './commands/removerowcommand';
 import RemoveColumnCommand from './commands/removecolumncommand';
+import SetTableHeadersCommand from './commands/settableheaderscommand';
 import { getParentTable } from './commands/utils';
 
 import './../theme/table.css';
@@ -104,6 +105,7 @@ export default class TablesEditing extends Plugin {
 		editor.commands.add( 'mergeLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
 		editor.commands.add( 'mergeDown', new MergeCellCommand( editor, { direction: 'down' } ) );
 		editor.commands.add( 'mergeUp', new MergeCellCommand( editor, { direction: 'up' } ) );
+		editor.commands.add( 'setTableHeaders', new SetTableHeadersCommand( editor ) );
 
 		this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabOnSelectedTable( ...args ) );
 		this.listenTo( editor.editing.view.document, 'keydown', ( ...args ) => this._handleTabInsideTable( ...args ) );

+ 165 - 0
packages/ckeditor5-table/tests/commands/settableheaderscommand.js

@@ -0,0 +1,165 @@
+/**
+ * @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 SetTableHeadersCommand from '../../src/commands/settableheaderscommand';
+import { downcastInsertTable } from '../../src/converters/downcast';
+import upcastTable from '../../src/converters/upcasttable';
+import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
+
+describe( 'SetTableHeadersCommand', () => {
+	let editor, model, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				model = editor.model;
+				command = new SetTableHeadersCommand( editor );
+
+				const conversion = editor.conversion;
+				const schema = model.schema;
+
+				schema.register( 'table', {
+					allowWhere: '$block',
+					allowAttributes: [ 'headingRows' ],
+					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 false if not in a table', () => {
+			setData( model, '<p>foo[]</p>' );
+			expect( command.isEnabled ).to.be.false;
+		} );
+
+		it( 'should be true if in table', () => {
+			setData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
+			expect( command.isEnabled ).to.be.true;
+		} );
+	} );
+
+	describe( 'execute()', () => {
+		it( 'should set heading rows attribute', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			] ) );
+
+			command.execute( { rows: 2 } );
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			], { headingRows: 2 } ) );
+		} );
+
+		it( 'should remove heading rows attribute', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			], { headingRows: 2 } ) );
+
+			command.execute( { rows: 0 } );
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			] ) );
+		} );
+
+		it( 'should set heading columns attribute', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			] ) );
+
+			command.execute( { columns: 2 } );
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			], { headingColumns: 2 } ) );
+		} );
+
+		it( 'should remove heading columns attribute', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			], { headingColumns: 2 } ) );
+
+			command.execute( { columns: 0 } );
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			] ) );
+		} );
+
+		it( 'should remove heading columns & heading rows attributes', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			], { headingColumns: 2, headingRows: 2 } ) );
+
+			command.execute();
+
+			expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+				[ '00', '01' ],
+				[ '[]10', '11' ],
+				[ '20', '21' ]
+			] ) );
+		} );
+	} );
+} );