Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

extra.js 3.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  1. // Function that applies light/dark theme based on the user's preference
  2. const applyAutoTheme = () => {
  3. // Determine the user's preferred color scheme
  4. const prefersLight = window.matchMedia("(prefers-color-scheme: light)").matches;
  5. const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
  6. // Apply the appropriate attributes based on the user's preference
  7. if (prefersLight) {
  8. document.body.setAttribute("data-md-color-scheme", "default");
  9. document.body.setAttribute("data-md-color-primary", "indigo");
  10. } else if (prefersDark) {
  11. document.body.setAttribute("data-md-color-scheme", "slate");
  12. document.body.setAttribute("data-md-color-primary", "black");
  13. }
  14. };
  15. // Function that checks and applies light/dark theme based on the user's preference (if auto theme is enabled)
  16. function checkAutoTheme() {
  17. // Array of supported language codes -> each language has its own palette (stored in local storage)
  18. const supportedLangCodes = ["en", "zh", "ko", "ja", "ru", "de", "fr", "es", "pt", "it", "tr", "vi", "nl"];
  19. // Get the URL path
  20. const path = window.location.pathname;
  21. // Extract the language code from the URL (assuming it's in the format /xx/...)
  22. const langCode = path.split("/")[1];
  23. // Check if the extracted language code is in the supported languages
  24. const isValidLangCode = supportedLangCodes.includes(langCode);
  25. // Construct the local storage key based on the language code if valid, otherwise default to the root key
  26. const localStorageKey = isValidLangCode ? `/${langCode}/.__palette` : "/.__palette";
  27. // Retrieve the palette from local storage using the constructed key
  28. const palette = localStorage.getItem(localStorageKey);
  29. if (palette) {
  30. // Check if the palette's index is 0 (auto theme)
  31. const paletteObj = JSON.parse(palette);
  32. if (paletteObj && paletteObj.index === 0) {
  33. applyAutoTheme();
  34. }
  35. }
  36. }
  37. // Run function when the script loads
  38. checkAutoTheme();
  39. // Re-run the function when the user's preference changes (when the user changes their system theme)
  40. window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", checkAutoTheme);
  41. window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", checkAutoTheme);
  42. // Re-run the function when the palette changes (e.g. user switched from dark theme to auto theme)
  43. // ! We can't use window.addEventListener("storage", checkAutoTheme) because it will NOT be triggered on the current tab
  44. // ! So we have to use the following workaround:
  45. // Get the palette input for auto theme
  46. var autoThemeInput = document.getElementById("__palette_1");
  47. if (autoThemeInput) {
  48. // Add a click event listener to the input
  49. autoThemeInput.addEventListener("click", function () {
  50. // Check if the auto theme is selected
  51. if (autoThemeInput.checked) {
  52. // Re-run the function after a short delay (to ensure that the palette has been updated)
  53. setTimeout(applyAutoTheme);
  54. }
  55. });
  56. }
  57. // Add iframe navigation
  58. window.onhashchange = function() {
  59. window.parent.postMessage({
  60. type: 'navigation',
  61. hash: window.location.pathname + window.location.search + window.location.hash
  62. }, '*');
  63. };
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...