diff --git a/resources/scripts/components/elements/AceEditor.tsx b/resources/scripts/components/elements/AceEditor.tsx index 638b6cbba..3afab6364 100644 --- a/resources/scripts/components/elements/AceEditor.tsx +++ b/resources/scripts/components/elements/AceEditor.tsx @@ -52,15 +52,14 @@ Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`)); export interface Props { style?: React.CSSProperties; + initialContent?: string; + initialModePath?: string; fetchContent: (callback: () => Promise) => void; onContentSaved: (content: string) => void; } -export default ({ fetchContent, onContentSaved }: Props) => { - const { location: { hash } } = useRouter(); - const [ content, setContent ] = useState(''); +export default ({ style, initialContent, initialModePath, fetchContent, onContentSaved }: Props) => { const [ mode, setMode ] = useState('plain_text'); - const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const [ editor, setEditor ] = useState(); const ref = useCallback(node => { @@ -69,30 +68,22 @@ export default ({ fetchContent, onContentSaved }: Props) => { } }, []); - useEffect(() => { - getFileContents(uuid, hash.replace(/^#/, '')) - .then(setContent) - .catch(error => console.error(error)); - }, [ uuid, hash ]); - - useEffect(() => { - if (!hash.length) { - return; - } - - const modelist = ace.acequire('ace/ext/modelist'); - if (modelist) { - setMode(modelist.getModeForPath(hash.replace(/^#/, '')).mode); - } - }, [hash]); - useEffect(() => { editor && editor.session.setMode(mode); }, [editor, mode]); useEffect(() => { - editor && editor.session.setValue(content); - }, [ editor, content ]); + editor && editor.session.setValue(initialContent || ''); + }, [ editor, initialContent ]); + + useEffect(() => { + if (initialModePath) { + const modelist = ace.acequire('ace/ext/modelist'); + if (modelist) { + setMode(modelist.getModeForPath(initialModePath).mode); + } + } + }, [ initialModePath ]); useEffect(() => { if (!editor) { @@ -120,7 +111,7 @@ export default ({ fetchContent, onContentSaved }: Props) => { }, [ editor, fetchContent, onContentSaved ]); return ( - +
diff --git a/resources/scripts/components/server/files/FileEditContainer.tsx b/resources/scripts/components/server/files/FileEditContainer.tsx index 4be49db95..1dc543856 100644 --- a/resources/scripts/components/server/files/FileEditContainer.tsx +++ b/resources/scripts/components/server/files/FileEditContainer.tsx @@ -1,23 +1,38 @@ -import React, { lazy } from 'react'; +import React, { lazy, useEffect, useState } from 'react'; import { ServerContext } from '@/state/server'; +import getFileContents from '@/api/server/files/getFileContents'; +import useRouter from 'use-react-router'; const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor')); export default () => { + const { location: { hash } } = useRouter(); + const [ content, setContent ] = useState(''); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); let ref: null| (() => Promise) = null; - setTimeout(() => ref && ref().then(console.log), 5000); + useEffect(() => { + getFileContents(uuid, hash.replace(/^#/, '')) + .then(setContent) + .catch(error => console.error(error)); + }, [ uuid, hash ]); return ( -
+
{ ref = value; }} onContentSaved={() => null} /> +
+ +
); };