@webstudio-is/sdk 0.125.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
@@ -35,14 +35,14 @@ var MIN_TITLE_LENGTH = 2;
35
35
  var PageId = z2.string();
36
36
  var FolderId = z2.string();
37
37
  var FolderName = z2.string().refine((value) => value.trim() !== "", "Can't be empty");
38
- var FolderSlug = z2.string().refine((slug) => slug !== "", "Can't be empty").refine((slug) => slug.includes("/") === false, "Can't contain a /").refine(
38
+ var Slug = z2.string().refine(
39
39
  (path) => /^[-a-z0-9]*$/.test(path),
40
40
  "Only a-z, 0-9 and - are allowed"
41
41
  );
42
42
  var Folder = z2.object({
43
43
  id: FolderId,
44
44
  name: FolderName,
45
- slug: z2.string(),
45
+ slug: Slug,
46
46
  children: z2.array(z2.union([FolderId, PageId]))
47
47
  });
48
48
  var PageName = z2.string().refine((value) => value.trim() !== "", "Can't be empty");
@@ -127,12 +127,13 @@ var ExpressionChild = z3.object({
127
127
  type: z3.literal("expression"),
128
128
  value: z3.string()
129
129
  });
130
+ var InstanceChild = z3.union([IdChild, TextChild, ExpressionChild]);
130
131
  var Instance = z3.object({
131
132
  type: z3.literal("instance"),
132
133
  id: InstanceId,
133
134
  component: z3.string(),
134
135
  label: z3.string().optional(),
135
- children: z3.array(z3.union([IdChild, TextChild, ExpressionChild]))
136
+ children: z3.array(InstanceChild)
136
137
  });
137
138
  var Instances = z3.map(InstanceId, Instance);
138
139
 
@@ -363,6 +364,21 @@ var Deployment = z11.object({
363
364
  projectDomain: z11.string()
364
365
  });
365
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
+
366
382
  // src/instances-utils.ts
367
383
  var traverseInstances = (instances, instanceId, callback) => {
368
384
  const instance = instances.get(instanceId);
@@ -408,6 +424,54 @@ var parseComponentName = (componentName) => {
408
424
  return [namespace, name];
409
425
  };
410
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
+
411
475
  // src/scope.ts
412
476
  var normalizeName = (name) => {
413
477
  name = name.replaceAll(/[^\w$]/g, "");
@@ -484,13 +548,13 @@ export {
484
548
  ExpressionChild,
485
549
  Folder,
486
550
  FolderName,
487
- FolderSlug,
488
551
  FontAsset,
489
552
  HomePagePath,
490
553
  IdChild,
491
554
  ImageAsset,
492
555
  ImageMeta,
493
556
  Instance,
557
+ InstanceChild,
494
558
  Instances,
495
559
  PageName,
496
560
  PagePath,
@@ -498,6 +562,7 @@ export {
498
562
  Pages,
499
563
  Prop,
500
564
  Props,
565
+ ROOT_FOLDER_ID,
501
566
  Resource,
502
567
  Resources,
503
568
  StyleDecl,
@@ -507,11 +572,16 @@ export {
507
572
  StyleSources,
508
573
  Styles,
509
574
  TextChild,
575
+ WebstudioFragment,
510
576
  createScope,
577
+ findPageByIdOrPath,
578
+ findParentFolderByChildId,
511
579
  findTreeInstanceIds,
512
580
  findTreeInstanceIdsExcludingSlotDescendants,
581
+ getPagePath,
513
582
  getStyleDeclKey,
514
583
  initialBreakpoints,
584
+ isRoot,
515
585
  loadResource,
516
586
  parseComponentName
517
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";
@@ -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 {};
@@ -32,6 +32,34 @@ export declare const ExpressionChild: z.ZodObject<{
32
32
  type: "expression";
33
33
  }>;
34
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
+ }>]>;
35
63
  export declare const Instance: z.ZodObject<{
36
64
  type: z.ZodLiteral<"instance">;
37
65
  id: z.ZodString;
@@ -1,10 +1,9 @@
1
1
  import { z } from "zod";
2
2
  export declare const FolderName: z.ZodEffects<z.ZodString, string, string>;
3
- export declare const FolderSlug: z.ZodEffects<z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, string, string>, string, string>;
4
3
  export declare const Folder: z.ZodObject<{
5
4
  id: z.ZodString;
6
5
  name: z.ZodEffects<z.ZodString, string, string>;
7
- slug: z.ZodString;
6
+ slug: z.ZodEffects<z.ZodString, string, string>;
8
7
  children: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
9
8
  }, "strip", z.ZodTypeAny, {
10
9
  name: string;
@@ -323,7 +322,7 @@ export declare const Pages: z.ZodObject<{
323
322
  folders: z.ZodEffects<z.ZodArray<z.ZodObject<{
324
323
  id: z.ZodString;
325
324
  name: z.ZodEffects<z.ZodString, string, string>;
326
- slug: z.ZodString;
325
+ slug: z.ZodEffects<z.ZodString, string, string>;
327
326
  children: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
328
327
  }, "strip", z.ZodTypeAny, {
329
328
  name: string;