8
0
Просмотр исходного кода

Added: Initial `InsertTableCommand` implementation.

Maciej Gołaszewski 8 лет назад
Родитель
Сommit
47c0a6048d

+ 69 - 0
packages/ckeditor5-table/src/inserttablecommand.js

@@ -0,0 +1,69 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module table/tablecommand
+ */
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import Position from '@ckeditor/ckeditor5-engine/src/model/position';
+
+export default class InsertTableCommand extends Command {
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const model = this.editor.model;
+		const doc = model.document;
+
+		const validParent = _getValidParent( doc.selection.getFirstPosition() );
+
+		this.isEnabled = model.schema.checkChild( validParent, 'table' );
+	}
+
+	/**
+	 * Executes the command.
+	 *
+	 * @protected
+	 * @param {Object} [options] Options for the executed command.
+	 * @param {String} [options.rows=2] Number of rows to create in inserted table.
+	 * @param {String} [options.columns=2] Number of columns to create in inserted table.
+	 *
+	 * @fires execute
+	 */
+	execute( options = {} ) {
+		const model = this.editor.model;
+		const document = model.document;
+		const selection = document.selection;
+
+		const rows = parseInt( options.rows ) || 2;
+		const columns = parseInt( options.columns ) || 2;
+
+		const firstPosition = selection.getFirstPosition();
+		const insertTablePosition = Position.createAfter( firstPosition.parent || firstPosition );
+
+		model.change( writer => {
+			const table = writer.createElement( 'table' );
+
+			writer.insert( table, insertTablePosition );
+
+			for ( let r = 0; r < rows; r++ ) {
+				const row = writer.createElement( 'tableRow' );
+
+				writer.insert( row, table, 'end' );
+
+				for ( let c = 0; c < columns; c++ ) {
+					const cell = writer.createElement( 'tableCell' );
+					writer.insert( cell, row, 'end' );
+				}
+			}
+		} );
+	}
+}
+
+function _getValidParent( firstPosition ) {
+	const parent = firstPosition.parent;
+	return parent === parent.root ? parent : parent.parent;
+}

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

@@ -10,6 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 import { createTableCell, createTableRow, downcastTableCell, downcastTable } from './converters';
+import InsertTableCommand from './inserttablecommand';
 
 export default class TablesEditing extends Plugin {
 	/**
@@ -56,5 +57,7 @@ export default class TablesEditing extends Plugin {
 
 		conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
 		conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
+
+		editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
 	}
 }