@webstudio-is/react-sdk 0.123.0 → 0.124.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/README.md +1 -1
- package/lib/index.js +283 -45
- package/lib/types/app/root.d.ts +1 -1
- package/lib/types/component-generator.d.ts +26 -5
- package/lib/types/css/css.d.ts +8 -2
- package/lib/types/css/global-rules.d.ts +2 -2
- package/lib/types/embed-template.d.ts +6 -0
- package/lib/types/expression.d.ts +43 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/remix.d.ts +20 -0
- package/lib/types/remix.test.d.ts +1 -0
- package/lib/types/resources-generator.d.ts +49 -0
- package/lib/types/resources-generator.test.d.ts +1 -0
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
# Webstudio SDK
|
|
2
2
|
|
|
3
|
-
Webstudio SDK is a TypeScript API that lets you use your Webstudio
|
|
3
|
+
Webstudio SDK is a TypeScript API that lets you use your Webstudio project or some components in your custom codebase or just render a complete Remix Document.
|
|
4
4
|
It is currently under development, but feel free to play with the the current [landing site](https://github.com/webstudio-is/webstudio-landing)
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,61 @@
|
|
|
1
|
+
// src/remix.ts
|
|
2
|
+
var getRemixSegment = (segment) => {
|
|
3
|
+
if (segment === "*") {
|
|
4
|
+
return "$";
|
|
5
|
+
}
|
|
6
|
+
const match = segment.match(/^:(?<name>\w+)(?<modifier>\*|\?)?$/);
|
|
7
|
+
const name = match?.groups?.name;
|
|
8
|
+
const modifier = match?.groups?.modifier;
|
|
9
|
+
if (name) {
|
|
10
|
+
if (modifier === "*") {
|
|
11
|
+
return "$";
|
|
12
|
+
}
|
|
13
|
+
if (modifier === "?") {
|
|
14
|
+
return `($${name})`;
|
|
15
|
+
}
|
|
16
|
+
return `$${name}`;
|
|
17
|
+
}
|
|
18
|
+
return `[${segment}]`;
|
|
19
|
+
};
|
|
20
|
+
var generateRemixRoute = (pathname) => {
|
|
21
|
+
if (pathname.startsWith("/")) {
|
|
22
|
+
pathname = pathname.slice(1);
|
|
23
|
+
}
|
|
24
|
+
if (pathname === "") {
|
|
25
|
+
return `_index`;
|
|
26
|
+
}
|
|
27
|
+
const base = pathname.split("/").map(getRemixSegment).join(".");
|
|
28
|
+
const tail = pathname.endsWith("*") ? "" : "._index";
|
|
29
|
+
return `${base}${tail}`;
|
|
30
|
+
};
|
|
31
|
+
var generateRemixParams = (pathname) => {
|
|
32
|
+
const name = pathname.match(/:(?<name>\w+)\*$/)?.groups?.name;
|
|
33
|
+
let generated = "";
|
|
34
|
+
generated += `export const getRemixParams = ({ ...params }: Params): Params => {
|
|
35
|
+
`;
|
|
36
|
+
if (name) {
|
|
37
|
+
generated += ` params["${name}"] = params["*"]
|
|
38
|
+
`;
|
|
39
|
+
generated += ` delete params["*"]
|
|
40
|
+
`;
|
|
41
|
+
}
|
|
42
|
+
if (pathname.endsWith("/*")) {
|
|
43
|
+
generated += ` params[0] = params["*"]
|
|
44
|
+
`;
|
|
45
|
+
generated += ` delete params["*"]
|
|
46
|
+
`;
|
|
47
|
+
}
|
|
48
|
+
generated += ` return params
|
|
49
|
+
`;
|
|
50
|
+
generated += `}
|
|
51
|
+
`;
|
|
52
|
+
return generated;
|
|
53
|
+
};
|
|
54
|
+
|
|
1
55
|
// src/css/global-rules.ts
|
|
2
56
|
import { getFontFaces } from "@webstudio-is/fonts";
|
|
3
|
-
var addGlobalRules = (
|
|
4
|
-
|
|
57
|
+
var addGlobalRules = (sheet, { assets, assetBaseUrl }) => {
|
|
58
|
+
sheet.addPlaintextRule("html {margin: 0; display: grid; min-height: 100%}");
|
|
5
59
|
const fontAssets = [];
|
|
6
60
|
for (const asset of assets.values()) {
|
|
7
61
|
if (asset.type === "font") {
|
|
@@ -10,7 +64,7 @@ var addGlobalRules = (engine, { assets, assetBaseUrl }) => {
|
|
|
10
64
|
}
|
|
11
65
|
const fontFaces = getFontFaces(fontAssets, { assetBaseUrl });
|
|
12
66
|
for (const fontFace of fontFaces) {
|
|
13
|
-
|
|
67
|
+
sheet.addFontFaceRule(fontFace);
|
|
14
68
|
}
|
|
15
69
|
};
|
|
16
70
|
|
|
@@ -153,14 +207,16 @@ var getPresetStyleRules = (component, presetStyle) => {
|
|
|
153
207
|
};
|
|
154
208
|
|
|
155
209
|
// src/css/css.ts
|
|
156
|
-
import {
|
|
157
|
-
|
|
210
|
+
import {
|
|
211
|
+
createRegularStyleSheet,
|
|
212
|
+
createAtomicStyleSheet
|
|
213
|
+
} from "@webstudio-is/css-engine";
|
|
214
|
+
var createImageValueTransformer = (assets, { assetBaseUrl }) => (styleValue) => {
|
|
158
215
|
if (styleValue.type === "image" && styleValue.value.type === "asset") {
|
|
159
216
|
const asset = assets.get(styleValue.value.value);
|
|
160
217
|
if (asset === void 0) {
|
|
161
218
|
return { type: "keyword", value: "none" };
|
|
162
219
|
}
|
|
163
|
-
const { assetBaseUrl } = options;
|
|
164
220
|
const url = `${assetBaseUrl}${asset.name}`;
|
|
165
221
|
return {
|
|
166
222
|
type: "image",
|
|
@@ -172,18 +228,17 @@ var createImageValueTransformer = (assets, options) => (styleValue) => {
|
|
|
172
228
|
};
|
|
173
229
|
}
|
|
174
230
|
};
|
|
175
|
-
var
|
|
231
|
+
var generateCss = (data, { assetBaseUrl, atomic = false }) => {
|
|
176
232
|
const assets = new Map(data.assets.map((asset) => [asset.id, asset]));
|
|
177
233
|
const breakpoints = new Map(data.breakpoints);
|
|
178
234
|
const styles = new Map(data.styles);
|
|
179
235
|
const styleSourceSelections = new Map(data.styleSourceSelections);
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
});
|
|
236
|
+
const classesMap = /* @__PURE__ */ new Map();
|
|
237
|
+
const regularSheet = createRegularStyleSheet({ name: "ssr-regular" });
|
|
238
|
+
const atomicSheet = atomic ? createAtomicStyleSheet({ name: "ssr-atomic" }) : void 0;
|
|
239
|
+
addGlobalRules(regularSheet, { assets, assetBaseUrl });
|
|
185
240
|
for (const breakpoint of breakpoints.values()) {
|
|
186
|
-
|
|
241
|
+
(atomicSheet ?? regularSheet).addMediaRule(breakpoint.id, breakpoint);
|
|
187
242
|
}
|
|
188
243
|
for (const [component, meta] of data.componentMetas) {
|
|
189
244
|
const presetStyle = meta.presetStyle;
|
|
@@ -192,21 +247,36 @@ var generateCssText = (data, options) => {
|
|
|
192
247
|
}
|
|
193
248
|
const rules = getPresetStyleRules(component, presetStyle);
|
|
194
249
|
for (const [selector, style] of rules) {
|
|
195
|
-
|
|
250
|
+
regularSheet.addStyleRule({ style }, selector);
|
|
196
251
|
}
|
|
197
252
|
}
|
|
198
253
|
const styleRules = getStyleRules(styles, styleSourceSelections);
|
|
254
|
+
const imageValueTransformer = createImageValueTransformer(assets, {
|
|
255
|
+
assetBaseUrl
|
|
256
|
+
});
|
|
199
257
|
for (const { breakpointId, instanceId, state, style } of styleRules) {
|
|
200
|
-
|
|
258
|
+
if (atomicSheet) {
|
|
259
|
+
const { classes } = atomicSheet.addStyleRule(
|
|
260
|
+
{ breakpoint: breakpointId, style },
|
|
261
|
+
state,
|
|
262
|
+
imageValueTransformer
|
|
263
|
+
);
|
|
264
|
+
classesMap.set(instanceId, [
|
|
265
|
+
...classesMap.get(instanceId) ?? [],
|
|
266
|
+
...classes
|
|
267
|
+
]);
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
regularSheet.addStyleRule(
|
|
271
|
+
{ breakpoint: breakpointId, style },
|
|
201
272
|
`[${idAttribute}="${instanceId}"]${state ?? ""}`,
|
|
202
|
-
|
|
203
|
-
breakpoint: breakpointId,
|
|
204
|
-
style
|
|
205
|
-
},
|
|
206
|
-
createImageValueTransformer(assets, options)
|
|
273
|
+
imageValueTransformer
|
|
207
274
|
);
|
|
208
275
|
}
|
|
209
|
-
return
|
|
276
|
+
return {
|
|
277
|
+
cssText: regularSheet.cssText + (atomicSheet?.cssText ?? ""),
|
|
278
|
+
classesMap
|
|
279
|
+
};
|
|
210
280
|
};
|
|
211
281
|
|
|
212
282
|
// src/tree/create-elements-tree.tsx
|
|
@@ -689,6 +759,26 @@ var decodeDataSourceVariable = (name) => {
|
|
|
689
759
|
}
|
|
690
760
|
return;
|
|
691
761
|
};
|
|
762
|
+
var generateExpression = ({
|
|
763
|
+
expression,
|
|
764
|
+
dataSources,
|
|
765
|
+
scope
|
|
766
|
+
}) => {
|
|
767
|
+
return validateExpression(expression, {
|
|
768
|
+
// parse any expression
|
|
769
|
+
effectful: true,
|
|
770
|
+
// transpile to safely executable member expressions
|
|
771
|
+
optional: true,
|
|
772
|
+
transformIdentifier: (identifier) => {
|
|
773
|
+
const depId = decodeDataSourceVariable(identifier);
|
|
774
|
+
const dep = depId ? dataSources.get(depId) : void 0;
|
|
775
|
+
if (dep) {
|
|
776
|
+
return scope.getName(dep.id, dep.name);
|
|
777
|
+
}
|
|
778
|
+
return identifier;
|
|
779
|
+
}
|
|
780
|
+
});
|
|
781
|
+
};
|
|
692
782
|
var generateDataSources = ({
|
|
693
783
|
scope,
|
|
694
784
|
typed = false,
|
|
@@ -1276,17 +1366,10 @@ var generatePropValue = ({
|
|
|
1276
1366
|
return scope.getName(dataSource.id, dataSource.name);
|
|
1277
1367
|
}
|
|
1278
1368
|
if (prop.type === "expression") {
|
|
1279
|
-
return
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
const depId = decodeDataSourceVariable(identifier);
|
|
1284
|
-
const dep = depId ? dataSources.get(depId) : void 0;
|
|
1285
|
-
if (dep) {
|
|
1286
|
-
return scope.getName(dep.id, dep.name);
|
|
1287
|
-
}
|
|
1288
|
-
return identifier;
|
|
1289
|
-
}
|
|
1369
|
+
return generateExpression({
|
|
1370
|
+
expression: prop.value,
|
|
1371
|
+
dataSources,
|
|
1372
|
+
scope
|
|
1290
1373
|
});
|
|
1291
1374
|
}
|
|
1292
1375
|
if (prop.type === "action") {
|
|
@@ -1300,7 +1383,8 @@ var generateJsxElement = ({
|
|
|
1300
1383
|
props,
|
|
1301
1384
|
dataSources,
|
|
1302
1385
|
indexesWithinAncestors,
|
|
1303
|
-
children
|
|
1386
|
+
children,
|
|
1387
|
+
classesMap
|
|
1304
1388
|
}) => {
|
|
1305
1389
|
let generatedProps = "";
|
|
1306
1390
|
generatedProps += `
|
|
@@ -1317,6 +1401,7 @@ ${indexAttribute}="${index}"`;
|
|
|
1317
1401
|
let conditionValue;
|
|
1318
1402
|
let collectionDataValue;
|
|
1319
1403
|
let collectionItemValue;
|
|
1404
|
+
const classes = Array.from(classesMap?.get(instance.id) ?? []);
|
|
1320
1405
|
for (const prop of props.values()) {
|
|
1321
1406
|
if (prop.instanceId !== instance.id) {
|
|
1322
1407
|
continue;
|
|
@@ -1341,11 +1426,21 @@ ${indexAttribute}="${index}"`;
|
|
|
1341
1426
|
}
|
|
1342
1427
|
continue;
|
|
1343
1428
|
}
|
|
1429
|
+
if (prop.name === "className") {
|
|
1430
|
+
if (prop.type === "string") {
|
|
1431
|
+
classes.push(prop.value);
|
|
1432
|
+
}
|
|
1433
|
+
continue;
|
|
1434
|
+
}
|
|
1344
1435
|
if (propValue !== void 0) {
|
|
1345
1436
|
generatedProps += `
|
|
1346
1437
|
${prop.name}={${propValue}}`;
|
|
1347
1438
|
}
|
|
1348
1439
|
}
|
|
1440
|
+
if (classes.length !== 0) {
|
|
1441
|
+
generatedProps += `
|
|
1442
|
+
className=${JSON.stringify(classes.join(" "))}`;
|
|
1443
|
+
}
|
|
1349
1444
|
let generatedElement = "";
|
|
1350
1445
|
if (conditionValue) {
|
|
1351
1446
|
generatedElement += `{(${conditionValue}) &&
|
|
@@ -1356,7 +1451,7 @@ ${prop.name}={${propValue}}`;
|
|
|
1356
1451
|
return "";
|
|
1357
1452
|
}
|
|
1358
1453
|
const indexVariable = scope.getName(`${instance.id}-index`, "index");
|
|
1359
|
-
generatedElement += `{${collectionDataValue}
|
|
1454
|
+
generatedElement += `{${collectionDataValue}?.map((${collectionItemValue}: any, ${indexVariable}: number) =>
|
|
1360
1455
|
`;
|
|
1361
1456
|
generatedElement += `<Fragment key={${indexVariable}}>
|
|
1362
1457
|
`;
|
|
@@ -1391,7 +1486,8 @@ var generateJsxChildren = ({
|
|
|
1391
1486
|
instances,
|
|
1392
1487
|
props,
|
|
1393
1488
|
dataSources,
|
|
1394
|
-
indexesWithinAncestors
|
|
1489
|
+
indexesWithinAncestors,
|
|
1490
|
+
classesMap
|
|
1395
1491
|
}) => {
|
|
1396
1492
|
let generatedChildren = "";
|
|
1397
1493
|
for (const child of children) {
|
|
@@ -1413,7 +1509,9 @@ var generateJsxChildren = ({
|
|
|
1413
1509
|
props,
|
|
1414
1510
|
dataSources,
|
|
1415
1511
|
indexesWithinAncestors,
|
|
1512
|
+
classesMap,
|
|
1416
1513
|
children: generateJsxChildren({
|
|
1514
|
+
classesMap,
|
|
1417
1515
|
scope,
|
|
1418
1516
|
children: instance.children,
|
|
1419
1517
|
instances,
|
|
@@ -1430,27 +1528,52 @@ var generateJsxChildren = ({
|
|
|
1430
1528
|
};
|
|
1431
1529
|
var generatePageComponent = ({
|
|
1432
1530
|
scope,
|
|
1433
|
-
|
|
1531
|
+
page,
|
|
1434
1532
|
instances,
|
|
1435
1533
|
props,
|
|
1436
1534
|
dataSources,
|
|
1437
|
-
indexesWithinAncestors
|
|
1535
|
+
indexesWithinAncestors,
|
|
1536
|
+
classesMap
|
|
1438
1537
|
}) => {
|
|
1439
|
-
const instance = instances.get(rootInstanceId);
|
|
1538
|
+
const instance = instances.get(page.rootInstanceId);
|
|
1440
1539
|
if (instance === void 0) {
|
|
1441
1540
|
return "";
|
|
1442
1541
|
}
|
|
1443
|
-
const {
|
|
1542
|
+
const { body: dataSourcesBody } = generateDataSources({
|
|
1444
1543
|
typed: true,
|
|
1445
1544
|
scope,
|
|
1446
1545
|
dataSources,
|
|
1447
1546
|
props
|
|
1448
1547
|
});
|
|
1449
1548
|
let generatedDataSources = "";
|
|
1450
|
-
for (const
|
|
1451
|
-
|
|
1452
|
-
|
|
1549
|
+
for (const dataSource of dataSources.values()) {
|
|
1550
|
+
if (dataSource.type === "variable") {
|
|
1551
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1552
|
+
const setterName = scope.getName(
|
|
1553
|
+
`set$${dataSource.id}`,
|
|
1554
|
+
`set$${dataSource.name}`
|
|
1555
|
+
);
|
|
1556
|
+
const initialValue = dataSource.value.value;
|
|
1557
|
+
const initialValueString = JSON.stringify(initialValue);
|
|
1558
|
+
generatedDataSources += `let [${valueName}, ${setterName}] = useState<any>(${initialValueString})
|
|
1559
|
+
`;
|
|
1560
|
+
}
|
|
1561
|
+
if (dataSource.type === "parameter") {
|
|
1562
|
+
if (dataSource.id === page.pathVariableId) {
|
|
1563
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1564
|
+
generatedDataSources += `let ${valueName} = _props.params
|
|
1453
1565
|
`;
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
if (dataSource.type === "resource") {
|
|
1569
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1570
|
+
const resourceName = scope.getName(
|
|
1571
|
+
dataSource.resourceId,
|
|
1572
|
+
dataSource.name
|
|
1573
|
+
);
|
|
1574
|
+
generatedDataSources += `let ${valueName}: any = _props.resources["${resourceName}"]
|
|
1575
|
+
`;
|
|
1576
|
+
}
|
|
1454
1577
|
}
|
|
1455
1578
|
generatedDataSources += dataSourcesBody;
|
|
1456
1579
|
const generatedJsx = generateJsxElement({
|
|
@@ -1459,17 +1582,23 @@ var generatePageComponent = ({
|
|
|
1459
1582
|
props,
|
|
1460
1583
|
dataSources,
|
|
1461
1584
|
indexesWithinAncestors,
|
|
1585
|
+
classesMap,
|
|
1462
1586
|
children: generateJsxChildren({
|
|
1463
1587
|
scope,
|
|
1464
1588
|
children: instance.children,
|
|
1465
1589
|
instances,
|
|
1466
1590
|
props,
|
|
1467
1591
|
dataSources,
|
|
1468
|
-
indexesWithinAncestors
|
|
1592
|
+
indexesWithinAncestors,
|
|
1593
|
+
classesMap
|
|
1469
1594
|
})
|
|
1470
1595
|
});
|
|
1471
1596
|
let generatedComponent = "";
|
|
1472
|
-
generatedComponent += `
|
|
1597
|
+
generatedComponent += `type Params = Record<string, string | undefined>
|
|
1598
|
+
`;
|
|
1599
|
+
generatedComponent += `type Resources = Record<string, unknown>
|
|
1600
|
+
`;
|
|
1601
|
+
generatedComponent += `const Page = (_props: { params: Params, resources: Resources }) => {
|
|
1473
1602
|
`;
|
|
1474
1603
|
generatedComponent += `${generatedDataSources}`;
|
|
1475
1604
|
generatedComponent += `return ${generatedJsx}`;
|
|
@@ -1477,6 +1606,112 @@ var generatePageComponent = ({
|
|
|
1477
1606
|
`;
|
|
1478
1607
|
return generatedComponent;
|
|
1479
1608
|
};
|
|
1609
|
+
|
|
1610
|
+
// src/resources-generator.ts
|
|
1611
|
+
var generateResourcesLoader = ({
|
|
1612
|
+
scope,
|
|
1613
|
+
page,
|
|
1614
|
+
dataSources,
|
|
1615
|
+
resources
|
|
1616
|
+
}) => {
|
|
1617
|
+
let generatedVariables = "";
|
|
1618
|
+
let generatedOutput = "";
|
|
1619
|
+
let generatedLoaders = "";
|
|
1620
|
+
let hasResources = false;
|
|
1621
|
+
for (const dataSource of dataSources.values()) {
|
|
1622
|
+
if (dataSource.type === "variable") {
|
|
1623
|
+
const name = scope.getName(dataSource.id, dataSource.name);
|
|
1624
|
+
const value = JSON.stringify(dataSource.value.value);
|
|
1625
|
+
generatedVariables += `let ${name} = ${value}
|
|
1626
|
+
`;
|
|
1627
|
+
}
|
|
1628
|
+
if (dataSource.type === "parameter") {
|
|
1629
|
+
if (dataSource.id !== page.pathVariableId) {
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
const name = scope.getName(dataSource.id, dataSource.name);
|
|
1633
|
+
generatedVariables += `const ${name} = _props.params
|
|
1634
|
+
`;
|
|
1635
|
+
}
|
|
1636
|
+
if (dataSource.type === "resource") {
|
|
1637
|
+
const resource = resources.get(dataSource.resourceId);
|
|
1638
|
+
if (resource === void 0) {
|
|
1639
|
+
continue;
|
|
1640
|
+
}
|
|
1641
|
+
hasResources = true;
|
|
1642
|
+
const resourceName = scope.getName(resource.id, dataSource.name);
|
|
1643
|
+
generatedOutput += `${resourceName},
|
|
1644
|
+
`;
|
|
1645
|
+
generatedLoaders += `loadResource({
|
|
1646
|
+
`;
|
|
1647
|
+
generatedLoaders += `id: "${resource.id}",
|
|
1648
|
+
`;
|
|
1649
|
+
generatedLoaders += `name: ${JSON.stringify(resource.name)},
|
|
1650
|
+
`;
|
|
1651
|
+
const url = generateExpression({
|
|
1652
|
+
expression: resource.url,
|
|
1653
|
+
dataSources,
|
|
1654
|
+
scope
|
|
1655
|
+
});
|
|
1656
|
+
generatedLoaders += `url: ${url},
|
|
1657
|
+
`;
|
|
1658
|
+
generatedLoaders += `method: "${resource.method}",
|
|
1659
|
+
`;
|
|
1660
|
+
generatedLoaders += `headers: [
|
|
1661
|
+
`;
|
|
1662
|
+
for (const header of resource.headers) {
|
|
1663
|
+
const value = generateExpression({
|
|
1664
|
+
expression: header.value,
|
|
1665
|
+
dataSources,
|
|
1666
|
+
scope
|
|
1667
|
+
});
|
|
1668
|
+
generatedLoaders += `{ name: "${header.name}", value: ${value} },
|
|
1669
|
+
`;
|
|
1670
|
+
}
|
|
1671
|
+
generatedLoaders += `],
|
|
1672
|
+
`;
|
|
1673
|
+
if (resource.body !== void 0 && resource.body.length > 0) {
|
|
1674
|
+
const body = generateExpression({
|
|
1675
|
+
expression: resource.body,
|
|
1676
|
+
dataSources,
|
|
1677
|
+
scope
|
|
1678
|
+
});
|
|
1679
|
+
generatedLoaders += `body: ${body},
|
|
1680
|
+
`;
|
|
1681
|
+
}
|
|
1682
|
+
generatedLoaders += `}),
|
|
1683
|
+
`;
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
let generated = "";
|
|
1687
|
+
if (hasResources) {
|
|
1688
|
+
generated += `import { loadResource } from "@webstudio-is/sdk";
|
|
1689
|
+
`;
|
|
1690
|
+
}
|
|
1691
|
+
generated += `type Params = Record<string, string | undefined>
|
|
1692
|
+
`;
|
|
1693
|
+
generated += `export const loadResources = async (_props: { params: Params }) => {
|
|
1694
|
+
`;
|
|
1695
|
+
if (hasResources) {
|
|
1696
|
+
generated += generatedVariables;
|
|
1697
|
+
generated += `const [
|
|
1698
|
+
`;
|
|
1699
|
+
generated += generatedOutput;
|
|
1700
|
+
generated += `] = await Promise.all([
|
|
1701
|
+
`;
|
|
1702
|
+
generated += generatedLoaders;
|
|
1703
|
+
generated += `])
|
|
1704
|
+
`;
|
|
1705
|
+
}
|
|
1706
|
+
generated += `return {
|
|
1707
|
+
`;
|
|
1708
|
+
generated += generatedOutput;
|
|
1709
|
+
generated += `} as Record<string, unknown>
|
|
1710
|
+
`;
|
|
1711
|
+
generated += `}
|
|
1712
|
+
`;
|
|
1713
|
+
return generated;
|
|
1714
|
+
};
|
|
1480
1715
|
export {
|
|
1481
1716
|
EmbedTemplateInstance,
|
|
1482
1717
|
EmbedTemplateProp,
|
|
@@ -1499,12 +1734,15 @@ export {
|
|
|
1499
1734
|
decodeDataSourceVariable,
|
|
1500
1735
|
defaultStates,
|
|
1501
1736
|
encodeDataSourceVariable,
|
|
1502
|
-
|
|
1737
|
+
generateCss,
|
|
1503
1738
|
generateDataFromEmbedTemplate,
|
|
1504
1739
|
generateDataSources,
|
|
1505
1740
|
generateJsxChildren,
|
|
1506
1741
|
generateJsxElement,
|
|
1507
1742
|
generatePageComponent,
|
|
1743
|
+
generateRemixParams,
|
|
1744
|
+
generateRemixRoute,
|
|
1745
|
+
generateResourcesLoader,
|
|
1508
1746
|
generateUtilsExport,
|
|
1509
1747
|
getClosestInstance,
|
|
1510
1748
|
getIndexWithinAncestorFromComponentProps,
|
package/lib/types/app/root.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Outlet as DefaultOutlet } from "@remix-run/react";
|
|
2
2
|
/**
|
|
3
|
-
* We are using Outlet prop from index layout when user renders
|
|
3
|
+
* We are using Outlet prop from index layout when user renders project from a subdomain.
|
|
4
4
|
*/
|
|
5
5
|
export declare const Root: ({ Outlet, }: {
|
|
6
6
|
Outlet: typeof DefaultOutlet;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Instances, Instance, Props, Scope, DataSources } from "@webstudio-is/sdk";
|
|
1
|
+
import type { Instances, Instance, Props, Scope, DataSources, Page } from "@webstudio-is/sdk";
|
|
2
2
|
import type { IndexesWithinAncestors } from "./instance-utils";
|
|
3
|
-
export declare const generateJsxElement: ({ scope, instance, props, dataSources, indexesWithinAncestors, children, }: {
|
|
3
|
+
export declare const generateJsxElement: ({ scope, instance, props, dataSources, indexesWithinAncestors, children, classesMap, }: {
|
|
4
4
|
scope: Scope;
|
|
5
5
|
instance: Instance;
|
|
6
6
|
props: Map<string, {
|
|
@@ -110,11 +110,18 @@ export declare const generateJsxElement: ({ scope, instance, props, dataSources,
|
|
|
110
110
|
name: string;
|
|
111
111
|
id: string;
|
|
112
112
|
scopeInstanceId?: string | undefined;
|
|
113
|
+
} | {
|
|
114
|
+
type: "resource";
|
|
115
|
+
name: string;
|
|
116
|
+
id: string;
|
|
117
|
+
resourceId: string;
|
|
118
|
+
scopeInstanceId?: string | undefined;
|
|
113
119
|
}>;
|
|
114
120
|
indexesWithinAncestors: IndexesWithinAncestors;
|
|
115
121
|
children: string;
|
|
122
|
+
classesMap?: Map<string, string[]> | undefined;
|
|
116
123
|
}) => string;
|
|
117
|
-
export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, indexesWithinAncestors, }: {
|
|
124
|
+
export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, indexesWithinAncestors, classesMap, }: {
|
|
118
125
|
scope: Scope;
|
|
119
126
|
children: Instance["children"];
|
|
120
127
|
instances: Map<string, {
|
|
@@ -237,12 +244,19 @@ export declare const generateJsxChildren: ({ scope, children, instances, props,
|
|
|
237
244
|
name: string;
|
|
238
245
|
id: string;
|
|
239
246
|
scopeInstanceId?: string | undefined;
|
|
247
|
+
} | {
|
|
248
|
+
type: "resource";
|
|
249
|
+
name: string;
|
|
250
|
+
id: string;
|
|
251
|
+
resourceId: string;
|
|
252
|
+
scopeInstanceId?: string | undefined;
|
|
240
253
|
}>;
|
|
241
254
|
indexesWithinAncestors: IndexesWithinAncestors;
|
|
255
|
+
classesMap?: Map<string, string[]> | undefined;
|
|
242
256
|
}) => string;
|
|
243
|
-
export declare const generatePageComponent: ({ scope,
|
|
257
|
+
export declare const generatePageComponent: ({ scope, page, instances, props, dataSources, indexesWithinAncestors, classesMap, }: {
|
|
244
258
|
scope: Scope;
|
|
245
|
-
|
|
259
|
+
page: Page;
|
|
246
260
|
instances: Map<string, {
|
|
247
261
|
type: "instance";
|
|
248
262
|
id: string;
|
|
@@ -363,6 +377,13 @@ export declare const generatePageComponent: ({ scope, rootInstanceId, instances,
|
|
|
363
377
|
name: string;
|
|
364
378
|
id: string;
|
|
365
379
|
scopeInstanceId?: string | undefined;
|
|
380
|
+
} | {
|
|
381
|
+
type: "resource";
|
|
382
|
+
name: string;
|
|
383
|
+
id: string;
|
|
384
|
+
resourceId: string;
|
|
385
|
+
scopeInstanceId?: string | undefined;
|
|
366
386
|
}>;
|
|
367
387
|
indexesWithinAncestors: IndexesWithinAncestors;
|
|
388
|
+
classesMap: Map<string, Array<string>>;
|
|
368
389
|
}) => string;
|
package/lib/types/css/css.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ type Data = {
|
|
|
10
10
|
};
|
|
11
11
|
type CssOptions = {
|
|
12
12
|
assetBaseUrl: string;
|
|
13
|
+
atomic: boolean;
|
|
13
14
|
};
|
|
14
15
|
export declare const createImageValueTransformer: (assets: Map<string, {
|
|
15
16
|
type: "font";
|
|
@@ -58,6 +59,11 @@ export declare const createImageValueTransformer: (assets: Map<string, {
|
|
|
58
59
|
size: number;
|
|
59
60
|
description: string | null;
|
|
60
61
|
createdAt: string;
|
|
61
|
-
}>,
|
|
62
|
-
|
|
62
|
+
}>, { assetBaseUrl }: {
|
|
63
|
+
assetBaseUrl: string;
|
|
64
|
+
}) => TransformValue;
|
|
65
|
+
export declare const generateCss: (data: Data, { assetBaseUrl, atomic }: CssOptions) => {
|
|
66
|
+
cssText: string;
|
|
67
|
+
classesMap: Map<string, string[]>;
|
|
68
|
+
};
|
|
63
69
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { StyleSheetAtomic, StyleSheetRegular } from "@webstudio-is/css-engine";
|
|
2
2
|
import type { Assets } from "@webstudio-is/sdk";
|
|
3
|
-
export declare const addGlobalRules: (
|
|
3
|
+
export declare const addGlobalRules: (sheet: StyleSheetRegular | StyleSheetAtomic, { assets, assetBaseUrl }: {
|
|
4
4
|
assets: Map<string, {
|
|
5
5
|
type: "font";
|
|
6
6
|
name: string;
|
|
@@ -2336,6 +2336,12 @@ export declare const generateDataFromEmbedTemplate: (treeTemplate: ({
|
|
|
2336
2336
|
name: string;
|
|
2337
2337
|
id: string;
|
|
2338
2338
|
scopeInstanceId?: string | undefined;
|
|
2339
|
+
} | {
|
|
2340
|
+
type: "resource";
|
|
2341
|
+
name: string;
|
|
2342
|
+
id: string;
|
|
2343
|
+
resourceId: string;
|
|
2344
|
+
scopeInstanceId?: string | undefined;
|
|
2339
2345
|
})[];
|
|
2340
2346
|
styleSourceSelections: {
|
|
2341
2347
|
values: string[];
|
|
@@ -14,6 +14,43 @@ export declare const validateExpression: (code: string, options?: {
|
|
|
14
14
|
}) => string;
|
|
15
15
|
export declare const encodeDataSourceVariable: (id: string) => string;
|
|
16
16
|
export declare const decodeDataSourceVariable: (name: string) => string | undefined;
|
|
17
|
+
export declare const generateExpression: ({ expression, dataSources, scope, }: {
|
|
18
|
+
expression: string;
|
|
19
|
+
dataSources: Map<string, {
|
|
20
|
+
value: {
|
|
21
|
+
value: number;
|
|
22
|
+
type: "number";
|
|
23
|
+
} | {
|
|
24
|
+
value: string;
|
|
25
|
+
type: "string";
|
|
26
|
+
} | {
|
|
27
|
+
value: boolean;
|
|
28
|
+
type: "boolean";
|
|
29
|
+
} | {
|
|
30
|
+
value: string[];
|
|
31
|
+
type: "string[]";
|
|
32
|
+
} | {
|
|
33
|
+
type: "json";
|
|
34
|
+
value?: unknown;
|
|
35
|
+
};
|
|
36
|
+
type: "variable";
|
|
37
|
+
name: string;
|
|
38
|
+
id: string;
|
|
39
|
+
scopeInstanceId?: string | undefined;
|
|
40
|
+
} | {
|
|
41
|
+
type: "parameter";
|
|
42
|
+
name: string;
|
|
43
|
+
id: string;
|
|
44
|
+
scopeInstanceId?: string | undefined;
|
|
45
|
+
} | {
|
|
46
|
+
type: "resource";
|
|
47
|
+
name: string;
|
|
48
|
+
id: string;
|
|
49
|
+
resourceId: string;
|
|
50
|
+
scopeInstanceId?: string | undefined;
|
|
51
|
+
}>;
|
|
52
|
+
scope: Scope;
|
|
53
|
+
}) => string;
|
|
17
54
|
type VariableName = string;
|
|
18
55
|
export declare const generateDataSources: ({ scope, typed, dataSources, props, }: {
|
|
19
56
|
scope: Scope;
|
|
@@ -44,6 +81,12 @@ export declare const generateDataSources: ({ scope, typed, dataSources, props, }
|
|
|
44
81
|
name: string;
|
|
45
82
|
id: string;
|
|
46
83
|
scopeInstanceId?: string | undefined;
|
|
84
|
+
} | {
|
|
85
|
+
type: "resource";
|
|
86
|
+
name: string;
|
|
87
|
+
id: string;
|
|
88
|
+
resourceId: string;
|
|
89
|
+
scopeInstanceId?: string | undefined;
|
|
47
90
|
}>;
|
|
48
91
|
props: Map<string, {
|
|
49
92
|
value: number;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export * from "./remix";
|
|
1
2
|
export * from "./css/index";
|
|
2
3
|
export * from "./tree/index";
|
|
3
4
|
export * from "./app/index";
|
|
@@ -13,3 +14,4 @@ export { getIndexesWithinAncestors } from "./instance-utils";
|
|
|
13
14
|
export * from "./hook";
|
|
14
15
|
export { generateUtilsExport } from "./generator";
|
|
15
16
|
export { generatePageComponent, generateJsxElement, generateJsxChildren, } from "./component-generator";
|
|
17
|
+
export { generateResourcesLoader } from "./resources-generator";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* transforms url pattern subset to remix route format
|
|
3
|
+
*
|
|
4
|
+
* /:name/ -> .$name. - named dynamic segment
|
|
5
|
+
* /:name?/ -> .($name). - optional dynamic segment
|
|
6
|
+
* /* -> .$ - splat in the end of pattern
|
|
7
|
+
* /:name* -> .$ - named splat which gets specified name at runtime
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare const generateRemixRoute: (pathname: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* generates a function to convert remix params to compatible with url pattern groups
|
|
13
|
+
*
|
|
14
|
+
* for /:name* pattern
|
|
15
|
+
* params["*"] is replaced with params["name"]
|
|
16
|
+
*
|
|
17
|
+
* for /* pattern
|
|
18
|
+
* params["*"] is replaced with params[0]
|
|
19
|
+
*/
|
|
20
|
+
export declare const generateRemixParams: (pathname: string) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { DataSources, Page, Resources, Scope } from "@webstudio-is/sdk";
|
|
2
|
+
export declare const generateResourcesLoader: ({ scope, page, dataSources, resources, }: {
|
|
3
|
+
scope: Scope;
|
|
4
|
+
page: Page;
|
|
5
|
+
dataSources: Map<string, {
|
|
6
|
+
value: {
|
|
7
|
+
value: number;
|
|
8
|
+
type: "number";
|
|
9
|
+
} | {
|
|
10
|
+
value: string;
|
|
11
|
+
type: "string";
|
|
12
|
+
} | {
|
|
13
|
+
value: boolean;
|
|
14
|
+
type: "boolean";
|
|
15
|
+
} | {
|
|
16
|
+
value: string[];
|
|
17
|
+
type: "string[]";
|
|
18
|
+
} | {
|
|
19
|
+
type: "json";
|
|
20
|
+
value?: unknown;
|
|
21
|
+
};
|
|
22
|
+
type: "variable";
|
|
23
|
+
name: string;
|
|
24
|
+
id: string;
|
|
25
|
+
scopeInstanceId?: string | undefined;
|
|
26
|
+
} | {
|
|
27
|
+
type: "parameter";
|
|
28
|
+
name: string;
|
|
29
|
+
id: string;
|
|
30
|
+
scopeInstanceId?: string | undefined;
|
|
31
|
+
} | {
|
|
32
|
+
type: "resource";
|
|
33
|
+
name: string;
|
|
34
|
+
id: string;
|
|
35
|
+
resourceId: string;
|
|
36
|
+
scopeInstanceId?: string | undefined;
|
|
37
|
+
}>;
|
|
38
|
+
resources: Map<string, {
|
|
39
|
+
name: string;
|
|
40
|
+
id: string;
|
|
41
|
+
method: "get" | "post" | "put" | "delete";
|
|
42
|
+
url: string;
|
|
43
|
+
headers: {
|
|
44
|
+
value: string;
|
|
45
|
+
name: string;
|
|
46
|
+
}[];
|
|
47
|
+
body?: string | undefined;
|
|
48
|
+
}>;
|
|
49
|
+
}) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webstudio-is/react-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.124.0",
|
|
4
4
|
"description": "Webstudio JavaScript / TypeScript API",
|
|
5
5
|
"author": "Webstudio <github@webstudio.is>",
|
|
6
6
|
"homepage": "https://webstudio.is",
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
"jsep": "^1.3.8",
|
|
34
34
|
"nanoid": "^5.0.1",
|
|
35
35
|
"title-case": "^4.1.0",
|
|
36
|
-
"@webstudio-is/
|
|
37
|
-
"@webstudio-is/
|
|
38
|
-
"@webstudio-is/
|
|
39
|
-
"@webstudio-is/
|
|
40
|
-
"@webstudio-is/sdk": "0.
|
|
36
|
+
"@webstudio-is/fonts": "0.124.0",
|
|
37
|
+
"@webstudio-is/css-engine": "0.124.0",
|
|
38
|
+
"@webstudio-is/image": "0.124.0",
|
|
39
|
+
"@webstudio-is/icons": "^0.124.0",
|
|
40
|
+
"@webstudio-is/sdk": "0.124.0"
|
|
41
41
|
},
|
|
42
42
|
"exports": {
|
|
43
43
|
".": {
|