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
support multiple trees in props + move db types to sdk
Oleg Isonen509e66fa8e27
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsxindex 9809bbebb0..3ef023989d 100644--- a/app/canvas/canvas.tsx+++ b/app/canvas/canvas.tsx@@ -72,7 +72,7 @@ export const Canvas = ({ data }: { data: Data }): JSX.Element => {const { instanceInsertionSpec, instanceReparentingSpec } =useDragDropHandlers({ rootInstance });useUpdateInstanceStyle({ rootInstance, setRootInstance });- usePublishSelectedInstance();+ usePublishSelectedInstance({ treeId: data.tree.id });useInsertInstance({ rootInstance, setRootInstance, instanceInsertionSpec });useReparentInstance({rootInstance,diff --git a/app/canvas/hooks/publish.ts b/app/canvas/hooks/publish.tsindex dda01fd68e..8ca8328cd9 100644--- a/app/canvas/hooks/publish.ts+++ b/app/canvas/hooks/publish.ts@@ -2,28 +2,47 @@ import { useEffect } from "react";import { type SelectedInstanceData } from "~/shared/component";import {getBrowserStyle,- type Instance,useAllUserProps,+ type Instance,+ type Tree,} from "@webstudio-is/sdk";import { useSelectedInstance, useSelectedElement } from "../nano-values";import { publish } from "../pubsub";+import ObjectId from "bson-objectid";-export const usePublishSelectedInstance = () => {+export const usePublishSelectedInstance = ({+ treeId,+}: {+ treeId: Tree["id"];+}) => {const [instance] = useSelectedInstance();const [selectedElement] = useSelectedElement();const [allUserProps] = useAllUserProps();useEffect(() => {+ // Unselects the instance by `undefined`+ let payload;+ if (instance !== undefined) {+ let props = allUserProps.get(instance.id);+ if (props === undefined) {+ props = {+ id: ObjectId().toString(),+ instanceId: instance.id,+ treeId,+ props: [],+ };+ }+ payload = {+ id: instance.id,+ component: instance.component,+ style: instance.style,+ browserStyle: getBrowserStyle(selectedElement),+ props,+ };+ }+publish<"selectInstance", SelectedInstanceData>({type: "selectInstance",- payload: instance- ? {- id: instance.id,- component: instance.component,- style: instance.style,- browserStyle: getBrowserStyle(selectedElement),- props: allUserProps.get(instance.id) || [],- }- : undefined, // Unselects the instance+ payload,});}, [instance, selectedElement]);};diff --git a/app/canvas/hooks/tree.ts b/app/canvas/hooks/tree.tsindex 77dba7dfad..b0a46aa1b4 100644--- a/app/canvas/hooks/tree.ts+++ b/app/canvas/hooks/tree.ts@@ -1,6 +1,5 @@import { useEffect, useState, useMemo, useRef } from "react";-import { type Instance } from "@webstudio-is/sdk";-import type { Tree } from "~/shared/db";+import { type Instance, type Tree } from "@webstudio-is/sdk";import {type InstanceInsertionSpec,type InstanceReparentingSpec,diff --git a/app/canvas/wrapper-component/wrapper-component.tsx b/app/canvas/wrapper-component/wrapper-component.tsxindex 8632c670cd..371f4df34e 100644--- a/app/canvas/wrapper-component/wrapper-component.tsx+++ b/app/canvas/wrapper-component/wrapper-component.tsx@@ -72,8 +72,13 @@ export const WrapperComponentDev = ({data-id={id}children={renderWrapperComponentChildren(contentEditableProps.children)}ref={refCallback}+ onClick={(event: MouseEvent) => {+ if (component === "Link") {+ event.preventDefault();+ }+ }}onMouseDown={(event: MouseEvent) => {- if (component === "Link" || component === "Input") {+ if (component === "Input") {event.preventDefault();}}}diff --git a/app/designer/designer.tsx b/app/designer/designer.tsxindex cff45141ab..c488db3749 100644--- a/app/designer/designer.tsx+++ b/app/designer/designer.tsx@@ -3,7 +3,7 @@ import { DndProvider } from "react-dnd";import { HTML5Backend } from "react-dnd-html5-backend";import type { Config } from "~/config";import type { SelectedInstanceData } from "~/shared/component";-import type { Project } from "~/shared/db";+import type { Project } from "@webstudio-is/sdk";import { Box, Grid, type CSS } from "~/shared/design-system";import { SidebarLeft } from "./sidebar-left";import { SidebarRight } from "./inspector";diff --git a/app/designer/settings-panel/use-props-logic.ts b/app/designer/settings-panel/use-props-logic.tsindex 048c732c75..40c5cfa562 100644--- a/app/designer/settings-panel/use-props-logic.ts+++ b/app/designer/settings-panel/use-props-logic.ts@@ -33,20 +33,25 @@ export const usePropsLogic = ({return;}const props =- selectedInstanceData.props.length === 0+ selectedInstanceData.props === undefined ||+ selectedInstanceData.props.props.length === 0? initialUserProps- : selectedInstanceData.props;+ : selectedInstanceData.props.props;setUserProps(props);}, [selectedInstanceData?.props]);const updateProps = useCallback(// @todo this may call the last callback after unmount// use useDebounce- debounce((updates) => {- if (selectedInstanceData === undefined) return;+ debounce((updates: UserPropsUpdates["updates"]) => {+ if (selectedInstanceData === undefined) {+ return;+ }publish<"updateProps", UserPropsUpdates>({type: "updateProps",payload: {+ treeId: selectedInstanceData.props.treeId,+ propsId: selectedInstanceData.props.id,instanceId: selectedInstanceData.id,updates,},@@ -83,7 +88,8 @@ export const usePropsLogic = ({propsToPublishRef.current[id] = true;const updates = Object.keys(propsToPublishRef.current).map((id) => nextUserProps.find((prop) => prop.id === id))- .filter(Boolean); // Can be empty if you quickly remove props which have pending changes+ // Could be empty if you quickly remove props which have pending changes+ .filter(Boolean) as Array<UserProp>;updateProps(updates);};diff --git a/app/designer/sync/use-sync.ts b/app/designer/sync/use-sync.tsindex 5eac7985f5..1929ac563f 100644--- a/app/designer/sync/use-sync.ts+++ b/app/designer/sync/use-sync.ts@@ -4,9 +4,9 @@ import {type Instance,type DeleteProp,type UserPropsUpdates,+ type Project,} from "@webstudio-is/sdk";import type { Config } from "~/config";-import type { Project } from "~/shared/db";import type {InstanceInsertionSpec,InstanceReparentingSpec,diff --git a/app/designer/topbar/publish.tsx b/app/designer/topbar/publish.tsxindex 45085a73c5..2e06402e42 100644--- a/app/designer/topbar/publish.tsx+++ b/app/designer/topbar/publish.tsx@@ -2,7 +2,7 @@ import { useState, useEffect } from "react";import { useFetcher } from "remix";import { useId } from "@radix-ui/react-id";import { RocketIcon, ExternalLinkIcon } from "~/shared/icons";-import type { Project } from "~/shared/db";+import { type Project } from "@webstudio-is/sdk";import {Button,Flex,diff --git a/app/designer/topbar/share.tsx b/app/designer/topbar/share.tsxindex da1420de6c..9d29bbb03e 100644--- a/app/designer/topbar/share.tsx+++ b/app/designer/topbar/share.tsx@@ -8,7 +8,7 @@ import {TextField,} from "~/shared/design-system";import { Share1Icon } from "~/shared/icons";-import type { Project } from "~/shared/db";+import { type Project } from "@webstudio-is/sdk";import { useIsShareDialogOpen } from "../nano-values";type ShareProps = { path: string; project: Project };diff --git a/app/designer/topbar/topbar.tsx b/app/designer/topbar/topbar.tsxindex 95c3488fdb..6f8c198f43 100644--- a/app/designer/topbar/topbar.tsx+++ b/app/designer/topbar/topbar.tsx@@ -1,5 +1,5 @@+import type { Project } from "@webstudio-is/sdk";import { Flex, type CSS } from "~/shared/design-system";-import type { Project } from "~/shared/db";import type { Config } from "~/config";import { Preview } from "./preview";import { Share } from "./share";diff --git a/app/routes/canvas/$id.tsx b/app/routes/canvas/$projectId.tsxsimilarity index 61%rename from app/routes/canvas/$id.tsxrename to app/routes/canvas/$projectId.tsxindex ce77658cb9..0112218714 100644--- a/app/routes/canvas/$id.tsx+++ b/app/routes/canvas/$projectId.tsx@@ -10,18 +10,13 @@ export type ErrorData = {export const loader: LoaderFunction = async ({params,}): Promise<Data | ErrorData> => {- if (params.id === undefined) throw new Error("Project id undefined");- const data = {} as Data;try {- const project = await db.project.loadOne(params.id);- if (project === null) {- return {- errors: `Project "${params.id}" doesn't exist`,- };- }- // @todo parallelize- data.tree = await db.tree.load(project.devTreeId);- data.props = await db.props.loadForTree(project.devTreeId);+ const project = await db.project.loadById(params.projectId);+ const [tree, props] = await Promise.all([+ db.tree.loadByProject(project, "development"),+ db.props.loadByProject(project, "development"),+ ]);+ return { tree, props };} catch (error) {if (error instanceof Error) {return {@@ -29,7 +24,7 @@ export const loader: LoaderFunction = async ({};}}- return data;+ return { errors: "Unexpected error" };};export default () => {diff --git a/app/routes/designer/$id.tsx b/app/routes/designer/$id.tsxindex 0f28de0717..bbcbc1ebde 100644--- a/app/routes/designer/$id.tsx+++ b/app/routes/designer/$id.tsx@@ -1,4 +1,5 @@import { useLoaderData, type LoaderFunction } from "remix";+import type { Project } from "@webstudio-is/sdk";import { Designer, links } from "~/designer";import * as db from "~/shared/db";import config from "~/config";@@ -7,13 +8,13 @@ export { links };export const loader: LoaderFunction = async ({ params }) => {if (params.id === undefined) throw new Error("Project id undefined");- const project = await db.project.loadOne(params.id);+ const project = await db.project.loadById(params.id);return { config, project };};type Data = {config: typeof config;- project: db.Project;+ project: Project;};export default () => {diff --git a/app/routes/index.tsx b/app/routes/index.tsxindex ae1521a86e..89cf668f2e 100644--- a/app/routes/index.tsx+++ b/app/routes/index.tsx@@ -1,12 +1,13 @@import { redirect, useLoaderData } from "remix";import { type LoaderFunction } from "remix";+import type { Tree } from "@webstudio-is/sdk";import config from "~/config";-import * as preview from "./preview/$id";+import * as preview from "./preview/$projectId";import * as db from "~/shared/db";// @todo all this subdomain logic is very hacky-type LoaderData = { tree: db.Tree | null; errors?: string };+type LoaderData = { tree: Tree | null; errors?: string };export const loader: LoaderFunction = async ({ request }) => {const host =@@ -18,7 +19,7 @@ export const loader: LoaderFunction = async ({ request }) => {if (subdomain !== "www" && domain !== undefined) {const data = {} as LoaderData;try {- data.tree = await db.tree.loadForDomain(subdomain);+ data.tree = await db.tree.loadByDomain(subdomain);} catch (error) {if (error instanceof Error) {data.errors = error.message;diff --git a/app/routes/preview/$id.tsx b/app/routes/preview/$id.tsxdeleted file mode 100644index d1a9cb73d8..0000000000--- a/app/routes/preview/$id.tsx+++ /dev/null@@ -1,10 +0,0 @@-import { useLoaderData } from "remix";-import { Root, type Data } from "@webstudio-is/sdk";-import { loader } from "../canvas/$id";--export { loader };--export default function Preview() {- const data = useLoaderData<Data>();- return <Root data={data} />;-}diff --git a/app/routes/preview/$projectId.tsx b/app/routes/preview/$projectId.tsxnew file mode 100644index 0000000000..e3d747a6f0--- /dev/null+++ b/app/routes/preview/$projectId.tsx@@ -0,0 +1,14 @@+import { useLoaderData } from "remix";+import { Root, type Data } from "@webstudio-is/sdk";+// @todo bad idea to import directly from routes+import { loader, type ErrorData } from "../canvas/$projectId";++export { loader };++export default function Preview() {+ const data = useLoaderData<Data | ErrorData>();+ if ("errors" in data) {+ return <p>{data.errors}</p>;+ }+ return <Root data={data} />;+}diff --git a/app/routes/rest/props.$projectId.ts b/app/routes/rest/props.$projectId.tsnew file mode 100644index 0000000000..138b5b9a97--- /dev/null+++ b/app/routes/rest/props.$projectId.ts@@ -0,0 +1,23 @@+import { type LoaderFunction } from "remix";+import { type InstanceProps } from "@webstudio-is/sdk";+import * as db from "~/shared/db";++type ErrorData = {+ errors: string;+};++export const loader: LoaderFunction = async ({+ params,+}): Promise<Array<InstanceProps> | ErrorData> => {+ try {+ const project = await db.project.loadById(params.projectId);+ return await db.props.loadByProject(project, "production");+ } catch (error) {+ if (error instanceof Error) {+ return {+ errors: error.message,+ };+ }+ }+ return { errors: "Unexpected error" };+};diff --git a/app/routes/rest/props.update.ts b/app/routes/rest/props.update.tsindex 1bfe387c36..a10d9132b9 100644--- a/app/routes/rest/props.update.ts+++ b/app/routes/rest/props.update.ts@@ -1,8 +1,9 @@import { type ActionFunction } from "remix";import * as db from "~/shared/db";-export const action: ActionFunction = async ({ request, params }) => {- const { instanceId, treeId, updates } = await request.json();- await db.props.update(instanceId, treeId, updates);+export const action: ActionFunction = async ({ request }) => {+ // @todo types+ const update = await request.json();+ await db.props.update(update);return { status: "ok" };};diff --git a/app/routes/rest/publish.ts b/app/routes/rest/publish.tsindex 3940c4037c..2927d65642 100644--- a/app/routes/rest/publish.ts+++ b/app/routes/rest/publish.ts@@ -5,31 +5,15 @@ export const action: ActionFunction = async ({ request }) => {const formData = await request.formData();const domain = formData.get("domain") as string | null;const projectId = formData.get("projectId") as string | null;- const data = {} as { domain?: string; errors?: string };- if (projectId === null || domain === null) {- data.errors = "Domain and projectId required";- return data;- }try {- const project = await db.project.loadOne(projectId);- if (project === null) throw new Error(`Project ${projectId} not found`);- const tree = await db.tree.clone(project.devTreeId);- const { prodTreeIdHistory } = project;- if (project.prodTreeId) {- prodTreeIdHistory.push(project.prodTreeId);- }- const updatedProject = await db.project.update({- id: projectId,- domain,- prodTreeId: tree.id,- prodTreeIdHistory,- });-- data.domain = updatedProject.domain;+ const project = await db.misc.publish({ projectId, domain });+ return { domain: project.domain };} catch (error) {if (error instanceof Error) {- data.errors = error.message;+ return {+ errors: error.message,+ };}}- return data;+ return { errors: "Unexpected error" };};diff --git a/app/routes/rest/tree.$projectId.ts b/app/routes/rest/tree.$projectId.tsindex 7c0847d49d..fc8d900b77 100644--- a/app/routes/rest/tree.$projectId.ts+++ b/app/routes/rest/tree.$projectId.ts@@ -1,5 +1,5 @@import { type LoaderFunction } from "remix";-import type { Tree } from "~/shared/db";+import type { Tree } from "@webstudio-is/sdk";import * as db from "~/shared/db";export type ErrorData = {@@ -9,20 +9,9 @@ export type ErrorData = {export const loader: LoaderFunction = async ({params,}): Promise<Tree | ErrorData> => {- if (params.projectId === undefined) throw new Error("Project ID undefined");try {- const project = await db.project.loadOne(params.projectId);- if (project === null) {- return {- errors: `Project "${params.projectId}" doesn't exist`,- };- }- if (project.prodTreeId === null) {- return {- errors: "Site needs to be published, production tree ID is null.",- };- }- return await db.tree.load(project.prodTreeId);+ const project = await db.project.loadById(params.projectId);+ return await db.tree.loadByProject(project, "production");} catch (error) {if (error instanceof Error) {return {@@ -30,5 +19,5 @@ export const loader: LoaderFunction = async ({};}}- return { errors: "This is an impossible state." };+ return { errors: "Unexpected error" };};diff --git a/app/shared/component/instance-data.ts b/app/shared/component/instance-data.tsindex f67a78e360..a91963b2dc 100644--- a/app/shared/component/instance-data.ts+++ b/app/shared/component/instance-data.ts@@ -1,13 +1,18 @@-import type { InstanceProps } from "~/shared/db";-import { type Instance } from "@webstudio-is/sdk";-import type { Style, StyleProperty, StyleValue } from "@webstudio-is/sdk";+import {+ type Instance,+ type InstanceProps,+ type Style,+ type StyleProperty,+ type StyleValue,+ type Tree,+} from "@webstudio-is/sdk";export type SelectedInstanceData = {id: Instance["id"];component: Instance["component"];style: Style;browserStyle: Style;- props: InstanceProps["props"];+ props: InstanceProps;};type StyleUpdate = {diff --git a/app/shared/db/index.ts b/app/shared/db/index.tsindex 961fa41c49..7e14ac8a8f 100644--- a/app/shared/db/index.ts+++ b/app/shared/db/index.ts@@ -1,5 +1,4 @@export * as tree from "./tree.server";export * as project from "./project.server";export * as props from "./props.server";-export type { Tree } from "./tree.server";-export type { Project, InstanceProps } from "./prisma.server";+export * as misc from "./misc.server";diff --git a/app/shared/db/misc.server.ts b/app/shared/db/misc.server.tsnew file mode 100644index 0000000000..180421a31e--- /dev/null+++ b/app/shared/db/misc.server.ts@@ -0,0 +1,43 @@+import * as db from "./";++/**+ * Conceptually publishing is cloning all data that affects user site+ * and referencing it under a new tree id.+ */+export const publish = async ({+ projectId,+ domain,+}: {+ projectId: string | null;+ domain: string | null;+}) => {+ if (projectId === null) {+ throw new Error("Project ID required");+ }+ if (domain === null) {+ throw new Error("Domain required");+ }++ const project = await db.project.loadById(projectId);+ if (project === null) {+ throw new Error(`Project "${projectId}" not found`);+ }++ const tree = await db.tree.clone(project.devTreeId);+ await db.props.publish({+ previousTreeId: project.devTreeId,+ nextTreeId: tree.id,+ });++ const { prodTreeIdHistory } = project;+ if (project.prodTreeId) {+ prodTreeIdHistory.push(project.prodTreeId);+ }+ const updatedProject = await db.project.update({+ id: projectId,+ domain,+ prodTreeId: tree.id,+ prodTreeIdHistory,+ });+ return updatedProject;+};diff --git a/app/shared/db/prisma.server.ts b/app/shared/db/prisma.server.tsindex cbd64efc9a..dfa0e67576 100644--- a/app/shared/db/prisma.server.ts+++ b/app/shared/db/prisma.server.ts@@ -1,12 +1,11 @@import {PrismaClient,Prisma,- type InstanceProps,- type Project,-} from "@prisma/client";+ PrismaClientKnownRequestError,+} from "@webstudio-is/sdk/lib/prisma.server";export const prisma = new PrismaClient();-export { Project, Prisma, InstanceProps };+export { Prisma, PrismaClientKnownRequestError };const main = async () => {await prisma.$connect();diff --git a/app/shared/db/project.server.ts b/app/shared/db/project.server.tsindex b559bb00a3..053d1bb5b6 100644--- a/app/shared/db/project.server.ts+++ b/app/shared/db/project.server.ts@@ -1,11 +1,16 @@import slugify from "slugify";import { nanoid } from "nanoid";-import { prisma, Prisma, type Project } from "./prisma.server";+import type { Project } from "@webstudio-is/sdk";+import { prisma, Prisma } from "./prisma.server";import * as db from ".";-export const loadOne = async (id: string): Promise<Project | null> => {+export const loadById = async (projectId?: string): Promise<Project | null> => {+ if (typeof projectId !== "string") {+ throw new Error("Project ID required");+ }+return await prisma.project.findUnique({- where: { id },+ where: { id: projectId },});};diff --git a/app/shared/db/props.server.ts b/app/shared/db/props.server.tsindex dc0c7ecd6a..fd41a5b680 100644--- a/app/shared/db/props.server.ts+++ b/app/shared/db/props.server.ts@@ -2,32 +2,52 @@ import {type Instance,type UserPropsUpdates,type UserProp,+ type Project,+ type InstanceProps,+ type Tree,} from "@webstudio-is/sdk";-import { prisma } from "./prisma.server";-import { type Tree } from "./tree.server";-import { PrismaClientKnownRequestError } from "@prisma/client/runtime";+import { prisma, PrismaClientKnownRequestError } from "./prisma.server";++export const loadByProject = async (+ project: Project | null,+ env: "production" | "development" = "development"+) => {+ if (project === null) {+ throw new Error("Project required");+ }++ const treeId = env === "production" ? project.prodTreeId : project.devTreeId;++ if (treeId === null) {+ throw new Error("Site needs to be published, production tree ID is null.");+ }-export const loadForTree = async (treeId: Tree["id"]) => {return await prisma.instanceProps.findMany({where: { treeId },});};-const loadForInstance = async (id: Instance["id"]) => {+const loadById = async (id: InstanceProps["id"]) => {return await prisma.instanceProps.findUnique({where: { id },});};-export const update = async (- instanceId: Instance["id"],- treeId: Tree["id"],- updates: UserPropsUpdates["updates"]-) => {+export const update = async ({+ propsId,+ instanceId,+ treeId,+ updates,+}: {+ propsId: InstanceProps["id"];+ instanceId: Instance["id"];+ treeId: Tree["id"];+ updates: UserPropsUpdates["updates"];+}) => {// @todo update in one command, remove queueing logic on the ui// as of prisma client v3.10.0 updating composite types like in this doc didn't work// https://www.prisma.io/docs/concepts/components/prisma-client/composite-types- const props = (await loadForInstance(instanceId))?.props ?? [];+ const props = (await loadById(propsId))?.props ?? [];for (const update of updates) {const index = props.findIndex((prop) => update.id == prop.id);@@ -35,9 +55,18 @@ export const update = async (else props.splice(index, 1, update);}+ console.log({ props, updates });++ //console.log({+ // where: { id: propsId },+ // create: { instanceId, treeId, props },+ // update: {+ // props,+ // },+ //});await prisma.instanceProps.upsert({- where: { id: instanceId },- create: { id: instanceId, treeId, props },+ where: { id: propsId },+ create: { instanceId, treeId, props },update: {props,},@@ -45,24 +74,24 @@ export const update = async (};export const deleteOne = async (- instanceId: Instance["id"],+ id: InstanceProps["id"],propId: UserProp["id"]) => {// @todo update in one command, remove queueing logic on the ui// as of prisma client v3.10.0 updating composite types like in this doc didn't work// https://www.prisma.io/docs/concepts/components/prisma-client/composite-types- const props = (await loadForInstance(instanceId))?.props ?? [];+ const props = (await loadById(id))?.props ?? [];const index = props.findIndex((prop) => prop.id == propId);if (index === -1) return;props.splice(index, 1);await prisma.instanceProps.update({- where: { id: instanceId },+ where: { id },data: { props },});};-export const deleteProps = async (id: Instance["id"]) => {+export const deleteProps = async (id: InstanceProps["id"]) => {try {await prisma.instanceProps.delete({ where: { id } });} catch (error: unknown) {@@ -77,3 +106,22 @@ export const deleteProps = async (id: Instance["id"]) => {throw error;}};++export const publish = async ({+ previousTreeId,+ nextTreeId,+}: {+ previousTreeId: string;+ nextTreeId: string;+}) => {+ const props = await prisma.instanceProps.findMany({+ where: { treeId: previousTreeId },+ });+ const data = props.map(({ id, treeId, ...rest }) => ({+ ...rest,+ treeId: nextTreeId,+ }));+ await prisma.instanceProps.createMany({+ data,+ });+};diff --git a/app/shared/db/tree.server.ts b/app/shared/db/tree.server.tsindex ab7b0c0258..e049c01958 100644--- a/app/shared/db/tree.server.ts+++ b/app/shared/db/tree.server.ts@@ -1,4 +1,9 @@-import { type ChildrenUpdates, type Instance } from "@webstudio-is/sdk";+import {+ type ChildrenUpdates,+ type Instance,+ type Project,+ type Tree,+} from "@webstudio-is/sdk";import { prisma } from "./prisma.server";import {insertInstance,@@ -13,11 +18,6 @@ import {import type { StyleUpdates } from "~/shared/component";import { deleteProps } from "./props.server";-export type Tree = {- id: string;- root: Instance;-};-const rootConfig = {// @todo this should be part of a root primitive in primitivescomponent: "Box",@@ -58,13 +58,13 @@ export const create = async (root?: Instance): Promise<Tree> => {})) as Tree;};-export const load = async (id: string): Promise<Tree> => {+export const loadById = async (id: string): Promise<Tree> => {return (await prisma.tree.findUnique({where: { id },})) as Tree;};-export const loadForDomain = async (domain: string): Promise<Tree> => {+export const loadByDomain = async (domain: string): Promise<Tree> => {const project = await prisma.project.findUnique({where: { domain },});@@ -74,7 +74,24 @@ export const loadForDomain = async (domain: string): Promise<Tree> => {if (project.prodTreeId === null) {throw new Error(`Site is not published`);}- return await load(project.prodTreeId);+ return await loadById(project.prodTreeId);+};++export const loadByProject = async (+ project: Project | null,+ env: "production" | "development" = "development"+) => {+ if (project === null) {+ throw new Error("Project required");+ }++ const treeId = env === "production" ? project.prodTreeId : project.devTreeId;++ if (treeId === null) {+ throw new Error("Site needs to be published, production tree ID is null.");+ }++ return await loadById(treeId);};export const insert = async (@@ -82,7 +99,7 @@ export const insert = async (instanceInsertionSpec: InstanceInsertionSpec) => {// @todo insert without loading the entire tree- const tree = await load(id);+ const tree = await loadById(id);const { instance: root } = insertInstance(instanceInsertionSpec, tree.root, {populate: false,});@@ -97,7 +114,7 @@ export const reparent = async (instanceReparentingSpec: InstanceReparentingSpec) => {// @todo reparent without loading the entire tree- const tree = await load(id);+ const tree = await loadById(id);const root = reparentInstance(tree.root, instanceReparentingSpec);await prisma.tree.update({data: { root },@@ -107,7 +124,7 @@ export const reparent = async (export const updateStyles = async (id: string, styleUpdates: StyleUpdates) => {// @todo we need to update without fetching the tree- const tree = await load(id);+ const tree = await loadById(id);const root = setInstanceStyle(tree.root,styleUpdates.id,@@ -124,7 +141,7 @@ export const updateChildren = async (change: { instanceId: Instance["id"]; updates: ChildrenUpdates }) => {// @todo we need to update without fetching the tree- const tree = await load(id);+ const tree = await loadById(id);const root = setInstanceChildren(change.instanceId,change.updates,@@ -141,7 +158,7 @@ export const deleteInstance = async (instanceId: Instance["id"]) => {// @todo we need to delete without fetching the tree- const tree = await load(id);+ const tree = await loadById(id);const root = deleteInstanceFromTree(tree.root, instanceId);if (root === null) return;await prisma.tree.update({@@ -152,6 +169,6 @@ export const deleteInstance = async (};export const clone = async (id: string) => {- const tree = await load(id);+ const tree = await loadById(id);return await create(tree.root);};diff --git a/package.json b/package.jsonindex d52abfd507..e00580d3e2 100644--- a/package.json+++ b/package.json@@ -6,14 +6,12 @@"license": "MIT","scripts": {"build": "yarn build:prisma && remix build",- "build:prisma": "prisma generate && prisma db push","dev": "node -r dotenv/config node_modules/.bin/remix dev",- "postinstall": "remix setup node && yarn build:prisma",+ "postinstall": "remix setup node","typecheck": "tsc -b","test": "jest"},"dependencies": {- "@prisma/client": "^3.10.0","@radix-ui/colors": "^0.1.8","@radix-ui/react-accessible-icon": "^0.1.3","@radix-ui/react-accordion": "^0.1.5",@@ -91,7 +89,6 @@"esbuild-node-externals": "^1.4.1","esbuild-node-loader": "^0.8.0","jest": "^27.5.1",- "prisma": "^3.10.0","typescript": "^4.1.2"},"engines": {diff --git a/prisma/schema.prisma b/prisma/schema.prismadeleted file mode 100644index da3538eb6f..0000000000--- a/prisma/schema.prisma+++ /dev/null@@ -1,39 +0,0 @@-// This is your Prisma schema file,-// learn more about it in the docs: https://pris.ly/d/prisma-schema--generator client {- provider = "prisma-client-js"- previewFeatures = ["mongoDb"]-}--datasource db {- provider = "mongodb"- url = env("DATABASE_URL")-}--model Project {- id String @id @default(auto()) @map("_id") @db.ObjectId- title String- domain String @unique- prodTreeId String? @db.ObjectId // exists when published- devTreeId String @db.ObjectId- prodTreeIdHistory String[] @db.ObjectId-}--model Tree {- id String @id @default(auto()) @map("_id") @db.ObjectId- root Json-}--model InstanceProps {- id String @id @default(auto()) @map("_id") @db.ObjectId- treeId String @db.ObjectId- props Props[]-}--type Props {- id String @db.ObjectId- prop String- value String-}-diff --git a/yarn.lock b/yarn.lockindex 83bc32f198..baaf67d374 100644--- a/yarn.lock+++ b/yarn.lock@@ -1667,10 +1667,20 @@dependencies:web-streams-polyfill "^3.1.1"+"@webstudio-is/sdk@/Users/isonen/work/webstudio/webstudio-sdk":+ version "0.1.0"+ dependencies:+ "@prisma/client" "^3.10.0"+ "@stitches/react" "^1.2.7"+ prisma "^3.10.0"+ react-nano-state "^0.4.0"+"@webstudio-is/sdk@file:../webstudio-sdk":version "0.1.0"dependencies:+ "@prisma/client" "^3.10.0""@stitches/react" "^1.2.7"+ prisma "^3.10.0"react-nano-state "^0.4.0""@xobotyi/scrollbar-width@^1.9.5":