Skip to content

Commit a019e38

Browse files
committed
Fix terminal panel
1 parent 56e751f commit a019e38

File tree

4 files changed

+82
-23
lines changed

4 files changed

+82
-23
lines changed

packages/mini-editor/src/editor-frame.tsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export {
1212
OutputPanel,
1313
TabsSnapshot,
1414
Tab,
15-
TerminalPanel,
1615
}
1716

1817
type Tab = {
@@ -329,24 +328,3 @@ function getPanelStyles(
329328
function tween(a: number, b: number, t: number) {
330329
return a + (b - a) * t
331330
}
332-
333-
type TerminalPanelProps = {
334-
height?: number
335-
children: React.ReactNode
336-
}
337-
338-
function TerminalPanel({
339-
height,
340-
children,
341-
}: TerminalPanelProps) {
342-
return !height ? null : (
343-
<div className="ch-editor-terminal" style={{ height }}>
344-
<div className="ch-editor-terminal-tab">
345-
<span>Terminal</span>
346-
</div>
347-
<div className="ch-editor-terminal-content">
348-
{children}
349-
</div>
350-
</div>
351-
)
352-
}

packages/mini-editor/src/editor-transition.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
useSnapshots,
1313
} from "./use-snapshots"
1414
import { getTabs } from "./tabs"
15+
import { TerminalPanel } from "./terminal-panel"
1516

1617
export { EditorTransition, EditorTransitionProps }
1718

@@ -45,11 +46,21 @@ function EditorTransition({
4546
backward,
4647
codeProps
4748
)
49+
50+
const terminalPanel = (
51+
<TerminalPanel
52+
prev={prev.terminal}
53+
next={next.terminal}
54+
t={t}
55+
backward={backward}
56+
/>
57+
)
4858
return (
4959
<EditorFrame
5060
ref={ref}
5161
northPanel={northPanel}
5262
southPanel={southPanel}
63+
terminalPanel={terminalPanel}
5364
{...rest}
5465
/>
5566
)

packages/mini-editor/src/mini-editor-hike.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { EditorFrame, TerminalPanel } from "./editor-frame"
2+
import { EditorFrame } from "./editor-frame"
33
import { InnerTerminal } from "@code-hike/mini-terminal"
44
import { Code } from "./code"
55
import {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { InnerTerminal } from "@code-hike/mini-terminal"
2+
import React from "react"
3+
4+
type TerminalPanelProps = {
5+
prev: string | undefined
6+
next: string | undefined
7+
t: number
8+
backward: boolean
9+
}
10+
11+
export function TerminalPanel({
12+
prev,
13+
next,
14+
t,
15+
backward,
16+
}: TerminalPanelProps) {
17+
const height = getHeight({ prev, next, t, backward })
18+
return !height ? null : (
19+
<div className="ch-editor-terminal" style={{ height }}>
20+
<div className="ch-editor-terminal-tab">
21+
<span>Terminal</span>
22+
</div>
23+
<div className="ch-editor-terminal-content">
24+
<InnerTerminal
25+
steps={[
26+
{ text: prev || "" },
27+
{ text: next || "" },
28+
]}
29+
progress={t}
30+
/>
31+
)
32+
</div>
33+
</div>
34+
)
35+
}
36+
37+
function getHeight({
38+
prev,
39+
next,
40+
t,
41+
backward,
42+
}: TerminalPanelProps) {
43+
if (!prev && !next) return 0
44+
if (!prev && next) return MAX_HEIGHT * Math.min(t * 4, 1)
45+
if (prev && !next)
46+
return MAX_HEIGHT * Math.max(1 - t * 4, 0)
47+
return MAX_HEIGHT
48+
}
49+
50+
const MAX_HEIGHT = 150
51+
function getTerminalHeight(steps: any, progress: number) {
52+
if (!steps.length) {
53+
return 0
54+
}
55+
56+
const prevIndex = Math.floor(progress)
57+
const nextIndex = Math.ceil(progress)
58+
const prevTerminal =
59+
steps[prevIndex] && steps[prevIndex].terminal
60+
const nextTerminal = steps[nextIndex].terminal
61+
62+
if (!prevTerminal && !nextTerminal) return 0
63+
64+
if (!prevTerminal && nextTerminal)
65+
return MAX_HEIGHT * Math.min((progress % 1) * 4, 1)
66+
if (prevTerminal && !nextTerminal)
67+
return MAX_HEIGHT * Math.max(1 - (progress % 1) * 4, 0)
68+
69+
return MAX_HEIGHT
70+
}

0 commit comments

Comments
 (0)