Files
webstudio/apps/builder/app/shared/webstudio-data-migrator.ts
T
Bogdan Chadkin 9e23dc9580 feat: switch to overflow longhands (#3588)
Ref https://github.com/webstudio-is/webstudio/issues/2647

Here refactored overflow UI to update "overflow-x" and "overflow-y"
longhands instead of "overflow" shorthand.

Removed "overflow" from advanced panel suggestions.

Added support for expanding overflow shorthand for webflow copy paste.

And implemented very basic migrations utility
which always run after data is loaded from server
and sends patch for expanding overflow longhands.
Patch is not sent when no properties are modified. Though eventually we
will need to support versioning to avoid executing old migrations.

<img width="279" alt="Screenshot 2024-06-18 at 19 33 21"
src="https://github.com/webstudio-is/webstudio/assets/5635476/ce44385e-3316-4e34-bc9c-5f790cbdcd4f">
2024-06-20 11:51:00 +03:00

44 lines
1.4 KiB
TypeScript

import { camelCase } from "change-case";
import {
getStyleDeclKey,
type StyleDecl,
type WebstudioData,
} from "@webstudio-is/sdk";
import { toValue, type StyleProperty } from "@webstudio-is/css-engine";
import { expandShorthands, parseCssValue } from "@webstudio-is/css-data";
/**
*
* Transform loaded data and sync into server
* before passing into application
* should be idempotent because can be executed multiple times
*
* Very basic version of client migrations
* Should be versioned eventually to avoid running every time
*
* For now patch is prevented by excluding empty transactions from sync queue
*
*/
export const migrateWebstudioDataMutable = (data: WebstudioData) => {
for (const [styleDeclKey, styleDecl] of data.styles) {
const property = styleDecl.property as string;
// expand overflow shorthand into overflow-x and overflow-y longhands
if (property === "overflow") {
data.styles.delete(styleDeclKey);
const longhands = expandShorthands([
[property, toValue(styleDecl.value)],
]);
for (const [hyphenedProperty, value] of longhands) {
const longhandProperty = camelCase(hyphenedProperty) as StyleProperty;
const longhandStyleDecl: StyleDecl = {
...styleDecl,
property: longhandProperty,
value: parseCssValue(longhandProperty, value),
};
data.styles.set(getStyleDeclKey(longhandStyleDecl), longhandStyleDecl);
}
}
}
};