瀏覽代碼

Other: Extract common utils from commands and move commands to own module.

Maciej Gołaszewski 7 年之前
父節點
當前提交
75ae53e09c

+ 14 - 31
packages/ckeditor5-table/src/insertcolumncommand.js

@@ -4,20 +4,13 @@
  */
 
 /**
- * @module table/insertcolumncommand
+ * @module table/commands/insertcolumncommand
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import TableWalker from './tablewalker';
+import TableWalker from '../tablewalker';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
-
-function createCells( columns, writer, insertPosition ) {
-	for ( let i = 0; i < columns; i++ ) {
-		const cell = writer.createElement( 'tableCell' );
-
-		writer.insert( cell, insertPosition );
-	}
-}
+import { getColumns, getParentTable } from './utils';
 
 /**
  * The insert column command.
@@ -32,7 +25,7 @@ export default class InsertColumnCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		const tableParent = getValidParent( doc.selection.getFirstPosition() );
+		const tableParent = getParentTable( doc.selection.getFirstPosition() );
 
 		this.isEnabled = !!tableParent;
 	}
@@ -54,7 +47,7 @@ export default class InsertColumnCommand extends Command {
 		const columns = parseInt( options.columns ) || 1;
 		const insertAt = parseInt( options.at ) || 0;
 
-		const table = getValidParent( selection.getFirstPosition() );
+		const table = getParentTable( selection.getFirstPosition() );
 
 		model.change( writer => {
 			const tableColumns = getColumns( table );
@@ -109,25 +102,15 @@ export default class InsertColumnCommand extends Command {
 	}
 }
 
-function getValidParent( firstPosition ) {
-	let parent = firstPosition.parent;
-
-	while ( parent ) {
-		if ( parent.name === 'table' ) {
-			return parent;
-		}
+// Creates cells at given position.
+//
+// @param {Number} columns Number of columns to create
+// @param {module:engine/model/writer} writer
+// @param {module:engine/model/position} insertPosition
+function createCells( columns, writer, insertPosition ) {
+	for ( let i = 0; i < columns; i++ ) {
+		const cell = writer.createElement( 'tableCell' );
 
-		parent = parent.parent;
+		writer.insert( cell, insertPosition );
 	}
 }
-
-// TODO: dup
-function getColumns( table ) {
-	const row = table.getChild( 0 );
-
-	return [ ...row.getChildren() ].reduce( ( columns, row ) => {
-		const columnWidth = parseInt( row.getAttribute( 'colspan' ) ) || 1;
-
-		return columns + ( columnWidth );
-	}, 0 );
-}

+ 5 - 26
packages/ckeditor5-table/src/insertrowcommand.js

@@ -4,11 +4,12 @@
  */
 
 /**
- * @module table/insertrowcommand
+ * @module table/commands/insertrowcommand
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import TableWalker from './tablewalker';
+import TableWalker from '../tablewalker';
+import { getColumns, getParentTable } from './utils';
 
 /**
  * The insert row command.
@@ -23,7 +24,7 @@ export default class InsertRowCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		const tableParent = getValidParent( doc.selection.getFirstPosition() );
+		const tableParent = getParentTable( doc.selection.getFirstPosition() );
 
 		this.isEnabled = !!tableParent;
 	}
@@ -45,7 +46,7 @@ export default class InsertRowCommand extends Command {
 		const rows = parseInt( options.rows ) || 1;
 		const insertAt = parseInt( options.at ) || 0;
 
-		const table = getValidParent( selection.getFirstPosition() );
+		const table = getParentTable( selection.getFirstPosition() );
 
 		const headingRows = table.getAttribute( 'headingRows' ) || 0;
 
@@ -93,25 +94,3 @@ export default class InsertRowCommand extends Command {
 		} );
 	}
 }
-
-function getValidParent( firstPosition ) {
-	let parent = firstPosition.parent;
-
-	while ( parent ) {
-		if ( parent.name === 'table' ) {
-			return parent;
-		}
-
-		parent = parent.parent;
-	}
-}
-
-function getColumns( table ) {
-	const row = table.getChild( 0 );
-
-	return [ ...row.getChildren() ].reduce( ( columns, row ) => {
-		const columnWidth = parseInt( row.getAttribute( 'colspan' ) ) || 1;
-
-		return columns + ( columnWidth );
-	}, 0 );
-}

+ 7 - 5
packages/ckeditor5-table/src/inserttablecommand.js

@@ -4,7 +4,7 @@
  */
 
 /**
- * @module table/inserttablecommand
+ * @module table/commands/inserttablecommand
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
@@ -23,7 +23,7 @@ export default class InsertTableCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		const validParent = _getValidParent( doc.selection.getFirstPosition() );
+		const validParent = getValidParent( doc.selection.getFirstPosition() );
 
 		this.isEnabled = model.schema.checkChild( validParent, 'table' );
 	}
@@ -47,7 +47,6 @@ export default class InsertTableCommand extends Command {
 
 		const firstPosition = selection.getFirstPosition();
 
-		// TODO does API has it?
 		const isRoot = firstPosition.parent === firstPosition.root;
 		const insertTablePosition = isRoot ? Position.createAt( firstPosition ) : Position.createAfter( firstPosition.parent );
 
@@ -72,7 +71,10 @@ export default class InsertTableCommand extends Command {
 	}
 }
 
-function _getValidParent( firstPosition ) {
-	const parent = firstPosition.parent;
+// Returns valid parent to insert table
+//
+// @param {module:engine/model/position} position
+function getValidParent( position ) {
+	const parent = position.parent;
 	return parent === parent.root ? parent : parent.parent;
 }

+ 42 - 0
packages/ckeditor5-table/src/commands/utils.js

@@ -0,0 +1,42 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module table/commands/utils
+ */
+
+/**
+ * Returns parent table.
+ *
+ * @param {module:engine/model/position} position
+ * @returns {*}
+ */
+export function getParentTable( position ) {
+	let parent = position.parent;
+
+	while ( parent ) {
+		if ( parent.name === 'table' ) {
+			return parent;
+		}
+
+		parent = parent.parent;
+	}
+}
+
+/**
+ * Returns number of columns for given table.
+ *
+ * @param {module:engine/model/element} table
+ * @returns {Number}
+ */
+export function getColumns( table ) {
+	const row = table.getChild( 0 );
+
+	return [ ...row.getChildren() ].reduce( ( columns, row ) => {
+		const columnWidth = parseInt( row.getAttribute( 'colspan' ) ) || 1;
+
+		return columns + ( columnWidth );
+	}, 0 );
+}

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

@@ -11,9 +11,9 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 import upcastTable from './converters/upcasttable';
 import { downcastInsertCell, downcastInsertRow, downcastInsertTable } from './converters/downcast';
-import InsertTableCommand from './inserttablecommand';
-import InsertRowCommand from './insertrowcommand';
-import InsertColumnCommand from './insertcolumncommand';
+import InsertTableCommand from './commands/inserttablecommand';
+import InsertRowCommand from './commands/insertrowcommand';
+import InsertColumnCommand from './commands/insertcolumncommand';
 
 /**
  * The table editing feature.

+ 4 - 4
packages/ckeditor5-table/tests/insertcolumncommand.js

@@ -7,10 +7,10 @@ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltestedit
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 
-import InsertColumnCommand from '../src/insertcolumncommand';
-import { downcastInsertTable } from '../src/converters/downcast';
-import upcastTable from '../src/converters/upcasttable';
-import { formatModelTable, formattedModelTable, modelTable } from './_utils/utils';
+import InsertColumnCommand from '../../src/commands/insertcolumncommand';
+import { downcastInsertTable } from '../../src/converters/downcast';
+import upcastTable from '../../src/converters/upcasttable';
+import { formatModelTable, formattedModelTable, modelTable } from '../_utils/utils';
 
 describe( 'InsertColumnCommand', () => {
 	let editor, model, command;

+ 4 - 4
packages/ckeditor5-table/tests/insertrowcommand.js

@@ -7,10 +7,10 @@ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltestedit
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 
-import InsertRowCommand from '../src/insertrowcommand';
-import { downcastInsertTable } from '../src/converters/downcast';
-import upcastTable from '../src/converters/upcasttable';
-import { formatModelTable, formattedModelTable, modelTable } from './_utils/utils';
+import InsertRowCommand from '../../src/commands/insertrowcommand';
+import { downcastInsertTable } from '../../src/converters/downcast';
+import upcastTable from '../../src/converters/upcasttable';
+import { formatModelTable, formattedModelTable, modelTable } from '../_utils/utils';
 
 describe( 'InsertRowCommand', () => {
 	let editor, model, command;

+ 3 - 3
packages/ckeditor5-table/tests/inserttablecommand.js

@@ -7,9 +7,9 @@ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltestedit
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 
-import InsertTableCommand from '../src/inserttablecommand';
-import { downcastInsertTable } from '../src/converters/downcast';
-import upcastTable from '../src/converters/upcasttable';
+import InsertTableCommand from '../../src/commands/inserttablecommand';
+import { downcastInsertTable } from '../../src/converters/downcast';
+import upcastTable from '../../src/converters/upcasttable';
 
 describe( 'InsertTableCommand', () => {
 	let editor, model, command;