File size: 3,844 Bytes
046723b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
$(document).ready(function () {
    var a = document.getElementById("a");
    var b = document.getElementById("b");
    var result = document.getElementById("result");
    var inputs;

    $('#jump-next-diff').click(function () {

        var element = inputs[inputs.current];
        var headerOffset = 80;
        var elementPosition = element.getBoundingClientRect().top;
        var offsetPosition = elementPosition - headerOffset + window.scrollY;

        window.scrollTo({
            top: offsetPosition,
            behavior: "smooth",
        });

        inputs.current++;
        if (inputs.current >= inputs.length) {
            inputs.current = 0;
        }
    });

    function changed() {
        // https://github.com/kpdecker/jsdiff/issues/389
        // I would love to use `{ignoreWhitespace: true}` here but it breaks the formatting
        options = {
            ignoreWhitespace: document.getElementById("ignoreWhitespace").checked,
        };

        var diff = Diff[window.diffType](a.textContent, b.textContent, options);
        var fragment = document.createDocumentFragment();
        for (var i = 0; i < diff.length; i++) {
            if (diff[i].added && diff[i + 1] && diff[i + 1].removed) {
                var swap = diff[i];
                diff[i] = diff[i + 1];
                diff[i + 1] = swap;
            }

            var node;
            if (diff[i].removed) {
                node = document.createElement("del");
                node.classList.add("change");
                const wrapper = node.appendChild(document.createElement("span"));
                wrapper.appendChild(document.createTextNode(diff[i].value));
            } else if (diff[i].added) {
                node = document.createElement("ins");
                node.classList.add("change");
                const wrapper = node.appendChild(document.createElement("span"));
                wrapper.appendChild(document.createTextNode(diff[i].value));
            } else {
                node = document.createTextNode(diff[i].value);
            }
            fragment.appendChild(node);
        }

        result.textContent = "";
        result.appendChild(fragment);

        // For nice mouse-over hover/title information
        const removed_current_option = $('#diff-version option:selected')
        if (removed_current_option) {
            $('del').each(function () {
                $(this).prop('title', 'Removed '+removed_current_option[0].label);
            });
        }
        const inserted_current_option = $('#current-version option:selected')
        if (removed_current_option) {
            $('ins').each(function () {
                $(this).prop('title', 'Inserted '+inserted_current_option[0].label);
            });
        }
        // Set the list of possible differences to jump to
        inputs = document.querySelectorAll('#diff-ui .change')
        // Set the "current" diff pointer
        inputs.current = 0;
        // Goto diff
        $('#jump-next-diff').click();
    }


    onDiffTypeChange(
        document.querySelector('#settings [name="diff_type"]:checked'),
    );
    changed();

    a.onpaste = a.onchange = b.onpaste = b.onchange = changed;

    if ("oninput" in a) {
        a.oninput = b.oninput = changed;
    } else {
        a.onkeyup = b.onkeyup = changed;
    }

    function onDiffTypeChange(radio) {
        window.diffType = radio.value;
        // Not necessary
        //	document.title = "Diff " + radio.value.slice(4);
    }

    var radio = document.getElementsByName("diff_type");
    for (var i = 0; i < radio.length; i++) {
        radio[i].onchange = function (e) {
            onDiffTypeChange(e.target);
            changed();
        };
    }

    document.getElementById("ignoreWhitespace").onchange = function (e) {
        changed();
    };

});