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
feat: redesigner loadProject + add new rest api for CSS
Andras Bacsai03583078cb94
diff --git a/apps/designer/app/routes/rest/css.$projectId.$pageId.tsx b/apps/designer/app/routes/rest/css.$projectId.$pageId.tsxnew file mode 100644index 0000000000..5bddf23b86--- /dev/null+++ b/apps/designer/app/routes/rest/css.$projectId.$pageId.tsx@@ -0,0 +1,68 @@+import { ActionFunction, json } from "@remix-run/node";+import { db } from "@webstudio-is/project/server";+import { utils } from "@webstudio-is/project";+import { loadCanvasData } from "~/shared/db";+import env from "~/env.server";+import { sentryException } from "~/shared/sentry";+import { createCssEngine } from "@webstudio-is/css-engine";+import { idAttribute } from "@webstudio-is/react-sdk";+import { addGlobalRules } from "~/canvas/shared/styles";++export const loader: ActionFunction = async ({ params }) => {+ try {+ const projectId = params.projectId ?? undefined;+ const pageId = params.pageId ?? undefined;+ console.log({ projectId, pageId });+ if (projectId === undefined) {+ throw json("Required project id", { status: 404 });+ }++ if (pageId === undefined) {+ throw json("Required page id", { status: 404 });+ }++ const project = await db.project.loadByParams({ projectId });++ if (project === null) {+ throw json("Project not found", { status: 404 });+ }++ const canvasData = await loadCanvasData(project, "prod", pageId);++ if (canvasData === undefined) {+ throw json("Page not found", { status: 404 });+ }++ const engine = createCssEngine();++ addGlobalRules(engine, canvasData);++ for (const breakpoint of canvasData.breakpoints) {+ engine.addMediaRule(breakpoint.id, breakpoint);+ }++ const cssRules = utils.tree.getCssRules(canvasData.tree?.root);+ for (const [instanceId, cssRule] of cssRules) {+ engine.addStyleRule(`[${idAttribute}="${instanceId}"]`, cssRule);+ }+ const { cssText } = engine;++ return new Response(cssText, {+ headers: {+ "Content-Type": "text/css",+ },+ });+ } catch (error) {+ // If a Response is thrown, we're rethrowing it for Remix to handle.+ // https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders+ if (error instanceof Response) {+ throw error;+ }++ sentryException({ error });+ return {+ errors: error instanceof Error ? error.message : String(error),+ env,+ };+ }+};diff --git a/packages/http-client/src/index.d.ts b/packages/http-client/src/index.d.tsindex 2d7e6c1b4f..e6da2f1621 100644--- a/packages/http-client/src/index.d.ts+++ b/packages/http-client/src/index.d.ts@@ -4,17 +4,19 @@ import type {UserProp,} from "@webstudio-is/react-sdk";-export type IncludeTypes = "tree" | "props" | "breakpoints" | "pages";+export type IncludeTypes = "tree" | "props" | "breakpoints";export type Includes<T> = { [include in IncludeTypes]: T };export type Page = {id: string;root: [string, string];};export type Project = {- tree: BaseInstance & { errors: string };- props: Array<UserProp> & { errors: string };- breakpoints: Array<Breakpoint> & {- errors: string;+ [include in string]: {+ tree: BaseInstance & { errors: string };+ props: Array<UserProp> & { errors: string };+ breakpoints: Array<Breakpoint> & {+ errors: string;+ };+ css: string;};- pages: Record<string, unknown> | null;};diff --git a/packages/http-client/src/index.test.ts b/packages/http-client/src/index.test.tsindex fdcf40efc9..d449c19690 100644--- a/packages/http-client/src/index.test.ts+++ b/packages/http-client/src/index.test.ts@@ -1,6 +1,6 @@import { loadProject } from "./index";-const existingProjectId = "398d3918-9b00-4a43-8ab6-de4e151ae98a";+const existingProjectId = "40c7a865-0bc7-4012-9318-6c4c665425de";const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";const apiUrl = "http://localhost:3000";@@ -9,7 +9,7 @@ describe("getProjectDetails", () => {const response = await loadProject({apiUrl,projectId: existingProjectId,- include: { tree: true, props: true, breakpoints: true, pages: true },+ include: { tree: true, props: true, breakpoints: true },});expect(response.pages).toBeTruthy();});@@ -17,7 +17,7 @@ describe("getProjectDetails", () => {const response = await loadProject({apiUrl,projectId: notPublishedProjectId,- include: { tree: true, props: true, breakpoints: true, pages: true },+ include: { tree: true, props: true, breakpoints: true },});expect(response.pages).toBeNull();});diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.tsindex f757b20303..f2b3c4a444 100644--- a/packages/http-client/src/index.ts+++ b/packages/http-client/src/index.ts@@ -4,7 +4,7 @@ import type { Includes, Project, Page } from "./index.d";export const loadProject = async ({apiUrl,projectId,- include = { tree: true, props: true, breakpoints: true, pages: false },+ include = { tree: true, props: true, breakpoints: true },}: {apiUrl: string;projectId: string;@@ -14,67 +14,102 @@ export const loadProject = async ({throw new Error("Webstudio API URL is required.");}const urls = [];- const pages: Record<string, unknown> | null = {};-+ const gatheredPages: Project = {};const baseUrl = new URL(apiUrl);- const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl);- const propsUrl = new URL(`/rest/props/${projectId}`, baseUrl);- const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);+ // Get all pagesconst pagesUrl = new URL(`/rest/pages/${projectId}`, baseUrl);+ const listOfPages = await (await fetch(pagesUrl)).json();+ if (listOfPages === undefined) {+ throw new Error("Project not found");+ }+ const { pages, homePage } = listOfPages;+ if (homePage) {+ const { id } = homePage;+ const treeUrl = new URL(`/rest/tree/${projectId}/${id}`, baseUrl);+ const propsUrl = new URL(`/rest/props/${projectId}/${id}`, baseUrl);+ const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);+ const cssUrl = new URL(`/rest/css/${projectId}/${id}`, baseUrl);+ urls.push({+ url: treeUrl,+ path: "/",+ type: "tree",+ });+ urls.push({+ url: propsUrl,+ path: "/",+ type: "props",+ });+ urls.push({+ url: breakpointsUrl,+ path: "/",+ type: "breakpoints",+ });+ urls.push({+ url: cssUrl,+ path: "/",+ type: "css",+ });+ }- const [tree, props, breakpoints, listOfPages] = await Promise.all([- include.tree && (await fetch(treeUrl)).json(),- include.props && (await fetch(propsUrl)).json(),- include.breakpoints && (await fetch(breakpointsUrl)).json(),- include.pages && (await fetch(pagesUrl)).json(),- ]);+ if (pages && pages.length > 0) {+ for (const page of pages) {+ const { id, path } = page;+ const treeUrl = new URL(`/rest/tree/${projectId}/${id}`, baseUrl);+ const propsUrl = new URL(`/rest/props/${projectId}/${id}`, baseUrl);+ const cssUrl = new URL(`/rest/css/${projectId}/${id}`, baseUrl);- if (include.pages && listOfPages.pages && listOfPages.pages.length > 0) {- for (const page of listOfPages.pages) {urls.push({- url: new URL(`/rest/tree/${projectId}/${page.id}`, baseUrl),- path: page.path,+ url: treeUrl,+ path,type: "tree",});+urls.push({- url: new URL(`/rest/props/${projectId}/${page.id}`, baseUrl),- path: page.path,+ url: propsUrl,+ path,type: "props",});++ urls.push({+ url: cssUrl,+ path,+ type: "css",+ });}- if (urls.length === 0) {- return {- tree,- props,- breakpoints,- pages,- };- }+ }++ if (urls.length === 0) {+ throw new Error("No pages to fetch");+ }+ const data = await Promise.all(+ urls.map(async (url) => {+ const res = await fetch(url.url);+ if (res.ok) {+ return {+ path: url.path,+ type: url.type,+ data: url.type === "css" ? await res.text() : await res.json(),+ };+ }+ })+ );+ const breakpoints = data.find(+ (d) => d?.type === "breakpoints" && d.path === "/"+ );- const pagesData = await Promise.all(- urls.map(async (url) => {- const res = await fetch(url.url);- if (res.ok) {- return { path: url.path, type: url.type, data: await res.json() };- }- })- );- if (Object.keys(pagesData).length > 0) {- for (const page of pagesData) {- if (page && page.path && page.type && page.data) {- pages[page.path] = {- ...(pages[page.path] as Record<string, Page>),- [page.type]: page.data,- breakpoints,- };- }+ if (Object.keys(data).length > 0) {+ for (const page of data) {+ if (page && page.path && page.type && page.data) {+ const { path, type, data } = page;+ gatheredPages[path] = {+ ...gatheredPages[path],+ [type]: data,+ breakpoints: breakpoints?.data || {},+ };}}}return {- tree,- props,- breakpoints,- pages: Object.keys(pages).length > 0 ? pages : null,+ ...gatheredPages,};};