diff --git a/components/canvas/bounds.tsx b/components/canvas/bounds.tsx index 00d541fad..400f1e02a 100644 --- a/components/canvas/bounds.tsx +++ b/components/canvas/bounds.tsx @@ -137,7 +137,6 @@ function Corner({ rCorner.current.setPointerCapture(e.pointerId) state.send("POINTED_BOUNDS_CORNER", inputs.pointerDown(e, corner)) }} - onPointerCancelCapture={() => console.log("oops")} onPointerUp={(e) => { e.stopPropagation() rCorner.current.releasePointerCapture(e.pointerId) diff --git a/components/code-panel/code-panel.tsx b/components/code-panel/code-panel.tsx index 2bbac4fdc..a6580b525 100644 --- a/components/code-panel/code-panel.tsx +++ b/components/code-panel/code-panel.tsx @@ -30,10 +30,11 @@ const getErrorLineAndColumn = (e: any) => { export default function CodePanel() { const rContainer = useRef(null) - - const fileId = "file0" const isReadOnly = useSelector((s) => s.data.isReadOnly) - const file = useSelector((s) => s.data.document.code[fileId]) + const fileId = useSelector((s) => s.data.currentCodeFileId) + const file = useSelector( + (s) => s.data.document.code[s.data.currentCodeFileId] + ) const isOpen = true const fontSize = useSelector((s) => s.data.settings.fontSize) @@ -52,7 +53,7 @@ export default function CodePanel() { on: { RAN_CODE: "runCode", SAVED_CODE: ["runCode", "saveCode"], - CHANGED_CODE: [{ secretlyDo: "setCode" }], + CHANGED_CODE: { secretlyDo: "setCode" }, CLEARED_ERROR: { if: "hasError", do: "clearError" }, TOGGLED_DOCS: { to: "viewingDocs" }, }, @@ -89,7 +90,8 @@ export default function CodePanel() { data.error = error }, saveCode(data) { - state.send("CHANGED_CODE", { fileId, code: data.code }) + const { code } = data + state.send("SAVED_CODE", { code }) }, clearError(data) { data.error = null diff --git a/components/editor.tsx b/components/editor.tsx index f65b881d1..847f894e7 100644 --- a/components/editor.tsx +++ b/components/editor.tsx @@ -14,7 +14,7 @@ export default function Editor() { - {/* */} + ) } diff --git a/lib/code/generate.ts b/lib/code/generate.ts index aa4f3d378..c770ab85e 100644 --- a/lib/code/generate.ts +++ b/lib/code/generate.ts @@ -3,12 +3,23 @@ import Circle from "./circle" import Ellipse from "./ellipse" import Polyline from "./polyline" import Dot from "./dot" +import Ray from "./ray" import Line from "./line" import Vector from "./vector" import Utils from "./utils" import { codeShapes } from "./index" -const scope = { Dot, Circle, Ellipse, Line, Polyline, Rectangle, Vector, Utils } +const scope = { + Dot, + Circle, + Ellipse, + Ray, + Line, + Polyline, + Rectangle, + Vector, + Utils, +} /** * Evaluate code, collecting generated shapes in the shape set. Return the diff --git a/lib/code/ray.ts b/lib/code/ray.ts new file mode 100644 index 000000000..cc5da9a67 --- /dev/null +++ b/lib/code/ray.ts @@ -0,0 +1,25 @@ +import CodeShape from "./index" +import { v4 as uuid } from "uuid" +import { RayShape, ShapeType } from "types" + +export default class Ray extends CodeShape { + constructor(props = {} as Partial) { + super({ + id: uuid(), + type: ShapeType.Ray, + isGenerated: false, + name: "Ray", + parentId: "page0", + childIndex: 0, + point: [0, 0], + direction: [0, 1], + rotation: 0, + style: { + fill: "#777", + stroke: "#000", + strokeWidth: 1, + }, + ...props, + }) + } +} diff --git a/lib/code/vector.ts b/lib/code/vector.ts index 9324b3602..f82f1a131 100644 --- a/lib/code/vector.ts +++ b/lib/code/vector.ts @@ -274,16 +274,12 @@ export default class Vector { lrp(b: Vector, t: number) { const n = new Vector(this) - this.vec(b) - .mul(t) - .add(n) + this.vec(b).mul(t).add(n) } static lrp(a: Vector, b: Vector, t: number) { const n = new Vector(a) - n.vec(b) - .mul(t) - .add(a) + n.vec(b).mul(t).add(a) return n } @@ -390,6 +386,14 @@ export default class Vector { return n.div(n.len()) } + normalize() { + return this.uni() + } + + static normalize(v: Vector) { + return Vector.uni(v) + } + isLeft(center: Vector, b: Vector) { return ( (center.x - this.x) * (b.y - this.y) - (b.x - this.x) * (center.y - b.y) diff --git a/state/history.ts b/state/history.ts index 69a304afc..03d6e16bb 100644 --- a/state/history.ts +++ b/state/history.ts @@ -106,12 +106,12 @@ class History extends BaseHistory { const cameraInfo = localStorage.getItem("code_slate_camera") if (cameraInfo !== null) { - Object.assign(data.camera, JSON.parse(cameraInfo)) + Object.assign(restoredData.camera, JSON.parse(cameraInfo)) // And update the CSS property document.documentElement.style.setProperty( "--camera-zoom", - data.camera.zoom.toString() + restoredData.camera.zoom.toString() ) } diff --git a/state/state.ts b/state/state.ts index c26094eaf..90ecabd69 100644 --- a/state/state.ts +++ b/state/state.ts @@ -31,6 +31,7 @@ const initialData: Data = { hoveredId: null, selectedIds: new Set([]), currentPageId: "page0", + currentCodeFileId: "file0", document: defaultDocument, } @@ -68,6 +69,7 @@ const state = createState({ REDO: { do: "redo" }, CANCELLED: { do: "clearSelectedIds" }, DELETED: { do: "deleteSelectedIds" }, + SAVED_CODE: "saveCode", GENERATED_SHAPES_FROM_CODE: "setGeneratedShapes", INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize", DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize", @@ -502,6 +504,11 @@ const state = createState({ }, // Data + saveCode(data, payload: { code: string }) { + data.document.code[data.currentCodeFileId].code = payload.code + history.save(data) + }, + restoreSavedData(data) { history.load(data) }, diff --git a/types.ts b/types.ts index 7709505c7..239517b92 100644 --- a/types.ts +++ b/types.ts @@ -13,10 +13,11 @@ export interface Data { zoom: number } brush?: Bounds - currentPageId: string selectedIds: Set pointedId?: string hoveredId?: string + currentPageId: string + currentCodeFileId: string document: { pages: Record code: Record