Skip to content

Commit f3ec9bf

Browse files
committed
Correct coutning for button hotkeys in popup
1 parent 2799ad0 commit f3ec9bf

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

modules/popup-page-modules-promptShare.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,21 @@ document.addEventListener('DOMContentLoaded', () => {
141141
}
142142

143143
// --- Event Listeners ---
144-
enableToggle.addEventListener('change', () => {
144+
enableToggle.addEventListener('change', async () => {
145145
currentSettings.enabled = enableToggle.checked;
146146
// The toggle now only changes the setting and saves it; it does not control UI visibility.
147-
saveModuleSettings();
147+
await saveModuleSettings();
148+
149+
// Trigger button cards re-render when enable/disable is toggled
150+
// This is needed because hotkey numbering depends on whether cross-chat is enabled
151+
if (typeof updatebuttonCardsList === 'function') {
152+
try {
153+
await updatebuttonCardsList();
154+
console.log('Button cards updated after toggling cross-chat module');
155+
} catch (error) {
156+
console.error('Error updating button cards after toggling cross-chat module:', error);
157+
}
158+
}
148159
});
149160

150161
autosendCopyToggle.addEventListener('change', () => {
@@ -158,10 +169,20 @@ document.addEventListener('DOMContentLoaded', () => {
158169
});
159170

160171
for (const radio of placementRadios) {
161-
radio.addEventListener('change', () => {
172+
radio.addEventListener('change', async () => {
162173
if (radio.checked) {
163174
currentSettings.placement = radio.value;
164-
saveModuleSettings();
175+
await saveModuleSettings();
176+
177+
// Trigger button cards re-render to update hotkey numbering
178+
if (typeof updatebuttonCardsList === 'function') {
179+
try {
180+
await updatebuttonCardsList();
181+
console.log('Button cards updated with new hotkey numbering');
182+
} catch (error) {
183+
console.error('Error updating button cards after placement change:', error);
184+
}
185+
}
165186
}
166187
});
167188
}

popup-page-scripts/popup-page-backup-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async function saveAndSwitchToImportedProfile(parsedProfile, isOverwrite) {
131131
profileSelect.value = parsedProfile.PROFILE_NAME;
132132
await switchProfile(parsedProfile.PROFILE_NAME);
133133

134-
updateInterface();
134+
await updateInterface(); // Now awaiting the async function
135135
logToGUIConsole(`Profile "${parsedProfile.PROFILE_NAME}" ${actionText} successfully.`);
136136
showToast(`Profile "${parsedProfile.PROFILE_NAME}" has been ${actionText} successfully.`, 'success');
137137
} catch (error) {

popup-page-scripts/popup-page-customButtons.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@
55
// as cards in <div id="buttonCardsList" ...> </div>
66
// This file creates elements that represent custom buttons (card like elements)
77
// Button cards contain: emoji input, text input, auto-send toggle, delete button, that are
8-
// used to create custom buttons for the extension.
8+
// used to create custom buttons for the extension.
99
// and separators for mostly visual funciton (separatprs behave like button cards with less stuff)
10-
// separator cards contain: visuals, delete button.
11-
// version: 1.0
10+
// separator cards contain: visuals, delete button.
11+
// version: 1.1
1212

1313
// -------------------------
1414
// Special constants
1515
// -------------------------
1616
const SETTINGS_BUTTON_MAGIC_TEXT = '%OCP_APP_SETTINGS_SYSTEM_BUTTON%';
1717

18+
/**
19+
* Gets the Cross-Chat module settings.
20+
* @returns {Promise<Object>} - The Cross-Chat module settings.
21+
*/
22+
async function getCrossChatSettings() {
23+
try {
24+
const response = await chrome.runtime.sendMessage({ type: 'getCrossChatModuleSettings' });
25+
return response && response.settings ? response.settings : { enabled: false, placement: 'after' };
26+
} catch (error) {
27+
console.error('Error fetching Cross-Chat settings:', error);
28+
return { enabled: false, placement: 'after' }; // Default fallback
29+
}
30+
}
31+
1832
// -------------------------
1933
// Create Button Element - this is start of everything, there can be zero to infinite buttons
2034
// -------------------------
@@ -25,7 +39,7 @@ const SETTINGS_BUTTON_MAGIC_TEXT = '%OCP_APP_SETTINGS_SYSTEM_BUTTON%';
2539
* @param {number} index - The index of the button in the customButtons array.
2640
* @returns {HTMLElement} - The button item element.
2741
*/
28-
function createButtonCardElement(button, index) {
42+
function createButtonCardElement(button, index, crossChatSettings = null) {
2943
const buttonItem = document.createElement('div');
3044
buttonItem.className = 'button-item';
3145
buttonItem.dataset.index = index;
@@ -50,9 +64,27 @@ function createButtonCardElement(button, index) {
5064
? `<div class="autosend-line"><label class="checkbox-row"><input type="checkbox" class="autosend-toggle" ${button.autoSend ? 'checked' : ''}><span>Auto-send</span></label></div>`
5165
: '';
5266

53-
const hotkeyHintHTML = index < 10
54-
? `<div class="shortcut-line"><span class="shortcut-indicator">[Alt+${index === 9 ? 0 : index + 1}]</span></div>`
55-
: '';
67+
// Calculate hotkey with consideration for CrossChat buttons and separators
68+
let hotkeyHintHTML = '';
69+
if (!button.separator) {
70+
// Calculate how many non-separator buttons are before this one
71+
let nonSeparatorButtonsCount = 0;
72+
for (let i = 0; i < index; i++) {
73+
if (!currentProfile.customButtons[i].separator) {
74+
nonSeparatorButtonsCount++;
75+
}
76+
}
77+
78+
// Apply the shift if CrossChat buttons are placed before
79+
const shift = (crossChatSettings && crossChatSettings.enabled && crossChatSettings.placement === 'before') ? 2 : 0;
80+
const hotkeyIndex = nonSeparatorButtonsCount + shift;
81+
82+
// Only show hotkey if it's within the 1-0 range (Alt+1 to Alt+0)
83+
if (hotkeyIndex < 10) {
84+
const displayKey = hotkeyIndex === 9 ? 0 : hotkeyIndex + 1;
85+
hotkeyHintHTML = `<div class="shortcut-line"><span class="shortcut-indicator">[Alt+${displayKey}]</span></div>`;
86+
}
87+
}
5688

5789
buttonItem.innerHTML = `
5890
<div class="drag-handle">&#9776;</div>
@@ -78,11 +110,14 @@ function createButtonCardElement(button, index) {
78110
/**
79111
* Updates the list of custom button cards in the buttonCardsList.
80112
*/
81-
function updatebuttonCardsList() {
113+
async function updatebuttonCardsList() {
114+
// Get Cross-Chat module settings
115+
const crossChatSettings = await getCrossChatSettings();
116+
82117
buttonCardsList.innerHTML = ''; // This already removes old listeners
83118
if (currentProfile.customButtons && currentProfile.customButtons.length > 0) {
84119
currentProfile.customButtons.forEach((button, index) => {
85-
const buttonElement = createButtonCardElement(button, index);
120+
const buttonElement = createButtonCardElement(button, index, crossChatSettings);
86121
buttonCardsList.appendChild(buttonElement);
87122
});
88123
} else {

popup-page-scripts/popup-page-script.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async function revertToDefault() {
9494
const response = await chrome.runtime.sendMessage({ type: 'createDefaultProfile' });
9595
currentProfile = response.config;
9696
await saveCurrentProfile();
97-
updateInterface();
97+
await updateInterface(); // Now awaiting the async function
9898
showToast('Reverted to default settings successfully.', 'success');
9999
logToGUIConsole('Reverted to default settings');
100100
} catch (error) {
@@ -137,24 +137,30 @@ function updateSaveStatus() {
137137
/**
138138
* Updates the entire interface based on the current profile.
139139
*/
140-
function updateInterface() {
140+
async function updateInterface() {
141141
// --- Added guard to check if currentProfile is valid ---
142142
if (!currentProfile || !currentProfile.customButtons) {
143143
logToGUIConsole('No valid current profile found. Attempting to retrieve default profile...');
144-
chrome.runtime.sendMessage({ type: 'getConfig' }, (response) => {
144+
try {
145+
const response = await chrome.runtime.sendMessage({ type: 'getConfig' });
145146
if (response && response.config) {
146147
currentProfile = response.config;
147-
updateInterface(); // Call updateInterface again after retrieving default
148+
await updateInterface(); // Call updateInterface again after retrieving default
148149
} else {
149150
logToGUIConsole('Failed to retrieve default profile in updateInterface.');
150151
}
151-
});
152+
} catch (error) {
153+
logToGUIConsole(`Error retrieving default profile: ${error.message}`);
154+
}
152155
return;
153156
}
157+
154158
// Update buttons, settings, etc. based on currentProfile
155-
updatebuttonCardsList();
159+
await updatebuttonCardsList(); // Now awaiting this async function
160+
156161
document.getElementById('autoSendToggle').checked = currentProfile.globalAutoSendEnabled;
157162
document.getElementById('shortcutsToggle').checked = currentProfile.enableShortcuts;
163+
158164
// Clear input fields
159165
document.getElementById('buttonIcon').value = '';
160166
document.getElementById('buttonText').value = '';

0 commit comments

Comments
 (0)