-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
143 lines (122 loc) · 3.23 KB
/
index.js
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
const DEBUG = false;
function layer(context, layer) {
try {
const style =
layer.textStyles.length === 0 ? null : layer.textStyles[0].textStyle;
const styles = [
width(layer),
height(layer),
fontFamily(style),
fontSize(style),
fontWeight(style),
fontStyle(style),
fontStretch(style),
lineHeight(style),
textAlign(style),
color(style),
border(layer),
borderRadius(layer),
boxShadow(layer),
opacity(layer),
backgroundColor(layer)
].filter(Boolean);
let language = "javascript";
let code = `
const Component = styled.div\`
${styles.join("\n ")}
\`;
`;
if (DEBUG) {
language = "json";
code = JSON.stringify({ context, layer });
}
return {
code,
language
};
} catch (e) {
return {
code: e.message,
language: "text"
};
}
}
function width(layer) {
const width = layer.rect.width;
return `width: ${Math.round(width)}px;`;
}
function height(layer) {
const height = layer.rect.height;
return `height: ${Math.round(height)}px;`;
}
function fontFamily(style) {
if (style == null) return "";
return `font-family: ${style.fontFamily};`;
}
function fontSize(style) {
if (style == null) return "";
return `font-size: ${style.fontSize}px;`;
}
function fontWeight(style) {
if (style == null) return "";
return `font-weight: ${style.fontWeight};`;
}
function fontStyle(style) {
if (style == null) return "";
return style.fontStyle === "normal" ? "" : `font-style: ${style.fontStyle};`;
}
function fontStretch(style) {
if (style == null) return "";
return style.fontStretch === "normal"
? ""
: `font-stretch: ${style.fontStretch};`;
}
function lineHeight(style) {
if (style == null || style.lineHeight === undefined) return "";
const height = style.lineHeight / style.fontSize;
return `line-height: ${Math.round(height * 100) / 100};`;
}
function textAlign(style) {
if (style == null) return "";
return style.textAlign === "left" ? "" : `text-align: ${style.textAlign};`;
}
function color(style) {
if (style == null) return "";
return `color: ${formatColor(style.color)};`;
}
function opacity(layer) {
return layer.opacity === 1 ? "" : `opacity: ${layer.opacity};`;
}
function border(layer) {
return layer.borders.length === 0
? ""
: `border: solid ${Math.round(layer.borders[0].thickness)}px ${formatColor(
layer.borders[0].fill.color
)};`;
}
function borderRadius(layer) {
return layer.borderRadius === 0
? ""
: `border-radius: ${layer.borderRadius}px;`;
}
function boxShadow(layer) {
return layer.shadows.length === 0
? ""
: `box-shadow: ${Math.round(layer.shadows[0].offsetX)}px ${Math.round(
layer.shadows[0].offsetY
)}px ${Math.round(layer.shadows[0].blurRadius)}px ${Math.round(
layer.shadows[0].spread
)}px ${formatColor(layer.borders[0].fill.color)};`;
}
function backgroundColor(layer) {
return layer.fills.length === 0
? ""
: `background-color: ${formatColor(layer.fills[0].color)};`;
}
z;
function formatColor(color) {
const hex = color.toHex();
return color.a === 1
? `#${hex.r}${hex.g}${hex.b}`
: `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;
}