Skip to content

Commit 0dff732

Browse files
committed
Editor improvements
1 parent ac6e5b9 commit 0dff732

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

resources/scripts/components/elements/AceEditor.tsx

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`));
5252

5353
export interface Props {
5454
style?: React.CSSProperties;
55+
initialContent?: string;
56+
initialModePath?: string;
5557
fetchContent: (callback: () => Promise<string>) => void;
5658
onContentSaved: (content: string) => void;
5759
}
5860

59-
export default ({ fetchContent, onContentSaved }: Props) => {
60-
const { location: { hash } } = useRouter();
61-
const [ content, setContent ] = useState('');
61+
export default ({ style, initialContent, initialModePath, fetchContent, onContentSaved }: Props) => {
6262
const [ mode, setMode ] = useState('plain_text');
63-
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
6463

6564
const [ editor, setEditor ] = useState<Editor>();
6665
const ref = useCallback(node => {
@@ -69,30 +68,22 @@ export default ({ fetchContent, onContentSaved }: Props) => {
6968
}
7069
}, []);
7170

72-
useEffect(() => {
73-
getFileContents(uuid, hash.replace(/^#/, ''))
74-
.then(setContent)
75-
.catch(error => console.error(error));
76-
}, [ uuid, hash ]);
77-
78-
useEffect(() => {
79-
if (!hash.length) {
80-
return;
81-
}
82-
83-
const modelist = ace.acequire('ace/ext/modelist');
84-
if (modelist) {
85-
setMode(modelist.getModeForPath(hash.replace(/^#/, '')).mode);
86-
}
87-
}, [hash]);
88-
8971
useEffect(() => {
9072
editor && editor.session.setMode(mode);
9173
}, [editor, mode]);
9274

9375
useEffect(() => {
94-
editor && editor.session.setValue(content);
95-
}, [ editor, content ]);
76+
editor && editor.session.setValue(initialContent || '');
77+
}, [ editor, initialContent ]);
78+
79+
useEffect(() => {
80+
if (initialModePath) {
81+
const modelist = ace.acequire('ace/ext/modelist');
82+
if (modelist) {
83+
setMode(modelist.getModeForPath(initialModePath).mode);
84+
}
85+
}
86+
}, [ initialModePath ]);
9687

9788
useEffect(() => {
9889
if (!editor) {
@@ -120,7 +111,7 @@ export default ({ fetchContent, onContentSaved }: Props) => {
120111
}, [ editor, fetchContent, onContentSaved ]);
121112

122113
return (
123-
<EditorContainer>
114+
<EditorContainer style={style}>
124115
<div id={'editor'} ref={ref}/>
125116
<div className={'absolute pin-r pin-t z-50'}>
126117
<div className={'m-3 rounded bg-neutral-900 border border-black'}>
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1-
import React, { lazy } from 'react';
1+
import React, { lazy, useEffect, useState } from 'react';
22
import { ServerContext } from '@/state/server';
3+
import getFileContents from '@/api/server/files/getFileContents';
4+
import useRouter from 'use-react-router';
35

46
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
57

68
export default () => {
9+
const { location: { hash } } = useRouter();
10+
const [ content, setContent ] = useState('');
711
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
812

913
let ref: null| (() => Promise<string>) = null;
1014

11-
setTimeout(() => ref && ref().then(console.log), 5000);
15+
useEffect(() => {
16+
getFileContents(uuid, hash.replace(/^#/, ''))
17+
.then(setContent)
18+
.catch(error => console.error(error));
19+
}, [ uuid, hash ]);
1220

1321
return (
14-
<div className={'my-10'}>
22+
<div className={'my-10 mb-4'}>
1523
<LazyAceEditor
24+
initialModePath={hash.replace(/^#/, '') || 'plain_text'}
25+
initialContent={content}
1626
fetchContent={value => {
1727
ref = value;
1828
}}
1929
onContentSaved={() => null}
2030
/>
31+
<div className={'flex justify-end mt-4'}>
32+
<button className={'btn btn-primary btn-sm'}>
33+
Save Content
34+
</button>
35+
</div>
2136
</div>
2237
);
2338
};

0 commit comments

Comments
 (0)