#550 Highlight specific lines
Merged 9 years ago by pingou. Opened 9 years ago by lsedlar.
lsedlar/pagure linking-lines  into  master

file modified
+4
@@ -455,3 +455,7 @@ 

  header:hover > a.headerlink {

      visibility: visible;

  }

+ 

+ .highlighted-line {

+     background-color: #FFFABA;

+ }

@@ -83,3 +83,37 @@ 

  {% endif %}

  

  {% endblock %}

+ 

+ {% block jscripts %}

+ {{ super() }}

+ <script type="text/javascript">

+ function updateHighlight() {

+   var cls = "highlighted-line";

+   $('.' + cls).removeClass(cls)

+   if (location.hash !== '') {

+     var lines = location.hash.substr(2).split('-').map(function (x) { return parseInt(x, 10) });

+     for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {

+       $('[data-line-number=' + i + ']').closest('tr').addClass(cls);

+     }

+   }

+ }

+ $(document).ready(updateHighlight);

+ $(window).on('hashchange', updateHighlight);

+ var selected = [];

+ $("[data-line-number]").click(function (ev) {

+   var line = $(this).attr('data-line-number');

+   if (ev.shiftKey) {

+     selected = selected.slice(-1).concat(line);

+   } else {

+     selected = [line];

+   }

+ 

+   var hash = '_' + selected[0];

+   if (selected.length === 2) {

+     hash = '_' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);

+   }

+   window.location.hash = hash;

+   return false;

+ });

+ </script>

+ {% endblock %}

no initial comment

The logic for selecting line range is different to what GitHub does, but this seems reasonable to me as well.

Ok it works fine but fails if you select, for example, line 8 and line 10.

I could fix it with the following change:

-    for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
+    for (var i = parseInt(lines[lines.length - 1]); i >= parseInt(lines[0]); i--) {

If you would like to integrate it and rebase your branch, I'll merge the PR :)

You're right, I only tested on a file with 8 lines total. Updated and rebased on master.

Took me a while to find the parseInt but it's there.

Looks good to me, thanks!