Fossil SCM

fossil-scm / src / diff.js
Source Blame History 73 lines
9e33074… drh 1 /* Refinements to the display of unified and side-by-side diffs.
9e33074… drh 2 **
9e33074… drh 3 ** In all cases, the table columns tagged with "difftxt" are expanded,
9e33074… drh 4 ** where possible, to fill the width of the screen.
9e33074… drh 5 **
9e33074… drh 6 ** For a side-by-side diff, if either column is two wide to fit on the
9e33074… drh 7 ** display, scrollbars are added. The scrollbars are linked, so that
9e33074… drh 8 ** both sides scroll together. Left and right arrows also scroll.
9e33074… drh 9 */
51c1efd… stephan 10 window.addEventListener('load',function(){
9e33074… drh 11 var SCROLL_LEN = 25;
9e33074… drh 12 function initDiff(diff){
9e33074… drh 13 var txtCols = diff.querySelectorAll('td.difftxt');
9e33074… drh 14 var txtPres = diff.querySelectorAll('td.difftxt pre');
9e33074… drh 15 var width = 0;
51c1efd… stephan 16 if(txtPres.length>=2){
51c1efd… stephan 17 width = Math.max(txtPres[0].scrollWidth, txtPres[1].scrollWidth);
51c1efd… stephan 18 }
9e33074… drh 19 var i;
9e33074… drh 20 for(i=0; i<txtCols.length; i++){
9e33074… drh 21 txtCols[i].style.width = width + 'px';
9e33074… drh 22 txtPres[i].style.maxWidth = width + 'px';
9e33074… drh 23 txtPres[i].style.width = width + 'px';
9e33074… drh 24 txtPres[i].onscroll = function(e){
9e33074… drh 25 for(var j=0; j<txtPres.length; j++) txtPres[j].scrollLeft = this.scrollLeft;
9e33074… drh 26 };
9e33074… drh 27 }
9e33074… drh 28 diff.tabIndex = 0;
9e33074… drh 29 diff.onkeydown = function(e){
9e33074… drh 30 e = e || event;
9e33074… drh 31 var len = {37: -SCROLL_LEN, 39: SCROLL_LEN}[e.keyCode];
9e33074… drh 32 if( !len ) return;
c624ed8… stephan 33 txtPres[0].scrollLeft += len;
9e33074… drh 34 return false;
9e33074… drh 35 };
9e33074… drh 36 }
9e33074… drh 37 var i, diffs = document.querySelectorAll('table.splitdiff')
9e33074… drh 38 for(i=0; i<diffs.length; i++){
9e33074… drh 39 initDiff(diffs[i]);
9e33074… drh 40 }
51c1efd… stephan 41 const checkWidth = function f(){
51c1efd… stephan 42 if(undefined === f.lastWidth){
51c1efd… stephan 43 f.lastWidth = 0;
51c1efd… stephan 44 }
51c1efd… stephan 45 if( document.body.clientWidth===f.lastWidth ) return;
51c1efd… stephan 46 f.lastWidth = document.body.clientWidth;
51c1efd… stephan 47 var w = f.lastWidth*0.5 - 100;
51c1efd… stephan 48 if(!f.colsL){
51c1efd… stephan 49 f.colsL = document.querySelectorAll('td.difftxtl pre');
51c1efd… stephan 50 }
51c1efd… stephan 51 for(let i=0; i<f.colsL.length; i++){
51c1efd… stephan 52 f.colsL[i].style.width = w + "px";
51c1efd… stephan 53 f.colsL[i].style.maxWidth = w + "px";
51c1efd… stephan 54 }
51c1efd… stephan 55 if(!f.colsR){
51c1efd… stephan 56 f.colsR = document.querySelectorAll('td.difftxtr pre');
51c1efd… stephan 57 }
51c1efd… stephan 58 for(let i=0; i<f.colsR.length; i++){
51c1efd… stephan 59 f.colsR[i].style.width = w + "px";
51c1efd… stephan 60 f.colsR[i].style.maxWidth = w + "px";
51c1efd… stephan 61 }
51c1efd… stephan 62 if(!f.allDiffs){
51c1efd… stephan 63 f.allDiffs = document.querySelectorAll('table.diff');
51c1efd… stephan 64 }
51c1efd… stephan 65 w = f.lastWidth;
51c1efd… stephan 66 for(let i=0; i<f.allDiffs.length; i++){
51c1efd… stephan 67 f.allDiffs[i].style.width = '100%'; // setting to w causes unsightly horiz. scrollbar
51c1efd… stephan 68 f.allDiffs[i].style.maxWidth = w + "px";
51c1efd… stephan 69 }
51c1efd… stephan 70 };
c5f8e79… stephan 71 checkWidth();
c5f8e79… stephan 72 window.addEventListener('resize', checkWidth);
51c1efd… stephan 73 }, false);

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button