/
mini-editor.tsx
133 lines (116 loc) · 3.23 KB
/
mini-editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React from "react";
import { EditorFrame, TerminalPanel } from "./editor-frame";
import { CodeSurfer } from "@code-surfer/standalone";
import { vsDark } from "@code-surfer/themes";
import { InnerTerminal } from "@code-hike/mini-terminal";
export { MiniEditor };
type MiniEditorStep = {
code?: string;
focus?: string;
lang?: string;
file?: string;
tabs?: string[];
terminal?: string;
};
type MiniEditorProps = {
progress?: number;
backward?: boolean;
code?: string;
focus?: string;
lang?: string;
file?: string;
steps?: MiniEditorStep[];
} & React.PropsWithoutRef<JSX.IntrinsicElements["div"]>;
function MiniEditor({
progress = 0,
backward = false,
code,
focus,
lang,
file,
steps: ogSteps,
...rest
}: MiniEditorProps) {
const { steps, files, stepsByFile } = useSteps(ogSteps, {
code,
focus,
lang,
file,
});
const activeStepIndex = backward ? Math.floor(progress) : Math.ceil(progress);
const activeStep = steps[activeStepIndex];
const activeFile = (activeStep && activeStep.file) || "";
const activeSteps = stepsByFile[activeFile] || [];
const index = activeSteps.indexOf(activeStep);
const activeProgress = Math.min(
Math.max(progress - activeStepIndex + index, 0),
activeSteps.length - 1
);
const tabs = activeStep.tabs || files;
const terminalHeight = getTerminalHeight(steps, progress);
const terminalSteps = steps.map((s) => ({ text: (s && s.terminal) || "" }));
return (
<EditorFrame
files={tabs}
active={activeFile}
terminalPanel={
<TerminalPanel height={terminalHeight}>
<InnerTerminal steps={terminalSteps} progress={progress} />
</TerminalPanel>
}
{...rest}
>
{activeSteps.length > 0 && (
<CodeSurfer
key={activeFile}
progress={activeProgress}
steps={activeSteps}
theme={vsDark}
nonblocking={true}
/>
)}
</EditorFrame>
);
}
function useSteps(
ogSteps: MiniEditorStep[] | undefined,
{ code = "", focus, lang, file }: MiniEditorStep
) {
return React.useMemo(() => {
const steps = ogSteps?.map((s) => ({
code,
focus,
lang,
file,
...s,
})) || [{ code, focus, lang, file }];
const files = [
...new Set(steps.map((s: any) => s.file).filter((f: any) => f != null)),
];
const stepsByFile: Record<string, MiniEditorStep[]> = {};
steps.forEach((s) => {
if (s.file == null) return;
if (!stepsByFile[s.file]) {
stepsByFile[s.file] = [];
}
stepsByFile[s.file].push(s);
});
return { steps, files, stepsByFile };
}, [ogSteps, code, focus, lang, file]);
}
const MAX_HEIGHT = 150;
function getTerminalHeight(steps: any, progress: number) {
if (!steps.length) {
return 0;
}
const prevIndex = Math.floor(progress);
const nextIndex = Math.ceil(progress);
const prevTerminal = steps[prevIndex] && steps[prevIndex].terminal;
const nextTerminal = steps[nextIndex].terminal;
if (!prevTerminal && !nextTerminal) return 0;
if (!prevTerminal && nextTerminal)
return MAX_HEIGHT * Math.min((progress % 1) * 4, 1);
if (prevTerminal && !nextTerminal)
return MAX_HEIGHT * Math.max(1 - (progress % 1) * 4, 0);
return MAX_HEIGHT;
}