-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathui-controller.ts
More file actions
162 lines (143 loc) · 4.28 KB
/
ui-controller.ts
File metadata and controls
162 lines (143 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ui-controller.ts - UI event handlers and state management
import { DiffViewMessageHandler } from '../../shared/webkit';
/**
* UI state and file metadata
*/
let filePath: string | null = null;
let fileEditStatus: string | null = null;
/**
* Interface for messages sent to Swift handlers
*/
interface SwiftMessage {
event: string;
data: {
filePath: string | null;
[key: string]: any;
};
}
/**
* Initialize and set up UI elements and their event handlers
* @param {string} initialPath - The initial file path
* @param {string} initialStatus - The initial file edit status
*/
function setupUI(initialPath: string | null = null, initialStatus: string | null = null): void {
filePath = initialPath;
fileEditStatus = initialStatus;
if (filePath) {
showFilePath(filePath);
}
const keepButton = document.getElementById('keep-button');
const undoButton = document.getElementById('undo-button');
const choiceButtons = document.getElementById('choice-buttons');
if (!keepButton || !undoButton || !choiceButtons) {
console.error("Could not find UI elements");
return;
}
// Set initial UI state
updateUIStatus(initialStatus);
// Setup event listeners
keepButton.addEventListener('click', handleKeepButtonClick);
undoButton.addEventListener('click', handleUndoButtonClick);
}
/**
* Update the UI based on file edit status
* @param {string} status - The current file edit status
*/
function updateUIStatus(status: string | null): void {
fileEditStatus = status;
const choiceButtons = document.getElementById('choice-buttons');
if (!choiceButtons) return;
// Hide buttons if file has been modified
if (status && status !== "none") {
choiceButtons.classList.add('hidden');
} else {
choiceButtons.classList.remove('hidden');
}
}
/**
* Update the file metadata
* @param {string} path - The file path
* @param {string} status - The file edit status
*/
function updateFileMetadata(path: string | null, status: string | null): void {
filePath = path;
updateUIStatus(status);
if (filePath) {
showFilePath(filePath)
}
}
/**
* Handle the "Keep" button click
*/
function handleKeepButtonClick(): void {
// Send message to Swift handler
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.swiftHandler) {
const message: SwiftMessage = {
event: 'keepButtonClicked',
data: {
filePath: filePath
}
};
window.webkit.messageHandlers.swiftHandler.postMessage(message);
} else {
console.log('Keep button clicked, but no message handler found');
}
// Hide the choice buttons
const choiceButtons = document.getElementById('choice-buttons');
if (choiceButtons) {
choiceButtons.classList.add('hidden');
}
}
/**
* Handle the "Undo" button click
*/
function handleUndoButtonClick(): void {
// Send message to Swift handler
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.swiftHandler) {
const message: SwiftMessage = {
event: 'undoButtonClicked',
data: {
filePath: filePath
}
};
window.webkit.messageHandlers.swiftHandler.postMessage(message);
} else {
console.log('Undo button clicked, but no message handler found');
}
// Hide the choice buttons
const choiceButtons = document.getElementById('choice-buttons');
if (choiceButtons) {
choiceButtons.classList.add('hidden');
}
}
/**
* Get the current file path
* @returns {string} The current file path
*/
function getFilePath(): string | null {
return filePath;
}
/**
* Show the current file path
*/
function showFilePath(path: string): void {
const filePathElement = document.getElementById('file-path');
const fileName = path.split('/').pop() ?? '';
if (filePathElement) {
filePathElement.textContent = fileName
}
}
/**
* Get the current file edit status
* @returns {string} The current file edit status
*/
function getFileEditStatus(): string | null {
return fileEditStatus;
}
export {
setupUI,
updateUIStatus,
updateFileMetadata,
getFilePath,
getFileEditStatus
};