@builder.io/sdk-react-nextjs 0.4.6-0 → 0.5.1

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 (70) hide show
  1. package/README.md +32 -44
  2. package/dist/blocks/button/button.d.ts +1 -2
  3. package/dist/blocks/button/button.js +2 -2
  4. package/dist/blocks/columns/columns.js +6 -6
  5. package/dist/blocks/embed/component-info.js +2 -1
  6. package/dist/blocks/image/component-info.js +2 -1
  7. package/dist/blocks/image/image.js +8 -7
  8. package/dist/blocks/img/img.d.ts +1 -2
  9. package/dist/blocks/section/section.d.ts +1 -2
  10. package/dist/blocks/symbol/symbol.d.ts +2 -4
  11. package/dist/blocks/symbol/symbol.helpers.js +2 -1
  12. package/dist/blocks/symbol/symbol.js +3 -3
  13. package/dist/blocks/video/video.js +4 -1
  14. package/dist/components/block/block.helpers.js +5 -3
  15. package/dist/components/block/block.js +4 -4
  16. package/dist/components/block/components/block-styles.js +1 -1
  17. package/dist/components/block/components/block-wrapper.d.ts +1 -2
  18. package/dist/components/block/components/component-ref/component-ref.helpers.d.ts +1 -1
  19. package/dist/components/block/components/component-ref/component-ref.helpers.js +2 -1
  20. package/dist/components/block/components/component-ref/component-ref.js +3 -3
  21. package/dist/components/block/components/repeated-block.js +1 -1
  22. package/dist/components/blocks/blocks.d.ts +3 -3
  23. package/dist/components/blocks/blocks.js +3 -3
  24. package/dist/components/content/components/enable-editor.js +1 -1
  25. package/dist/components/content/components/styles.helpers.js +4 -2
  26. package/dist/components/content/components/styles.js +1 -1
  27. package/dist/components/content/content.helpers.js +10 -5
  28. package/dist/components/content/content.js +6 -4
  29. package/dist/components/content-variants/content-variants.js +3 -3
  30. package/dist/components/content-variants/helpers.js +2 -1
  31. package/dist/constants/builder-registered-components.js +11 -11
  32. package/dist/constants/sdk-version.d.ts +1 -1
  33. package/dist/constants/sdk-version.js +1 -1
  34. package/dist/context/components.context.d.ts +3 -0
  35. package/dist/context/components.context.js +2 -0
  36. package/dist/functions/evaluate/evaluate.d.ts +10 -0
  37. package/dist/functions/evaluate/evaluate.js +70 -0
  38. package/dist/functions/evaluate/index.d.ts +1 -0
  39. package/dist/functions/evaluate/index.js +1 -0
  40. package/dist/functions/evaluate/interpreter.d.ts +2 -0
  41. package/dist/functions/evaluate/interpreter.js +3853 -0
  42. package/dist/functions/evaluate/non-node-runtime.d.ts +2 -0
  43. package/dist/functions/evaluate/non-node-runtime.js +84 -0
  44. package/dist/functions/evaluate/types.d.ts +10 -0
  45. package/dist/functions/evaluate/types.js +1 -0
  46. package/dist/functions/extract-text-styles.js +2 -1
  47. package/dist/functions/get-block-actions-handler.js +1 -1
  48. package/dist/functions/get-block-component-options.js +4 -2
  49. package/dist/functions/get-block-properties.js +2 -1
  50. package/dist/functions/get-content/generate-content-url.js +2 -1
  51. package/dist/functions/get-content/index.d.ts +1 -5
  52. package/dist/functions/get-content/index.js +4 -3
  53. package/dist/functions/get-processed-block.js +7 -4
  54. package/dist/functions/get-react-native-block-styles.js +1 -0
  55. package/dist/functions/is-non-node-server.d.ts +4 -0
  56. package/dist/functions/is-non-node-server.js +8 -0
  57. package/dist/functions/register-component.js +8 -4
  58. package/dist/functions/sanitize-react-native-block-styles.js +6 -3
  59. package/dist/functions/track/helpers.js +2 -1
  60. package/dist/functions/track/index.js +4 -2
  61. package/dist/functions/transform-block.js +1 -0
  62. package/dist/helpers/ab-tests.js +12 -6
  63. package/dist/helpers/cookie.js +2 -1
  64. package/dist/helpers/flatten.js +4 -2
  65. package/dist/helpers/preview-lru-cache/init.d.ts +5 -0
  66. package/dist/helpers/preview-lru-cache/types.js +5 -0
  67. package/dist/index-helpers/blocks-exports.d.ts +10 -10
  68. package/dist/index-helpers/blocks-exports.js +10 -10
  69. package/dist/scripts/init-editing.js +2 -1
  70. package/package.json +1 -1
@@ -0,0 +1,10 @@
1
+ import type { BuilderContextInterface, BuilderRenderState } from '../../context/types.js';
2
+ import type { ExecutorArgs } from './types.js';
3
+ export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression }: {
4
+ code: string;
5
+ event?: Event;
6
+ isExpression?: boolean;
7
+ } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): any;
8
+ export declare const runInBrowser: ({ useCode, builder, context, event, localState, rootSetState, rootState }: ExecutorArgs) => any;
9
+ export declare const runInNode: (args: ExecutorArgs) => any;
10
+ export declare function flattenState(rootState: Record<string | symbol, any>, localState: Record<string | symbol, any> | undefined, rootSetState: ((rootState: BuilderRenderState) => void) | undefined): BuilderRenderState;
@@ -0,0 +1,70 @@
1
+ import { logger } from '../../helpers/logger.js';
2
+ import { isBrowser } from '../is-browser.js';
3
+ import { isEditing } from '../is-editing.js';
4
+ import { isNonNodeServer } from '../is-non-node-server.js';
5
+ import { runInNonNode } from './non-node-runtime.js';
6
+ export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true }) {
7
+ if (code === '') {
8
+ logger.warn('Skipping evaluation of empty code block.');
9
+ return;
10
+ }
11
+ const builder = {
12
+ isEditing: isEditing(),
13
+ isBrowser: isBrowser(),
14
+ isServer: !isBrowser()
15
+ };
16
+ // Be able to handle simple expressions like "state.foo" or "1 + 1"
17
+ // as well as full blocks like "var foo = "bar"; return foo"
18
+ const useReturn =
19
+ // we disable this for cases where we definitely don't want a return
20
+ isExpression && !(code.includes(';') || code.includes(' return ') || code.trim().startsWith('return '));
21
+ const useCode = useReturn ? `return (${code});` : code;
22
+ const args = {
23
+ useCode,
24
+ builder,
25
+ context,
26
+ event,
27
+ rootSetState,
28
+ rootState,
29
+ localState
30
+ };
31
+ if (isBrowser())
32
+ return runInBrowser(args);
33
+ if (isNonNodeServer())
34
+ return runInNonNode(args);
35
+ return runInNode(args);
36
+ }
37
+ export const runInBrowser = ({ useCode, builder, context, event, localState, rootSetState, rootState }) => {
38
+ const state = flattenState(rootState, localState, rootSetState);
39
+ try {
40
+ return new Function('builder', 'Builder' /* <- legacy */, 'state', 'context', 'event', useCode)(builder, builder, state, context, event);
41
+ }
42
+ catch (e) {
43
+ logger.warn('Builder custom code error: \n While Evaluating: \n ', useCode, '\n', e);
44
+ }
45
+ };
46
+ export const runInNode = (args) => {
47
+ // TO-DO: use vm-isolate
48
+ return runInBrowser(args);
49
+ };
50
+ export function flattenState(rootState, localState, rootSetState) {
51
+ if (rootState === localState) {
52
+ throw new Error('rootState === localState');
53
+ }
54
+ return new Proxy(rootState, {
55
+ get: (_, prop) => {
56
+ if (localState && prop in localState) {
57
+ return localState[prop];
58
+ }
59
+ return rootState[prop];
60
+ },
61
+ set: (_, prop, value) => {
62
+ if (localState && prop in localState) {
63
+ throw new Error('Writing to local state is not allowed as it is read-only.');
64
+ }
65
+ rootState[prop] = value;
66
+ rootSetState?.(rootState);
67
+ return true;
68
+ }
69
+ });
70
+ }
@@ -0,0 +1 @@
1
+ export { evaluate } from './evaluate';
@@ -0,0 +1 @@
1
+ export { evaluate } from './evaluate';
@@ -0,0 +1,2 @@
1
+ declare const _default: Class;
2
+ export default _default;