1 | /**
|
2 | * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3 | * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4 | */
|
5 | import { isObject } from 'lodash-es';
|
6 | /**
|
7 | * Returns a string if all four values of box sides are equal.
|
8 | *
|
9 | * If a string is passed, it is treated as a single value (pass-through).
|
10 | *
|
11 | * ```ts
|
12 | * // Returns 'foo':
|
13 | * getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
|
14 | * getSingleValue( 'foo' );
|
15 | *
|
16 | * // Returns undefined:
|
17 | * getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
|
18 | * getSingleValue( { top: 'foo', right: 'foo' } );
|
19 | * ```
|
20 | */
|
21 | export function getSingleValue(objectOrString) {
|
22 | if (!objectOrString || !isObject(objectOrString)) {
|
23 | return objectOrString;
|
24 | }
|
25 | const { top, right, bottom, left } = objectOrString;
|
26 | if (top == right && right == bottom && bottom == left) {
|
27 | return top;
|
28 | }
|
29 | }
|
30 | /**
|
31 | * Adds a unit to a value if the value is a number or a string representing a number.
|
32 | *
|
33 | * **Note**: It does nothing to non-numeric values.
|
34 | *
|
35 | * ```ts
|
36 | * getSingleValue( 25, 'px' ); // '25px'
|
37 | * getSingleValue( 25, 'em' ); // '25em'
|
38 | * getSingleValue( '25em', 'px' ); // '25em'
|
39 | * getSingleValue( 'foo', 'px' ); // 'foo'
|
40 | * ```
|
41 | *
|
42 | * @param defaultUnit A default unit added to a numeric value.
|
43 | */
|
44 | export function addDefaultUnitToNumericValue(value, defaultUnit) {
|
45 | const numericValue = parseFloat(value);
|
46 | if (Number.isNaN(numericValue)) {
|
47 | return value;
|
48 | }
|
49 | if (String(numericValue) !== String(value)) {
|
50 | return value;
|
51 | }
|
52 | return `${numericValue}${defaultUnit}`;
|
53 | }
|
54 | /**
|
55 | * Returns the normalized configuration.
|
56 | *
|
57 | * @param options.includeAlignmentProperty Whether the "alignment" property should be added.
|
58 | * @param options.includePaddingProperty Whether the "padding" property should be added.
|
59 | * @param options.includeVerticalAlignmentProperty Whether the "verticalAlignment" property should be added.
|
60 | * @param options.includeHorizontalAlignmentProperty Whether the "horizontalAlignment" property should be added.
|
61 | * @param options.isRightToLeftContent Whether the content is right-to-left.
|
62 | */
|
63 | export function getNormalizedDefaultProperties(config, options = {}) {
|
64 | const normalizedConfig = {
|
65 | borderStyle: 'none',
|
66 | borderWidth: '',
|
67 | borderColor: '',
|
68 | backgroundColor: '',
|
69 | width: '',
|
70 | height: '',
|
71 | ...config
|
72 | };
|
73 | if (options.includeAlignmentProperty && !normalizedConfig.alignment) {
|
74 | normalizedConfig.alignment = 'center';
|
75 | }
|
76 | if (options.includePaddingProperty && !normalizedConfig.padding) {
|
77 | normalizedConfig.padding = '';
|
78 | }
|
79 | if (options.includeVerticalAlignmentProperty && !normalizedConfig.verticalAlignment) {
|
80 | normalizedConfig.verticalAlignment = 'middle';
|
81 | }
|
82 | if (options.includeHorizontalAlignmentProperty && !normalizedConfig.horizontalAlignment) {
|
83 | normalizedConfig.horizontalAlignment = options.isRightToLeftContent ? 'right' : 'left';
|
84 | }
|
85 | return normalizedConfig;
|
86 | }
|