|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Psr\Http\Message\ServerRequestInterface; |
3 | 4 | use Yiisoft\ErrorHandler\CompositeException; |
4 | 5 | use Yiisoft\ErrorHandler\Exception\ErrorException; |
| 6 | +use Yiisoft\ErrorHandler\Renderer\HtmlRenderer; |
5 | 7 | use Yiisoft\FriendlyException\FriendlyExceptionInterface; |
6 | 8 |
|
7 | 9 | /** |
8 | | - * @var $this \Yiisoft\ErrorHandler\Renderer\HtmlRenderer |
9 | | - * @var $request \Psr\Http\Message\ServerRequestInterface|null |
10 | | - * @var $throwable \Throwable |
| 10 | + * @var $this HtmlRenderer |
| 11 | + * @var $request ServerRequestInterface|null |
| 12 | + * @var $throwable Throwable |
11 | 13 | */ |
12 | 14 |
|
13 | 15 | $theme = $_COOKIE['yii-exception-theme'] ?? ''; |
@@ -363,28 +365,42 @@ function enableLightTheme() { |
363 | 365 | document.onmouseup = function() { document.getElementsByTagName('body')[0].classList.remove('mousedown'); } |
364 | 366 |
|
365 | 367 | <?php if (empty($theme)): ?> |
366 | | - var theme = getCookie('yii-exception-theme'); |
| 368 | + const themeMedia = window.matchMedia('(prefers-color-scheme: dark)'); |
| 369 | + const theme = getCookie('yii-exception-theme'); |
367 | 370 |
|
368 | 371 | if (theme) { |
| 372 | + applyTheme(theme); |
| 373 | + } else { |
| 374 | + applyTheme((themeMedia.matches ? 'dark-theme' : 'light-theme')); |
| 375 | + } |
| 376 | + |
| 377 | + themeMedia.addEventListener('change', event => { |
| 378 | + if (!theme) { |
| 379 | + applyTheme((event.matches ? 'dark-theme' : 'light-theme')); |
| 380 | + } |
| 381 | + }); |
| 382 | + |
| 383 | + function applyTheme(theme){ |
369 | 384 | document.body.classList.add(theme); |
370 | 385 | } |
| 386 | + |
371 | 387 | <?php endif; ?> |
372 | 388 |
|
373 | 389 | function setCookie(name, value) { |
374 | | - var date = new Date(2100, 0, 1); |
375 | | - var expires = "; expires=" + date.toUTCString(); |
| 390 | + const date = new Date(2100, 0, 1); |
| 391 | + const expires = "; expires=" + date.toUTCString(); |
376 | 392 |
|
377 | 393 | document.cookie = name + "=" + (value || "") + expires + "; path=/"; |
378 | 394 | } |
379 | 395 |
|
380 | 396 | function getCookie(name) { |
381 | | - var nameEQ = name + "="; |
382 | | - var ca = document.cookie.split(';'); |
| 397 | + const nameEQ = name + "="; |
| 398 | + const ca = document.cookie.split(';'); |
383 | 399 |
|
384 | | - for (var i=0; i < ca.length; i++) { |
385 | | - var c = ca[i]; |
386 | | - while (c.charAt(0)==' ') c = c.substring(1,c.length); |
387 | | - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); |
| 400 | + for (let i=0; i < ca.length; i++) { |
| 401 | + let c = ca[i]; |
| 402 | + while (c.charAt(0) === ' ') c = c.substring(1,c.length); |
| 403 | + if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length); |
388 | 404 | } |
389 | 405 |
|
390 | 406 | return null; |
|
0 commit comments