@@ -5,6 +5,10 @@ import getFileContents from '@/api/server/files/getFileContents';
55import ace , { Editor } from 'brace' ;
66import styled from 'styled-components' ;
77
8+ // @ts -ignore
9+ require ( 'brace/ext/modelist' ) ;
10+ require ( 'ayu-ace/mirage' ) ;
11+
812const EditorContainer = styled . div `
913 min-height: 16rem;
1014 height: calc(100vh - 16rem);
@@ -36,16 +40,20 @@ const modes: { [k: string]: string } = {
3640 properties : 'Properties' ,
3741 python : 'Python' ,
3842 ruby : 'Ruby' ,
39- text : 'Plaintext' ,
43+ // eslint-disable-next-line @typescript-eslint/camelcase
44+ plain_text : 'Plaintext' ,
4045 toml : 'TOML' ,
4146 typescript : 'Typescript' ,
4247 xml : 'XML' ,
4348 yaml : 'YAML' ,
4449} ;
4550
51+ Object . keys ( modes ) . forEach ( mode => require ( `brace/mode/${ mode } ` ) ) ;
52+
4653export default ( ) => {
4754 const { location : { hash } } = useRouter ( ) ;
4855 const [ content , setContent ] = useState ( '' ) ;
56+ const [ mode , setMode ] = useState ( 'plain_text' ) ;
4957 const uuid = ServerContext . useStoreState ( state => state . server . data ! . uuid ) ;
5058
5159 const [ editor , setEditor ] = useState < Editor > ( ) ;
@@ -55,18 +63,27 @@ export default () => {
5563 }
5664 } , [ ] ) ;
5765
58- useEffect ( ( ) => {
59- Object . keys ( modes ) . forEach ( mode => {
60- import ( /* webpackMode: "lazy-once", webpackChunkName: "ace_mode" */ `brace/mode/${ mode } ` ) ;
61- } ) ;
62- } , [ ] ) ;
63-
6466 useEffect ( ( ) => {
6567 getFileContents ( uuid , hash . replace ( / ^ # / , '' ) )
6668 . then ( setContent )
6769 . catch ( error => console . error ( error ) ) ;
6870 } , [ uuid , hash ] ) ;
6971
72+ useEffect ( ( ) => {
73+ if ( ! hash . length ) {
74+ return ;
75+ }
76+
77+ const modelist = ace . acequire ( 'ace/ext/modelist' ) ;
78+ if ( modelist ) {
79+ setMode ( modelist . getModeForPath ( hash . replace ( / ^ # / , '' ) ) . mode ) ;
80+ }
81+ } , [ hash ] ) ;
82+
83+ useEffect ( ( ) => {
84+ editor && editor . session . setMode ( mode ) ;
85+ } , [ editor , mode ] ) ;
86+
7087 useEffect ( ( ) => {
7188 editor && editor . session . setValue ( content ) ;
7289 } , [ editor , content ] ) ;
@@ -76,7 +93,6 @@ export default () => {
7693 return ;
7794 }
7895
79- require ( 'ayu-ace/mirage' ) ;
8096 editor . setTheme ( 'ace/theme/ayu-mirage' ) ;
8197
8298 editor . $blockScrolling = Infinity ;
@@ -96,11 +112,8 @@ export default () => {
96112 < div className = { 'm-3 rounded bg-neutral-900 border border-black' } >
97113 < select
98114 className = { 'input-dark' }
99- onChange = { e => {
100- if ( editor ) {
101- editor . session . setMode ( `ace/mode/${ e . currentTarget . value } ` ) ;
102- }
103- } }
115+ defaultValue = { mode }
116+ onChange = { e => setMode ( `ace/mode/${ e . currentTarget . value } ` ) }
104117 >
105118 {
106119 Object . keys ( modes ) . map ( key => (
0 commit comments