@@ -4,17 +4,14 @@ import { InnerCode, updateEditorStep } from "./code"
4
4
5
5
const SectionContext = React . createContext < {
6
6
props : EditorProps
7
- selectedId ?: string
8
7
setFocus : ( x : {
9
8
fileName ?: string
10
9
focus : string | null
11
10
id : string
12
11
} ) => void
13
- resetFocus : ( ) => void
14
12
} > ( {
15
13
props : null ! ,
16
14
setFocus : ( ) => { } ,
17
- resetFocus : ( ) => { } ,
18
15
} )
19
16
20
17
export function Section ( {
@@ -48,23 +45,27 @@ export function Section({
48
45
const { selectedId, ...rest } = state
49
46
50
47
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 >
61
63
)
62
64
}
63
65
64
66
export function SectionCode ( ) {
65
- const { props, setFocus } = React . useContext (
66
- SectionContext
67
- )
67
+ const { props, setFocus } =
68
+ React . useContext ( SectionContext )
68
69
69
70
const onTabClick = ( filename : string ) => {
70
71
setFocus ( { fileName : filename , focus : null , id : "" } )
@@ -73,6 +74,8 @@ export function SectionCode() {
73
74
return < InnerCode { ...props } onTabClick = { onTabClick } />
74
75
}
75
76
77
+ // ---
78
+
76
79
export function SectionLink ( {
77
80
focus,
78
81
file,
@@ -84,27 +87,79 @@ export function SectionLink({
84
87
file ?: string
85
88
children : React . ReactNode
86
89
} ) {
87
- const {
88
- setFocus,
89
- resetFocus,
90
- selectedId,
91
- } = React . useContext ( SectionContext )
90
+ const { activate, reset, activatedId } =
91
+ React . useContext ( LinkableContext )
92
92
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 })
97
97
98
98
return (
99
99
< 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}
107
103
children = { children }
104
+ onMouseOver = { ( ) =>
105
+ activate ( { fileName : file , focus, id } )
106
+ }
107
+ onMouseOut = { reset }
108
108
/>
109
109
)
110
110
}
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