|
| 1 | +import React from "react" |
| 2 | +import { useSpring } from "use-spring" |
| 3 | +import { |
| 4 | + EditorTransition, |
| 5 | + EditorTransitionProps, |
| 6 | +} from "./editor-transition" |
| 7 | +import { EditorStep, StepFile } from "./use-snapshots" |
| 8 | +import { CodeProps } from "./code" |
| 9 | + |
| 10 | +export { MiniEditor, MiniEditorProps } |
| 11 | + |
| 12 | +type SingleFileEditorProps = { |
| 13 | + code: string |
| 14 | + lang: string |
| 15 | + focus?: string |
| 16 | + filename?: string |
| 17 | + terminal?: string |
| 18 | + frameProps: Partial<EditorTransitionProps> |
| 19 | + codeProps: Partial<CodeProps> |
| 20 | +} |
| 21 | +type SinglePanelEditorProps = { |
| 22 | + files: StepFile[] |
| 23 | + active: string |
| 24 | + terminal?: string |
| 25 | + frameProps: Partial<EditorTransitionProps> |
| 26 | + codeProps: Partial<CodeProps> |
| 27 | +} |
| 28 | +type TwoPanelEditorProps = EditorStep & { |
| 29 | + frameProps: Partial<EditorTransitionProps> |
| 30 | + codeProps: Partial<CodeProps> |
| 31 | +} |
| 32 | +type MiniEditorProps = |
| 33 | + | SingleFileEditorProps |
| 34 | + | SinglePanelEditorProps |
| 35 | + | TwoPanelEditorProps |
| 36 | + |
| 37 | +function MiniEditor(props: MiniEditorProps) { |
| 38 | + if ("northPanel" in props) { |
| 39 | + return <TwoPanelEditor {...props} /> |
| 40 | + } else if ("active" in props) { |
| 41 | + return <SinglePanelEditor {...props} /> |
| 42 | + } else { |
| 43 | + return <SingleFileEditor {...props} /> |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function SingleFileEditor({ |
| 48 | + code = "", |
| 49 | + lang = "js", |
| 50 | + focus, |
| 51 | + filename = "", |
| 52 | + terminal, |
| 53 | + frameProps, |
| 54 | + codeProps, |
| 55 | +}: SingleFileEditorProps) { |
| 56 | + const step = React.useMemo(() => { |
| 57 | + const step: EditorStep = { |
| 58 | + files: [{ name: filename, code, lang, focus }], |
| 59 | + northPanel: { |
| 60 | + active: filename, |
| 61 | + tabs: [filename], |
| 62 | + heightRatio: 1, |
| 63 | + }, |
| 64 | + terminal, |
| 65 | + } |
| 66 | + return step |
| 67 | + }, [code, lang, focus, filename, terminal]) |
| 68 | + |
| 69 | + const { prev, next, t } = useStepSpring(step) |
| 70 | + return ( |
| 71 | + <EditorTransition |
| 72 | + {...frameProps} |
| 73 | + t={t} |
| 74 | + backward={false} |
| 75 | + prev={prev} |
| 76 | + next={next} |
| 77 | + codeProps={codeProps} |
| 78 | + /> |
| 79 | + ) |
| 80 | +} |
| 81 | +function SinglePanelEditor({ |
| 82 | + files, |
| 83 | + active, |
| 84 | + terminal, |
| 85 | + frameProps, |
| 86 | + codeProps, |
| 87 | +}: SinglePanelEditorProps) { |
| 88 | + const step = React.useMemo(() => { |
| 89 | + const tabs = files.map(file => file.name) |
| 90 | + const step: EditorStep = { |
| 91 | + files, |
| 92 | + northPanel: { |
| 93 | + active, |
| 94 | + tabs, |
| 95 | + heightRatio: 1, |
| 96 | + }, |
| 97 | + terminal, |
| 98 | + } |
| 99 | + return step |
| 100 | + }, [files, active, terminal]) |
| 101 | + |
| 102 | + const { prev, next, t } = useStepSpring(step) |
| 103 | + return ( |
| 104 | + <EditorTransition |
| 105 | + {...frameProps} |
| 106 | + t={t} |
| 107 | + backward={false} |
| 108 | + prev={prev} |
| 109 | + next={next} |
| 110 | + codeProps={codeProps} |
| 111 | + /> |
| 112 | + ) |
| 113 | +} |
| 114 | +function TwoPanelEditor({ |
| 115 | + frameProps, |
| 116 | + codeProps, |
| 117 | + northPanel, |
| 118 | + southPanel, |
| 119 | + files, |
| 120 | + terminal, |
| 121 | +}: TwoPanelEditorProps) { |
| 122 | + const step = React.useMemo(() => { |
| 123 | + return { |
| 124 | + northPanel, |
| 125 | + southPanel, |
| 126 | + files, |
| 127 | + terminal, |
| 128 | + } |
| 129 | + }, [northPanel, southPanel, files, terminal]) |
| 130 | + |
| 131 | + const { prev, next, t } = useStepSpring(step) |
| 132 | + return ( |
| 133 | + <EditorTransition |
| 134 | + {...frameProps} |
| 135 | + t={t} |
| 136 | + backward={false} |
| 137 | + prev={prev} |
| 138 | + next={next} |
| 139 | + codeProps={codeProps} |
| 140 | + /> |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +function useStepSpring(step: EditorStep) { |
| 145 | + const [{ target, prev, next }, setState] = React.useState( |
| 146 | + { |
| 147 | + target: 0, |
| 148 | + prev: step, |
| 149 | + next: step, |
| 150 | + } |
| 151 | + ) |
| 152 | + |
| 153 | + React.useEffect(() => { |
| 154 | + if (next != step) { |
| 155 | + setState(s => ({ |
| 156 | + target: s.target + 1, |
| 157 | + prev: next, |
| 158 | + next: step, |
| 159 | + })) |
| 160 | + } |
| 161 | + }, [step]) |
| 162 | + |
| 163 | + const [progress] = useSpring(target, { |
| 164 | + stiffness: 256, |
| 165 | + damping: 24, |
| 166 | + mass: 0.2, |
| 167 | + decimals: 3, |
| 168 | + }) |
| 169 | + |
| 170 | + const t = progress % 1 |
| 171 | + |
| 172 | + return { prev, next, t: t || 1 } |
| 173 | +} |
0 commit comments