You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

code-panel.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* eslint-disable @typescript-eslint/ban-ts-comment */
  2. import styled from 'styles'
  3. import { useStateDesigner } from '@state-designer/react'
  4. import React, { useEffect, useRef } from 'react'
  5. import { motion } from 'framer-motion'
  6. import state, { useSelector } from 'state'
  7. import { CodeFile } from 'types'
  8. import CodeDocs from './code-docs'
  9. import CodeEditor from './code-editor'
  10. import { generateFromCode } from 'lib/code/generate'
  11. import * as Panel from '../panel'
  12. import { IconButton } from '../shared'
  13. import {
  14. X,
  15. Code,
  16. Info,
  17. PlayCircle,
  18. ChevronUp,
  19. ChevronDown,
  20. } from 'react-feather'
  21. const getErrorLineAndColumn = (e: any) => {
  22. if ('line' in e) {
  23. return { line: Number(e.line), column: e.column }
  24. }
  25. const result = e.stack.match(/:([0-9]+):([0-9]+)/)
  26. if (result) {
  27. return { line: Number(result[1]) - 1, column: result[2] }
  28. }
  29. }
  30. export default function CodePanel() {
  31. const rContainer = useRef<HTMLDivElement>(null)
  32. const isReadOnly = useSelector((s) => s.data.isReadOnly)
  33. const fileId = useSelector((s) => s.data.currentCodeFileId)
  34. const file = useSelector(
  35. (s) => s.data.document.code[s.data.currentCodeFileId]
  36. )
  37. const isOpen = useSelector((s) => s.data.settings.isCodeOpen)
  38. const fontSize = useSelector((s) => s.data.settings.fontSize)
  39. const local = useStateDesigner({
  40. data: {
  41. code: file.code,
  42. error: null as { message: string; line: number; column: number } | null,
  43. },
  44. on: {
  45. MOUNTED: 'setCode',
  46. CHANGED_FILE: 'loadFile',
  47. },
  48. initial: 'editingCode',
  49. states: {
  50. editingCode: {
  51. on: {
  52. RAN_CODE: ['saveCode', 'runCode'],
  53. SAVED_CODE: ['saveCode', 'runCode'],
  54. CHANGED_CODE: { secretlyDo: 'setCode' },
  55. CLEARED_ERROR: { if: 'hasError', do: 'clearError' },
  56. TOGGLED_DOCS: { to: 'viewingDocs' },
  57. },
  58. },
  59. viewingDocs: {
  60. on: {
  61. TOGGLED_DOCS: { to: 'editingCode' },
  62. },
  63. },
  64. },
  65. conditions: {
  66. hasError(data) {
  67. return !!data.error
  68. },
  69. },
  70. actions: {
  71. loadFile(data, payload: { file: CodeFile }) {
  72. data.code = payload.file.code
  73. },
  74. setCode(data, payload: { code: string }) {
  75. data.code = payload.code
  76. },
  77. runCode(data) {
  78. let error = null
  79. try {
  80. const { shapes, controls } = generateFromCode(state.data, data.code)
  81. state.send('GENERATED_FROM_CODE', { shapes, controls })
  82. } catch (e) {
  83. console.error(e)
  84. error = { message: e.message, ...getErrorLineAndColumn(e) }
  85. }
  86. data.error = error
  87. },
  88. saveCode(data) {
  89. const { code } = data
  90. state.send('SAVED_CODE', { code })
  91. },
  92. clearError(data) {
  93. data.error = null
  94. },
  95. },
  96. })
  97. useEffect(() => {
  98. local.send('CHANGED_FILE', { file })
  99. }, [file])
  100. useEffect(() => {
  101. local.send('MOUNTED', { code: state.data.document.code[fileId].code })
  102. return () => {
  103. state.send('CHANGED_CODE', { fileId, code: local.data.code })
  104. }
  105. }, [])
  106. const { error } = local.data
  107. return (
  108. <Panel.Root
  109. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  110. data-bp-desktop
  111. ref={rContainer}
  112. isOpen={isOpen}
  113. variant="code"
  114. >
  115. {isOpen ? (
  116. <Panel.Layout>
  117. <Panel.Header side="left">
  118. <IconButton
  119. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  120. size="small"
  121. onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
  122. >
  123. <X />
  124. </IconButton>
  125. <h3>Code</h3>
  126. <ButtonsGroup>
  127. <FontSizeButtons>
  128. <IconButton
  129. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  130. size="small"
  131. disabled={!local.isIn('editingCode')}
  132. onClick={() => state.send('INCREASED_CODE_FONT_SIZE')}
  133. >
  134. <ChevronUp />
  135. </IconButton>
  136. <IconButton
  137. size="small"
  138. disabled={!local.isIn('editingCode')}
  139. onClick={() => state.send('DECREASED_CODE_FONT_SIZE')}
  140. >
  141. <ChevronDown />
  142. </IconButton>
  143. </FontSizeButtons>
  144. <IconButton
  145. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  146. size="small"
  147. onClick={() => local.send('TOGGLED_DOCS')}
  148. >
  149. <Info />
  150. </IconButton>
  151. <IconButton
  152. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  153. size="small"
  154. disabled={!local.isIn('editingCode')}
  155. onClick={() => local.send('SAVED_CODE')}
  156. >
  157. <PlayCircle />
  158. </IconButton>
  159. </ButtonsGroup>
  160. </Panel.Header>
  161. <Panel.Content>
  162. <CodeEditor
  163. fontSize={fontSize}
  164. readOnly={isReadOnly}
  165. value={file.code}
  166. error={error}
  167. onChange={(code) => local.send('CHANGED_CODE', { code })}
  168. onSave={() => local.send('SAVED_CODE')}
  169. onKey={() => local.send('CLEARED_ERROR')}
  170. />
  171. <CodeDocs isHidden={!local.isIn('viewingDocs')} />
  172. </Panel.Content>
  173. <Panel.Footer>
  174. {error &&
  175. (error.line
  176. ? `(${Number(error.line) - 2}:${error.column}) ${error.message}`
  177. : error.message)}
  178. </Panel.Footer>
  179. </Panel.Layout>
  180. ) : (
  181. <IconButton
  182. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  183. size="small"
  184. onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
  185. >
  186. <Code />
  187. </IconButton>
  188. )}
  189. </Panel.Root>
  190. )
  191. }
  192. const ButtonsGroup = styled('div', {
  193. gridRow: '1',
  194. gridColumn: '3',
  195. display: 'flex',
  196. })
  197. const FontSizeButtons = styled('div', {
  198. paddingRight: 4,
  199. display: 'flex',
  200. flexDirection: 'column',
  201. '& > button': {
  202. height: '50%',
  203. '&:nth-of-type(1)': {
  204. alignItems: 'flex-end',
  205. },
  206. '&:nth-of-type(2)': {
  207. alignItems: 'flex-start',
  208. },
  209. '& svg': {
  210. height: 12,
  211. },
  212. },
  213. })