Steve Ruiz 4 лет назад
Родитель
Сommit
e21748f7b7

+ 0
- 1
components/canvas/bounds.tsx Просмотреть файл

@@ -137,7 +137,6 @@ function Corner({
137 137
           rCorner.current.setPointerCapture(e.pointerId)
138 138
           state.send("POINTED_BOUNDS_CORNER", inputs.pointerDown(e, corner))
139 139
         }}
140
-        onPointerCancelCapture={() => console.log("oops")}
141 140
         onPointerUp={(e) => {
142 141
           e.stopPropagation()
143 142
           rCorner.current.releasePointerCapture(e.pointerId)

+ 7
- 5
components/code-panel/code-panel.tsx Просмотреть файл

@@ -30,10 +30,11 @@ const getErrorLineAndColumn = (e: any) => {
30 30
 
31 31
 export default function CodePanel() {
32 32
   const rContainer = useRef<HTMLDivElement>(null)
33
-
34
-  const fileId = "file0"
35 33
   const isReadOnly = useSelector((s) => s.data.isReadOnly)
36
-  const file = useSelector((s) => s.data.document.code[fileId])
34
+  const fileId = useSelector((s) => s.data.currentCodeFileId)
35
+  const file = useSelector(
36
+    (s) => s.data.document.code[s.data.currentCodeFileId]
37
+  )
37 38
   const isOpen = true
38 39
   const fontSize = useSelector((s) => s.data.settings.fontSize)
39 40
 
@@ -52,7 +53,7 @@ export default function CodePanel() {
52 53
         on: {
53 54
           RAN_CODE: "runCode",
54 55
           SAVED_CODE: ["runCode", "saveCode"],
55
-          CHANGED_CODE: [{ secretlyDo: "setCode" }],
56
+          CHANGED_CODE: { secretlyDo: "setCode" },
56 57
           CLEARED_ERROR: { if: "hasError", do: "clearError" },
57 58
           TOGGLED_DOCS: { to: "viewingDocs" },
58 59
         },
@@ -89,7 +90,8 @@ export default function CodePanel() {
89 90
         data.error = error
90 91
       },
91 92
       saveCode(data) {
92
-        state.send("CHANGED_CODE", { fileId, code: data.code })
93
+        const { code } = data
94
+        state.send("SAVED_CODE", { code })
93 95
       },
94 96
       clearError(data) {
95 97
         data.error = null

+ 1
- 1
components/editor.tsx Просмотреть файл

@@ -14,7 +14,7 @@ export default function Editor() {
14 14
       <Canvas />
15 15
       <StatusBar />
16 16
       <Toolbar />
17
-      {/* <CodePanel /> */}
17
+      <CodePanel />
18 18
     </>
19 19
   )
20 20
 }

+ 12
- 1
lib/code/generate.ts Просмотреть файл

@@ -3,12 +3,23 @@ import Circle from "./circle"
3 3
 import Ellipse from "./ellipse"
4 4
 import Polyline from "./polyline"
5 5
 import Dot from "./dot"
6
+import Ray from "./ray"
6 7
 import Line from "./line"
7 8
 import Vector from "./vector"
8 9
 import Utils from "./utils"
9 10
 import { codeShapes } from "./index"
10 11
 
11
-const scope = { Dot, Circle, Ellipse, Line, Polyline, Rectangle, Vector, Utils }
12
+const scope = {
13
+  Dot,
14
+  Circle,
15
+  Ellipse,
16
+  Ray,
17
+  Line,
18
+  Polyline,
19
+  Rectangle,
20
+  Vector,
21
+  Utils,
22
+}
12 23
 
13 24
 /**
14 25
  * Evaluate code, collecting generated shapes in the shape set. Return the

+ 25
- 0
lib/code/ray.ts Просмотреть файл

@@ -0,0 +1,25 @@
1
+import CodeShape from "./index"
2
+import { v4 as uuid } from "uuid"
3
+import { RayShape, ShapeType } from "types"
4
+
5
+export default class Ray extends CodeShape<RayShape> {
6
+  constructor(props = {} as Partial<RayShape>) {
7
+    super({
8
+      id: uuid(),
9
+      type: ShapeType.Ray,
10
+      isGenerated: false,
11
+      name: "Ray",
12
+      parentId: "page0",
13
+      childIndex: 0,
14
+      point: [0, 0],
15
+      direction: [0, 1],
16
+      rotation: 0,
17
+      style: {
18
+        fill: "#777",
19
+        stroke: "#000",
20
+        strokeWidth: 1,
21
+      },
22
+      ...props,
23
+    })
24
+  }
25
+}

+ 10
- 6
lib/code/vector.ts Просмотреть файл

@@ -274,16 +274,12 @@ export default class Vector {
274 274
 
275 275
   lrp(b: Vector, t: number) {
276 276
     const n = new Vector(this)
277
-    this.vec(b)
278
-      .mul(t)
279
-      .add(n)
277
+    this.vec(b).mul(t).add(n)
280 278
   }
281 279
 
282 280
   static lrp(a: Vector, b: Vector, t: number) {
283 281
     const n = new Vector(a)
284
-    n.vec(b)
285
-      .mul(t)
286
-      .add(a)
282
+    n.vec(b).mul(t).add(a)
287 283
     return n
288 284
   }
289 285
 
@@ -390,6 +386,14 @@ export default class Vector {
390 386
     return n.div(n.len())
391 387
   }
392 388
 
389
+  normalize() {
390
+    return this.uni()
391
+  }
392
+
393
+  static normalize(v: Vector) {
394
+    return Vector.uni(v)
395
+  }
396
+
393 397
   isLeft(center: Vector, b: Vector) {
394 398
     return (
395 399
       (center.x - this.x) * (b.y - this.y) - (b.x - this.x) * (center.y - b.y)

+ 2
- 2
state/history.ts Просмотреть файл

@@ -106,12 +106,12 @@ class History extends BaseHistory<Data> {
106 106
     const cameraInfo = localStorage.getItem("code_slate_camera")
107 107
 
108 108
     if (cameraInfo !== null) {
109
-      Object.assign(data.camera, JSON.parse(cameraInfo))
109
+      Object.assign(restoredData.camera, JSON.parse(cameraInfo))
110 110
 
111 111
       // And update the CSS property
112 112
       document.documentElement.style.setProperty(
113 113
         "--camera-zoom",
114
-        data.camera.zoom.toString()
114
+        restoredData.camera.zoom.toString()
115 115
       )
116 116
     }
117 117
 

+ 7
- 0
state/state.ts Просмотреть файл

@@ -31,6 +31,7 @@ const initialData: Data = {
31 31
   hoveredId: null,
32 32
   selectedIds: new Set([]),
33 33
   currentPageId: "page0",
34
+  currentCodeFileId: "file0",
34 35
   document: defaultDocument,
35 36
 }
36 37
 
@@ -68,6 +69,7 @@ const state = createState({
68 69
         REDO: { do: "redo" },
69 70
         CANCELLED: { do: "clearSelectedIds" },
70 71
         DELETED: { do: "deleteSelectedIds" },
72
+        SAVED_CODE: "saveCode",
71 73
         GENERATED_SHAPES_FROM_CODE: "setGeneratedShapes",
72 74
         INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize",
73 75
         DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize",
@@ -502,6 +504,11 @@ const state = createState({
502 504
     },
503 505
 
504 506
     // Data
507
+    saveCode(data, payload: { code: string }) {
508
+      data.document.code[data.currentCodeFileId].code = payload.code
509
+      history.save(data)
510
+    },
511
+
505 512
     restoreSavedData(data) {
506 513
       history.load(data)
507 514
     },

+ 2
- 1
types.ts Просмотреть файл

@@ -13,10 +13,11 @@ export interface Data {
13 13
     zoom: number
14 14
   }
15 15
   brush?: Bounds
16
-  currentPageId: string
17 16
   selectedIds: Set<string>
18 17
   pointedId?: string
19 18
   hoveredId?: string
19
+  currentPageId: string
20
+  currentCodeFileId: string
20 21
   document: {
21 22
     pages: Record<string, Page>
22 23
     code: Record<string, CodeFile>

Загрузка…
Отмена
Сохранить