|
| 1 | +/** |
| 2 | + * Automatically enable word wrap in code blocks on mobile devices |
| 3 | + * This script clicks the word wrap button when the page loads on mobile screens |
| 4 | + */ |
| 5 | + |
| 6 | +function enableWordWrapOnMobile() { |
| 7 | + // Check if we're on a mobile device (screen width <= 996px) |
| 8 | + if (window.innerWidth <= 996) { |
| 9 | + // Wait for the DOM to be fully loaded and code blocks to render |
| 10 | + setTimeout(() => { |
| 11 | + // Find all word wrap buttons |
| 12 | + // The button has class 'clean-btn' and when enabled also has 'wordWrapButtonEnabled_*' |
| 13 | + const wordWrapButtons = document.querySelectorAll('.clean-btn[aria-label*="word wrap"], .clean-btn[aria-label*="Word wrap"], .clean-btn[title*="word wrap"], .clean-btn[title*="Word wrap"]'); |
| 14 | + |
| 15 | + wordWrapButtons.forEach(button => { |
| 16 | + // Check if the button is NOT already enabled |
| 17 | + // When enabled, it has a class containing 'wordWrapButtonEnabled' |
| 18 | + const isEnabled = Array.from(button.classList).some(className => |
| 19 | + className.includes('wordWrapButtonEnabled') |
| 20 | + ); |
| 21 | + |
| 22 | + if (!isEnabled) { |
| 23 | + // Click the button to enable word wrap |
| 24 | + button.click(); |
| 25 | + } |
| 26 | + }); |
| 27 | + }, 500); // Wait 500ms for code blocks to render |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Run on initial page load |
| 32 | +if (typeof window !== 'undefined') { |
| 33 | + // Execute when DOM is ready |
| 34 | + if (document.readyState === 'loading') { |
| 35 | + document.addEventListener('DOMContentLoaded', enableWordWrapOnMobile); |
| 36 | + } else { |
| 37 | + enableWordWrapOnMobile(); |
| 38 | + } |
| 39 | + |
| 40 | + // Also run when navigating between pages (Docusaurus uses client-side routing) |
| 41 | + window.addEventListener('popstate', enableWordWrapOnMobile); |
| 42 | + |
| 43 | + // Listen for route changes in Docusaurus |
| 44 | + if (window.docusaurus) { |
| 45 | + window.docusaurus.prefetch = function() { |
| 46 | + // Hook into Docusaurus navigation |
| 47 | + enableWordWrapOnMobile(); |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + // Use MutationObserver to detect when new code blocks are added to the page |
| 52 | + const observer = new MutationObserver((mutations) => { |
| 53 | + let shouldCheck = false; |
| 54 | + |
| 55 | + mutations.forEach((mutation) => { |
| 56 | + if (mutation.addedNodes.length > 0) { |
| 57 | + mutation.addedNodes.forEach((node) => { |
| 58 | + // Check if a code block or button was added |
| 59 | + if (node.nodeType === 1) { // Element node |
| 60 | + if (node.matches && ( |
| 61 | + node.matches('.theme-code-block') || |
| 62 | + node.matches('.clean-btn') || |
| 63 | + node.querySelector('.theme-code-block') || |
| 64 | + node.querySelector('.clean-btn') |
| 65 | + )) { |
| 66 | + shouldCheck = true; |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + if (shouldCheck) { |
| 74 | + enableWordWrapOnMobile(); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // Start observing the document body for changes |
| 79 | + observer.observe(document.body, { |
| 80 | + childList: true, |
| 81 | + subtree: true |
| 82 | + }); |
| 83 | +} |
0 commit comments