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.7KB

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