浏览代码

Move table properties related utility functions to utils/table-properties.js.

Maciej Gołaszewski 5 年之前
父节点
当前提交
f05ba70c83

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

@@ -7,8 +7,6 @@
  * @module table/commands/utils
  */
 
-import { isObject } from 'lodash-es';
-
 /**
  * Returns the parent element of the given name. Returns undefined if the position or the element is not inside the desired parent.
  *
@@ -60,62 +58,6 @@ export function createEmptyTableCell( writer, insertPosition, attributes = {} )
 }
 
 /**
- * Returns a string if all four values of box sides are equal.
- *
- * If a string is passed, it is treated as a single value (pass-through).
- *
- *		// Returns 'foo':
- *		getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
- *		getSingleValue( 'foo' );
- *
- *		// Returns undefined:
- *		getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
- *		getSingleValue( { top: 'foo', right: 'foo' } );
- *
- * @param objectOrString
- * @returns {module:engine/view/stylesmap~BoxSides|String}
- */
-export function getSingleValue( objectOrString ) {
-	if ( !objectOrString || !isObject( objectOrString ) ) {
-		return objectOrString;
-	}
-
-	const { top, right, bottom, left } = objectOrString;
-
-	if ( top == right && right == bottom && bottom == left ) {
-		return top;
-	}
-}
-
-/**
- * Adds a unit to a value if the value is a number or a string representing a number.
- *
- * **Note**: It does nothing to non-numeric values.
- *
- *		getSingleValue( 25, 'px' );		// '25px'
- *		getSingleValue( 25, 'em' );		// '25em'
- *		getSingleValue( '25em', 'px' );	// '25em'
- *		getSingleValue( 'foo', 'px' );	// 'foo'
- *
- * @param {*} value
- * @param {String} defaultUnit A default unit added to a numeric value.
- * @returns {String|*}
- */
-export function addDefaultUnitToNumericValue( value, defaultUnit ) {
-	const numericValue = parseFloat( value );
-
-	if ( Number.isNaN( numericValue ) ) {
-		return value;
-	}
-
-	if ( String( numericValue ) !== String( value ) ) {
-		return value;
-	}
-
-	return `${ numericValue }${ defaultUnit }`;
-}
-
-/**
  * Checks if a table cell belongs to the heading column section.
  *
  * @param {module:table/tableutils~TableUtils} tableUtils

+ 1 - 1
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellbordercolorcommand.js

@@ -8,7 +8,7 @@
  */
 
 import TableCellPropertyCommand from './tablecellpropertycommand';
-import { getSingleValue } from '../../commands/utils';
+import { getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table cell border color command.

+ 1 - 1
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellborderstylecommand.js

@@ -8,7 +8,7 @@
  */
 
 import TableCellPropertyCommand from './tablecellpropertycommand';
-import { getSingleValue } from '../../commands/utils';
+import { getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table cell border style command.

+ 1 - 1
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellborderwidthcommand.js

@@ -7,8 +7,8 @@
  * @module table/tablecellproperties/commands/tablecellborderwidthcommand
  */
 
-import { addDefaultUnitToNumericValue, getSingleValue } from '../../commands/utils';
 import TableCellPropertyCommand from './tablecellpropertycommand';
+import { addDefaultUnitToNumericValue, getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table cell border width command.

+ 1 - 1
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellheightcommand.js

@@ -7,8 +7,8 @@
  * @module table/tablecellproperties/commands/tablecellheightcommand
  */
 
-import { addDefaultUnitToNumericValue } from '../../commands/utils';
 import TableCellPropertyCommand from './tablecellpropertycommand';
+import { addDefaultUnitToNumericValue } from '../../utils/table-properties';
 
 /**
  * The table cell height command.

+ 1 - 1
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellpaddingcommand.js

@@ -7,8 +7,8 @@
  * @module table/tablecellproperties/commands/tablecellpaddingcommand
  */
 
-import { addDefaultUnitToNumericValue, getSingleValue } from '../../commands/utils';
 import TableCellPropertyCommand from './tablecellpropertycommand';
+import { addDefaultUnitToNumericValue, getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table cell padding command.

+ 1 - 1
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellwidthcommand.js

@@ -7,8 +7,8 @@
  * @module table/tablecellproperties/commands/tablecellwidthcommand
  */
 
-import { addDefaultUnitToNumericValue } from '../../commands/utils';
 import TableCellPropertyCommand from './tablecellpropertycommand';
+import { addDefaultUnitToNumericValue } from '../../utils/table-properties';
 
 /**
  * The table cell width command.

+ 1 - 1
packages/ckeditor5-table/src/tableproperties/commands/tablebordercolorcommand.js

@@ -8,7 +8,7 @@
  */
 
 import TablePropertyCommand from './tablepropertycommand';
-import { getSingleValue } from '../../commands/utils';
+import { getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table border color command.

+ 1 - 1
packages/ckeditor5-table/src/tableproperties/commands/tableborderstylecommand.js

@@ -8,7 +8,7 @@
  */
 
 import TablePropertyCommand from './tablepropertycommand';
-import { getSingleValue } from '../../commands/utils';
+import { getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table style border command.

+ 1 - 1
packages/ckeditor5-table/src/tableproperties/commands/tableborderwidthcommand.js

@@ -7,8 +7,8 @@
  * @module table/tableproperties/commands/tableborderwidthcommand
  */
 
-import { addDefaultUnitToNumericValue, getSingleValue } from '../../commands/utils';
 import TablePropertyCommand from './tablepropertycommand';
+import { addDefaultUnitToNumericValue, getSingleValue } from '../../utils/table-properties';
 
 /**
  * The table width border command.

+ 1 - 1
packages/ckeditor5-table/src/tableproperties/commands/tableheightcommand.js

@@ -8,7 +8,7 @@
  */
 
 import TablePropertyCommand from './tablepropertycommand';
-import { addDefaultUnitToNumericValue } from '../../commands/utils';
+import { addDefaultUnitToNumericValue } from '../../utils/table-properties';
 
 /**
  * The table height command.

+ 1 - 1
packages/ckeditor5-table/src/tableproperties/commands/tablewidthcommand.js

@@ -7,8 +7,8 @@
  * @module table/tableproperties/commands/tablewidthcommand
  */
 
-import { addDefaultUnitToNumericValue } from '../../commands/utils';
 import TablePropertyCommand from './tablepropertycommand';
+import { addDefaultUnitToNumericValue } from '../../utils/table-properties';
 
 /**
  * The table width command.

+ 66 - 0
packages/ckeditor5-table/src/utils/table-properties.js

@@ -0,0 +1,66 @@
+/**
+ * @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/utils/table-properties
+ */
+
+import { isObject } from 'lodash-es';
+
+/**
+ * Returns a string if all four values of box sides are equal.
+ *
+ * If a string is passed, it is treated as a single value (pass-through).
+ *
+ *		// Returns 'foo':
+ *		getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
+ *		getSingleValue( 'foo' );
+ *
+ *		// Returns undefined:
+ *		getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
+ *		getSingleValue( { top: 'foo', right: 'foo' } );
+ *
+ * @param objectOrString
+ * @returns {module:engine/view/stylesmap~BoxSides|String}
+ */
+export function getSingleValue( objectOrString ) {
+	if ( !objectOrString || !isObject( objectOrString ) ) {
+		return objectOrString;
+	}
+
+	const { top, right, bottom, left } = objectOrString;
+
+	if ( top == right && right == bottom && bottom == left ) {
+		return top;
+	}
+}
+
+/**
+ * Adds a unit to a value if the value is a number or a string representing a number.
+ *
+ * **Note**: It does nothing to non-numeric values.
+ *
+ *		getSingleValue( 25, 'px' );		// '25px'
+ *		getSingleValue( 25, 'em' );		// '25em'
+ *		getSingleValue( '25em', 'px' );	// '25em'
+ *		getSingleValue( 'foo', 'px' );	// 'foo'
+ *
+ * @param {*} value
+ * @param {String} defaultUnit A default unit added to a numeric value.
+ * @returns {String|*}
+ */
+export function addDefaultUnitToNumericValue( value, defaultUnit ) {
+	const numericValue = parseFloat( value );
+
+	if ( Number.isNaN( numericValue ) ) {
+		return value;
+	}
+
+	if ( String( numericValue ) !== String( value ) ) {
+		return value;
+	}
+
+	return `${ numericValue }${ defaultUnit }`;
+}