瀏覽代碼

Adds stretch

main
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
 import Command from "./command"
1
 import Command from "./command"
2
 import history from "../history"
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
 import {
4
 import {
6
   getBoundsCenter,
5
   getBoundsCenter,
7
-  getBoundsFromPoints,
8
   getCommonBounds,
6
   getCommonBounds,
9
   getPage,
7
   getPage,
10
   getSelectedShapes,
8
   getSelectedShapes,
19
   const entries = selectedShapes.map(
17
   const entries = selectedShapes.map(
20
     (shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
18
     (shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
21
   )
19
   )
20
+
22
   const boundsForShapes = Object.fromEntries(entries)
21
   const boundsForShapes = Object.fromEntries(entries)
23
 
22
 
24
   const commonBounds = getCommonBounds(...entries.map((entry) => entry[1]))
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
   const centers = Object.fromEntries(
25
   const centers = Object.fromEntries(
34
     selectedShapes.map((shape) => [
26
     selectedShapes.map((shape) => [
35
       shape.id,
27
       shape.id,
73
                 ])
65
                 ])
74
               }
66
               }
75
             } else {
67
             } else {
76
-              const sortedByCenter = entries.sort(
68
+              const entriesToMove = entries.sort(
77
                 (a, b) => centers[a[0]][0] - centers[b[0]][0]
69
                 (a, b) => centers[a[0]][0] - centers[b[0]][0]
78
               )
70
               )
79
 
71
 
80
               let x = commonBounds.minX
72
               let x = commonBounds.minX
81
               const step = (commonBounds.width - span) / (len - 1)
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
                 const shape = shapes[id]
77
                 const shape = shapes[id]
86
                 getShapeUtils(shape).translateTo(shape, [x, bounds.minY])
78
                 getShapeUtils(shape).translateTo(shape, [x, bounds.minY])
87
                 x += bounds.width + step
79
                 x += bounds.width + step
115
                 ])
107
                 ])
116
               }
108
               }
117
             } else {
109
             } else {
118
-              const sortedByCenter = entries.sort(
110
+              const entriesToMove = entries.sort(
119
                 (a, b) => centers[a[0]][1] - centers[b[0]][1]
111
                 (a, b) => centers[a[0]][1] - centers[b[0]][1]
120
               )
112
               )
121
 
113
 
122
               let y = commonBounds.minY
114
               let y = commonBounds.minY
123
               const step = (commonBounds.height - span) / (len - 1)
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
                 const shape = shapes[id]
119
                 const shape = shapes[id]
128
                 getShapeUtils(shape).translateTo(shape, [bounds.minX, y])
120
                 getShapeUtils(shape).translateTo(shape, [bounds.minX, y])
129
                 y += bounds.height + step
121
                 y += bounds.height + step

+ 54
- 8
state/commands/stretch.ts 查看文件

1
 import Command from "./command"
1
 import Command from "./command"
2
 import history from "../history"
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
 import { getShapeUtils } from "lib/shape-utils"
5
 import { getShapeUtils } from "lib/shape-utils"
6
+import { current } from "immer"
6
 
7
 
7
 export default function stretchCommand(data: Data, type: StretchType) {
8
 export default function stretchCommand(data: Data, type: StretchType) {
8
   const { currentPageId } = data
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
   history.execute(
17
   history.execute(
15
     data,
18
     data,
16
     new Command({
19
     new Command({
17
-      name: "distributed",
20
+      name: "stretched",
18
       category: "canvas",
21
       category: "canvas",
19
       do(data) {
22
       do(data) {
20
         const { shapes } = getPage(data, currentPageId)
23
         const { shapes } = getPage(data, currentPageId)
21
 
24
 
22
         switch (type) {
25
         switch (type) {
23
           case StretchType.Horizontal: {
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
           case StretchType.Vertical: {
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
       undo(data) {
67
       undo(data) {
30
         const { shapes } = getPage(data, currentPageId)
68
         const { shapes } = getPage(data, currentPageId)
31
-        for (let id in initialPoints) {
69
+        for (let id in boundsForShapes) {
32
           const shape = shapes[id]
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
     TOGGLED_STYLE_PANEL_OPEN: "toggleStylePanel",
81
     TOGGLED_STYLE_PANEL_OPEN: "toggleStylePanel",
82
     CHANGED_STYLE: ["updateStyles", "applyStylesToSelection"],
82
     CHANGED_STYLE: ["updateStyles", "applyStylesToSelection"],
83
     RESET_CAMERA: "resetCamera",
83
     RESET_CAMERA: "resetCamera",
84
-    ALIGNED: "alignSelection",
85
-    DISTRIBUTED: "distributeSelection",
86
     ZOOMED_TO_FIT: "zoomCameraToFit",
84
     ZOOMED_TO_FIT: "zoomCameraToFit",
87
     ZOOMED_TO_SELECTION: {
85
     ZOOMED_TO_SELECTION: {
88
       if: "hasSelection",
86
       if: "hasSelection",
125
             INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize",
123
             INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize",
126
             DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize",
124
             DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize",
127
             CHANGED_CODE_CONTROL: "updateControls",
125
             CHANGED_CODE_CONTROL: "updateControls",
126
+            ALIGNED: "alignSelection",
127
+            STRETCHED: "stretchSelection",
128
+            DISTRIBUTED: "distributeSelection",
128
             MOVED: "moveSelection",
129
             MOVED: "moveSelection",
129
           },
130
           },
130
           initial: "notPointing",
131
           initial: "notPointing",

Loading…
取消
儲存