2021-05-14 22:56:41 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2021-05-29 10:12:28 +00:00
|
|
|
import styled from 'styles'
|
|
|
|
import { useStateDesigner } from '@state-designer/react'
|
|
|
|
import React, { useEffect, useRef } from 'react'
|
|
|
|
import state, { useSelector } from 'state'
|
|
|
|
import { CodeFile } from 'types'
|
|
|
|
import CodeDocs from './code-docs'
|
|
|
|
import CodeEditor from './code-editor'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { generateFromCode } from 'state/code/generate'
|
2021-05-29 10:12:28 +00:00
|
|
|
import * as Panel from '../panel'
|
|
|
|
import { IconButton } from '../shared'
|
2021-05-14 22:56:41 +00:00
|
|
|
import {
|
|
|
|
X,
|
|
|
|
Code,
|
|
|
|
Info,
|
|
|
|
PlayCircle,
|
|
|
|
ChevronUp,
|
|
|
|
ChevronDown,
|
2021-05-29 10:12:28 +00:00
|
|
|
} from 'react-feather'
|
2021-05-14 22:56:41 +00:00
|
|
|
|
|
|
|
const getErrorLineAndColumn = (e: any) => {
|
2021-05-29 10:12:28 +00:00
|
|
|
if ('line' in e) {
|
2021-05-14 22:56:41 +00:00
|
|
|
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] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function CodePanel(): JSX.Element {
|
2021-05-14 22:56:41 +00:00
|
|
|
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: {
|
2021-05-29 10:12:28 +00:00
|
|
|
MOUNTED: 'setCode',
|
|
|
|
CHANGED_FILE: 'loadFile',
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
2021-05-29 10:12:28 +00:00
|
|
|
initial: 'editingCode',
|
2021-05-14 22:56:41 +00:00
|
|
|
states: {
|
|
|
|
editingCode: {
|
|
|
|
on: {
|
2021-05-29 10:12:28 +00:00
|
|
|
RAN_CODE: ['saveCode', 'runCode'],
|
|
|
|
SAVED_CODE: ['saveCode', 'runCode'],
|
|
|
|
CHANGED_CODE: { secretlyDo: 'setCode' },
|
|
|
|
CLEARED_ERROR: { if: 'hasError', do: 'clearError' },
|
|
|
|
TOGGLED_DOCS: { to: 'viewingDocs' },
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
viewingDocs: {
|
|
|
|
on: {
|
2021-05-29 10:12:28 +00:00
|
|
|
TOGGLED_DOCS: { to: 'editingCode' },
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-06-08 10:32:20 +00:00
|
|
|
const { shapes, controls } = generateFromCode(state.data, data.code)
|
2021-05-29 10:12:28 +00:00
|
|
|
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
|
2021-05-29 10:12:28 +00:00
|
|
|
state.send('SAVED_CODE', { code })
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
clearError(data) {
|
|
|
|
data.error = null
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-05-29 10:12:28 +00:00
|
|
|
local.send('CHANGED_FILE', { file })
|
2021-05-14 22:56:41 +00:00
|
|
|
}, [file])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-05-29 10:12:28 +00:00
|
|
|
local.send('MOUNTED', { code: state.data.document.code[fileId].code })
|
2021-05-14 22:56:41 +00:00
|
|
|
return () => {
|
2021-05-29 10:12:28 +00:00
|
|
|
state.send('CHANGED_CODE', { fileId, code: local.data.code })
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const { error } = local.data
|
|
|
|
|
|
|
|
return (
|
2021-06-03 16:13:23 +00:00
|
|
|
<Panel.Root
|
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
|
|
|
data-bp-desktop
|
|
|
|
ref={rContainer}
|
|
|
|
isOpen={isOpen}
|
|
|
|
variant="code"
|
|
|
|
>
|
2021-05-14 22:56:41 +00:00
|
|
|
{isOpen ? (
|
2021-05-17 10:01:11 +00:00
|
|
|
<Panel.Layout>
|
2021-05-29 10:12:28 +00:00
|
|
|
<Panel.Header side="left">
|
2021-06-02 11:50:34 +00:00
|
|
|
<IconButton
|
2021-06-03 16:13:23 +00:00
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
2021-06-02 11:50:34 +00:00
|
|
|
size="small"
|
|
|
|
onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
|
|
|
|
>
|
2021-05-14 22:56:41 +00:00
|
|
|
<X />
|
|
|
|
</IconButton>
|
|
|
|
<h3>Code</h3>
|
|
|
|
<ButtonsGroup>
|
|
|
|
<FontSizeButtons>
|
|
|
|
<IconButton
|
2021-06-03 16:13:23 +00:00
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
2021-06-02 11:50:34 +00:00
|
|
|
size="small"
|
2021-05-29 10:12:28 +00:00
|
|
|
disabled={!local.isIn('editingCode')}
|
|
|
|
onClick={() => state.send('INCREASED_CODE_FONT_SIZE')}
|
2021-05-14 22:56:41 +00:00
|
|
|
>
|
|
|
|
<ChevronUp />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
2021-06-02 11:50:34 +00:00
|
|
|
size="small"
|
2021-05-29 10:12:28 +00:00
|
|
|
disabled={!local.isIn('editingCode')}
|
|
|
|
onClick={() => state.send('DECREASED_CODE_FONT_SIZE')}
|
2021-05-14 22:56:41 +00:00
|
|
|
>
|
|
|
|
<ChevronDown />
|
|
|
|
</IconButton>
|
|
|
|
</FontSizeButtons>
|
2021-06-02 11:50:34 +00:00
|
|
|
<IconButton
|
2021-06-03 16:13:23 +00:00
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
2021-06-02 11:50:34 +00:00
|
|
|
size="small"
|
|
|
|
onClick={() => local.send('TOGGLED_DOCS')}
|
|
|
|
>
|
2021-05-14 22:56:41 +00:00
|
|
|
<Info />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
2021-06-03 16:13:23 +00:00
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
2021-06-02 11:50:34 +00:00
|
|
|
size="small"
|
2021-05-29 10:12:28 +00:00
|
|
|
disabled={!local.isIn('editingCode')}
|
|
|
|
onClick={() => local.send('SAVED_CODE')}
|
2021-05-14 22:56:41 +00:00
|
|
|
>
|
|
|
|
<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}
|
2021-05-29 10:12:28 +00:00
|
|
|
onChange={(code) => local.send('CHANGED_CODE', { code })}
|
|
|
|
onSave={() => local.send('SAVED_CODE')}
|
|
|
|
onKey={() => local.send('CLEARED_ERROR')}
|
2021-05-14 22:56:41 +00:00
|
|
|
/>
|
2021-05-29 10:12:28 +00:00
|
|
|
<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-06-02 11:50:34 +00:00
|
|
|
<IconButton
|
2021-06-03 16:13:23 +00:00
|
|
|
bp={{ '@initial': 'mobile', '@sm': 'small' }}
|
2021-06-02 11:50:34 +00:00
|
|
|
size="small"
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
const ButtonsGroup = styled('div', {
|
|
|
|
gridRow: '1',
|
|
|
|
gridColumn: '3',
|
|
|
|
display: 'flex',
|
2021-05-14 22:56:41 +00:00
|
|
|
})
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
const FontSizeButtons = styled('div', {
|
2021-05-14 22:56:41 +00:00
|
|
|
paddingRight: 4,
|
2021-05-29 10:12:28 +00:00
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
2021-05-14 22:56:41 +00:00
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
'& > button': {
|
|
|
|
height: '50%',
|
|
|
|
'&:nth-of-type(1)': {
|
|
|
|
alignItems: 'flex-end',
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
'&:nth-of-type(2)': {
|
|
|
|
alignItems: 'flex-start',
|
2021-05-14 22:56:41 +00:00
|
|
|
},
|
|
|
|
|
2021-05-29 10:12:28 +00:00
|
|
|
'& svg': {
|
2021-05-14 22:56:41 +00:00
|
|
|
height: 12,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|