@builder.io/sdk-svelte 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/components/blocks/blocks-wrapper.svelte +8 -4
- package/lib/browser/components/blocks/blocks.svelte +8 -1
- package/lib/browser/components/content/components/enable-editor.svelte +7 -2
- package/lib/browser/components/content/components/enable-editor.svelte.d.ts +3 -1
- package/lib/browser/components/content/content.svelte +8 -0
- package/lib/browser/components/content/content.svelte.d.ts +4 -0
- package/lib/browser/components/content-variants/content-variants.svelte +12 -0
- package/lib/browser/components/content-variants/content-variants.svelte.d.ts +4 -0
- package/lib/browser/components/content-variants/content-variants.types.d.ts +16 -0
- package/lib/browser/constants/sdk-version.d.ts +1 -1
- package/lib/browser/constants/sdk-version.js +1 -1
- package/lib/browser/context/builder.context.d.ts +2 -0
- package/lib/browser/context/builder.context.js +2 -0
- package/lib/browser/context/types.d.ts +2 -1
- package/lib/browser/functions/get-block-properties.js +5 -9
- package/lib/browser/functions/transform-block-properties.d.ts +7 -1
- package/lib/browser/functions/transform-block-properties.js +1 -1
- package/lib/browser/types/enforced-partials.d.ts +7 -2
- package/lib/edge/components/blocks/blocks-wrapper.svelte +8 -4
- package/lib/edge/components/blocks/blocks.svelte +8 -1
- package/lib/edge/components/content/components/enable-editor.svelte +7 -2
- package/lib/edge/components/content/components/enable-editor.svelte.d.ts +3 -1
- package/lib/edge/components/content/content.svelte +8 -0
- package/lib/edge/components/content/content.svelte.d.ts +4 -0
- package/lib/edge/components/content-variants/content-variants.svelte +12 -0
- package/lib/edge/components/content-variants/content-variants.svelte.d.ts +4 -0
- package/lib/edge/components/content-variants/content-variants.types.d.ts +16 -0
- package/lib/edge/constants/sdk-version.d.ts +1 -1
- package/lib/edge/constants/sdk-version.js +1 -1
- package/lib/edge/context/builder.context.d.ts +2 -0
- package/lib/edge/context/builder.context.js +2 -0
- package/lib/edge/context/types.d.ts +2 -1
- package/lib/edge/functions/get-block-properties.js +5 -9
- package/lib/edge/functions/transform-block-properties.d.ts +7 -1
- package/lib/edge/functions/transform-block-properties.js +1 -1
- package/lib/edge/types/enforced-partials.d.ts +7 -2
- package/lib/node/components/blocks/blocks-wrapper.svelte +8 -4
- package/lib/node/components/blocks/blocks.svelte +8 -1
- package/lib/node/components/content/components/enable-editor.svelte +7 -2
- package/lib/node/components/content/components/enable-editor.svelte.d.ts +3 -1
- package/lib/node/components/content/content.svelte +8 -0
- package/lib/node/components/content/content.svelte.d.ts +4 -0
- package/lib/node/components/content-variants/content-variants.svelte +12 -0
- package/lib/node/components/content-variants/content-variants.svelte.d.ts +4 -0
- package/lib/node/components/content-variants/content-variants.types.d.ts +16 -0
- package/lib/node/constants/sdk-version.d.ts +1 -1
- package/lib/node/constants/sdk-version.js +1 -1
- package/lib/node/context/builder.context.d.ts +2 -0
- package/lib/node/context/builder.context.js +2 -0
- package/lib/node/context/types.d.ts +2 -1
- package/lib/node/functions/get-block-properties.js +5 -9
- package/lib/node/functions/transform-block-properties.d.ts +7 -1
- package/lib/node/functions/transform-block-properties.js +1 -1
- package/lib/node/types/enforced-partials.d.ts +7 -2
- package/package.json +1 -1
- package/src/components/blocks/blocks-wrapper.svelte +16 -4
- package/src/components/blocks/blocks.svelte +11 -2
- package/src/components/content/components/enable-editor.svelte +14 -3
- package/src/components/content/content.svelte +9 -0
- package/src/components/content-variants/content-variants.svelte +15 -0
- package/src/components/content-variants/content-variants.types.ts +16 -0
- package/src/constants/sdk-version.ts +1 -1
- package/src/context/builder.context.ts +2 -0
- package/src/context/types.ts +2 -1
- package/src/functions/get-block-properties.ts +5 -9
- package/src/functions/transform-block-properties.ts +9 -1
- package/src/types/enforced-partials.ts +8 -2
- package/lib/browser/functions/get-react-native-block-styles.d.ts +0 -7
- package/lib/browser/functions/get-react-native-block-styles.js +0 -17
- package/lib/browser/functions/sanitize-react-native-block-styles.d.ts +0 -3
- package/lib/browser/functions/sanitize-react-native-block-styles.js +0 -61
- package/lib/edge/functions/get-react-native-block-styles.d.ts +0 -7
- package/lib/edge/functions/get-react-native-block-styles.js +0 -17
- package/lib/edge/functions/sanitize-react-native-block-styles.d.ts +0 -3
- package/lib/edge/functions/sanitize-react-native-block-styles.js +0 -61
- package/lib/node/functions/get-react-native-block-styles.d.ts +0 -7
- package/lib/node/functions/get-react-native-block-styles.js +0 -17
- package/lib/node/functions/sanitize-react-native-block-styles.d.ts +0 -3
- package/lib/node/functions/sanitize-react-native-block-styles.js +0 -61
- package/src/functions/get-react-native-block-styles.ts +0 -27
- package/src/functions/sanitize-react-native-block-styles.ts +0 -58
package/src/context/types.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { BlocksWrapperProps } from '../components/blocks/blocks-wrapper.svelte';
|
|
1
2
|
import type { ApiVersion } from '../types/api-version.js';
|
|
2
3
|
import type { BuilderContent } from '../types/builder-content.js';
|
|
3
4
|
import type { ComponentInfo } from '../types/components.js';
|
|
@@ -8,7 +9,7 @@ export type RegisteredComponent = ComponentInfo & {
|
|
|
8
9
|
export type RegisteredComponents = Dictionary<RegisteredComponent>;
|
|
9
10
|
export type BuilderRenderState = Record<string, unknown>;
|
|
10
11
|
export type BuilderRenderContext = Record<string, unknown>;
|
|
11
|
-
export interface BuilderContextInterface {
|
|
12
|
+
export interface BuilderContextInterface extends Pick<BlocksWrapperProps, 'BlocksWrapper' | 'BlocksWrapperProps'> {
|
|
12
13
|
content: Nullable<BuilderContent>;
|
|
13
14
|
context: BuilderRenderContext;
|
|
14
15
|
/**
|
|
@@ -2,7 +2,6 @@ import { TARGET } from '../constants/target.js';
|
|
|
2
2
|
import type { BuilderContextInterface } from '../context/types.js';
|
|
3
3
|
import { convertStyleMapToCSSArray } from '../helpers/css.js';
|
|
4
4
|
import type { BuilderBlock } from '../types/builder-block.js';
|
|
5
|
-
import { getReactNativeBlockStyles } from './get-react-native-block-styles.js';
|
|
6
5
|
import { transformBlockProperties } from './transform-block-properties.js';
|
|
7
6
|
const extractRelevantRootBlockProperties = (block: BuilderBlock) => {
|
|
8
7
|
// currently we are only spreading the `href` property
|
|
@@ -52,14 +51,11 @@ export function getBlockProperties({
|
|
|
52
51
|
style: block.style ? getStyleAttribute(block.style) : undefined,
|
|
53
52
|
class: [block.id, 'builder-block', block.class, block.properties?.class].filter(Boolean).join(' ')
|
|
54
53
|
};
|
|
55
|
-
|
|
56
|
-
properties
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
return transformBlockProperties(properties);
|
|
54
|
+
return transformBlockProperties({
|
|
55
|
+
properties,
|
|
56
|
+
context,
|
|
57
|
+
block
|
|
58
|
+
});
|
|
63
59
|
}
|
|
64
60
|
/**
|
|
65
61
|
* Svelte does not support style attribute as an object so we need to flatten it.
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import type { BuilderContextInterface } from '../context/types';
|
|
2
|
+
import type { BuilderBlock } from '../types/builder-block';
|
|
3
|
+
export function transformBlockProperties<T>({
|
|
4
|
+
properties
|
|
5
|
+
}: {
|
|
6
|
+
properties: T;
|
|
7
|
+
context: BuilderContextInterface;
|
|
8
|
+
block: BuilderBlock;
|
|
9
|
+
}) {
|
|
2
10
|
return properties;
|
|
3
11
|
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { Prettify } from './typescript.js';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lol Typescript
|
|
5
|
+
* https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
|
|
6
|
+
*/
|
|
7
|
+
type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
|
|
8
|
+
type OptionalFieldsOnly<T> = { [K in keyof T as T[K] extends Required<T>[K] ? IfAny<T[K], K, never> : K]: T[K] };
|
|
9
|
+
type RequiredFieldsOnly<T> = { [K in keyof T as T[K] extends Required<T>[K] ? IfAny<T[K], never, K> : never]: T[K] };
|
|
4
10
|
type Enforced<T> = { [K in keyof T]-?: T[K] };
|
|
5
11
|
type AndUndefined<T> = { [K in keyof T]: T[K] | undefined };
|
|
6
12
|
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
|
-
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
|
-
export declare function getReactNativeBlockStyles({ block, context, blockStyles }: {
|
|
4
|
-
block: BuilderBlock;
|
|
5
|
-
context: BuilderContextInterface;
|
|
6
|
-
blockStyles: any;
|
|
7
|
-
}): CSSStyleDeclaration | Record<string, string | undefined>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { sanitizeReactNativeBlockStyles } from './sanitize-react-native-block-styles.js';
|
|
2
|
-
export function getReactNativeBlockStyles({ block, context, blockStyles }) {
|
|
3
|
-
const responsiveStyles = block.responsiveStyles;
|
|
4
|
-
if (!responsiveStyles) {
|
|
5
|
-
return {};
|
|
6
|
-
}
|
|
7
|
-
const styles = {
|
|
8
|
-
// recursively apply inherited styles so that they can be passed down to children `Text` blocks
|
|
9
|
-
...context.inheritedStyles,
|
|
10
|
-
...(responsiveStyles.large || {}),
|
|
11
|
-
...(responsiveStyles.medium || {}),
|
|
12
|
-
...(responsiveStyles.small || {}),
|
|
13
|
-
...blockStyles
|
|
14
|
-
};
|
|
15
|
-
const newStyles = sanitizeReactNativeBlockStyles(styles);
|
|
16
|
-
return newStyles;
|
|
17
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
const propertiesThatMustBeNumber = new Set(['lineHeight']);
|
|
2
|
-
const displayValues = new Set(['flex', 'none']);
|
|
3
|
-
const SHOW_WARNINGS = false;
|
|
4
|
-
const normalizeNumber = (value) => {
|
|
5
|
-
if (Number.isNaN(value)) {
|
|
6
|
-
return undefined;
|
|
7
|
-
}
|
|
8
|
-
else if (value < 0) {
|
|
9
|
-
// TODO: why are negative values not allowed?
|
|
10
|
-
return 0;
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
export const sanitizeReactNativeBlockStyles = (styles) => {
|
|
17
|
-
return Object.keys(styles).reduce((acc, key) => {
|
|
18
|
-
const propertyValue = styles[key];
|
|
19
|
-
if (key === 'display' && !displayValues.has(propertyValue)) {
|
|
20
|
-
if (SHOW_WARNINGS) {
|
|
21
|
-
console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
|
|
22
|
-
}
|
|
23
|
-
return acc;
|
|
24
|
-
}
|
|
25
|
-
if (propertiesThatMustBeNumber.has(key) && typeof propertyValue !== 'number') {
|
|
26
|
-
if (SHOW_WARNINGS) {
|
|
27
|
-
console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
|
|
28
|
-
}
|
|
29
|
-
return acc;
|
|
30
|
-
}
|
|
31
|
-
if (typeof propertyValue === 'string') {
|
|
32
|
-
// `px` units need to be stripped and replaced with numbers
|
|
33
|
-
// https://regexr.com/6ualn
|
|
34
|
-
const isPixelUnit = propertyValue.match(/^-?(\d*)(\.?)(\d*)*px$/);
|
|
35
|
-
if (isPixelUnit) {
|
|
36
|
-
const newValue = parseFloat(propertyValue);
|
|
37
|
-
const normalizedValue = normalizeNumber(newValue);
|
|
38
|
-
if (normalizedValue) {
|
|
39
|
-
return {
|
|
40
|
-
...acc,
|
|
41
|
-
[key]: normalizedValue
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
return acc;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
else if (propertyValue === '0') {
|
|
49
|
-
// 0 edge case needs to be handled
|
|
50
|
-
return {
|
|
51
|
-
...acc,
|
|
52
|
-
[key]: 0
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
...acc,
|
|
58
|
-
[key]: propertyValue
|
|
59
|
-
};
|
|
60
|
-
}, {});
|
|
61
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
|
-
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
|
-
export declare function getReactNativeBlockStyles({ block, context, blockStyles }: {
|
|
4
|
-
block: BuilderBlock;
|
|
5
|
-
context: BuilderContextInterface;
|
|
6
|
-
blockStyles: any;
|
|
7
|
-
}): CSSStyleDeclaration | Record<string, string | undefined>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { sanitizeReactNativeBlockStyles } from './sanitize-react-native-block-styles.js';
|
|
2
|
-
export function getReactNativeBlockStyles({ block, context, blockStyles }) {
|
|
3
|
-
const responsiveStyles = block.responsiveStyles;
|
|
4
|
-
if (!responsiveStyles) {
|
|
5
|
-
return {};
|
|
6
|
-
}
|
|
7
|
-
const styles = {
|
|
8
|
-
// recursively apply inherited styles so that they can be passed down to children `Text` blocks
|
|
9
|
-
...context.inheritedStyles,
|
|
10
|
-
...(responsiveStyles.large || {}),
|
|
11
|
-
...(responsiveStyles.medium || {}),
|
|
12
|
-
...(responsiveStyles.small || {}),
|
|
13
|
-
...blockStyles
|
|
14
|
-
};
|
|
15
|
-
const newStyles = sanitizeReactNativeBlockStyles(styles);
|
|
16
|
-
return newStyles;
|
|
17
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
const propertiesThatMustBeNumber = new Set(['lineHeight']);
|
|
2
|
-
const displayValues = new Set(['flex', 'none']);
|
|
3
|
-
const SHOW_WARNINGS = false;
|
|
4
|
-
const normalizeNumber = (value) => {
|
|
5
|
-
if (Number.isNaN(value)) {
|
|
6
|
-
return undefined;
|
|
7
|
-
}
|
|
8
|
-
else if (value < 0) {
|
|
9
|
-
// TODO: why are negative values not allowed?
|
|
10
|
-
return 0;
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
export const sanitizeReactNativeBlockStyles = (styles) => {
|
|
17
|
-
return Object.keys(styles).reduce((acc, key) => {
|
|
18
|
-
const propertyValue = styles[key];
|
|
19
|
-
if (key === 'display' && !displayValues.has(propertyValue)) {
|
|
20
|
-
if (SHOW_WARNINGS) {
|
|
21
|
-
console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
|
|
22
|
-
}
|
|
23
|
-
return acc;
|
|
24
|
-
}
|
|
25
|
-
if (propertiesThatMustBeNumber.has(key) && typeof propertyValue !== 'number') {
|
|
26
|
-
if (SHOW_WARNINGS) {
|
|
27
|
-
console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
|
|
28
|
-
}
|
|
29
|
-
return acc;
|
|
30
|
-
}
|
|
31
|
-
if (typeof propertyValue === 'string') {
|
|
32
|
-
// `px` units need to be stripped and replaced with numbers
|
|
33
|
-
// https://regexr.com/6ualn
|
|
34
|
-
const isPixelUnit = propertyValue.match(/^-?(\d*)(\.?)(\d*)*px$/);
|
|
35
|
-
if (isPixelUnit) {
|
|
36
|
-
const newValue = parseFloat(propertyValue);
|
|
37
|
-
const normalizedValue = normalizeNumber(newValue);
|
|
38
|
-
if (normalizedValue) {
|
|
39
|
-
return {
|
|
40
|
-
...acc,
|
|
41
|
-
[key]: normalizedValue
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
return acc;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
else if (propertyValue === '0') {
|
|
49
|
-
// 0 edge case needs to be handled
|
|
50
|
-
return {
|
|
51
|
-
...acc,
|
|
52
|
-
[key]: 0
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
...acc,
|
|
58
|
-
[key]: propertyValue
|
|
59
|
-
};
|
|
60
|
-
}, {});
|
|
61
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
|
-
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
|
-
export declare function getReactNativeBlockStyles({ block, context, blockStyles }: {
|
|
4
|
-
block: BuilderBlock;
|
|
5
|
-
context: BuilderContextInterface;
|
|
6
|
-
blockStyles: any;
|
|
7
|
-
}): CSSStyleDeclaration | Record<string, string | undefined>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { sanitizeReactNativeBlockStyles } from './sanitize-react-native-block-styles.js';
|
|
2
|
-
export function getReactNativeBlockStyles({ block, context, blockStyles }) {
|
|
3
|
-
const responsiveStyles = block.responsiveStyles;
|
|
4
|
-
if (!responsiveStyles) {
|
|
5
|
-
return {};
|
|
6
|
-
}
|
|
7
|
-
const styles = {
|
|
8
|
-
// recursively apply inherited styles so that they can be passed down to children `Text` blocks
|
|
9
|
-
...context.inheritedStyles,
|
|
10
|
-
...(responsiveStyles.large || {}),
|
|
11
|
-
...(responsiveStyles.medium || {}),
|
|
12
|
-
...(responsiveStyles.small || {}),
|
|
13
|
-
...blockStyles
|
|
14
|
-
};
|
|
15
|
-
const newStyles = sanitizeReactNativeBlockStyles(styles);
|
|
16
|
-
return newStyles;
|
|
17
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
const propertiesThatMustBeNumber = new Set(['lineHeight']);
|
|
2
|
-
const displayValues = new Set(['flex', 'none']);
|
|
3
|
-
const SHOW_WARNINGS = false;
|
|
4
|
-
const normalizeNumber = (value) => {
|
|
5
|
-
if (Number.isNaN(value)) {
|
|
6
|
-
return undefined;
|
|
7
|
-
}
|
|
8
|
-
else if (value < 0) {
|
|
9
|
-
// TODO: why are negative values not allowed?
|
|
10
|
-
return 0;
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
export const sanitizeReactNativeBlockStyles = (styles) => {
|
|
17
|
-
return Object.keys(styles).reduce((acc, key) => {
|
|
18
|
-
const propertyValue = styles[key];
|
|
19
|
-
if (key === 'display' && !displayValues.has(propertyValue)) {
|
|
20
|
-
if (SHOW_WARNINGS) {
|
|
21
|
-
console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
|
|
22
|
-
}
|
|
23
|
-
return acc;
|
|
24
|
-
}
|
|
25
|
-
if (propertiesThatMustBeNumber.has(key) && typeof propertyValue !== 'number') {
|
|
26
|
-
if (SHOW_WARNINGS) {
|
|
27
|
-
console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
|
|
28
|
-
}
|
|
29
|
-
return acc;
|
|
30
|
-
}
|
|
31
|
-
if (typeof propertyValue === 'string') {
|
|
32
|
-
// `px` units need to be stripped and replaced with numbers
|
|
33
|
-
// https://regexr.com/6ualn
|
|
34
|
-
const isPixelUnit = propertyValue.match(/^-?(\d*)(\.?)(\d*)*px$/);
|
|
35
|
-
if (isPixelUnit) {
|
|
36
|
-
const newValue = parseFloat(propertyValue);
|
|
37
|
-
const normalizedValue = normalizeNumber(newValue);
|
|
38
|
-
if (normalizedValue) {
|
|
39
|
-
return {
|
|
40
|
-
...acc,
|
|
41
|
-
[key]: normalizedValue
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
return acc;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
else if (propertyValue === '0') {
|
|
49
|
-
// 0 edge case needs to be handled
|
|
50
|
-
return {
|
|
51
|
-
...acc,
|
|
52
|
-
[key]: 0
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
...acc,
|
|
58
|
-
[key]: propertyValue
|
|
59
|
-
};
|
|
60
|
-
}, {});
|
|
61
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
|
-
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
|
-
import { sanitizeReactNativeBlockStyles } from './sanitize-react-native-block-styles.js';
|
|
4
|
-
export function getReactNativeBlockStyles({
|
|
5
|
-
block,
|
|
6
|
-
context,
|
|
7
|
-
blockStyles
|
|
8
|
-
}: {
|
|
9
|
-
block: BuilderBlock;
|
|
10
|
-
context: BuilderContextInterface;
|
|
11
|
-
blockStyles: any;
|
|
12
|
-
}): CSSStyleDeclaration | Record<string, string | undefined> {
|
|
13
|
-
const responsiveStyles = block.responsiveStyles;
|
|
14
|
-
if (!responsiveStyles) {
|
|
15
|
-
return {};
|
|
16
|
-
}
|
|
17
|
-
const styles = ({
|
|
18
|
-
// recursively apply inherited styles so that they can be passed down to children `Text` blocks
|
|
19
|
-
...context.inheritedStyles,
|
|
20
|
-
...(responsiveStyles.large || {}),
|
|
21
|
-
...(responsiveStyles.medium || {}),
|
|
22
|
-
...(responsiveStyles.small || {}),
|
|
23
|
-
...blockStyles
|
|
24
|
-
} as Record<string, string | number>);
|
|
25
|
-
const newStyles = ((sanitizeReactNativeBlockStyles(styles) as any) as CSSStyleDeclaration);
|
|
26
|
-
return newStyles;
|
|
27
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
const propertiesThatMustBeNumber = new Set(['lineHeight']);
|
|
2
|
-
const displayValues = new Set(['flex', 'none']);
|
|
3
|
-
const SHOW_WARNINGS = false;
|
|
4
|
-
type Styles = Record<string, string | number>;
|
|
5
|
-
const normalizeNumber = (value: number): number | undefined => {
|
|
6
|
-
if (Number.isNaN(value)) {
|
|
7
|
-
return undefined;
|
|
8
|
-
} else if (value < 0) {
|
|
9
|
-
// TODO: why are negative values not allowed?
|
|
10
|
-
return 0;
|
|
11
|
-
} else {
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
export const sanitizeReactNativeBlockStyles = (styles: Styles): Styles => {
|
|
16
|
-
return Object.keys(styles).reduce<Styles>((acc, key): Styles => {
|
|
17
|
-
const propertyValue = styles[key];
|
|
18
|
-
if (key === 'display' && !displayValues.has((propertyValue as string))) {
|
|
19
|
-
if (SHOW_WARNINGS) {
|
|
20
|
-
console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
|
|
21
|
-
}
|
|
22
|
-
return acc;
|
|
23
|
-
}
|
|
24
|
-
if (propertiesThatMustBeNumber.has(key) && typeof propertyValue !== 'number') {
|
|
25
|
-
if (SHOW_WARNINGS) {
|
|
26
|
-
console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
|
|
27
|
-
}
|
|
28
|
-
return acc;
|
|
29
|
-
}
|
|
30
|
-
if (typeof propertyValue === 'string') {
|
|
31
|
-
// `px` units need to be stripped and replaced with numbers
|
|
32
|
-
// https://regexr.com/6ualn
|
|
33
|
-
const isPixelUnit = propertyValue.match(/^-?(\d*)(\.?)(\d*)*px$/);
|
|
34
|
-
if (isPixelUnit) {
|
|
35
|
-
const newValue = parseFloat(propertyValue);
|
|
36
|
-
const normalizedValue = normalizeNumber(newValue);
|
|
37
|
-
if (normalizedValue) {
|
|
38
|
-
return {
|
|
39
|
-
...acc,
|
|
40
|
-
[key]: normalizedValue
|
|
41
|
-
};
|
|
42
|
-
} else {
|
|
43
|
-
return acc;
|
|
44
|
-
}
|
|
45
|
-
} else if (propertyValue === '0') {
|
|
46
|
-
// 0 edge case needs to be handled
|
|
47
|
-
return {
|
|
48
|
-
...acc,
|
|
49
|
-
[key]: 0
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
...acc,
|
|
55
|
-
[key]: propertyValue
|
|
56
|
-
};
|
|
57
|
-
}, {});
|
|
58
|
-
}
|