/
style.ts
139 lines (125 loc) · 3.19 KB
/
style.ts
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
import { kebabCase, only } from "../util.js";
import * as nodes from "./style/nodes/index.js";
import { DEFAULT_UNIT } from "./style/units.js";
const VECTOR_TYPE_RE =
/VECTOR|BOOLEAN_OPERATION|STAR|LINE|ELLIPSE|REGULAR_POLYGON|RECTANGLE/;
const FRAME_TYPE_RE = /FRAME|COMPONENT|COMPONENT_SET|INSTANCE/;
const { defineProperties, fromEntries, entries } = Object;
const styles = {
position: ["top", "right", "bottom", "left"],
size: ["width", "height"],
shape: ["borderRadius"],
border: ["border"],
background: ["background", "backgroundColor"],
color: ["color", "fill", "stroke"],
blend: ["filter", "opacity"],
frame: [
"padding",
"flexDirection",
"justifyContent",
"alignItems",
"gap",
"overflow",
"contain",
],
text: [
"fontStyle",
"fontWeight",
"fontSize",
"fontFamily",
"lineHeight",
"textDecoration",
"textAlign",
"verticalAlign",
"letterSpacing",
"textTransform",
"textIndent",
"whiteSpace",
"wordBreak",
],
};
const options = { unit: DEFAULT_UNIT };
const processed = Symbol("styleplugin");
export default (opts?: { unit: "pixel" | "rem" | "perc" | "unitless" }) => {
if (opts) {
options.unit = opts.unit;
}
return {
name: "style",
node(node: NodeApi) {
if ((node as any)[processed]) return;
(node as any)[processed] = true;
let getter: any;
if (node?.type.match(VECTOR_TYPE_RE)) {
getter = vectorStyle;
} else if (node?.type.match(FRAME_TYPE_RE)) {
getter = frameStyle;
} else if (node?.type === "TEXT") {
getter = textStyle;
} else if (node?.type === "GROUP") {
getter = frameStyle;
}
if (getter !== undefined) {
defineProperties(node, {
styleMap: {
get() {
return getter(node);
},
},
styles: {
get() {
return Object.fromEntries(
Object.entries(styles).map(([name, props]) => {
return [name, new StyleMap(only(this.styleMap, ...props))];
})
);
},
},
});
}
},
};
};
export class StyleMap {
constructor(props: Record<string, string | number>) {
Object.assign(this, props);
}
toString() {
return Object.entries(this)
.filter(([_, v]) => !!v)
.map(([k, v]) => `${kebabCase(k)}: ${v};`)
.join("\n");
}
}
function frameStyle(node: NodeApi): StyleMap {
return new StyleMap(
fromEntries(
entries(nodes.frame)
.map(([p, f]) => [p, f(node as NodeApi<FrameNode>, options.unit)])
.filter(([_, v]) => !!v)
)
);
}
function vectorStyle(node: NodeApi): StyleMap {
return new StyleMap(
fromEntries(
entries(nodes.vector)
.map(([p, f]: any) => [p, f(node as NodeApi<VectorNode>, options.unit)])
.filter(([_, v]) => !!v)
)
);
}
function textStyle(node: NodeApi): StyleMap {
return new StyleMap(
fromEntries(
entries(nodes.text)
.map(([p, f]) => [p, f(node as NodeApi<TextNode>, options.unit)])
.filter(([_, v]) => !!v)
)
);
}
declare module "../figmatik" {
interface NodeApi {
styleMap: StyleMap;
}
}