diff --git a/assets/js/app.js b/assets/js/app.js index 901c14b..240807b 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -60,30 +60,42 @@ window.Hooks.InfiniteScroll = { } } -let liveSocket = new LiveSocket("/live", Socket, { hooks: window.Hooks }) -liveSocket.connect() - -/* -Make it possible to click line numbers to update the address bar to a -link directly to that line. -*/ -if (location.hash) { - document.getElementById(location.hash.replace('#', '')).classList.add('selected') -} - -const lines = document.querySelectorAll('.ghd-line-number') -lines.forEach(line => { - line.addEventListener('click', e => { - const parent = line.parentNode +window.Hooks.DiffList = { + mounted() { + this.el.addEventListener('click', e => { + const lineNumber = e.target.closest('.ghd-line-number') + if (!lineNumber) return + + const parent = lineNumber.parentNode + if (parent && parent.id) { + this.el.querySelectorAll('.ghd-line.selected').forEach(el => { + el.classList.remove('selected') + }) + parent.classList.add('selected') + history.replaceState(null, null, '#' + parent.id) + } + }) - if (parent && parent.id) { - document.querySelectorAll('.ghd-line.selected').forEach(line => { - line.classList.remove('selected') - }) + this.selectHash() + }, - parent.classList.add('selected') + updated() { + this.selectHash() + }, - history.replaceState(null, null, '#' + parent.id) + selectHash() { + if (location.hash) { + const el = document.getElementById(location.hash.replace('#', '')) + if (el) { + this.el.querySelectorAll('.ghd-line.selected').forEach(el => { + el.classList.remove('selected') + }) + el.classList.add('selected') + el.scrollIntoView({ block: 'center' }) + } } - }) -}) + } +} + +let liveSocket = new LiveSocket("/live", Socket, { hooks: window.Hooks }) +liveSocket.connect() diff --git a/lib/diff_web/templates/live/diff.html.leex b/lib/diff_web/templates/live/diff.html.leex index d738b32..6dfd12c 100644 --- a/lib/diff_web/templates/live/diff.html.leex +++ b/lib/diff_web/templates/live/diff.html.leex @@ -28,7 +28,7 @@ <% end %> -