Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve diff navigation #2358

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion indigo/analysis/differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def repl(match):
return '\xA0' * (b - a)
del_.text = ws_re.sub(repl, del_.text)

if ins is None or ins.tag != 'ins' or del_.tail:
if del_.tail or ins is None or (ins.tag != 'ins' and not (ins.get('class') or '').startswith('ins ')):
ins = del_.makeelement('ins')
# non-breaking space
ins.text = '\xA0'
Expand Down
2 changes: 1 addition & 1 deletion indigo/tests/test_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_inline_tag_added(self):
)

self.assertEqual(
'<p>Some text <span class="diff-pair"><del>bold text and a tail.</del><ins>&#xA0;</ins></span><b class="ins ">bold text</b><ins> and a tail.</ins></p>',
'<p>Some text <span class="diff-pair"><del>bold text and a tail.</del><b class="ins ">bold text</b></span><ins> and a tail.</ins></p>',
diff,
)

Expand Down
31 changes: 29 additions & 2 deletions indigo_app/static/javascript/indigo/views/diff_navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,27 @@
},

initialize: function (options) {
this.counterEl = this.el.querySelector('.diff-counter');
this.refresh();
},

refresh: function() {
this.changedElements = $(this.el.getAttribute('data-bs-target')).find('ins, del, .ins, .del');
const target = document.querySelector(this.el.getAttribute('data-bs-target'));
const changedElements = target.querySelectorAll('.diff-pair, ins, del, .ins, .del');

this.changedElements = [];

for (let i = 0; i < changedElements.length; i++) {
const el = changedElements[i];
// don't go inside diff-pairs, and ignore adjacent diffs
if (!el.parentElement.classList.contains('diff-pair') && (
this.changedElements.length === 0 || this.changedElements[this.changedElements.length - 1] !== el.previousElementSibling)) {
this.changedElements.push(el);
}
}

this.currentElementIndex = -1;
this.updateCounter();
},

prevChange: function (e) {
Expand All @@ -30,11 +45,23 @@
this.currentElementIndex--;
}
if (this.currentElementIndex > -1) this.changedElements[this.currentElementIndex].scrollIntoView();
this.updateCounter();
},

nextChange: function (e) {
this.currentElementIndex = (this.currentElementIndex + 1) % this.changedElements.length;
if (this.currentElementIndex > -1) this.changedElements[this.currentElementIndex].scrollIntoView();
}
this.updateCounter();
},

updateCounter: function () {
if (this.counterEl) {
if (this.changedElements.length === 0) {
this.counterEl.textContent = '0';
} else {
this.counterEl.textContent = (this.currentElementIndex + 1) + ' / ' + this.changedElements.length;
}
}
},
});
})(window);
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@
$.get(revision.url() + '/diff')
.then(function(response) {
var $akn = self.$el.find('.revision-preview la-akoma-ntoso').html(response.content);
self.$el.find('.change-count').text(
response.n_changes + ' change' + (response.n_changes != 1 ? 's' : ''));
self.diffNavigator.refresh();
});
},
Expand Down
8 changes: 8 additions & 0 deletions indigo_app/static/stylesheets/_diffs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
}

.diffset {
.diff-pair,
.ins,
ins,
.del,
del {
scroll-margin-top: 3em;
}

ins,
.ins,
.ins > .akn-num {
Expand Down
2 changes: 1 addition & 1 deletion indigo_app/templates/indigo_api/document/_revisions.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<header class="revisions-header">
<div class="title">
<div class="diff-navigator float-end" data-bs-target="#revision-akn">
<span class="change-count me-3"></span>
<span class="diff-counter me-3"></span>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary prev-change-btn">{% trans 'Previous' %} <i class="fas fa-chevron-up"></i></button>
<button type="button" class="btn btn-outline-secondary next-change-btn">{% trans 'Next' %} <i class="fas fa-chevron-down"></i></button>
Expand Down