-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addClassesToSVGElement.js
84 lines (80 loc) · 1.87 KB
/
addClassesToSVGElement.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
export const name = 'addClassesToSVGElement';
export const description = 'adds classnames to an outer <svg> element';
var ENOCLS = `Error in plugin "addClassesToSVGElement": absent parameters.
It should have a list of classes in "classNames" or one "className".
Config example:
plugins: [
{
name: "addClassesToSVGElement",
params: {
className: "mySvg"
}
}
]
plugins: [
{
name: "addClassesToSVGElement",
params: {
classNames: ["mySvg", "size-big"]
}
}
]
`;
/**
* Add classnames to an outer <svg> element. Example config:
*
* plugins: [
* {
* name: "addClassesToSVGElement",
* params: {
* className: "mySvg"
* }
* }
* ]
*
* plugins: [
* {
* name: "addClassesToSVGElement",
* params: {
* classNames: ["mySvg", "size-big"]
* }
* }
* ]
*
* @author April Arcus
*
* @type {import('./plugins-types.js').Plugin<'addClassesToSVGElement'>}
*/
export const fn = (root, params, info) => {
if (
!(Array.isArray(params.classNames) && params.classNames.length !== 0) &&
!params.className
) {
console.error(ENOCLS);
return null;
}
const classNames = params.classNames || [params.className];
return {
element: {
enter: (node, parentNode) => {
if (node.name === 'svg' && parentNode.type === 'root') {
const classList = new Set(
node.attributes.class == null
? null
: node.attributes.class.split(' '),
);
for (const className of classNames) {
if (className != null) {
const classToAdd =
typeof className === 'string'
? className
: className(node, info);
classList.add(classToAdd);
}
}
node.attributes.class = Array.from(classList).join(' ');
}
},
},
};
};