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.

createBatchUpdate to apply multiple properties updates at once

Oleg Isonen69e123a224d2
diff --git a/apps/designer/app/designer/features/style-panel/lib/use-style-data.ts b/apps/designer/app/designer/features/style-panel/lib/use-style-data.ts
index 5e0a4afa18..b352d523a4 100644
--- a/apps/designer/app/designer/features/style-panel/lib/use-style-data.ts
+++ b/apps/designer/app/designer/features/style-panel/lib/use-style-data.ts
@@ -86,13 +86,18 @@ export const useStyleData = ({
});
};
+ const toStyleValue = (property: StyleProperty, value: string) => {
+ if (currentStyle === undefined) return;
+ const currentValue = currentStyle[property];
+ const defaultUnit =
+ currentValue?.type === "unit" ? currentValue?.unit : undefined;
+ return parseCssValue(property, value, defaultUnit);
+ };
+
const setProperty: SetProperty = (property) => {
return (input, options = { isEphemeral: false }) => {
- if (currentStyle === undefined) return;
- const currentValue = currentStyle[property];
- const defaultUnit =
- currentValue?.type === "unit" ? currentValue?.unit : undefined;
- const nextValue = parseCssValue(property, input, defaultUnit);
+ const nextValue = toStyleValue(property, input);
+ if (nextValue === undefined) return;
if (nextValue.type !== "invalid") {
const updates = [{ property, value: nextValue }];
const type = options.isEphemeral ? "preview" : "update";
@@ -104,5 +109,37 @@ export const useStyleData = ({
};
};
- return { currentStyle, inheritedStyle, setProperty };
+ const createBatchUpdate = () => {
+ const updates: StyleUpdates["updates"] = [];
+
+ const setProperty = (property: StyleProperty) => {
+ const setValue = (input: string) => {
+ const value = toStyleValue(property, input);
+ if (value === undefined || value.type === "invalid") {
+ return;
+ }
+ updates.push({ property, value });
+ };
+ return setValue;
+ };
+
+ const publish = () => {
+ publishUpdates("update", updates);
+ const nextStyle = updates.reduce(
+ (currentStyle, { property, value }) => {
+ currentStyle[property] = value;
+ return currentStyle;
+ },
+ { ...currentStyle }
+ );
+ setCurrentStyle(nextStyle);
+ };
+
+ return {
+ setProperty,
+ publish,
+ };
+ };
+
+ return { currentStyle, inheritedStyle, setProperty, createBatchUpdate };
};