@@ -4,17 +4,14 @@ import { InnerCode, updateEditorStep } from "./code"
44
55const SectionContext = React . createContext < {
66 props : EditorProps
7- selectedId ?: string
87 setFocus : ( x : {
98 fileName ?: string
109 focus : string | null
1110 id : string
1211 } ) => void
13- resetFocus : ( ) => void
1412} > ( {
1513 props : null ! ,
1614 setFocus : ( ) => { } ,
17- resetFocus : ( ) => { } ,
1815} )
1916
2017export function Section ( {
@@ -48,23 +45,27 @@ export function Section({
4845 const { selectedId, ...rest } = state
4946
5047 return (
51- < SectionContext . Provider
52- value = { {
53- props : rest ,
54- setFocus,
55- resetFocus,
56- selectedId,
57- } }
58- >
59- < section > { children } </ section >
60- </ SectionContext . Provider >
48+ < section >
49+ < SectionContext . Provider
50+ value = { {
51+ props : rest ,
52+ setFocus,
53+ } }
54+ >
55+ < LinkableSection
56+ onActivation = { setFocus }
57+ onReset = { resetFocus }
58+ >
59+ { children }
60+ </ LinkableSection >
61+ </ SectionContext . Provider >
62+ </ section >
6163 )
6264}
6365
6466export function SectionCode ( ) {
65- const { props, setFocus } = React . useContext (
66- SectionContext
67- )
67+ const { props, setFocus } =
68+ React . useContext ( SectionContext )
6869
6970 const onTabClick = ( filename : string ) => {
7071 setFocus ( { fileName : filename , focus : null , id : "" } )
@@ -73,6 +74,8 @@ export function SectionCode() {
7374 return < InnerCode { ...props } onTabClick = { onTabClick } />
7475}
7576
77+ // ---
78+
7679export function SectionLink ( {
7780 focus,
7881 file,
@@ -84,27 +87,79 @@ export function SectionLink({
8487 file ?: string
8588 children : React . ReactNode
8689} ) {
87- const {
88- setFocus,
89- resetFocus,
90- selectedId,
91- } = React . useContext ( SectionContext )
90+ const { activate, reset, activatedId } =
91+ React . useContext ( LinkableContext )
9292
93- const isSelected = selectedId === id
94- const handleClick = isSelected
95- ? resetFocus
96- : ( ) => setFocus ( { fileName : file , focus, id } )
93+ const isSelected = activatedId === id
94+ // const handleClick = isSelected
95+ // ? resetFocus
96+ // : () => setFocus({ fileName: file, focus, id })
9797
9898 return (
9999 < span
100- style = { {
101- textDecoration : "underline" ,
102- textDecorationStyle : "dotted" ,
103- cursor : "pointer" ,
104- backgroundColor : isSelected ? "yellow" : undefined ,
105- } }
106- onClick = { handleClick }
100+ className = "ch-section-link"
101+ data-active = { isSelected }
102+ // onClick={handleClick}
107103 children = { children }
104+ onMouseOver = { ( ) =>
105+ activate ( { fileName : file , focus, id } )
106+ }
107+ onMouseOut = { reset }
108108 />
109109 )
110110}
111+
112+ const LinkableContext = React . createContext < {
113+ activate : ( x : {
114+ fileName ?: string
115+ focus : string | null
116+ id : string
117+ } ) => void
118+ reset : ( ) => void
119+ activatedId : string | undefined
120+ } > ( {
121+ activatedId : undefined ,
122+ activate : ( ) => { } ,
123+ reset : ( ) => { } ,
124+ } )
125+
126+ export function LinkableSection ( {
127+ onActivation,
128+ onReset,
129+ children,
130+ } : {
131+ onActivation : ( x : {
132+ fileName ?: string
133+ focus : string | null
134+ id : string
135+ } ) => void
136+ onReset : ( ) => void
137+ children : React . ReactNode
138+ } ) {
139+ const [ activatedId , setActivatedId ] =
140+ React . useState < any > ( undefined )
141+
142+ const activate = React . useCallback (
143+ x => {
144+ setActivatedId ( x . id )
145+ onActivation ( x )
146+ } ,
147+ [ onActivation ]
148+ )
149+ const reset = React . useCallback ( ( ) => {
150+ setActivatedId ( undefined )
151+ onReset ( )
152+ } , [ onReset ] )
153+
154+ return (
155+ < LinkableContext . Provider
156+ value = { {
157+ activate,
158+ reset,
159+ activatedId,
160+ } }
161+ >
162+ { children }
163+ </ LinkableContext . Provider >
164+ )
165+ }
0 commit comments