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
delete breakpoint
Oleg Isonena08cc5e185cf
diff --git a/app/canvas/shared/breakpoints.ts b/app/canvas/shared/breakpoints.tsindex cf972e2424..f2cccf6422 100644--- a/app/canvas/shared/breakpoints.ts+++ b/app/canvas/shared/breakpoints.ts@@ -48,7 +48,22 @@ const useBreakpointChange = () => {);};+const useBreakpointDelete = () => {+ useSubscribe<"breakpointDelete", Breakpoint>(+ "breakpointDelete",+ (breakpoint) => {+ store.createTransaction([breakpointsContainer], (breakpoints) => {+ const index = breakpoints.findIndex(({ id }) => id == breakpoint.id);+ if (index !== -1) {+ breakpoints.splice(index, 1);+ }+ });+ }+ );+};+export const useHandleBreakpoints = () => {usePublishBreakpoints();useBreakpointChange();+ useBreakpointDelete();};diff --git a/app/designer/designer.tsx b/app/designer/designer.tsxindex 1b96c73143..c3e9f7f887 100644--- a/app/designer/designer.tsx+++ b/app/designer/designer.tsx@@ -1,7 +1,7 @@import { useEffect, useState } from "react";import { DndProvider } from "react-dnd";import { HTML5Backend } from "react-dnd-html5-backend";-import type { Breakpoint, Project } from "@webstudio-is/sdk";+import { Breakpoint, Project, sort } from "@webstudio-is/sdk";import type { Config } from "~/config";import type { SelectedInstanceData } from "~/shared/component";import { Box, Flex, Grid, type CSS } from "~/shared/design-system";@@ -50,15 +50,15 @@ const useSubscribeSyncStatus = () => {};const useSubscribeBreakpoints = () => {- const [breakpoints, setValue] = useBreakpoints();+ const [breakpoints, setBreakpoints] = useBreakpoints();const [selectedBreakpoint, setSelectedBreakpoint] = useSelectedBreakpoint();useSubscribe<"loadBreakpoints", Array<Breakpoint>>("loadBreakpoints",- setValue+ setBreakpoints);useEffect(() => {- if (selectedBreakpoint === undefined) {- setSelectedBreakpoint(breakpoints[breakpoints.length - 1]);+ if (selectedBreakpoint === undefined && breakpoints.length !== 0) {+ setSelectedBreakpoint(sort(breakpoints)[breakpoints.length - 1]);}}, [breakpoints, selectedBreakpoint, setSelectedBreakpoint]);};diff --git a/app/designer/features/topbar/breakpoints/breakpoints-editor.tsx b/app/designer/features/topbar/breakpoints/breakpoints-editor.tsxindex c5b4f1ad91..cc2e3571b9 100644--- a/app/designer/features/topbar/breakpoints/breakpoints-editor.tsx+++ b/app/designer/features/topbar/breakpoints/breakpoints-editor.tsx@@ -1,6 +1,6 @@-import { useState } from "react";+import { useEffect, useState } from "react";import useDebounce from "react-use/lib/useDebounce";-import { type Project, type Breakpoint } from "@webstudio-is/sdk";+import { type Breakpoint, sort } from "@webstudio-is/sdk";import { Button, TextField, Flex, Text } from "~/shared/design-system";import { PlusIcon, TrashIcon } from "~/shared/icons";import { type Publish } from "~/designer/shared/canvas-iframe";@@ -27,7 +27,7 @@ const BreakpointEditorItem = ({onChange(breakpoint);}},- 1000,+ 500,[breakpoint]);@@ -38,7 +38,11 @@ const BreakpointEditorItem = ({}}onChange={(event) => {event.stopPropagation();- const data = new FormData(event.currentTarget);+ const form = event.currentTarget;+ if (form.reportValidity() === false) {+ return;+ }+ const data = new FormData(form);const nextBreakpoint: Breakpoint = {...breakpoint,// @todo if label changed, we want to generate a ref name@@ -51,22 +55,26 @@ const BreakpointEditorItem = ({><Flex gap="1" css={{ paddingLeft: "$4", paddingRight: "$3" }}><TextField+ css={{ width: 100, flexGrow: 1 }}+ type="text"variant="ghost"defaultValue={breakpoint.label}placeholder="Breakpoint name"- css={{ width: 100, flexGrow: 1 }}name="label"minLength={2}+ required/><TextField+ css={{ textAlign: "right", width: 50 }}variant="ghost"defaultValue={breakpoint.minWidth}type="number"name="minWidth"min={0}- css={{ textAlign: "right", width: 50 }}+ required/><Button+ type="button"ghostonClick={() => {onDelete(breakpoint);@@ -80,7 +88,6 @@ const BreakpointEditorItem = ({};type BreakpointsEditorProps = {- project: Project;breakpoints: Array<Breakpoint>;onSelect: (breakpoint: Breakpoint) => void;publish: Publish;@@ -88,11 +95,15 @@ type BreakpointsEditorProps = {export const BreakpointsEditor = ({breakpoints: initialBreakpoints,- project,onSelect,publish,}: BreakpointsEditorProps) => {- const [breakpoints, setBreakpoints] = useState(initialBreakpoints);+ const [breakpoints, setBreakpoints] = useState(sort(initialBreakpoints));++ useEffect(() => {+ setBreakpoints(initialBreakpoints);+ }, [initialBreakpoints]);+return (<Flex gap="2" direction="column"><Flex@@ -109,7 +120,6 @@ export const BreakpointsEditor = ({...breakpoints,{id: ObjectId().toString(),- projectId: project.id,label: "",minWidth: 0,},@@ -119,10 +129,10 @@ export const BreakpointsEditor = ({<PlusIcon /></Button></Flex>- {breakpoints.map((breakpoint, index) => {+ {breakpoints.map((breakpoint) => {return (<BreakpointEditorItem- key={index}+ key={breakpoint.id}breakpoint={breakpoint}onFocus={() => {onSelect(breakpoint);@@ -132,7 +142,10 @@ export const BreakpointsEditor = ({publish({ type: "breakpointChange", payload: breakpoint });}}onDelete={(breakpoint) => {- onSelect(breakpoints[0]);+ const nextBreakpoints = [...breakpoints];+ const index = breakpoints.indexOf(breakpoint);+ nextBreakpoints.splice(index, 1);+ setBreakpoints(nextBreakpoints);publish({ type: "breakpointDelete", payload: breakpoint });}}/>diff --git a/app/designer/features/topbar/breakpoints/breakpoints.tsx b/app/designer/features/topbar/breakpoints/breakpoints.tsxindex 425172397f..1fbae901fc 100644--- a/app/designer/features/topbar/breakpoints/breakpoints.tsx+++ b/app/designer/features/topbar/breakpoints/breakpoints.tsx@@ -1,5 +1,5 @@import { useState } from "react";-import { type Project, type Breakpoint, sort } from "@webstudio-is/sdk";+import { type Breakpoint, sort } from "@webstudio-is/sdk";import {DropdownMenu,DropdownMenuContent,@@ -52,10 +52,9 @@ const menuItemCss = {type BreakpointsProps = {publish: Publish;- project: Project;};-export const Breakpoints = ({ publish, project }: BreakpointsProps) => {+export const Breakpoints = ({ publish }: BreakpointsProps) => {const [breakpoints, setBreakpoints] = useBreakpoints();const [selectedBreakpoint, setSelectedBreakpoint] = useSelectedBreakpoint();const [isEditing, setIsEditing] = useState(false);@@ -81,13 +80,12 @@ export const Breakpoints = ({ publish, project }: BreakpointsProps) => {breakpoints={breakpoints}onSelect={setBreakpointHint}publish={publish}- project={project}/>) : (- breakpoints.map((breakpoint, index) => {+ sort(breakpoints).map((breakpoint) => {return (<DropdownMenuItem- key={index}+ key={breakpoint.id}css={menuItemCss}onMouseOver={() => {setBreakpointHint(breakpoint);@@ -116,7 +114,7 @@ export const Breakpoints = ({ publish, project }: BreakpointsProps) => {onSelect={(event) => {event.preventDefault();setIsEditing(!isEditing);- setBreakpoints(sort(breakpoints));+ setBreakpoints(breakpoints);}}>{isEditing ? "Done" : "Edit"}diff --git a/app/designer/features/topbar/topbar.tsx b/app/designer/features/topbar/topbar.tsxindex 411b554f6e..81b0bfab5c 100644--- a/app/designer/features/topbar/topbar.tsx+++ b/app/designer/features/topbar/topbar.tsx@@ -30,7 +30,7 @@ export const Topbar = ({ config, css, project, publish }: TopbarProps) => {}}><Menu config={config} publish={publish} />- <Breakpoints publish={publish} project={project} />+ <Breakpoints publish={publish} /><Flex gap="1" align="center"><SyncStatus /><Preview publish={publish} />diff --git a/app/routes/index.tsx b/app/routes/index.tsxindex 0c315c3f8b..585a791bb9 100644--- a/app/routes/index.tsx+++ b/app/routes/index.tsx@@ -35,7 +35,7 @@ export const loader: LoaderFunction = async ({db.props.loadByTreeId(project.prodTreeId),db.breakpoints.load(project.id),]);- return { tree, props, breakpoints };+ return { tree, props, breakpoints: breakpoints?.values ?? [] };} catch (error) {if (error instanceof Error) {return { errors: error.message };diff --git a/app/shared/db/breakpoints.server.ts b/app/shared/db/breakpoints.server.tsindex 36b8642dea..3d46c4e8b0 100644--- a/app/shared/db/breakpoints.server.ts+++ b/app/shared/db/breakpoints.server.ts@@ -1,30 +1,24 @@-import {- initialBreakpoints,- sort,- type Project,- type Tree,-} from "@webstudio-is/sdk";+import { initialBreakpoints, type Project, type Tree } from "@webstudio-is/sdk";+import ObjectId from "bson-objectid";import { applyPatches, type Patch } from "immer";import { prisma } from "./prisma.server";-export const create = async (projectId: Project["id"]) => {- const data = initialBreakpoints.map((breakpoint) => ({- ...breakpoint,- 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 }))- );- return breakpoints;-};-export const load = async (projectId: Project["id"]) => {- const breakpoints = await prisma.breakpoint.findMany({+ return await prisma.breakpoints.findUnique({where: { projectId },});- return sort(breakpoints);+};++export const create = async (projectId: Project["id"]) => {+ const data = {+ projectId,+ values: initialBreakpoints.map((breakpoint) => ({+ ...breakpoint,+ id: ObjectId().toString(),+ })),+ };+ await prisma.breakpoints.create({ data });+ return data;};export const patch = async (@@ -32,16 +26,10 @@ export const patch = async (patches: Array<Patch>) => {const breakpoints = await load(projectId);- const nextBreakpoints = applyPatches(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,- });- })- );+ if (breakpoints === null) return;+ const nextBreakpoints = applyPatches(breakpoints.values, patches);+ await prisma.breakpoints.update({+ where: { projectId: projectId },+ data: { values: nextBreakpoints },+ });};diff --git a/app/shared/db/canvas.server.ts b/app/shared/db/canvas.server.tsindex 2fabd432ca..e5d9108b86 100644--- a/app/shared/db/canvas.server.ts+++ b/app/shared/db/canvas.server.ts@@ -17,11 +17,9 @@ const loadData = async (projectId: Project["id"]) => {const [tree, props, breakpoints] = await Promise.all([db.tree.loadByProject(project, "development"),db.props.loadByProject(project, "development"),- existingBreakpoints.length > 1- ? existingBreakpoints- : db.breakpoints.create(projectId),+ existingBreakpoints || db.breakpoints.create(projectId),]);- return { tree, props, project, breakpoints };+ return { tree, props, project, breakpoints: breakpoints?.values || [] };};export const loadCanvasData = async ({diff --git a/app/shared/db/props.server.ts b/app/shared/db/props.server.tsindex 4530cc141f..702737a240 100644--- a/app/shared/db/props.server.ts+++ b/app/shared/db/props.server.ts@@ -1,13 +1,6 @@-import {- type Instance,- type UserPropsUpdates,- type Project,- type InstanceProps,- type Tree,- type AllUserProps,-} from "@webstudio-is/sdk";+import { type Project, type Tree, type AllUserProps } from "@webstudio-is/sdk";import { applyPatches, type Patch } from "immer";-import { prisma, PrismaClientKnownRequestError } from "./prisma.server";+import { prisma } from "./prisma.server";export const loadByProject = async (project: Project | null,@@ -32,66 +25,6 @@ export const loadByTreeId = async (treeId: Tree["id"]) => {});};-const loadById = async (id: InstanceProps["id"]) => {- return await prisma.instanceProps.findUnique({- where: { id },- });-};--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 loadById(propsId))?.props ?? [];-- for (const update of updates) {- const index = props.findIndex((prop) => update.id == prop.id);- if (index === -1) props.push(update);- else props.splice(index, 1, update);- }-- await prisma.instanceProps.upsert({- where: { id: propsId },- create: { id: propsId, instanceId, treeId, props },- update: {- props,- },- });-};--export const deleteProps = async ({- instanceId,- treeId,-}: {- instanceId: InstanceProps["id"];- treeId: Tree["id"];-}) => {- try {- await prisma.instanceProps.deleteMany({ where: { instanceId, treeId } });- } catch (error: unknown) {- if (- error instanceof PrismaClientKnownRequestError &&- // Record to delete does not exist.- // @todo should this be treated?- error.code === "P2025"- ) {- return;- }-- throw error;- }-};-export const clone = async ({previousTreeId,nextTreeId,@@ -131,15 +64,14 @@ export const patch = async ();await Promise.all(- Object.values(nextProps).map(- async ({ id, instanceId, treeId, props }) =>- await prisma.instanceProps.upsert({- where: { id: id },- create: { id: id, instanceId, treeId, props },- update: {- props,- },- })+ Object.values(nextProps).map(({ id, instanceId, treeId, props }) =>+ prisma.instanceProps.upsert({+ where: { id: id },+ create: { id: id, instanceId, treeId, props },+ update: {+ props,+ },+ })));};