@webstudio-is/sdk 0.124.0 → 0.126.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
@@ -32,13 +32,26 @@ var Assets = z.map(AssetId, Asset);
32
32
  // src/schema/pages.ts
33
33
  import { z as z2 } from "zod";
34
34
  var MIN_TITLE_LENGTH = 2;
35
+ var PageId = z2.string();
36
+ var FolderId = z2.string();
37
+ var FolderName = z2.string().refine((value) => value.trim() !== "", "Can't be empty");
38
+ var Slug = z2.string().refine(
39
+ (path) => /^[-a-z0-9]*$/.test(path),
40
+ "Only a-z, 0-9 and - are allowed"
41
+ );
42
+ var Folder = z2.object({
43
+ id: FolderId,
44
+ name: FolderName,
45
+ slug: Slug,
46
+ children: z2.array(z2.union([FolderId, PageId]))
47
+ });
35
48
  var PageName = z2.string().refine((value) => value.trim() !== "", "Can't be empty");
36
49
  var PageTitle = z2.string().refine(
37
50
  (val) => val.length >= MIN_TITLE_LENGTH,
38
51
  `Minimum ${MIN_TITLE_LENGTH} characters required`
39
52
  );
40
53
  var commonPageFields = {
41
- id: z2.string(),
54
+ id: PageId,
42
55
  name: PageName,
43
56
  title: PageTitle,
44
57
  meta: z2.object({
@@ -95,26 +108,32 @@ var Pages = z2.object({
95
108
  pages: z2.array(Page).refine(
96
109
  (array) => new Set(array.map((page) => page.path)).size === array.length,
97
110
  "All paths must be unique"
98
- )
111
+ ),
112
+ folders: z2.array(Folder).refine((folders) => folders.length > 0, "Folders can't be empty")
99
113
  });
100
114
 
101
115
  // src/schema/instances.ts
102
116
  import { z as z3 } from "zod";
103
- var Text = z3.object({
117
+ var TextChild = z3.object({
104
118
  type: z3.literal("text"),
105
119
  value: z3.string()
106
120
  });
107
121
  var InstanceId = z3.string();
108
- var Id = z3.object({
122
+ var IdChild = z3.object({
109
123
  type: z3.literal("id"),
110
124
  value: InstanceId
111
125
  });
126
+ var ExpressionChild = z3.object({
127
+ type: z3.literal("expression"),
128
+ value: z3.string()
129
+ });
130
+ var InstanceChild = z3.union([IdChild, TextChild, ExpressionChild]);
112
131
  var Instance = z3.object({
113
132
  type: z3.literal("instance"),
114
133
  id: InstanceId,
115
134
  component: z3.string(),
116
135
  label: z3.string().optional(),
117
- children: z3.array(z3.union([Id, Text]))
136
+ children: z3.array(InstanceChild)
118
137
  });
119
138
  var Instances = z3.map(InstanceId, Instance);
120
139
 
@@ -345,6 +364,21 @@ var Deployment = z11.object({
345
364
  projectDomain: z11.string()
346
365
  });
347
366
 
367
+ // src/schema/webstudio.ts
368
+ import { z as z12 } from "zod";
369
+ var WebstudioFragment = z12.object({
370
+ children: z12.array(InstanceChild),
371
+ instances: z12.array(Instance),
372
+ assets: z12.array(Asset),
373
+ dataSources: z12.array(DataSource),
374
+ resources: z12.array(Resource),
375
+ props: z12.array(Prop),
376
+ breakpoints: z12.array(Breakpoint),
377
+ styleSourceSelections: z12.array(StyleSourceSelection),
378
+ styleSources: z12.array(StyleSource),
379
+ styles: z12.array(StyleDecl)
380
+ });
381
+
348
382
  // src/instances-utils.ts
349
383
  var traverseInstances = (instances, instanceId, callback) => {
350
384
  const instance = instances.get(instanceId);
@@ -390,6 +424,54 @@ var parseComponentName = (componentName) => {
390
424
  return [namespace, name];
391
425
  };
392
426
 
427
+ // src/page-utils.ts
428
+ var ROOT_FOLDER_ID = "root";
429
+ var isRoot = (folder) => folder.id === ROOT_FOLDER_ID;
430
+ var findPageByIdOrPath = (idOrPath, pages) => {
431
+ if (idOrPath === "" || idOrPath === "/" || idOrPath === pages.homePage.id) {
432
+ return pages.homePage;
433
+ }
434
+ return pages.pages.find(
435
+ (page) => page.id === idOrPath || getPagePath(page.id, pages) === idOrPath
436
+ );
437
+ };
438
+ var findParentFolderByChildId = (id, folders) => {
439
+ for (const folder of folders) {
440
+ if (folder.children.includes(id)) {
441
+ return folder;
442
+ }
443
+ }
444
+ };
445
+ var getPagePath = (id, pages) => {
446
+ const foldersMap = /* @__PURE__ */ new Map();
447
+ const childParentMap = /* @__PURE__ */ new Map();
448
+ for (const folder of pages.folders) {
449
+ foldersMap.set(folder.id, folder);
450
+ for (const childId of folder.children) {
451
+ childParentMap.set(childId, folder.id);
452
+ }
453
+ }
454
+ const paths = [];
455
+ let currentId = id;
456
+ const allPages = [pages.homePage, ...pages.pages];
457
+ for (const page of allPages) {
458
+ if (page.id === id) {
459
+ paths.push(page.path);
460
+ currentId = childParentMap.get(page.id);
461
+ break;
462
+ }
463
+ }
464
+ while (currentId) {
465
+ const folder = foldersMap.get(currentId);
466
+ if (folder === void 0) {
467
+ break;
468
+ }
469
+ paths.push(folder.slug);
470
+ currentId = childParentMap.get(currentId);
471
+ }
472
+ return paths.reverse().join("/").replace(/\/+/g, "/");
473
+ };
474
+
393
475
  // src/scope.ts
394
476
  var normalizeName = (name) => {
395
477
  name = name.replaceAll(/[^\w$]/g, "");
@@ -463,12 +545,16 @@ export {
463
545
  DataSourceVariableValue,
464
546
  DataSources,
465
547
  Deployment,
548
+ ExpressionChild,
549
+ Folder,
550
+ FolderName,
466
551
  FontAsset,
467
552
  HomePagePath,
468
- Id,
553
+ IdChild,
469
554
  ImageAsset,
470
555
  ImageMeta,
471
556
  Instance,
557
+ InstanceChild,
472
558
  Instances,
473
559
  PageName,
474
560
  PagePath,
@@ -476,6 +562,7 @@ export {
476
562
  Pages,
477
563
  Prop,
478
564
  Props,
565
+ ROOT_FOLDER_ID,
479
566
  Resource,
480
567
  Resources,
481
568
  StyleDecl,
@@ -484,12 +571,17 @@ export {
484
571
  StyleSourceSelections,
485
572
  StyleSources,
486
573
  Styles,
487
- Text,
574
+ TextChild,
575
+ WebstudioFragment,
488
576
  createScope,
577
+ findPageByIdOrPath,
578
+ findParentFolderByChildId,
489
579
  findTreeInstanceIds,
490
580
  findTreeInstanceIdsExcludingSlotDescendants,
581
+ getPagePath,
491
582
  getStyleDeclKey,
492
583
  initialBreakpoints,
584
+ isRoot,
493
585
  loadResource,
494
586
  parseComponentName
495
587
  };
@@ -9,6 +9,8 @@ export * from "./schema/style-sources";
9
9
  export * from "./schema/style-source-selections";
10
10
  export * from "./schema/styles";
11
11
  export * from "./schema/deployment";
12
+ export * from "./schema/webstudio";
12
13
  export * from "./instances-utils";
14
+ export * from "./page-utils";
13
15
  export * from "./scope";
14
16
  export * from "./resource-loader";
@@ -2,27 +2,33 @@ import type { Instance } from "./schema/instances";
2
2
  export declare const findTreeInstanceIds: (instances: Map<string, {
3
3
  type: "instance";
4
4
  id: string;
5
- component: string;
6
5
  children: ({
7
6
  value: string;
8
7
  type: "text";
9
8
  } | {
10
9
  value: string;
11
10
  type: "id";
11
+ } | {
12
+ value: string;
13
+ type: "expression";
12
14
  })[];
15
+ component: string;
13
16
  label?: string | undefined;
14
17
  }>, rootInstanceId: Instance["id"]) => Set<string>;
15
18
  export declare const findTreeInstanceIdsExcludingSlotDescendants: (instances: Map<string, {
16
19
  type: "instance";
17
20
  id: string;
18
- component: string;
19
21
  children: ({
20
22
  value: string;
21
23
  type: "text";
22
24
  } | {
23
25
  value: string;
24
26
  type: "id";
27
+ } | {
28
+ value: string;
29
+ type: "expression";
25
30
  })[];
31
+ component: string;
26
32
  label?: string | undefined;
27
33
  }>, rootInstanceId: Instance["id"]) => Set<string>;
28
34
  export declare const parseComponentName: (componentName: string) => readonly [string | undefined, string];
@@ -0,0 +1,18 @@
1
+ import type { Folder, Page, Pages } from "./schema/pages";
2
+ export declare const ROOT_FOLDER_ID = "root";
3
+ /**
4
+ * Returns true if folder is the root folder.
5
+ */
6
+ export declare const isRoot: (folder: Folder) => boolean;
7
+ /**
8
+ * Find a page by id or path.
9
+ */
10
+ export declare const findPageByIdOrPath: (idOrPath: string, pages: Pages) => Page | undefined;
11
+ /**
12
+ * Find a folder that has has that id in the children.
13
+ */
14
+ export declare const findParentFolderByChildId: (id: Folder["id"] | Page["id"], folders: Array<Folder>) => Folder | undefined;
15
+ /**
16
+ * Get a path from all folder slugs from root to the current folder or page.
17
+ */
18
+ export declare const getPagePath: (id: Folder["id"] | Page["id"], pages: Pages) => string;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- export declare const Text: z.ZodObject<{
2
+ export declare const TextChild: z.ZodObject<{
3
3
  type: z.ZodLiteral<"text">;
4
4
  value: z.ZodString;
5
5
  }, "strip", z.ZodTypeAny, {
@@ -9,8 +9,8 @@ export declare const Text: z.ZodObject<{
9
9
  value: string;
10
10
  type: "text";
11
11
  }>;
12
- export type Text = z.infer<typeof Text>;
13
- export declare const Id: z.ZodObject<{
12
+ export type TextChild = z.infer<typeof TextChild>;
13
+ export declare const IdChild: z.ZodObject<{
14
14
  type: z.ZodLiteral<"id">;
15
15
  value: z.ZodString;
16
16
  }, "strip", z.ZodTypeAny, {
@@ -20,7 +20,46 @@ export declare const Id: z.ZodObject<{
20
20
  value: string;
21
21
  type: "id";
22
22
  }>;
23
- export type Id = z.infer<typeof Id>;
23
+ export type IdChild = z.infer<typeof IdChild>;
24
+ export declare const ExpressionChild: z.ZodObject<{
25
+ type: z.ZodLiteral<"expression">;
26
+ value: z.ZodString;
27
+ }, "strip", z.ZodTypeAny, {
28
+ value: string;
29
+ type: "expression";
30
+ }, {
31
+ value: string;
32
+ type: "expression";
33
+ }>;
34
+ export type ExpressionChild = z.infer<typeof ExpressionChild>;
35
+ export declare const InstanceChild: z.ZodUnion<[z.ZodObject<{
36
+ type: z.ZodLiteral<"id">;
37
+ value: z.ZodString;
38
+ }, "strip", z.ZodTypeAny, {
39
+ value: string;
40
+ type: "id";
41
+ }, {
42
+ value: string;
43
+ type: "id";
44
+ }>, z.ZodObject<{
45
+ type: z.ZodLiteral<"text">;
46
+ value: z.ZodString;
47
+ }, "strip", z.ZodTypeAny, {
48
+ value: string;
49
+ type: "text";
50
+ }, {
51
+ value: string;
52
+ type: "text";
53
+ }>, z.ZodObject<{
54
+ type: z.ZodLiteral<"expression">;
55
+ value: z.ZodString;
56
+ }, "strip", z.ZodTypeAny, {
57
+ value: string;
58
+ type: "expression";
59
+ }, {
60
+ value: string;
61
+ type: "expression";
62
+ }>]>;
24
63
  export declare const Instance: z.ZodObject<{
25
64
  type: z.ZodLiteral<"instance">;
26
65
  id: z.ZodString;
@@ -44,30 +83,45 @@ export declare const Instance: z.ZodObject<{
44
83
  }, {
45
84
  value: string;
46
85
  type: "text";
86
+ }>, z.ZodObject<{
87
+ type: z.ZodLiteral<"expression">;
88
+ value: z.ZodString;
89
+ }, "strip", z.ZodTypeAny, {
90
+ value: string;
91
+ type: "expression";
92
+ }, {
93
+ value: string;
94
+ type: "expression";
47
95
  }>]>, "many">;
48
96
  }, "strip", z.ZodTypeAny, {
49
97
  type: "instance";
50
98
  id: string;
51
- component: string;
52
99
  children: ({
53
100
  value: string;
54
101
  type: "text";
55
102
  } | {
56
103
  value: string;
57
104
  type: "id";
105
+ } | {
106
+ value: string;
107
+ type: "expression";
58
108
  })[];
109
+ component: string;
59
110
  label?: string | undefined;
60
111
  }, {
61
112
  type: "instance";
62
113
  id: string;
63
- component: string;
64
114
  children: ({
65
115
  value: string;
66
116
  type: "text";
67
117
  } | {
68
118
  value: string;
69
119
  type: "id";
120
+ } | {
121
+ value: string;
122
+ type: "expression";
70
123
  })[];
124
+ component: string;
71
125
  label?: string | undefined;
72
126
  }>;
73
127
  export type Instance = z.infer<typeof Instance>;
@@ -94,30 +148,45 @@ export declare const Instances: z.ZodMap<z.ZodString, z.ZodObject<{
94
148
  }, {
95
149
  value: string;
96
150
  type: "text";
151
+ }>, z.ZodObject<{
152
+ type: z.ZodLiteral<"expression">;
153
+ value: z.ZodString;
154
+ }, "strip", z.ZodTypeAny, {
155
+ value: string;
156
+ type: "expression";
157
+ }, {
158
+ value: string;
159
+ type: "expression";
97
160
  }>]>, "many">;
98
161
  }, "strip", z.ZodTypeAny, {
99
162
  type: "instance";
100
163
  id: string;
101
- component: string;
102
164
  children: ({
103
165
  value: string;
104
166
  type: "text";
105
167
  } | {
106
168
  value: string;
107
169
  type: "id";
170
+ } | {
171
+ value: string;
172
+ type: "expression";
108
173
  })[];
174
+ component: string;
109
175
  label?: string | undefined;
110
176
  }, {
111
177
  type: "instance";
112
178
  id: string;
113
- component: string;
114
179
  children: ({
115
180
  value: string;
116
181
  type: "text";
117
182
  } | {
118
183
  value: string;
119
184
  type: "id";
185
+ } | {
186
+ value: string;
187
+ type: "expression";
120
188
  })[];
189
+ component: string;
121
190
  label?: string | undefined;
122
191
  }>>;
123
192
  export type Instances = z.infer<typeof Instances>;
@@ -1,4 +1,22 @@
1
1
  import { z } from "zod";
2
+ export declare const FolderName: z.ZodEffects<z.ZodString, string, string>;
3
+ export declare const Folder: z.ZodObject<{
4
+ id: z.ZodString;
5
+ name: z.ZodEffects<z.ZodString, string, string>;
6
+ slug: z.ZodEffects<z.ZodString, string, string>;
7
+ children: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
8
+ }, "strip", z.ZodTypeAny, {
9
+ name: string;
10
+ id: string;
11
+ slug: string;
12
+ children: string[];
13
+ }, {
14
+ name: string;
15
+ id: string;
16
+ slug: string;
17
+ children: string[];
18
+ }>;
19
+ export type Folder = z.infer<typeof Folder>;
2
20
  export declare const PageName: z.ZodEffects<z.ZodString, string, string>;
3
21
  export declare const PageTitle: z.ZodEffects<z.ZodString, string, string>;
4
22
  export declare const HomePagePath: z.ZodEffects<z.ZodString, string, string>;
@@ -301,6 +319,32 @@ export declare const Pages: z.ZodObject<{
301
319
  rootInstanceId: string;
302
320
  pathVariableId?: string | undefined;
303
321
  }[]>;
322
+ folders: z.ZodEffects<z.ZodArray<z.ZodObject<{
323
+ id: z.ZodString;
324
+ name: z.ZodEffects<z.ZodString, string, string>;
325
+ slug: z.ZodEffects<z.ZodString, string, string>;
326
+ children: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
327
+ }, "strip", z.ZodTypeAny, {
328
+ name: string;
329
+ id: string;
330
+ slug: string;
331
+ children: string[];
332
+ }, {
333
+ name: string;
334
+ id: string;
335
+ slug: string;
336
+ children: string[];
337
+ }>, "many">, {
338
+ name: string;
339
+ id: string;
340
+ slug: string;
341
+ children: string[];
342
+ }[], {
343
+ name: string;
344
+ id: string;
345
+ slug: string;
346
+ children: string[];
347
+ }[]>;
304
348
  }, "strip", z.ZodTypeAny, {
305
349
  homePage: {
306
350
  path: string;
@@ -338,6 +382,12 @@ export declare const Pages: z.ZodObject<{
338
382
  rootInstanceId: string;
339
383
  pathVariableId?: string | undefined;
340
384
  }[];
385
+ folders: {
386
+ name: string;
387
+ id: string;
388
+ slug: string;
389
+ children: string[];
390
+ }[];
341
391
  meta?: {
342
392
  siteName?: string | undefined;
343
393
  faviconAssetId?: string | undefined;
@@ -383,6 +433,12 @@ export declare const Pages: z.ZodObject<{
383
433
  rootInstanceId: string;
384
434
  pathVariableId?: string | undefined;
385
435
  }[];
436
+ folders: {
437
+ name: string;
438
+ id: string;
439
+ slug: string;
440
+ children: string[];
441
+ }[];
386
442
  meta?: {
387
443
  siteName?: string | undefined;
388
444
  faviconAssetId?: string | undefined;