Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

code-panel.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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(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 data-bp-desktop ref={rContainer} isOpen={isOpen}>
  109. {isOpen ? (
  110. <Panel.Layout>
  111. <Panel.Header side="left">
  112. <IconButton
  113. size="small"
  114. onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
  115. >
  116. <X />
  117. </IconButton>
  118. <h3>Code</h3>
  119. <ButtonsGroup>
  120. <FontSizeButtons>
  121. <IconButton
  122. size="small"
  123. disabled={!local.isIn('editingCode')}
  124. onClick={() => state.send('INCREASED_CODE_FONT_SIZE')}
  125. >
  126. <ChevronUp />
  127. </IconButton>
  128. <IconButton
  129. size="small"
  130. disabled={!local.isIn('editingCode')}
  131. onClick={() => state.send('DECREASED_CODE_FONT_SIZE')}
  132. >
  133. <ChevronDown />
  134. </IconButton>
  135. </FontSizeButtons>
  136. <IconButton
  137. size="small"
  138. onClick={() => local.send('TOGGLED_DOCS')}
  139. >
  140. <Info />
  141. </IconButton>
  142. <IconButton
  143. size="small"
  144. disabled={!local.isIn('editingCode')}
  145. onClick={() => local.send('SAVED_CODE')}
  146. >
  147. <PlayCircle />
  148. </IconButton>
  149. </ButtonsGroup>
  150. </Panel.Header>
  151. <Panel.Content>
  152. <CodeEditor
  153. fontSize={fontSize}
  154. readOnly={isReadOnly}
  155. value={file.code}
  156. error={error}
  157. onChange={(code) => local.send('CHANGED_CODE', { code })}
  158. onSave={() => local.send('SAVED_CODE')}
  159. onKey={() => local.send('CLEARED_ERROR')}
  160. />
  161. <CodeDocs isHidden={!local.isIn('viewingDocs')} />
  162. </Panel.Content>
  163. <Panel.Footer>
  164. {error &&
  165. (error.line
  166. ? `(${Number(error.line) - 2}:${error.column}) ${error.message}`
  167. : error.message)}
  168. </Panel.Footer>
  169. </Panel.Layout>
  170. ) : (
  171. <IconButton
  172. size="small"
  173. onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
  174. >
  175. <Code />
  176. </IconButton>
  177. )}
  178. </Panel.Root>
  179. )
  180. }
  181. const ButtonsGroup = styled('div', {
  182. gridRow: '1',
  183. gridColumn: '3',
  184. display: 'flex',
  185. })
  186. const FontSizeButtons = styled('div', {
  187. paddingRight: 4,
  188. display: 'flex',
  189. flexDirection: 'column',
  190. '& > button': {
  191. height: '50%',
  192. '&:nth-of-type(1)': {
  193. alignItems: 'flex-end',
  194. },
  195. '&:nth-of-type(2)': {
  196. alignItems: 'flex-start',
  197. },
  198. '& svg': {
  199. height: 12,
  200. },
  201. },
  202. })