浏览代码

Extract MarginStyles normalizer.

Maciej Gołaszewski 6 年之前
父节点
当前提交
9ad7720831

+ 2 - 36
packages/ckeditor5-engine/src/view/styles.js

@@ -11,7 +11,8 @@ import { get, has, isObject, merge, set, unset } from 'lodash-es';
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import BorderStyles from './styles/borderstyles';
-import { getTopRightBottomLeftValueReducer } from './styles/utils';
+import { getPositionShorthandNormalizer, getTopRightBottomLeftValueReducer } from './styles/utils';
+import MarginStyles from './styles/marginstyles';
 
 class StylesConverter {
 	/**
@@ -167,19 +168,6 @@ mix( StylesConverter, EmitterMixin );
 
 export const stylesConverter = new StylesConverter();
 
-class MarginStyles {
-	static attach( stylesConverter ) {
-		stylesConverter.on( 'normalize:margin', getPositionShorthandNormalizer( 'margin' ) );
-
-		stylesConverter.on( 'normalize:margin-top', ( evt, data ) => ( data.path = 'margin.top' ) );
-		stylesConverter.on( 'normalize:margin-right', ( evt, data ) => ( data.path = 'margin.right' ) );
-		stylesConverter.on( 'normalize:margin-bottom', ( evt, data ) => ( data.path = 'margin.bottom' ) );
-		stylesConverter.on( 'normalize:margin-left', ( evt, data ) => ( data.path = 'margin.left' ) );
-
-		stylesConverter.on( 'reduce:margin', getTopRightBottomLeftValueReducer( 'margin' ) );
-	}
-}
-
 class PaddingStyles {
 	static attach( stylesConverter ) {
 		stylesConverter.on( 'normalize:padding', getPositionShorthandNormalizer( 'padding' ) );
@@ -414,28 +402,6 @@ export default class Styles {
 	}
 }
 
-function getTopRightBottomLeftValues( value = '' ) {
-	if ( value === '' ) {
-		return { top: undefined, right: undefined, bottom: undefined, left: undefined };
-	}
-
-	const values = value.split( ' ' );
-
-	const top = values[ 0 ];
-	const bottom = values[ 2 ] || top;
-	const right = values[ 1 ] || top;
-	const left = values[ 3 ] || right;
-
-	return { top, bottom, right, left };
-}
-
-function getPositionShorthandNormalizer( longhand ) {
-	return ( evt, data ) => {
-		data.path = longhand;
-		data.value = getTopRightBottomLeftValues( data.value );
-	};
-}
-
 function normalizeBackground( evt, data ) {
 	const background = {};
 

+ 23 - 0
packages/ckeditor5-engine/src/view/styles/marginstyles.js

@@ -0,0 +1,23 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import { getPositionShorthandNormalizer, getTopRightBottomLeftValueReducer } from './utils';
+
+/**
+ * @module engine/view/styles
+ */
+
+export default class MarginStyles {
+	static attach( stylesConverter ) {
+		stylesConverter.on( 'normalize:margin', getPositionShorthandNormalizer( 'margin' ) );
+
+		stylesConverter.on( 'normalize:margin-top', ( evt, data ) => ( data.path = 'margin.top' ) );
+		stylesConverter.on( 'normalize:margin-right', ( evt, data ) => ( data.path = 'margin.right' ) );
+		stylesConverter.on( 'normalize:margin-bottom', ( evt, data ) => ( data.path = 'margin.bottom' ) );
+		stylesConverter.on( 'normalize:margin-left', ( evt, data ) => ( data.path = 'margin.left' ) );
+
+		stylesConverter.on( 'reduce:margin', getTopRightBottomLeftValueReducer( 'margin' ) );
+	}
+}

+ 7 - 0
packages/ckeditor5-engine/src/view/styles/utils.js

@@ -79,3 +79,10 @@ export function getTopRightBottomLeftShorthandValue( { left, right, top, bottom
 
 	return out.join( ' ' );
 }
+
+export function getPositionShorthandNormalizer( longhand ) {
+	return ( evt, data ) => {
+		data.path = longhand;
+		data.value = getTopRightBottomLeftValues( data.value );
+	};
+}