-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from code-hike/slides
Slideshow component
- Loading branch information
Showing
12 changed files
with
966 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
.ch-slideshow { | ||
margin: 1rem 0; | ||
} | ||
|
||
.ch-slideshow-slide { | ||
display: flex; | ||
flex-flow: row; | ||
gap: 0.5rem; | ||
align-items: stretch; | ||
aspect-ratio: 16 / 9; | ||
} | ||
|
||
.ch-slideshow-slide .ch-editor-frame, | ||
.ch-slideshow-slide .ch-code { | ||
flex: 2; | ||
} | ||
|
||
.ch-slideshow-preview { | ||
flex: 1; | ||
height: auto; | ||
min-width: 0; | ||
} | ||
|
||
.ch-slideshow-range { | ||
display: flex; | ||
flex-flow: row; | ||
gap: 0.5rem; | ||
} | ||
|
||
.ch-slideshow-range input { | ||
flex: 1; | ||
} | ||
|
||
.ch-slideshow-notes { | ||
border-radius: 0.25rem; | ||
margin-top: 1rem; | ||
padding: 1rem; | ||
border: 1px solid #e3e3e3; | ||
} | ||
|
||
.ch-slideshow-note { | ||
min-height: 140px; | ||
max-height: 140px; | ||
padding: 0.05px; | ||
overflow: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React from "react" | ||
import { | ||
EditorProps, | ||
EditorStep, | ||
} from "@code-hike/mini-editor" | ||
import { InnerCode, updateEditorStep } from "./code" | ||
import { Preview, PresetConfig } from "./preview" | ||
|
||
export function Slideshow({ | ||
children, | ||
editorSteps, | ||
codeConfig, | ||
presetConfig, | ||
code, | ||
}: { | ||
children: React.ReactNode | ||
editorSteps: EditorStep[] | ||
codeConfig: EditorProps["codeConfig"] | ||
presetConfig?: PresetConfig | ||
code?: EditorProps["codeConfig"] | ||
}) { | ||
const stepsChildren = React.Children.toArray(children) | ||
|
||
const hasNotes = stepsChildren.some( | ||
(child: any) => child.props?.children | ||
) | ||
|
||
const [state, setState] = React.useState({ | ||
stepIndex: 0, | ||
step: editorSteps[0], | ||
}) | ||
const tab = state.step | ||
|
||
function onTabClick(filename: string) { | ||
const newStep = updateEditorStep( | ||
state.step, | ||
filename, | ||
null | ||
) | ||
setState({ ...state, step: newStep }) | ||
} | ||
|
||
return ( | ||
<div | ||
className={`ch-slideshow ${ | ||
presetConfig ? "ch-slideshow-with-preview" : "" | ||
}`} | ||
> | ||
<div className="ch-slideshow-slide"> | ||
<InnerCode | ||
{...(tab as any)} | ||
codeConfig={{ | ||
...codeConfig, | ||
...code, | ||
}} | ||
onTabClick={onTabClick} | ||
/> | ||
{presetConfig && ( | ||
<Preview | ||
className="ch-slideshow-preview" | ||
files={tab.files} | ||
presetConfig={presetConfig} | ||
/> | ||
)} | ||
</div> | ||
|
||
<div className="ch-slideshow-notes"> | ||
<div className="ch-slideshow-range"> | ||
<button | ||
onClick={() => | ||
setState(s => { | ||
const stepIndex = Math.max( | ||
0, | ||
s.stepIndex - 1 | ||
) | ||
return { | ||
stepIndex, | ||
step: editorSteps[stepIndex], | ||
} | ||
}) | ||
} | ||
> | ||
Prev | ||
</button> | ||
<input | ||
type="range" | ||
min={0} | ||
max={editorSteps.length - 1} | ||
value={state.stepIndex} | ||
step={1} | ||
onChange={e => | ||
setState({ | ||
stepIndex: +e.target.value, | ||
step: editorSteps[+e.target.value], | ||
}) | ||
} | ||
/> | ||
<button | ||
onClick={() => | ||
setState(s => { | ||
const stepIndex = Math.min( | ||
editorSteps.length - 1, | ||
s.stepIndex + 1 | ||
) | ||
return { | ||
stepIndex, | ||
step: editorSteps[stepIndex], | ||
} | ||
}) | ||
} | ||
> | ||
Next | ||
</button> | ||
</div> | ||
|
||
{hasNotes && ( | ||
<div className="ch-slideshow-note"> | ||
{stepsChildren[state.stepIndex]} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { visitAsync, toJSX } from "./unist-utils" | ||
import { Node, Parent } from "unist" | ||
import { extractStepsInfo } from "./steps" | ||
import { getPresetConfig } from "./preview" | ||
|
||
export async function transformSlideshows( | ||
tree: Node, | ||
config: { theme: any } | ||
) { | ||
await visitAsync( | ||
tree, | ||
"mdxJsxFlowElement", | ||
async node => { | ||
if (node.name === "CH.Slideshow") { | ||
await transformSlideshow(node, config) | ||
} | ||
} | ||
) | ||
} | ||
async function transformSlideshow( | ||
node: Node, | ||
{ theme }: { theme: any } | ||
) { | ||
const editorSteps = await extractStepsInfo( | ||
node as Parent, | ||
{ theme }, | ||
"merge step with previous" | ||
) | ||
|
||
const presetConfig = await getPresetConfig( | ||
(node as any).attributes | ||
) | ||
|
||
toJSX(node, { | ||
props: { | ||
codeConfig: { theme }, | ||
editorSteps: editorSteps, | ||
presetConfig, | ||
}, | ||
appendProps: true, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
flex: 1; | ||
padding: 0 10px; | ||
color: #544; | ||
min-width: 5px; | ||
} | ||
|
||
.ch-browser-button { | ||
|
Oops, something went wrong.