|
| 1 | +import React from "react" |
| 2 | +import { |
| 3 | + EditorProps, |
| 4 | + EditorStep, |
| 5 | +} from "@code-hike/mini-editor" |
| 6 | +import { InnerCode, updateEditorStep } from "./code" |
| 7 | +import { Preview, PresetConfig } from "./preview" |
| 8 | + |
| 9 | +export function Slideshow({ |
| 10 | + children, |
| 11 | + editorSteps, |
| 12 | + codeConfig, |
| 13 | + presetConfig, |
| 14 | +}: { |
| 15 | + children: React.ReactNode |
| 16 | + editorSteps: EditorStep[] |
| 17 | + codeConfig: EditorProps["codeConfig"] |
| 18 | + presetConfig?: PresetConfig |
| 19 | +}) { |
| 20 | + const stepsChildren = React.Children.toArray(children) |
| 21 | + |
| 22 | + const hasNotes = stepsChildren.some( |
| 23 | + (child: any) => child.props?.children |
| 24 | + ) |
| 25 | + |
| 26 | + const [state, setState] = React.useState({ |
| 27 | + stepIndex: 0, |
| 28 | + step: editorSteps[0], |
| 29 | + }) |
| 30 | + const tab = state.step |
| 31 | + |
| 32 | + function onTabClick(filename: string) { |
| 33 | + const newStep = updateEditorStep( |
| 34 | + state.step, |
| 35 | + filename, |
| 36 | + null |
| 37 | + ) |
| 38 | + setState({ ...state, step: newStep }) |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + className={`ch-slideshow ${ |
| 44 | + presetConfig ? "ch-slideshow-with-preview" : "" |
| 45 | + }`} |
| 46 | + > |
| 47 | + <div className="ch-slideshow-slide"> |
| 48 | + <InnerCode |
| 49 | + {...(tab as any)} |
| 50 | + codeConfig={codeConfig} |
| 51 | + onTabClick={onTabClick} |
| 52 | + /> |
| 53 | + {presetConfig && ( |
| 54 | + <Preview |
| 55 | + className="ch-slideshow-preview" |
| 56 | + files={tab.files} |
| 57 | + presetConfig={presetConfig} |
| 58 | + /> |
| 59 | + )} |
| 60 | + </div> |
| 61 | + |
| 62 | + <div className="ch-slideshow-notes"> |
| 63 | + <div className="ch-slideshow-range"> |
| 64 | + <button |
| 65 | + onClick={() => |
| 66 | + setState(s => { |
| 67 | + const stepIndex = Math.max( |
| 68 | + 0, |
| 69 | + s.stepIndex - 1 |
| 70 | + ) |
| 71 | + return { |
| 72 | + stepIndex, |
| 73 | + step: editorSteps[stepIndex], |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | + > |
| 78 | + Prev |
| 79 | + </button> |
| 80 | + <input |
| 81 | + type="range" |
| 82 | + min={0} |
| 83 | + max={editorSteps.length - 1} |
| 84 | + value={state.stepIndex} |
| 85 | + step={1} |
| 86 | + onChange={e => |
| 87 | + setState({ |
| 88 | + stepIndex: +e.target.value, |
| 89 | + step: editorSteps[+e.target.value], |
| 90 | + }) |
| 91 | + } |
| 92 | + /> |
| 93 | + <button |
| 94 | + onClick={() => |
| 95 | + setState(s => { |
| 96 | + const stepIndex = Math.min( |
| 97 | + editorSteps.length - 1, |
| 98 | + s.stepIndex + 1 |
| 99 | + ) |
| 100 | + return { |
| 101 | + stepIndex, |
| 102 | + step: editorSteps[stepIndex], |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | + > |
| 107 | + Next |
| 108 | + </button> |
| 109 | + </div> |
| 110 | + |
| 111 | + {hasNotes && ( |
| 112 | + <div className="ch-slideshow-note"> |
| 113 | + {stepsChildren[state.stepIndex]} |
| 114 | + </div> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ) |
| 119 | +} |
0 commit comments