Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
create root component with styles on a breakpoint
Oleg Isonene3c9eb144712
diff --git a/app/shared/db/breakpoints.server.ts b/app/shared/db/breakpoints.server.tsindex 3d46c4e8b0..86b79d31dd 100644--- a/app/shared/db/breakpoints.server.ts+++ b/app/shared/db/breakpoints.server.ts@@ -1,4 +1,9 @@-import { initialBreakpoints, type Project, type Tree } from "@webstudio-is/sdk";+import {+ initialBreakpoints,+ type Project,+ type Tree,+ type Breakpoint,+} from "@webstudio-is/sdk";import ObjectId from "bson-objectid";import { applyPatches, type Patch } from "immer";import { prisma } from "./prisma.server";@@ -9,13 +14,19 @@ export const load = async (projectId: Project["id"]) => {});};-export const create = async (projectId: Project["id"]) => {+export const getBreakpointsWithId = () =>+ initialBreakpoints.map((breakpoint) => ({+ ...breakpoint,+ id: ObjectId().toString(),+ }));++export const create = async (+ projectId: Project["id"],+ breakpoints: Array<Breakpoint>+) => {const data = {projectId,- values: initialBreakpoints.map((breakpoint) => ({- ...breakpoint,- id: ObjectId().toString(),- })),+ values: breakpoints,};await prisma.breakpoints.create({ data });return data;diff --git a/app/shared/db/canvas.server.ts b/app/shared/db/canvas.server.tsindex e5d9108b86..8d642c5211 100644--- a/app/shared/db/canvas.server.ts+++ b/app/shared/db/canvas.server.ts@@ -8,16 +8,15 @@ export type ErrorData = {};const loadData = async (projectId: Project["id"]) => {- const [existingBreakpoints, project] = await Promise.all([+ const [breakpoints, project] = await Promise.all([db.breakpoints.load(projectId),db.project.loadById(projectId),]);if (project === null) throw new Error(`Project "${projectId}" not found`);- const [tree, props, breakpoints] = await Promise.all([+ const [tree, props] = await Promise.all([db.tree.loadByProject(project, "development"),db.props.loadByProject(project, "development"),- existingBreakpoints || db.breakpoints.create(projectId),]);return { tree, props, project, breakpoints: breakpoints?.values || [] };};diff --git a/app/shared/db/project.server.ts b/app/shared/db/project.server.tsindex 003ec6575f..a80880e7e1 100644--- a/app/shared/db/project.server.ts+++ b/app/shared/db/project.server.ts@@ -53,8 +53,9 @@ export const create = async ({}const domain = generateDomain(title);- const tree = await db.tree.create();- return await prisma.project.create({+ const breakpoints = db.breakpoints.getBreakpointsWithId();+ const tree = await db.tree.create(db.tree.createRootInstance(breakpoints));+ const project = await prisma.project.create({data: {userId,title,@@ -62,6 +63,8 @@ export const create = async ({devTreeId: tree.id,},});+ await db.breakpoints.create(project.id, breakpoints);+ return project;};export const clone = async (clonableDomain: string): Promise<Project> => {diff --git a/app/shared/db/tree.server.ts b/app/shared/db/tree.server.tsindex 683205361a..1a8d7eb117 100644--- a/app/shared/db/tree.server.ts+++ b/app/shared/db/tree.server.ts@@ -1,49 +1,68 @@-import { type Instance, type Project, type Tree } from "@webstudio-is/sdk";+import {+ type Instance,+ type Project,+ type Tree,+ type Breakpoint,+} from "@webstudio-is/sdk";import { applyPatches, type Patch } from "immer";import { prisma } from "./prisma.server";import { createInstance } from "~/shared/tree-utils";-const rootConfig = {+export const createRootInstance = (breakpoints: Array<Breakpoint>) => {+ const defaultBreakpoint = breakpoints.find(+ (breakpoint) => breakpoint.minWidth === 0+ );+ if (defaultBreakpoint === undefined) {+ throw new Error("A breakpoint with minWidth 0 is required");+ }// @todo this should be part of a root primitive in primitives- component: "Box",- style: {- backgroundColor: {- type: "keyword",- value: "white",- },- fontFamily: {- type: "keyword",- value: "Arial",- },- fontSize: {- type: "unit",- unit: "px",- value: 14,- },- lineHeight: {- type: "unit",- unit: "px",- value: 20,- },- color: {- type: "keyword",- value: "#232323",- },- minHeight: {- type: "unit",- unit: "vh",- value: 100,- },- flexDirection: {- type: "keyword",- value: "column",- },- },-} as const;+ const rootConfig: Pick<Instance, "component" | "cssRules"> = {+ component: "Box",+ cssRules: [+ {+ breakpoint: defaultBreakpoint.id,+ style: {+ backgroundColor: {+ type: "keyword",+ value: "white",+ },+ fontFamily: {+ type: "keyword",+ value: "Arial",+ },+ fontSize: {+ type: "unit",+ unit: "px",+ value: 14,+ },+ lineHeight: {+ type: "unit",+ unit: "px",+ value: 20,+ },+ color: {+ type: "keyword",+ value: "#232323",+ },+ minHeight: {+ type: "unit",+ unit: "vh",+ value: 100,+ },+ flexDirection: {+ type: "keyword",+ value: "column",+ },+ },+ },+ ],+ };+ return createInstance(rootConfig);+};-export const create = async (root?: Instance): Promise<Tree> => {+export const create = async (root: Instance): Promise<Tree> => {return (await prisma.tree.create({- data: { root: root ?? createInstance(rootConfig) },+ data: { root },})) as Tree;};