Files
webstudio/apps/builder/app/shared/webstudio-data-migrator.ts
T
Bogdan Chadkin 6a453b94dc refactor: migrate border and outline to hyphenated properties (#4960)
Migrated two more sections and made ComputedStyleDecl['property'] more
strict instead of just a string, now can get rid from `as StyleProperty`
casting all over the codebase.
2025-03-07 17:16:16 +07:00

53 lines
1.7 KiB
TypeScript

import {
getStyleDeclKey,
type StyleDecl,
type WebstudioData,
} from "@webstudio-is/sdk";
import { hyphenateProperty, toValue } from "@webstudio-is/css-engine";
import {
camelCaseProperty,
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) as string;
// 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 longhandStyleDecl: StyleDecl = {
...styleDecl,
property: camelCaseProperty(hyphenedProperty),
value: parseCssValue(hyphenedProperty, value),
};
data.styles.set(getStyleDeclKey(longhandStyleDecl), longhandStyleDecl);
}
}
}
};