-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (56 loc) · 1.6 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
import { createSVGWindow } from "svgdom";
import { SVG, registerWindow } from "@svgdotjs/svg.js";
import { serializeSvgElement } from "./utils";
interface SVGOData {
children: Array<{
type: string;
name: string;
[key: string]: any;
}>;
}
interface Node {
attributes: { [key: string]: any };
}
export const svgoCropPlugin = {
name: "svgo-crop-plugin",
fn: (data: SVGOData) => {
const window = createSVGWindow();
const document = window.document;
registerWindow(window, document);
const svgElement = data.children.find(
(child) => child.type === "element" && child.name === "svg"
);
if (!svgElement) {
throw new Error("SVG element not found");
}
const svgString = serializeSvgElement(svgElement);
document.documentElement.innerHTML = svgString;
const draw = SVG(document.documentElement);
let minX = Infinity,
minY = Infinity,
maxX = -Infinity,
maxY = -Infinity;
draw.find("path").forEach((path) => {
if (path.attr("fill") !== "none" && path.node.style.display !== "none") {
const bbox = path.bbox();
minX = Math.min(minX, bbox.x);
minY = Math.min(minY, bbox.y);
maxX = Math.max(maxX, bbox.x + bbox.width);
maxY = Math.max(maxY, bbox.y + bbox.height);
}
});
return {
element: {
enter: (node: Node) => {
if (!node.attributes.viewBox) {
return;
}
node.attributes = {
...node.attributes,
viewBox: `${minX} ${minY} ${maxX - minX} ${maxY - minY}`,
};
},
},
};
},
};