|
| 1 | +let selections = {}; |
| 2 | + |
| 3 | +function parseLines() { |
| 4 | + let params = new URLSearchParams(document.location.search); |
| 5 | + let param = params.get("lines"); |
| 6 | + |
| 7 | + if (!param) { |
| 8 | + return |
| 9 | + } |
| 10 | + |
| 11 | + const regex = /F(\d+)-L(\d+)(?:-L(\d+))?/g; |
| 12 | + let match; |
| 13 | + while ((match = regex.exec(param)) !== null) { |
| 14 | + let file = parseInt(match[1]); |
| 15 | + let start = parseInt(match[2]); |
| 16 | + let end = match[3] ? parseInt(match[3]) : start; |
| 17 | + |
| 18 | + if (isNaN(file) || isNaN(start) || isNaN(end)) { |
| 19 | + continue; |
| 20 | + } |
| 21 | + |
| 22 | + highlightLine(null, file - 1, start); |
| 23 | + if (start !== end) { |
| 24 | + highlightLine(null, file - 1, end); |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +parseLines(); |
| 30 | + |
| 31 | +function removeSelected(lines) { |
| 32 | + lines.forEach(line => { |
| 33 | + let child = line.querySelector("td.lineSelected"); |
| 34 | + if (child) { |
| 35 | + line.removeChild(child); |
| 36 | + line.classList.remove("lineNumRowSelected"); |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function updateParams() { |
| 42 | + const url = new URL(window.location); |
| 43 | + let param = Object.entries(selections).map(([key, value]) => { |
| 44 | + let end = value.end !== value.start ? `-L${value.end}_` : ''; |
| 45 | + return `F${parseInt(key) + 1}-L${value.start}${end}`; |
| 46 | + }).join(''); |
| 47 | + |
| 48 | + url.searchParams.set("lines", param); |
| 49 | + url.searchParams.delete("pastePassword"); |
| 50 | + |
| 51 | + history.pushState(null, '', url); |
| 52 | +} |
| 53 | + |
| 54 | +function replaceSelected(lines, idIndex, index, start, end) { |
| 55 | + let newLines = lines.slice(start, end); |
| 56 | + removeSelected(newLines); |
| 57 | + |
| 58 | + let line = lines[index - 1]; |
| 59 | + line.insertAdjacentHTML("beforeend", `<td class="lineSelected"></td>`); |
| 60 | + line.classList.add("lineNumRowSelected"); |
| 61 | + |
| 62 | + selections[idIndex] = { "start": index, "end": index }; |
| 63 | + updateParams(); |
| 64 | +} |
| 65 | + |
| 66 | +function addLines(lines, idIndex, start, end) { |
| 67 | + let newLines = lines.slice(start - 1, end); |
| 68 | + newLines.forEach(line => { |
| 69 | + if (!line.querySelector("td.lineSelected")) { |
| 70 | + line.insertAdjacentHTML("beforeend", `<td class="lineSelected"></td>`); |
| 71 | + line.classList.add("lineNumRowSelected"); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + selections[idIndex] = { "start": start, "end": end }; |
| 76 | + updateParams(); |
| 77 | +} |
| 78 | + |
| 79 | +function highlightLine(event, idI, selected) { |
| 80 | + let idIndex = parseInt(idI); |
| 81 | + let id = `__paste_c_${idIndex}`; |
| 82 | + let block = document.getElementById(id); |
| 83 | + |
| 84 | + if (!block) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + let lines = Array.from(block.querySelectorAll("tbody>tr")); |
| 89 | + let line = Math.min(parseInt(selected), lines.length); |
| 90 | + |
| 91 | + let current = selections[idIndex]; |
| 92 | + if (!current) { |
| 93 | + let selectedLine = lines[line - 1]; |
| 94 | + selectedLine.insertAdjacentHTML("beforeend", `<td class="lineSelected"></td>`); |
| 95 | + selectedLine.classList.add("lineNumRowSelected"); |
| 96 | + |
| 97 | + selections[idIndex] = { "start": line, "end": line }; |
| 98 | + updateParams(); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + let { start, end } = current; |
| 103 | + |
| 104 | + if (event && !event.shiftKey) { |
| 105 | + replaceSelected(lines, idIndex, line, start - 1, end); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (!event || event.shiftKey) { |
| 110 | + if (line < start) { |
| 111 | + removeSelected(lines.slice(start, end)); |
| 112 | + addLines(lines, idIndex, line, start); |
| 113 | + } else if (line <= end) { |
| 114 | + replaceSelected(lines, idIndex, line, start - 1, end); |
| 115 | + } else { |
| 116 | + addLines(lines, idIndex, start, line); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments