|
|
@@ -1,36 +1,82 @@
|
|
1
|
1
|
import Command from "./command"
|
|
2
|
2
|
import history from "../history"
|
|
3
|
|
-import { StretchType, Data } from "types"
|
|
4
|
|
-import { getPage } from "utils/utils"
|
|
|
3
|
+import { StretchType, Data, Edge, Corner } from "types"
|
|
|
4
|
+import { getCommonBounds, getPage, getSelectedShapes } from "utils/utils"
|
|
5
|
5
|
import { getShapeUtils } from "lib/shape-utils"
|
|
|
6
|
+import { current } from "immer"
|
|
6
|
7
|
|
|
7
|
8
|
export default function stretchCommand(data: Data, type: StretchType) {
|
|
8
|
9
|
const { currentPageId } = data
|
|
9
|
|
-
|
|
10
|
|
- const initialPoints = Object.fromEntries(
|
|
11
|
|
- Object.entries(getPage(data).shapes).map(([id, shape]) => [id, shape.point])
|
|
|
10
|
+ const initialShapes = getSelectedShapes(current(data))
|
|
|
11
|
+ const entries = initialShapes.map(
|
|
|
12
|
+ (shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
|
|
12
|
13
|
)
|
|
|
14
|
+ const boundsForShapes = Object.fromEntries(entries)
|
|
|
15
|
+ const commonBounds = getCommonBounds(...entries.map((entry) => entry[1]))
|
|
13
|
16
|
|
|
14
|
17
|
history.execute(
|
|
15
|
18
|
data,
|
|
16
|
19
|
new Command({
|
|
17
|
|
- name: "distributed",
|
|
|
20
|
+ name: "stretched",
|
|
18
|
21
|
category: "canvas",
|
|
19
|
22
|
do(data) {
|
|
20
|
23
|
const { shapes } = getPage(data, currentPageId)
|
|
21
|
24
|
|
|
22
|
25
|
switch (type) {
|
|
23
|
26
|
case StretchType.Horizontal: {
|
|
|
27
|
+ for (let id in boundsForShapes) {
|
|
|
28
|
+ const initialShape = initialShapes[id]
|
|
|
29
|
+ const shape = shapes[id]
|
|
|
30
|
+ const oldBounds = boundsForShapes[id]
|
|
|
31
|
+ const newBounds = { ...oldBounds }
|
|
|
32
|
+ newBounds.minX = commonBounds.minX
|
|
|
33
|
+ newBounds.width = commonBounds.width
|
|
|
34
|
+ newBounds.maxX = commonBounds.maxX
|
|
|
35
|
+
|
|
|
36
|
+ getShapeUtils(shape).transform(shape, newBounds, {
|
|
|
37
|
+ type: Corner.TopLeft,
|
|
|
38
|
+ scaleX: newBounds.width / oldBounds.width,
|
|
|
39
|
+ scaleY: 1,
|
|
|
40
|
+ initialShape,
|
|
|
41
|
+ transformOrigin: [0.5, 0.5],
|
|
|
42
|
+ })
|
|
|
43
|
+ }
|
|
|
44
|
+ break
|
|
24
|
45
|
}
|
|
25
|
46
|
case StretchType.Vertical: {
|
|
|
47
|
+ for (let id in boundsForShapes) {
|
|
|
48
|
+ const initialShape = initialShapes[id]
|
|
|
49
|
+ const shape = shapes[id]
|
|
|
50
|
+ const oldBounds = boundsForShapes[id]
|
|
|
51
|
+ const newBounds = { ...oldBounds }
|
|
|
52
|
+ newBounds.minY = commonBounds.minY
|
|
|
53
|
+ newBounds.height = commonBounds.height
|
|
|
54
|
+ newBounds.maxY = commonBounds.maxY
|
|
|
55
|
+
|
|
|
56
|
+ getShapeUtils(shape).transform(shape, newBounds, {
|
|
|
57
|
+ type: Corner.TopLeft,
|
|
|
58
|
+ scaleX: 1,
|
|
|
59
|
+ scaleY: newBounds.height / oldBounds.height,
|
|
|
60
|
+ initialShape,
|
|
|
61
|
+ transformOrigin: [0.5, 0.5],
|
|
|
62
|
+ })
|
|
|
63
|
+ }
|
|
26
|
64
|
}
|
|
27
|
65
|
}
|
|
28
|
66
|
},
|
|
29
|
67
|
undo(data) {
|
|
30
|
68
|
const { shapes } = getPage(data, currentPageId)
|
|
31
|
|
- for (let id in initialPoints) {
|
|
|
69
|
+ for (let id in boundsForShapes) {
|
|
32
|
70
|
const shape = shapes[id]
|
|
33
|
|
- getShapeUtils(shape).translateTo(shape, initialPoints[id])
|
|
|
71
|
+ const initialShape = initialShapes[id]
|
|
|
72
|
+ const initialBounds = boundsForShapes[id]
|
|
|
73
|
+ getShapeUtils(shape).transform(shape, initialBounds, {
|
|
|
74
|
+ type: Corner.BottomRight,
|
|
|
75
|
+ scaleX: 1,
|
|
|
76
|
+ scaleY: 1,
|
|
|
77
|
+ initialShape,
|
|
|
78
|
+ transformOrigin: [0.5, 0.5],
|
|
|
79
|
+ })
|
|
34
|
80
|
}
|
|
35
|
81
|
},
|
|
36
|
82
|
})
|