@webstudio-is/react-sdk 0.174.0 → 0.179.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/index.js CHANGED
@@ -326,17 +326,14 @@ import {
326
326
  } from "react";
327
327
 
328
328
  // src/context.tsx
329
- import { createContext, useContext } from "react";
329
+ import { createContext, useContext, useMemo } from "react";
330
+ import { createJsonStringifyProxy, isPlainObject } from "@webstudio-is/sdk";
330
331
  var ReactSdkContext = createContext({
331
332
  assetBaseUrl: "/",
332
333
  imageBaseUrl: "/",
333
334
  imageLoader: ({ src }) => src,
334
335
  resources: {}
335
336
  });
336
- var useResource = (name) => {
337
- const { resources } = useContext(ReactSdkContext);
338
- return resources[name];
339
- };
340
337
 
341
338
  // src/tree/create-elements-tree.tsx
342
339
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -539,9 +536,6 @@ var showAttribute = "data-ws-show";
539
536
  var indexAttribute = "data-ws-index";
540
537
  var collapsedAttribute = "data-ws-collapsed";
541
538
  var textContentAttribute = "data-ws-text-content";
542
- var getIndexWithinAncestorFromComponentProps = (props) => {
543
- return props[indexAttribute];
544
- };
545
539
 
546
540
  // src/prop-meta.ts
547
541
  import { z } from "zod";
@@ -577,10 +571,14 @@ var Code = z.object({
577
571
  ...common,
578
572
  control: z.literal("code"),
579
573
  type: z.literal("string"),
574
+ language: z.union([z.literal("html"), z.literal("markdown")]),
580
575
  defaultValue: z.string().optional()
581
576
  });
582
- var CodeText = Code.extend({
583
- control: z.literal("codetext")
577
+ var CodeText = z.object({
578
+ ...common,
579
+ control: z.literal("codetext"),
580
+ type: z.literal("string"),
581
+ defaultValue: z.string().optional()
584
582
  });
585
583
  var Color = z.object({
586
584
  ...common,
@@ -1187,28 +1185,6 @@ var getIndexesWithinAncestors = (metas, instances, rootIds) => {
1187
1185
  return indexes;
1188
1186
  };
1189
1187
 
1190
- // src/hook.ts
1191
- var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
1192
- let matched = false;
1193
- for (const instance of instancePath) {
1194
- if (currentInstance === instance) {
1195
- matched = true;
1196
- }
1197
- if (matched && instance.component === closestComponent) {
1198
- return instance;
1199
- }
1200
- }
1201
- };
1202
- var getInstanceSelectorById = (instanceSelector, instanceId) => {
1203
- const index = instanceSelector.findIndex(
1204
- (selector) => selector === instanceId
1205
- );
1206
- if (index === -1) {
1207
- return [];
1208
- }
1209
- return instanceSelector.slice(index);
1210
- };
1211
-
1212
1188
  // src/component-generator.ts
1213
1189
  import {
1214
1190
  parseComponentName as parseComponentName2,
@@ -1544,7 +1520,7 @@ var generateWebstudioComponent = ({
1544
1520
  );
1545
1521
  const initialValue = dataSource.value.value;
1546
1522
  const initialValueString = JSON.stringify(initialValue);
1547
- generatedDataSources += `let [${valueName}, ${setterName}] = useState<any>(${initialValueString})
1523
+ generatedDataSources += `let [${valueName}, ${setterName}] = useVariableState<any>(${initialValueString})
1548
1524
  `;
1549
1525
  }
1550
1526
  if (dataSource.type === "resource") {
@@ -1572,7 +1548,6 @@ export {
1572
1548
  EmbedTemplateProp,
1573
1549
  EmbedTemplateStyleDecl,
1574
1550
  PropMeta,
1575
- ReactSdkContext,
1576
1551
  WsComponentMeta,
1577
1552
  WsEmbedTemplate,
1578
1553
  addGlobalRules,
@@ -1594,10 +1569,7 @@ export {
1594
1569
  generateRemixParams,
1595
1570
  generateRemixRoute,
1596
1571
  generateWebstudioComponent,
1597
- getClosestInstance,
1598
- getIndexWithinAncestorFromComponentProps,
1599
1572
  getIndexesWithinAncestors,
1600
- getInstanceSelectorById,
1601
1573
  idAttribute,
1602
1574
  indexAttribute,
1603
1575
  isCoreComponent,
@@ -1607,6 +1579,5 @@ export {
1607
1579
  selectorIdAttribute,
1608
1580
  showAttribute,
1609
1581
  stateCategories,
1610
- textContentAttribute,
1611
- useResource
1582
+ textContentAttribute
1612
1583
  };
package/lib/runtime.js ADDED
@@ -0,0 +1,65 @@
1
+ // src/context.tsx
2
+ import { createContext, useContext, useMemo } from "react";
3
+ import { createJsonStringifyProxy, isPlainObject } from "@webstudio-is/sdk";
4
+ var ReactSdkContext = createContext({
5
+ assetBaseUrl: "/",
6
+ imageBaseUrl: "/",
7
+ imageLoader: ({ src }) => src,
8
+ resources: {}
9
+ });
10
+ var useResource = (name) => {
11
+ const { resources } = useContext(ReactSdkContext);
12
+ const resource = resources[name];
13
+ const resourceMemozied = useMemo(
14
+ () => isPlainObject(resource) ? createJsonStringifyProxy(resource) : resource,
15
+ [resource]
16
+ );
17
+ return resourceMemozied;
18
+ };
19
+
20
+ // src/hook.ts
21
+ var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
22
+ let matched = false;
23
+ for (const instance of instancePath) {
24
+ if (currentInstance === instance) {
25
+ matched = true;
26
+ }
27
+ if (matched && instance.component === closestComponent) {
28
+ return instance;
29
+ }
30
+ }
31
+ };
32
+ var getInstanceSelectorById = (instanceSelector, instanceId) => {
33
+ const index = instanceSelector.findIndex(
34
+ (selector) => selector === instanceId
35
+ );
36
+ if (index === -1) {
37
+ return [];
38
+ }
39
+ return instanceSelector.slice(index);
40
+ };
41
+
42
+ // src/variable-state.tsx
43
+ import { createJsonStringifyProxy as createJsonStringifyProxy2, isPlainObject as isPlainObject2 } from "@webstudio-is/sdk";
44
+ import { useState, useMemo as useMemo2 } from "react";
45
+ var useVariableState = (initialState) => {
46
+ const [state, setState] = useState(initialState);
47
+ const value = useMemo2(
48
+ () => isPlainObject2(state) ? createJsonStringifyProxy2(state) : state,
49
+ [state]
50
+ );
51
+ return [value, setState];
52
+ };
53
+
54
+ // src/runtime.ts
55
+ var getIndexWithinAncestorFromComponentProps = (props) => {
56
+ return props["data-ws-index"];
57
+ };
58
+ export {
59
+ ReactSdkContext,
60
+ getClosestInstance,
61
+ getIndexWithinAncestorFromComponentProps,
62
+ getInstanceSelectorById,
63
+ useResource,
64
+ useVariableState
65
+ };
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import type { htmlTags as HtmlTags } from "html-tags";
2
+ import type { HtmlTags } from "html-tags";
3
3
  import { EmbedTemplateStyleDecl } from "../embed-template";
4
4
  export type PresetStyle<Tag extends HtmlTags = HtmlTags> = Partial<Record<Tag, EmbedTemplateStyleDecl[]>>;
5
5
  declare const WsComponentPropsMeta: z.ZodObject<{
@@ -72,6 +72,7 @@ declare const WsComponentPropsMeta: z.ZodObject<{
72
72
  }>, z.ZodObject<{
73
73
  control: z.ZodLiteral<"code">;
74
74
  type: z.ZodLiteral<"string">;
75
+ language: z.ZodUnion<[z.ZodLiteral<"html">, z.ZodLiteral<"markdown">]>;
75
76
  defaultValue: z.ZodOptional<z.ZodString>;
76
77
  label: z.ZodOptional<z.ZodString>;
77
78
  description: z.ZodOptional<z.ZodString>;
@@ -79,6 +80,7 @@ declare const WsComponentPropsMeta: z.ZodObject<{
79
80
  }, "strip", z.ZodTypeAny, {
80
81
  type: "string";
81
82
  required: boolean;
83
+ language: "html" | "markdown";
82
84
  control: "code";
83
85
  defaultValue?: string | undefined;
84
86
  label?: string | undefined;
@@ -86,31 +88,32 @@ declare const WsComponentPropsMeta: z.ZodObject<{
86
88
  }, {
87
89
  type: "string";
88
90
  required: boolean;
91
+ language: "html" | "markdown";
89
92
  control: "code";
90
93
  defaultValue?: string | undefined;
91
94
  label?: string | undefined;
92
95
  description?: string | undefined;
93
96
  }>, z.ZodObject<{
97
+ control: z.ZodLiteral<"codetext">;
94
98
  type: z.ZodLiteral<"string">;
95
- required: z.ZodBoolean;
96
- description: z.ZodOptional<z.ZodString>;
97
- label: z.ZodOptional<z.ZodString>;
98
99
  defaultValue: z.ZodOptional<z.ZodString>;
99
- control: z.ZodLiteral<"codetext">;
100
+ label: z.ZodOptional<z.ZodString>;
101
+ description: z.ZodOptional<z.ZodString>;
102
+ required: z.ZodBoolean;
100
103
  }, "strip", z.ZodTypeAny, {
101
104
  type: "string";
102
105
  required: boolean;
103
106
  control: "codetext";
104
- description?: string | undefined;
105
- label?: string | undefined;
106
107
  defaultValue?: string | undefined;
108
+ label?: string | undefined;
109
+ description?: string | undefined;
107
110
  }, {
108
111
  type: "string";
109
112
  required: boolean;
110
113
  control: "codetext";
111
- description?: string | undefined;
112
- label?: string | undefined;
113
114
  defaultValue?: string | undefined;
115
+ label?: string | undefined;
116
+ description?: string | undefined;
114
117
  }>, z.ZodObject<{
115
118
  control: z.ZodLiteral<"color">;
116
119
  type: z.ZodLiteral<"string">;
@@ -454,6 +457,7 @@ declare const WsComponentPropsMeta: z.ZodObject<{
454
457
  } | {
455
458
  type: "string";
456
459
  required: boolean;
460
+ language: "html" | "markdown";
457
461
  control: "code";
458
462
  defaultValue?: string | undefined;
459
463
  label?: string | undefined;
@@ -462,9 +466,9 @@ declare const WsComponentPropsMeta: z.ZodObject<{
462
466
  type: "string";
463
467
  required: boolean;
464
468
  control: "codetext";
465
- description?: string | undefined;
466
- label?: string | undefined;
467
469
  defaultValue?: string | undefined;
470
+ label?: string | undefined;
471
+ description?: string | undefined;
468
472
  } | {
469
473
  type: "string";
470
474
  required: boolean;
@@ -598,6 +602,7 @@ declare const WsComponentPropsMeta: z.ZodObject<{
598
602
  } | {
599
603
  type: "string";
600
604
  required: boolean;
605
+ language: "html" | "markdown";
601
606
  control: "code";
602
607
  defaultValue?: string | undefined;
603
608
  label?: string | undefined;
@@ -606,9 +611,9 @@ declare const WsComponentPropsMeta: z.ZodObject<{
606
611
  type: "string";
607
612
  required: boolean;
608
613
  control: "codetext";
609
- description?: string | undefined;
610
- label?: string | undefined;
611
614
  defaultValue?: string | undefined;
615
+ label?: string | undefined;
616
+ description?: string | undefined;
612
617
  } | {
613
618
  type: "string";
614
619
  required: boolean;
@@ -33,6 +33,7 @@ export declare const corePropsMetas: {
33
33
  } | {
34
34
  type: "string";
35
35
  required: boolean;
36
+ language: "html" | "markdown";
36
37
  control: "code";
37
38
  defaultValue?: string | undefined;
38
39
  label?: string | undefined;
@@ -41,9 +42,9 @@ export declare const corePropsMetas: {
41
42
  type: "string";
42
43
  required: boolean;
43
44
  control: "codetext";
44
- description?: string | undefined;
45
- label?: string | undefined;
46
45
  defaultValue?: string | undefined;
46
+ label?: string | undefined;
47
+ description?: string | undefined;
47
48
  } | {
48
49
  type: "string";
49
50
  required: boolean;
@@ -178,6 +179,7 @@ export declare const corePropsMetas: {
178
179
  } | {
179
180
  type: "string";
180
181
  required: boolean;
182
+ language: "html" | "markdown";
181
183
  control: "code";
182
184
  defaultValue?: string | undefined;
183
185
  label?: string | undefined;
@@ -186,9 +188,9 @@ export declare const corePropsMetas: {
186
188
  type: "string";
187
189
  required: boolean;
188
190
  control: "codetext";
189
- description?: string | undefined;
190
- label?: string | undefined;
191
191
  defaultValue?: string | undefined;
192
+ label?: string | undefined;
193
+ description?: string | undefined;
192
194
  } | {
193
195
  type: "string";
194
196
  required: boolean;
@@ -7,7 +7,7 @@ export { PropMeta } from "./prop-meta";
7
7
  export { type WsComponentPropsMeta, type ComponentState, type PresetStyle, WsComponentMeta, componentCategories, stateCategories, defaultStates, } from "./components/component-meta";
8
8
  export * from "./embed-template";
9
9
  export * from "./props";
10
- export * from "./context";
10
+ export type * from "./context";
11
11
  export { getIndexesWithinAncestors } from "./instance-utils";
12
- export * from "./hook";
12
+ export type * from "./hook";
13
13
  export { generateWebstudioComponent, generateJsxElement, generateJsxChildren, } from "./component-generator";
@@ -72,6 +72,7 @@ export declare const PropMeta: z.ZodUnion<[z.ZodObject<{
72
72
  }>, z.ZodObject<{
73
73
  control: z.ZodLiteral<"code">;
74
74
  type: z.ZodLiteral<"string">;
75
+ language: z.ZodUnion<[z.ZodLiteral<"html">, z.ZodLiteral<"markdown">]>;
75
76
  defaultValue: z.ZodOptional<z.ZodString>;
76
77
  label: z.ZodOptional<z.ZodString>;
77
78
  description: z.ZodOptional<z.ZodString>;
@@ -79,6 +80,7 @@ export declare const PropMeta: z.ZodUnion<[z.ZodObject<{
79
80
  }, "strip", z.ZodTypeAny, {
80
81
  type: "string";
81
82
  required: boolean;
83
+ language: "html" | "markdown";
82
84
  control: "code";
83
85
  defaultValue?: string | undefined;
84
86
  label?: string | undefined;
@@ -86,31 +88,32 @@ export declare const PropMeta: z.ZodUnion<[z.ZodObject<{
86
88
  }, {
87
89
  type: "string";
88
90
  required: boolean;
91
+ language: "html" | "markdown";
89
92
  control: "code";
90
93
  defaultValue?: string | undefined;
91
94
  label?: string | undefined;
92
95
  description?: string | undefined;
93
96
  }>, z.ZodObject<{
97
+ control: z.ZodLiteral<"codetext">;
94
98
  type: z.ZodLiteral<"string">;
95
- required: z.ZodBoolean;
96
- description: z.ZodOptional<z.ZodString>;
97
- label: z.ZodOptional<z.ZodString>;
98
99
  defaultValue: z.ZodOptional<z.ZodString>;
99
- control: z.ZodLiteral<"codetext">;
100
+ label: z.ZodOptional<z.ZodString>;
101
+ description: z.ZodOptional<z.ZodString>;
102
+ required: z.ZodBoolean;
100
103
  }, "strip", z.ZodTypeAny, {
101
104
  type: "string";
102
105
  required: boolean;
103
106
  control: "codetext";
104
- description?: string | undefined;
105
- label?: string | undefined;
106
107
  defaultValue?: string | undefined;
108
+ label?: string | undefined;
109
+ description?: string | undefined;
107
110
  }, {
108
111
  type: "string";
109
112
  required: boolean;
110
113
  control: "codetext";
111
- description?: string | undefined;
112
- label?: string | undefined;
113
114
  defaultValue?: string | undefined;
115
+ label?: string | undefined;
116
+ description?: string | undefined;
114
117
  }>, z.ZodObject<{
115
118
  control: z.ZodLiteral<"color">;
116
119
  type: z.ZodLiteral<"string">;
@@ -101,4 +101,3 @@ export declare const showAttribute: "data-ws-show";
101
101
  export declare const indexAttribute: "data-ws-index";
102
102
  export declare const collapsedAttribute: "data-ws-collapsed";
103
103
  export declare const textContentAttribute: "data-ws-text-content";
104
- export declare const getIndexWithinAncestorFromComponentProps: (props: Record<string, unknown>) => string | undefined;
@@ -0,0 +1,4 @@
1
+ export * from "./context";
2
+ export * from "./hook";
3
+ export * from "./variable-state";
4
+ export declare const getIndexWithinAncestorFromComponentProps: (props: Record<string, unknown>) => string | undefined;
@@ -0,0 +1,2 @@
1
+ import { type Dispatch, type SetStateAction } from "react";
2
+ export declare const useVariableState: <S>(initialState: S | (() => S)) => [S, Dispatch<SetStateAction<S>>];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.174.0",
3
+ "version": "0.179.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -12,11 +12,11 @@
12
12
  "react": "18.3.0-canary-14898b6a9-20240318",
13
13
  "react-dom": "18.3.0-canary-14898b6a9-20240318",
14
14
  "strip-indent": "^4.0.0",
15
- "type-fest": "^4.3.1",
15
+ "type-fest": "^4.24.0",
16
16
  "typescript": "5.5.2",
17
17
  "zod": "^3.22.4",
18
- "@webstudio-is/tsconfig": "1.0.7",
19
- "@webstudio-is/jest-config": "1.0.7"
18
+ "@webstudio-is/jest-config": "1.0.7",
19
+ "@webstudio-is/tsconfig": "1.0.7"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": "18.3.0-canary-14898b6a9-20240318",
@@ -25,14 +25,14 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "change-case": "^5.4.4",
28
- "html-tags": "^3.3.1",
28
+ "html-tags": "^4.0.0",
29
29
  "nanoid": "^5.0.1",
30
- "title-case": "^4.1.0",
31
- "@webstudio-is/css-engine": "0.174.0",
32
- "@webstudio-is/fonts": "0.174.0",
33
- "@webstudio-is/icons": "^0.174.0",
34
- "@webstudio-is/sdk": "0.174.0",
35
- "@webstudio-is/image": "0.174.0"
30
+ "title-case": "^4.3.1",
31
+ "@webstudio-is/css-engine": "0.179.0",
32
+ "@webstudio-is/fonts": "0.179.0",
33
+ "@webstudio-is/icons": "^0.179.0",
34
+ "@webstudio-is/image": "0.179.0",
35
+ "@webstudio-is/sdk": "0.179.0"
36
36
  },
37
37
  "exports": {
38
38
  ".": {
@@ -40,10 +40,10 @@
40
40
  "types": "./lib/types/index.d.ts",
41
41
  "import": "./lib/index.js"
42
42
  },
43
- "./css-normalize": {
44
- "webstudio": "./src/css/normalize.ts",
45
- "types": "./lib/types/css/normalize.d.ts",
46
- "import": "./lib/css/normalize.js"
43
+ "./runtime": {
44
+ "webstudio": "./src/runtime.ts",
45
+ "types": "./lib/types/runtime.d.ts",
46
+ "import": "./lib/runtime.js"
47
47
  },
48
48
  "./placeholder": {
49
49
  "types": "./placeholder.d.ts"
@@ -58,7 +58,7 @@
58
58
  "private": false,
59
59
  "sideEffects": false,
60
60
  "scripts": {
61
- "build": "rm -rf lib && esbuild src/index.ts ./src/css/normalize.ts --outdir=lib --bundle --format=esm --packages=external",
61
+ "build": "rm -rf lib && esbuild src/index.ts src/runtime.ts --outdir=lib --bundle --format=esm --packages=external",
62
62
  "dts": "tsc --project tsconfig.dts.json",
63
63
  "typecheck": "tsc",
64
64
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"