Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

code-panel.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 state, { useSelector } from 'state'
  6. import { CodeFile } from 'types'
  7. import CodeDocs from './code-docs'
  8. import { generateFromCode } from 'state/code/generate'
  9. import * as Panel from '../panel'
  10. import { IconButton } from '../shared'
  11. import {
  12. X,
  13. Code,
  14. Info,
  15. PlayCircle,
  16. ChevronUp,
  17. ChevronDown,
  18. } from 'react-feather'
  19. import dynamic from 'next/dynamic'
  20. const CodeEditor = dynamic(() => import('./code-editor'))
  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(): JSX.Element {
  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. generateFromCode(state.data, data.code).then(
  81. ({ shapes, controls }) => {
  82. state.send('GENERATED_FROM_CODE', { shapes, controls })
  83. }
  84. )
  85. } catch (e) {
  86. console.error('Got an error!', e)
  87. error = { message: e.message, ...getErrorLineAndColumn(e) }
  88. }
  89. data.error = error
  90. },
  91. saveCode(data) {
  92. const { code } = data
  93. state.send('SAVED_CODE', { code })
  94. },
  95. clearError(data) {
  96. data.error = null
  97. },
  98. },
  99. })
  100. useEffect(() => {
  101. local.send('CHANGED_FILE', { file })
  102. }, [file])
  103. useEffect(() => {
  104. local.send('MOUNTED', { code: state.data.document.code[fileId].code })
  105. return () => {
  106. state.send('CHANGED_CODE', { fileId, code: local.data.code })
  107. }
  108. }, [])
  109. const { error } = local.data
  110. return (
  111. <Panel.Root
  112. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  113. data-bp-desktop
  114. ref={rContainer}
  115. isOpen={isOpen}
  116. variant="code"
  117. >
  118. {isOpen ? (
  119. <Panel.Layout>
  120. <Panel.Header side="left">
  121. <IconButton
  122. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  123. size="small"
  124. onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
  125. >
  126. <X />
  127. </IconButton>
  128. <h3>Code</h3>
  129. <ButtonsGroup>
  130. <FontSizeButtons>
  131. <IconButton
  132. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  133. size="small"
  134. disabled={!local.isIn('editingCode')}
  135. onClick={() => state.send('INCREASED_CODE_FONT_SIZE')}
  136. >
  137. <ChevronUp />
  138. </IconButton>
  139. <IconButton
  140. size="small"
  141. disabled={!local.isIn('editingCode')}
  142. onClick={() => state.send('DECREASED_CODE_FONT_SIZE')}
  143. >
  144. <ChevronDown />
  145. </IconButton>
  146. </FontSizeButtons>
  147. <IconButton
  148. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  149. size="small"
  150. onClick={() => local.send('TOGGLED_DOCS')}
  151. >
  152. <Info />
  153. </IconButton>
  154. <IconButton
  155. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  156. size="small"
  157. disabled={!local.isIn('editingCode')}
  158. onClick={() => local.send('SAVED_CODE')}
  159. >
  160. <PlayCircle />
  161. </IconButton>
  162. </ButtonsGroup>
  163. </Panel.Header>
  164. <Panel.Content>
  165. <CodeEditor
  166. fontSize={fontSize}
  167. readOnly={isReadOnly}
  168. value={file.code}
  169. error={error}
  170. onChange={(code) => local.send('CHANGED_CODE', { code })}
  171. onSave={() => local.send('SAVED_CODE')}
  172. onKey={() => local.send('CLEARED_ERROR')}
  173. />
  174. <CodeDocs isHidden={!local.isIn('viewingDocs')} />
  175. </Panel.Content>
  176. <Panel.Footer>
  177. {error &&
  178. (error.line
  179. ? `(${Number(error.line) - 2}:${error.column}) ${error.message}`
  180. : error.message)}
  181. </Panel.Footer>
  182. </Panel.Layout>
  183. ) : (
  184. <IconButton
  185. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  186. size="small"
  187. onClick={() => state.send('TOGGLED_CODE_PANEL_OPEN')}
  188. >
  189. <Code />
  190. </IconButton>
  191. )}
  192. </Panel.Root>
  193. )
  194. }
  195. const ButtonsGroup = styled('div', {
  196. gridRow: '1',
  197. gridColumn: '3',
  198. display: 'flex',
  199. })
  200. const FontSizeButtons = styled('div', {
  201. paddingRight: 4,
  202. display: 'flex',
  203. flexDirection: 'column',
  204. '& > button': {
  205. height: '50%',
  206. '&:nth-of-type(1)': {
  207. alignItems: 'flex-end',
  208. },
  209. '&:nth-of-type(2)': {
  210. alignItems: 'flex-start',
  211. },
  212. '& svg': {
  213. height: 12,
  214. },
  215. },
  216. })