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
Merge branch main into 54-multi-page-sites-and-routing
Andras Bacsai0770b4034a7e
diff --cc apps/designer/app/routes/designer/$id.tsxindex 1bc0df733c,d9ec4d91c8..75a4119232--- a/apps/designer/app/routes/designer/$id.tsx+++ b/apps/designer/app/routes/designer/$id.tsx@@@ -1,19 -1,18 +1,20 @@@import { useLoaderData } from "@remix-run/react";import { LoaderFunction } from "@remix-run/node";import { Designer, links } from "~/designer";- import * as db from "~/shared/db";+ import { db } from "@webstudio-is/project";+ import type { Project } from "@webstudio-is/project";import config from "~/config";-import env from "~/env.server";import { ErrorMessage } from "~/shared/error";import { action, useAction } from "./_assets";- export { links, action };+ export { action, links };-export const loader: LoaderFunction = async ({ params }) => {+export const loader: LoaderFunction = async ({+ params,+}): Promise<Data | Error> => {if (params.id === undefined) throw new Error("Project id undefined");const project = await db.project.loadById(params.id);+if (project === null) {return { errors: `Project "${params.id}" not found` };}diff --cc apps/designer/app/routes/rest/patch.tsindex 42d1b0964e,abc739d53b..d654a68b1a--- a/apps/designer/app/routes/rest/patch.ts+++ b/apps/designer/app/routes/rest/patch.ts@@@ -1,7 -1,8 +1,7 @@@import { type ActionFunction } from "@remix-run/node";- import * as db from "~/shared/db";+ import { db } from "@webstudio-is/project";import { type SyncItem } from "immerhin";import { type Tree } from "@webstudio-is/react-sdk";-import { type Project } from "@webstudio-is/prisma-client";const updaters = {root: db.tree.patchRoot,diff --cc apps/designer/app/shared/db/index.tsindex 21a7c26a97,9ae3bd4ee5..b7d9801512--- a/apps/designer/app/shared/db/index.ts+++ b/apps/designer/app/shared/db/index.ts@@@ -1,9 -1,3 +1,4 @@@- export * as tree from "./tree.server";- export * as project from "./project.server";- export * as props from "./props.server";export * as misc from "./misc.server";export * as user from "./user.server";+// @todo make the export consistent with the restexport * from "./canvas.server";- export * as breakpoints from "./breakpoints.server";- export * as build from "./build.server";diff --cc packages/project/src/db/build.tsindex 1a279417fb,0000000000..e2e6a2ad4fmode 100644,000000..100644--- a/packages/project/src/db/build.ts+++ b/packages/project/src/db/build.ts@@@ -1,141 -1,0 +1,119 @@@+import { prisma, Build as DbBuild } from "@webstudio-is/prisma-client";- import { z } from "zod";+import { v4 as uuid } from "uuid";+import * as db from ".";-- const PageSchema = z.object({- id: z.string(),- name: z.string(),- path: z.string(),- title: z.string(),- meta: z.record(z.string(), z.string()),- treeId: z.string(),- });-- type Page = z.infer<typeof PageSchema>;-- const PagesSchema: z.ZodType<{ homePage: Page; pages: Array<Page> }> = z.object(- {- homePage: PageSchema,- pages: z.array(PageSchema),- }- );-- type Pages = z.infer<typeof PagesSchema>;-- export type Build = Omit<DbBuild, "pages"> & { pages: Pages };++import { Build, Pages, PagesSchema } from "./types";++export const parseBuild = (build: DbBuild): Build => {+ const pages = PagesSchema.parse(JSON.parse(build.pages));++ return { ...build, pages };+};++export const loadById = async (id: Build["id"]): Promise<Build> => {+ if (typeof id !== "string") {+ throw new Error("Build ID required");+ }++ const build = await prisma.build.findUnique({+ where: { id },+ });++ if (build === null) {+ throw new Error("Build not found");+ }++ return parseBuild(build);+};++export const loadProdByProjectId = async (+ projectId: Build["projectId"]+): Promise<Build | undefined> => {+ if (typeof projectId !== "string") {+ throw new Error("Project ID required");+ }++ const build = await prisma.build.findFirst({+ where: { projectId, isCurrentProd: true },+ });++ if (build === null) {+ return undefined;+ }++ return parseBuild(build);+};++const createPages = async () => {+ const breakpoints = db.breakpoints.getBreakpointsWithId();+ const tree = await db.tree.create(db.tree.createRootInstance(breakpoints));+ await db.breakpoints.create(tree.id, breakpoints);+ return PagesSchema.parse({+ homePage: {+ id: uuid(),+ name: "Home",+ path: "",+ title: "Home",+ meta: {},+ treeId: tree.id,+ },+ pages: [],+ });+};++const clonePages = async (source: Pages) => {+ // @todo: clone pages other than home page++ const treeId = source.homePage.treeId;++ const tree = await db.tree.clone(treeId);+ await db.props.clone({ previousTreeId: treeId, nextTreeId: tree.id });+ await db.breakpoints.clone({ previousTreeId: treeId, nextTreeId: tree.id });++ return PagesSchema.parse({+ homePage: {+ ...source.homePage,+ id: uuid(),+ treeId: tree.id,+ },+ pages: [],+ });+};++export const createDev = async (baseBuild?: Build) => {+ if (baseBuild === undefined) {+ const pages = await createPages();+ return prisma.build.create({+ data: { pages: JSON.stringify(pages) },+ });+ } else {+ const pages = await clonePages(baseBuild.pages);+ return prisma.build.create({+ data: { pages: JSON.stringify(pages) },+ });+ }+};++export const createProd = async (+ sourceBuild: Build,+ projectId: Build["projectId"]+) => {+ const pages = await clonePages(sourceBuild.pages);++ const cloneBuild = await prisma.build.create({+ data: {+ pages: JSON.stringify(pages),+ projectId: projectId,+ },+ });++ await prisma.$transaction([+ prisma.build.updateMany({+ where: { projectId: projectId, isCurrentProd: true },+ data: { isCurrentProd: false },+ }),+ prisma.build.update({+ where: { id: cloneBuild.id },+ data: { isCurrentProd: true },+ }),+ ]);+};diff --cc packages/project/src/db/index.tsindex 0000000000,3c3ad21e48..0c117c9378mode 000000,100644..100644--- a/packages/project/src/db/index.ts+++ b/packages/project/src/db/index.ts@@@ -1,0 -1,5 +1,6 @@@+ import * as project from "./project";+ import * as tree from "./tree";+ import * as props from "./props";+ import * as breakpoints from "./breakpoints";-export { project, tree, breakpoints, props };++import * as build from "./build";++export { project, tree, breakpoints, props, build };diff --cc packages/project/src/db/project.tsindex b1a8adfdf3,e67c58fc71..c4b5844efe--- a/packages/project/src/db/project.ts+++ b/packages/project/src/db/project.ts@@@ -6,20 -7,17 +6,21 @@@ importPrisma,Project as BaseProject,} from "@webstudio-is/prisma-client";- import * as db from ".";import { formatAsset } from "@webstudio-is/asset-uploader";+import type { Asset } from "@webstudio-is/asset-uploader";++import { Build } from "./types";+ import * as db from "./index";-import type { Project } from "../index";-const TreeHistorySchema = z.array(z.string());++export type Project = Omit<BaseProject, "assets" | "devBuild"> & {+ assets?: Array<Asset>;- devBuild?: db.build.Build;++ devBuild?: Build;+};const parseProject = (project: BaseProject): Project => {- const prodTreeIdHistory = JSON.parse(project.prodTreeIdHistory);return {...project,- prodTreeIdHistory: TreeHistorySchema.parse(prodTreeIdHistory),assets: project?.assets?.map(formatAsset),+ devBuild: project?.devBuild && db.build.parseBuild(project.devBuild),};};@@@ -97,7 -92,6 +98,7 @@@ export const create = async (}: {userId: string;title: string;- devBuildId?: db.build.Build["id"];++ devBuildId?: Build["id"];}) => {if (title.length < MIN_TITLE_LENGTH) {throw new Error(`Minimum ${MIN_TITLE_LENGTH} characters required`);diff --cc packages/project/src/db/tree.tsindex 82d9266ca0,f7f5a48141..859cc8b025--- a/packages/project/src/db/tree.ts+++ b/packages/project/src/db/tree.ts@@@ -6,9 -6,9 +6,8 @@@ import} from "@webstudio-is/react-sdk";import { applyPatches, type Patch } from "immer";import { prisma } from "@webstudio-is/prisma-client";- import { createInstance, populateInstance } from "~/shared/tree-utils";- import { sort } from "~/shared/breakpoints";import { Tree as DbTree } from "@prisma/client";-import type { Project } from "../index";+ import { utils } from "../index";export const createRootInstance = (breakpoints: Array<Breakpoint>) => {// Take the smallest breakpoint as defaultdiff --cc packages/project/src/db/types.tsindex 0000000000,1dd7e6d83d..a4189139a5mode 000000,100644..100644--- a/packages/project/src/db/types.ts+++ b/packages/project/src/db/types.ts@@@ -1,0 -1,9 +1,33 @@@+ import { z } from "zod";-import { Project as BaseProject } from "@webstudio-is/prisma-client";++import {++ Project as BaseProject,++ Build as DbBuild,++} from "@webstudio-is/prisma-client";+ import type { Asset } from "@webstudio-is/asset-uploader";-const TreeHistorySchema = z.array(z.string());+-export type Project = Omit<BaseProject, "prodTreeIdHistory" | "assets"> & {- prodTreeIdHistory: z.infer<typeof TreeHistorySchema>;++// Project++export type Project = Omit<BaseProject, "assets" | "devBuild"> & {+ assets?: Array<Asset>;++ devBuild?: Build;+ };++++// Build++const PageSchema = z.object({++ id: z.string(),++ name: z.string(),++ path: z.string(),++ title: z.string(),++ meta: z.record(z.string(), z.string()),++ treeId: z.string(),++});++++export type Page = z.infer<typeof PageSchema>;++++export const PagesSchema: z.ZodType<{ homePage: Page; pages: Array<Page> }> =++ z.object({++ homePage: PageSchema,++ pages: z.array(PageSchema),++ });++export type Pages = z.infer<typeof PagesSchema>;++++export type Build = Omit<DbBuild, "pages"> & { pages: Pages };diff --cc yarn.lockindex d6cc70497a,095f496093..7583045aeb--- a/yarn.lock+++ b/yarn.lock@@@ -18123,10 -18118,10 +18128,10 @@@ zod@^3.17.3resolved "https://registry.yarnpkg.com/zod/-/zod-3.17.3.tgz#86abbc670ff0063a4588d85a4dcc917d6e4af2ba"integrity sha512-4oKP5zvG6GGbMlqBkI5FESOAweldEhSOZ6LI6cG+JzUT7ofj1ZOC0PJudpQOpT1iqOFpYYtX5Pw0+o403y4bcg==- zod@^3.19.0:- version "3.19.0"- resolved "https://registry.yarnpkg.com/zod/-/zod-3.19.0.tgz#a9303fcaed0950a3dc57bf35cc11f6282d4f52f2"- integrity sha512-Yw0qvUsCNGBe5YacikdMt5gVYeUuaEFVDIHKMfElrGSaBhwR3CQK6vOzgfAJOjTdGIhEeoaj8GtT+NDZrepZbw==-zod@^3.19.1:++zod@^3.19.0, zod@^3.19.1:+ version "3.19.1"+ resolved "https://registry.yarnpkg.com/zod/-/zod-3.19.1.tgz#112f074a97b50bfc4772d4ad1576814bd8ac4473"+ integrity sha512-LYjZsEDhCdYET9ikFu6dVPGp2YH9DegXjdJToSzD9rO6fy4qiRYFoyEYwps88OseJlPyl2NOe2iJuhEhL7IpEA==zwitch@^1.0.0:version "1.0.5"