2021-05-14 22:56:41 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2021-05-15 13:02:13 +00:00
|
|
|
import styled from "styles"
|
|
|
|
import { useStateDesigner } from "@state-designer/react"
|
2021-05-14 22:56:41 +00:00
|
|
|
import React, { useEffect, useRef } from "react"
|
|
|
|
import { motion } from "framer-motion"
|
2021-05-15 13:02:13 +00:00
|
|
|
import state, { useSelector } from "state"
|
2021-05-14 22:56:41 +00:00
|
|
|
import { CodeFile } from "types"
|
|
|
|
import CodeDocs from "./code-docs"
|
|
|
|
import CodeEditor from "./code-editor"
|
2021-05-17 10:01:11 +00:00
|
|
|
import { generateFromCode } from "lib/code/generate"
|
|
|
|
import * as Panel from "../panel"
|
|
|
|
import { IconButton } from "../shared"
|
2021-05-14 22:56:41 +00:00
|
|
|
import {
|
|
|
|
X,
|
|
|
|
Code,
|
|
|
|
Info,
|
|
|
|
PlayCircle,
|
|
|
|
ChevronUp,
|
|
|
|
ChevronDown,
|
|
|
|
} from "react-feather"
|
|
|
|
|
|
|
|
const getErrorLineAndColumn = (e: any) => {
|
|
|
|
if ("line" in e) {
|
|
|
|
return { line: Number(e.line), column: e.column }
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = e.stack.match(/:([0-9]+):([0-9]+)/)
|
|
|
|
if (result) {
|
|
|
|
return { line: Number(result[1]) - 1, column: result[2] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function CodePanel() {
|
|
|
|
const rContainer = useRef<HTMLDivElement>(null)
|
|
|
|
const isReadOnly = useSelector((s) => s.data.isReadOnly)
|
2021-05-16 08:33:08 +00:00
|
|
|
const fileId = useSelector((s) => s.data.currentCodeFileId)
|
|
|
|
const file = useSelector(
|
|
|
|
(s) => s.data.document.code[s.data.currentCodeFileId]
|
|
|
|
)
|
2021-05-17 21:27:18 +00:00
|
|
|
const isOpen = useSelector((s) => s.data.settings.isCodeOpen)
|
2021-05-14 22:56:41 +00:00
|
|
|
const fontSize = useSelector((s) => s.data.settings.fontSize)
|
|
|
|
|
|
|
|
const local = useStateDesigner({
|
|
|
|
data: {
|
|
|
|
code: file.code,
|
|
|
|
error: null as { message: string; line: number; column: number } | null,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
MOUNTED: "setCode",
|
|
|
|
CHANGED_FILE: "loadFile",
|
|
|
|
},
|
|
|
|
initial: "editingCode",
|
|
|
|
states: {
|
|
|
|
editingCode: {
|
|
|
|
on: {
|
2021-05-17 10:01:11 +00:00
|
|
|
RAN_CODE: ["saveCode", "runCode"],
|
|
|
|
SAVED_CODE: ["saveCode", "runCode"],
|
2021-05-16 08:33:08 +00:00
|
|
|
CHANGED_CODE: { secretlyDo: "setCode" },
|
2021-05-14 22:56:41 +00:00
|
|
|
CLEARED_ERROR: { if: "hasError", do: "clearError" },
|
|
|
|
TOGGLED_DOCS: { to: "viewingDocs" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
viewingDocs: {
|
|
|
|
on: {
|
|
|
|
TOGGLED_DOCS: { to: "editingCode" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
conditions: {
|
|
|
|
hasError(data) {
|
|
|
|
return !!data.error
|
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
loadFile(data, payload: { file: CodeFile }) {
|
|
|
|
data.code = payload.file.code
|
|
|
|
},
|
|
|
|
setCode(data, payload: { code: string }) {
|
|
|
|
data.code = payload.code
|
|
|
|
},
|
|
|
|
runCode(data) {
|
|
|
|
let error = null
|
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
try {
|
2021-05-17 10:01:11 +00:00
|
|
|
const { shapes, controls } = generateFromCode(data.code)
|
|
|
|
state.send("GENERATED_FROM_CODE", { shapes, controls })
|
2021-05-15 13:02:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
error = { message: e.message, ...getErrorLineAndColumn(e) }
|
|
|
|
}
|
2021-05-14 22:56:41 +00:00
|
|
|
|
|
|
|
data.error = error
|
|
|
|
},
|
|
|
|
saveCode(data) {
|
2021-05-16 08:33:08 +00:00
|
|
|
const { code } = data
|
|
|
|
state.send("SAVED_CODE", { code })
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
clearError(data) {
|
|
|
|
data.error = null
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
local.send("CHANGED_FILE", { file })
|
|
|
|
}, [file])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
local.send("MOUNTED", { code: state.data.document.code[fileId].code })
|
|
|
|
return () => {
|
|
|
|
state.send("CHANGED_CODE", { fileId, code: local.data.code })
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const { error } = local.data
|
|
|
|
|
|
|
|
return (
|
2021-05-17 21:27:18 +00:00
|
|
|
<Panel.Root data-bp-desktop ref={rContainer} isOpen={isOpen}>
|
2021-05-14 22:56:41 +00:00
|
|
|
{isOpen ? (
|
2021-05-17 10:01:11 +00:00
|
|
|
<Panel.Layout>
|
|
|
|
<Panel.Header>
|
2021-05-17 21:27:18 +00:00
|
|
|
<IconButton onClick={() => state.send("TOGGLED_CODE_PANEL_OPEN")}>
|
2021-05-14 22:56:41 +00:00
|
|
|
<X />
|
|
|
|
</IconButton>
|
|
|
|
<h3>Code</h3>
|
|
|
|
<ButtonsGroup>
|
|
|
|
<FontSizeButtons>
|
|
|
|
<IconButton
|
|
|
|
disabled={!local.isIn("editingCode")}
|
|
|
|
onClick={() => state.send("INCREASED_CODE_FONT_SIZE")}
|
|
|
|
>
|
|
|
|
<ChevronUp />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!local.isIn("editingCode")}
|
|
|
|
onClick={() => state.send("DECREASED_CODE_FONT_SIZE")}
|
|
|
|
>
|
|
|
|
<ChevronDown />
|
|
|
|
</IconButton>
|
|
|
|
</FontSizeButtons>
|
|
|
|
<IconButton onClick={() => local.send("TOGGLED_DOCS")}>
|
|
|
|
<Info />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
disabled={!local.isIn("editingCode")}
|
|
|
|
onClick={() => local.send("SAVED_CODE")}
|
|
|
|
>
|
|
|
|
<PlayCircle />
|
|
|
|
</IconButton>
|
|
|
|
</ButtonsGroup>
|
2021-05-17 10:01:11 +00:00
|
|
|
</Panel.Header>
|
|
|
|
<Panel.Content>
|
2021-05-14 22:56:41 +00:00
|
|
|
<CodeEditor
|
|
|
|
fontSize={fontSize}
|
|
|
|
readOnly={isReadOnly}
|
|
|
|
value={file.code}
|
|
|
|
error={error}
|
|
|
|
onChange={(code) => local.send("CHANGED_CODE", { code })}
|
|
|
|
onSave={() => local.send("SAVED_CODE")}
|
|
|
|
onKey={() => local.send("CLEARED_ERROR")}
|
|
|
|
/>
|
|
|
|
<CodeDocs isHidden={!local.isIn("viewingDocs")} />
|
2021-05-17 10:01:11 +00:00
|
|
|
</Panel.Content>
|
|
|
|
<Panel.Footer>
|
2021-05-14 22:56:41 +00:00
|
|
|
{error &&
|
|
|
|
(error.line
|
|
|
|
? `(${Number(error.line) - 2}:${error.column}) ${error.message}`
|
|
|
|
: error.message)}
|
2021-05-17 10:01:11 +00:00
|
|
|
</Panel.Footer>
|
|
|
|
</Panel.Layout>
|
2021-05-14 22:56:41 +00:00
|
|
|
) : (
|
2021-05-17 21:27:18 +00:00
|
|
|
<IconButton onClick={() => state.send("TOGGLED_CODE_PANEL_OPEN")}>
|
2021-05-14 22:56:41 +00:00
|
|
|
<Code />
|
|
|
|
</IconButton>
|
|
|
|
)}
|
2021-05-17 10:01:11 +00:00
|
|
|
</Panel.Root>
|
2021-05-14 22:56:41 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const ButtonsGroup = styled("div", {
|
|
|
|
gridRow: "1",
|
|
|
|
gridColumn: "3",
|
|
|
|
display: "flex",
|
|
|
|
})
|
|
|
|
|
|
|
|
const FontSizeButtons = styled("div", {
|
|
|
|
paddingRight: 4,
|
2021-05-17 10:01:11 +00:00
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
2021-05-14 22:56:41 +00:00
|
|
|
|
|
|
|
"& > button": {
|
|
|
|
height: "50%",
|
|
|
|
"&:nth-of-type(1)": {
|
2021-05-17 10:01:11 +00:00
|
|
|
alignItems: "flex-end",
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"&:nth-of-type(2)": {
|
2021-05-17 10:01:11 +00:00
|
|
|
alignItems: "flex-start",
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"& svg": {
|
|
|
|
height: 12,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|