2020-07-05 00:57:24 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2019-10-19 22:31:02 +00:00
|
|
|
import ace, { Editor } from 'brace';
|
2020-07-03 21:19:05 +00:00
|
|
|
import styled from 'styled-components/macro';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-05 05:16:06 +00:00
|
|
|
import modes from '@/modes';
|
2019-10-19 22:31:02 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
require('brace/ext/modelist');
|
|
|
|
require('ayu-ace/mirage');
|
|
|
|
|
|
|
|
const EditorContainer = styled.div`
|
|
|
|
min-height: 16rem;
|
2020-07-05 00:57:24 +00:00
|
|
|
height: calc(100vh - 20rem);
|
2019-10-19 22:31:02 +00:00
|
|
|
${tw`relative`};
|
|
|
|
|
|
|
|
#editor {
|
|
|
|
${tw`rounded h-full`};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
Object.keys(modes).forEach(mode => require(`brace/mode/${mode}`));
|
2020-08-20 04:11:29 +00:00
|
|
|
const modelist = ace.acequire('ace/ext/modelist');
|
2019-10-19 22:31:02 +00:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
style?: React.CSSProperties;
|
2019-10-20 00:35:01 +00:00
|
|
|
initialContent?: string;
|
2020-08-20 04:11:29 +00:00
|
|
|
mode: string;
|
|
|
|
filename?: string;
|
|
|
|
onModeChanged: (mode: string) => void;
|
2019-10-19 22:31:02 +00:00
|
|
|
fetchContent: (callback: () => Promise<string>) => void;
|
|
|
|
onContentSaved: (content: string) => void;
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:11:29 +00:00
|
|
|
export default ({ style, initialContent, filename, mode, fetchContent, onContentSaved, onModeChanged }: Props) => {
|
2019-10-19 22:31:02 +00:00
|
|
|
const [ editor, setEditor ] = useState<Editor>();
|
|
|
|
const ref = useCallback(node => {
|
2020-08-20 04:11:29 +00:00
|
|
|
if (node) setEditor(ace.edit('editor'));
|
2019-10-19 22:31:02 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-08-20 04:11:29 +00:00
|
|
|
if (modelist && filename) {
|
|
|
|
onModeChanged(modelist.getModeForPath(filename).mode.replace(/^ace\/mode\//, ''));
|
|
|
|
}
|
|
|
|
}, [ filename ]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
editor && editor.session.setMode(`ace/mode/${mode}`);
|
2020-07-05 00:57:24 +00:00
|
|
|
}, [ editor, mode ]);
|
2019-10-19 22:31:02 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-10-20 00:35:01 +00:00
|
|
|
editor && editor.session.setValue(initialContent || '');
|
|
|
|
}, [ editor, initialContent ]);
|
|
|
|
|
2019-10-19 22:31:02 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!editor) {
|
|
|
|
fetchContent(() => Promise.reject(new Error('no editor session has been configured')));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.setTheme('ace/theme/ayu-mirage');
|
|
|
|
|
|
|
|
editor.$blockScrolling = Infinity;
|
|
|
|
editor.container.style.lineHeight = '1.375rem';
|
|
|
|
editor.container.style.fontWeight = '500';
|
|
|
|
editor.renderer.updateFontSize();
|
|
|
|
editor.renderer.setShowPrintMargin(false);
|
|
|
|
editor.session.setTabSize(4);
|
|
|
|
editor.session.setUseSoftTabs(true);
|
|
|
|
|
|
|
|
editor.commands.addCommand({
|
|
|
|
name: 'Save',
|
|
|
|
bindKey: { win: 'Ctrl-s', mac: 'Command-s' },
|
|
|
|
exec: (editor: Editor) => onContentSaved(editor.session.getValue()),
|
|
|
|
});
|
|
|
|
|
|
|
|
fetchContent(() => Promise.resolve(editor.session.getValue()));
|
|
|
|
}, [ editor, fetchContent, onContentSaved ]);
|
|
|
|
|
|
|
|
return (
|
2019-10-20 00:35:01 +00:00
|
|
|
<EditorContainer style={style}>
|
2019-10-19 22:31:02 +00:00
|
|
|
<div id={'editor'} ref={ref}/>
|
|
|
|
</EditorContainer>
|
|
|
|
);
|
|
|
|
};
|