mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-05 03:28:59 +09:00
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.
55 lines
1.8 KiB
TypeScript
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);
|
|
}
|
|
}
|
|
}
|
|
};
|