@@ -174,3 +174,54 @@ document.querySelectorAll(".vizdom.shadow").forEach((container) => {
174
174
}
175
175
} ) ;
176
176
} ) ;
177
+
178
+ // interactivity for d2 diagrams
179
+ document . querySelectorAll ( ".d2.shadow" ) . forEach ( ( container ) => {
180
+ const data = container . getAttribute ( "data-beoe" )
181
+ ? JSON . parse ( container . getAttribute ( "data-beoe" ) ! )
182
+ : null ;
183
+
184
+ if ( ! data ) return ;
185
+ const graph = json . read ( data ) ;
186
+
187
+ function clear ( ) {
188
+ container
189
+ . querySelectorAll ( ".shape,.connection" )
190
+ . forEach ( ( node ) => node . classList . remove ( "shadow" ) ) ;
191
+ }
192
+
193
+ function highlight ( id : string ) {
194
+ container
195
+ . querySelectorAll ( ".shape,.connection" )
196
+ . forEach ( ( node ) => node . classList . add ( "shadow" ) ) ;
197
+ alg . postorder ( graph , [ id ] ) . forEach ( ( node ) => {
198
+ container
199
+ . querySelector ( `#${ CSS . escape ( node ) } .shape` )
200
+ ?. classList . remove ( "shadow" ) ;
201
+ graph . outEdges ( node ) ?. forEach ( ( { name } ) => {
202
+ container
203
+ . querySelector ( `#${ CSS . escape ( name ! ) } ` )
204
+ ?. classList . remove ( "shadow" ) ;
205
+ } ) ;
206
+ } ) ;
207
+ }
208
+
209
+ // highlight on hover
210
+ let currentHover : string | null = null ;
211
+ container . addEventListener ( "mouseover" , ( e ) => {
212
+ // @ts -expect-error
213
+ const node = e . target ?. closest ( ".shape" ) ;
214
+
215
+ if ( node ) {
216
+ const id = node . parentElement . getAttribute ( "id" ) ;
217
+ if ( currentHover == id ) return ;
218
+ clear ( ) ;
219
+ highlight ( id ) ;
220
+ currentHover = id ;
221
+ } else {
222
+ if ( currentHover == null ) return ;
223
+ clear ( ) ;
224
+ currentHover = null ;
225
+ }
226
+ } ) ;
227
+ } ) ;
0 commit comments