| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 1 | import { css, html, LitElement, unsafeCSS } from "lit"; |
| 2 | import { customElement, property, state } from "lit/decorators.js"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 3 | import * as Diff2Html from "diff2html"; |
| 4 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 5 | @customElement("sketch-diff-view") |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 6 | export class SketchDiffView extends LitElement { |
| 7 | // Current commit hash being viewed |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 8 | @property({ type: String }) |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 9 | commitHash: string = ""; |
| 10 | |
| 11 | // Selected line in the diff for commenting |
| 12 | @state() |
| 13 | private selectedDiffLine: string | null = null; |
| 14 | |
| 15 | // View format (side-by-side or line-by-line) |
| 16 | @state() |
| 17 | private viewFormat: "side-by-side" | "line-by-line" = "side-by-side"; |
| 18 | |
| 19 | static styles = css` |
| 20 | .diff-view { |
| 21 | flex: 1; |
| 22 | display: flex; |
| 23 | flex-direction: column; |
| 24 | overflow: hidden; |
| 25 | height: 100%; |
| 26 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 27 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 28 | .diff-container { |
| 29 | height: 100%; |
| 30 | overflow: auto; |
| 31 | flex: 1; |
| 32 | padding: 0 1rem; |
| 33 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 34 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 35 | #diff-view-controls { |
| 36 | display: flex; |
| 37 | justify-content: flex-end; |
| 38 | padding: 10px; |
| 39 | background: #f8f8f8; |
| 40 | border-bottom: 1px solid #eee; |
| 41 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 42 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 43 | .diff-view-format { |
| 44 | display: flex; |
| 45 | gap: 10px; |
| 46 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 47 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 48 | .diff-view-format label { |
| 49 | cursor: pointer; |
| 50 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 51 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 52 | .diff2html-content { |
| 53 | font-family: var(--monospace-font); |
| 54 | position: relative; |
| 55 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 56 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 57 | /* Comment box styles */ |
| 58 | .diff-comment-box { |
| 59 | position: fixed; |
| 60 | bottom: 80px; |
| 61 | right: 20px; |
| 62 | width: 400px; |
| 63 | background-color: white; |
| 64 | border: 1px solid #ddd; |
| 65 | border-radius: 4px; |
| 66 | box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); |
| 67 | padding: 16px; |
| 68 | z-index: 1000; |
| 69 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 70 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 71 | .diff-comment-box h3 { |
| 72 | margin-top: 0; |
| 73 | margin-bottom: 10px; |
| 74 | font-size: 16px; |
| 75 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 76 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 77 | .selected-line { |
| 78 | margin-bottom: 10px; |
| 79 | font-size: 14px; |
| 80 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 81 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 82 | .selected-line pre { |
| 83 | padding: 6px; |
| 84 | background: #f5f5f5; |
| 85 | border: 1px solid #eee; |
| 86 | border-radius: 3px; |
| 87 | margin: 5px 0; |
| 88 | max-height: 100px; |
| 89 | overflow: auto; |
| 90 | font-family: var(--monospace-font); |
| 91 | font-size: 13px; |
| 92 | white-space: pre-wrap; |
| 93 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 94 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 95 | #diffCommentInput { |
| 96 | width: 100%; |
| 97 | height: 100px; |
| 98 | padding: 8px; |
| 99 | border: 1px solid #ddd; |
| 100 | border-radius: 4px; |
| 101 | resize: vertical; |
| 102 | font-family: inherit; |
| 103 | margin-bottom: 10px; |
| 104 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 105 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 106 | .diff-comment-buttons { |
| 107 | display: flex; |
| 108 | justify-content: flex-end; |
| 109 | gap: 8px; |
| 110 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 111 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 112 | .diff-comment-buttons button { |
| 113 | padding: 6px 12px; |
| 114 | border-radius: 4px; |
| 115 | border: 1px solid #ddd; |
| 116 | background: white; |
| 117 | cursor: pointer; |
| 118 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 119 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 120 | .diff-comment-buttons button:hover { |
| 121 | background: #f5f5f5; |
| 122 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 123 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 124 | .diff-comment-buttons button#submitDiffComment { |
| 125 | background: #1a73e8; |
| 126 | color: white; |
| 127 | border-color: #1a73e8; |
| 128 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 129 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 130 | .diff-comment-buttons button#submitDiffComment:hover { |
| 131 | background: #1967d2; |
| 132 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 133 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 134 | /* Styles for the comment button on diff lines */ |
| 135 | .d2h-gutter-comment-button { |
| 136 | position: absolute; |
| 137 | right: 0; |
| 138 | top: 50%; |
| 139 | transform: translateY(-50%); |
| 140 | visibility: hidden; |
| 141 | background: rgba(255, 255, 255, 0.8); |
| 142 | border-radius: 50%; |
| 143 | width: 16px; |
| 144 | height: 16px; |
| 145 | line-height: 13px; |
| 146 | text-align: center; |
| 147 | font-size: 14px; |
| 148 | cursor: pointer; |
| 149 | color: #666; |
| 150 | box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); |
| 151 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 152 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 153 | tr:hover .d2h-gutter-comment-button { |
| 154 | visibility: visible; |
| 155 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 156 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 157 | .d2h-gutter-comment-button:hover { |
| 158 | background: white; |
| 159 | color: #333; |
| 160 | box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); |
| 161 | } |
| 162 | `; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 163 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 164 | constructor() { |
| 165 | super(); |
| 166 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 167 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 168 | // See https://lit.dev/docs/components/lifecycle/ |
| 169 | connectedCallback() { |
| 170 | super.connectedCallback(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 171 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 172 | // Load the diff2html CSS if needed |
| 173 | this.loadDiff2HtmlCSS(); |
| 174 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 175 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 176 | // Load diff2html CSS into the shadow DOM |
| 177 | private async loadDiff2HtmlCSS() { |
| 178 | try { |
| 179 | // Check if diff2html styles are already loaded |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 180 | const styleId = "diff2html-styles"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 181 | if (this.shadowRoot?.getElementById(styleId)) { |
| 182 | return; // Already loaded |
| 183 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 184 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 185 | // Fetch the diff2html CSS |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 186 | const response = await fetch("static/diff2html.min.css"); |
| 187 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 188 | if (!response.ok) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 189 | console.error( |
| 190 | `Failed to load diff2html CSS: ${response.status} ${response.statusText}`, |
| 191 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 192 | return; |
| 193 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 194 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 195 | const cssText = await response.text(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 196 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 197 | // Create a style element and append to shadow DOM |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 198 | const style = document.createElement("style"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 199 | style.id = styleId; |
| 200 | style.textContent = cssText; |
| 201 | this.shadowRoot?.appendChild(style); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 202 | |
| 203 | console.log("diff2html CSS loaded into shadow DOM"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 204 | } catch (error) { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 205 | console.error("Error loading diff2html CSS:", error); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 208 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 209 | // See https://lit.dev/docs/components/lifecycle/ |
| 210 | disconnectedCallback() { |
| 211 | super.disconnectedCallback(); |
| 212 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 213 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 214 | // Method called to load diff content |
| 215 | async loadDiffContent() { |
| 216 | // Wait for the component to be rendered |
| 217 | await this.updateComplete; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 218 | |
| 219 | const diff2htmlContent = |
| 220 | this.shadowRoot?.getElementById("diff2htmlContent"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 221 | if (!diff2htmlContent) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 222 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 223 | try { |
| 224 | // Show loading state |
| 225 | diff2htmlContent.innerHTML = "Loading enhanced diff..."; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 226 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 227 | // Build the diff URL - include commit hash if specified |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 228 | const diffUrl = this.commitHash |
| 229 | ? `diff?commit=${this.commitHash}` |
| 230 | : "diff"; |
| 231 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 232 | // Fetch the diff from the server |
| 233 | const response = await fetch(diffUrl); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 234 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 235 | if (!response.ok) { |
| 236 | throw new Error( |
| 237 | `Server returned ${response.status}: ${response.statusText}`, |
| 238 | ); |
| 239 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 240 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 241 | const diffText = await response.text(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 242 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 243 | if (!diffText || diffText.trim() === "") { |
| 244 | diff2htmlContent.innerHTML = |
| 245 | "<span style='color: #666; font-style: italic;'>No changes detected since conversation started.</span>"; |
| 246 | return; |
| 247 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 248 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 249 | // Render the diff using diff2html |
| 250 | const diffHtml = Diff2Html.html(diffText, { |
| 251 | outputFormat: this.viewFormat, |
| 252 | drawFileList: true, |
| 253 | matching: "lines", |
| 254 | renderNothingWhenEmpty: false, |
| 255 | colorScheme: "light" as any, // Force light mode to match the rest of the UI |
| 256 | }); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 257 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 258 | // Insert the generated HTML |
| 259 | diff2htmlContent.innerHTML = diffHtml; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 260 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 261 | // Add CSS styles to ensure we don't have double scrollbars |
| 262 | const d2hFiles = diff2htmlContent.querySelectorAll(".d2h-file-wrapper"); |
| 263 | d2hFiles.forEach((file) => { |
| 264 | const contentElem = file.querySelector(".d2h-files-diff"); |
| 265 | if (contentElem) { |
| 266 | // Remove internal scrollbar - the outer container will handle scrolling |
| 267 | (contentElem as HTMLElement).style.overflow = "visible"; |
| 268 | (contentElem as HTMLElement).style.maxHeight = "none"; |
| 269 | } |
| 270 | }); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 271 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 272 | // Add click event handlers to each code line for commenting |
| 273 | this.setupDiffLineComments(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 274 | } catch (error) { |
| 275 | console.error("Error loading diff2html content:", error); |
| 276 | const errorMessage = |
| 277 | error instanceof Error ? error.message : "Unknown error"; |
| 278 | diff2htmlContent.innerHTML = `<span style='color: #dc3545;'>Error loading enhanced diff: ${errorMessage}</span>`; |
| 279 | } |
| 280 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 281 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 282 | // Handle view format changes |
| 283 | private handleViewFormatChange(event: Event) { |
| 284 | const input = event.target as HTMLInputElement; |
| 285 | if (input.checked) { |
| 286 | this.viewFormat = input.value as "side-by-side" | "line-by-line"; |
| 287 | this.loadDiffContent(); |
| 288 | } |
| 289 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 290 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Setup handlers for diff code lines to enable commenting |
| 293 | */ |
| 294 | private setupDiffLineComments(): void { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 295 | const diff2htmlContent = |
| 296 | this.shadowRoot?.getElementById("diff2htmlContent"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 297 | if (!diff2htmlContent) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 298 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 299 | console.log("Setting up diff line comments"); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 300 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 301 | // Add plus buttons to each code line |
| 302 | this.addCommentButtonsToCodeLines(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 303 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 304 | // Use event delegation for handling clicks on plus buttons |
| 305 | diff2htmlContent.addEventListener("click", (event) => { |
| 306 | const target = event.target as HTMLElement; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 307 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 308 | // Only respond to clicks on the plus button |
| 309 | if (target.classList.contains("d2h-gutter-comment-button")) { |
| 310 | // Find the parent row first |
| 311 | const row = target.closest("tr"); |
| 312 | if (!row) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 313 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 314 | // Then find the code line in that row |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 315 | const codeLine = |
| 316 | row.querySelector(".d2h-code-side-line") || |
| 317 | row.querySelector(".d2h-code-line"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 318 | if (!codeLine) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 319 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 320 | // Get the line text content |
| 321 | const lineContent = codeLine.querySelector(".d2h-code-line-ctn"); |
| 322 | if (!lineContent) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 323 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 324 | const lineText = lineContent.textContent?.trim() || ""; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 325 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 326 | // Get file name to add context |
| 327 | const fileHeader = codeLine |
| 328 | .closest(".d2h-file-wrapper") |
| 329 | ?.querySelector(".d2h-file-name"); |
| 330 | const fileName = fileHeader |
| 331 | ? fileHeader.textContent?.trim() |
| 332 | : "Unknown file"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 333 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 334 | // Get line number if available |
| 335 | const lineNumElem = codeLine |
| 336 | .closest("tr") |
| 337 | ?.querySelector(".d2h-code-side-linenumber"); |
| 338 | const lineNum = lineNumElem ? lineNumElem.textContent?.trim() : ""; |
| 339 | const lineInfo = lineNum ? `Line ${lineNum}: ` : ""; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 340 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 341 | // Format the line for the comment box with file context and line number |
| 342 | const formattedLine = `${fileName} ${lineInfo}${lineText}`; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 343 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 344 | console.log("Comment button clicked for line: ", formattedLine); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 345 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 346 | // Open the comment box with this line |
| 347 | this.openDiffCommentBox(formattedLine); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 348 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 349 | // Prevent event from bubbling up |
| 350 | event.stopPropagation(); |
| 351 | } |
| 352 | }); |
| 353 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 354 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 355 | /** |
| 356 | * Add plus buttons to each table row in the diff for commenting |
| 357 | */ |
| 358 | private addCommentButtonsToCodeLines(): void { |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 359 | const diff2htmlContent = |
| 360 | this.shadowRoot?.getElementById("diff2htmlContent"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 361 | if (!diff2htmlContent) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 362 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 363 | // Target code lines first, then find their parent rows |
| 364 | const codeLines = diff2htmlContent.querySelectorAll( |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 365 | ".d2h-code-side-line, .d2h-code-line", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 366 | ); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 367 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 368 | // Create a Set to store unique rows to avoid duplicates |
| 369 | const rowsSet = new Set<HTMLElement>(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 370 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 371 | // Get all rows that contain code lines |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 372 | codeLines.forEach((line) => { |
| 373 | const row = line.closest("tr"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 374 | if (row) rowsSet.add(row as HTMLElement); |
| 375 | }); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 376 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 377 | // Convert Set back to array for processing |
| 378 | const codeRows = Array.from(rowsSet); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 379 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 380 | codeRows.forEach((row) => { |
| 381 | const rowElem = row as HTMLElement; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 382 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 383 | // Skip info lines without actual code (e.g., "file added") |
| 384 | if (rowElem.querySelector(".d2h-info")) { |
| 385 | return; |
| 386 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 387 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 388 | // Find the code line number element (first TD in the row) |
| 389 | const lineNumberCell = rowElem.querySelector( |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 390 | ".d2h-code-side-linenumber, .d2h-code-linenumber", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 391 | ); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 392 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 393 | if (!lineNumberCell) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 394 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 395 | // Create the plus button |
| 396 | const plusButton = document.createElement("span"); |
| 397 | plusButton.className = "d2h-gutter-comment-button"; |
| 398 | plusButton.innerHTML = "+"; |
| 399 | plusButton.title = "Add a comment on this line"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 400 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 401 | // Add button to the line number cell for proper positioning |
| 402 | (lineNumberCell as HTMLElement).style.position = "relative"; // Ensure positioning context |
| 403 | lineNumberCell.appendChild(plusButton); |
| 404 | }); |
| 405 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 406 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 407 | /** |
| 408 | * Open the comment box for a selected diff line |
| 409 | */ |
| 410 | private openDiffCommentBox(lineText: string): void { |
| 411 | // Make sure the comment box div exists |
| 412 | const commentBoxId = "diffCommentBox"; |
| 413 | let commentBox = this.shadowRoot?.getElementById(commentBoxId); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 414 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 415 | // If it doesn't exist, create it |
| 416 | if (!commentBox) { |
| 417 | commentBox = document.createElement("div"); |
| 418 | commentBox.id = commentBoxId; |
| 419 | commentBox.className = "diff-comment-box"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 420 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 421 | // Create the comment box contents |
| 422 | commentBox.innerHTML = ` |
| 423 | <h3>Add a comment</h3> |
| 424 | <div class="selected-line"> |
| 425 | Line: |
| 426 | <pre id="selectedLine"></pre> |
| 427 | </div> |
| 428 | <textarea |
| 429 | id="diffCommentInput" |
| 430 | placeholder="Enter your comment about this line..." |
| 431 | ></textarea> |
| 432 | <div class="diff-comment-buttons"> |
| 433 | <button id="cancelDiffComment">Cancel</button> |
| 434 | <button id="submitDiffComment">Add Comment</button> |
| 435 | </div> |
| 436 | `; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 437 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 438 | this.shadowRoot?.appendChild(commentBox); |
| 439 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 440 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 441 | // Store the selected line |
| 442 | this.selectedDiffLine = lineText; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 443 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 444 | // Display the line in the comment box |
| 445 | const selectedLine = this.shadowRoot?.getElementById("selectedLine"); |
| 446 | if (selectedLine) { |
| 447 | selectedLine.textContent = lineText; |
| 448 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 449 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 450 | // Reset the comment input |
| 451 | const commentInput = this.shadowRoot?.getElementById( |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 452 | "diffCommentInput", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 453 | ) as HTMLTextAreaElement; |
| 454 | if (commentInput) { |
| 455 | commentInput.value = ""; |
| 456 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 457 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 458 | // Show the comment box |
| 459 | if (commentBox) { |
| 460 | commentBox.style.display = "block"; |
| 461 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 462 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 463 | // Add event listeners for submit and cancel buttons |
| 464 | const submitButton = this.shadowRoot?.getElementById("submitDiffComment"); |
| 465 | if (submitButton) { |
| 466 | submitButton.onclick = () => this.submitDiffComment(); |
| 467 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 468 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 469 | const cancelButton = this.shadowRoot?.getElementById("cancelDiffComment"); |
| 470 | if (cancelButton) { |
| 471 | cancelButton.onclick = () => this.closeDiffCommentBox(); |
| 472 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 473 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 474 | // Focus on the comment input |
| 475 | if (commentInput) { |
| 476 | commentInput.focus(); |
| 477 | } |
| 478 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 479 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 480 | /** |
| 481 | * Close the diff comment box without submitting |
| 482 | */ |
| 483 | private closeDiffCommentBox(): void { |
| 484 | const commentBox = this.shadowRoot?.getElementById("diffCommentBox"); |
| 485 | if (commentBox) { |
| 486 | commentBox.style.display = "none"; |
| 487 | } |
| 488 | this.selectedDiffLine = null; |
| 489 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 490 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Submit a comment on a diff line |
| 493 | */ |
| 494 | private submitDiffComment(): void { |
| 495 | const commentInput = this.shadowRoot?.getElementById( |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 496 | "diffCommentInput", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 497 | ) as HTMLTextAreaElement; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 498 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 499 | if (!commentInput) return; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 500 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 501 | const comment = commentInput.value.trim(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 502 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 503 | // Validate inputs |
| 504 | if (!this.selectedDiffLine || !comment) { |
| 505 | alert("Please select a line and enter a comment."); |
| 506 | return; |
| 507 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 508 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 509 | // Format the comment in a readable way |
| 510 | const formattedComment = `\`\`\`\n${this.selectedDiffLine}\n\`\`\`\n\n${comment}`; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 511 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 512 | // Dispatch a custom event with the formatted comment |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 513 | const event = new CustomEvent("diff-comment", { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 514 | detail: { comment: formattedComment }, |
| 515 | bubbles: true, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 516 | composed: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 517 | }); |
| 518 | this.dispatchEvent(event); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 519 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 520 | // Close only the comment box but keep the diff view open |
| 521 | this.closeDiffCommentBox(); |
| 522 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 523 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 524 | // Clear the current state |
| 525 | public clearState(): void { |
| 526 | this.commitHash = ""; |
| 527 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 528 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 529 | // Show diff for a specific commit |
| 530 | public showCommitDiff(commitHash: string): void { |
| 531 | // Store the commit hash |
| 532 | this.commitHash = commitHash; |
| 533 | // Load the diff content |
| 534 | this.loadDiffContent(); |
| 535 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 536 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 537 | render() { |
| 538 | return html` |
| 539 | <div class="diff-view"> |
| 540 | <div class="diff-container"> |
| 541 | <div id="diff-view-controls"> |
| 542 | <div class="diff-view-format"> |
| 543 | <label> |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 544 | <input |
| 545 | type="radio" |
| 546 | name="diffViewFormat" |
| 547 | value="side-by-side" |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 548 | ?checked=${this.viewFormat === "side-by-side"} |
| 549 | @change=${this.handleViewFormatChange} |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 550 | /> |
| 551 | Side-by-side |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 552 | </label> |
| 553 | <label> |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 554 | <input |
| 555 | type="radio" |
| 556 | name="diffViewFormat" |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 557 | value="line-by-line" |
| 558 | ?checked=${this.viewFormat === "line-by-line"} |
| 559 | @change=${this.handleViewFormatChange} |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 560 | /> |
| 561 | Line-by-line |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 562 | </label> |
| 563 | </div> |
| 564 | </div> |
| 565 | <div id="diff2htmlContent" class="diff2html-content"></div> |
| 566 | </div> |
| 567 | </div> |
| 568 | `; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | declare global { |
| 573 | interface HTMLElementTagNameMap { |
| 574 | "sketch-diff-view": SketchDiffView; |
| 575 | } |
| 576 | } |