| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 2 | import { css, html, LitElement } from "lit"; |
| 3 | import { customElement, property, state } from "lit/decorators.js"; |
| 4 | import "./sketch-monaco-view"; |
| 5 | import "./sketch-diff-range-picker"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 6 | import "./sketch-diff-empty-view"; |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 7 | import { GitDiffFile, GitDataService } from "./git-data-service"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 8 | import { DiffRange } from "./sketch-diff-range-picker"; |
| 9 | |
| 10 | /** |
| 11 | * A component that displays diffs using Monaco editor with range and file pickers |
| 12 | */ |
| 13 | @customElement("sketch-diff2-view") |
| 14 | export class SketchDiff2View extends LitElement { |
| 15 | /** |
| 16 | * Handles comment events from the Monaco editor and forwards them to the chat input |
| 17 | * using the same event format as the original diff view for consistency. |
| 18 | */ |
| 19 | private handleMonacoComment(event: CustomEvent) { |
| 20 | try { |
| 21 | // Validate incoming data |
| 22 | if (!event.detail || !event.detail.formattedComment) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 23 | console.error("Invalid comment data received"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 24 | return; |
| 25 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 26 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 27 | // Create and dispatch event using the standardized format |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 28 | const commentEvent = new CustomEvent("diff-comment", { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 29 | detail: { comment: event.detail.formattedComment }, |
| 30 | bubbles: true, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 31 | composed: true, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 32 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 33 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 34 | this.dispatchEvent(commentEvent); |
| 35 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 36 | console.error("Error handling Monaco comment:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 39 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 40 | /** |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 41 | * Handle height change events from the Monaco editor |
| 42 | */ |
| 43 | private handleMonacoHeightChange(event: CustomEvent) { |
| 44 | try { |
| 45 | // Get the monaco view that emitted the event |
| 46 | const monacoView = event.target as HTMLElement; |
| 47 | if (!monacoView) return; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 48 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 49 | // Find the parent file-diff-editor container |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 50 | const fileDiffEditor = monacoView.closest( |
| 51 | ".file-diff-editor", |
| 52 | ) as HTMLElement; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 53 | if (!fileDiffEditor) return; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 54 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 55 | // Get the new height from the event |
| 56 | const newHeight = event.detail.height; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 57 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 58 | // Only update if the height actually changed to avoid unnecessary layout |
| 59 | const currentHeight = fileDiffEditor.style.height; |
| 60 | const newHeightStr = `${newHeight}px`; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 61 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 62 | if (currentHeight !== newHeightStr) { |
| 63 | // Update the file-diff-editor height to match monaco's height |
| 64 | fileDiffEditor.style.height = newHeightStr; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 65 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 66 | // Remove any previous min-height constraint that might interfere |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 67 | fileDiffEditor.style.minHeight = "auto"; |
| 68 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 69 | // IMPORTANT: Tell Monaco to relayout after its container size changed |
| 70 | // Monaco has automaticLayout: false, so it won't detect container changes |
| 71 | setTimeout(() => { |
| 72 | const monacoComponent = monacoView as any; |
| 73 | if (monacoComponent && monacoComponent.editor) { |
| 74 | // Force layout with explicit dimensions to ensure Monaco fills the space |
| 75 | const editorWidth = fileDiffEditor.offsetWidth; |
| 76 | monacoComponent.editor.layout({ |
| 77 | width: editorWidth, |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 78 | height: newHeight, |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 79 | }); |
| 80 | } |
| 81 | }, 0); |
| 82 | } |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 83 | } catch (error) { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 84 | console.error("Error handling Monaco height change:", error); |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 89 | * Handle save events from the Monaco editor |
| 90 | */ |
| 91 | private async handleMonacoSave(event: CustomEvent) { |
| 92 | try { |
| 93 | // Validate incoming data |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 94 | if ( |
| 95 | !event.detail || |
| 96 | !event.detail.path || |
| 97 | event.detail.content === undefined |
| 98 | ) { |
| 99 | console.error("Invalid save data received"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 102 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 103 | const { path, content } = event.detail; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 104 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 105 | // Get Monaco view component |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 106 | const monacoView = this.shadowRoot?.querySelector("sketch-monaco-view"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 107 | if (!monacoView) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 108 | console.error("Monaco view not found"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 109 | return; |
| 110 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 111 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 112 | try { |
| 113 | await this.gitService?.saveFileContent(path, content); |
| 114 | console.log(`File saved: ${path}`); |
| 115 | (monacoView as any).notifySaveComplete(true); |
| 116 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 117 | console.error( |
| 118 | `Error saving file: ${error instanceof Error ? error.message : String(error)}`, |
| 119 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 120 | (monacoView as any).notifySaveComplete(false); |
| 121 | } |
| 122 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 123 | console.error("Error handling save:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | @property({ type: String }) |
| 127 | initialCommit: string = ""; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 128 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 129 | // The commit to show - used when showing a specific commit from timeline |
| 130 | @property({ type: String }) |
| 131 | commit: string = ""; |
| 132 | |
| 133 | @property({ type: String }) |
| 134 | selectedFilePath: string = ""; |
| 135 | |
| 136 | @state() |
| 137 | private files: GitDiffFile[] = []; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 138 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 139 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 140 | private currentRange: DiffRange = { type: "range", from: "", to: "HEAD" }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 141 | |
| 142 | @state() |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 143 | private fileContents: Map< |
| 144 | string, |
| 145 | { original: string; modified: string; editable: boolean } |
| 146 | > = new Map(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 147 | |
| 148 | @state() |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 149 | private fileExpandStates: Map<string, boolean> = new Map(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 150 | |
| 151 | @state() |
| 152 | private loading: boolean = false; |
| 153 | |
| 154 | @state() |
| 155 | private error: string | null = null; |
| 156 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 157 | @state() |
| 158 | private selectedFile: string = ""; // Empty string means "All files" |
| 159 | |
| 160 | @state() |
| 161 | private viewMode: "all" | "single" = "all"; |
| 162 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 163 | static styles = css` |
| 164 | :host { |
| 165 | display: flex; |
| 166 | height: 100%; |
| 167 | flex: 1; |
| 168 | flex-direction: column; |
| 169 | min-height: 0; /* Critical for flex child behavior */ |
| 170 | overflow: hidden; |
| 171 | position: relative; /* Establish positioning context */ |
| 172 | } |
| 173 | |
| 174 | .controls { |
| 175 | padding: 8px 16px; |
| 176 | border-bottom: 1px solid var(--border-color, #e0e0e0); |
| 177 | background-color: var(--background-light, #f8f8f8); |
| 178 | flex-shrink: 0; /* Prevent controls from shrinking */ |
| 179 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 180 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 181 | .controls-container { |
| 182 | display: flex; |
| 183 | flex-direction: column; |
| 184 | gap: 12px; |
| 185 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 186 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 187 | .range-row { |
| 188 | width: 100%; |
| 189 | display: flex; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 190 | align-items: center; |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 191 | gap: 12px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 192 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 193 | |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 194 | .file-selector-container { |
| 195 | display: flex; |
| 196 | align-items: center; |
| 197 | gap: 8px; |
| 198 | } |
| 199 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 200 | .file-selector { |
| 201 | min-width: 200px; |
| 202 | padding: 8px 12px; |
| 203 | border: 1px solid var(--border-color, #ccc); |
| 204 | border-radius: 4px; |
| 205 | background-color: var(--background-color, #fff); |
| 206 | font-family: var(--font-family, system-ui, sans-serif); |
| 207 | font-size: 14px; |
| 208 | cursor: pointer; |
| 209 | } |
| 210 | |
| 211 | .file-selector:focus { |
| 212 | outline: none; |
| 213 | border-color: var(--accent-color, #007acc); |
| 214 | box-shadow: 0 0 0 2px var(--accent-color-light, rgba(0, 122, 204, 0.2)); |
| 215 | } |
| 216 | |
| David Crawshaw | 5c6d829 | 2025-06-15 19:09:19 +0000 | [diff] [blame] | 217 | .file-selector:disabled { |
| 218 | background-color: var(--background-disabled, #f5f5f5); |
| 219 | color: var(--text-disabled, #999); |
| 220 | cursor: not-allowed; |
| 221 | } |
| 222 | |
| 223 | .spacer { |
| 224 | flex: 1; |
| 225 | } |
| 226 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 227 | sketch-diff-range-picker { |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 228 | flex: 1; |
| David Crawshaw | e2954ce | 2025-06-15 00:06:34 +0000 | [diff] [blame] | 229 | min-width: 400px; /* Ensure minimum width for range picker */ |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 230 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 231 | |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 232 | .view-toggle-button, |
| 233 | .header-expand-button { |
| 234 | background-color: transparent; |
| 235 | border: 1px solid var(--border-color, #e0e0e0); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 236 | border-radius: 4px; |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 237 | padding: 6px 8px; |
| 238 | font-size: 14px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 239 | cursor: pointer; |
| 240 | white-space: nowrap; |
| 241 | transition: background-color 0.2s; |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 242 | display: flex; |
| 243 | align-items: center; |
| 244 | justify-content: center; |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 245 | min-width: 32px; |
| 246 | min-height: 32px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 247 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 248 | |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 249 | .view-toggle-button:hover, |
| 250 | .header-expand-button:hover { |
| 251 | background-color: var(--background-hover, #e8e8e8); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | .diff-container { |
| 255 | flex: 1; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 256 | overflow: auto; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 257 | display: flex; |
| 258 | flex-direction: column; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 259 | min-height: 0; |
| 260 | position: relative; |
| 261 | height: 100%; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | .diff-content { |
| 265 | flex: 1; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 266 | overflow: auto; |
| 267 | min-height: 0; |
| 268 | display: flex; |
| 269 | flex-direction: column; |
| 270 | position: relative; |
| 271 | height: 100%; |
| 272 | } |
| 273 | |
| 274 | .multi-file-diff-container { |
| 275 | display: flex; |
| 276 | flex-direction: column; |
| 277 | width: 100%; |
| 278 | min-height: 100%; |
| 279 | } |
| 280 | |
| 281 | .file-diff-section { |
| 282 | display: flex; |
| 283 | flex-direction: column; |
| 284 | border-bottom: 3px solid var(--border-color, #e0e0e0); |
| 285 | margin-bottom: 0; |
| 286 | } |
| 287 | |
| 288 | .file-diff-section:last-child { |
| 289 | border-bottom: none; |
| 290 | } |
| 291 | |
| 292 | .file-header { |
| 293 | background-color: var(--background-light, #f8f8f8); |
| 294 | border-bottom: 1px solid var(--border-color, #e0e0e0); |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 295 | padding: 8px 16px; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 296 | font-family: var(--font-family, system-ui, sans-serif); |
| 297 | font-weight: 500; |
| 298 | font-size: 14px; |
| 299 | color: var(--text-primary-color, #333); |
| 300 | position: sticky; |
| 301 | top: 0; |
| 302 | z-index: 10; |
| 303 | box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); |
| 304 | display: flex; |
| 305 | justify-content: space-between; |
| 306 | align-items: center; |
| 307 | } |
| 308 | |
| 309 | .file-header-left { |
| 310 | display: flex; |
| 311 | align-items: center; |
| 312 | gap: 8px; |
| 313 | } |
| 314 | |
| 315 | .file-header-right { |
| 316 | display: flex; |
| 317 | align-items: center; |
| 318 | } |
| 319 | |
| 320 | .file-expand-button { |
| 321 | background-color: transparent; |
| 322 | border: 1px solid var(--border-color, #e0e0e0); |
| 323 | border-radius: 4px; |
| 324 | padding: 4px 8px; |
| 325 | font-size: 14px; |
| 326 | cursor: pointer; |
| 327 | transition: background-color 0.2s; |
| 328 | display: flex; |
| 329 | align-items: center; |
| 330 | justify-content: center; |
| 331 | min-width: 32px; |
| 332 | min-height: 32px; |
| 333 | } |
| 334 | |
| 335 | .file-expand-button:hover { |
| 336 | background-color: var(--background-hover, #e8e8e8); |
| 337 | } |
| 338 | |
| 339 | .file-path { |
| 340 | font-family: monospace; |
| 341 | font-weight: normal; |
| 342 | color: var(--text-secondary-color, #666); |
| 343 | } |
| 344 | |
| 345 | .file-status { |
| 346 | display: inline-block; |
| 347 | padding: 2px 6px; |
| 348 | border-radius: 3px; |
| 349 | font-size: 12px; |
| 350 | font-weight: bold; |
| 351 | margin-right: 8px; |
| 352 | } |
| 353 | |
| 354 | .file-status.added { |
| 355 | background-color: #d4edda; |
| 356 | color: #155724; |
| 357 | } |
| 358 | |
| 359 | .file-status.modified { |
| 360 | background-color: #fff3cd; |
| 361 | color: #856404; |
| 362 | } |
| 363 | |
| 364 | .file-status.deleted { |
| 365 | background-color: #f8d7da; |
| 366 | color: #721c24; |
| 367 | } |
| 368 | |
| 369 | .file-status.renamed { |
| 370 | background-color: #d1ecf1; |
| 371 | color: #0c5460; |
| 372 | } |
| 373 | |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 374 | .file-status.copied { |
| 375 | background-color: #e2e3ff; |
| 376 | color: #383d41; |
| 377 | } |
| 378 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 379 | .file-changes { |
| 380 | margin-left: 8px; |
| 381 | font-size: 12px; |
| 382 | color: var(--text-secondary-color, #666); |
| 383 | } |
| 384 | |
| 385 | .file-diff-editor { |
| 386 | display: flex; |
| 387 | flex-direction: column; |
| 388 | min-height: 200px; |
| 389 | /* Height will be set dynamically by monaco editor */ |
| 390 | overflow: visible; /* Ensure content is not clipped */ |
| 391 | } |
| 392 | |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 393 | .loading, |
| 394 | .empty-diff { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 395 | display: flex; |
| 396 | align-items: center; |
| 397 | justify-content: center; |
| 398 | height: 100%; |
| 399 | font-family: var(--font-family, system-ui, sans-serif); |
| 400 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 401 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 402 | .empty-diff { |
| 403 | color: var(--text-secondary-color, #666); |
| 404 | font-size: 16px; |
| 405 | text-align: center; |
| 406 | } |
| 407 | |
| 408 | .error { |
| 409 | color: var(--error-color, #dc3545); |
| 410 | padding: 16px; |
| 411 | font-family: var(--font-family, system-ui, sans-serif); |
| 412 | } |
| 413 | |
| 414 | sketch-monaco-view { |
| 415 | --editor-width: 100%; |
| 416 | --editor-height: 100%; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 417 | display: flex; |
| 418 | flex-direction: column; |
| 419 | width: 100%; |
| 420 | min-height: 200px; |
| 421 | /* Ensure Monaco view takes full container space */ |
| 422 | flex: 1; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 423 | } |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 424 | |
| 425 | /* Single file view styles */ |
| 426 | .single-file-view { |
| 427 | flex: 1; |
| 428 | display: flex; |
| 429 | flex-direction: column; |
| 430 | height: 100%; |
| 431 | min-height: 0; |
| 432 | } |
| 433 | |
| 434 | .single-file-monaco { |
| 435 | flex: 1; |
| 436 | width: 100%; |
| 437 | height: 100%; |
| 438 | min-height: 0; |
| 439 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 440 | `; |
| 441 | |
| 442 | @property({ attribute: false, type: Object }) |
| 443 | gitService!: GitDataService; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 444 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 445 | // The gitService must be passed from parent to ensure proper dependency injection |
| 446 | |
| 447 | constructor() { |
| 448 | super(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 449 | console.log("SketchDiff2View initialized"); |
| 450 | |
| David Crawshaw | e2954ce | 2025-06-15 00:06:34 +0000 | [diff] [blame] | 451 | // Fix for monaco-aria-container positioning and hide scrollbars globally |
| 452 | // Add a global style to ensure proper positioning of aria containers and hide scrollbars |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 453 | const styleElement = document.createElement("style"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 454 | styleElement.textContent = ` |
| 455 | .monaco-aria-container { |
| 456 | position: absolute !important; |
| 457 | top: 0 !important; |
| 458 | left: 0 !important; |
| 459 | width: 1px !important; |
| 460 | height: 1px !important; |
| 461 | overflow: hidden !important; |
| 462 | clip: rect(1px, 1px, 1px, 1px) !important; |
| 463 | white-space: nowrap !important; |
| 464 | margin: 0 !important; |
| 465 | padding: 0 !important; |
| 466 | border: 0 !important; |
| 467 | z-index: -1 !important; |
| 468 | } |
| David Crawshaw | e2954ce | 2025-06-15 00:06:34 +0000 | [diff] [blame] | 469 | |
| 470 | /* Aggressively hide all Monaco scrollbar elements */ |
| 471 | .monaco-editor .scrollbar, |
| 472 | .monaco-editor .scroll-decoration, |
| 473 | .monaco-editor .invisible.scrollbar, |
| 474 | .monaco-editor .slider, |
| 475 | .monaco-editor .vertical.scrollbar, |
| 476 | .monaco-editor .horizontal.scrollbar, |
| 477 | .monaco-diff-editor .scrollbar, |
| 478 | .monaco-diff-editor .scroll-decoration, |
| 479 | .monaco-diff-editor .invisible.scrollbar, |
| 480 | .monaco-diff-editor .slider, |
| 481 | .monaco-diff-editor .vertical.scrollbar, |
| 482 | .monaco-diff-editor .horizontal.scrollbar { |
| 483 | display: none !important; |
| 484 | visibility: hidden !important; |
| 485 | width: 0 !important; |
| 486 | height: 0 !important; |
| 487 | opacity: 0 !important; |
| 488 | } |
| 489 | |
| 490 | /* Target the specific scrollbar classes that Monaco uses */ |
| 491 | .monaco-scrollable-element > .scrollbar, |
| 492 | .monaco-scrollable-element > .scroll-decoration, |
| 493 | .monaco-scrollable-element .slider { |
| 494 | display: none !important; |
| 495 | visibility: hidden !important; |
| 496 | width: 0 !important; |
| 497 | height: 0 !important; |
| 498 | } |
| 499 | |
| 500 | /* Remove scrollbar space/padding from content area */ |
| 501 | .monaco-editor .monaco-scrollable-element, |
| 502 | .monaco-diff-editor .monaco-scrollable-element { |
| 503 | padding-right: 0 !important; |
| 504 | padding-bottom: 0 !important; |
| 505 | margin-right: 0 !important; |
| 506 | margin-bottom: 0 !important; |
| 507 | } |
| 508 | |
| 509 | /* Ensure the diff content takes full width without scrollbar space */ |
| 510 | .monaco-diff-editor .editor.modified, |
| 511 | .monaco-diff-editor .editor.original { |
| 512 | margin-right: 0 !important; |
| 513 | padding-right: 0 !important; |
| 514 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 515 | `; |
| 516 | document.head.appendChild(styleElement); |
| 517 | } |
| 518 | |
| 519 | connectedCallback() { |
| 520 | super.connectedCallback(); |
| 521 | // Initialize with default range and load data |
| 522 | // Get base commit if not set |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 523 | if ( |
| 524 | this.currentRange.type === "range" && |
| 525 | !("from" in this.currentRange && this.currentRange.from) |
| 526 | ) { |
| 527 | this.gitService |
| 528 | .getBaseCommitRef() |
| 529 | .then((baseRef) => { |
| 530 | this.currentRange = { type: "range", from: baseRef, to: "HEAD" }; |
| 531 | this.loadDiffData(); |
| 532 | }) |
| 533 | .catch((error) => { |
| 534 | console.error("Error getting base commit ref:", error); |
| 535 | // Use default range |
| 536 | this.loadDiffData(); |
| 537 | }); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 538 | } else { |
| 539 | this.loadDiffData(); |
| 540 | } |
| 541 | } |
| 542 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 543 | // Toggle hideUnchangedRegions setting for a specific file |
| 544 | private toggleFileExpansion(filePath: string) { |
| 545 | const currentState = this.fileExpandStates.get(filePath) ?? false; |
| 546 | const newState = !currentState; |
| 547 | this.fileExpandStates.set(filePath, newState); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 548 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 549 | // Apply to the specific Monaco view component for this file |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 550 | const monacoView = this.shadowRoot?.querySelector( |
| 551 | `sketch-monaco-view[data-file-path="${filePath}"]`, |
| 552 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 553 | if (monacoView) { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 554 | (monacoView as any).toggleHideUnchangedRegions(!newState); // inverted because true means "hide unchanged" |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 555 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 556 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 557 | // Force a re-render to update the button state |
| 558 | this.requestUpdate(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 559 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 560 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 561 | render() { |
| 562 | return html` |
| 563 | <div class="controls"> |
| 564 | <div class="controls-container"> |
| 565 | <div class="range-row"> |
| 566 | <sketch-diff-range-picker |
| 567 | .gitService="${this.gitService}" |
| 568 | @range-change="${this.handleRangeChange}" |
| 569 | ></sketch-diff-range-picker> |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 570 | <div class="spacer"></div> |
| 571 | ${this.renderFileSelector()} |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 572 | </div> |
| 573 | </div> |
| 574 | </div> |
| 575 | |
| 576 | <div class="diff-container"> |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 577 | <div class="diff-content">${this.renderDiffContent()}</div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 578 | </div> |
| 579 | `; |
| 580 | } |
| 581 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 582 | renderFileSelector() { |
| David Crawshaw | 5c6d829 | 2025-06-15 19:09:19 +0000 | [diff] [blame] | 583 | const fileCount = this.files.length; |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 584 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 585 | return html` |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 586 | <div class="file-selector-container"> |
| 587 | <select |
| 588 | class="file-selector" |
| 589 | .value="${this.selectedFile}" |
| 590 | @change="${this.handleFileSelection}" |
| 591 | ?disabled="${fileCount === 0}" |
| 592 | > |
| 593 | <option value="">All files (${fileCount})</option> |
| 594 | ${this.files.map( |
| 595 | (file) => html` |
| 596 | <option value="${file.path}"> |
| 597 | ${this.getFileDisplayName(file)} |
| 598 | </option> |
| 599 | `, |
| 600 | )} |
| 601 | </select> |
| 602 | ${this.selectedFile ? this.renderSingleFileExpandButton() : ""} |
| 603 | </div> |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 604 | `; |
| 605 | } |
| 606 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 607 | renderDiffContent() { |
| 608 | if (this.loading) { |
| 609 | return html`<div class="loading">Loading diff...</div>`; |
| 610 | } |
| 611 | |
| 612 | if (this.error) { |
| 613 | return html`<div class="error">${this.error}</div>`; |
| 614 | } |
| 615 | |
| 616 | if (this.files.length === 0) { |
| 617 | return html`<sketch-diff-empty-view></sketch-diff-empty-view>`; |
| 618 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 619 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 620 | // Render single file view if a specific file is selected |
| 621 | if (this.selectedFile && this.viewMode === "single") { |
| 622 | return this.renderSingleFileView(); |
| 623 | } |
| 624 | |
| 625 | // Render multi-file view |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 626 | return html` |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 627 | <div class="multi-file-diff-container"> |
| 628 | ${this.files.map((file, index) => this.renderFileDiff(file, index))} |
| 629 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 630 | `; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Load diff data for the current range |
| 635 | */ |
| 636 | async loadDiffData() { |
| 637 | this.loading = true; |
| 638 | this.error = null; |
| 639 | |
| 640 | try { |
| 641 | // Initialize files as empty array if undefined |
| 642 | if (!this.files) { |
| 643 | this.files = []; |
| 644 | } |
| 645 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 646 | // Load diff data for the range |
| 647 | this.files = await this.gitService.getDiff( |
| 648 | this.currentRange.from, |
| 649 | this.currentRange.to, |
| 650 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 651 | |
| 652 | // Ensure files is always an array, even when API returns null |
| 653 | if (!this.files) { |
| 654 | this.files = []; |
| 655 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 656 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 657 | // Load content for all files |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 658 | if (this.files.length > 0) { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 659 | // Initialize expand states for new files (default to collapsed) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 660 | this.files.forEach((file) => { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 661 | if (!this.fileExpandStates.has(file.path)) { |
| 662 | this.fileExpandStates.set(file.path, false); // false = collapsed (hide unchanged regions) |
| 663 | } |
| 664 | }); |
| 665 | await this.loadAllFileContents(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 666 | } else { |
| 667 | // No files to display - reset the view to initial state |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 668 | this.selectedFilePath = ""; |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 669 | this.selectedFile = ""; |
| 670 | this.viewMode = "all"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 671 | this.fileContents.clear(); |
| 672 | this.fileExpandStates.clear(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 673 | } |
| 674 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 675 | console.error("Error loading diff data:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 676 | this.error = `Error loading diff data: ${error.message}`; |
| 677 | // Ensure files is an empty array when an error occurs |
| 678 | this.files = []; |
| 679 | // Reset the view to initial state |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 680 | this.selectedFilePath = ""; |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 681 | this.selectedFile = ""; |
| 682 | this.viewMode = "all"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 683 | this.fileContents.clear(); |
| 684 | this.fileExpandStates.clear(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 685 | } finally { |
| 686 | this.loading = false; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /** |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 691 | * Load content for all files in the diff |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 692 | */ |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 693 | async loadAllFileContents() { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 694 | this.loading = true; |
| 695 | this.error = null; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 696 | this.fileContents.clear(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 697 | |
| 698 | try { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 699 | let isUnstagedChanges = false; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 700 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 701 | // Determine the commits to compare based on the current range |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 702 | const _fromCommit = this.currentRange.from; |
| 703 | const toCommit = this.currentRange.to; |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 704 | // Check if this is an unstaged changes view |
| 705 | isUnstagedChanges = toCommit === ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 706 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 707 | // Load content for all files |
| 708 | const promises = this.files.map(async (file) => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 709 | try { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 710 | let originalCode = ""; |
| 711 | let modifiedCode = ""; |
| 712 | let editable = isUnstagedChanges; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 713 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 714 | // Load the original code based on file status |
| 715 | if (file.status !== "A") { |
| 716 | // For modified, renamed, or deleted files: load original content |
| 717 | originalCode = await this.gitService.getFileContent( |
| 718 | file.old_hash || "", |
| 719 | ); |
| 720 | } |
| 721 | |
| 722 | // For modified code, always use working copy when editable |
| 723 | if (editable) { |
| 724 | try { |
| 725 | // Always use working copy when editable, regardless of diff status |
| 726 | modifiedCode = await this.gitService.getWorkingCopyContent( |
| 727 | file.path, |
| 728 | ); |
| 729 | } catch (error) { |
| 730 | if (file.status === "D") { |
| 731 | // For deleted files, silently use empty content |
| 732 | console.warn( |
| 733 | `Could not get working copy for deleted file ${file.path}, using empty content`, |
| 734 | ); |
| 735 | modifiedCode = ""; |
| 736 | } else { |
| 737 | // For any other file status, propagate the error |
| 738 | console.error( |
| 739 | `Failed to get working copy for ${file.path}:`, |
| 740 | error, |
| 741 | ); |
| 742 | throw error; |
| 743 | } |
| 744 | } |
| 745 | } else { |
| 746 | // For non-editable view, use git content based on file status |
| 747 | if (file.status === "D") { |
| 748 | // Deleted file: empty modified |
| 749 | modifiedCode = ""; |
| 750 | } else { |
| 751 | // Added/modified/renamed: use the content from git |
| 752 | modifiedCode = await this.gitService.getFileContent( |
| 753 | file.new_hash || "", |
| 754 | ); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // Don't make deleted files editable |
| 759 | if (file.status === "D") { |
| 760 | editable = false; |
| 761 | } |
| 762 | |
| 763 | this.fileContents.set(file.path, { |
| 764 | original: originalCode, |
| 765 | modified: modifiedCode, |
| 766 | editable, |
| 767 | }); |
| 768 | } catch (error) { |
| 769 | console.error(`Error loading content for file ${file.path}:`, error); |
| 770 | // Store empty content for failed files to prevent blocking |
| 771 | this.fileContents.set(file.path, { |
| 772 | original: "", |
| 773 | modified: "", |
| 774 | editable: false, |
| 775 | }); |
| 776 | } |
| 777 | }); |
| 778 | |
| 779 | await Promise.all(promises); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 780 | } catch (error) { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 781 | console.error("Error loading file contents:", error); |
| 782 | this.error = `Error loading file contents: ${error.message}`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 783 | } finally { |
| 784 | this.loading = false; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Handle range change event from the range picker |
| 790 | */ |
| 791 | handleRangeChange(event: CustomEvent) { |
| 792 | const { range } = event.detail; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 793 | console.log("Range changed:", range); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 794 | this.currentRange = range; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 795 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 796 | // Load diff data for the new range |
| 797 | this.loadDiffData(); |
| 798 | } |
| 799 | |
| 800 | /** |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 801 | * Render a single file diff section |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 802 | */ |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 803 | renderFileDiff(file: GitDiffFile, index: number) { |
| 804 | const content = this.fileContents.get(file.path); |
| 805 | if (!content) { |
| 806 | return html` |
| 807 | <div class="file-diff-section"> |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 808 | <div class="file-header">${this.renderFileHeader(file)}</div> |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 809 | <div class="loading">Loading ${file.path}...</div> |
| 810 | </div> |
| 811 | `; |
| 812 | } |
| 813 | |
| 814 | return html` |
| 815 | <div class="file-diff-section"> |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 816 | <div class="file-header">${this.renderFileHeader(file)}</div> |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 817 | <div class="file-diff-editor"> |
| 818 | <sketch-monaco-view |
| 819 | .originalCode="${content.original}" |
| 820 | .modifiedCode="${content.modified}" |
| 821 | .originalFilename="${file.path}" |
| 822 | .modifiedFilename="${file.path}" |
| 823 | ?readOnly="${!content.editable}" |
| 824 | ?editable-right="${content.editable}" |
| 825 | @monaco-comment="${this.handleMonacoComment}" |
| 826 | @monaco-save="${this.handleMonacoSave}" |
| 827 | @monaco-height-changed="${this.handleMonacoHeightChange}" |
| 828 | data-file-index="${index}" |
| 829 | data-file-path="${file.path}" |
| 830 | ></sketch-monaco-view> |
| 831 | </div> |
| 832 | </div> |
| 833 | `; |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Render file header with status and path info |
| 838 | */ |
| 839 | renderFileHeader(file: GitDiffFile) { |
| 840 | const statusClass = this.getFileStatusClass(file.status); |
| 841 | const statusText = this.getFileStatusText(file.status); |
| 842 | const changesInfo = this.getChangesInfo(file); |
| 843 | const pathInfo = this.getPathInfo(file); |
| 844 | |
| 845 | const isExpanded = this.fileExpandStates.get(file.path) ?? false; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 846 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 847 | return html` |
| 848 | <div class="file-header-left"> |
| 849 | <span class="file-status ${statusClass}">${statusText}</span> |
| 850 | <span class="file-path">${pathInfo}</span> |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 851 | ${changesInfo |
| 852 | ? html`<span class="file-changes">${changesInfo}</span>` |
| 853 | : ""} |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 854 | </div> |
| 855 | <div class="file-header-right"> |
| 856 | <button |
| 857 | class="file-expand-button" |
| 858 | @click="${() => this.toggleFileExpansion(file.path)}" |
| 859 | title="${isExpanded |
| 860 | ? "Collapse: Hide unchanged regions to focus on changes" |
| 861 | : "Expand: Show all lines including unchanged regions"}" |
| 862 | > |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 863 | ${isExpanded ? this.renderCollapseIcon() : this.renderExpandAllIcon()} |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 864 | </button> |
| 865 | </div> |
| 866 | `; |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Get CSS class for file status |
| 871 | */ |
| 872 | getFileStatusClass(status: string): string { |
| 873 | switch (status.toUpperCase()) { |
| 874 | case "A": |
| 875 | return "added"; |
| 876 | case "M": |
| 877 | return "modified"; |
| 878 | case "D": |
| 879 | return "deleted"; |
| 880 | case "R": |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 881 | case "C": |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 882 | default: |
| 883 | if (status.toUpperCase().startsWith("R")) { |
| 884 | return "renamed"; |
| 885 | } |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 886 | if (status.toUpperCase().startsWith("C")) { |
| 887 | return "copied"; |
| 888 | } |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 889 | return "modified"; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * Get display text for file status |
| 895 | */ |
| 896 | getFileStatusText(status: string): string { |
| 897 | switch (status.toUpperCase()) { |
| 898 | case "A": |
| 899 | return "Added"; |
| 900 | case "M": |
| 901 | return "Modified"; |
| 902 | case "D": |
| 903 | return "Deleted"; |
| 904 | case "R": |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 905 | case "C": |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 906 | default: |
| 907 | if (status.toUpperCase().startsWith("R")) { |
| 908 | return "Renamed"; |
| 909 | } |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 910 | if (status.toUpperCase().startsWith("C")) { |
| 911 | return "Copied"; |
| 912 | } |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 913 | return "Modified"; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Get changes information (+/-) for display |
| 919 | */ |
| 920 | getChangesInfo(file: GitDiffFile): string { |
| 921 | const additions = file.additions || 0; |
| 922 | const deletions = file.deletions || 0; |
| 923 | |
| 924 | if (additions === 0 && deletions === 0) { |
| 925 | return ""; |
| 926 | } |
| 927 | |
| 928 | const parts = []; |
| 929 | if (additions > 0) { |
| 930 | parts.push(`+${additions}`); |
| 931 | } |
| 932 | if (deletions > 0) { |
| 933 | parts.push(`-${deletions}`); |
| 934 | } |
| 935 | |
| 936 | return `(${parts.join(", ")})`; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Get path information for display, handling renames |
| 941 | */ |
| 942 | getPathInfo(file: GitDiffFile): string { |
| 943 | if (file.old_path && file.old_path !== "") { |
| 944 | // For renames, show old_path → new_path |
| 945 | return `${file.old_path} → ${file.path}`; |
| 946 | } |
| 947 | // For regular files, just show the path |
| 948 | return file.path; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /** |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 952 | * Render expand all icon (dotted line with arrows pointing away) |
| 953 | */ |
| 954 | renderExpandAllIcon() { |
| 955 | return html` |
| 956 | <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"> |
| 957 | <!-- Dotted line in the middle --> |
| 958 | <line |
| 959 | x1="2" |
| 960 | y1="8" |
| 961 | x2="14" |
| 962 | y2="8" |
| 963 | stroke="currentColor" |
| 964 | stroke-width="1" |
| 965 | stroke-dasharray="2,1" |
| 966 | /> |
| 967 | <!-- Large arrow pointing up --> |
| 968 | <path d="M8 2 L5 6 L11 6 Z" fill="currentColor" /> |
| 969 | <!-- Large arrow pointing down --> |
| 970 | <path d="M8 14 L5 10 L11 10 Z" fill="currentColor" /> |
| 971 | </svg> |
| 972 | `; |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * Render collapse icon (arrows pointing towards dotted line) |
| 977 | */ |
| 978 | renderCollapseIcon() { |
| 979 | return html` |
| 980 | <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"> |
| 981 | <!-- Dotted line in the middle --> |
| 982 | <line |
| 983 | x1="2" |
| 984 | y1="8" |
| 985 | x2="14" |
| 986 | y2="8" |
| 987 | stroke="currentColor" |
| 988 | stroke-width="1" |
| 989 | stroke-dasharray="2,1" |
| 990 | /> |
| 991 | <!-- Large arrow pointing down towards line --> |
| 992 | <path d="M8 6 L5 2 L11 2 Z" fill="currentColor" /> |
| 993 | <!-- Large arrow pointing up towards line --> |
| 994 | <path d="M8 10 L5 14 L11 14 Z" fill="currentColor" /> |
| 995 | </svg> |
| 996 | `; |
| 997 | } |
| 998 | |
| 999 | /** |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 1000 | * Handle file selection change from the dropdown |
| 1001 | */ |
| 1002 | handleFileSelection(event: Event) { |
| 1003 | const selectElement = event.target as HTMLSelectElement; |
| 1004 | const selectedValue = selectElement.value; |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 1005 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 1006 | this.selectedFile = selectedValue; |
| 1007 | this.viewMode = selectedValue ? "single" : "all"; |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 1008 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 1009 | // Force re-render |
| 1010 | this.requestUpdate(); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * Get display name for file in the selector |
| 1015 | */ |
| 1016 | getFileDisplayName(file: GitDiffFile): string { |
| 1017 | const status = this.getFileStatusText(file.status); |
| 1018 | const pathInfo = this.getPathInfo(file); |
| 1019 | return `${status}: ${pathInfo}`; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 1023 | * Render expand/collapse button for single file view in header |
| 1024 | */ |
| 1025 | renderSingleFileExpandButton() { |
| 1026 | if (!this.selectedFile) return ""; |
| 1027 | |
| 1028 | const isExpanded = this.fileExpandStates.get(this.selectedFile) ?? false; |
| 1029 | |
| 1030 | return html` |
| 1031 | <button |
| 1032 | class="header-expand-button" |
| 1033 | @click="${() => this.toggleFileExpansion(this.selectedFile)}" |
| 1034 | title="${isExpanded |
| 1035 | ? "Collapse: Hide unchanged regions to focus on changes" |
| 1036 | : "Expand: Show all lines including unchanged regions"}" |
| 1037 | > |
| 1038 | ${isExpanded ? this.renderCollapseIcon() : this.renderExpandAllIcon()} |
| 1039 | </button> |
| 1040 | `; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 1044 | * Render single file view with full-screen Monaco editor |
| 1045 | */ |
| 1046 | renderSingleFileView() { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 1047 | const selectedFileData = this.files.find( |
| 1048 | (f) => f.path === this.selectedFile, |
| 1049 | ); |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 1050 | if (!selectedFileData) { |
| 1051 | return html`<div class="error">Selected file not found</div>`; |
| 1052 | } |
| 1053 | |
| 1054 | const content = this.fileContents.get(this.selectedFile); |
| 1055 | if (!content) { |
| 1056 | return html`<div class="loading">Loading ${this.selectedFile}...</div>`; |
| 1057 | } |
| 1058 | |
| 1059 | return html` |
| 1060 | <div class="single-file-view"> |
| 1061 | <sketch-monaco-view |
| 1062 | class="single-file-monaco" |
| 1063 | .originalCode="${content.original}" |
| 1064 | .modifiedCode="${content.modified}" |
| 1065 | .originalFilename="${selectedFileData.path}" |
| 1066 | .modifiedFilename="${selectedFileData.path}" |
| 1067 | ?readOnly="${!content.editable}" |
| 1068 | ?editable-right="${content.editable}" |
| 1069 | @monaco-comment="${this.handleMonacoComment}" |
| 1070 | @monaco-save="${this.handleMonacoSave}" |
| 1071 | data-file-path="${selectedFileData.path}" |
| 1072 | ></sketch-monaco-view> |
| 1073 | </div> |
| 1074 | `; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1078 | * Refresh the diff view by reloading commits and diff data |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 1079 | * |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1080 | * This is called when the Monaco diff tab is activated to ensure: |
| 1081 | * 1. Branch information from git/recentlog is current (branches can change frequently) |
| 1082 | * 2. The diff content is synchronized with the latest repository state |
| 1083 | * 3. Users always see up-to-date information without manual refresh |
| 1084 | */ |
| 1085 | refreshDiffView() { |
| 1086 | // First refresh the range picker to get updated branch information |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 1087 | const rangePicker = this.shadowRoot?.querySelector( |
| 1088 | "sketch-diff-range-picker", |
| 1089 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1090 | if (rangePicker) { |
| 1091 | (rangePicker as any).loadCommits(); |
| 1092 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 1093 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1094 | if (this.commit) { |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 1095 | // Convert single commit to range (commit^ to commit) |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 1096 | this.currentRange = { |
| 1097 | type: "range", |
| 1098 | from: `${this.commit}^`, |
| 1099 | to: this.commit, |
| 1100 | }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1101 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 1102 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1103 | // Then reload diff data based on the current range |
| 1104 | this.loadDiffData(); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | declare global { |
| 1109 | interface HTMLElementTagNameMap { |
| 1110 | "sketch-diff2-view": SketchDiff2View; |
| 1111 | } |
| 1112 | } |