@@ -223,19 +223,54 @@ def segment_length(segment):
223223
224224# Toggle tile click state (for example usage)
225225def toggle_tile (row , col ):
226- # Do not allow toggling for the FREE SPACE cell (center cell)
226+ global clicked_tiles , tile_buttons # Explicitly declare tile_buttons as global
227227 if (row , col ) == (2 , 2 ):
228228 return
229229 key = (row , col )
230230 if key in clicked_tiles :
231- logging .debug (f"Tile at { key } unclicked" )
232231 clicked_tiles .remove (key )
233232 else :
234- logging .debug (f"Tile at { key } clicked" )
235233 clicked_tiles .add (key )
236234
237235 check_winner ()
238- sync_board_state ()
236+
237+ for view_key , (container , tile_buttons_local ) in board_views .items ():
238+ for (r , c ), tile in tile_buttons_local .items ():
239+ phrase = board [r ][c ]
240+ if (r , c ) in clicked_tiles :
241+ new_card_style = f"background-color: { TILE_CLICKED_BG_COLOR } ; color: { TILE_CLICKED_TEXT_COLOR } ; border: none; outline: 3px solid { TILE_CLICKED_TEXT_COLOR } ;"
242+ new_label_color = TILE_CLICKED_TEXT_COLOR
243+ else :
244+ new_card_style = f"background-color: { TILE_UNCLICKED_BG_COLOR } ; color: { TILE_UNCLICKED_TEXT_COLOR } ; border: none;"
245+ new_label_color = TILE_UNCLICKED_TEXT_COLOR
246+
247+ tile ["card" ].style (new_card_style )
248+ lines = split_phrase_into_lines (phrase )
249+ line_count = len (lines )
250+ new_label_style = get_line_style_for_lines (line_count , new_label_color )
251+
252+ for label_info in tile ["labels" ]:
253+ lbl = label_info ["ref" ]
254+ lbl .classes (label_info ["base_classes" ])
255+ lbl .style (new_label_style )
256+ lbl .update ()
257+
258+ tile ["card" ].update ()
259+
260+ container .update ()
261+
262+ try :
263+ js_code = """
264+ setTimeout(function() {
265+ if (typeof fitty !== 'undefined') {
266+ fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
267+ fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
268+ }
269+ }, 50);
270+ """
271+ ui .run_javascript (js_code )
272+ except Exception as e :
273+ logging .debug (f"JavaScript execution failed: { e } " )
239274
240275# Check for Bingo win condition
241276def check_winner ():
@@ -330,12 +365,18 @@ def sync_board_state():
330365 for view_key , (container , tile_buttons_local ) in board_views .items ():
331366 update_tile_styles (tile_buttons_local )
332367
333- # Safely run JavaScript
368+ # Safely run JavaScript to resize text
334369 try :
335- ui .run_javascript (
336- "fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
337- "fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
338- )
370+ # Add a slight delay to ensure DOM updates have propagated
371+ js_code = """
372+ setTimeout(function() {
373+ if (typeof fitty !== 'undefined') {
374+ fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
375+ fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
376+ }
377+ }, 50);
378+ """
379+ ui .run_javascript (js_code )
339380 except Exception as e :
340381 logging .debug (f"JavaScript execution failed (likely disconnected client): { e } " )
341382 except Exception as e :
@@ -442,8 +483,10 @@ def setup_head(background_color: str):
442483 return;
443484 }
444485 // Run fitty to ensure text is resized and centered
445- fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
446- fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
486+ if (typeof fitty !== 'undefined') {
487+ fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
488+ fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
489+ }
447490
448491 // Wait a short period to ensure that the board is fully rendered and styles have settled.
449492 setTimeout(function() {
@@ -460,22 +503,35 @@ def setup_head(background_color: str):
460503 });
461504 }, 500); // Adjust delay if necessary
462505 }
506+
507+ // Function to safely apply fitty
508+ function applyFitty() {
509+ if (typeof fitty !== 'undefined') {
510+ fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
511+ fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
512+ fitty('.fit-header', { multiLine: true, minSize: 10, maxSize: 2000 });
513+ }
514+ }
463515 </script>
464516 """ )
465517
466518 ui .add_head_html (f'<style>body {{ background-color: { background_color } ; }}</style>' )
467519
468520 ui .add_head_html ("""<script>
469- document.addEventListener('DOMContentLoaded', () => {
470- fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
471- fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
472- fitty('.fit-header', { multiLine: true, minSize: 10, maxSize: 2000 });
521+ // Run fitty when DOM is loaded
522+ document.addEventListener('DOMContentLoaded', function() {
523+ setTimeout(applyFitty, 100); // Slight delay to ensure all elements are rendered
473524 });
474- window.addEventListener('resize', () => {
475- fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
476- fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
477- fitty('.fit-header', { multiLine: true, minSize: 10, maxSize: 2000 });
525+
526+ // Run fitty when window is resized
527+ let resizeTimer;
528+ window.addEventListener('resize', function() {
529+ clearTimeout(resizeTimer);
530+ resizeTimer = setTimeout(applyFitty, 100); // Debounce resize events
478531 });
532+
533+ // Periodically check and reapply fitty for any dynamic changes
534+ setInterval(applyFitty, 1000);
479535 </script>""" )
480536
481537 # Use full width with padding so the header spans edge-to-edge
@@ -569,10 +625,16 @@ def update_tile_styles(tile_buttons_dict: dict):
569625
570626 # Safely run JavaScript
571627 try :
572- ui .run_javascript (
573- "fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
574- "fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
575- )
628+ # Add a slight delay to ensure DOM updates have propagated
629+ js_code = """
630+ setTimeout(function() {
631+ if (typeof fitty !== 'undefined') {
632+ fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
633+ fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
634+ }
635+ }, 50);
636+ """
637+ ui .run_javascript (js_code )
576638 except Exception as e :
577639 logging .debug (f"JavaScript execution failed (likely disconnected client): { e } " )
578640
@@ -631,10 +693,16 @@ def has_too_many_repeats(phrase, threshold=0.5):
631693
632694 # Safely run JavaScript
633695 try :
634- ui .run_javascript (
635- "fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
636- "fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
637- )
696+ # Add a slight delay to ensure DOM updates have propagated
697+ js_code = """
698+ setTimeout(function() {
699+ if (typeof fitty !== 'undefined') {
700+ fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
701+ fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
702+ }
703+ }, 50);
704+ """
705+ ui .run_javascript (js_code )
638706 except Exception as e :
639707 logging .debug (f"JavaScript execution failed (likely disconnected client): { e } " )
640708
0 commit comments