-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergeScaleTransform2.index.js
68 lines (62 loc) · 1.96 KB
/
mergeScaleTransform2.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
import { each, go, go1, mapL, rangeL, some } from "fxjs/es";
import { $$getAttrNS } from "../getAttrNS/getAttrNS.index.js";
import { $$getBaseTransformList } from "../getBaseTransformList/getBaseTransformList.index.js";
import { $$isValidFxScaleSVGTransformList } from "../isValidFxScaleSVGTransformList/isValidFxScaleSVGTransformList.index.js";
import { $$setAttrNS } from "../setAttrNS/setAttrNS.index.js";
const VALID_DIRECTION = new Set(["n", "ne", "e", "se", "s", "sw", "w", "nw"]);
export const $$mergeScaleTransform2 = ({
index = 1,
is_need_correction = true,
x_name = "x",
y_name = "y",
width_name = "width",
height_name = "height",
direction,
} = {}) => ($el) => {
const transform_list = $$getBaseTransformList($el);
if (
!$$isValidFxScaleSVGTransformList({ index })(transform_list) ||
!VALID_DIRECTION.has(direction)
) {
return $el;
}
const [{ e: cx2, f: cy2 }, { a: sx, d: sy }, { e: cx1, f: cy1 }] = go(
rangeL(3),
mapL((i) => index - 1 + i),
mapL((i) => transform_list.getItem(i)),
mapL(({ matrix: m }) => m)
);
const [x, y, width, height] = go(
[x_name, y_name, width_name, height_name],
mapL((name) => $$getAttrNS(name)($el)),
mapL(parseFloat)
);
go(
[
[x, sx, cx1, cx2, width, ["e", "w"]],
[y, sy, cy1, cy2, height, ["n", "s"]],
],
mapL(([v, s, c1, c2, l, conditions]) =>
go(
conditions,
some((condition) => direction.includes(condition)),
(is_changed) =>
is_changed
? go1((v + c1) * s + c2, (v) =>
s < 0 && is_need_correction ? v + l * s : v
)
: v
)
),
([scaled_x, scaled_y]) => [
[x_name, scaled_x],
[y_name, scaled_y],
[width_name, width * Math.abs(sx)],
[height_name, height * Math.abs(sy)],
],
mapL(([k, v]) => [k, `${v}`]),
each((kv) => $$setAttrNS(kv)($el))
);
each(() => transform_list.removeItem(index - 1), rangeL(3));
return $el;
};