feat: allow to open up to three terminals (#22)

This commit is contained in:
Dominic Elm 2024-07-31 13:18:41 +02:00 committed by GitHub
parent 49196273b0
commit e5ed23c33d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 64 additions and 20 deletions

View File

@ -1,7 +1,7 @@
import { memo } from 'react'; import { memo } from 'react';
import { classNames } from '~/utils/classNames'; import { classNames } from '~/utils/classNames';
type IconSize = 'sm' | 'md' | 'xl' | 'xxl'; type IconSize = 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
interface BaseIconButtonProps { interface BaseIconButtonProps {
size?: IconSize; size?: IconSize;
@ -64,6 +64,8 @@ function getIconSize(size: IconSize) {
return 'text-sm'; return 'text-sm';
} else if (size === 'md') { } else if (size === 'md') {
return 'text-md'; return 'text-md';
} else if (size === 'lg') {
return 'text-lg';
} else if (size === 'xl') { } else if (size === 'xl') {
return 'text-xl'; return 'text-xl';
} else { } else {

View File

@ -9,12 +9,14 @@ import {
type OnSaveCallback as OnEditorSave, type OnSaveCallback as OnEditorSave,
type OnScrollCallback as OnEditorScroll, type OnScrollCallback as OnEditorScroll,
} from '~/components/editor/codemirror/CodeMirrorEditor'; } from '~/components/editor/codemirror/CodeMirrorEditor';
import { IconButton } from '~/components/ui/IconButton';
import { PanelHeader } from '~/components/ui/PanelHeader'; import { PanelHeader } from '~/components/ui/PanelHeader';
import { PanelHeaderButton } from '~/components/ui/PanelHeaderButton'; import { PanelHeaderButton } from '~/components/ui/PanelHeaderButton';
import { shortcutEventEmitter } from '~/lib/hooks'; import { shortcutEventEmitter } from '~/lib/hooks';
import type { FileMap } from '~/lib/stores/files'; import type { FileMap } from '~/lib/stores/files';
import { themeStore } from '~/lib/stores/theme'; import { themeStore } from '~/lib/stores/theme';
import { workbenchStore } from '~/lib/stores/workbench'; import { workbenchStore } from '~/lib/stores/workbench';
import { classNames } from '~/utils/classNames';
import { renderLogger } from '~/utils/logger'; import { renderLogger } from '~/utils/logger';
import { isMobile } from '~/utils/mobile'; import { isMobile } from '~/utils/mobile';
import { FileTreePanel } from './FileTreePanel'; import { FileTreePanel } from './FileTreePanel';
@ -33,6 +35,7 @@ interface EditorPanelProps {
onFileReset?: () => void; onFileReset?: () => void;
} }
const MAX_TERMINALS = 3;
const DEFAULT_TERMINAL_SIZE = 25; const DEFAULT_TERMINAL_SIZE = 25;
const DEFAULT_EDITOR_SIZE = 100 - DEFAULT_TERMINAL_SIZE; const DEFAULT_EDITOR_SIZE = 100 - DEFAULT_TERMINAL_SIZE;
@ -60,7 +63,8 @@ export const EditorPanel = memo(
const terminalPanelRef = useRef<ImperativePanelHandle>(null); const terminalPanelRef = useRef<ImperativePanelHandle>(null);
const terminalToggledByShortcut = useRef(false); const terminalToggledByShortcut = useRef(false);
const [terminalCount] = useState(1); const [activeTerminal, setActiveTerminal] = useState(0);
const [terminalCount, setTerminalCount] = useState(1);
const activeFile = useMemo(() => { const activeFile = useMemo(() => {
if (!editorDocument) { if (!editorDocument) {
@ -109,6 +113,13 @@ export const EditorPanel = memo(
terminalToggledByShortcut.current = false; terminalToggledByShortcut.current = false;
}, [showTerminal]); }, [showTerminal]);
const addTerminal = () => {
if (terminalCount < MAX_TERMINALS) {
setTerminalCount(terminalCount + 1);
setActiveTerminal(terminalCount);
}
};
return ( return (
<PanelGroup direction="vertical"> <PanelGroup direction="vertical">
<Panel defaultSize={showTerminal ? DEFAULT_EDITOR_SIZE : 100} minSize={20}> <Panel defaultSize={showTerminal ? DEFAULT_EDITOR_SIZE : 100} minSize={20}>
@ -180,28 +191,51 @@ export const EditorPanel = memo(
} }
}} }}
> >
<div className="border-t h-full"> <div className="border-t h-full flex flex-col">
<PanelHeader> <div className="flex items-center bg-gray-50 min-h-[34px]">
<span className="i-ph:terminal-window-duotone shrink-0" /> Terminal
</PanelHeader>
<div className="p-3.5">
{Array.from({ length: terminalCount }, (_, index) => { {Array.from({ length: terminalCount }, (_, index) => {
const isActive = activeTerminal === index;
return (
<button
key={index}
className={classNames(
'flex items-center text-sm bg-transparent cursor-pointer gap-1.5 px-3.5 h-full whitespace-nowrap',
{
'bg-white': isActive,
'hover:bg-gray-100': !isActive,
},
)}
onClick={() => setActiveTerminal(index)}
>
<div className="i-ph:terminal-window-duotone text-md" />
Terminal {terminalCount > 1 && index + 1}
</button>
);
})}
{terminalCount < MAX_TERMINALS && (
<IconButton className="ml-2" icon="i-ph:plus" size="md" onClick={addTerminal} />
)}
</div>
{Array.from({ length: terminalCount }, (_, index) => {
const isActive = activeTerminal === index;
return ( return (
<div key={index} className="h-full">
<Terminal <Terminal
key={index}
className={classNames('h-full overflow-hidden', {
hidden: !isActive,
})}
ref={(ref) => { ref={(ref) => {
terminalRefs.current.push(ref); terminalRefs.current.push(ref);
}} }}
onTerminalReady={(terminal) => workbenchStore.attachTerminal(terminal)} onTerminalReady={(terminal) => workbenchStore.attachTerminal(terminal)}
onTerminalResize={(cols, rows) => workbenchStore.onTerminalResize(cols, rows)} onTerminalResize={(cols, rows) => workbenchStore.onTerminalResize(cols, rows)}
className="h-full"
theme={theme} theme={theme}
/> />
</div>
); );
})} })}
</div> </div>
</div>
</Panel> </Panel>
</PanelGroup> </PanelGroup>
); );

View File

@ -3,9 +3,10 @@ import { WebLinksAddon } from '@xterm/addon-web-links';
import { Terminal as XTerm } from '@xterm/xterm'; import { Terminal as XTerm } from '@xterm/xterm';
import { forwardRef, memo, useEffect, useImperativeHandle, useRef } from 'react'; import { forwardRef, memo, useEffect, useImperativeHandle, useRef } from 'react';
import type { Theme } from '~/lib/stores/theme'; import type { Theme } from '~/lib/stores/theme';
import { createScopedLogger } from '~/utils/logger';
import { getTerminalTheme } from './theme'; import { getTerminalTheme } from './theme';
import '@xterm/xterm/css/xterm.css'; const logger = createScopedLogger('Terminal');
export interface TerminalRef { export interface TerminalRef {
reloadStyles: () => void; reloadStyles: () => void;
@ -52,6 +53,8 @@ export const Terminal = memo(
resizeObserver.observe(element); resizeObserver.observe(element);
logger.info('Attach terminal');
onTerminalReady?.(terminal); onTerminalReady?.(terminal);
return () => { return () => {

View File

@ -41,7 +41,10 @@ export function useShortcuts(): void {
) { ) {
shortcutEventEmitter.dispatch(name as keyof Shortcuts); shortcutEventEmitter.dispatch(name as keyof Shortcuts);
event.preventDefault(); event.preventDefault();
event.stopPropagation();
shortcut.action(); shortcut.action();
break; break;
} }
} }

View File

@ -7,6 +7,7 @@ import { stripIndents } from './utils/stripIndent';
import reactToastifyStyles from 'react-toastify/dist/ReactToastify.css?url'; import reactToastifyStyles from 'react-toastify/dist/ReactToastify.css?url';
import globalStyles from './styles/index.scss?url'; import globalStyles from './styles/index.scss?url';
import xtermStyles from '@xterm/xterm/css/xterm.css?url';
import 'virtual:uno.css'; import 'virtual:uno.css';
@ -19,6 +20,7 @@ export const links: LinksFunction = () => [
{ rel: 'stylesheet', href: tailwindReset }, { rel: 'stylesheet', href: tailwindReset },
{ rel: 'stylesheet', href: globalStyles }, { rel: 'stylesheet', href: globalStyles },
{ rel: 'stylesheet', href: reactToastifyStyles }, { rel: 'stylesheet', href: reactToastifyStyles },
{ rel: 'stylesheet', href: xtermStyles },
{ {
rel: 'preconnect', rel: 'preconnect',
href: 'https://fonts.googleapis.com', href: 'https://fonts.googleapis.com',

View File

@ -1,3 +1,3 @@
.xterm { .xterm {
height: 100%; padding: 1rem;
} }