Skip to content

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.

save breakpoint change to db

Oleg Isonen2403d8dd8e45
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsx
index 02dc15df18..48447313e1 100644
--- a/app/canvas/canvas.tsx
+++ b/app/canvas/canvas.tsx
@@ -25,13 +25,10 @@ import {
useInsertInstance,
useDeleteInstance,
useReparentInstance,
-} from "./shared/tree";
-import { useUpdateStyle } from "./shared/style";
-import {
usePublishSelectedInstance,
usePublishRootInstance,
- usePublishBreakpoints,
-} from "./shared/publish";
+} from "./shared/instance";
+import { useUpdateStyle } from "./shared/style";
import { useActiveElementTracking } from "./shared/active-element";
import { WrapperComponentDev } from "./features/wrapper-component";
import {
@@ -41,6 +38,10 @@ import {
} from "./shared/nano-values";
import { useSync } from "./shared/sync";
import { useManageProps } from "./shared/props";
+import {
+ useBreakpointChange,
+ usePublishBreakpoints,
+} from "./shared/breakpoints";
const useElementsTree = () => {
const [rootInstance] = useRootInstance();
@@ -107,6 +108,7 @@ const DesignMode = ({ treeId, project }: DesignModeProps) => {
usePublishRootInstance();
useActiveElementTracking();
useSync({ project });
+ useBreakpointChange();
const elements = useElementsTree();
return (
// Using touch backend becuase html5 drag&drop doesn't fire drag events in our case
diff --git a/app/canvas/shared/breakpoints.ts b/app/canvas/shared/breakpoints.ts
new file mode 100644
index 0000000000..123fea4d0c
--- /dev/null
+++ b/app/canvas/shared/breakpoints.ts
@@ -0,0 +1,34 @@
+import { useEffect } from "react";
+import store from "immerhin";
+import { type Breakpoint, useSubscribe } from "@webstudio-is/sdk";
+import { useBreakpoints, breakpointsContainer } from "./nano-values";
+import { publish } from "./pubsub";
+
+store.register("breakpoints", breakpointsContainer);
+
+export const usePublishBreakpoints = () => {
+ const [breakpoints] = useBreakpoints();
+ useEffect(() => {
+ publish<"loadBreakpoints", Array<Breakpoint>>({
+ type: "loadBreakpoints",
+ payload: breakpoints,
+ });
+ }, [breakpoints]);
+};
+
+export const useBreakpointChange = () => {
+ useSubscribe<"breakpointChange", Breakpoint>(
+ "breakpointChange",
+ (breakpoint) => {
+ store.createTransaction([breakpointsContainer], (breakpoints) => {
+ const foundBreakpoint = breakpoints.find(
+ ({ id }) => id == breakpoint.id
+ );
+ if (foundBreakpoint) {
+ foundBreakpoint.label = breakpoint.label;
+ foundBreakpoint.minWidth = breakpoint.minWidth;
+ }
+ });
+ }
+ );
+};
diff --git a/app/canvas/shared/tree.ts b/app/canvas/shared/instance.ts
similarity index 72%
rename from app/canvas/shared/tree.ts
rename to app/canvas/shared/instance.ts
index 93a970de30..29ffc2b652 100644
--- a/app/canvas/shared/tree.ts
+++ b/app/canvas/shared/instance.ts
@@ -1,9 +1,12 @@
import { useEffect } from "react";
+import ObjectId from "bson-objectid";
import {
type InstanceProps,
type Instance,
type Tree,
allUserPropsContainer,
+ getBrowserStyle,
+ useAllUserProps,
} from "@webstudio-is/sdk";
import {
deleteInstanceMutable,
@@ -13,13 +16,14 @@ import {
insertInstanceMutable,
} from "~/shared/tree-utils";
import store from "immerhin";
-import { DropData } from "~/shared/component";
+import { DropData, type SelectedInstanceData } from "~/shared/component";
import {
rootInstanceContainer,
useRootInstance,
useSelectedInstance,
+ useSelectedElement,
} from "./nano-values";
-import { useSubscribe } from "./pubsub";
+import { useSubscribe, publish } from "./pubsub";
export const usePopulateRootInstance = (tree: Tree) => {
const [, setRootInstance] = useRootInstance();
@@ -109,3 +113,50 @@ export const useDeleteInstance = () => {
}
);
};
+
+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[instance.id];
+ if (props === undefined) {
+ props = {
+ id: ObjectId().toString(),
+ instanceId: instance.id,
+ treeId,
+ props: [],
+ };
+ }
+ payload = {
+ id: instance.id,
+ component: instance.component,
+ cssRules: instance.cssRules,
+ browserStyle: getBrowserStyle(selectedElement),
+ props,
+ };
+ }
+
+ publish<"selectInstance", SelectedInstanceData>({
+ type: "selectInstance",
+ payload,
+ });
+ }, [instance, selectedElement, allUserProps, treeId]);
+};
+
+export const usePublishRootInstance = () => {
+ const [rootInstance] = useRootInstance();
+ useEffect(() => {
+ publish<"loadRootInstance", Instance>({
+ type: "loadRootInstance",
+ payload: rootInstance,
+ });
+ }, [rootInstance]);
+};
diff --git a/app/canvas/shared/publish.ts b/app/canvas/shared/publish.ts
deleted file mode 100644
index f62c647476..0000000000
--- a/app/canvas/shared/publish.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { useEffect } from "react";
-import ObjectId from "bson-objectid";
-import {
- getBrowserStyle,
- useAllUserProps,
- type Instance,
- type Tree,
- type Breakpoint,
-} from "@webstudio-is/sdk";
-import { type SelectedInstanceData } from "~/shared/component";
-import {
- useSelectedInstance,
- useSelectedElement,
- useRootInstance,
- useBreakpoints,
-} from "./nano-values";
-import { publish } from "./pubsub";
-
-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[instance.id];
- if (props === undefined) {
- props = {
- id: ObjectId().toString(),
- instanceId: instance.id,
- treeId,
- props: [],
- };
- }
- payload = {
- id: instance.id,
- component: instance.component,
- cssRules: instance.cssRules,
- browserStyle: getBrowserStyle(selectedElement),
- props,
- };
- }
-
- publish<"selectInstance", SelectedInstanceData>({
- type: "selectInstance",
- payload,
- });
- }, [instance, selectedElement, allUserProps, treeId]);
-};
-
-export const usePublishRootInstance = () => {
- const [rootInstance] = useRootInstance();
- useEffect(() => {
- publish<"loadRootInstance", Instance>({
- type: "loadRootInstance",
- payload: rootInstance,
- });
- }, [rootInstance]);
-};
-
-export const usePublishBreakpoints = () => {
- const [breakpoints] = useBreakpoints();
- useEffect(() => {
- publish<"loadBreakpoints", Array<Breakpoint>>({
- type: "loadBreakpoints",
- payload: breakpoints,
- });
- }, [breakpoints]);
-};
diff --git a/app/designer/features/topbar/breakpoints.tsx b/app/designer/features/topbar/breakpoints.tsx
index 57b121eb1e..60f131d09c 100644
--- a/app/designer/features/topbar/breakpoints.tsx
+++ b/app/designer/features/topbar/breakpoints.tsx
@@ -69,7 +69,7 @@ const EditableBreakpoint = ({
);
};
-const Hint = ({ breakpoint }: any) => {
+const Hint = ({ breakpoint }: { breakpoint: Breakpoint }) => {
return (
<Box css={{ px: "$3" }}>
<Paragraph css={{ fontSize: "$1" }}>CSS Preview:</Paragraph>
@@ -120,8 +120,9 @@ export const Breakpoints = ({ publish }: BreakpointsProps) => {
onFocus={() => {
setBreakpointHint(breakpoint);
}}
- onChange={(_breakpoint) => {
- // @todo
+ onChange={(breakpoint) => {
+ setBreakpointHint(breakpoint);
+ publish({ type: "breakpointChange", payload: breakpoint });
}}
/>
);
diff --git a/app/routes/rest/patch.ts b/app/routes/rest/patch.ts
index dba1f8f022..6e1e3ab6c4 100644
--- a/app/routes/rest/patch.ts
+++ b/app/routes/rest/patch.ts
@@ -6,6 +6,7 @@ import { type Project, type Tree } from "@webstudio-is/sdk";
const updaters = {
root: db.tree.patchRoot,
props: db.props.patch,
+ breakpoints: db.breakpoints.patch,
};
type UpdaterKey = keyof typeof updaters;
diff --git a/app/shared/db/breakpoints.server.ts b/app/shared/db/breakpoints.server.ts
index d40bea34b7..8340e651d9 100644
--- a/app/shared/db/breakpoints.server.ts
+++ b/app/shared/db/breakpoints.server.ts
@@ -1,8 +1,11 @@
import {
initialBreakpoints,
- type Project,
defaultBreakpoint,
+ type Project,
+ type Tree,
+ type Breakpoint,
} from "@webstudio-is/sdk";
+import { applyPatches, type Patch } from "immer";
import { prisma } from "./prisma.server";
export const create = async (projectId: Project["id"]) => {
@@ -13,6 +16,7 @@ export const create = async (projectId: Project["id"]) => {
projectId,
}));
// https://github.com/prisma/prisma/issues/8131
+ // @todo run this in parallel
const breakpoints = await prisma.$transaction(
data.map((breakpoint) => prisma.breakpoint.create({ data: breakpoint }))
);
@@ -29,3 +33,25 @@ export const load = async (projectId: Project["id"]) => {
breakpoints.push(defaultBreakpoint);
return breakpoints;
};
+
+export const patch = async (
+ { projectId }: { treeId: Tree["id"]; projectId: Project["id"] },
+ patches: Array<Patch>
+) => {
+ const breakpoints = await await prisma.breakpoint.findMany({
+ where: { projectId },
+ });
+
+ const nextBreakpoints = applyPatches<Array<Breakpoint>>(breakpoints, patches);
+
+ await Promise.all(
+ Object.values(nextBreakpoints).map(async (breakpoint) => {
+ const { id, ...rest } = breakpoint;
+ await prisma.breakpoint.upsert({
+ where: { id },
+ create: breakpoint,
+ update: rest,
+ });
+ })
+ );
+};