|
| 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 | + |
| 7 | + // Apply the appropriate attributes based on the user's preference |
| 8 | + if (prefersLight) { |
| 9 | + document.body.setAttribute("data-md-color-scheme", "default"); |
| 10 | + document.body.setAttribute("data-md-color-primary", "indigo"); |
| 11 | + } else if (prefersDark) { |
| 12 | + document.body.setAttribute("data-md-color-scheme", "slate"); |
| 13 | + document.body.setAttribute("data-md-color-primary", "black"); |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +// Function that checks and applies light/dark theme based on the user's preference (if auto theme is enabled) |
| 18 | +function checkAutoTheme() { |
| 19 | + // Array of supported language codes -> each language has its own palette (stored in local storage) |
| 20 | + const supportedLangCodes = ["en", "zh", "ko", "ja", "ru", "de", "fr", "es", "pt", "it", "tr", "vi", "nl"]; |
| 21 | + // Get the URL path |
| 22 | + const path = window.location.pathname; |
| 23 | + // Extract the language code from the URL (assuming it's in the format /xx/...) |
| 24 | + const langCode = path.split("/")[1]; |
| 25 | + // Check if the extracted language code is in the supported languages |
| 26 | + const isValidLangCode = supportedLangCodes.includes(langCode); |
| 27 | + // Construct the local storage key based on the language code if valid, otherwise default to the root key |
| 28 | + const localStorageKey = isValidLangCode ? `/${langCode}/.__palette` : "/.__palette"; |
| 29 | + // Retrieve the palette from local storage using the constructed key |
| 30 | + const palette = localStorage.getItem(localStorageKey); |
| 31 | + if (palette) { |
| 32 | + // Check if the palette's index is 0 (auto theme) |
| 33 | + const paletteObj = JSON.parse(palette); |
| 34 | + if (paletteObj && paletteObj.index === 0) { |
| 35 | + applyAutoTheme(); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Run function when the script loads |
| 41 | +checkAutoTheme(); |
| 42 | + |
| 43 | +// Re-run the function when the user's preference changes (when the user changes their system theme) |
| 44 | +window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", checkAutoTheme); |
| 45 | +window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", checkAutoTheme); |
| 46 | + |
| 47 | +// Re-run the function when the palette changes (e.g. user switched from dark theme to auto theme) |
| 48 | +// ! We can't use window.addEventListener("storage", checkAutoTheme) because it will NOT be triggered on the current tab |
| 49 | +// ! So we have to use the following workaround: |
| 50 | +// Get the palette input for auto theme |
| 51 | +var autoThemeInput = document.getElementById("__palette_1"); |
| 52 | +if (autoThemeInput) { |
| 53 | + // Add a click event listener to the input |
| 54 | + autoThemeInput.addEventListener("click", function () { |
| 55 | + // Check if the auto theme is selected |
| 56 | + if (autoThemeInput.checked) { |
| 57 | + // Re-run the function after a short delay (to ensure that the palette has been updated) |
| 58 | + setTimeout(applyAutoTheme); |
| 59 | + } |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +// Add iframe navigation |
| 64 | +window.onhashchange = function() { |
| 65 | + window.parent.postMessage({ |
| 66 | + type: 'navigation', |
| 67 | + hash: window.location.pathname + window.location.search + window.location.hash |
| 68 | + }, '*'); |
| 69 | +}; |
| 70 | + |
| 71 | +// Add Inkeep button |
| 72 | +document.addEventListener("DOMContentLoaded", () => { |
| 73 | + const inkeepScript = document.createElement("script"); |
| 74 | + inkeepScript.src = "https://unpkg.com/@inkeep/[email protected]/dist/embed.js"; |
| 75 | + inkeepScript.type = "module"; |
| 76 | + inkeepScript.defer = true; |
| 77 | + document.head.appendChild(inkeepScript); |
| 78 | + |
| 79 | + // Configure and initialize the widget |
| 80 | + const addInkeepWidget = () => { |
| 81 | + const inkeepWidget = Inkeep().embed({ |
| 82 | + componentType: "ChatButton", |
| 83 | + colorModeSync: { |
| 84 | + observedElement: document.documentElement, |
| 85 | + isDarkModeCallback: (el) => { |
| 86 | + const currentTheme = el.getAttribute("data-color-mode"); |
| 87 | + return currentTheme === "dark"; |
| 88 | + }, |
| 89 | + colorModeAttribute: "data-color-mode", |
| 90 | + }, |
| 91 | + properties: { |
| 92 | + chatButtonType: "PILL", |
| 93 | + fixedPositionXOffset: "1rem", |
| 94 | + fixedPositionYOffset: "3rem", |
| 95 | + chatButtonBgColor: "#E1FF25", |
| 96 | + baseSettings: { |
| 97 | + apiKey: "13dfec2e75982bc9bae3199a08e13b86b5fbacd64e9b2f89", |
| 98 | + integrationId: "cm1shscmm00y26sj83lgxzvkw", |
| 99 | + organizationId: "org_e3869az6hQZ0mXdF", |
| 100 | + primaryBrandColor: "#E1FF25", |
| 101 | + organizationDisplayName: "Ultralytics", |
| 102 | + theme: { |
| 103 | + stylesheetUrls: ["/stylesheets/style.css"], |
| 104 | + }, |
| 105 | + // ...optional settings |
| 106 | + }, |
| 107 | + modalSettings: { |
| 108 | + // optional settings |
| 109 | + }, |
| 110 | + searchSettings: { |
| 111 | + // optional settings |
| 112 | + }, |
| 113 | + aiChatSettings: { |
| 114 | + chatSubjectName: "Ultralytics", |
| 115 | + botAvatarSrcUrl: "https://storage.googleapis.com/organization-image-assets/ultralytics-botAvatarSrcUrl-1727908259285.png", |
| 116 | + botAvatarDarkSrcUrl: "https://storage.googleapis.com/organization-image-assets/ultralytics-botAvatarDarkSrcUrl-1727908258478.png", |
| 117 | + quickQuestions: [ |
| 118 | + "What's new in Ultralytics YOLO11?", |
| 119 | + "How can I get started with Ultralytics HUB?", |
| 120 | + "How does Ultralytics Enterprise Licensing work?" |
| 121 | + ], |
| 122 | + getHelpCallToActions: [ |
| 123 | + { |
| 124 | + name: "Ask on Ultralytics GitHub", |
| 125 | + url: "https://github.com/ultralytics/ultralytics", |
| 126 | + icon: { |
| 127 | + builtIn: "FaGithub" |
| 128 | + } |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "Ask on Ultralytics Discourse", |
| 132 | + url: "https://community.ultralytics.com/", |
| 133 | + icon: { |
| 134 | + builtIn: "FaDiscourse" |
| 135 | + } |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "Ask on Ultralytics Discord", |
| 139 | + url: "https://discord.com/invite/ultralytics", |
| 140 | + icon: { |
| 141 | + builtIn: "FaDiscord" |
| 142 | + } |
| 143 | + } |
| 144 | + ], |
| 145 | + }, |
| 146 | + }, |
| 147 | + }); |
| 148 | + }; |
| 149 | + inkeepScript.addEventListener("load", () => { |
| 150 | + addInkeepWidget(); // initialize the widget |
| 151 | + }); |
| 152 | +}); |
0 commit comments