Tldraw/components/code-panel/code-panel.tsx

206 wiersze
5.5 KiB
TypeScript
Czysty Zwykły widok Historia

2021-05-14 22:56:41 +00:00
/* eslint-disable @typescript-eslint/ban-ts-comment */
import styled from 'styles'
import { useStateDesigner } from '@state-designer/react'
import React, { useEffect, useRef } from 'react'
import { motion } from 'framer-motion'
import state, { useSelector } from 'state'
import { CodeFile } from 'types'
import CodeDocs from './code-docs'
import CodeEditor from './code-editor'
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'
2021-05-14 22:56:41 +00:00
const getErrorLineAndColumn = (e: any) => {
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] }
}
}
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',
2021-05-14 22:56:41 +00:00
},
initial: 'editingCode',
2021-05-14 22:56:41 +00:00
states: {
editingCode: {
on: {
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: {
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-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 })
2021-05-14 22:56:41 +00:00
}, [file])
useEffect(() => {
local.send('MOUNTED', { code: state.data.document.code[fileId].code })
2021-05-14 22:56:41 +00:00
return () => {
state.send('CHANGED_CODE', { fileId, code: local.data.code })
2021-05-14 22:56:41 +00:00
}
}, [])
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 side="left">
<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')}
2021-05-14 22:56:41 +00:00
>
<ChevronUp />
</IconButton>
<IconButton
disabled={!local.isIn('editingCode')}
onClick={() => state.send('DECREASED_CODE_FONT_SIZE')}
2021-05-14 22:56:41 +00:00
>
<ChevronDown />
</IconButton>
</FontSizeButtons>
<IconButton onClick={() => local.send('TOGGLED_DOCS')}>
2021-05-14 22:56:41 +00:00
<Info />
</IconButton>
<IconButton
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}
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
/>
<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
) : (
<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',
2021-05-14 22:56:41 +00:00
})
const FontSizeButtons = styled('div', {
2021-05-14 22:56:41 +00:00
paddingRight: 4,
display: 'flex',
flexDirection: 'column',
2021-05-14 22:56:41 +00:00
'& > button': {
height: '50%',
'&:nth-of-type(1)': {
alignItems: 'flex-end',
2021-05-14 22:56:41 +00:00
},
'&:nth-of-type(2)': {
alignItems: 'flex-start',
2021-05-14 22:56:41 +00:00
},
'& svg': {
2021-05-14 22:56:41 +00:00
height: 12,
},
},
})