2024-07-17 18:54:46 +00:00
|
|
|
import { useStore } from '@nanostores/react';
|
2024-07-29 12:37:23 +00:00
|
|
|
import { memo, useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
import { Panel, PanelGroup, PanelResizeHandle, type ImperativePanelHandle } from 'react-resizable-panels';
|
2024-07-18 21:07:04 +00:00
|
|
|
import {
|
|
|
|
CodeMirrorEditor,
|
|
|
|
type EditorDocument,
|
2024-07-24 14:10:39 +00:00
|
|
|
type EditorSettings,
|
2024-07-18 21:07:04 +00:00
|
|
|
type OnChangeCallback as OnEditorChange,
|
2024-07-24 14:10:39 +00:00
|
|
|
type OnSaveCallback as OnEditorSave,
|
2024-07-18 21:07:04 +00:00
|
|
|
type OnScrollCallback as OnEditorScroll,
|
2024-07-24 15:43:32 +00:00
|
|
|
} from '~/components/editor/codemirror/CodeMirrorEditor';
|
2024-07-29 12:37:23 +00:00
|
|
|
import { PanelHeader } from '~/components/ui/PanelHeader';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { PanelHeaderButton } from '~/components/ui/PanelHeaderButton';
|
2024-07-29 12:37:23 +00:00
|
|
|
import { shortcutEventEmitter } from '~/lib/hooks';
|
2024-07-24 15:43:32 +00:00
|
|
|
import type { FileMap } from '~/lib/stores/files';
|
|
|
|
import { themeStore } from '~/lib/stores/theme';
|
2024-07-29 12:37:23 +00:00
|
|
|
import { workbenchStore } from '~/lib/stores/workbench';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { renderLogger } from '~/utils/logger';
|
|
|
|
import { isMobile } from '~/utils/mobile';
|
2024-07-17 18:54:46 +00:00
|
|
|
import { FileTreePanel } from './FileTreePanel';
|
2024-07-29 12:37:23 +00:00
|
|
|
import { Terminal, type TerminalRef } from './terminal/Terminal';
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 21:07:04 +00:00
|
|
|
interface EditorPanelProps {
|
|
|
|
files?: FileMap;
|
2024-07-24 14:10:39 +00:00
|
|
|
unsavedFiles?: Set<string>;
|
2024-07-18 21:07:04 +00:00
|
|
|
editorDocument?: EditorDocument;
|
|
|
|
selectedFile?: string | undefined;
|
|
|
|
isStreaming?: boolean;
|
|
|
|
onEditorChange?: OnEditorChange;
|
|
|
|
onEditorScroll?: OnEditorScroll;
|
|
|
|
onFileSelect?: (value?: string) => void;
|
2024-07-24 14:10:39 +00:00
|
|
|
onFileSave?: OnEditorSave;
|
|
|
|
onFileReset?: () => void;
|
2024-07-17 18:54:46 +00:00
|
|
|
}
|
2024-07-18 21:07:04 +00:00
|
|
|
|
2024-07-29 12:37:23 +00:00
|
|
|
const DEFAULT_TERMINAL_SIZE = 25;
|
|
|
|
const DEFAULT_EDITOR_SIZE = 100 - DEFAULT_TERMINAL_SIZE;
|
|
|
|
|
2024-07-24 14:10:39 +00:00
|
|
|
const editorSettings: EditorSettings = { tabSize: 2 };
|
|
|
|
|
2024-07-18 21:07:04 +00:00
|
|
|
export const EditorPanel = memo(
|
2024-07-24 14:10:39 +00:00
|
|
|
({
|
|
|
|
files,
|
|
|
|
unsavedFiles,
|
|
|
|
editorDocument,
|
|
|
|
selectedFile,
|
|
|
|
isStreaming,
|
|
|
|
onFileSelect,
|
|
|
|
onEditorChange,
|
|
|
|
onEditorScroll,
|
|
|
|
onFileSave,
|
|
|
|
onFileReset,
|
|
|
|
}: EditorPanelProps) => {
|
2024-07-18 21:07:04 +00:00
|
|
|
renderLogger.trace('EditorPanel');
|
|
|
|
|
|
|
|
const theme = useStore(themeStore);
|
2024-07-29 12:37:23 +00:00
|
|
|
const showTerminal = useStore(workbenchStore.showTerminal);
|
|
|
|
|
|
|
|
const terminalRefs = useRef<Array<TerminalRef | null>>([]);
|
|
|
|
const terminalPanelRef = useRef<ImperativePanelHandle>(null);
|
|
|
|
const terminalToggledByShortcut = useRef(false);
|
|
|
|
|
|
|
|
const [terminalCount] = useState(1);
|
2024-07-18 21:07:04 +00:00
|
|
|
|
2024-07-24 14:10:39 +00:00
|
|
|
const activeFile = useMemo(() => {
|
|
|
|
if (!editorDocument) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return editorDocument.filePath.split('/').at(-1);
|
|
|
|
}, [editorDocument]);
|
|
|
|
|
|
|
|
const activeFileUnsaved = useMemo(() => {
|
|
|
|
return editorDocument !== undefined && unsavedFiles?.has(editorDocument.filePath);
|
|
|
|
}, [editorDocument, unsavedFiles]);
|
|
|
|
|
2024-07-29 12:37:23 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const unsubscribeFromEventEmitter = shortcutEventEmitter.on('toggleTerminal', () => {
|
|
|
|
terminalToggledByShortcut.current = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const unsubscribeFromThemeStore = themeStore.subscribe(() => {
|
|
|
|
for (const ref of Object.values(terminalRefs.current)) {
|
|
|
|
ref?.reloadStyles();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
unsubscribeFromEventEmitter();
|
|
|
|
unsubscribeFromThemeStore();
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const { current: terminal } = terminalPanelRef;
|
|
|
|
|
|
|
|
if (!terminal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isCollapsed = terminal.isCollapsed();
|
|
|
|
|
|
|
|
if (!showTerminal && !isCollapsed) {
|
|
|
|
terminal.collapse();
|
|
|
|
} else if (showTerminal && isCollapsed) {
|
|
|
|
terminal.resize(DEFAULT_TERMINAL_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
terminalToggledByShortcut.current = false;
|
|
|
|
}, [showTerminal]);
|
|
|
|
|
2024-07-18 21:07:04 +00:00
|
|
|
return (
|
2024-07-29 12:37:23 +00:00
|
|
|
<PanelGroup direction="vertical">
|
|
|
|
<Panel defaultSize={showTerminal ? DEFAULT_EDITOR_SIZE : 100} minSize={20}>
|
|
|
|
<PanelGroup direction="horizontal">
|
|
|
|
<Panel defaultSize={25} minSize={10} collapsible>
|
|
|
|
<div className="flex flex-col border-r h-full">
|
|
|
|
<PanelHeader>
|
|
|
|
<div className="i-ph:tree-structure-duotone shrink-0" />
|
|
|
|
Files
|
|
|
|
</PanelHeader>
|
|
|
|
<FileTreePanel
|
|
|
|
files={files}
|
|
|
|
unsavedFiles={unsavedFiles}
|
|
|
|
selectedFile={selectedFile}
|
|
|
|
onFileSelect={onFileSelect}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Panel>
|
|
|
|
<PanelResizeHandle />
|
|
|
|
<Panel className="flex flex-col" defaultSize={75} minSize={20}>
|
|
|
|
<PanelHeader>
|
|
|
|
{activeFile && (
|
|
|
|
<div className="flex items-center flex-1 text-sm">
|
|
|
|
{activeFile} {isStreaming && <span className="text-xs ml-1 font-semibold">(read-only)</span>}
|
|
|
|
{activeFileUnsaved && (
|
|
|
|
<div className="flex gap-1 ml-auto -mr-1.5">
|
|
|
|
<PanelHeaderButton onClick={onFileSave}>
|
|
|
|
<div className="i-ph:floppy-disk-duotone" />
|
|
|
|
Save
|
|
|
|
</PanelHeaderButton>
|
|
|
|
<PanelHeaderButton onClick={onFileReset}>
|
|
|
|
<div className="i-ph:clock-counter-clockwise-duotone" />
|
|
|
|
Reset
|
|
|
|
</PanelHeaderButton>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-07-24 14:10:39 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2024-07-29 12:37:23 +00:00
|
|
|
</PanelHeader>
|
|
|
|
<div className="h-full flex-1 overflow-hidden">
|
|
|
|
<CodeMirrorEditor
|
|
|
|
theme={theme}
|
|
|
|
editable={!isStreaming && editorDocument !== undefined}
|
|
|
|
settings={editorSettings}
|
|
|
|
doc={editorDocument}
|
|
|
|
autoFocusOnDocumentChange={!isMobile()}
|
|
|
|
onScroll={onEditorScroll}
|
|
|
|
onChange={onEditorChange}
|
|
|
|
onSave={onFileSave}
|
|
|
|
/>
|
2024-07-24 14:10:39 +00:00
|
|
|
</div>
|
2024-07-29 12:37:23 +00:00
|
|
|
</Panel>
|
|
|
|
</PanelGroup>
|
|
|
|
</Panel>
|
|
|
|
<PanelResizeHandle />
|
|
|
|
<Panel
|
|
|
|
ref={terminalPanelRef}
|
|
|
|
defaultSize={showTerminal ? DEFAULT_TERMINAL_SIZE : 0}
|
|
|
|
minSize={10}
|
|
|
|
collapsible
|
|
|
|
onExpand={() => {
|
|
|
|
if (!terminalToggledByShortcut.current) {
|
|
|
|
workbenchStore.toggleTerminal(true);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onCollapse={() => {
|
|
|
|
if (!terminalToggledByShortcut.current) {
|
|
|
|
workbenchStore.toggleTerminal(false);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="border-t h-full">
|
|
|
|
<PanelHeader>
|
|
|
|
<span className="i-ph:terminal-window-duotone shrink-0" /> Terminal
|
|
|
|
</PanelHeader>
|
|
|
|
<div className="p-3.5">
|
|
|
|
{Array.from({ length: terminalCount }, (_, index) => {
|
|
|
|
return (
|
|
|
|
<div key={index} className="h-full">
|
|
|
|
<Terminal
|
|
|
|
ref={(ref) => {
|
|
|
|
terminalRefs.current.push(ref);
|
|
|
|
}}
|
|
|
|
onTerminalReady={(terminal) => workbenchStore.attachTerminal(terminal)}
|
|
|
|
onTerminalResize={(cols, rows) => workbenchStore.onTerminalResize(cols, rows)}
|
|
|
|
className="h-full"
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2024-07-24 14:10:39 +00:00
|
|
|
</div>
|
2024-07-18 21:07:04 +00:00
|
|
|
</Panel>
|
|
|
|
</PanelGroup>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|