@webstudio-is/react-sdk 0.0.0-4f7bf18 → 0.0.0-50b8685

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
@@ -54,178 +54,6 @@ var generateRemixParams = (pathname) => {
54
54
  return generated;
55
55
  };
56
56
 
57
- // src/css/global-rules.ts
58
- import { getFontFaces } from "@webstudio-is/fonts";
59
- var addGlobalRules = (sheet, { assets, assetBaseUrl }) => {
60
- const fontAssets = [];
61
- for (const asset of assets.values()) {
62
- if (asset.type === "font") {
63
- fontAssets.push(asset);
64
- }
65
- }
66
- const fontFaces = getFontFaces(fontAssets, { assetBaseUrl });
67
- for (const fontFace of fontFaces) {
68
- sheet.addFontFaceRule(fontFace);
69
- }
70
- };
71
-
72
- // src/css/css.ts
73
- import {
74
- createRegularStyleSheet,
75
- generateAtomic
76
- } from "@webstudio-is/css-engine";
77
- import {
78
- ROOT_INSTANCE_ID,
79
- createScope,
80
- parseComponentName,
81
- descendantComponent,
82
- rootComponent
83
- } from "@webstudio-is/sdk";
84
- import { kebabCase } from "change-case";
85
- var createImageValueTransformer = (assets, { assetBaseUrl }) => (styleValue) => {
86
- if (styleValue.type === "image" && styleValue.value.type === "asset") {
87
- const asset = assets.get(styleValue.value.value);
88
- if (asset === void 0) {
89
- return { type: "keyword", value: "none" };
90
- }
91
- const url = `${assetBaseUrl}${asset.name}`;
92
- return {
93
- type: "image",
94
- value: {
95
- type: "url",
96
- url
97
- },
98
- hidden: styleValue.hidden
99
- };
100
- }
101
- };
102
- var normalizeClassName = (name) => kebabCase(name);
103
- var generateCss = ({
104
- assets,
105
- instances,
106
- props,
107
- breakpoints,
108
- styles,
109
- styleSourceSelections,
110
- componentMetas,
111
- assetBaseUrl,
112
- atomic
113
- }) => {
114
- const globalSheet = createRegularStyleSheet({ name: "ssr" });
115
- const sheet = createRegularStyleSheet({ name: "ssr" });
116
- addGlobalRules(globalSheet, { assets, assetBaseUrl });
117
- globalSheet.addMediaRule("presets");
118
- const presetClasses = /* @__PURE__ */ new Map();
119
- const scope = createScope([], normalizeClassName, "-");
120
- for (const [component, meta] of componentMetas) {
121
- const [_namespace, componentName] = parseComponentName(component);
122
- const className = `w-${scope.getName(component, meta.label ?? componentName)}`;
123
- const presetStyle = Object.entries(meta.presetStyle ?? {});
124
- if (presetStyle.length > 0) {
125
- presetClasses.set(component, className);
126
- }
127
- for (const [tag, styles2] of presetStyle) {
128
- const selector = component === rootComponent ? ":root" : `:where(${tag}.${className})`;
129
- const rule = globalSheet.addNestingRule(selector);
130
- for (const declaration of styles2) {
131
- rule.setDeclaration({
132
- breakpoint: "presets",
133
- selector: declaration.state ?? "",
134
- property: declaration.property,
135
- value: declaration.value
136
- });
137
- }
138
- }
139
- }
140
- for (const breakpoint of breakpoints.values()) {
141
- sheet.addMediaRule(breakpoint.id, breakpoint);
142
- }
143
- const imageValueTransformer = createImageValueTransformer(assets, {
144
- assetBaseUrl
145
- });
146
- sheet.setTransformer(imageValueTransformer);
147
- for (const styleDecl of styles.values()) {
148
- const rule = sheet.addMixinRule(styleDecl.styleSourceId);
149
- rule.setDeclaration({
150
- breakpoint: styleDecl.breakpointId,
151
- selector: styleDecl.state ?? "",
152
- property: styleDecl.property,
153
- value: styleDecl.value
154
- });
155
- }
156
- const classes = /* @__PURE__ */ new Map();
157
- const parentIdByInstanceId = /* @__PURE__ */ new Map();
158
- for (const instance of instances.values()) {
159
- const presetClass = presetClasses.get(instance.component);
160
- if (presetClass) {
161
- classes.set(instance.id, [presetClass]);
162
- }
163
- for (const child of instance.children) {
164
- if (child.type === "id") {
165
- parentIdByInstanceId.set(child.value, instance.id);
166
- }
167
- }
168
- }
169
- const descendantSelectorByInstanceId = /* @__PURE__ */ new Map();
170
- for (const prop of props.values()) {
171
- if (prop.name === "selector" && prop.type === "string") {
172
- descendantSelectorByInstanceId.set(prop.instanceId, prop.value);
173
- }
174
- }
175
- const instanceByRule = /* @__PURE__ */ new Map();
176
- for (const selection of styleSourceSelections.values()) {
177
- let { instanceId } = selection;
178
- const { values } = selection;
179
- if (instanceId === ROOT_INSTANCE_ID) {
180
- const rule2 = sheet.addNestingRule(`:root`);
181
- rule2.applyMixins(values);
182
- continue;
183
- }
184
- let descendantSuffix = "";
185
- const instance = instances.get(instanceId);
186
- if (instance === void 0) {
187
- continue;
188
- }
189
- if (instance.component === descendantComponent) {
190
- const parentId = parentIdByInstanceId.get(instanceId);
191
- const descendantSelector = descendantSelectorByInstanceId.get(instanceId);
192
- if (parentId && descendantSelector) {
193
- descendantSuffix = descendantSelector;
194
- instanceId = parentId;
195
- }
196
- }
197
- const meta = componentMetas.get(instance.component);
198
- const [_namespace, shortName] = parseComponentName(instance.component);
199
- const baseName = instance.label ?? meta?.label ?? shortName;
200
- const className = `w-${scope.getName(instanceId, baseName)}`;
201
- if (atomic === false) {
202
- let classList = classes.get(instanceId);
203
- if (classList === void 0) {
204
- classList = [];
205
- classes.set(instanceId, classList);
206
- }
207
- classList.push(className);
208
- }
209
- const rule = sheet.addNestingRule(`.${className}`, descendantSuffix);
210
- rule.applyMixins(values);
211
- instanceByRule.set(rule, instanceId);
212
- }
213
- if (atomic) {
214
- const { cssText } = generateAtomic(sheet, {
215
- getKey: (rule) => instanceByRule.get(rule),
216
- transformValue: imageValueTransformer,
217
- classes
218
- });
219
- return { cssText: `${globalSheet.cssText}
220
- ${cssText}`, classes };
221
- }
222
- return {
223
- cssText: `${globalSheet.cssText}
224
- ${sheet.cssText}`,
225
- classes
226
- };
227
- };
228
-
229
57
  // src/props.ts
230
58
  import {
231
59
  getPagePath,
@@ -328,8 +156,7 @@ var showAttribute = "data-ws-show";
328
156
  var indexAttribute = "data-ws-index";
329
157
  var collapsedAttribute = "data-ws-collapsed";
330
158
  var textContentAttribute = "data-ws-text-content";
331
- var editablePlaceholderVariable = "--data-ws-editable-placeholder";
332
- var editingPlaceholderVariable = "--data-ws-editing-placeholder";
159
+ var animationCanPlayOnCanvasAttribute = "data-ws-animation-can-play-on-canvas";
333
160
  var attributeNameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
334
161
  var attributeNameChar = attributeNameStartChar + ":\\-0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
335
162
  var validAttributeNameRegex = new RegExp(
@@ -573,30 +400,6 @@ var generateDataFromEmbedTemplate = (treeTemplate, metas, generateId = nanoid) =
573
400
  resources: []
574
401
  };
575
402
  };
576
- var namespaceEmbedTemplateComponents = (template, namespace, components) => {
577
- return template.map((item) => {
578
- if (item.type === "text") {
579
- return item;
580
- }
581
- if (item.type === "expression") {
582
- return item;
583
- }
584
- if (item.type === "instance") {
585
- const prefix = components.has(item.component) ? `${namespace}:` : "";
586
- return {
587
- ...item,
588
- component: `${prefix}${item.component}`,
589
- children: namespaceEmbedTemplateComponents(
590
- item.children,
591
- namespace,
592
- components
593
- )
594
- };
595
- }
596
- item;
597
- throw Error("Impossible case");
598
- });
599
- };
600
403
  var namespaceMatcher = (namespace, matcher) => {
601
404
  const newMatcher = structuredClone(matcher);
602
405
  if (newMatcher.component?.$eq) {
@@ -631,70 +434,20 @@ var namespaceMeta = (meta, namespace, components) => {
631
434
  if (newMeta.indexWithinAncestor) {
632
435
  newMeta.indexWithinAncestor = components.has(newMeta.indexWithinAncestor) ? `${namespace}:${newMeta.indexWithinAncestor}` : newMeta.indexWithinAncestor;
633
436
  }
634
- if (newMeta.template) {
635
- newMeta.template = namespaceEmbedTemplateComponents(
636
- newMeta.template,
637
- namespace,
638
- components
639
- );
640
- }
641
437
  return newMeta;
642
438
  };
643
439
 
644
- // src/instance-utils.ts
645
- var getIndexesWithinAncestors = (metas, instances, rootIds) => {
646
- const ancestors = /* @__PURE__ */ new Set();
647
- for (const meta of metas.values()) {
648
- if (meta.indexWithinAncestor !== void 0) {
649
- ancestors.add(meta.indexWithinAncestor);
650
- }
651
- }
652
- const indexes = /* @__PURE__ */ new Map();
653
- const traverseInstances = (instances2, instanceId, latestIndexes2 = /* @__PURE__ */ new Map()) => {
654
- const instance = instances2.get(instanceId);
655
- if (instance === void 0) {
656
- return;
657
- }
658
- const meta = metas.get(instance.component);
659
- if (meta === void 0) {
660
- return;
661
- }
662
- if (ancestors.has(instance.component)) {
663
- latestIndexes2 = new Map(latestIndexes2);
664
- latestIndexes2.set(instance.component, /* @__PURE__ */ new Map());
665
- }
666
- if (meta.indexWithinAncestor !== void 0) {
667
- const ancestorIndexes = latestIndexes2.get(meta.indexWithinAncestor);
668
- if (ancestorIndexes !== void 0) {
669
- let index = ancestorIndexes.get(instance.component) ?? -1;
670
- index += 1;
671
- ancestorIndexes.set(instance.component, index);
672
- indexes.set(instance.id, index);
673
- }
674
- }
675
- for (const child of instance.children) {
676
- if (child.type === "id") {
677
- traverseInstances(instances2, child.value, latestIndexes2);
678
- }
679
- }
680
- };
681
- const latestIndexes = /* @__PURE__ */ new Map();
682
- for (const instanceId of rootIds) {
683
- traverseInstances(instances, instanceId, latestIndexes);
684
- }
685
- return indexes;
686
- };
687
-
688
440
  // src/component-generator.ts
689
441
  import {
690
- parseComponentName as parseComponentName2,
442
+ parseComponentName,
691
443
  generateExpression,
692
444
  decodeDataSourceVariable,
693
445
  transpileExpression as transpileExpression2,
694
446
  blockComponent,
695
447
  blockTemplateComponent,
696
448
  collectionComponent,
697
- descendantComponent as descendantComponent2
449
+ descendantComponent,
450
+ getIndexesWithinAncestors
698
451
  } from "@webstudio-is/sdk";
699
452
  var generateAction = ({
700
453
  scope,
@@ -758,7 +511,7 @@ var generatePropValue = ({
758
511
  if (prop.type === "asset" || prop.type === "page") {
759
512
  return;
760
513
  }
761
- if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]" || prop.type === "json") {
514
+ if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]" || prop.type === "json" || prop.type === "animationAction") {
762
515
  return JSON.stringify(prop.value);
763
516
  }
764
517
  if (prop.type === "parameter") {
@@ -796,7 +549,7 @@ var generateJsxElement = ({
796
549
  children,
797
550
  classesMap
798
551
  }) => {
799
- if (instance.component === descendantComponent2) {
552
+ if (instance.component === descendantComponent) {
800
553
  return "";
801
554
  }
802
555
  let generatedProps = "";
@@ -870,7 +623,7 @@ ${prop.name}={${propValue}}`;
870
623
  return "";
871
624
  }
872
625
  const indexVariable = scope.getName(`${instance.id}-index`, "index");
873
- generatedElement += `{${collectionDataValue}?.map((${collectionItemValue}: any, ${indexVariable}: number) =>
626
+ generatedElement += `{${collectionDataValue}?.map?.((${collectionItemValue}: any, ${indexVariable}: number) =>
874
627
  `;
875
628
  generatedElement += `<Fragment key={${indexVariable}}>
876
629
  `;
@@ -882,7 +635,7 @@ ${prop.name}={${propValue}}`;
882
635
  } else if (instance.component === blockComponent) {
883
636
  generatedElement += children;
884
637
  } else {
885
- const [_namespace, shortName] = parseComponentName2(instance.component);
638
+ const [_namespace, shortName] = parseComponentName(instance.component);
886
639
  const componentVariable = scope.getName(instance.component, shortName);
887
640
  if (instance.children.length === 0) {
888
641
  generatedElement += `<${componentVariable}${generatedProps} />
@@ -992,49 +745,56 @@ var generateWebstudioComponent = ({
992
745
  instances,
993
746
  props,
994
747
  dataSources,
995
- indexesWithinAncestors,
748
+ metas,
996
749
  classesMap
997
750
  }) => {
998
751
  const instance = instances.get(rootInstanceId);
999
- if (instance === void 0) {
1000
- return "";
1001
- }
752
+ const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
753
+ rootInstanceId
754
+ ]);
1002
755
  const usedDataSources = /* @__PURE__ */ new Map();
1003
- const generatedJsx = generateJsxElement({
1004
- context: "expression",
1005
- scope,
1006
- instance,
1007
- props,
1008
- dataSources,
1009
- usedDataSources,
1010
- indexesWithinAncestors,
1011
- classesMap,
1012
- children: generateJsxChildren({
756
+ let generatedJsx = "<></>\n";
757
+ if (instance) {
758
+ generatedJsx = generateJsxElement({
759
+ context: "expression",
1013
760
  scope,
1014
- children: instance.children,
1015
- instances,
761
+ instance,
1016
762
  props,
1017
763
  dataSources,
1018
764
  usedDataSources,
1019
765
  indexesWithinAncestors,
1020
- classesMap
1021
- })
1022
- });
766
+ classesMap,
767
+ children: generateJsxChildren({
768
+ scope,
769
+ children: instance.children,
770
+ instances,
771
+ props,
772
+ dataSources,
773
+ usedDataSources,
774
+ indexesWithinAncestors,
775
+ classesMap
776
+ })
777
+ });
778
+ }
1023
779
  let generatedProps = "";
780
+ let generatedParameters = "";
781
+ const uniqueParameters = new Set(
782
+ parameters.map((parameter) => parameter.name)
783
+ );
1024
784
  if (parameters.length > 0) {
1025
- let generatedPropsValue = "{ ";
1026
- let generatedPropsType = "{ ";
785
+ let generatedPropsType = "";
786
+ for (const parameterName of uniqueParameters) {
787
+ generatedPropsType += `${parameterName}: any; `;
788
+ }
789
+ generatedProps = `_props: { ${generatedPropsType}}`;
1027
790
  for (const parameter of parameters) {
1028
791
  const dataSource = usedDataSources.get(parameter.value);
1029
792
  if (dataSource) {
1030
793
  const valueName = scope.getName(dataSource.id, dataSource.name);
1031
- generatedPropsValue += `${parameter.name}: ${valueName}, `;
794
+ generatedParameters += `const ${valueName} = _props.${parameter.name};
795
+ `;
1032
796
  }
1033
- generatedPropsType += `${parameter.name}: any; `;
1034
797
  }
1035
- generatedPropsValue += `}`;
1036
- generatedPropsType += `}`;
1037
- generatedProps = `${generatedPropsValue}: ${generatedPropsType}`;
1038
798
  }
1039
799
  let generatedDataSources = "";
1040
800
  for (const dataSource of usedDataSources.values()) {
@@ -1063,6 +823,7 @@ var generateWebstudioComponent = ({
1063
823
  let generatedComponent = "";
1064
824
  generatedComponent += `const ${name} = (${generatedProps}) => {
1065
825
  `;
826
+ generatedComponent += `${generatedParameters}`;
1066
827
  generatedComponent += `${generatedDataSources}`;
1067
828
  generatedComponent += `return ${generatedJsx}`;
1068
829
  generatedComponent += `}
@@ -1070,20 +831,15 @@ var generateWebstudioComponent = ({
1070
831
  return generatedComponent;
1071
832
  };
1072
833
  export {
1073
- addGlobalRules,
834
+ animationCanPlayOnCanvasAttribute,
1074
835
  collapsedAttribute,
1075
836
  componentAttribute,
1076
- createImageValueTransformer,
1077
- editablePlaceholderVariable,
1078
- editingPlaceholderVariable,
1079
- generateCss,
1080
837
  generateDataFromEmbedTemplate,
1081
838
  generateJsxChildren,
1082
839
  generateJsxElement,
1083
840
  generateRemixParams,
1084
841
  generateRemixRoute,
1085
842
  generateWebstudioComponent,
1086
- getIndexesWithinAncestors,
1087
843
  idAttribute,
1088
844
  indexAttribute,
1089
845
  isAttributeNameSafe,
package/lib/runtime.js CHANGED
@@ -7,7 +7,8 @@ import {
7
7
  var ReactSdkContext = createContext({
8
8
  assetBaseUrl: "/",
9
9
  imageLoader: ({ src }) => src,
10
- resources: {}
10
+ resources: {},
11
+ breakpoints: []
11
12
  });
12
13
  var useResource = (name) => {
13
14
  const { resources } = useContext(ReactSdkContext);
@@ -1,5 +1,4 @@
1
- import type { Instances, Instance, Props, Scope, DataSources, Prop } from "@webstudio-is/sdk";
2
- import type { IndexesWithinAncestors } from "./instance-utils";
1
+ import type { Instances, Instance, Props, Scope, DataSources, Prop, WsComponentMeta, IndexesWithinAncestors } from "@webstudio-is/sdk";
3
2
  export declare const generateJsxElement: ({ context, scope, instance, props, dataSources, usedDataSources, indexesWithinAncestors, children, classesMap, }: {
4
3
  context?: "expression" | "jsx";
5
4
  scope: Scope;
@@ -22,7 +21,7 @@ export declare const generateJsxChildren: ({ scope, children, instances, props,
22
21
  classesMap?: Map<string, Array<string>>;
23
22
  excludePlaceholders?: boolean;
24
23
  }) => string;
25
- export declare const generateWebstudioComponent: ({ scope, name, rootInstanceId, parameters, instances, props, dataSources, indexesWithinAncestors, classesMap, }: {
24
+ export declare const generateWebstudioComponent: ({ scope, name, rootInstanceId, parameters, instances, props, dataSources, metas, classesMap, }: {
26
25
  scope: Scope;
27
26
  name: string;
28
27
  rootInstanceId: Instance["id"];
@@ -32,6 +31,6 @@ export declare const generateWebstudioComponent: ({ scope, name, rootInstanceId,
32
31
  instances: Instances;
33
32
  props: Props;
34
33
  dataSources: DataSources;
35
- indexesWithinAncestors: IndexesWithinAncestors;
36
34
  classesMap: Map<string, Array<string>>;
35
+ metas: Map<Instance["component"], WsComponentMeta>;
37
36
  }) => string;
@@ -22,5 +22,10 @@ export type Params = {
22
22
  export declare const ReactSdkContext: import("react").Context<Params & {
23
23
  imageLoader: ImageLoader;
24
24
  resources: Record<string, any>;
25
+ breakpoints: {
26
+ id: string;
27
+ minWidth?: number;
28
+ maxWidth?: number;
29
+ }[];
25
30
  }>;
26
31
  export declare const useResource: (name: string) => any;
@@ -3,17 +3,10 @@ export declare const generateDataFromEmbedTemplate: (treeTemplate: WsEmbedTempla
3
3
  export declare const namespaceMeta: (meta: WsComponentMeta, namespace: string, components: Set<EmbedTemplateInstance["component"]>) => {
4
4
  type: "control" | "embed" | "container" | "rich-text-child";
5
5
  description?: string | undefined;
6
- category?: "data" | "xml" | "hidden" | "media" | "general" | "typography" | "forms" | "localization" | "radix" | "internal" | undefined;
6
+ category?: "xml" | "hidden" | "data" | "media" | "general" | "typography" | "forms" | "localization" | "radix" | "internal" | undefined;
7
+ placeholder?: string | undefined;
7
8
  label?: string | undefined;
8
9
  order?: number | undefined;
9
- template?: ({
10
- value: string;
11
- type: "text";
12
- placeholder?: boolean | undefined;
13
- } | {
14
- value: string;
15
- type: "expression";
16
- } | EmbedTemplateInstance)[] | undefined;
17
10
  states?: {
18
11
  label: string;
19
12
  selector: string;
@@ -21,6 +14,7 @@ export declare const namespaceMeta: (meta: WsComponentMeta, namespace: string, c
21
14
  }[] | undefined;
22
15
  constraints?: {
23
16
  relation: "ancestor" | "parent" | "self" | "child" | "descendant";
17
+ text?: false | undefined;
24
18
  component?: {
25
19
  $eq?: string | undefined;
26
20
  $neq?: string | undefined;
@@ -35,6 +29,7 @@ export declare const namespaceMeta: (meta: WsComponentMeta, namespace: string, c
35
29
  } | undefined;
36
30
  } | {
37
31
  relation: "ancestor" | "parent" | "self" | "child" | "descendant";
32
+ text?: false | undefined;
38
33
  component?: {
39
34
  $eq?: string | undefined;
40
35
  $neq?: string | undefined;
@@ -1,5 +1,4 @@
1
- import type { Instance, Prop } from "@webstudio-is/sdk";
2
- import type { IndexesWithinAncestors } from "./instance-utils";
1
+ import type { IndexesWithinAncestors, Instance, Prop } from "@webstudio-is/sdk";
3
2
  export type InstanceData = {
4
3
  id: Instance["id"];
5
4
  instanceKey: string;
@@ -1,9 +1,7 @@
1
1
  export * from "./remix";
2
- export * from "./css/index";
3
2
  export * from "./components/components-utils";
4
3
  export * from "./embed-template";
5
4
  export * from "./props";
6
5
  export type * from "./context";
7
- export { getIndexesWithinAncestors } from "./instance-utils";
8
6
  export type * from "./hook";
9
7
  export { generateWebstudioComponent, generateJsxElement, generateJsxChildren, } from "./component-generator";