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.

predefined breakpoints now loaded from server

Oleg Isonen5116ea414012
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsx
index 87d14a507f..02dc15df18 100644
--- a/app/canvas/canvas.tsx
+++ b/app/canvas/canvas.tsx
@@ -6,9 +6,12 @@ import {
type OnChangeChildren,
type Data,
type Tree,
+ type Breakpoint,
useAllUserProps,
WrapperComponent,
globalStyles,
+ setBreakpoints,
+ Project,
} from "@webstudio-is/sdk";
import { useSubscribe } from "./shared/pubsub";
import {
@@ -27,15 +30,21 @@ import { useUpdateStyle } from "./shared/style";
import {
usePublishSelectedInstance,
usePublishRootInstance,
+ usePublishBreakpoints,
} from "./shared/publish";
import { useActiveElementTracking } from "./shared/active-element";
import { WrapperComponentDev } from "./features/wrapper-component";
-import { rootInstanceContainer, useRootInstance } from "./shared/nano-values";
-import { usePeriodicSync } from "./shared/use-periodic-sync";
+import {
+ rootInstanceContainer,
+ useRootInstance,
+ useBreakpoints,
+} from "./shared/nano-values";
+import { useSync } from "./shared/sync";
import { useManageProps } from "./shared/props";
const useElementsTree = () => {
const [rootInstance] = useRootInstance();
+ const [breakpoints] = useBreakpoints();
return useMemo(() => {
if (rootInstance === undefined) return;
@@ -51,22 +60,31 @@ const useElementsTree = () => {
return createElementsTree({
instance: rootInstance,
+ breakpoints,
Component: WrapperComponentDev,
onChangeChildren,
});
- }, [rootInstance]);
+ }, [rootInstance, breakpoints]);
};
-const useIsPreviewMode = () => {
+const useSubscribePreviewMode = () => {
const [isPreviewMode, setIsPreviewMode] = useState<boolean>(false);
useSubscribe<"previewMode", boolean>("previewMode", setIsPreviewMode);
return isPreviewMode;
};
+const useUpdateBreakpoints = (breakpoints: Array<Breakpoint>) => {
+ const [, setCurrentBreakpoints] = useBreakpoints();
+ setBreakpoints(breakpoints);
+ setCurrentBreakpoints(breakpoints);
+};
+
const PreviewMode = () => {
const [rootInstance] = useRootInstance();
+ const [breakpoints] = useBreakpoints();
if (rootInstance === undefined) return null;
return createElementsTree({
+ breakpoints,
instance: rootInstance,
Component: WrapperComponent,
});
@@ -74,19 +92,21 @@ const PreviewMode = () => {
type DesignModeProps = {
treeId: Tree["id"];
+ project: Project;
};
-const DesignMode = ({ treeId }: DesignModeProps) => {
+const DesignMode = ({ treeId, project }: DesignModeProps) => {
useDragDropHandlers();
useUpdateStyle();
useManageProps();
usePublishSelectedInstance({ treeId });
+ usePublishBreakpoints();
useInsertInstance();
useReparentInstance();
useDeleteInstance();
usePublishRootInstance();
useActiveElementTracking();
- usePeriodicSync();
+ useSync({ project });
const elements = useElementsTree();
return (
// Using touch backend becuase html5 drag&drop doesn't fire drag events in our case
@@ -96,17 +116,22 @@ const DesignMode = ({ treeId }: DesignModeProps) => {
);
};
-export const Canvas = ({ data }: { data: Data }): JSX.Element | null => {
+type CanvasProps = {
+ data: Data & { project: Project };
+};
+
+export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
+ useUpdateBreakpoints(data.breakpoints);
globalStyles();
useAllUserProps(data.props);
usePopulateRootInstance(data.tree);
// e.g. toggling preview is still needed in both modes
useShortcuts();
- const isPreviewMode = useIsPreviewMode();
+ const isPreviewMode = useSubscribePreviewMode();
if (isPreviewMode) {
return <PreviewMode />;
}
- return <DesignMode treeId={data.tree.id} />;
+ return <DesignMode treeId={data.tree.id} project={data.project} />;
};
diff --git a/app/canvas/shared/nano-values.ts b/app/canvas/shared/nano-values.ts
index 8d1e800233..b6696bb257 100644
--- a/app/canvas/shared/nano-values.ts
+++ b/app/canvas/shared/nano-values.ts
@@ -1,5 +1,9 @@
import { createValueContainer, useValue } from "react-nano-state";
-import { type Instance } from "@webstudio-is/sdk";
+import {
+ defaultBreakpoint,
+ type Breakpoint,
+ type Instance,
+} from "@webstudio-is/sdk";
import { type DropData } from "~/shared/component";
import store from "immerhin";
@@ -22,3 +26,14 @@ export const rootInstanceContainer = createValueContainer<
>();
export const useRootInstance = () => useValue(rootInstanceContainer);
store.register("root", rootInstanceContainer);
+
+export const breakpointsContainer = createValueContainer<Array<Breakpoint>>([
+ defaultBreakpoint,
+]);
+export const useBreakpoints = () => useValue(breakpointsContainer);
+
+export const selectedBreakpointContainer = createValueContainer<
+ Breakpoint | undefined
+>();
+export const useSelectedBreakpoint = () =>
+ useValue(selectedBreakpointContainer);
diff --git a/app/canvas/shared/publish.ts b/app/canvas/shared/publish.ts
index e86abd9019..f62c647476 100644
--- a/app/canvas/shared/publish.ts
+++ b/app/canvas/shared/publish.ts
@@ -5,12 +5,14 @@ import {
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";
@@ -38,7 +40,7 @@ export const usePublishSelectedInstance = ({
payload = {
id: instance.id,
component: instance.component,
- style: instance.style,
+ cssRules: instance.cssRules,
browserStyle: getBrowserStyle(selectedElement),
props,
};
@@ -60,3 +62,13 @@ export const usePublishRootInstance = () => {
});
}, [rootInstance]);
};
+
+export const usePublishBreakpoints = () => {
+ const [breakpoints] = useBreakpoints();
+ useEffect(() => {
+ publish<"loadBreakpoints", Array<Breakpoint>>({
+ type: "loadBreakpoints",
+ payload: breakpoints,
+ });
+ }, [breakpoints]);
+};
diff --git a/app/canvas/shared/style.ts b/app/canvas/shared/style.ts
index 12d31434a4..8d4f186a32 100644
--- a/app/canvas/shared/style.ts
+++ b/app/canvas/shared/style.ts
@@ -9,12 +9,12 @@ export const useUpdateStyle = () => {
useSubscribe<"updateStyle", StyleUpdates>(
"updateStyle",
- ({ id, updates }) => {
+ ({ id, updates, breakpoint }) => {
store.createTransaction([rootInstanceContainer], (rootInstance) => {
if (rootInstance === undefined || id !== selectedInstance?.id) {
return;
}
- setInstanceStyleMutable(rootInstance, id, updates);
+ setInstanceStyleMutable(rootInstance, id, updates, breakpoint);
});
}
);
diff --git a/app/designer/features/sync/index.ts b/app/canvas/shared/sync/index.ts
similarity index 51%
rename from app/designer/features/sync/index.ts
rename to app/canvas/shared/sync/index.ts
index 6bec9c3c28..375f89800c 100644
--- a/app/designer/features/sync/index.ts
+++ b/app/canvas/shared/sync/index.ts
@@ -1,2 +1 @@
export * from "./use-sync";
-export * from "./status";
diff --git a/app/designer/features/sync/queue.ts b/app/canvas/shared/sync/queue.ts
similarity index 72%
rename from app/designer/features/sync/queue.ts
rename to app/canvas/shared/sync/queue.ts
index 25db50dcfa..437f0964bf 100644
--- a/app/designer/features/sync/queue.ts
+++ b/app/canvas/shared/sync/queue.ts
@@ -1,4 +1,5 @@
-import { statusContainer } from "./status";
+import { type SyncStatus } from "~/shared/sync";
+import { publish } from "../pubsub";
// @todo because of limitation with nested models and saving tree as a Json type
// we load that entire Json to manipulate and if a second request happens before the first one has written
@@ -21,10 +22,16 @@ const dequeue = () => {
const job = queue.shift();
if (job) {
isInProgress = true;
- statusContainer.dispatch(isInProgress ? "syncing" : "idle");
+ publish<"syncStatus", SyncStatus>({
+ type: "syncStatus",
+ payload: "syncing",
+ });
job().finally(() => {
isInProgress = false;
- statusContainer.dispatch(isInProgress ? "syncing" : "idle");
+ publish<"syncStatus", SyncStatus>({
+ type: "syncStatus",
+ payload: "idle",
+ });
dequeue();
});
}
diff --git a/app/canvas/shared/sync/use-sync.ts b/app/canvas/shared/sync/use-sync.ts
new file mode 100644
index 0000000000..7bb011f61d
--- /dev/null
+++ b/app/canvas/shared/sync/use-sync.ts
@@ -0,0 +1,26 @@
+import useInterval from "react-use/lib/useInterval";
+import { sync } from "immerhin";
+import { enqueue } from "./queue";
+import { Project } from "@webstudio-is/sdk";
+
+export const useSync = ({ project }: { project: Project }) => {
+ useInterval(() => {
+ const entries = sync();
+ if (entries.length === 0) return;
+
+ // @todo this entire queueing logic needs to be gone, it's a workaround,
+ // because prisma can't do atomic updates yet with embeded documents
+ // and backend fetches and updates big objects, so if we send quickly,
+ // we end up overwriting things
+ enqueue(() =>
+ fetch(`/rest/patch`, {
+ method: "post",
+ body: JSON.stringify({
+ transactions: entries,
+ treeId: project.devTreeId,
+ projectId: project.id,
+ }),
+ })
+ );
+ }, 1000);
+};
diff --git a/app/canvas/shared/use-periodic-sync.ts b/app/canvas/shared/use-periodic-sync.ts
deleted file mode 100644
index 5fca0e8032..0000000000
--- a/app/canvas/shared/use-periodic-sync.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import useInterval from "react-use/lib/useInterval";
-import { sync, SyncItem } from "immerhin";
-import { publish } from "./pubsub";
-
-export const usePeriodicSync = () => {
- useInterval(() => {
- const entries = sync();
- if (entries.length === 0) return;
- publish<"syncChanges", Array<SyncItem>>({
- type: "syncChanges",
- payload: entries,
- });
- }, 1000);
-};
diff --git a/app/designer/designer.tsx b/app/designer/designer.tsx
index ab5128f46e..64e71f8155 100644
--- a/app/designer/designer.tsx
+++ b/app/designer/designer.tsx
@@ -1,7 +1,7 @@
import { useState } from "react";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
-import type { Project } from "@webstudio-is/sdk";
+import type { Breakpoint, Project } 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";
@@ -10,16 +10,18 @@ import { SidebarLeft } from "./features/sidebar-left";
import { Inspector } from "./features/inspector";
import { CanvasIframe, useSubscribe, usePublish } from "./shared/canvas-iframe";
import {
+ useBreakpoints,
useIsPreviewMode,
useRootInstance,
useSelectedInstanceData,
+ useSyncStatus,
} from "./shared/nano-values";
import { Topbar } from "./features/topbar";
import designerStyles from "./designer.css";
-import { useSync } from "./features/sync";
import { Breadcrumbs } from "./features/breadcrumbs";
import { TreePrevew } from "./features/tree-preview";
import { usePublishShortcuts } from "./shared/shortcuts/use-publish-shortcuts";
+import { type SyncStatus } from "~/shared/sync";
export const links = () => {
return [
@@ -41,6 +43,30 @@ const useSubscribeSelectedInstanceData = () => {
);
};
+const useSubscribeSyncStatus = () => {
+ const [, setValue] = useSyncStatus();
+ useSubscribe<"syncStatus", SyncStatus>("syncStatus", setValue);
+};
+
+const useSubscribeBreakpoints = () => {
+ const [, setValue] = useBreakpoints();
+ useSubscribe<"loadBreakpoints", Array<Breakpoint>>(
+ "loadBreakpoints",
+ setValue
+ );
+};
+
+const useIsDragging = (): [boolean, (isDragging: boolean) => void] => {
+ const [isDragging, setIsDragging] = useState<boolean>(false);
+ useSubscribe<"dragStartInstance">("dragStartInstance", () => {
+ setIsDragging(true);
+ });
+ useSubscribe<"dragEndInstance">("dragEndInstance", () => {
+ setIsDragging(false);
+ });
+ return [isDragging, setIsDragging];
+};
+
type SidePanelProps = {
children: JSX.Element | Array<JSX.Element>;
isPreviewMode: boolean;
@@ -130,21 +156,15 @@ type DesignerProps = {
};
export const Designer = ({ config, project }: DesignerProps) => {
- useSync({ config, project });
+ useSubscribeSyncStatus();
useSubscribeRootInstance();
useSubscribeSelectedInstanceData();
- const [isDragging, setIsDragging] = useState<boolean>();
+ useSubscribeBreakpoints();
const [publish, iframeRef] = usePublish();
const [isPreviewMode] = useIsPreviewMode();
+ const [isDragging, setIsDragging] = useIsDragging();
usePublishShortcuts(publish);
- useSubscribe<"dragStartInstance">("dragStartInstance", () => {
- setIsDragging(true);
- });
- useSubscribe<"dragEndInstance">("dragEndInstance", () => {
- setIsDragging(false);
- });
-
return (
<DndProvider backend={HTML5Backend}>
<ChromeWrapper isPreviewMode={isPreviewMode}>
diff --git a/app/designer/features/style-panel/get-inherited-style.ts b/app/designer/features/style-panel/get-inherited-style.ts
index 06c4c1cda8..0b2d924b07 100644
--- a/app/designer/features/style-panel/get-inherited-style.ts
+++ b/app/designer/features/style-panel/get-inherited-style.ts
@@ -50,6 +50,7 @@ const findParents = (
// - Walk down the tree and remember all parent instances until the found instance
// - Walk back up and find a value for each inheritable property and where value is not "inherit"
+// @todo this also has to match the media query, otheriwse its wrong
export const getInheritedStyle = (
rootInstance: Instance,
instanceId: Instance["id"]
@@ -57,16 +58,18 @@ export const getInheritedStyle = (
const parents = findParents(rootInstance, instanceId).reverse();
const inheritedStyle = {} as InheritedStyle;
for (const parent of parents) {
- for (const property in parent.style) {
- const isInheritable = property in inheritableProperties;
- const value = parent.style[property as StyleProperty];
- const hasValue = value !== undefined && value.value !== "inherit";
- const isFirst = property in inheritedStyle === false;
- if (isInheritable && hasValue && isFirst) {
- inheritedStyle[property as StyleProperty] = {
- instance: parent,
- value,
- };
+ for (const cssRule of parent.cssRules) {
+ for (const property in cssRule.style) {
+ const isInheritable = property in inheritableProperties;
+ const value = cssRule.style[property as StyleProperty];
+ const hasValue = value !== undefined && value.value !== "inherit";
+ const isFirst = property in inheritedStyle === false;
+ if (isInheritable && hasValue && isFirst) {
+ inheritedStyle[property as StyleProperty] = {
+ instance: parent,
+ value,
+ };
+ }
}
}
}
diff --git a/app/designer/features/style-panel/lib/spacing-widget.tsx b/app/designer/features/style-panel/lib/spacing-widget.tsx
index 5f7d23c2c3..9e472adb12 100644
--- a/app/designer/features/style-panel/lib/spacing-widget.tsx
+++ b/app/designer/features/style-panel/lib/spacing-widget.tsx
@@ -1,5 +1,9 @@
import { Box } from "~/shared/design-system";
-import { toCss, type StyleProperty, type StyleValue } from "@webstudio-is/sdk";
+import {
+ toValue,
+ type StyleProperty,
+ type StyleValue,
+} from "@webstudio-is/sdk";
import { SetProperty } from "../use-style-data";
import { useEffect, useState } from "react";
@@ -128,6 +132,17 @@ const TextField = ({ property, value, onEnter }: TextFieldProps) => {
);
};
+const toCss = (style: SpacingSingularStyle) => {
+ const css: Record<string, string> = {};
+ let property: SpacingProperty;
+ for (property in style) {
+ const value = style[property];
+ if (value === undefined) continue;
+ css[property] = toValue(value);
+ }
+ return css;
+};
+
type SpacingWidgetProps = {
setProperty: SetProperty;
values: SpacingStyles;
@@ -160,7 +175,7 @@ export const SpacingWidget = ({ setProperty, values }: SpacingWidgetProps) => {
>
<TextField
property={property}
- value={margins[property]?.toString()}
+ value={margins[property]}
onEnter={(value: string) => {
updateSpacing({
value,
@@ -182,7 +197,7 @@ export const SpacingWidget = ({ setProperty, values }: SpacingWidgetProps) => {
>
<TextField
property={property}
- value={paddings[property]?.toString()}
+ value={paddings[property]}
onEnter={(value: string) => {
updateSpacing({
value,
diff --git a/app/designer/features/style-panel/use-style-data.ts b/app/designer/features/style-panel/use-style-data.ts
index 08645df131..82430f716b 100644
--- a/app/designer/features/style-panel/use-style-data.ts
+++ b/app/designer/features/style-panel/use-style-data.ts
@@ -1,8 +1,15 @@
import { useState, useMemo, useEffect } from "react";
import type { SelectedInstanceData, StyleUpdates } from "~/shared/component";
-import { type Style, type StyleProperty } from "@webstudio-is/sdk";
+import {
+ type Breakpoint,
+ type Style,
+ type StyleProperty,
+} from "@webstudio-is/sdk";
import { type Publish } from "~/designer/shared/canvas-iframe";
-import { useRootInstance } from "../../shared/nano-values";
+import {
+ useRootInstance,
+ useSelectedBreakpoint,
+} from "../../shared/nano-values";
import { parseCssValue } from "./parse-css-value";
import { getInheritedStyle, type InheritedStyle } from "./get-inherited-style";
@@ -17,15 +24,36 @@ export type SetProperty = (
property: StyleProperty
) => (value: string, options?: StyleUpdateOptions) => void;
+const getCurrentStyle = (
+ {
+ cssRules,
+ browserStyle,
+ }: Pick<SelectedInstanceData, "cssRules" | "browserStyle"> = {
+ cssRules: [],
+ browserStyle: {},
+ },
+ breakpoint?: Breakpoint
+) => {
+ if (breakpoint === undefined) return browserStyle;
+ const cssRule = cssRules.find(
+ (cssRule) => cssRule.breakpoint === breakpoint.ref
+ );
+ if (cssRule === undefined) return browserStyle;
+ return {
+ ...browserStyle,
+ ...cssRule.style,
+ };
+};
+
export const useStyleData = ({
selectedInstanceData,
publish,
}: UseStyleData): [Style | void, InheritedStyle | void, SetProperty] => {
const [rootInstance] = useRootInstance();
- const [currentStyle, setCurrentStyle] = useState<Style | undefined>({
- ...selectedInstanceData?.browserStyle,
- ...selectedInstanceData?.style,
- });
+ const [selectedBreakpoint] = useSelectedBreakpoint();
+ const [currentStyle, setCurrentStyle] = useState<Style | undefined>(
+ getCurrentStyle(selectedInstanceData, selectedBreakpoint)
+ );
const inheritedStyle = useMemo(() => {
if (
currentStyle === undefined ||
@@ -38,11 +66,8 @@ export const useStyleData = ({
}, [currentStyle, selectedInstanceData, rootInstance]);
useEffect(() => {
- setCurrentStyle({
- ...selectedInstanceData?.browserStyle,
- ...selectedInstanceData?.style,
- });
- }, [selectedInstanceData?.style, selectedInstanceData?.browserStyle]);
+ setCurrentStyle(getCurrentStyle(selectedInstanceData, selectedBreakpoint));
+ }, [selectedInstanceData, selectedBreakpoint]);
const publishUpdates = (
type: "update" | "preview",
@@ -57,6 +82,7 @@ export const useStyleData = ({
payload: {
id: selectedInstanceData.id,
updates,
+ breakpoint: selectedBreakpoint,
},
});
};
diff --git a/app/designer/features/sync/status.ts b/app/designer/features/sync/status.ts
deleted file mode 100644
index fe132fde0f..0000000000
--- a/app/designer/features/sync/status.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-import { createValueContainer } from "react-nano-state";
-export const statusContainer = createValueContainer<"syncing" | "idle">("idle");
diff --git a/app/designer/features/sync/use-sync.ts b/app/designer/features/sync/use-sync.ts
deleted file mode 100644
index 61a67d6a6c..0000000000
--- a/app/designer/features/sync/use-sync.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { type Project } from "@webstudio-is/sdk";
-import { type SyncItem } from "immerhin";
-import type { Config } from "~/config";
-import { useSubscribe } from "~/designer/shared/canvas-iframe";
-import { enqueue } from "./queue";
-
-// @todo this entire queueing logic needs to be gone, it's a workaround,
-// because prisma can't do atomic updates yet with embeded documents
-// and backend fetches and updates big objects, so if we send quickly,
-// we end up overwriting things
-export const useSync = ({ project }: { config: Config; project: Project }) => {
- useSubscribe<"syncChanges", Array<SyncItem>>(
- "syncChanges",
- (transactions) => {
- enqueue(() =>
- fetch(`/rest/patch`, {
- method: "post",
- body: JSON.stringify({
- transactions,
- treeId: project.devTreeId,
- projectId: project.id,
- }),
- })
- );
- }
- );
-};
diff --git a/app/designer/features/topbar/breakpoints.tsx b/app/designer/features/topbar/breakpoints.tsx
index 4d4ead61c5..dfbad5d8c3 100644
--- a/app/designer/features/topbar/breakpoints.tsx
+++ b/app/designer/features/topbar/breakpoints.tsx
@@ -1,4 +1,5 @@
import { useState } from "react";
+import { type Breakpoint } from "@webstudio-is/sdk";
import {
DropdownMenu,
DropdownMenuContent,
@@ -11,21 +12,13 @@ import {
Flex,
} from "~/shared/design-system";
import { type Publish } from "../../shared/canvas-iframe";
-
-type DefaultBreakpoint = {
- label: string;
- isDefault: true;
-};
-
-type CustomBreakpoint = {
- label: string;
- maxWidth: number;
-};
-
-type Breakpoint = CustomBreakpoint | DefaultBreakpoint;
+import {
+ useBreakpoints,
+ useSelectedBreakpoint,
+} from "../../shared/nano-values";
type EditableBreakpointProps = {
- breakpoint: CustomBreakpoint;
+ breakpoint: Breakpoint;
onChange: (breakpoint: Breakpoint) => void;
};
@@ -33,6 +26,7 @@ const EditableBreakpoint = ({
breakpoint,
onChange,
}: EditableBreakpointProps) => {
+ const isDefault = breakpoint.maxWidth === -1;
return (
<form
onKeyDown={(event) => {
@@ -41,7 +35,8 @@ const EditableBreakpoint = ({
onChange={(event) => {
event.stopPropagation();
const data = new FormData(event.currentTarget);
- const nextBreakpoint: CustomBreakpoint = {
+ const nextBreakpoint: Breakpoint = {
+ ref: breakpoint.ref,
label: String(data.get("label")),
maxWidth: Number(data.get("maxWidth")),
};
@@ -53,13 +48,16 @@ const EditableBreakpoint = ({
defaultValue={breakpoint.label}
css={{ width: 120 }}
name="label"
+ readOnly={isDefault}
/>
<TextField
variant="ghost"
- defaultValue={breakpoint.maxWidth}
+ defaultValue={isDefault ? "∞" : breakpoint.maxWidth}
css={{ width: 60 }}
- type="number"
+ type={isDefault ? "text" : "number"}
name="maxWidth"
+ min={0}
+ readOnly={isDefault}
/>
</form>
);
@@ -72,41 +70,30 @@ const menuItemCss = {
flexGrow: 1,
};
-const defaultBreakpoints: Array<Breakpoint> = [
- { label: "Default", isDefault: true },
- { label: "Desktop L", maxWidth: 1920 },
- { label: "Desktop M", maxWidth: 1440 },
- { label: "Desktop S", maxWidth: 1280 },
- { label: "Tablet L", maxWidth: 1024 },
- { label: "Tablet M", maxWidth: 768 },
- { label: "Tablet S", maxWidth: 601 },
- { label: "Phone", maxWidth: 414 },
-];
-
type BreakpointsProps = {
publish: Publish;
};
export const Breakpoints = ({ publish }: BreakpointsProps) => {
- const [breakpoints] = useState(defaultBreakpoints);
+ const [breakpoints] = useBreakpoints();
+ const [selectedBreakpoint, setSelectedBreakpoint] = useSelectedBreakpoint();
const [isEditing, setIsEditing] = useState(false);
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button ghost aria-label="Breakpoints">
- Desktop
+ {selectedBreakpoint?.label ?? breakpoints[0].label}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
{breakpoints.map((breakpoint, index) => {
if (isEditing) {
- if ("isDefault" in breakpoint) return null;
return (
<Flex gap="1" css={{ px: "$2" }} key={index}>
<EditableBreakpoint
- breakpoint={breakpoint as CustomBreakpoint}
- onChange={(breakpoint) => {
- console.log(breakpoint);
+ breakpoint={breakpoint}
+ onChange={(_breakpoint) => {
+ // @todo
}}
/>
</Flex>
@@ -118,10 +105,11 @@ export const Breakpoints = ({ publish }: BreakpointsProps) => {
key={index}
css={menuItemCss}
onSelect={() => {
- publish<"shortcut", string>({
- type: "shortcut",
- payload: "x",
+ publish({
+ type: "selectBreakpoint",
+ payload: breakpoint,
});
+ setSelectedBreakpoint(breakpoint);
}}
>
{breakpoint.label}
diff --git a/app/designer/features/topbar/sync-status.tsx b/app/designer/features/topbar/sync-status.tsx
index a48007d775..5373ba29a2 100644
--- a/app/designer/features/topbar/sync-status.tsx
+++ b/app/designer/features/topbar/sync-status.tsx
@@ -1,7 +1,6 @@
-import { useValue } from "react-nano-state";
import { Flex, keyframes, AccessibleIcon } from "~/shared/design-system";
import { CheckIcon, DotsHorizontalIcon } from "~/shared/icons";
-import { statusContainer } from "../sync";
+import { useSyncStatus } from "../../shared/nano-values";
const iconSize = 15;
@@ -25,7 +24,7 @@ const AnimatedDotsIcon = () => {
};
export const SyncStatus = () => {
- const [status] = useValue(statusContainer);
+ const [status] = useSyncStatus();
return (
<Flex
align="center"
diff --git a/app/designer/shared/nano-values/index.ts b/app/designer/shared/nano-values/index.ts
index 060e016823..13967f43d1 100644
--- a/app/designer/shared/nano-values/index.ts
+++ b/app/designer/shared/nano-values/index.ts
@@ -1,6 +1,11 @@
import { createValueContainer, useValue } from "react-nano-state";
-import { type Instance } from "@webstudio-is/sdk";
+import {
+ type Breakpoint,
+ type Instance,
+ defaultBreakpoint,
+} from "@webstudio-is/sdk";
import { SelectedInstanceData } from "~/shared/component";
+import { type SyncStatus } from "~/shared/sync";
const rootInstanceContainer = createValueContainer<Instance | undefined>();
export const useRootInstance = () => useValue(rootInstanceContainer);
@@ -20,3 +25,16 @@ export const useIsShareDialogOpen = () => useValue(isShareDialogOpenContainer);
const isPublishDialogOpenContainer = createValueContainer<boolean>(false);
export const useIsPublishDialogOpen = () =>
useValue(isPublishDialogOpenContainer);
+
+export const selectedBreakpointContainer =
+ createValueContainer<Breakpoint>(defaultBreakpoint);
+export const useSelectedBreakpoint = () =>
+ useValue(selectedBreakpointContainer);
+
+export const breakpointsContainer = createValueContainer<Array<Breakpoint>>([
+ defaultBreakpoint,
+]);
+export const useBreakpoints = () => useValue(breakpointsContainer);
+
+const syncStatusContainer = createValueContainer<SyncStatus>("idle");
+export const useSyncStatus = () => useValue(syncStatusContainer);
diff --git a/app/routes/canvas/$projectId.tsx b/app/routes/canvas/$projectId.tsx
index b1c5c2cc03..54448b5a11 100644
--- a/app/routes/canvas/$projectId.tsx
+++ b/app/routes/canvas/$projectId.tsx
@@ -1,22 +1,15 @@
import { useLoaderData, type LoaderFunction } from "remix";
-import { type Data } from "@webstudio-is/sdk";
import { Canvas } from "~/canvas";
-import * as db from "~/shared/db";
-
-export type ErrorData = {
- errors: string;
-};
+import { loadCanvasData, type ErrorData, type CanvasData } from "~/shared/db";
export const loader: LoaderFunction = async ({
params,
-}): Promise<Data | ErrorData> => {
+}): Promise<CanvasData | ErrorData> => {
+ if (params.projectId === undefined) {
+ return { errors: "Missing projectId" };
+ }
try {
- 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 };
+ return await loadCanvasData({ projectId: params.projectId });
} catch (error) {
if (error instanceof Error) {
return {
@@ -28,7 +21,7 @@ export const loader: LoaderFunction = async ({
};
const CanvasRoute = () => {
- const data = useLoaderData<Data | ErrorData>();
+ const data = useLoaderData<CanvasData | ErrorData>();
// @todo how should we treat this kind of errors?
if ("errors" in data) {
return <p>{data.errors}</p>;
diff --git a/app/routes/index.tsx b/app/routes/index.tsx
index 44d5c874a1..41d7328f07 100644
--- a/app/routes/index.tsx
+++ b/app/routes/index.tsx
@@ -1,5 +1,5 @@
import { redirect, useLoaderData, type LoaderFunction } from "remix";
-import { Root, type Data } from "@webstudio-is/sdk";
+import { Root, initialBreakpoints, type Data } from "@webstudio-is/sdk";
import config from "~/config";
import * as db from "~/shared/db";
import Document from "./canvas";
@@ -8,7 +8,9 @@ import Document from "./canvas";
type Error = { errors: string };
-export const loader: LoaderFunction = async ({ request }) => {
+export const loader: LoaderFunction = async ({
+ request,
+}): Promise<Data | Error | Response> => {
const host =
request.headers.get("x-forwarded-host") ||
request.headers.get("host") ||
@@ -23,7 +25,9 @@ export const loader: LoaderFunction = async ({ request }) => {
try {
const tree = await db.tree.loadByDomain(userDomain);
const props = await db.props.loadByTreeId(tree.id);
- return { tree, props };
+ // @todo fetch breakpoints
+ const breakpoints = initialBreakpoints;
+ return { tree, props, breakpoints };
} catch (error) {
if (error instanceof Error) {
return { errors: error.message };
diff --git a/app/routes/preview/$projectId.tsx b/app/routes/preview/$projectId.tsx
index dff565aeba..0f43f8a37d 100644
--- a/app/routes/preview/$projectId.tsx
+++ b/app/routes/preview/$projectId.tsx
@@ -1,12 +1,27 @@
-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";
+import { useLoaderData, type LoaderFunction } from "remix";
+import { Root } from "@webstudio-is/sdk";
+import { loadPreviewData, type PreviewData, type ErrorData } from "~/shared/db";
-export { loader };
+export const loader: LoaderFunction = async ({
+ params,
+}): Promise<PreviewData | ErrorData> => {
+ if (params.projectId === undefined) {
+ return { errors: "Missing projectId" };
+ }
+ try {
+ return await loadPreviewData({ projectId: params.projectId });
+ } catch (error) {
+ if (error instanceof Error) {
+ return {
+ errors: error.message,
+ };
+ }
+ }
+ return { errors: "Unexpected error" };
+};
const PreviewRoute = () => {
- const data = useLoaderData<Data | ErrorData>();
+ const data = useLoaderData<PreviewData | ErrorData>();
if ("errors" in data) {
return <p>{data.errors}</p>;
}
diff --git a/app/shared/component/instance-data.ts b/app/shared/component/instance-data.ts
index d61b232ab4..7e728e8f5f 100644
--- a/app/shared/component/instance-data.ts
+++ b/app/shared/component/instance-data.ts
@@ -1,15 +1,17 @@
import {
type Instance,
type InstanceProps,
+ type CssRule,
type Style,
type StyleProperty,
type StyleValue,
+ type Breakpoint,
} from "@webstudio-is/sdk";
export type SelectedInstanceData = {
id: Instance["id"];
component: Instance["component"];
- style: Style;
+ cssRules: Array<CssRule>;
browserStyle: Style;
props: InstanceProps;
};
@@ -22,4 +24,5 @@ type StyleUpdate = {
export type StyleUpdates = {
id: Instance["id"];
updates: Array<StyleUpdate>;
+ breakpoint: Breakpoint;
};
diff --git a/app/shared/db/canvas.server.ts b/app/shared/db/canvas.server.ts
new file mode 100644
index 0000000000..86dccb4049
--- /dev/null
+++ b/app/shared/db/canvas.server.ts
@@ -0,0 +1,38 @@
+import { type Data, initialBreakpoints, type Project } from "@webstudio-is/sdk";
+import * as db from ".";
+
+export type CanvasData = Data & { project: Project };
+
+export type ErrorData = {
+ errors: string;
+};
+
+const loadData = async ({ projectId }: { projectId: string }) => {
+ const project = await db.project.loadById(projectId);
+ if (project === null) throw new Error(`Project "${projectId}" not found`);
+ const [tree, props] = await Promise.all([
+ db.tree.loadByProject(project, "development"),
+ db.props.loadByProject(project, "development"),
+ ]);
+ // @todo fetch breakpoints from db
+ return { tree, props, project, breakpoints: initialBreakpoints };
+};
+
+export const loadCanvasData = async ({
+ projectId,
+}: {
+ projectId: string;
+}): Promise<CanvasData | ErrorData> => {
+ return await loadData({ projectId });
+};
+
+export type PreviewData = Data;
+
+export const loadPreviewData = async ({
+ projectId,
+}: {
+ projectId: string;
+}): Promise<PreviewData | ErrorData> => {
+ const { tree, props, breakpoints } = await loadData({ projectId });
+ return { tree, props, breakpoints };
+};
diff --git a/app/shared/db/index.ts b/app/shared/db/index.ts
index 7e14ac8a8f..73291816bb 100644
--- a/app/shared/db/index.ts
+++ b/app/shared/db/index.ts
@@ -2,3 +2,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 * from "./canvas.server";
diff --git a/app/shared/sync/index.ts b/app/shared/sync/index.ts
new file mode 100644
index 0000000000..583bfa713b
--- /dev/null
+++ b/app/shared/sync/index.ts
@@ -0,0 +1 @@
+export * from "./sync-status";
diff --git a/app/shared/sync/sync-status.ts b/app/shared/sync/sync-status.ts
new file mode 100644
index 0000000000..06c827be78
--- /dev/null
+++ b/app/shared/sync/sync-status.ts
@@ -0,0 +1 @@
+export type SyncStatus = "syncing" | "idle";
diff --git a/app/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap b/app/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap
index 6ee425297b..e9b9078d9c 100644
--- a/app/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap
+++ b/app/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap
@@ -6,13 +6,13 @@ Object {
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "2",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "1",
- "style": Object {},
}
`;
@@ -22,12 +22,12 @@ Object {
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "2",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "1",
- "style": Object {},
}
`;
diff --git a/app/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap b/app/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap
index a99eea03ea..99a82e1136 100644
--- a/app/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap
+++ b/app/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap
@@ -6,19 +6,19 @@ Object {
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "2",
- "style": Object {},
},
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "3",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "1",
- "style": Object {},
}
`;
@@ -28,19 +28,19 @@ Object {
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "2",
- "style": Object {},
},
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "3",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "1",
- "style": Object {},
}
`;
@@ -50,19 +50,19 @@ Object {
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "3",
- "style": Object {},
},
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "2",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "1",
- "style": Object {},
}
`;
@@ -74,17 +74,17 @@ Object {
Object {
"children": Array [],
"component": "Box",
+ "cssRules": Array [],
"id": "3",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "2",
- "style": Object {},
},
],
"component": "Box",
+ "cssRules": Array [],
"id": "1",
- "style": Object {},
}
`;
diff --git a/app/shared/tree-utils/clone-instance.test.ts b/app/shared/tree-utils/clone-instance.test.ts
index 58af6281ee..59fb08459c 100644
--- a/app/shared/tree-utils/clone-instance.test.ts
+++ b/app/shared/tree-utils/clone-instance.test.ts
@@ -14,17 +14,17 @@ describe("Clone instance", () => {
const instance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3",
- style: {},
+ cssRules: [],
children: [],
},
],
diff --git a/app/shared/tree-utils/create-instance.test.ts b/app/shared/tree-utils/create-instance.test.ts
index c9501b05ac..2da713f8d6 100644
--- a/app/shared/tree-utils/create-instance.test.ts
+++ b/app/shared/tree-utils/create-instance.test.ts
@@ -20,7 +20,7 @@ describe("create instance", () => {
const createdInstance = createInstance({
component: "Box",
});
- expect(typeof createdInstance.style).toBe("object");
+ expect(Array.isArray(createdInstance.cssRules)).toBeTruthy();
});
test("instance with children ", () => {
@@ -30,17 +30,17 @@ describe("create instance", () => {
{
id: "1",
component: "Box",
- style: {},
+ cssRules: [],
children: [],
},
],
- style: {},
+ cssRules: [],
});
expect(createdInstance.children).toEqual([
{
id: "1",
component: "Box",
- style: {},
+ cssRules: [],
children: [],
},
]);
diff --git a/app/shared/tree-utils/create-instance.ts b/app/shared/tree-utils/create-instance.ts
index 1d7e0e40e5..5d32348028 100644
--- a/app/shared/tree-utils/create-instance.ts
+++ b/app/shared/tree-utils/create-instance.ts
@@ -1,23 +1,23 @@
import ObjectId from "bson-objectid";
-import { type Style, type Instance } from "@webstudio-is/sdk";
+import { type CssRule, type Instance } from "@webstudio-is/sdk";
import { primitives } from "~/shared/component";
export const createInstance = ({
component,
id,
children,
- style,
+ cssRules,
}: {
component: Instance["component"];
id?: Instance["id"];
children?: Instance["children"];
- style?: Style;
+ cssRules?: Array<CssRule>;
}): Instance => {
const primitive = primitives[component];
return {
component,
id: id === undefined ? ObjectId().toString() : id,
- style: style ?? {},
+ cssRules: cssRules ?? [],
children:
children === undefined
? "children" in primitive
diff --git a/app/shared/tree-utils/delete-instance.test.ts b/app/shared/tree-utils/delete-instance.test.ts
index 16a2520bf6..e086bc68d7 100644
--- a/app/shared/tree-utils/delete-instance.test.ts
+++ b/app/shared/tree-utils/delete-instance.test.ts
@@ -6,17 +6,17 @@ describe("Delete instance", () => {
const instance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3",
- style: {},
+ cssRules: [],
children: [],
},
],
@@ -32,18 +32,18 @@ describe("Delete instance", () => {
const instance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [],
},
{
component: "Box",
id: "3",
- style: {},
+ cssRules: [],
children: [],
},
],
diff --git a/app/shared/tree-utils/find-closest-sibling-instance.test.ts b/app/shared/tree-utils/find-closest-sibling-instance.test.ts
index 25e9ff7385..b7549a4e90 100644
--- a/app/shared/tree-utils/find-closest-sibling-instance.test.ts
+++ b/app/shared/tree-utils/find-closest-sibling-instance.test.ts
@@ -6,26 +6,26 @@ describe("Find closest sibling instance", () => {
const rootInstance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3",
- style: {},
+ cssRules: [],
children: [],
},
"a",
{
component: "Box",
id: "4",
- style: {},
+ cssRules: [],
children: [],
},
"b",
{
component: "Box",
id: "5",
- style: {},
+ cssRules: [],
children: [],
},
],
diff --git a/app/shared/tree-utils/find-parent-instance.test.ts b/app/shared/tree-utils/find-parent-instance.test.ts
index 4391227455..693518f0a3 100644
--- a/app/shared/tree-utils/find-parent-instance.test.ts
+++ b/app/shared/tree-utils/find-parent-instance.test.ts
@@ -6,17 +6,17 @@ describe("Find parent instance", () => {
const rootInstance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3",
- style: {},
+ cssRules: [],
children: [],
},
],
diff --git a/app/shared/tree-utils/get-instance-path.test.ts b/app/shared/tree-utils/get-instance-path.test.ts
index 4561625582..4e9599053d 100644
--- a/app/shared/tree-utils/get-instance-path.test.ts
+++ b/app/shared/tree-utils/get-instance-path.test.ts
@@ -8,17 +8,17 @@ describe("Get instance path", () => {
const instance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3",
- style: {},
+ cssRules: [],
children: [],
},
],
@@ -33,17 +33,17 @@ describe("Get instance path", () => {
const instance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2a",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3a",
- style: {},
+ cssRules: [],
children: [],
},
],
@@ -51,12 +51,12 @@ describe("Get instance path", () => {
{
component: "Box",
id: "2b",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "3b",
- style: {},
+ cssRules: [],
children: [],
},
],
diff --git a/app/shared/tree-utils/insert-instance.test.ts b/app/shared/tree-utils/insert-instance.test.ts
index 4b6c3a112b..242f4ffcbb 100644
--- a/app/shared/tree-utils/insert-instance.test.ts
+++ b/app/shared/tree-utils/insert-instance.test.ts
@@ -9,12 +9,12 @@ describe("Insert instance", () => {
const rootInstance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [],
},
],
@@ -32,12 +32,12 @@ describe("Insert instance", () => {
const rootInstance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [],
},
],
@@ -55,12 +55,12 @@ describe("Insert instance", () => {
const rootInstance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [],
},
],
@@ -77,12 +77,12 @@ describe("Insert instance", () => {
const rootInstance: Instance = {
component: "Box",
id: "1",
- style: {},
+ cssRules: [],
children: [
{
component: "Box",
id: "2",
- style: {},
+ cssRules: [],
children: [],
},
],
diff --git a/app/shared/tree-utils/populate.ts b/app/shared/tree-utils/populate.ts
index 82213f81c6..92dc5d106f 100644
--- a/app/shared/tree-utils/populate.ts
+++ b/app/shared/tree-utils/populate.ts
@@ -1,4 +1,4 @@
-import { type Instance } from "@webstudio-is/sdk";
+import { type Instance, defaultBreakpoint } from "@webstudio-is/sdk";
import { primitives } from "~/shared/component";
/**
@@ -9,22 +9,12 @@ import { primitives } from "~/shared/component";
export const populateInstance = (instance: Instance): Instance => {
const populatedInstance = { ...instance };
const primitive = primitives[instance.component];
- if (primitive !== undefined) {
- populatedInstance.style =
- "defaultStyle" in primitive
- ? { ...primitive.defaultStyle, ...instance.style }
- : instance.style;
- }
- return populatedInstance;
-};
-
-export const populateTree = (instance: Instance): Instance => {
- const populatedInstance = populateInstance(instance);
- populatedInstance.children = [];
- for (const child of instance.children) {
- const populatedChild =
- typeof child === "string" ? child : populateTree(child);
- populatedInstance.children.push(populatedChild);
+ if (primitive !== undefined && "defaultStyle" in primitive) {
+ const cssRule = {
+ breakpoint: defaultBreakpoint.ref,
+ style: primitive.defaultStyle,
+ };
+ populatedInstance.cssRules.push(cssRule);
}
return populatedInstance;
};
diff --git a/app/shared/tree-utils/set-instance-style.ts b/app/shared/tree-utils/set-instance-style.ts
index 556285e112..429eeaad03 100644
--- a/app/shared/tree-utils/set-instance-style.ts
+++ b/app/shared/tree-utils/set-instance-style.ts
@@ -1,17 +1,25 @@
-import { type Instance } from "@webstudio-is/sdk";
+import { Breakpoint, type Instance } from "@webstudio-is/sdk";
import { type StyleUpdates } from "~/shared/component";
import { findInstanceById } from "./find-instance";
export const setInstanceStyleMutable = (
rootInstance: Instance,
id: Instance["id"],
- updates: StyleUpdates["updates"]
+ updates: StyleUpdates["updates"],
+ breakpoint: Breakpoint
) => {
const instance = findInstanceById(rootInstance, id);
if (instance === undefined) return false;
+ let cssRule = instance.cssRules.find(
+ (cssRule) => cssRule.breakpoint === breakpoint.ref
+ );
+ if (cssRule === undefined) {
+ cssRule = { style: {}, breakpoint: breakpoint.ref };
+ instance.cssRules.push(cssRule);
+ }
for (const update of updates) {
- instance.style[update.property] = update.value;
+ cssRule.style[update.property] = update.value;
}
return true;
};
diff --git a/mongo-migrations/style.js b/mongo-migrations/style.js
new file mode 100644
index 0000000000..137759a6d6
--- /dev/null
+++ b/mongo-migrations/style.js
@@ -0,0 +1,31 @@
+// I am running this migration manually over console,
+// because we don't have an automated setup for migrations yet.
+// This converts instance.style to instance.cssRules
+const cursor = db.Tree.find();
+
+const transform = (instance) => {
+ if (instance.style) {
+ instance.cssRules = [
+ {
+ breakpoint: "default",
+ style: instance.style,
+ },
+ ];
+ delete instance.style;
+ }
+ if (instance.children) {
+ instance.children.forEach((child) => {
+ transform(child);
+ });
+ }
+};
+
+const migrate = (tree) => {
+ transform(tree.root);
+ db.Tree.update({ _id: tree._id }, { $set: { root: tree.root } });
+ printjson(tree._id.toString());
+};
+
+while (cursor.hasNext()) {
+ migrate(cursor.next());
+}
diff --git a/package.json b/package.json
index c79bde8283..54a7ed414a 100644
--- a/package.json
+++ b/package.json
@@ -42,7 +42,7 @@
"@remix-run/serve": "^1.2.3",
"@remix-run/vercel": "^1.2.3",
"@stitches/react": "^1.2.7",
- "@webstudio-is/sdk": "^0.1.22",
+ "@webstudio-is/sdk": "../webstudio-sdk",
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
"hyphenate-style-name": "^1.0.4",
diff --git a/yarn.lock b/yarn.lock
index 2d6efedc5c..4b53a49639 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1809,10 +1809,8 @@
dependencies:
web-streams-polyfill "^3.1.1"
-"@webstudio-is/sdk@^0.1.22":
+"@webstudio-is/sdk@../webstudio-sdk":
version "0.1.22"
- resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.1.22.tgz#5247a05a32363fc8a200997949f6aefc443fbffc"
- integrity sha512-sKF4T7yTkm0VQleV1wFWJqlBPj4PtSoDzlGsRT2Y5eRSJRg03WyAu9GkBufJgFJh/6evL9dWPj3Ktjjm0KdkDQ==
dependencies:
"@prisma/client" "^3.10.0"
"@stitches/react" "^1.2.7"