+
@@ -184,11 +185,31 @@ document.addEventListener('DOMContentLoaded', function() {
const btnRefine = document.getElementById('btnRefine');
const btnSelectAll = document.getElementById('btnSelectAll');
+ function findCounterpart(el, contextRoot) {
+ const key = el.getAttribute('data-diff-key');
+ const stableParent = el.closest('[data-stable-id]');
+ if (stableParent) {
+ const stableId = stableParent.getAttribute('data-stable-id');
+ const otherParent = contextRoot.querySelector(`[data-stable-id="${stableId.replace(/"/g, '\\"')}"]`);
+ if (otherParent) {
+ const parentKey = stableParent.getAttribute('data-diff-key');
+ if (key.startsWith(parentKey)) {
+ const suffix = key.substring(parentKey.length);
+ const otherParentKey = otherParent.getAttribute('data-diff-key');
+ return contextRoot.querySelector(`[data-diff-key="${otherParentKey + suffix}"]`);
+ }
+ return otherParent;
+ }
+ return null;
+ }
+ return contextRoot.querySelector(`[data-diff-key="${key}"]`);
+ }
+
// 1. Highlight Differences
const newElements = newDraft.querySelectorAll('[data-diff-key]');
newElements.forEach(el => {
const key = el.getAttribute('data-diff-key');
- const origEl = original.querySelector(`[data-diff-key="${key}"]`);
+ const origEl = findCounterpart(el, original);
// Find associated checkbox (it might be a sibling or parent wrapper)
// In our macro, checkbox is usually a sibling of the span with data-diff-key
@@ -215,6 +236,14 @@ document.addEventListener('DOMContentLoaded', function() {
checkbox.classList.remove('d-none');
checkbox.checked = true;
}
+ } else if (el.getAttribute('data-diff-key') !== origEl.getAttribute('data-diff-key')) {
+ // Moved (Index changed but content matched by ID)
+ el.classList.add('diff-moved');
+ el.title = "Moved from original position";
+ if (checkbox) {
+ checkbox.classList.remove('d-none');
+ checkbox.checked = true;
+ }
} else if (el.innerText.trim() !== origEl.innerText.trim()) {
el.classList.add('diff-changed');
origEl.classList.add('diff-changed');
@@ -229,8 +258,7 @@ document.addEventListener('DOMContentLoaded', function() {
// Check for removed items
const origElements = original.querySelectorAll('[data-diff-key]');
origElements.forEach(el => {
- const key = el.getAttribute('data-diff-key');
- const newEl = newDraft.querySelector(`[data-diff-key="${key}"]`);
+ const newEl = findCounterpart(el, newDraft);
if (!newEl) {
el.classList.add('diff-removed');
el.title = "Removed in new draft";