Move the file selector out of the editor itself; closes #2147
This commit is contained in:
parent
540cc82e3d
commit
13ace83f42
4 changed files with 36 additions and 44 deletions
|
@ -2,8 +2,6 @@ import React, { useCallback, useEffect, useState } from 'react';
|
||||||
import ace, { Editor } from 'brace';
|
import ace, { Editor } from 'brace';
|
||||||
import styled from 'styled-components/macro';
|
import styled from 'styled-components/macro';
|
||||||
import tw from 'twin.macro';
|
import tw from 'twin.macro';
|
||||||
import Select from '@/components/elements/Select';
|
|
||||||
// @ts-ignore
|
|
||||||
import modes from '@/modes';
|
import modes from '@/modes';
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -21,42 +19,38 @@ const EditorContainer = styled.div`
|
||||||
`;
|
`;
|
||||||
|
|
||||||
Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`));
|
Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`));
|
||||||
|
const modelist = ace.acequire('ace/ext/modelist');
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
style?: React.CSSProperties;
|
style?: React.CSSProperties;
|
||||||
initialContent?: string;
|
initialContent?: string;
|
||||||
initialModePath?: string;
|
mode: string;
|
||||||
|
filename?: string;
|
||||||
|
onModeChanged: (mode: string) => void;
|
||||||
fetchContent: (callback: () => Promise<string>) => void;
|
fetchContent: (callback: () => Promise<string>) => void;
|
||||||
onContentSaved: (content: string) => void;
|
onContentSaved: (content: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ({ style, initialContent, initialModePath, fetchContent, onContentSaved }: Props) => {
|
export default ({ style, initialContent, filename, mode, fetchContent, onContentSaved, onModeChanged }: Props) => {
|
||||||
const [ mode, setMode ] = useState('ace/mode/plain_text');
|
|
||||||
|
|
||||||
const [ editor, setEditor ] = useState<Editor>();
|
const [ editor, setEditor ] = useState<Editor>();
|
||||||
const ref = useCallback(node => {
|
const ref = useCallback(node => {
|
||||||
if (node) {
|
if (node) setEditor(ace.edit('editor'));
|
||||||
setEditor(ace.edit('editor'));
|
|
||||||
}
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor && editor.session.setMode(mode);
|
if (modelist && filename) {
|
||||||
|
onModeChanged(modelist.getModeForPath(filename).mode.replace(/^ace\/mode\//, ''));
|
||||||
|
}
|
||||||
|
}, [ filename ]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
editor && editor.session.setMode(`ace/mode/${mode}`);
|
||||||
}, [ editor, mode ]);
|
}, [ editor, mode ]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
editor && editor.session.setValue(initialContent || '');
|
editor && editor.session.setValue(initialContent || '');
|
||||||
}, [ editor, initialContent ]);
|
}, [ 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) {
|
||||||
fetchContent(() => Promise.reject(new Error('no editor session has been configured')));
|
fetchContent(() => Promise.reject(new Error('no editor session has been configured')));
|
||||||
|
@ -85,20 +79,6 @@ export default ({ style, initialContent, initialModePath, fetchContent, onConten
|
||||||
return (
|
return (
|
||||||
<EditorContainer style={style}>
|
<EditorContainer style={style}>
|
||||||
<div id={'editor'} ref={ref}/>
|
<div id={'editor'} ref={ref}/>
|
||||||
<div css={tw`absolute right-0 bottom-0 z-50`}>
|
|
||||||
<div css={tw`m-3 rounded bg-neutral-900 border border-black`}>
|
|
||||||
<Select
|
|
||||||
value={mode.split('/').pop()}
|
|
||||||
onChange={e => setMode(`ace/mode/${e.currentTarget.value}`)}
|
|
||||||
>
|
|
||||||
{
|
|
||||||
Object.keys(modes).map(key => (
|
|
||||||
<option key={key} value={key}>{(modes as { [k: string]: string })[key]}</option>
|
|
||||||
))
|
|
||||||
}
|
|
||||||
</Select>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</EditorContainer>
|
</EditorContainer>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
import React, { lazy, useEffect, useState } from 'react';
|
import React, { lazy, useEffect, useState } from 'react';
|
||||||
import { ServerContext } from '@/state/server';
|
|
||||||
import getFileContents from '@/api/server/files/getFileContents';
|
import getFileContents from '@/api/server/files/getFileContents';
|
||||||
import { Actions, useStoreActions } from 'easy-peasy';
|
|
||||||
import { ApplicationStore } from '@/state';
|
|
||||||
import { httpErrorToHuman } from '@/api/http';
|
import { httpErrorToHuman } from '@/api/http';
|
||||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
import saveFileContents from '@/api/server/files/saveFileContents';
|
import saveFileContents from '@/api/server/files/saveFileContents';
|
||||||
|
@ -15,6 +12,10 @@ import PageContentBlock from '@/components/elements/PageContentBlock';
|
||||||
import ServerError from '@/components/screens/ServerError';
|
import ServerError from '@/components/screens/ServerError';
|
||||||
import tw from 'twin.macro';
|
import tw from 'twin.macro';
|
||||||
import Button from '@/components/elements/Button';
|
import Button from '@/components/elements/Button';
|
||||||
|
import Select from '@/components/elements/Select';
|
||||||
|
import modes from '@/modes';
|
||||||
|
import useServer from '@/plugins/useServer';
|
||||||
|
import useFlash from '@/plugins/useFlash';
|
||||||
|
|
||||||
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
|
const LazyAceEditor = lazy(() => import(/* webpackChunkName: "editor" */'@/components/elements/AceEditor'));
|
||||||
|
|
||||||
|
@ -24,12 +25,13 @@ export default () => {
|
||||||
const [ loading, setLoading ] = useState(action === 'edit');
|
const [ loading, setLoading ] = useState(action === 'edit');
|
||||||
const [ content, setContent ] = useState('');
|
const [ content, setContent ] = useState('');
|
||||||
const [ modalVisible, setModalVisible ] = useState(false);
|
const [ modalVisible, setModalVisible ] = useState(false);
|
||||||
|
const [ mode, setMode ] = useState('plain_text');
|
||||||
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { hash } = useLocation();
|
const { hash } = useLocation();
|
||||||
|
|
||||||
const { id, uuid } = ServerContext.useStoreState(state => state.server.data!);
|
const { id, uuid } = useServer();
|
||||||
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
const { addError, clearFlashes } = useFlash();
|
||||||
|
|
||||||
let fetchFileContent: null | (() => Promise<string>) = null;
|
let fetchFileContent: null | (() => Promise<string>) = null;
|
||||||
|
|
||||||
|
@ -75,10 +77,7 @@ export default () => {
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<ServerError
|
<ServerError message={error} onBack={() => history.goBack()}/>
|
||||||
message={error}
|
|
||||||
onBack={() => history.goBack()}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,15 +108,24 @@ export default () => {
|
||||||
<div css={tw`relative`}>
|
<div css={tw`relative`}>
|
||||||
<SpinnerOverlay visible={loading}/>
|
<SpinnerOverlay visible={loading}/>
|
||||||
<LazyAceEditor
|
<LazyAceEditor
|
||||||
initialModePath={hash.replace(/^#/, '') || 'plain_text'}
|
mode={mode}
|
||||||
|
filename={hash.replace(/^#/, '')}
|
||||||
|
onModeChanged={setMode}
|
||||||
initialContent={content}
|
initialContent={content}
|
||||||
fetchContent={value => {
|
fetchContent={value => {
|
||||||
fetchFileContent = value;
|
fetchFileContent = value;
|
||||||
}}
|
}}
|
||||||
onContentSaved={() => save()}
|
onContentSaved={save}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div css={tw`flex justify-end mt-4`}>
|
<div css={tw`flex justify-end mt-4`}>
|
||||||
|
<div css={tw`rounded bg-neutral-900 mr-4`}>
|
||||||
|
<Select value={mode} onChange={e => setMode(e.currentTarget.value)}>
|
||||||
|
{Object.keys(modes).map(key => (
|
||||||
|
<option key={key} value={key}>{modes[key]}</option>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
{action === 'edit' ?
|
{action === 'edit' ?
|
||||||
<Can action={'file.update'}>
|
<Can action={'file.update'}>
|
||||||
<Button onClick={() => save()}>
|
<Button onClick={() => save()}>
|
||||||
|
|
3
resources/scripts/modes.d.ts
vendored
Normal file
3
resources/scripts/modes.d.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
declare const modes: Record<string, string>;
|
||||||
|
|
||||||
|
export default modes;
|
|
@ -1,3 +1,4 @@
|
||||||
|
// This file must be plain Javascript since we're using it within Webpack.
|
||||||
module.exports = {
|
module.exports = {
|
||||||
assembly_x86: 'Assembly (x86)',
|
assembly_x86: 'Assembly (x86)',
|
||||||
c_cpp: 'C++',
|
c_cpp: 'C++',
|
||||||
|
|
Loading…
Reference in a new issue