From 0f9043fc5446686943bcab37b681b967b0f2ab75 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 27 Jan 2023 12:34:28 +0300 Subject: [PATCH] Add style sources schema (#850) Ref https://github.com/webstudio-is/webstudio-designer/issues/807 We are gonna consolidate design token styles and local styles. On build (project) level there will be list of styles and list of style sources which are basically a group of styles to be referenced. These style sources can be local with mandatory tree id or token with optional tree id to scope a token on specific page instead of whole project. Tree will get new list of references to style sources. Here resulting types ```ts // Stored as Build.styles type StylesItem = { // same as before but instead of instanceId style is referenced through style source styleSourceId: string, breakpointId, property, value } type StyleSourcesToken = { type: token id: string treeId?: string name: string } type StyleSourcesLocal = { type: local id: string treeId: string name: string } // Stored as Build.styleSources type StyleSourcesItem = StyleSourcesToken | StyleSourcesLocal // Stored as Tree.styleRefs type StyleRefsItem = { instanceId: string, values: StyleSourcesItem['id'] } ``` --- .../migration.sql | 8 +++++ packages/prisma-client/prisma/schema.prisma | 16 +++++---- packages/project-build/src/index.ts | 1 + .../project-build/src/schema/style-sources.ts | 36 +++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 packages/prisma-client/prisma/migrations/20230127120101_style_sources/migration.sql create mode 100644 packages/project-build/src/schema/style-sources.ts diff --git a/packages/prisma-client/prisma/migrations/20230127120101_style_sources/migration.sql b/packages/prisma-client/prisma/migrations/20230127120101_style_sources/migration.sql new file mode 100644 index 000000000000..5ccac368bb16 --- /dev/null +++ b/packages/prisma-client/prisma/migrations/20230127120101_style_sources/migration.sql @@ -0,0 +1,8 @@ +-- AlterTable +ALTER TABLE "Build" + ADD COLUMN "styleSources" TEXT NOT NULL DEFAULT '[]', + ADD COLUMN "styles" TEXT NOT NULL DEFAULT '[]'; + +-- AlterTable +ALTER TABLE "Tree" + ADD COLUMN "styleSelections" TEXT NOT NULL DEFAULT '[]'; diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma index b46d5f2e7bf7..dcc3eac83ba1 100644 --- a/packages/prisma-client/prisma/schema.prisma +++ b/packages/prisma-client/prisma/schema.prisma @@ -75,15 +75,19 @@ model Build { breakpoints Breakpoints? designTokens DesignTokens? + + styles String @default("[]") + styleSources String @default("[]") } model Tree { - id String @id @default(uuid()) - root String - instances String @default("[]") - props String @default("[]") - presetStyles String @default("[]") - styles String @default("[]") + id String @id @default(uuid()) + root String + instances String @default("[]") + props String @default("[]") + presetStyles String @default("[]") + styles String @default("[]") + styleSelections String @default("[]") } model InstanceProps { diff --git a/packages/project-build/src/index.ts b/packages/project-build/src/index.ts index f4872ca7df78..7248fb538e7a 100644 --- a/packages/project-build/src/index.ts +++ b/packages/project-build/src/index.ts @@ -1,4 +1,5 @@ export * from "./schema/instances"; export * from "./schema/props"; export * from "./schema/styles"; +export * from "./schema/style-sources"; export * from "./types"; diff --git a/packages/project-build/src/schema/style-sources.ts b/packages/project-build/src/schema/style-sources.ts new file mode 100644 index 000000000000..89af6d1d7a91 --- /dev/null +++ b/packages/project-build/src/schema/style-sources.ts @@ -0,0 +1,36 @@ +import { z } from "zod"; + +const StyleSourceId = z.string(); + +const StyleSourceToken = z.object({ + type: z.literal("token"), + id: StyleSourceId, + treeId: z.string().optional(), + name: z.string(), +}); + +const StyleSourceLocal = z.object({ + type: z.literal("local"), + id: StyleSourceId, + treeId: z.string(), + name: z.string(), +}); + +export const StyleSource = z.union([StyleSourceToken, StyleSourceLocal]); + +export type StyleSource = z.infer; + +export const StyleSources = z.array(StyleSource); + +export type StyleSources = z.infer; + +export const StyleSourceSelection = z.object({ + instanceId: z.string(), + values: z.array(StyleSourceId), +}); + +export type StyleSourceSelection = z.infer; + +export const StyleSourceSelections = z.array(StyleSourceSelection); + +export type StyleSourceSelections = z.infer;