Steve Ruiz 4 роки тому
джерело
коміт
e9d46a70e4
3 змінених файлів з 65 додано та 26 видалено
  1. 8
    16
      state/commands/distribute.ts
  2. 54
    8
      state/commands/stretch.ts
  3. 3
    2
      state/state.ts

+ 8
- 16
state/commands/distribute.ts Переглянути файл

@@ -1,10 +1,8 @@
1 1
 import Command from "./command"
2 2
 import history from "../history"
3
-import { AlignType, Data, DistributeType } from "types"
4
-import * as vec from "utils/vec"
3
+import { Data, DistributeType } from "types"
5 4
 import {
6 5
   getBoundsCenter,
7
-  getBoundsFromPoints,
8 6
   getCommonBounds,
9 7
   getPage,
10 8
   getSelectedShapes,
@@ -19,17 +17,11 @@ export default function distributeCommand(data: Data, type: DistributeType) {
19 17
   const entries = selectedShapes.map(
20 18
     (shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
21 19
   )
20
+
22 21
   const boundsForShapes = Object.fromEntries(entries)
23 22
 
24 23
   const commonBounds = getCommonBounds(...entries.map((entry) => entry[1]))
25 24
 
26
-  const innerBounds = getBoundsFromPoints(
27
-    entries.map((entry) => getBoundsCenter(entry[1]))
28
-  )
29
-
30
-  const midX = commonBounds.minX + commonBounds.width / 2
31
-  const midY = commonBounds.minY + commonBounds.height / 2
32
-
33 25
   const centers = Object.fromEntries(
34 26
     selectedShapes.map((shape) => [
35 27
       shape.id,
@@ -73,15 +65,15 @@ export default function distributeCommand(data: Data, type: DistributeType) {
73 65
                 ])
74 66
               }
75 67
             } else {
76
-              const sortedByCenter = entries.sort(
68
+              const entriesToMove = entries.sort(
77 69
                 (a, b) => centers[a[0]][0] - centers[b[0]][0]
78 70
               )
79 71
 
80 72
               let x = commonBounds.minX
81 73
               const step = (commonBounds.width - span) / (len - 1)
82 74
 
83
-              for (let i = 0; i < sortedByCenter.length - 1; i++) {
84
-                const [id, bounds] = sortedByCenter[i]
75
+              for (let i = 0; i < entriesToMove.length - 1; i++) {
76
+                const [id, bounds] = entriesToMove[i]
85 77
                 const shape = shapes[id]
86 78
                 getShapeUtils(shape).translateTo(shape, [x, bounds.minY])
87 79
                 x += bounds.width + step
@@ -115,15 +107,15 @@ export default function distributeCommand(data: Data, type: DistributeType) {
115 107
                 ])
116 108
               }
117 109
             } else {
118
-              const sortedByCenter = entries.sort(
110
+              const entriesToMove = entries.sort(
119 111
                 (a, b) => centers[a[0]][1] - centers[b[0]][1]
120 112
               )
121 113
 
122 114
               let y = commonBounds.minY
123 115
               const step = (commonBounds.height - span) / (len - 1)
124 116
 
125
-              for (let i = 0; i < sortedByCenter.length - 1; i++) {
126
-                const [id, bounds] = sortedByCenter[i]
117
+              for (let i = 0; i < entriesToMove.length - 1; i++) {
118
+                const [id, bounds] = entriesToMove[i]
127 119
                 const shape = shapes[id]
128 120
                 getShapeUtils(shape).translateTo(shape, [bounds.minX, y])
129 121
                 y += bounds.height + step

+ 54
- 8
state/commands/stretch.ts Переглянути файл

@@ -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
     })

+ 3
- 2
state/state.ts Переглянути файл

@@ -81,8 +81,6 @@ const state = createState({
81 81
     TOGGLED_STYLE_PANEL_OPEN: "toggleStylePanel",
82 82
     CHANGED_STYLE: ["updateStyles", "applyStylesToSelection"],
83 83
     RESET_CAMERA: "resetCamera",
84
-    ALIGNED: "alignSelection",
85
-    DISTRIBUTED: "distributeSelection",
86 84
     ZOOMED_TO_FIT: "zoomCameraToFit",
87 85
     ZOOMED_TO_SELECTION: {
88 86
       if: "hasSelection",
@@ -125,6 +123,9 @@ const state = createState({
125 123
             INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize",
126 124
             DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize",
127 125
             CHANGED_CODE_CONTROL: "updateControls",
126
+            ALIGNED: "alignSelection",
127
+            STRETCHED: "stretchSelection",
128
+            DISTRIBUTED: "distributeSelection",
128 129
             MOVED: "moveSelection",
129 130
           },
130 131
           initial: "notPointing",

Завантаження…
Відмінити
Зберегти