|
1 | 1 | if (!window.MathJax) { |
| 2 | + const problems = []; |
| 3 | + |
2 | 4 | window.MathJax = { |
3 | 5 | tex: { packages: { '[+]': webworkConfig?.showMathJaxErrors ? [] : ['noerrors'] } }, |
4 | 6 | loader: { |
@@ -106,9 +108,12 @@ if (!window.MathJax) { |
106 | 108 | }, |
107 | 109 | pageReady() { |
108 | 110 | return MathJax.startup.defaultPageReady().then(() => { |
109 | | - for (const problemBody of document.querySelectorAll('.problem-content')) { |
110 | | - problemBody.style.visibility = ''; |
| 111 | + for (const [problemContent, loaderOverlay, resizeObserver] of problems) { |
| 112 | + resizeObserver.disconnect(); |
| 113 | + loaderOverlay.remove(); |
| 114 | + problemContent.style.visibility = ''; |
111 | 115 | } |
| 116 | + problems.length = 0; |
112 | 117 | }); |
113 | 118 | } |
114 | 119 | }, |
@@ -142,7 +147,45 @@ if (!window.MathJax) { |
142 | 147 | } |
143 | 148 | }; |
144 | 149 |
|
145 | | - for (const problemBody of document.querySelectorAll('.problem-content')) { |
146 | | - problemBody.style.visibility = 'hidden'; |
| 150 | + for (const problemContent of document.querySelectorAll('.problem-content')) { |
| 151 | + problemContent.style.visibility = 'hidden'; |
| 152 | + const loaderOverlay = document.createElement('div'); |
| 153 | + loaderOverlay.classList.add('problem-content'); |
| 154 | + const bodyRectangle = problemContent.getBoundingClientRect(); |
| 155 | + loaderOverlay.style.position = 'absolute'; |
| 156 | + loaderOverlay.style.top = `${bodyRectangle.y}px`; |
| 157 | + loaderOverlay.style.left = `${bodyRectangle.x}px`; |
| 158 | + loaderOverlay.style.width = `${bodyRectangle.width}px`; |
| 159 | + loaderOverlay.style.height = `${bodyRectangle.height}px`; |
| 160 | + loaderOverlay.style.overflow = 'clip'; |
| 161 | + loaderOverlay.style.transition = 'height 0.3s ease'; |
| 162 | + |
| 163 | + loaderOverlay.classList.add( |
| 164 | + 'd-flex', |
| 165 | + 'flex-column', |
| 166 | + 'justify-content-start', |
| 167 | + 'align-items-center', |
| 168 | + 'text-dark' |
| 169 | + ); |
| 170 | + |
| 171 | + const loaderText = document.createElement('div'); |
| 172 | + loaderText.textContent = 'Loading...'; |
| 173 | + loaderText.classList.add('fs-1'); |
| 174 | + |
| 175 | + const loaderIcon = document.createElement('i'); |
| 176 | + loaderIcon.classList.add('fa-solid', 'fa-spinner', 'fa-spin', 'fa-2x'); |
| 177 | + |
| 178 | + loaderOverlay.append(loaderText, loaderIcon); |
| 179 | + |
| 180 | + problemContent.after(loaderOverlay); |
| 181 | + const resizeObserver = new ResizeObserver(() => { |
| 182 | + const bodyRectangle = problemContent.getBoundingClientRect(); |
| 183 | + loaderOverlay.style.top = `${bodyRectangle.top}px`; |
| 184 | + loaderOverlay.style.left = `${bodyRectangle.left}px`; |
| 185 | + loaderOverlay.style.width = `${bodyRectangle.width}px`; |
| 186 | + loaderOverlay.style.height = `${bodyRectangle.height}px`; |
| 187 | + }); |
| 188 | + resizeObserver.observe(problemContent); |
| 189 | + problems.push([problemContent, loaderOverlay, resizeObserver]); |
147 | 190 | } |
148 | 191 | } |
0 commit comments