Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | [# TODO(jrobbins): move this into compiled javascript. ] |
| 2 | <script type="text/javascript" nonce="[nonce]"> |
| 3 | runOnLoad(function() { |
| 4 | var numsGenState = {table_base_id: 'nums_table_'}; |
| 5 | var srcGenState = {table_base_id: 'src_table_'}; |
| 6 | var alignerRunning = false; |
| 7 | var startOver = false; |
| 8 | |
| 9 | function setLineNumberHeights() { |
| 10 | if (alignerRunning) { |
| 11 | startOver = true; |
| 12 | return; |
| 13 | } |
| 14 | numsGenState.chunk_id = 0; |
| 15 | numsGenState.table = document.getElementById('nums_table_0'); |
| 16 | numsGenState.row_num = 0; |
| 17 | |
| 18 | if (!numsGenState.table) { |
| 19 | return; // Silently exit if no file is present. |
| 20 | } |
| 21 | |
| 22 | srcGenState.chunk_id = 0; |
| 23 | srcGenState.table = document.getElementById('src_table_0'); |
| 24 | srcGenState.row_num = 0; |
| 25 | |
| 26 | alignerRunning = true; |
| 27 | continueToSetLineNumberHeights(); |
| 28 | } |
| 29 | |
| 30 | function rowGenerator(genState) { |
| 31 | if (genState.row_num < genState.table.rows.length) { |
| 32 | var currentRow = genState.table.rows[[]genState.row_num]; |
| 33 | genState.row_num++; |
| 34 | return currentRow; |
| 35 | } |
| 36 | |
| 37 | var newTable = document.getElementById( |
| 38 | genState.table_base_id + (genState.chunk_id + 1)); |
| 39 | if (newTable) { |
| 40 | genState.chunk_id++; |
| 41 | genState.row_num = 0; |
| 42 | genState.table = newTable; |
| 43 | return genState.table.rows[[]0]; |
| 44 | } |
| 45 | |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | var MAX_ROWS_PER_PASS = 1000; |
| 50 | |
| 51 | function continueToSetLineNumberHeights() { |
| 52 | var rowsInThisPass = 0; |
| 53 | var numRow = 1; |
| 54 | var srcRow = 1; |
| 55 | |
| 56 | while (numRow && srcRow && rowsInThisPass < MAX_ROWS_PER_PASS) { |
| 57 | numRow = rowGenerator(numsGenState); |
| 58 | srcRow = rowGenerator(srcGenState); |
| 59 | rowsInThisPass++; |
| 60 | |
| 61 | if (numRow && srcRow) { |
| 62 | if (numRow.offsetHeight != srcRow.offsetHeight) { |
| 63 | numRow.firstChild.style.height = srcRow.offsetHeight + 'px'; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (rowsInThisPass >= MAX_ROWS_PER_PASS) { |
| 69 | setTimeout(continueToSetLineNumberHeights, 10); |
| 70 | } else { |
| 71 | alignerRunning = false; |
| 72 | if (startOver) { |
| 73 | startOver = false; |
| 74 | setTimeout(setLineNumberHeights, 500); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | function initLineNumberHeights() { |
| 81 | // Do 2 complete passes, because there can be races |
| 82 | // between this code and prettify. |
| 83 | startOver = true; |
| 84 | setTimeout(setLineNumberHeights, 250); |
| 85 | window.addEventListener('resize', setLineNumberHeights); |
| 86 | } |
| 87 | initLineNumberHeights(); |
| 88 | }); |
| 89 | </script> |