|
| 1 | +// This script is for critical JS that needs to run as soon as possible. |
| 2 | +// Do not import any dependencies and keep to vanilla JS only. |
| 3 | + |
| 4 | +// This function runs before DOMContentLoaded and checks if most of the page |
| 5 | +// has loaded so we can do DOM mutations before anything is painted on the screen. |
| 6 | +requestAnimationFrame(function init(elapsed) { |
| 7 | + if (elapsed > 10000) return; |
| 8 | + if (!document.querySelector('script[src*="index.js"]')) return requestAnimationFrame(init); |
| 9 | + |
| 10 | + // Synchronously set clone button states and urls here to avoid flickering |
| 11 | + // on page load. initRepoCloneLink calls this when proto changes. |
| 12 | + // this applies the protocol-dependant clone url to all elements with the |
| 13 | + // `js-clone-url` and `js-clone-url-vsc` classes. |
| 14 | + // TODO: This localStorage setting should be moved to backend user config. |
| 15 | + (window.updateCloneStates = function() { |
| 16 | + const httpsBtn = document.getElementById('repo-clone-https'); |
| 17 | + const sshBtn = document.getElementById('repo-clone-ssh'); |
| 18 | + const value = localStorage.getItem('repo-clone-protocol') || 'https'; |
| 19 | + const isSSH = value === 'ssh' && sshBtn || value !== 'ssh' && !httpsBtn; |
| 20 | + |
| 21 | + if (httpsBtn) httpsBtn.classList[!isSSH ? 'add' : 'remove']('primary'); |
| 22 | + if (sshBtn) sshBtn.classList[isSSH ? 'add' : 'remove']('primary'); |
| 23 | + |
| 24 | + const btn = isSSH ? sshBtn : httpsBtn; |
| 25 | + if (!btn) return; |
| 26 | + |
| 27 | + const link = btn.getAttribute('data-link'); |
| 28 | + for (const el of document.getElementsByClassName('js-clone-url')) { |
| 29 | + el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link; |
| 30 | + } |
| 31 | + for (const el of document.getElementsByClassName('js-clone-url-vsc')) { |
| 32 | + el.href = `vscode://vscode.git/clone?url=${encodeURIComponent(link)}`; |
| 33 | + } |
| 34 | + })(); |
| 35 | +}); |
0 commit comments