-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathindex.ts
213 lines (178 loc) · 5.06 KB
/
index.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
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
import { getBoundingClientRect } from "./helpers/dimensions";
import { getImagePaintWithUrl } from "./helpers/image";
import { isHidden, textNodesUnder, traverse } from "./helpers/nodes";
import { size } from "./helpers/object";
import { getRgb } from "./helpers/parsers";
import {
addStrokesFromBorder,
getStrokesRectangle,
getAppliedComputedStyles,
getDropShadowEffects,
getBorderRadii,
} from "./helpers/styles";
import { createSvgLayer, processSvgUseElements } from "./helpers/svg";
import { buildTextNode } from "./helpers/text";
import { getLayersForFrames } from "./helpers/frames";
import { LayerNode, WithRef } from "./types/nodes";
const generateElements = (el: Element) => {
const getShadowEls = (el: Element): Element[] =>
Array.from(el.shadowRoot?.querySelectorAll("*") || []).reduce(
(memo, el) => [...memo, el, ...getShadowEls(el)],
[] as Element[]
);
const els = Array.from(el.querySelectorAll("*")).reduce(
(memo, el) => [...memo, el, ...getShadowEls(el)],
[] as Element[]
);
return els;
};
function removeRefs({
layers,
root,
}: {
layers: LayerNode[];
root: WithRef<FrameNode>;
}) {
layers.concat([root]).forEach((layer) => {
traverse(layer, (child) => {
delete child.ref;
});
});
}
const getLayersForElement = (el: Element) => {
const elementLayers: LayerNode[] = [];
if (isHidden(el)) {
return [];
}
if (el instanceof SVGSVGElement) {
elementLayers.push(createSvgLayer(el));
return elementLayers;
}
// Sub SVG Eleemnt
else if (el instanceof SVGElement) {
return [];
}
// for `picture`, we only need the `image` element. We can ignore the parent `picture` and
// `source` sibling elements.
if (
(el.parentElement instanceof HTMLPictureElement &&
el instanceof HTMLSourceElement) ||
el instanceof HTMLPictureElement
) {
return [];
}
// TO-DO: what does `appliedStyles` do here? All we do is check that it's non-empty
const appliedStyles = getAppliedComputedStyles(el);
const computedStyle = getComputedStyle(el);
if (
(size(appliedStyles) ||
el instanceof HTMLImageElement ||
el instanceof HTMLVideoElement) &&
computedStyle.display !== "none"
) {
const rect = getBoundingClientRect(el);
if (rect.width >= 1 && rect.height >= 1) {
const fills: Paint[] = [];
const color = getRgb(computedStyle.backgroundColor);
if (color) {
const solidPaint: SolidPaint = {
type: "SOLID",
color: {
r: color.r,
g: color.g,
b: color.b,
},
opacity: color.a || 1,
};
fills.push(solidPaint);
}
const rectNode: WithRef<RectangleNode> = {
type: "RECTANGLE",
ref: el,
x: Math.round(rect.left),
y: Math.round(rect.top),
width: Math.round(rect.width),
height: Math.round(rect.height),
fills,
};
const strokes = addStrokesFromBorder({ computedStyle });
if (strokes) {
Object.assign(rectNode, strokes);
}
if (!rectNode.strokes) {
for (const dir of ["top", "left", "right", "bottom"] as const) {
const strokesLayer = getStrokesRectangle({
dir,
rect,
computedStyle,
el,
});
if (strokesLayer) {
elementLayers.push(strokesLayer);
}
}
}
const imagePaint = getImagePaintWithUrl({ computedStyle, el });
if (imagePaint) {
fills.push(imagePaint);
rectNode.name = "IMAGE";
}
const shadowEffects = getDropShadowEffects({ computedStyle });
if (shadowEffects) {
rectNode.effects = shadowEffects;
}
const borderRadii = getBorderRadii({ computedStyle });
Object.assign(rectNode, borderRadii);
elementLayers.push(rectNode);
}
}
return elementLayers;
};
export function htmlToFigma(
selector: HTMLElement | string = "body",
useFrames = false,
time = false
) {
if (time) {
console.time("Parse dom");
}
const layers: LayerNode[] = [];
const el =
selector instanceof HTMLElement
? selector
: document.querySelector(selector || "body");
if (el) {
processSvgUseElements(el);
const els = generateElements(el);
els.forEach((el) => {
const elLayers = getLayersForElement(el);
layers.push(...elLayers);
});
const textNodes = textNodesUnder(el);
for (const node of textNodes) {
const textNode = buildTextNode({ node });
if (textNode) {
layers.push(textNode);
}
}
}
// TODO: send frame: { children: []}
const root: WithRef<FrameNode> = {
type: "FRAME",
width: Math.round(window.innerWidth),
height: Math.round(document.documentElement.scrollHeight),
x: 0,
y: 0,
ref: document.body,
};
layers.unshift(root);
const framesLayers = useFrames
? getLayersForFrames({ layers, root })
: layers;
removeRefs({ layers: framesLayers, root });
if (time) {
console.info("\n");
console.timeEnd("Parse dom");
}
return framesLayers;
}