1- function resizeText ( multiplier ) {
2- if ( document . body . style . fontSize == "" ) {
3- document . body . style . fontSize = "1.0em" ;
1+ // Track if initialisation has been performed
2+ var isInitialised = false ;
3+
4+ // Track cumulative resize value
5+ var cumulativeResize = 0 ;
6+ var minResize = - 3 ;
7+ var maxResize = 6 ;
8+
9+ function initialise ( ) {
10+ if ( isInitialised ) return ; // Prevent double initialisation
11+ isInitialised = true ;
12+ applyToArticle ( function ( element ) {
13+ var computedStyle = window . getComputedStyle ( element ) ;
14+ var currentFontSize = parseFloat ( computedStyle . fontSize ) ;
15+ element . style . fontSize = currentFontSize + "px" ;
16+ } ) ;
17+ }
18+
19+ function resizeText ( multiplier ) {
20+ // Calculate new cumulative value
21+ var newCumulative = cumulativeResize + multiplier ;
22+
23+ // Check if the new value would be within bounds
24+ if ( newCumulative < minResize || newCumulative > maxResize ) {
25+ console . log ( 'Resize limit reached. Cumulative: ' + cumulativeResize + ', Attempted: ' + newCumulative ) ;
26+ return ;
27+ }
28+
29+ // Update cumulative value and proceed with resize
30+ cumulativeResize = newCumulative ;
31+
32+ function resize ( element ) {
33+ var computedStyle = window . getComputedStyle ( element ) ;
34+ var currentFontSize = parseFloat ( computedStyle . fontSize ) ;
35+ var newFontSize = Math . ceil ( currentFontSize + ( multiplier * 0.2 * currentFontSize ) ) ;
36+ element . style . fontSize = newFontSize + "px" ;
37+ element . style . setProperty ( 'font-size' , newFontSize + "px" , 'important' ) ;
438 }
5- document . body . style . fontSize = parseFloat ( document . body . style . fontSize ) + ( multiplier * 0.2 ) + "em" ;
39+
40+ applyToArticle ( resize ) ;
641}
742
8- $ ( "#dyslexia-mode" ) . click ( function ( e ) {
9- e . preventDefault ( ) ;
10- return $ ( '#article' ) . toggleClass ( 'dyslexia-friendly' ) ;
11- } ) ;
43+ function toggleDyslexia ( ) {
44+ function toggleDyslexiaClass ( element ) {
45+ element . classList . toggle ( 'dyslexia-friendly' ) ;
46+ }
47+ applyToArticle ( toggleDyslexiaClass ) ;
48+ }
49+
50+ function applyToArticle ( textFunction ) {
51+ if ( ! isInitialised ) {
52+ initialise ( ) ;
53+ }
54+ var articleElement = document . getElementById ( 'article' ) ;
55+ if ( ! articleElement ) return ;
56+ var allElements = articleElement . querySelectorAll (
57+ 'h1, h2, h3, h4, h5, h6, ' +
58+ 'blockquote, div, p, pre, ' +
59+ 'li, caption, table, tbody, td, tfoot, th, thead, tr, ' +
60+ 'a, b, em, i, label, small, span, strong, code'
61+ ) ;
62+ allElements . forEach ( textFunction ) ;
63+ }
0 commit comments