-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathto-value.ts
More file actions
218 lines (191 loc) · 6.5 KB
/
to-value.ts
File metadata and controls
218 lines (191 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";
import type { StyleValue } from "../schema";
export type TransformValue = (styleValue: StyleValue) => undefined | StyleValue;
const fallbackTransform: TransformValue = (styleValue) => {
if (styleValue.type !== "fontFamily") {
return;
}
// By default we assume its a custom font stack.
let { value } = styleValue;
// Shouldn't be possible, but just in case.
if (value.length === 0) {
value = [DEFAULT_FONT_FALLBACK];
}
// User provided a single name. It could be a specific font name or a stack name.
if (value.length === 1) {
const stack = SYSTEM_FONTS.get(value[0])?.stack;
value = stack ?? [value[0], DEFAULT_FONT_FALLBACK];
}
return {
type: "fontFamily",
value: Array.from(new Set(value)),
};
};
// Use JSON.stringify to escape double quotes and backslashes in strings as it automatically replaces " with \" and \ with \\.
const sanitizeCssUrl = (str: string) => JSON.stringify(str);
export const toValue = (
styleValue: undefined | StyleValue,
transformValue?: TransformValue
): string => {
if (styleValue === undefined) {
return "";
}
const transformedValue =
transformValue?.(styleValue) ?? fallbackTransform(styleValue);
const value = transformedValue ?? styleValue;
if (value.type === "unit") {
return value.value + (value.unit === "number" ? "" : value.unit);
}
if (value.type === "fontFamily") {
const families = [];
for (const family of value.value) {
families.push(family.includes(" ") ? `"${family}"` : family);
}
return families.join(", ");
}
if (value.type === "var") {
if (value.hidden) {
return "";
}
let fallbacksString = "";
if (value.fallback) {
fallbacksString = `, ${toValue(value.fallback, transformValue)}`;
}
return `var(--${value.value}${fallbacksString})`;
}
if (value.type === "keyword") {
// The hidden property is used to hide values in the builder
// But we can't use none here like its done for image.
// As none is not valid in all cases.
// Eg: backface-visibility
// https://developer.mozilla.org/en-US/docs/Web/CSS/backface-visibility#syntax
if (value.hidden === true) {
return "";
}
return value.value;
}
if (value.type === "invalid") {
return value.value;
}
if (value.type === "unset") {
return value.value;
}
if (value.type === "rgb") {
return `rgb(${value.r} ${value.g} ${value.b} / ${value.alpha})`;
}
if (value.type === "color") {
let [c1, c2, c3] = value.components;
const alpha =
typeof value.alpha === "number" ? value.alpha : toValue(value.alpha);
// Use specific CSS functions when available
switch (value.colorSpace) {
case "hex": {
const toHex = (v: number) =>
Math.round(Math.min(Math.max(v, 0), 1) * 255)
.toString(16)
.padStart(2, "0");
const hex = `#${toHex(c1)}${toHex(c2)}${toHex(c3)}`;
const alphaNum = typeof alpha === "number" ? alpha : 1;
return alphaNum < 1 ? hex + toHex(alphaNum) : hex;
}
case "srgb": {
c1 = Math.round(c1 * 255);
c2 = Math.round(c2 * 255);
c3 = Math.round(c3 * 255);
return `rgb(${c1} ${c2} ${c3} / ${alpha})`;
}
case "hsl":
return `hsl(${c1} ${c2}% ${c3}% / ${alpha})`;
case "hwb":
return `hwb(${c1} ${c2}% ${c3}% / ${alpha})`;
case "lab":
return `lab(${c1}% ${c2} ${c3} / ${alpha})`;
case "lch":
return `lch(${c1}% ${c2} ${c3} / ${alpha})`;
case "oklab":
return `oklab(${c1} ${c2} ${c3} / ${alpha})`;
case "oklch":
return `oklch(${c1} ${c2} ${c3} / ${alpha})`;
// Fall back to color() function for less common color spaces.
// Webstudio uses colorjs internal names; map to CSS predefined color space names.
case "p3":
return `color(display-p3 ${c1} ${c2} ${c3} / ${alpha})`;
case "a98rgb":
return `color(a98-rgb ${c1} ${c2} ${c3} / ${alpha})`;
case "prophoto":
return `color(prophoto-rgb ${c1} ${c2} ${c3} / ${alpha})`;
case "srgb-linear":
case "rec2020":
case "xyz-d65":
case "xyz-d50":
default:
return `color(${value.colorSpace} ${c1} ${c2} ${c3} / ${alpha})`;
}
}
if (value.type === "image") {
if (value.hidden || value.value.type !== "url") {
// We assume that property is background-image and use this to hide background layers
// In the future we might want to have a more generic way to hide values
// i.e. have knowledge about property-name, as none is property specific
return "none";
}
// @todo image-set
return `url(${sanitizeCssUrl(value.value.url)})`;
}
if (value.type === "unparsed") {
if (value.hidden === true) {
// We assume that property is background-image and use this to hide background layers
// In the future we might want to have a more generic way to hide values
// i.e. have knowledge about property-name, as none is property specific
return "none";
}
return value.value;
}
if (value.type === "layers") {
const valueString = value.value
.filter((layer) => layer.hidden !== true)
.map((layer) => toValue(layer, transformValue))
.join(", ");
return valueString === "" ? "none" : valueString;
}
if (value.type === "tuple") {
// Properties ike translate and scale are handled as tuples directly.
// When the layer is hidden, the value goes as none.
if (value.hidden === true) {
return "none";
}
return value.value
.filter((value) => value.hidden !== true)
.map((value) => toValue(value, transformValue))
.join(" ");
}
if (value.type === "shadow") {
let shadow = `${toValue(value.offsetX)} ${toValue(value.offsetY)}`;
if (value.blur) {
shadow += ` ${toValue(value.blur)}`;
}
if (value.spread) {
shadow += ` ${toValue(value.spread)}`;
}
if (value.color) {
shadow += ` ${toValue(value.color)}`;
}
if (value.position === "inset") {
shadow += ` inset`;
}
return shadow;
}
if (value.type === "function") {
// Right now, we are using function-value only for filter and backdrop-filter functions
if (value.hidden === true) {
return "";
}
return `${value.name}(${toValue(value.args, transformValue)})`;
}
// https://www.w3.org/TR/css-variables-1/#guaranteed-invalid
if (value.type === "guaranteedInvalid") {
return "";
}
value satisfies never;
return "";
};