Files
webstudio/apps/builder/app/shared/webstudio-data-migrator.ts
T
Bogdan Chadkin 87210fa069 feat: expand backgound-position shorthand (#3646)
Ref https://github.com/webstudio-is/webstudio/issues/2647

Here replaced shorthand background-position with
background-position-x and background-position-y

Added shorthand expander, migration and merger
for css engine to improve browser support for offsets.
2024-06-29 13:43:37 +03:00

55 lines
1.8 KiB
TypeScript

import { camelCase } from "change-case";
import {
getStyleDeclKey,
type StyleDecl,
type WebstudioData,
} from "@webstudio-is/sdk";
import {
hyphenateProperty,
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 = hyphenateProperty(styleDecl.property);
// expands overflow shorthand into overflow-x and overflow-y longhands
// expands transition shorthand into transition-property, transition-duration, transition-timing-function, transition-delay longhands
// expands white-space into white-space-collapse and text-wrap-mode
if (
property === "overflow" ||
property === "transition" ||
property === "white-space" ||
property === "background-position"
) {
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);
}
}
}
};