Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

code-panel.tsx 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  123. data-bp-desktop
  124. ref={rContainer}
  125. isOpen={isOpen}
  126. variant="code"
  127. >
  128. {isOpen ? (
  129. <Panel.Layout>
  130. <Panel.Header side="left">
  131. <IconButton
  132. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  133. size="small"
  134. onClick={toggleCodePanel}
  135. >
  136. <X />
  137. </IconButton>
  138. <h3>Code</h3>
  139. <ButtonsGroup>
  140. <FontSizeButtons>
  141. <IconButton
  142. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  143. size="small"
  144. disabled={!local.isIn('editingCode')}
  145. onClick={increaseCodeSize}
  146. >
  147. <ChevronUp />
  148. </IconButton>
  149. <IconButton
  150. size="small"
  151. disabled={!local.isIn('editingCode')}
  152. onClick={decreaseCodeSize}
  153. >
  154. <ChevronDown />
  155. </IconButton>
  156. </FontSizeButtons>
  157. <IconButton
  158. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  159. size="small"
  160. onClick={toggleDocs}
  161. >
  162. <ReaderIcon />
  163. </IconButton>
  164. <IconButton
  165. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  166. size="small"
  167. disabled={!local.isIn('editingCode')}
  168. onClick={handleSave}
  169. >
  170. <PlayCircle />
  171. </IconButton>
  172. </ButtonsGroup>
  173. </Panel.Header>
  174. <Panel.Content>
  175. <CodeEditor
  176. fontSize={fontSize}
  177. readOnly={isReadOnly}
  178. value={file.code}
  179. error={error}
  180. onChange={handleCodeChange}
  181. onSave={handleSave}
  182. onKey={handleKey}
  183. />
  184. <CodeDocs isHidden={!local.isIn('viewingDocs')} />
  185. </Panel.Content>
  186. {error && <Panel.Footer>{error.message}</Panel.Footer>}
  187. </Panel.Layout>
  188. ) : (
  189. <IconButton
  190. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  191. size="small"
  192. onClick={toggleCodePanel}
  193. >
  194. <Code />
  195. </IconButton>
  196. )}
  197. </Panel.Root>
  198. )
  199. }
  200. const ButtonsGroup = styled('div', {
  201. gridRow: '1',
  202. gridColumn: '3',
  203. display: 'flex',
  204. })
  205. const FontSizeButtons = styled('div', {
  206. paddingRight: 4,
  207. display: 'flex',
  208. flexDirection: 'column',
  209. '& > button': {
  210. height: '50%',
  211. '&:nth-of-type(1)': {
  212. alignItems: 'flex-end',
  213. },
  214. '&:nth-of-type(2)': {
  215. alignItems: 'flex-start',
  216. },
  217. '& svg': {
  218. height: 12,
  219. },
  220. },
  221. })