-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
215 lines (188 loc) · 8.86 KB
/
script.js
File metadata and controls
215 lines (188 loc) · 8.86 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
document.addEventListener('DOMContentLoaded', () => {
// Card related elements
const addCardBtn = document.getElementById('add-card-btn');
const cardsContainer = document.getElementById('cards-container');
const cardTemplate = document.getElementById('card-template');
const combineTextBtn = document.getElementById('combine-text-btn');
// Modal elements
const editModal = document.getElementById('edit-modal');
const modalCloseBtn = document.getElementById('modal-close-btn');
const modalTitleInput = document.getElementById('modal-title-input');
const modalContentTextarea = document.getElementById('modal-content-textarea');
const modalSaveBtn = document.getElementById('modal-save-btn');
const modalCancelBtn = document.getElementById('modal-cancel-btn');
let cardCount = 0;
let selectedCard = null; // Variable to store the currently selected card
let cardBeingEdited = null; // Variable to store the card being edited via modal
// --- Modal Functions ---
function openEditModal(cardElement) {
cardBeingEdited = cardElement;
const currentTitle = cardElement.querySelector('.card-title').textContent;
const currentContent = cardElement.querySelector('.card-content').textContent;
modalTitleInput.value = currentTitle;
modalContentTextarea.value = currentContent;
editModal.style.display = 'block';
}
function closeEditModal() {
editModal.style.display = 'none';
cardBeingEdited = null;
// Optionally clear fields:
// modalTitleInput.value = '';
// modalContentTextarea.value = '';
}
// Event listeners for modal buttons and window
modalCloseBtn.addEventListener('click', closeEditModal);
modalCancelBtn.addEventListener('click', closeEditModal);
modalSaveBtn.addEventListener('click', () => {
if (cardBeingEdited) {
const newTitle = modalTitleInput.value.trim();
const newContent = modalContentTextarea.value.trim();
cardBeingEdited.querySelector('.card-title').textContent = newTitle || `Prompt Card ${cardBeingEdited.id.split('-')[1]}`; // Fallback title
cardBeingEdited.querySelector('.card-content').textContent = newContent || 'Click "Edit" to add your prompt here.'; // Fallback content
}
closeEditModal();
});
window.addEventListener('click', (event) => {
if (event.target == editModal) {
closeEditModal();
}
});
// --- Card Creation and Management ---
function createAndSetupCard(initialTitle, initialContent) {
cardCount++;
const newCard = cardTemplate.content.cloneNode(true).querySelector('.card');
newCard.id = `card-${cardCount}`;
const cardTitleElement = newCard.querySelector('.card-title');
const cardContentElement = newCard.querySelector('.card-content');
if (cardTitleElement) {
cardTitleElement.textContent = initialTitle || `Prompt Card ${cardCount}`;
}
if (cardContentElement) {
cardContentElement.textContent = initialContent || 'Please edit to add content.';
}
cardsContainer.appendChild(newCard);
// Card selection logic
newCard.addEventListener('click', (event) => {
if (event.target.tagName === 'BUTTON' || event.target.closest('.modal-content')) {
return;
}
if (selectedCard && selectedCard !== newCard) { // Ensure not deselecting then reselecting same card
selectedCard.classList.remove('selected-card');
}
selectedCard = newCard;
selectedCard.classList.add('selected-card');
});
// Add event listeners for new card buttons
const editBtn = newCard.querySelector('.edit-btn');
const copyBtn = newCard.querySelector('.copy-btn');
const deleteBtn = newCard.querySelector('.delete-btn');
if(editBtn) {
editBtn.addEventListener('click', () => {
openEditModal(newCard);
});
}
if(copyBtn) {
copyBtn.addEventListener('click', () => {
const contentElement = newCard.querySelector('.card-content');
if (contentElement) {
const textToCopy = contentElement.textContent;
navigator.clipboard.writeText(textToCopy)
.then(() => {
const originalText = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
})
.catch(err => {
console.error('Failed to copy text: ', err);
alert('Failed to copy text.');
});
} else {
alert('No content to copy.');
}
});
}
if(deleteBtn) {
deleteBtn.addEventListener('click', () => {
console.log(`Delete button clicked for ${newCard.id}`);
if (selectedCard && selectedCard.id === newCard.id) {
selectedCard = null;
}
if (cardBeingEdited && cardBeingEdited.id === newCard.id) {
closeEditModal();
}
newCard.remove();
});
}
return newCard; // Return the created card element
}
addCardBtn.addEventListener('click', () => {
const newCard = createAndSetupCard(null, null); // Use default title/content
openEditModal(newCard); // Open modal for the newly added card
});
combineTextBtn.addEventListener('click', async () => {
if (!selectedCard) {
alert('Please select a card first to combine its text with the clipboard.');
return;
}
const cardContentElement = selectedCard.querySelector('.card-content');
if (!cardContentElement) {
alert('Could not find content in the selected card.');
return;
}
const cardText = cardContentElement.textContent;
try {
const clipboardText = await navigator.clipboard.readText();
if (typeof clipboardText !== 'string' || clipboardText.trim() === '') {
alert('Clipboard is empty or does not contain text. Try copying some text first.');
return;
}
const combinedText = cardText + "\n" + clipboardText;
await navigator.clipboard.writeText(combinedText);
const originalBtnText = combineTextBtn.textContent;
combineTextBtn.textContent = 'Combined & Copied!';
setTimeout(() => {
combineTextBtn.textContent = originalBtnText;
}, 3000);
} catch (err) {
console.error('Failed to read from or write to clipboard: ', err);
if (err.name === 'NotFoundError' || (typeof clipboardText === 'undefined')) {
alert('Clipboard is empty or text could not be read. Try copying some text first.');
} else if (err.name === 'NotAllowedError') {
alert('Permission to access clipboard was denied. Please allow clipboard access in your browser settings.');
} else {
alert('Failed to combine text with clipboard. Error: ' + err.message);
}
}
});
// --- Keyboard Shortcuts ---
document.addEventListener('keydown', function(event) {
// Paste Text as New Card (Ctrl+Shift+V)
if (event.ctrlKey && event.shiftKey && event.key === 'V') {
event.preventDefault();
navigator.clipboard.readText()
.then(text => {
if (text && text.trim() !== '') {
const newCard = createAndSetupCard("Pasted Card", text);
openEditModal(newCard); // Open modal to refine
} else {
alert('Clipboard is empty or contains no text to paste.');
}
})
.catch(err => {
console.error('Failed to read from clipboard for paste: ', err);
if (err.name === 'NotAllowedError') {
alert('Permission to access clipboard was denied. Please allow clipboard access.');
} else {
alert('Failed to paste text from clipboard. See console for details.');
}
});
}
// Copy Combined Card Shortcut (Ctrl+Shift+C)
if (event.ctrlKey && event.shiftKey && event.key === 'C') {
event.preventDefault();
combineTextBtn.click(); // Programmatically click the existing button
}
});
});