|
1 | | -import React, { useEffect, useState } from 'react'; |
| 1 | +import React, { useCallback, useEffect, useState } from 'react'; |
2 | 2 | import useRouter from 'use-react-router'; |
3 | 3 | import { ServerContext } from '@/state/server'; |
4 | 4 | import getFileContents from '@/api/server/files/getFileContents'; |
| 5 | +import ace, { Editor } from 'brace'; |
| 6 | +import styled from 'styled-components'; |
| 7 | + |
| 8 | +const EditorContainer = styled.div` |
| 9 | + height: calc(100vh - 16rem); |
| 10 | + ${tw`relative`}; |
| 11 | + |
| 12 | + #editor { |
| 13 | + ${tw`rounded h-full`}; |
| 14 | + } |
| 15 | +`; |
| 16 | + |
| 17 | +const modes: { [k: string]: string } = { |
| 18 | + // eslint-disable-next-line @typescript-eslint/camelcase |
| 19 | + assembly_x86: 'Assembly (x86)', |
| 20 | + // eslint-disable-next-line @typescript-eslint/camelcase |
| 21 | + c_cpp: 'C++', |
| 22 | + coffee: 'Coffeescript', |
| 23 | + css: 'CSS', |
| 24 | + dockerfile: 'Dockerfile', |
| 25 | + golang: 'Go', |
| 26 | + html: 'HTML', |
| 27 | + ini: 'Ini', |
| 28 | + java: 'Java', |
| 29 | + javascript: 'Javascript', |
| 30 | + json: 'JSON', |
| 31 | + kotlin: 'Kotlin', |
| 32 | + lua: 'Luascript', |
| 33 | + perl: 'Perl', |
| 34 | + php: 'PHP', |
| 35 | + properties: 'Properties', |
| 36 | + python: 'Python', |
| 37 | + ruby: 'Ruby', |
| 38 | + text: 'Plaintext', |
| 39 | + toml: 'TOML', |
| 40 | + typescript: 'Typescript', |
| 41 | + xml: 'XML', |
| 42 | + yaml: 'YAML', |
| 43 | +}; |
5 | 44 |
|
6 | 45 | export default () => { |
7 | 46 | const { location: { hash } } = useRouter(); |
8 | | - const [content, setContent] = useState(''); |
| 47 | + const [ content, setContent ] = useState(''); |
9 | 48 | const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); |
10 | 49 |
|
| 50 | + const [ editor, setEditor ] = useState<Editor>(); |
| 51 | + const ref = useCallback(node => { |
| 52 | + if (node) { |
| 53 | + setEditor(ace.edit('editor')); |
| 54 | + } |
| 55 | + }, []); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + Object.keys(modes).forEach(mode => { |
| 59 | + import(/* webpackMode: "lazy-once", webpackChunkName: "ace_mode" */`brace/mode/${mode}`); |
| 60 | + }); |
| 61 | + }, []); |
| 62 | + |
11 | 63 | useEffect(() => { |
12 | 64 | getFileContents(uuid, hash.replace(/^#/, '')) |
13 | 65 | .then(setContent) |
14 | 66 | .catch(error => console.error(error)); |
15 | | - }, []); |
| 67 | + }, [ uuid, hash ]); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + editor && editor.session.setValue(content); |
| 71 | + }, [ editor, content ]); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + if (!editor) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + require('ayu-ace/mirage'); |
| 79 | + editor.setTheme('ace/theme/ayu-mirage'); |
| 80 | + |
| 81 | + editor.$blockScrolling = Infinity; |
| 82 | + editor.container.style.lineHeight = '1.375rem'; |
| 83 | + editor.container.style.fontWeight = '500'; |
| 84 | + editor.renderer.updateFontSize(); |
| 85 | + editor.renderer.setShowPrintMargin(false); |
| 86 | + editor.session.setTabSize(4); |
| 87 | + editor.session.setUseSoftTabs(true); |
| 88 | + }, [ editor ]); |
16 | 89 |
|
17 | 90 | return ( |
18 | 91 | <div className={'my-10'}> |
19 | | - <textarea |
20 | | - value={content} |
21 | | - className={'rounded bg-black h-32 w-full text-neutral-100 text-sm font-mono'} |
22 | | - /> |
| 92 | + <EditorContainer> |
| 93 | + <div id={'editor'} ref={ref}/> |
| 94 | + <div className={'absolute pin-r pin-t z-50'}> |
| 95 | + <div className={'m-3 rounded bg-neutral-900 border border-black'}> |
| 96 | + <select |
| 97 | + className={'input-dark'} |
| 98 | + onChange={e => { |
| 99 | + if (editor) { |
| 100 | + editor.session.setMode(`ace/mode/${e.currentTarget.value}`); |
| 101 | + } |
| 102 | + }} |
| 103 | + > |
| 104 | + { |
| 105 | + Object.keys(modes).map(key => ( |
| 106 | + <option key={key} value={key}>{modes[key]}</option> |
| 107 | + )) |
| 108 | + } |
| 109 | + </select> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + </EditorContainer> |
23 | 113 | </div> |
24 | 114 | ); |
25 | 115 | }; |
0 commit comments