@builder.io/sdk-svelte 0.2.2 → 0.3.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.
- package/package/--package.json +3 -1
- package/package/CHANGELOG.md +4 -0
- package/package/blocks/columns/columns.svelte +21 -24
- package/package/blocks/columns/columns.svelte.d.ts +1 -1
- package/package/blocks/image/image.svelte +1 -2
- package/package/blocks/image/image.svelte.d.ts +2 -2
- package/package/blocks/symbol/symbol.svelte +1 -3
- package/package/blocks/symbol/symbol.svelte.d.ts +2 -2
- package/package/components/render-block/block-styles.svelte +3 -3
- package/package/components/render-block/block-styles.svelte.d.ts +2 -2
- package/package/components/render-block/render-block.helpers.d.ts +0 -1
- package/package/components/render-block/render-block.helpers.js +8 -20
- package/package/components/render-block/render-block.svelte +22 -19
- package/package/components/render-block/render-block.svelte.d.ts +2 -2
- package/package/components/render-block/render-component.svelte +1 -3
- package/package/components/render-block/render-component.svelte.d.ts +2 -2
- package/package/components/render-block/render-repeated-block.svelte +4 -4
- package/package/components/render-block/render-repeated-block.svelte.d.ts +2 -2
- package/package/components/render-blocks.svelte +0 -1
- package/package/components/render-blocks.svelte.d.ts +1 -1
- package/package/components/render-content/components/render-styles.svelte +8 -9
- package/package/components/render-content/components/render-styles.svelte.d.ts +1 -1
- package/package/components/render-content/render-content.svelte +18 -12
- package/package/components/render-content/render-content.svelte.d.ts +1 -1
- package/package/components/render-inlined-styles.svelte +2 -2
- package/package/constants/sdk-version.d.ts +1 -0
- package/package/constants/sdk-version.js +1 -0
- package/package/context/builder.context.d.ts +3 -2
- package/package/context/builder.context.js +3 -2
- package/package/context/types.d.ts +17 -2
- package/package/functions/evaluate.d.ts +4 -3
- package/package/functions/evaluate.js +23 -2
- package/package/functions/evaluate.test.d.ts +1 -0
- package/package/functions/evaluate.test.js +17 -0
- package/package/functions/get-block-actions-handler.d.ts +1 -1
- package/package/functions/get-block-actions-handler.js +3 -1
- package/package/functions/get-block-actions.d.ts +1 -1
- package/package/functions/get-content/index.js +1 -1
- package/package/functions/get-processed-block.d.ts +2 -2
- package/package/functions/get-processed-block.js +16 -4
- package/package/functions/get-processed-block.test.js +3 -1
- package/package/index.d.ts +1 -0
- package/package/scripts/init-editing.js +2 -0
- package/package.json +1 -4
- package/src/blocks/columns/columns.svelte +26 -30
- package/src/blocks/image/image.svelte +3 -3
- package/src/blocks/symbol/symbol.svelte +3 -3
- package/src/components/render-block/block-styles.svelte +5 -3
- package/src/components/render-block/render-block.helpers.ts +9 -31
- package/src/components/render-block/render-block.svelte +23 -18
- package/src/components/render-block/render-component.svelte +2 -2
- package/src/components/render-block/render-repeated-block.svelte +6 -4
- package/src/components/render-blocks.svelte +1 -1
- package/src/components/render-content/components/render-styles.svelte +9 -9
- package/src/components/render-content/render-content.svelte +25 -12
- package/src/components/render-inlined-styles.svelte +2 -2
- package/src/constants/sdk-version.ts +1 -0
- package/src/context/builder.context.ts +3 -2
- package/src/context/types.ts +17 -2
- package/src/functions/evaluate.test.ts +21 -0
- package/src/functions/evaluate.ts +46 -4
- package/src/functions/get-block-actions-handler.ts +7 -2
- package/src/functions/get-block-actions.ts +4 -1
- package/src/functions/get-content/index.ts +1 -1
- package/src/functions/get-processed-block.test.ts +3 -1
- package/src/functions/get-processed-block.ts +28 -6
- package/src/index.ts +1 -0
- package/src/scripts/init-editing.ts +2 -0
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
<script lang="ts">
|
|
10
10
|
import RenderInlinedStyles from "../../render-inlined-styles.svelte";
|
|
11
|
-
import { CustomFont } from "./render-styles.helpers";
|
|
11
|
+
import type { CustomFont } from "./render-styles.helpers";
|
|
12
12
|
import { getCss } from "./render-styles.helpers";
|
|
13
13
|
import { getFontCss } from "./render-styles.helpers";
|
|
14
14
|
|
|
@@ -26,16 +26,16 @@ ${getFontCss({
|
|
|
26
26
|
})}
|
|
27
27
|
|
|
28
28
|
.builder-text > p:first-of-type, .builder-text > .builder-paragraph:first-of-type {
|
|
29
|
-
margin: 0;
|
|
29
|
+
margin: 0;
|
|
30
30
|
}
|
|
31
31
|
.builder-text > p, .builder-text > .builder-paragraph {
|
|
32
|
-
color: inherit;
|
|
33
|
-
line-height: inherit;
|
|
34
|
-
letter-spacing: inherit;
|
|
35
|
-
font-weight: inherit;
|
|
36
|
-
font-size: inherit;
|
|
37
|
-
text-align: inherit;
|
|
38
|
-
font-family: inherit;
|
|
32
|
+
color: inherit;
|
|
33
|
+
line-height: inherit;
|
|
34
|
+
letter-spacing: inherit;
|
|
35
|
+
font-weight: inherit;
|
|
36
|
+
font-size: inherit;
|
|
37
|
+
text-align: inherit;
|
|
38
|
+
font-family: inherit;
|
|
39
39
|
}
|
|
40
40
|
`;
|
|
41
41
|
</script>
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { onDestroy, onMount, setContext } from "svelte";
|
|
3
3
|
|
|
4
4
|
import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
|
|
5
|
-
import {
|
|
5
|
+
import type {
|
|
6
6
|
BuilderRenderState,
|
|
7
7
|
RegisteredComponent,
|
|
8
8
|
RegisteredComponents,
|
|
@@ -18,8 +18,11 @@
|
|
|
18
18
|
createRegisterComponentMessage,
|
|
19
19
|
} from "../../functions/register-component.js";
|
|
20
20
|
import { _track } from "../../functions/track/index.js";
|
|
21
|
-
import {
|
|
22
|
-
|
|
21
|
+
import type {
|
|
22
|
+
Breakpoints,
|
|
23
|
+
BuilderContent,
|
|
24
|
+
} from "../../types/builder-content.js";
|
|
25
|
+
import type { Nullable } from "../../types/typescript.js";
|
|
23
26
|
import RenderBlocks from "../render-blocks.svelte";
|
|
24
27
|
import RenderContentStyles from "./components/render-styles.svelte";
|
|
25
28
|
import builderContext from "../../context/builder.context.js";
|
|
@@ -29,7 +32,7 @@
|
|
|
29
32
|
} from "../../scripts/init-editing.js";
|
|
30
33
|
import { checkIsDefined } from "../../helpers/nullable.js";
|
|
31
34
|
import { getInteractionPropertiesForEvent } from "../../functions/track/interaction.js";
|
|
32
|
-
import {
|
|
35
|
+
import type {
|
|
33
36
|
RenderContentProps,
|
|
34
37
|
BuilderComponentStateChange,
|
|
35
38
|
} from "./render-content.types.js";
|
|
@@ -76,8 +79,8 @@
|
|
|
76
79
|
},
|
|
77
80
|
};
|
|
78
81
|
}
|
|
79
|
-
function
|
|
80
|
-
contentState =
|
|
82
|
+
function contentSetState(newRootState: BuilderRenderState) {
|
|
83
|
+
contentState = newRootState;
|
|
81
84
|
}
|
|
82
85
|
function processMessage(event: MessageEvent) {
|
|
83
86
|
const { data } = event;
|
|
@@ -124,7 +127,9 @@
|
|
|
124
127
|
evaluate({
|
|
125
128
|
code: jsCode,
|
|
126
129
|
context: context || {},
|
|
127
|
-
|
|
130
|
+
localState: undefined,
|
|
131
|
+
rootState: contentState,
|
|
132
|
+
rootSetState: contentSetState,
|
|
128
133
|
});
|
|
129
134
|
}
|
|
130
135
|
}
|
|
@@ -151,7 +156,9 @@
|
|
|
151
156
|
evaluate({
|
|
152
157
|
code: group,
|
|
153
158
|
context: context || {},
|
|
154
|
-
|
|
159
|
+
localState: undefined,
|
|
160
|
+
rootState: contentState,
|
|
161
|
+
rootSetState: contentSetState,
|
|
155
162
|
})
|
|
156
163
|
);
|
|
157
164
|
}
|
|
@@ -163,7 +170,7 @@
|
|
|
163
170
|
...contentState,
|
|
164
171
|
[key]: json,
|
|
165
172
|
};
|
|
166
|
-
|
|
173
|
+
contentSetState(newState);
|
|
167
174
|
})
|
|
168
175
|
.catch((err) => {
|
|
169
176
|
console.error("error fetching dynamic data", url, err);
|
|
@@ -345,11 +352,14 @@
|
|
|
345
352
|
get content() {
|
|
346
353
|
return useContent;
|
|
347
354
|
},
|
|
348
|
-
get
|
|
355
|
+
get localState() {
|
|
356
|
+
return undefined;
|
|
357
|
+
},
|
|
358
|
+
get rootState() {
|
|
349
359
|
return contentState;
|
|
350
360
|
},
|
|
351
|
-
get
|
|
352
|
-
return
|
|
361
|
+
get rootSetState() {
|
|
362
|
+
return TARGET === "qwik" ? undefined : contentSetState;
|
|
353
363
|
},
|
|
354
364
|
get context() {
|
|
355
365
|
return context || {};
|
|
@@ -363,6 +373,9 @@
|
|
|
363
373
|
get registeredComponents() {
|
|
364
374
|
return allRegisteredComponents;
|
|
365
375
|
},
|
|
376
|
+
get inheritedStyles() {
|
|
377
|
+
return {};
|
|
378
|
+
},
|
|
366
379
|
});
|
|
367
380
|
|
|
368
381
|
onDestroy(() => {
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
export let styles: Props["styles"];
|
|
11
11
|
|
|
12
12
|
$: tag = () => {
|
|
13
|
-
// NOTE: we have to
|
|
13
|
+
// NOTE: we have to obfuscate the name of the tag due to a limitation in the svelte-preprocessor plugin.
|
|
14
14
|
// https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
|
|
15
|
-
return "sty" + "le";
|
|
15
|
+
return ("sty" + "le") as any;
|
|
16
16
|
};
|
|
17
17
|
$: injectedStyleScript = () => {
|
|
18
18
|
return `<${tag()}>${styles}</${tag()}>`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SDK_VERSION = "0.3.1"
|
package/src/context/types.ts
CHANGED
|
@@ -16,8 +16,23 @@ export type BuilderRenderContext = Record<string, unknown>;
|
|
|
16
16
|
export interface BuilderContextInterface {
|
|
17
17
|
content: Nullable<BuilderContent>;
|
|
18
18
|
context: BuilderRenderContext;
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The state of the application.
|
|
21
|
+
*
|
|
22
|
+
* NOTE: see `localState` below to understand how it is different from `rootState`.
|
|
23
|
+
*/
|
|
24
|
+
rootState: BuilderRenderState;
|
|
25
|
+
/**
|
|
26
|
+
* Some frameworks have a `setState` function which needs to be invoked to notify
|
|
27
|
+
* the framework of state change. (other frameworks don't in which case it is `undefined')
|
|
28
|
+
*/
|
|
29
|
+
rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* The local state of the current component. This is different from `rootState` in that
|
|
32
|
+
* it can be a child state created by a repeater containing local state.
|
|
33
|
+
* The `rootState` is where all of the state mutations are actually stored.
|
|
34
|
+
*/
|
|
35
|
+
localState: BuilderRenderState | undefined;
|
|
21
36
|
apiKey: string | null;
|
|
22
37
|
apiVersion: ApiVersion | undefined;
|
|
23
38
|
registeredComponents: RegisteredComponents;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { flattenState } from './evaluate';
|
|
2
|
+
|
|
3
|
+
describe('flatten state', () => {
|
|
4
|
+
it('should behave normally when no PROTO_STATE', () => {
|
|
5
|
+
const localState = {};
|
|
6
|
+
const rootState = { foo: 'bar' };
|
|
7
|
+
const flattened = flattenState(rootState, localState, undefined);
|
|
8
|
+
expect(flattened.foo).toEqual('bar');
|
|
9
|
+
flattened.foo = 'baz';
|
|
10
|
+
expect(rootState.foo).toEqual('baz');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should shadow write ', () => {
|
|
14
|
+
const rootState = { foo: 'foo' };
|
|
15
|
+
const localState = { foo: 'baz' };
|
|
16
|
+
const flattened = flattenState(rootState, localState, undefined);
|
|
17
|
+
expect(() => (flattened.foo = 'bar')).toThrow(
|
|
18
|
+
'Writing to local state is not allowed as it is read-only.'
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
BuilderContextInterface,
|
|
3
|
+
BuilderRenderState,
|
|
4
|
+
} from '../context/types.js';
|
|
2
5
|
import { isBrowser } from './is-browser.js';
|
|
3
6
|
import { isEditing } from './is-editing.js';
|
|
4
7
|
|
|
5
8
|
export function evaluate({
|
|
6
9
|
code,
|
|
7
10
|
context,
|
|
8
|
-
|
|
11
|
+
localState,
|
|
12
|
+
rootState,
|
|
13
|
+
rootSetState,
|
|
9
14
|
event,
|
|
10
15
|
isExpression = true,
|
|
11
16
|
}: {
|
|
12
17
|
code: string;
|
|
13
18
|
event?: Event;
|
|
14
19
|
isExpression?: boolean;
|
|
15
|
-
} & Pick<
|
|
20
|
+
} & Pick<
|
|
21
|
+
BuilderContextInterface,
|
|
22
|
+
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
23
|
+
>): any {
|
|
16
24
|
if (code === '') {
|
|
17
25
|
console.warn('Skipping evaluation of empty code block.');
|
|
18
26
|
return;
|
|
@@ -45,7 +53,13 @@ export function evaluate({
|
|
|
45
53
|
'context',
|
|
46
54
|
'event',
|
|
47
55
|
useCode
|
|
48
|
-
)(
|
|
56
|
+
)(
|
|
57
|
+
builder,
|
|
58
|
+
builder,
|
|
59
|
+
flattenState(rootState, localState, rootSetState),
|
|
60
|
+
context,
|
|
61
|
+
event
|
|
62
|
+
);
|
|
49
63
|
} catch (e) {
|
|
50
64
|
console.warn(
|
|
51
65
|
'Builder custom code error: \n While Evaluating: \n ',
|
|
@@ -55,3 +69,31 @@ export function evaluate({
|
|
|
55
69
|
);
|
|
56
70
|
}
|
|
57
71
|
}
|
|
72
|
+
|
|
73
|
+
export function flattenState(
|
|
74
|
+
rootState: Record<string | symbol, any>,
|
|
75
|
+
localState: Record<string | symbol, any> | undefined,
|
|
76
|
+
rootSetState: ((rootState: BuilderRenderState) => void) | undefined
|
|
77
|
+
): BuilderRenderState {
|
|
78
|
+
if (rootState === localState) {
|
|
79
|
+
throw new Error('rootState === localState');
|
|
80
|
+
}
|
|
81
|
+
return new Proxy(rootState, {
|
|
82
|
+
get: (_, prop) => {
|
|
83
|
+
if (localState && prop in localState) {
|
|
84
|
+
return localState[prop];
|
|
85
|
+
}
|
|
86
|
+
return rootState[prop as string];
|
|
87
|
+
},
|
|
88
|
+
set: (_, prop, value) => {
|
|
89
|
+
if (localState && prop in localState) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
'Writing to local state is not allowed as it is read-only.'
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
rootState[prop as string] = value;
|
|
95
|
+
rootSetState?.(rootState);
|
|
96
|
+
return true;
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -4,7 +4,10 @@ import { evaluate } from './evaluate.js';
|
|
|
4
4
|
|
|
5
5
|
type Options = {
|
|
6
6
|
block: BuilderBlock;
|
|
7
|
-
} & Pick<
|
|
7
|
+
} & Pick<
|
|
8
|
+
BuilderContextInterface,
|
|
9
|
+
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
10
|
+
>;
|
|
8
11
|
|
|
9
12
|
type EventHandler = (event: Event) => any;
|
|
10
13
|
|
|
@@ -14,7 +17,9 @@ export const createEventHandler =
|
|
|
14
17
|
evaluate({
|
|
15
18
|
code: value,
|
|
16
19
|
context: options.context,
|
|
17
|
-
|
|
20
|
+
localState: options.localState,
|
|
21
|
+
rootState: options.rootState,
|
|
22
|
+
rootSetState: options.rootSetState,
|
|
18
23
|
event,
|
|
19
24
|
isExpression: false,
|
|
20
25
|
});
|
|
@@ -8,7 +8,10 @@ type Actions = { [index: string]: (event: Event) => any };
|
|
|
8
8
|
export function getBlockActions(
|
|
9
9
|
options: {
|
|
10
10
|
block: BuilderBlock;
|
|
11
|
-
} & Pick<
|
|
11
|
+
} & Pick<
|
|
12
|
+
BuilderContextInterface,
|
|
13
|
+
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
14
|
+
>
|
|
12
15
|
): Actions {
|
|
13
16
|
const obj: Actions = {};
|
|
14
17
|
const optionActions = options.block.actions ?? {};
|
|
@@ -35,7 +35,7 @@ export async function getAllContent(
|
|
|
35
35
|
const content = await (res.json() as Promise<ContentResponse>);
|
|
36
36
|
|
|
37
37
|
if ('status' in content && !('results' in content)) {
|
|
38
|
-
logger.error('Error fetching data. ', content, options);
|
|
38
|
+
logger.error('Error fetching data. ', { url, content, options });
|
|
39
39
|
return content;
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -21,7 +21,9 @@ test('Can process bindings', () => {
|
|
|
21
21
|
const processed = getProcessedBlock({
|
|
22
22
|
block,
|
|
23
23
|
context: {},
|
|
24
|
-
|
|
24
|
+
rootState: { test: 'hello' },
|
|
25
|
+
rootSetState: undefined,
|
|
26
|
+
localState: undefined,
|
|
25
27
|
shouldEvaluateBindings: true,
|
|
26
28
|
});
|
|
27
29
|
expect(processed).not.toEqual(block);
|
|
@@ -8,10 +8,15 @@ import { transformBlock } from './transform-block.js';
|
|
|
8
8
|
const evaluateBindings = ({
|
|
9
9
|
block,
|
|
10
10
|
context,
|
|
11
|
-
|
|
11
|
+
localState,
|
|
12
|
+
rootState,
|
|
13
|
+
rootSetState,
|
|
12
14
|
}: {
|
|
13
15
|
block: BuilderBlock;
|
|
14
|
-
} & Pick<
|
|
16
|
+
} & Pick<
|
|
17
|
+
BuilderContextInterface,
|
|
18
|
+
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
19
|
+
>): BuilderBlock => {
|
|
15
20
|
if (!block.bindings) {
|
|
16
21
|
return block;
|
|
17
22
|
}
|
|
@@ -24,7 +29,13 @@ const evaluateBindings = ({
|
|
|
24
29
|
|
|
25
30
|
for (const binding in block.bindings) {
|
|
26
31
|
const expression = block.bindings[binding];
|
|
27
|
-
const value = evaluate({
|
|
32
|
+
const value = evaluate({
|
|
33
|
+
code: expression,
|
|
34
|
+
localState,
|
|
35
|
+
rootState,
|
|
36
|
+
rootSetState,
|
|
37
|
+
context,
|
|
38
|
+
});
|
|
28
39
|
set(copied, binding, value);
|
|
29
40
|
}
|
|
30
41
|
|
|
@@ -35,7 +46,9 @@ export function getProcessedBlock({
|
|
|
35
46
|
block,
|
|
36
47
|
context,
|
|
37
48
|
shouldEvaluateBindings,
|
|
38
|
-
|
|
49
|
+
localState,
|
|
50
|
+
rootState,
|
|
51
|
+
rootSetState,
|
|
39
52
|
}: {
|
|
40
53
|
block: BuilderBlock;
|
|
41
54
|
/**
|
|
@@ -43,11 +56,20 @@ export function getProcessedBlock({
|
|
|
43
56
|
* also sometimes too early to consider bindings, e.g. when we might be looking at a repeated block.
|
|
44
57
|
*/
|
|
45
58
|
shouldEvaluateBindings: boolean;
|
|
46
|
-
} & Pick<
|
|
59
|
+
} & Pick<
|
|
60
|
+
BuilderContextInterface,
|
|
61
|
+
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
62
|
+
>): BuilderBlock {
|
|
47
63
|
const transformedBlock = transformBlock(block);
|
|
48
64
|
|
|
49
65
|
if (shouldEvaluateBindings) {
|
|
50
|
-
return evaluateBindings({
|
|
66
|
+
return evaluateBindings({
|
|
67
|
+
block: transformedBlock,
|
|
68
|
+
localState,
|
|
69
|
+
rootState,
|
|
70
|
+
rootSetState,
|
|
71
|
+
context,
|
|
72
|
+
});
|
|
51
73
|
} else {
|
|
52
74
|
return transformedBlock;
|
|
53
75
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,3 +12,4 @@ export * from './functions/get-builder-search-params/index.js';
|
|
|
12
12
|
export { track } from './functions/track/index.js';
|
|
13
13
|
export type { RegisteredComponent } from './context/types';
|
|
14
14
|
export type { ComponentInfo } from './types/components';
|
|
15
|
+
export type { RenderContentProps } from './components/render-content/render-content.types.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SDK_VERSION } from '../constants/sdk-version.js';
|
|
1
2
|
import { TARGET } from '../constants/target.js';
|
|
2
3
|
import { isBrowser } from '../functions/is-browser.js';
|
|
3
4
|
import { register } from '../functions/register.js';
|
|
@@ -40,6 +41,7 @@ export const setupBrowserForEditing = (
|
|
|
40
41
|
type: 'builder.sdkInfo',
|
|
41
42
|
data: {
|
|
42
43
|
target: TARGET,
|
|
44
|
+
version: SDK_VERSION,
|
|
43
45
|
// TODO: compile these in
|
|
44
46
|
// type: process.env.SDK_TYPE,
|
|
45
47
|
// version: process.env.SDK_VERSION,
|