@builder.io/sdk-svelte 0.8.0 → 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.
Files changed (88) hide show
  1. package/lib/browser/components/blocks/blocks-wrapper.svelte +8 -4
  2. package/lib/browser/components/blocks/blocks.svelte +8 -1
  3. package/lib/browser/components/content/components/enable-editor.svelte +7 -2
  4. package/lib/browser/components/content/components/enable-editor.svelte.d.ts +3 -1
  5. package/lib/browser/components/content/content.svelte +8 -0
  6. package/lib/browser/components/content/content.svelte.d.ts +4 -0
  7. package/lib/browser/components/content-variants/content-variants.svelte +12 -0
  8. package/lib/browser/components/content-variants/content-variants.svelte.d.ts +4 -0
  9. package/lib/browser/components/content-variants/content-variants.types.d.ts +16 -0
  10. package/lib/browser/constants/sdk-version.d.ts +1 -1
  11. package/lib/browser/constants/sdk-version.js +1 -1
  12. package/lib/browser/context/builder.context.d.ts +2 -0
  13. package/lib/browser/context/builder.context.js +2 -0
  14. package/lib/browser/context/types.d.ts +2 -1
  15. package/lib/browser/functions/evaluate/node-runtime/safeDynamicRequire.d.ts +0 -10
  16. package/lib/browser/functions/evaluate/node-runtime/safeDynamicRequire.js +10 -3
  17. package/lib/browser/functions/get-block-properties.js +5 -9
  18. package/lib/browser/functions/transform-block-properties.d.ts +7 -1
  19. package/lib/browser/functions/transform-block-properties.js +1 -1
  20. package/lib/browser/types/enforced-partials.d.ts +7 -2
  21. package/lib/edge/components/blocks/blocks-wrapper.svelte +8 -4
  22. package/lib/edge/components/blocks/blocks.svelte +8 -1
  23. package/lib/edge/components/content/components/enable-editor.svelte +7 -2
  24. package/lib/edge/components/content/components/enable-editor.svelte.d.ts +3 -1
  25. package/lib/edge/components/content/content.svelte +8 -0
  26. package/lib/edge/components/content/content.svelte.d.ts +4 -0
  27. package/lib/edge/components/content-variants/content-variants.svelte +12 -0
  28. package/lib/edge/components/content-variants/content-variants.svelte.d.ts +4 -0
  29. package/lib/edge/components/content-variants/content-variants.types.d.ts +16 -0
  30. package/lib/edge/constants/sdk-version.d.ts +1 -1
  31. package/lib/edge/constants/sdk-version.js +1 -1
  32. package/lib/edge/context/builder.context.d.ts +2 -0
  33. package/lib/edge/context/builder.context.js +2 -0
  34. package/lib/edge/context/types.d.ts +2 -1
  35. package/lib/edge/functions/evaluate/node-runtime/safeDynamicRequire.d.ts +0 -10
  36. package/lib/edge/functions/evaluate/node-runtime/safeDynamicRequire.js +10 -3
  37. package/lib/edge/functions/get-block-properties.js +5 -9
  38. package/lib/edge/functions/transform-block-properties.d.ts +7 -1
  39. package/lib/edge/functions/transform-block-properties.js +1 -1
  40. package/lib/edge/types/enforced-partials.d.ts +7 -2
  41. package/lib/node/components/blocks/blocks-wrapper.svelte +8 -4
  42. package/lib/node/components/blocks/blocks.svelte +8 -1
  43. package/lib/node/components/content/components/enable-editor.svelte +7 -2
  44. package/lib/node/components/content/components/enable-editor.svelte.d.ts +3 -1
  45. package/lib/node/components/content/content.svelte +8 -0
  46. package/lib/node/components/content/content.svelte.d.ts +4 -0
  47. package/lib/node/components/content-variants/content-variants.svelte +12 -0
  48. package/lib/node/components/content-variants/content-variants.svelte.d.ts +4 -0
  49. package/lib/node/components/content-variants/content-variants.types.d.ts +16 -0
  50. package/lib/node/constants/sdk-version.d.ts +1 -1
  51. package/lib/node/constants/sdk-version.js +1 -1
  52. package/lib/node/context/builder.context.d.ts +2 -0
  53. package/lib/node/context/builder.context.js +2 -0
  54. package/lib/node/context/types.d.ts +2 -1
  55. package/lib/node/functions/evaluate/node-runtime/safeDynamicRequire.d.ts +0 -10
  56. package/lib/node/functions/evaluate/node-runtime/safeDynamicRequire.js +10 -3
  57. package/lib/node/functions/get-block-properties.js +5 -9
  58. package/lib/node/functions/transform-block-properties.d.ts +7 -1
  59. package/lib/node/functions/transform-block-properties.js +1 -1
  60. package/lib/node/types/enforced-partials.d.ts +7 -2
  61. package/package.json +1 -1
  62. package/src/components/blocks/blocks-wrapper.svelte +16 -4
  63. package/src/components/blocks/blocks.svelte +11 -2
  64. package/src/components/content/components/enable-editor.svelte +14 -3
  65. package/src/components/content/content.svelte +9 -0
  66. package/src/components/content-variants/content-variants.svelte +15 -0
  67. package/src/components/content-variants/content-variants.types.ts +16 -0
  68. package/src/constants/sdk-version.ts +1 -1
  69. package/src/context/builder.context.ts +2 -0
  70. package/src/context/types.ts +2 -1
  71. package/src/functions/evaluate/node-runtime/safeDynamicRequire.ts +9 -3
  72. package/src/functions/get-block-properties.ts +5 -9
  73. package/src/functions/transform-block-properties.ts +9 -1
  74. package/src/types/enforced-partials.ts +8 -2
  75. package/lib/browser/functions/get-react-native-block-styles.d.ts +0 -7
  76. package/lib/browser/functions/get-react-native-block-styles.js +0 -17
  77. package/lib/browser/functions/sanitize-react-native-block-styles.d.ts +0 -3
  78. package/lib/browser/functions/sanitize-react-native-block-styles.js +0 -61
  79. package/lib/edge/functions/get-react-native-block-styles.d.ts +0 -7
  80. package/lib/edge/functions/get-react-native-block-styles.js +0 -17
  81. package/lib/edge/functions/sanitize-react-native-block-styles.d.ts +0 -3
  82. package/lib/edge/functions/sanitize-react-native-block-styles.js +0 -61
  83. package/lib/node/functions/get-react-native-block-styles.d.ts +0 -7
  84. package/lib/node/functions/get-react-native-block-styles.js +0 -17
  85. package/lib/node/functions/sanitize-react-native-block-styles.d.ts +0 -3
  86. package/lib/node/functions/sanitize-react-native-block-styles.js +0 -61
  87. package/src/functions/get-react-native-block-styles.ts +0 -27
  88. package/src/functions/sanitize-react-native-block-styles.ts +0 -58
@@ -1,7 +1,12 @@
1
1
  <script context="module" lang="ts">
2
2
  type BuilderEditorProps = Omit<
3
3
  ContentProps,
4
- "customComponents" | "data" | "apiVersion" | "isSsrAbTest"
4
+ | "customComponents"
5
+ | "data"
6
+ | "apiVersion"
7
+ | "isSsrAbTest"
8
+ | "blocksWrapper"
9
+ | "blocksWrapperProps"
5
10
  > & {
6
11
  builderContextSignal: Writable<BuilderContextInterface>;
7
12
  setBuilderContextSignal?: (signal: any) => any;
@@ -40,6 +45,7 @@
40
45
  import type { Writable } from "svelte/store";
41
46
 
42
47
  export let builderContextSignal: BuilderEditorProps["builderContextSignal"];
48
+ export let contentWrapper: BuilderEditorProps["contentWrapper"] = undefined;
43
49
  export let model: BuilderEditorProps["model"] = undefined;
44
50
  export let context: BuilderEditorProps["context"] = undefined;
45
51
  export let canTrack: BuilderEditorProps["canTrack"] = undefined;
@@ -49,6 +55,8 @@
49
55
  export let enrich: BuilderEditorProps["enrich"] = undefined;
50
56
  export let content: BuilderEditorProps["content"] = undefined;
51
57
  export let showContent: BuilderEditorProps["showContent"];
58
+ export let contentWrapperProps: BuilderEditorProps["contentWrapperProps"] =
59
+ undefined;
52
60
 
53
61
  function mergeNewContent(newContent: BuilderContent) {
54
62
  const newContentValue = {
@@ -265,6 +273,7 @@
265
273
  let forceReRenderCount = 0;
266
274
  let lastUpdated = 0;
267
275
  let shouldSendResetCookie = false;
276
+ let ContentWrapper = contentWrapper || "div";
268
277
  let httpReqsData = {};
269
278
  let clicked = false;
270
279
 
@@ -345,7 +354,8 @@
345
354
  </script>
346
355
 
347
356
  {#if $builderContextSignal.content}
348
- <div
357
+ <svelte:element
358
+ this={ContentWrapper}
349
359
  {...{}}
350
360
  key={forceReRenderCount}
351
361
  bind:this={elementRef}
@@ -361,6 +371,7 @@
361
371
  hidden: true,
362
372
  "aria-hidden": true,
363
373
  }}
374
+ {...contentWrapperProps}
364
375
  class={`variant-${content?.testVariationId || content?.id}`}
365
376
  on:initeditingbldr={(event) => {
366
377
  elementRef_onIniteditingbldr(event);
@@ -370,5 +381,5 @@
370
381
  }}
371
382
  >
372
383
  <slot />
373
- </div>
384
+ </svelte:element>
374
385
  {/if}
@@ -35,11 +35,16 @@
35
35
  export let context: ContentProps["context"] = undefined;
36
36
  export let apiKey: ContentProps["apiKey"];
37
37
  export let apiVersion: ContentProps["apiVersion"] = undefined;
38
+ export let blocksWrapper: ContentProps["blocksWrapper"] = undefined;
39
+ export let blocksWrapperProps: ContentProps["blocksWrapperProps"] = undefined;
38
40
  export let model: ContentProps["model"] = undefined;
39
41
  export let canTrack: ContentProps["canTrack"] = undefined;
40
42
  export let includeRefs: ContentProps["includeRefs"] = undefined;
41
43
  export let enrich: ContentProps["enrich"] = undefined;
42
44
  export let showContent: ContentProps["showContent"];
45
+ export let contentWrapper: ContentProps["contentWrapper"] = undefined;
46
+ export let contentWrapperProps: ContentProps["contentWrapperProps"] =
47
+ undefined;
43
48
  export let isSsrAbTest: ContentProps["isSsrAbTest"];
44
49
 
45
50
  function contentSetState(newRootState: BuilderRenderState) {
@@ -103,6 +108,8 @@
103
108
  {}
104
109
  ),
105
110
  inheritedStyles: {},
111
+ BlocksWrapper: blocksWrapper || "div",
112
+ BlocksWrapperProps: blocksWrapperProps || {},
106
113
  });
107
114
 
108
115
  setContext(ComponentsContext.key, {
@@ -121,6 +128,8 @@
121
128
  {enrich}
122
129
  {showContent}
123
130
  {builderContextSignal}
131
+ {contentWrapper}
132
+ {contentWrapperProps}
124
133
  {...{}}
125
134
  >
126
135
  {#if isSsrAbTest}
@@ -38,6 +38,13 @@
38
38
  export let locale: VariantsProviderProps["locale"] = undefined;
39
39
  export let includeRefs: VariantsProviderProps["includeRefs"] = undefined;
40
40
  export let enrich: VariantsProviderProps["enrich"] = undefined;
41
+ export let blocksWrapper: VariantsProviderProps["blocksWrapper"] = undefined;
42
+ export let blocksWrapperProps: VariantsProviderProps["blocksWrapperProps"] =
43
+ undefined;
44
+ export let contentWrapper: VariantsProviderProps["contentWrapper"] =
45
+ undefined;
46
+ export let contentWrapperProps: VariantsProviderProps["contentWrapperProps"] =
47
+ undefined;
41
48
 
42
49
  $: updateCookieAndStylesScriptStr = () => {
43
50
  return getUpdateCookieAndStylesScript(
@@ -105,6 +112,10 @@
105
112
  {includeRefs}
106
113
  {enrich}
107
114
  isSsrAbTest={shouldRenderVariants}
115
+ {blocksWrapper}
116
+ {blocksWrapperProps}
117
+ {contentWrapper}
118
+ {contentWrapperProps}
108
119
  />
109
120
  {/each}
110
121
  {/if}
@@ -124,4 +135,8 @@
124
135
  {includeRefs}
125
136
  {enrich}
126
137
  isSsrAbTest={shouldRenderVariants}
138
+ {blocksWrapper}
139
+ {blocksWrapperProps}
140
+ {contentWrapper}
141
+ {contentWrapperProps}
127
142
  />
@@ -17,4 +17,20 @@ export interface ContentVariantsPrps {
17
17
  /** @deprecated use `enrich` instead **/
18
18
  includeRefs?: boolean;
19
19
  enrich?: boolean;
20
+ /**
21
+ * The element that wraps your content. Defaults to `div` ('ScrollView' in React Native).
22
+ */
23
+ contentWrapper?: any;
24
+ /**
25
+ * Additonal props to pass to `contentWrapper`. Defaults to `{}`.
26
+ */
27
+ contentWrapperProps?: any;
28
+ /**
29
+ * The element that wraps your blocks. Defaults to `div` ('ScrollView' in React Native).
30
+ */
31
+ blocksWrapper?: any;
32
+ /**
33
+ * Additonal props to pass to `blocksWrapper`. Defaults to `{}`.
34
+ */
35
+ blocksWrapperProps?: any;
20
36
  }
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.8.0"
1
+ export const SDK_VERSION = "0.9.0"
@@ -12,6 +12,8 @@ export default {
12
12
  apiVersion: undefined,
13
13
  componentInfos: {},
14
14
  inheritedStyles: {},
15
+ BlocksWrapper: "div",
16
+ BlocksWrapperProps: {},
15
17
  }),
16
18
  key,
17
19
  };
@@ -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
  /**
@@ -1,4 +1,4 @@
1
- const noop = () => null;
1
+ const noop = () => {};
2
2
 
3
3
  /**
4
4
  * Allows us to require node-specific code safely within bundlers.
@@ -9,10 +9,16 @@ const noop = () => null;
9
9
  * This is guaranteed by the fact that each SDK has separate bundles for each runtime.
10
10
  * However, it might still end up running in the `browser` if the `node` bundle is loaded in the
11
11
  * browser (which happens in many apps' dev mode, and certain other scenarios like Qwik's prod mode).
12
+ * This is why we catch and ignore the error at the very end.
12
13
  */
14
+ import { createRequire } from 'node:module';
13
15
  export let safeDynamicRequire: typeof require = ((noop as unknown) as typeof require);
14
16
  try {
15
- safeDynamicRequire = eval('require');
17
+ safeDynamicRequire = createRequire(import.meta.url);
16
18
  } catch (error) {
17
- /* empty */
19
+ try {
20
+ safeDynamicRequire = eval('require');
21
+ } catch (error) {
22
+ /* empty */
23
+ }
18
24
  }
@@ -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
- if (TARGET === 'reactNative') {
56
- properties.style = getReactNativeBlockStyles({
57
- block,
58
- context,
59
- blockStyles: properties.style
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
- export function transformBlockProperties<T>(properties: T) {
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
- type OptionalFieldsOnly<T> = { [K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K] };
3
- type RequiredFieldsOnly<T> = { [K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K] };
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,3 +0,0 @@
1
- type Styles = Record<string, string | number>;
2
- export declare const sanitizeReactNativeBlockStyles: (styles: Styles) => Styles;
3
- export {};
@@ -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,3 +0,0 @@
1
- type Styles = Record<string, string | number>;
2
- export declare const sanitizeReactNativeBlockStyles: (styles: Styles) => Styles;
3
- export {};
@@ -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,3 +0,0 @@
1
- type Styles = Record<string, string | number>;
2
- export declare const sanitizeReactNativeBlockStyles: (styles: Styles) => Styles;
3
- export {};
@@ -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
- }