-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletter-replacement-lab.html
More file actions
312 lines (281 loc) · 11.8 KB
/
Copy pathletter-replacement-lab.html
File metadata and controls
312 lines (281 loc) · 11.8 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Letter Swap Lab - Text-Kitchen</title>
<link rel="stylesheet" href="styles.css">
<style>
.letter-buttons {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
gap: 8px;
margin: 15px 0;
}
.letter-btn {
background: #ffffff;
border: 2px solid #4a4a4a;
padding: 12px 8px;
text-align: center;
cursor: pointer;
font-weight: 600;
font-size: 1.05em;
transition: all 0.3s ease;
text-transform: uppercase;
}
.letter-btn:hover {
border-color: #c41e3a;
background: #e8e8e8;
}
.letter-btn.selected {
background: #1a1a1a;
color: #ffffff;
border-color: #1a1a1a;
}
.swap-panels {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 25px;
}
@media (max-width: 768px) {
.swap-panels {
grid-template-columns: 1fr;
}
}
.swap-panel {
padding: 25px;
background: #ffffff;
border: 2px solid #4a4a4a;
}
.swap-panel h3 {
margin-bottom: 10px;
color: #1a1a1a;
}
.selected-row {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 5px;
color: #4a4a4a;
font-size: 0.95em;
}
.selected-indicator {
font-size: 1.2em;
font-weight: 700;
color: #c41e3a;
display: inline-block;
min-width: 40px;
text-align: left;
border-bottom: 2px solid #c41e3a;
padding: 0 5px;
line-height: 1.4;
word-break: break-all;
}
</style>
</head>
<body>
<nav class="main-nav">
<a href="index.html">Home</a>
<a href="text-reverse-lab.html">Text Reversal</a>
<a href="line-break-lab.html">Line Break Lab</a>
<a href="eraser-lab.html">Eraser Lab</a>
<a href="alphabetizer-lab.html">Alphabetizer Lab</a>
<a href="markov-mashup-lab.html">Markov Mashup Lab</a>
<a href="lipogram-lab.html">Lipogram Lab</a>
<a href="long-tail-lab.html">Long Tail Lab</a>
<a href="grid-it-lab.html">Grid-It Lab</a>
<a href="letter-replacement-lab.html">Letter Swap</a>
<a href="chromacode-lab.html">Chromacode</a>
<a href="about.html">About</a>
</nav>
<div class="container">
<div class="header">
<h1>Letter Swap Lab</h1>
<p>Replace every occurrence of one letter with another</p>
</div>
<div class="upload-section">
<div class="upload-icon">📄</div>
<h2>Upload Text File</h2>
<p>Select a .txt file to swap letters in</p>
<label class="file-input-wrapper">
Choose File
<input type="file" id="textFile" accept=".txt" />
</label>
</div>
<div id="optionsSection" style="display: none;">
<div class="swap-panels">
<div class="swap-panel">
<h3>Replace these letters:</h3>
<div class="selected-row">
Selected: <span class="selected-indicator" id="fromSelected">—</span>
</div>
<div class="letter-buttons" id="fromButtons"></div>
</div>
<div class="swap-panel">
<h3>Replace with:</h3>
<div class="selected-row">
Selected: <span class="selected-indicator" id="toSelected">—</span>
</div>
<div class="letter-buttons" id="toButtons"></div>
</div>
</div>
<div class="option-group" style="margin-bottom: 20px;">
<div class="checkbox-container">
<input type="checkbox" id="caseSensitive" />
<label for="caseSensitive">Case-sensitive (only replace the uppercase letter shown; leave lowercase unchanged)</label>
</div>
</div>
<div style="display: flex; gap: 15px; flex-wrap: wrap;">
<button class="apply-btn" id="applyBtn" disabled>Apply Swap</button>
<button class="reset-btn" id="resetBtn" style="display: none;">Reset to Original</button>
</div>
</div>
<div class="results-section" id="resultsSection" style="display: none;">
<div class="output-header">
<div class="output-title">Swapped Text</div>
<div class="font-size-control">
<label for="fontSizeSlider">Font Size:</label>
<input type="range" id="fontSizeSlider" class="slider" min="12" max="32" value="16" step="1">
</div>
</div>
<div class="output-text" id="outputText"></div>
<div class="stats" id="statsDisplay"></div>
<button class="apply-btn" id="exportBtn" style="margin-top: 15px;">Export TXT</button>
</div>
</div>
<script>
let fileContent = '';
let currentText = '';
let fromLetters = new Set();
let toLetter = null;
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const fileInput = document.getElementById('textFile');
const optionsSection = document.getElementById('optionsSection');
const resultsSection = document.getElementById('resultsSection');
const outputText = document.getElementById('outputText');
const statsDisplay = document.getElementById('statsDisplay');
const applyBtn = document.getElementById('applyBtn');
const resetBtn = document.getElementById('resetBtn');
const exportBtn = document.getElementById('exportBtn');
function initButtons() {
const fromContainer = document.getElementById('fromButtons');
const toContainer = document.getElementById('toButtons');
alphabet.forEach(letter => {
const fromBtn = document.createElement('button');
fromBtn.className = 'letter-btn';
fromBtn.textContent = letter;
fromBtn.addEventListener('click', () => {
if (fromLetters.has(letter)) {
fromLetters.delete(letter);
fromBtn.classList.remove('selected');
} else {
fromLetters.add(letter);
fromBtn.classList.add('selected');
}
const display = fromLetters.size > 0 ? Array.from(fromLetters).join(' ') : '—';
document.getElementById('fromSelected').textContent = display;
updateApplyBtn();
});
fromContainer.appendChild(fromBtn);
const toBtn = document.createElement('button');
toBtn.className = 'letter-btn';
toBtn.textContent = letter;
toBtn.addEventListener('click', () => {
document.querySelectorAll('#toButtons .letter-btn').forEach(b => b.classList.remove('selected'));
if (toLetter === letter) {
toLetter = null;
document.getElementById('toSelected').textContent = '—';
} else {
toLetter = letter;
toBtn.classList.add('selected');
document.getElementById('toSelected').textContent = letter;
}
updateApplyBtn();
});
toContainer.appendChild(toBtn);
});
}
function updateApplyBtn() {
applyBtn.disabled = !(fromLetters.size > 0 && toLetter && fileContent);
}
fileInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (!file) return;
if (file.type === 'text/plain' || file.name.endsWith('.txt')) {
const reader = new FileReader();
reader.onload = function(e) {
fileContent = e.target.result;
currentText = fileContent;
optionsSection.style.display = 'block';
resultsSection.style.display = 'none';
updateApplyBtn();
};
reader.onerror = function() {
alert('Error reading file. Please try again.');
};
reader.readAsText(file);
} else {
alert('Please select a valid .txt file');
}
});
applyBtn.addEventListener('click', function() {
if (!currentText || fromLetters.size === 0 || !toLetter) return;
const caseSensitive = document.getElementById('caseSensitive').checked;
let count = 0;
let result;
const toUpper = toLetter;
const toLower = toLetter.toLowerCase();
if (caseSensitive) {
const charClass = '[' + Array.from(fromLetters).join('') + ']';
result = currentText.replace(new RegExp(charClass, 'g'), function() {
count++;
return toUpper;
});
} else {
const chars = Array.from(fromLetters).map(l => l + l.toLowerCase()).join('');
result = currentText.replace(new RegExp('[' + chars + ']', 'g'), function(char) {
count++;
return char === char.toUpperCase() ? toUpper : toLower;
});
}
currentText = result;
outputText.textContent = result;
const fromList = Array.from(fromLetters).join(', ');
const caseNote = caseSensitive ? ' (uppercase only)' : ' (both cases)';
statsDisplay.textContent = 'Last swap: replaced ' + count + ' occurrence' + (count !== 1 ? 's' : '') + ' of "' + fromList + '"' + caseNote + ' with "' + toLetter + '"';
resetBtn.style.display = 'block';
resultsSection.style.display = 'block';
resultsSection.scrollIntoView({ behavior: 'smooth' });
});
resetBtn.addEventListener('click', function() {
currentText = fileContent;
fromLetters.clear();
document.querySelectorAll('#fromButtons .letter-btn').forEach(b => b.classList.remove('selected'));
document.getElementById('fromSelected').textContent = '—';
toLetter = null;
document.querySelectorAll('#toButtons .letter-btn').forEach(b => b.classList.remove('selected'));
document.getElementById('toSelected').textContent = '—';
updateApplyBtn();
outputText.textContent = '';
statsDisplay.textContent = '';
resultsSection.style.display = 'none';
resetBtn.style.display = 'none';
});
exportBtn.addEventListener('click', function() {
const text = outputText.textContent;
if (!text) return;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'letter-swap-output.txt';
a.click();
URL.revokeObjectURL(url);
});
document.getElementById('fontSizeSlider').addEventListener('input', function(e) {
outputText.style.fontSize = e.target.value + 'px';
});
initButtons();
</script>
</body>
</html>