| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 2 | import { html } from "lit"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 3 | import { customElement, property, state } from "lit/decorators.js"; |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 4 | import { SketchTailwindElement } from "./sketch-tailwind-element.js"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 5 | import "./sketch-monaco-view"; |
| 6 | import "./sketch-diff-range-picker"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 7 | import "./sketch-diff-empty-view"; |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 8 | import { GitDiffFile, GitDataService } from "./git-data-service"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 9 | import { DiffRange } from "./sketch-diff-range-picker"; |
| 10 | |
| 11 | /** |
| 12 | * A component that displays diffs using Monaco editor with range and file pickers |
| 13 | */ |
| 14 | @customElement("sketch-diff2-view") |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 15 | export class SketchDiff2View extends SketchTailwindElement { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 16 | /** |
| 17 | * Handles comment events from the Monaco editor and forwards them to the chat input |
| 18 | * using the same event format as the original diff view for consistency. |
| 19 | */ |
| 20 | private handleMonacoComment(event: CustomEvent) { |
| 21 | try { |
| 22 | // Validate incoming data |
| 23 | if (!event.detail || !event.detail.formattedComment) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 24 | console.error("Invalid comment data received"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 25 | return; |
| 26 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 27 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 28 | // Create and dispatch event using the standardized format |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 29 | const commentEvent = new CustomEvent("diff-comment", { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 30 | detail: { comment: event.detail.formattedComment }, |
| 31 | bubbles: true, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 32 | composed: true, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 33 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 34 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 35 | this.dispatchEvent(commentEvent); |
| 36 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 37 | console.error("Error handling Monaco comment:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 40 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 41 | /** |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 42 | * Handle height change events from the Monaco editor |
| 43 | */ |
| 44 | private handleMonacoHeightChange(event: CustomEvent) { |
| 45 | try { |
| 46 | // Get the monaco view that emitted the event |
| 47 | const monacoView = event.target as HTMLElement; |
| 48 | if (!monacoView) return; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 49 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 50 | // Find the parent file-diff-editor container |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 51 | const fileDiffEditor = monacoView.closest( |
| 52 | ".file-diff-editor", |
| 53 | ) as HTMLElement; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 54 | if (!fileDiffEditor) return; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 55 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 56 | // Get the new height from the event |
| 57 | const newHeight = event.detail.height; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 58 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 59 | // Only update if the height actually changed to avoid unnecessary layout |
| 60 | const currentHeight = fileDiffEditor.style.height; |
| 61 | const newHeightStr = `${newHeight}px`; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 62 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 63 | if (currentHeight !== newHeightStr) { |
| 64 | // Update the file-diff-editor height to match monaco's height |
| 65 | fileDiffEditor.style.height = newHeightStr; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 66 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 67 | // Remove any previous min-height constraint that might interfere |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 68 | fileDiffEditor.style.minHeight = "auto"; |
| 69 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 70 | // IMPORTANT: Tell Monaco to relayout after its container size changed |
| 71 | // Monaco has automaticLayout: false, so it won't detect container changes |
| 72 | setTimeout(() => { |
| 73 | const monacoComponent = monacoView as any; |
| 74 | if (monacoComponent && monacoComponent.editor) { |
| 75 | // Force layout with explicit dimensions to ensure Monaco fills the space |
| 76 | const editorWidth = fileDiffEditor.offsetWidth; |
| 77 | monacoComponent.editor.layout({ |
| 78 | width: editorWidth, |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 79 | height: newHeight, |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 80 | }); |
| 81 | } |
| 82 | }, 0); |
| 83 | } |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 84 | } catch (error) { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 85 | console.error("Error handling Monaco height change:", error); |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 90 | * Handle save events from the Monaco editor |
| 91 | */ |
| 92 | private async handleMonacoSave(event: CustomEvent) { |
| 93 | try { |
| 94 | // Validate incoming data |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 95 | if ( |
| 96 | !event.detail || |
| 97 | !event.detail.path || |
| 98 | event.detail.content === undefined |
| 99 | ) { |
| 100 | console.error("Invalid save data received"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 101 | return; |
| 102 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 103 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 104 | const { path, content } = event.detail; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 105 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 106 | // Get Monaco view component |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 107 | const monacoView = this.shadowRoot?.querySelector("sketch-monaco-view"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 108 | if (!monacoView) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 109 | console.error("Monaco view not found"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 112 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 113 | try { |
| 114 | await this.gitService?.saveFileContent(path, content); |
| 115 | console.log(`File saved: ${path}`); |
| 116 | (monacoView as any).notifySaveComplete(true); |
| 117 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 118 | console.error( |
| 119 | `Error saving file: ${error instanceof Error ? error.message : String(error)}`, |
| 120 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 121 | (monacoView as any).notifySaveComplete(false); |
| 122 | } |
| 123 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 124 | console.error("Error handling save:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | @property({ type: String }) |
| 128 | initialCommit: string = ""; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 129 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 130 | // The commit to show - used when showing a specific commit from timeline |
| 131 | @property({ type: String }) |
| 132 | commit: string = ""; |
| 133 | |
| 134 | @property({ type: String }) |
| 135 | selectedFilePath: string = ""; |
| 136 | |
| 137 | @state() |
| 138 | private files: GitDiffFile[] = []; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 139 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 140 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 141 | private currentRange: DiffRange = { type: "range", from: "", to: "HEAD" }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 142 | |
| 143 | @state() |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 144 | private fileContents: Map< |
| 145 | string, |
| 146 | { original: string; modified: string; editable: boolean } |
| 147 | > = new Map(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 148 | |
| 149 | @state() |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 150 | private fileExpandStates: Map<string, boolean> = new Map(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 151 | |
| 152 | @state() |
| 153 | private loading: boolean = false; |
| 154 | |
| 155 | @state() |
| 156 | private error: string | null = null; |
| 157 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 158 | @state() |
| 159 | private selectedFile: string = ""; // Empty string means "All files" |
| 160 | |
| 161 | @state() |
| 162 | private viewMode: "all" | "single" = "all"; |
| 163 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 164 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 165 | |
| 166 | @property({ attribute: false, type: Object }) |
| 167 | gitService!: GitDataService; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 168 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 169 | // The gitService must be passed from parent to ensure proper dependency injection |
| 170 | |
| 171 | constructor() { |
| 172 | super(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 173 | console.log("SketchDiff2View initialized"); |
| 174 | |
| David Crawshaw | e2954ce | 2025-06-15 00:06:34 +0000 | [diff] [blame] | 175 | // Fix for monaco-aria-container positioning and hide scrollbars globally |
| 176 | // 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] | 177 | const styleElement = document.createElement("style"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 178 | styleElement.textContent = ` |
| 179 | .monaco-aria-container { |
| 180 | position: absolute !important; |
| 181 | top: 0 !important; |
| 182 | left: 0 !important; |
| 183 | width: 1px !important; |
| 184 | height: 1px !important; |
| 185 | overflow: hidden !important; |
| 186 | clip: rect(1px, 1px, 1px, 1px) !important; |
| 187 | white-space: nowrap !important; |
| 188 | margin: 0 !important; |
| 189 | padding: 0 !important; |
| 190 | border: 0 !important; |
| 191 | z-index: -1 !important; |
| 192 | } |
| David Crawshaw | e2954ce | 2025-06-15 00:06:34 +0000 | [diff] [blame] | 193 | |
| 194 | /* Aggressively hide all Monaco scrollbar elements */ |
| 195 | .monaco-editor .scrollbar, |
| 196 | .monaco-editor .scroll-decoration, |
| 197 | .monaco-editor .invisible.scrollbar, |
| 198 | .monaco-editor .slider, |
| 199 | .monaco-editor .vertical.scrollbar, |
| 200 | .monaco-editor .horizontal.scrollbar, |
| 201 | .monaco-diff-editor .scrollbar, |
| 202 | .monaco-diff-editor .scroll-decoration, |
| 203 | .monaco-diff-editor .invisible.scrollbar, |
| 204 | .monaco-diff-editor .slider, |
| 205 | .monaco-diff-editor .vertical.scrollbar, |
| 206 | .monaco-diff-editor .horizontal.scrollbar { |
| 207 | display: none !important; |
| 208 | visibility: hidden !important; |
| 209 | width: 0 !important; |
| 210 | height: 0 !important; |
| 211 | opacity: 0 !important; |
| 212 | } |
| 213 | |
| 214 | /* Target the specific scrollbar classes that Monaco uses */ |
| 215 | .monaco-scrollable-element > .scrollbar, |
| 216 | .monaco-scrollable-element > .scroll-decoration, |
| 217 | .monaco-scrollable-element .slider { |
| 218 | display: none !important; |
| 219 | visibility: hidden !important; |
| 220 | width: 0 !important; |
| 221 | height: 0 !important; |
| 222 | } |
| 223 | |
| 224 | /* Remove scrollbar space/padding from content area */ |
| 225 | .monaco-editor .monaco-scrollable-element, |
| 226 | .monaco-diff-editor .monaco-scrollable-element { |
| 227 | padding-right: 0 !important; |
| 228 | padding-bottom: 0 !important; |
| 229 | margin-right: 0 !important; |
| 230 | margin-bottom: 0 !important; |
| 231 | } |
| 232 | |
| 233 | /* Ensure the diff content takes full width without scrollbar space */ |
| 234 | .monaco-diff-editor .editor.modified, |
| 235 | .monaco-diff-editor .editor.original { |
| 236 | margin-right: 0 !important; |
| 237 | padding-right: 0 !important; |
| 238 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 239 | `; |
| 240 | document.head.appendChild(styleElement); |
| 241 | } |
| 242 | |
| 243 | connectedCallback() { |
| 244 | super.connectedCallback(); |
| 245 | // Initialize with default range and load data |
| 246 | // Get base commit if not set |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 247 | if ( |
| 248 | this.currentRange.type === "range" && |
| 249 | !("from" in this.currentRange && this.currentRange.from) |
| 250 | ) { |
| 251 | this.gitService |
| 252 | .getBaseCommitRef() |
| 253 | .then((baseRef) => { |
| 254 | this.currentRange = { type: "range", from: baseRef, to: "HEAD" }; |
| 255 | this.loadDiffData(); |
| 256 | }) |
| 257 | .catch((error) => { |
| 258 | console.error("Error getting base commit ref:", error); |
| 259 | // Use default range |
| 260 | this.loadDiffData(); |
| 261 | }); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 262 | } else { |
| 263 | this.loadDiffData(); |
| 264 | } |
| 265 | } |
| 266 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 267 | // Toggle hideUnchangedRegions setting for a specific file |
| 268 | private toggleFileExpansion(filePath: string) { |
| 269 | const currentState = this.fileExpandStates.get(filePath) ?? false; |
| 270 | const newState = !currentState; |
| 271 | this.fileExpandStates.set(filePath, newState); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 272 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 273 | // Apply to the specific Monaco view component for this file |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 274 | const monacoView = this.shadowRoot?.querySelector( |
| 275 | `sketch-monaco-view[data-file-path="${filePath}"]`, |
| 276 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 277 | if (monacoView) { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 278 | (monacoView as any).toggleHideUnchangedRegions(!newState); // inverted because true means "hide unchanged" |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 279 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 280 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 281 | // Force a re-render to update the button state |
| 282 | this.requestUpdate(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 283 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 284 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 285 | render() { |
| 286 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 287 | <div class="flex h-full flex-1 flex-col min-h-0 overflow-hidden relative"> |
| 288 | <div class="px-4 py-2 border-b border-gray-300 bg-gray-100 flex-shrink-0"> |
| 289 | <div class="flex flex-col gap-3"> |
| 290 | <div class="w-full flex items-center gap-3"> |
| 291 | <sketch-diff-range-picker |
| 292 | class="flex-1 min-w-[400px]" |
| 293 | .gitService="${this.gitService}" |
| 294 | @range-change="${this.handleRangeChange}" |
| 295 | ></sketch-diff-range-picker> |
| 296 | <div class="flex-1"></div> |
| 297 | ${this.renderFileSelector()} |
| 298 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 299 | </div> |
| 300 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 301 | |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 302 | <div class="flex-1 overflow-auto flex flex-col min-h-0 relative h-full"> |
| 303 | <div class="flex-1 overflow-auto min-h-0 flex flex-col relative h-full">${this.renderDiffContent()}</div> |
| 304 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 305 | </div> |
| 306 | `; |
| 307 | } |
| 308 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 309 | renderFileSelector() { |
| David Crawshaw | 5c6d829 | 2025-06-15 19:09:19 +0000 | [diff] [blame] | 310 | const fileCount = this.files.length; |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 311 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 312 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 313 | <div class="flex items-center gap-2"> |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 314 | <select |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 315 | class="min-w-[200px] px-3 py-2 border border-gray-400 rounded bg-white text-sm cursor-pointer focus:outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-200 disabled:bg-gray-100 disabled:text-gray-500 disabled:cursor-not-allowed" |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 316 | .value="${this.selectedFile}" |
| 317 | @change="${this.handleFileSelection}" |
| 318 | ?disabled="${fileCount === 0}" |
| 319 | > |
| 320 | <option value="">All files (${fileCount})</option> |
| 321 | ${this.files.map( |
| 322 | (file) => html` |
| 323 | <option value="${file.path}"> |
| 324 | ${this.getFileDisplayName(file)} |
| 325 | </option> |
| 326 | `, |
| 327 | )} |
| 328 | </select> |
| 329 | ${this.selectedFile ? this.renderSingleFileExpandButton() : ""} |
| 330 | </div> |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 331 | `; |
| 332 | } |
| 333 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 334 | renderDiffContent() { |
| 335 | if (this.loading) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 336 | return html`<div class="flex items-center justify-center h-full">Loading diff...</div>`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (this.error) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 340 | return html`<div class="text-red-600 p-4">${this.error}</div>`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (this.files.length === 0) { |
| 344 | return html`<sketch-diff-empty-view></sketch-diff-empty-view>`; |
| 345 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 346 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 347 | // Render single file view if a specific file is selected |
| 348 | if (this.selectedFile && this.viewMode === "single") { |
| 349 | return this.renderSingleFileView(); |
| 350 | } |
| 351 | |
| 352 | // Render multi-file view |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 353 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 354 | <div class="flex flex-col w-full min-h-full"> |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 355 | ${this.files.map((file, index) => this.renderFileDiff(file, index))} |
| 356 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 357 | `; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Load diff data for the current range |
| 362 | */ |
| 363 | async loadDiffData() { |
| 364 | this.loading = true; |
| 365 | this.error = null; |
| 366 | |
| 367 | try { |
| 368 | // Initialize files as empty array if undefined |
| 369 | if (!this.files) { |
| 370 | this.files = []; |
| 371 | } |
| 372 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 373 | // Load diff data for the range |
| 374 | this.files = await this.gitService.getDiff( |
| 375 | this.currentRange.from, |
| 376 | this.currentRange.to, |
| 377 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 378 | |
| 379 | // Ensure files is always an array, even when API returns null |
| 380 | if (!this.files) { |
| 381 | this.files = []; |
| 382 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 383 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 384 | // Load content for all files |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 385 | if (this.files.length > 0) { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 386 | // Initialize expand states for new files (default to collapsed) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 387 | this.files.forEach((file) => { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 388 | if (!this.fileExpandStates.has(file.path)) { |
| 389 | this.fileExpandStates.set(file.path, false); // false = collapsed (hide unchanged regions) |
| 390 | } |
| 391 | }); |
| 392 | await this.loadAllFileContents(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 393 | } else { |
| 394 | // No files to display - reset the view to initial state |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 395 | this.selectedFilePath = ""; |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 396 | this.selectedFile = ""; |
| 397 | this.viewMode = "all"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 398 | this.fileContents.clear(); |
| 399 | this.fileExpandStates.clear(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 400 | } |
| 401 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 402 | console.error("Error loading diff data:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 403 | this.error = `Error loading diff data: ${error.message}`; |
| 404 | // Ensure files is an empty array when an error occurs |
| 405 | this.files = []; |
| 406 | // Reset the view to initial state |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 407 | this.selectedFilePath = ""; |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 408 | this.selectedFile = ""; |
| 409 | this.viewMode = "all"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 410 | this.fileContents.clear(); |
| 411 | this.fileExpandStates.clear(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 412 | } finally { |
| 413 | this.loading = false; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 418 | * Load content for all files in the diff |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 419 | */ |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 420 | async loadAllFileContents() { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 421 | this.loading = true; |
| 422 | this.error = null; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 423 | this.fileContents.clear(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 424 | |
| 425 | try { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 426 | let isUnstagedChanges = false; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 427 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 428 | // Determine the commits to compare based on the current range |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 429 | const _fromCommit = this.currentRange.from; |
| 430 | const toCommit = this.currentRange.to; |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 431 | // Check if this is an unstaged changes view |
| 432 | isUnstagedChanges = toCommit === ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 433 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 434 | // Load content for all files |
| 435 | const promises = this.files.map(async (file) => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 436 | try { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 437 | let originalCode = ""; |
| 438 | let modifiedCode = ""; |
| 439 | let editable = isUnstagedChanges; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 440 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 441 | // Load the original code based on file status |
| 442 | if (file.status !== "A") { |
| 443 | // For modified, renamed, or deleted files: load original content |
| 444 | originalCode = await this.gitService.getFileContent( |
| 445 | file.old_hash || "", |
| 446 | ); |
| 447 | } |
| 448 | |
| 449 | // For modified code, always use working copy when editable |
| 450 | if (editable) { |
| 451 | try { |
| 452 | // Always use working copy when editable, regardless of diff status |
| 453 | modifiedCode = await this.gitService.getWorkingCopyContent( |
| 454 | file.path, |
| 455 | ); |
| 456 | } catch (error) { |
| 457 | if (file.status === "D") { |
| 458 | // For deleted files, silently use empty content |
| 459 | console.warn( |
| 460 | `Could not get working copy for deleted file ${file.path}, using empty content`, |
| 461 | ); |
| 462 | modifiedCode = ""; |
| 463 | } else { |
| 464 | // For any other file status, propagate the error |
| 465 | console.error( |
| 466 | `Failed to get working copy for ${file.path}:`, |
| 467 | error, |
| 468 | ); |
| 469 | throw error; |
| 470 | } |
| 471 | } |
| 472 | } else { |
| 473 | // For non-editable view, use git content based on file status |
| 474 | if (file.status === "D") { |
| 475 | // Deleted file: empty modified |
| 476 | modifiedCode = ""; |
| 477 | } else { |
| 478 | // Added/modified/renamed: use the content from git |
| 479 | modifiedCode = await this.gitService.getFileContent( |
| 480 | file.new_hash || "", |
| 481 | ); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Don't make deleted files editable |
| 486 | if (file.status === "D") { |
| 487 | editable = false; |
| 488 | } |
| 489 | |
| 490 | this.fileContents.set(file.path, { |
| 491 | original: originalCode, |
| 492 | modified: modifiedCode, |
| 493 | editable, |
| 494 | }); |
| 495 | } catch (error) { |
| 496 | console.error(`Error loading content for file ${file.path}:`, error); |
| 497 | // Store empty content for failed files to prevent blocking |
| 498 | this.fileContents.set(file.path, { |
| 499 | original: "", |
| 500 | modified: "", |
| 501 | editable: false, |
| 502 | }); |
| 503 | } |
| 504 | }); |
| 505 | |
| 506 | await Promise.all(promises); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 507 | } catch (error) { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 508 | console.error("Error loading file contents:", error); |
| 509 | this.error = `Error loading file contents: ${error.message}`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 510 | } finally { |
| 511 | this.loading = false; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Handle range change event from the range picker |
| 517 | */ |
| 518 | handleRangeChange(event: CustomEvent) { |
| 519 | const { range } = event.detail; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 520 | console.log("Range changed:", range); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 521 | this.currentRange = range; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 522 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 523 | // Load diff data for the new range |
| 524 | this.loadDiffData(); |
| 525 | } |
| 526 | |
| 527 | /** |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 528 | * Render a single file diff section |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 529 | */ |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 530 | renderFileDiff(file: GitDiffFile, index: number) { |
| 531 | const content = this.fileContents.get(file.path); |
| 532 | if (!content) { |
| 533 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 534 | <div class="flex flex-col border-b-4 border-gray-300 mb-0 last:border-b-0"> |
| 535 | <div class="bg-gray-100 border-b border-gray-300 px-4 py-2 font-medium text-sm text-gray-800 sticky top-0 z-10 shadow-sm flex justify-between items-center">${this.renderFileHeader(file)}</div> |
| 536 | <div class="flex items-center justify-center h-full">Loading ${file.path}...</div> |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 537 | </div> |
| 538 | `; |
| 539 | } |
| 540 | |
| 541 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 542 | <div class="flex flex-col border-b-4 border-gray-300 mb-0 last:border-b-0"> |
| 543 | <div class="bg-gray-100 border-b border-gray-300 px-4 py-2 font-medium text-sm text-gray-800 sticky top-0 z-10 shadow-sm flex justify-between items-center">${this.renderFileHeader(file)}</div> |
| 544 | <div class="flex flex-col min-h-[200px] overflow-visible"> |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 545 | <sketch-monaco-view |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 546 | class="flex flex-col w-full min-h-[200px] flex-1" |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 547 | .originalCode="${content.original}" |
| 548 | .modifiedCode="${content.modified}" |
| 549 | .originalFilename="${file.path}" |
| 550 | .modifiedFilename="${file.path}" |
| 551 | ?readOnly="${!content.editable}" |
| 552 | ?editable-right="${content.editable}" |
| 553 | @monaco-comment="${this.handleMonacoComment}" |
| 554 | @monaco-save="${this.handleMonacoSave}" |
| 555 | @monaco-height-changed="${this.handleMonacoHeightChange}" |
| 556 | data-file-index="${index}" |
| 557 | data-file-path="${file.path}" |
| 558 | ></sketch-monaco-view> |
| 559 | </div> |
| 560 | </div> |
| 561 | `; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Render file header with status and path info |
| 566 | */ |
| 567 | renderFileHeader(file: GitDiffFile) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 568 | const statusClasses = this.getFileStatusTailwindClasses(file.status); |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 569 | const statusText = this.getFileStatusText(file.status); |
| 570 | const changesInfo = this.getChangesInfo(file); |
| 571 | const pathInfo = this.getPathInfo(file); |
| 572 | |
| 573 | const isExpanded = this.fileExpandStates.get(file.path) ?? false; |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 574 | |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 575 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 576 | <div class="flex items-center gap-2"> |
| 577 | <span class="inline-block px-1.5 py-0.5 rounded text-xs font-bold mr-2 ${statusClasses}">${statusText}</span> |
| 578 | <span class="font-mono font-normal text-gray-600">${pathInfo}</span> |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 579 | ${changesInfo |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 580 | ? html`<span class="ml-2 text-xs text-gray-600">${changesInfo}</span>` |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 581 | : ""} |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 582 | </div> |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 583 | <div class="flex items-center"> |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 584 | <button |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 585 | class="bg-transparent border border-gray-300 rounded px-2 py-1 text-sm cursor-pointer whitespace-nowrap transition-colors duration-200 flex items-center justify-center min-w-8 min-h-8 hover:bg-gray-200" |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 586 | @click="${() => this.toggleFileExpansion(file.path)}" |
| 587 | title="${isExpanded |
| 588 | ? "Collapse: Hide unchanged regions to focus on changes" |
| 589 | : "Expand: Show all lines including unchanged regions"}" |
| 590 | > |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 591 | ${isExpanded ? this.renderCollapseIcon() : this.renderExpandAllIcon()} |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 592 | </button> |
| 593 | </div> |
| 594 | `; |
| 595 | } |
| 596 | |
| 597 | /** |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 598 | * Get Tailwind CSS classes for file status |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 599 | */ |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 600 | getFileStatusTailwindClasses(status: string): string { |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 601 | switch (status.toUpperCase()) { |
| 602 | case "A": |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 603 | return "bg-green-100 text-green-800"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 604 | case "M": |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 605 | return "bg-yellow-100 text-yellow-800"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 606 | case "D": |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 607 | return "bg-red-100 text-red-800"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 608 | case "R": |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 609 | case "C": |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 610 | default: |
| 611 | if (status.toUpperCase().startsWith("R")) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 612 | return "bg-cyan-100 text-cyan-800"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 613 | } |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 614 | if (status.toUpperCase().startsWith("C")) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 615 | return "bg-indigo-100 text-indigo-800"; |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 616 | } |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 617 | return "bg-yellow-100 text-yellow-800"; |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Get display text for file status |
| 623 | */ |
| 624 | getFileStatusText(status: string): string { |
| 625 | switch (status.toUpperCase()) { |
| 626 | case "A": |
| 627 | return "Added"; |
| 628 | case "M": |
| 629 | return "Modified"; |
| 630 | case "D": |
| 631 | return "Deleted"; |
| 632 | case "R": |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 633 | case "C": |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 634 | default: |
| 635 | if (status.toUpperCase().startsWith("R")) { |
| 636 | return "Renamed"; |
| 637 | } |
| Josh Bleecher Snyder | b3aff88 | 2025-07-01 02:17:27 +0000 | [diff] [blame] | 638 | if (status.toUpperCase().startsWith("C")) { |
| 639 | return "Copied"; |
| 640 | } |
| David Crawshaw | 26f3f34 | 2025-06-14 19:58:32 +0000 | [diff] [blame] | 641 | return "Modified"; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Get changes information (+/-) for display |
| 647 | */ |
| 648 | getChangesInfo(file: GitDiffFile): string { |
| 649 | const additions = file.additions || 0; |
| 650 | const deletions = file.deletions || 0; |
| 651 | |
| 652 | if (additions === 0 && deletions === 0) { |
| 653 | return ""; |
| 654 | } |
| 655 | |
| 656 | const parts = []; |
| 657 | if (additions > 0) { |
| 658 | parts.push(`+${additions}`); |
| 659 | } |
| 660 | if (deletions > 0) { |
| 661 | parts.push(`-${deletions}`); |
| 662 | } |
| 663 | |
| 664 | return `(${parts.join(", ")})`; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Get path information for display, handling renames |
| 669 | */ |
| 670 | getPathInfo(file: GitDiffFile): string { |
| 671 | if (file.old_path && file.old_path !== "") { |
| 672 | // For renames, show old_path → new_path |
| 673 | return `${file.old_path} → ${file.path}`; |
| 674 | } |
| 675 | // For regular files, just show the path |
| 676 | return file.path; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 680 | * Render expand all icon (dotted line with arrows pointing away) |
| 681 | */ |
| 682 | renderExpandAllIcon() { |
| 683 | return html` |
| 684 | <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"> |
| 685 | <!-- Dotted line in the middle --> |
| 686 | <line |
| 687 | x1="2" |
| 688 | y1="8" |
| 689 | x2="14" |
| 690 | y2="8" |
| 691 | stroke="currentColor" |
| 692 | stroke-width="1" |
| 693 | stroke-dasharray="2,1" |
| 694 | /> |
| 695 | <!-- Large arrow pointing up --> |
| 696 | <path d="M8 2 L5 6 L11 6 Z" fill="currentColor" /> |
| 697 | <!-- Large arrow pointing down --> |
| 698 | <path d="M8 14 L5 10 L11 10 Z" fill="currentColor" /> |
| 699 | </svg> |
| 700 | `; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Render collapse icon (arrows pointing towards dotted line) |
| 705 | */ |
| 706 | renderCollapseIcon() { |
| 707 | return html` |
| 708 | <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"> |
| 709 | <!-- Dotted line in the middle --> |
| 710 | <line |
| 711 | x1="2" |
| 712 | y1="8" |
| 713 | x2="14" |
| 714 | y2="8" |
| 715 | stroke="currentColor" |
| 716 | stroke-width="1" |
| 717 | stroke-dasharray="2,1" |
| 718 | /> |
| 719 | <!-- Large arrow pointing down towards line --> |
| 720 | <path d="M8 6 L5 2 L11 2 Z" fill="currentColor" /> |
| 721 | <!-- Large arrow pointing up towards line --> |
| 722 | <path d="M8 10 L5 14 L11 14 Z" fill="currentColor" /> |
| 723 | </svg> |
| 724 | `; |
| 725 | } |
| 726 | |
| 727 | /** |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 728 | * Handle file selection change from the dropdown |
| 729 | */ |
| 730 | handleFileSelection(event: Event) { |
| 731 | const selectElement = event.target as HTMLSelectElement; |
| 732 | const selectedValue = selectElement.value; |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 733 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 734 | this.selectedFile = selectedValue; |
| 735 | this.viewMode = selectedValue ? "single" : "all"; |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 736 | |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 737 | // Force re-render |
| 738 | this.requestUpdate(); |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Get display name for file in the selector |
| 743 | */ |
| 744 | getFileDisplayName(file: GitDiffFile): string { |
| 745 | const status = this.getFileStatusText(file.status); |
| 746 | const pathInfo = this.getPathInfo(file); |
| 747 | return `${status}: ${pathInfo}`; |
| 748 | } |
| 749 | |
| 750 | /** |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 751 | * Render expand/collapse button for single file view in header |
| 752 | */ |
| 753 | renderSingleFileExpandButton() { |
| 754 | if (!this.selectedFile) return ""; |
| 755 | |
| 756 | const isExpanded = this.fileExpandStates.get(this.selectedFile) ?? false; |
| 757 | |
| 758 | return html` |
| 759 | <button |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 760 | class="bg-transparent border border-gray-300 rounded px-1.5 py-1.5 text-sm cursor-pointer whitespace-nowrap transition-colors duration-200 flex items-center justify-center min-w-8 min-h-8 hover:bg-gray-200" |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 761 | @click="${() => this.toggleFileExpansion(this.selectedFile)}" |
| 762 | title="${isExpanded |
| 763 | ? "Collapse: Hide unchanged regions to focus on changes" |
| 764 | : "Expand: Show all lines including unchanged regions"}" |
| 765 | > |
| 766 | ${isExpanded ? this.renderCollapseIcon() : this.renderExpandAllIcon()} |
| 767 | </button> |
| 768 | `; |
| 769 | } |
| 770 | |
| 771 | /** |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 772 | * Render single file view with full-screen Monaco editor |
| 773 | */ |
| 774 | renderSingleFileView() { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 775 | const selectedFileData = this.files.find( |
| 776 | (f) => f.path === this.selectedFile, |
| 777 | ); |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 778 | if (!selectedFileData) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 779 | return html`<div class="text-red-600 p-4">Selected file not found</div>`; |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | const content = this.fileContents.get(this.selectedFile); |
| 783 | if (!content) { |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 784 | return html`<div class="flex items-center justify-center h-full">Loading ${this.selectedFile}...</div>`; |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | return html` |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 788 | <div class="flex-1 flex flex-col h-full min-h-0"> |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 789 | <sketch-monaco-view |
| banksean | 5450584 | 2025-07-03 00:18:44 +0000 | [diff] [blame^] | 790 | class="flex-1 w-full h-full min-h-0" |
| David Crawshaw | 4cd0129 | 2025-06-15 18:59:13 +0000 | [diff] [blame] | 791 | .originalCode="${content.original}" |
| 792 | .modifiedCode="${content.modified}" |
| 793 | .originalFilename="${selectedFileData.path}" |
| 794 | .modifiedFilename="${selectedFileData.path}" |
| 795 | ?readOnly="${!content.editable}" |
| 796 | ?editable-right="${content.editable}" |
| 797 | @monaco-comment="${this.handleMonacoComment}" |
| 798 | @monaco-save="${this.handleMonacoSave}" |
| 799 | data-file-path="${selectedFileData.path}" |
| 800 | ></sketch-monaco-view> |
| 801 | </div> |
| 802 | `; |
| 803 | } |
| 804 | |
| 805 | /** |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 806 | * Refresh the diff view by reloading commits and diff data |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 807 | * |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 808 | * This is called when the Monaco diff tab is activated to ensure: |
| 809 | * 1. Branch information from git/recentlog is current (branches can change frequently) |
| 810 | * 2. The diff content is synchronized with the latest repository state |
| 811 | * 3. Users always see up-to-date information without manual refresh |
| 812 | */ |
| 813 | refreshDiffView() { |
| 814 | // First refresh the range picker to get updated branch information |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 815 | const rangePicker = this.shadowRoot?.querySelector( |
| 816 | "sketch-diff-range-picker", |
| 817 | ); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 818 | if (rangePicker) { |
| 819 | (rangePicker as any).loadCommits(); |
| 820 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 821 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 822 | if (this.commit) { |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 823 | // Convert single commit to range (commit^ to commit) |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 824 | this.currentRange = { |
| 825 | type: "range", |
| 826 | from: `${this.commit}^`, |
| 827 | to: this.commit, |
| 828 | }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 829 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 830 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 831 | // Then reload diff data based on the current range |
| 832 | this.loadDiffData(); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | declare global { |
| 837 | interface HTMLElementTagNameMap { |
| 838 | "sketch-diff2-view": SketchDiff2View; |
| 839 | } |
| 840 | } |