Editor improvements
This commit is contained in:
parent
ac6e5b9943
commit
0dff732883
|
@ -52,15 +52,14 @@ Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`));
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
style?: React.CSSProperties;
|
style?: React.CSSProperties;
|
||||||
|
initialContent?: string;
|
||||||
|
initialModePath?: string;
|
||||||
fetchContent: (callback: () => Promise<string>) => void;
|
fetchContent: (callback: () => Promise<string>) => void;
|
||||||
onContentSaved: (content: string) => void;
|
onContentSaved: (content: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ({ fetchContent, onContentSaved }: Props) => {
|
export default ({ style, initialContent, initialModePath, fetchContent, onContentSaved }: Props) => {
|
||||||
const { location: { hash } } = useRouter();
|
|
||||||
const [ content, setContent ] = useState('');
|
|
||||||
const [ mode, setMode ] = useState('plain_text');
|
const [ mode, setMode ] = useState('plain_text');
|
||||||
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
||||||
|
|
||||||
const [ editor, setEditor ] = useState<Editor>();
|
const [ editor, setEditor ] = useState<Editor>();
|
||||||
const ref = useCallback(node => {
|
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(() => {
|
useEffect(() => {
|
||||||
editor && editor.session.setMode(mode);
|
editor && editor.session.setMode(mode);
|
||||||
}, [editor, mode]);
|
}, [editor, mode]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor && editor.session.setValue(content);
|
editor && editor.session.setValue(initialContent || '');
|
||||||
}, [ editor, content ]);
|
}, [ editor, initialContent ]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (initialModePath) {
|
||||||
|
const modelist = ace.acequire('ace/ext/modelist');
|
||||||
|
if (modelist) {
|
||||||
|
setMode(modelist.getModeForPath(initialModePath).mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [ initialModePath ]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
|
@ -120,7 +111,7 @@ export default ({ fetchContent, onContentSaved }: Props) => {
|
||||||
}, [ editor, fetchContent, onContentSaved ]);
|
}, [ editor, fetchContent, onContentSaved ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<EditorContainer>
|
<EditorContainer style={style}>
|
||||||
<div id={'editor'} ref={ref}/>
|
<div id={'editor'} ref={ref}/>
|
||||||
<div className={'absolute pin-r pin-t z-50'}>
|
<div className={'absolute pin-r pin-t z-50'}>
|
||||||
<div className={'m-3 rounded bg-neutral-900 border border-black'}>
|
<div className={'m-3 rounded bg-neutral-900 border border-black'}>
|
||||||
|
|
|
@ -1,23 +1,38 @@
|
||||||
import React, { lazy } from 'react';
|
import React, { lazy, useEffect, useState } from 'react';
|
||||||
import { ServerContext } from '@/state/server';
|
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'));
|
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
const { location: { hash } } = useRouter();
|
||||||
|
const [ content, setContent ] = useState('');
|
||||||
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
||||||
|
|
||||||
let ref: null| (() => Promise<string>) = null;
|
let ref: null| (() => Promise<string>) = null;
|
||||||
|
|
||||||
setTimeout(() => ref && ref().then(console.log), 5000);
|
useEffect(() => {
|
||||||
|
getFileContents(uuid, hash.replace(/^#/, ''))
|
||||||
|
.then(setContent)
|
||||||
|
.catch(error => console.error(error));
|
||||||
|
}, [ uuid, hash ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={'my-10'}>
|
<div className={'my-10 mb-4'}>
|
||||||
<LazyAceEditor
|
<LazyAceEditor
|
||||||
|
initialModePath={hash.replace(/^#/, '') || 'plain_text'}
|
||||||
|
initialContent={content}
|
||||||
fetchContent={value => {
|
fetchContent={value => {
|
||||||
ref = value;
|
ref = value;
|
||||||
}}
|
}}
|
||||||
onContentSaved={() => null}
|
onContentSaved={() => null}
|
||||||
/>
|
/>
|
||||||
|
<div className={'flex justify-end mt-4'}>
|
||||||
|
<button className={'btn btn-primary btn-sm'}>
|
||||||
|
Save Content
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue