-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
815 lines (693 loc) · 29.6 KB
/
script.js
File metadata and controls
815 lines (693 loc) · 29.6 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Initialize the table with one column and one row
document.addEventListener('DOMContentLoaded', function() {
const tableBody = document.getElementById('tableBody');
const headerRow = document.getElementById('headerRow');
const inputsContainer = document.getElementById('inputsContainer');
const exportBtn = document.getElementById('exportBtn');
const resetBtn = document.getElementById('resetBtn');
// Function to add a new column
function addColumn() {
// Add header cell
const headerCell = document.createElement('th');
headerCell.className = 'header-cell';
headerCell.innerHTML = `
<input type="text" class="var-name" placeholder="Variable name" />
<select class="var-type">
<option value="str">str</option>
<option value="int">int</option>
<option value="bool">bool</option>
</select>
`;
headerRow.appendChild(headerCell);
// Add data cells to all existing rows
const rows = tableBody.querySelectorAll('tr');
rows.forEach(row => {
const dataCell = document.createElement('td');
dataCell.className = 'data-cell';
dataCell.innerHTML = '<input type="text" class="cell-input" placeholder="Value" />';
row.appendChild(dataCell);
});
// Attach event listeners to the new header inputs
attachHeaderListeners(headerCell);
updateStrikethrough();
}
// Function to add a new row
function addRow() {
const row = document.createElement('tr');
const columnCount = headerRow.querySelectorAll('th').length;
for (let i = 0; i < columnCount; i++) {
const dataCell = document.createElement('td');
dataCell.className = 'data-cell';
dataCell.innerHTML = '<input type="text" class="cell-input" placeholder="Value" />';
row.appendChild(dataCell);
}
tableBody.appendChild(row);
updateStrikethrough();
}
// Function to update strikethrough styling for cells with values below
function updateStrikethrough() {
const rows = tableBody.querySelectorAll('tr');
rows.forEach((row, rowIndex) => {
const cells = row.querySelectorAll('.cell-input');
cells.forEach((input, colIndex) => {
// Check if any cell below in the same column has a value
let hasValueBelow = false;
for (let i = rowIndex + 1; i < rows.length; i++) {
const belowRow = rows[i];
const belowInput = belowRow.querySelectorAll('.cell-input')[colIndex];
if (belowInput && belowInput.value.trim() !== '') {
hasValueBelow = true;
break;
}
}
// Apply or remove strikethrough
const wasStrikethrough = input.classList.contains('strikethrough');
if (hasValueBelow && input.value.trim() !== '') {
input.classList.add('strikethrough');
} else {
input.classList.remove('strikethrough');
}
// Re-validate if strikethrough status changed or if it's strikethrough
if (wasStrikethrough !== input.classList.contains('strikethrough') || input.classList.contains('strikethrough')) {
const headerCell = headerRow.querySelectorAll('th')[colIndex];
if (headerCell && input.value.trim() !== '') {
const type = headerCell.querySelector('.var-type').value;
validateCellInput(input, type);
}
}
});
});
}
// Track if we're currently adding/removing to prevent infinite loops
let isAddingColumn = false;
let isAddingRow = false;
let isRemovingColumn = false;
let isRemovingRow = false;
// Function to remove a column
function removeColumn(columnIndex) {
if (isRemovingColumn) return;
let headers = headerRow.querySelectorAll('th');
if (columnIndex >= headers.length) {
return;
}
isRemovingColumn = true;
// Remove header
if (headers[columnIndex]) {
headers[columnIndex].remove();
}
// Remove cells from all rows
const rows = tableBody.querySelectorAll('tr');
rows.forEach(row => {
const cells = row.querySelectorAll('td');
if (cells[columnIndex]) {
cells[columnIndex].remove();
}
});
updateStrikethrough();
// Re-validate all variable names after removal (in case a duplicate was removed)
validateAllVariableNames();
setTimeout(() => {
isRemovingColumn = false;
}, 100);
}
// Function to remove a row
function removeRow(rowIndex) {
if (isRemovingRow) return;
let rows = tableBody.querySelectorAll('tr');
if (rowIndex >= rows.length) {
return;
}
isRemovingRow = true;
if (rows[rowIndex]) {
rows[rowIndex].remove();
}
updateStrikethrough();
setTimeout(() => {
isRemovingRow = false;
}, 100);
}
// Function to check if a column is empty (header name empty and all cells empty)
function checkRemoveColumn(columnIndex) {
let headers = headerRow.querySelectorAll('th');
const header = headers[columnIndex];
if (!header) return;
const varNameInput = header.querySelector('.var-name');
const headerName = varNameInput.value.trim();
// If header name is empty, check all cells
if (headerName === '') {
const rows = tableBody.querySelectorAll('tr');
let hasAnyValue = false;
rows.forEach(row => {
const cells = row.querySelectorAll('.cell-input');
const cell = cells[columnIndex];
if (cell && cell.value.trim() !== '') {
hasAnyValue = true;
}
});
// If this column has no values, remove it
if (!hasAnyValue) {
removeColumn(columnIndex);
}
}
}
// Function to check if a row is empty (all cells empty)
function checkRemoveRow(rowIndex) {
const rows = tableBody.querySelectorAll('tr');
const row = rows[rowIndex];
if (!row) return;
const cells = row.querySelectorAll('.cell-input');
let hasAnyValue = false;
cells.forEach(cell => {
if (cell.value.trim() !== '') {
hasAnyValue = true;
}
});
// If this row has no values, remove it
if (!hasAnyValue) {
removeRow(rowIndex);
}
}
// Function to check if we need to add a new column
function checkAddColumn() {
if (isAddingColumn) return;
const lastHeader = headerRow.querySelector('th:last-child');
const varNameInput = lastHeader.querySelector('.var-name');
// If the last column's variable name has content, add a new column
if (varNameInput.value.trim() !== '') {
isAddingColumn = true;
addColumn();
// Reset flag after a short delay
setTimeout(() => {
isAddingColumn = false;
}, 100);
}
}
// Function to check if we need to add a new row
function checkAddRow() {
if (isAddingRow) return;
const lastRow = tableBody.querySelector('tr:last-child');
const inputs = lastRow.querySelectorAll('.cell-input');
// Check if any cell in the last row has content
let hasContent = false;
inputs.forEach(input => {
if (input.value.trim() !== '') {
hasContent = true;
}
});
if (hasContent) {
isAddingRow = true;
addRow();
// Reset flag after a short delay
setTimeout(() => {
isAddingRow = false;
}, 100);
}
}
// Function to check if a string is a valid Python identifier
function isValidPythonIdentifier(name) {
if (!name || name.trim() === '') {
return false;
}
// Python identifier rules:
// - Must start with a letter or underscore
// - Can contain letters, digits, and underscores
// - Cannot be a keyword (we'll check common ones)
// - Cannot start with a digit
const trimmed = name.trim();
// Empty string is not valid
if (trimmed === '') {
return false;
}
// Cannot start with a digit
if (/^\d/.test(trimmed)) {
return false;
}
// Must match pattern: letter/underscore followed by letters, digits, underscores
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(trimmed)) {
return false;
}
// Check for Python keywords
const keywords = [
'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'False', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'None', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'True', 'try', 'while', 'with', 'yield'
];
if (keywords.includes(trimmed)) {
return false;
}
return true;
}
// Function to get all variable names from headers
function getAllVariableNames(excludeInput) {
const headers = headerRow.querySelectorAll('th');
const names = [];
headers.forEach((header, index) => {
const varNameInput = header.querySelector('.var-name');
if (varNameInput && varNameInput !== excludeInput) {
const name = varNameInput.value.trim();
if (name !== '') {
names.push({ name: name.toLowerCase(), index: index, input: varNameInput });
}
}
});
return names;
}
// Function to validate variable name input
function validateVariableName(input) {
const value = input.value.trim();
// Remove error class first
input.classList.remove('error');
// Empty is valid (no error, but no highlight)
if (value === '') {
return;
}
// Check if it's a valid Python identifier
if (!isValidPythonIdentifier(value)) {
input.classList.add('error');
return;
}
// Check for duplicates (case-insensitive)
const allNames = getAllVariableNames(input);
const lowerValue = value.toLowerCase();
const isDuplicate = allNames.some(item => item.name === lowerValue);
if (isDuplicate) {
input.classList.add('error');
}
}
// Function to validate all variable names (for when one changes)
function validateAllVariableNames() {
const headers = headerRow.querySelectorAll('th');
headers.forEach(header => {
const varNameInput = header.querySelector('.var-name');
if (varNameInput) {
validateVariableName(varNameInput);
}
});
}
// Function to validate and format cell input based on type
function validateCellInput(input, type) {
const value = input.value.trim();
const isStrikethrough = input.classList.contains('strikethrough');
// Remove validation classes first
input.classList.remove('valid', 'invalid');
// Reset validation if empty
if (value === '') {
return;
}
let isValid = false;
if (type === 'int') {
// Check if it's a valid integer
isValid = /^-?\d+$/.test(value);
} else if (type === 'str') {
// Check if it's a valid string (in quotes)
isValid = /^".*"$/.test(value) || /^'.*'$/.test(value);
} else if (type === 'bool') {
// Check if it's a valid boolean (True or False)
isValid = value === 'True' || value === 'False';
}
// Apply appropriate class
if (isValid) {
input.classList.add('valid');
} else {
input.classList.add('invalid');
}
}
// Attach event listeners to header inputs
function attachHeaderListeners(headerCell) {
const varNameInput = headerCell.querySelector('.var-name');
const varTypeSelect = headerCell.querySelector('.var-type');
varNameInput.addEventListener('input', function() {
// Validate this variable name
validateVariableName(varNameInput);
// Validate all variable names to check for duplicates
validateAllVariableNames();
checkAddColumn();
// Check if this column should be removed (use setTimeout to check after value is cleared)
const currentHeaders = headerRow.querySelectorAll('th');
const columnIndex = Array.from(currentHeaders).indexOf(headerCell);
setTimeout(() => {
checkRemoveColumn(columnIndex);
}, 0);
});
varNameInput.addEventListener('blur', function() {
// Validate this variable name
validateVariableName(varNameInput);
// Validate all variable names to check for duplicates
validateAllVariableNames();
checkAddColumn();
const headers = headerRow.querySelectorAll('th');
const columnIndex = Array.from(headers).indexOf(headerCell);
// Never check for removal if this is the last column
if (columnIndex === headers.length - 1) return;
checkRemoveColumn(columnIndex);
});
varTypeSelect.addEventListener('change', function() {
checkAddColumn();
// Update validation for all cells in this column
const columnIndex = Array.from(headerRow.querySelectorAll('th')).indexOf(headerCell);
const rows = tableBody.querySelectorAll('tr');
rows.forEach(row => {
const cellInput = row.querySelectorAll('.cell-input')[columnIndex];
if (cellInput && cellInput.value.trim() !== '') {
validateCellInput(cellInput, varTypeSelect.value);
}
});
});
}
// Arrow key navigation for table cells
function handleArrowKeyNavigation(e) {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
const currentInput = e.target;
const currentCell = currentInput.closest('td');
const currentRow = currentInput.closest('tr');
const rows = tableBody.querySelectorAll('tr');
const currentRowIndex = Array.from(rows).indexOf(currentRow);
const columnIndex = Array.from(currentRow.querySelectorAll('td')).indexOf(currentCell);
let targetRowIndex;
if (e.key === 'ArrowUp') {
targetRowIndex = currentRowIndex - 1;
} else { // ArrowDown
targetRowIndex = currentRowIndex + 1;
}
// Check if target row exists
if (targetRowIndex >= 0 && targetRowIndex < rows.length) {
e.preventDefault();
const targetRow = rows[targetRowIndex];
const targetInput = targetRow.querySelectorAll('.cell-input')[columnIndex];
if (targetInput) {
targetInput.focus();
targetInput.select();
}
}
}
}
// Use event delegation for cell inputs (more efficient and handles dynamically added cells)
tableBody.addEventListener('input', function(e) {
if (e.target.classList.contains('cell-input')) {
checkAddRow();
// Get the column index
const cell = e.target.closest('td');
const row = e.target.closest('tr');
const rows = tableBody.querySelectorAll('tr');
const rowIndex = Array.from(rows).indexOf(row);
const columnIndex = Array.from(row.querySelectorAll('td')).indexOf(cell);
// Get the type from the header
const headerCell = headerRow.querySelectorAll('th')[columnIndex];
if (headerCell) {
const type = headerCell.querySelector('.var-type').value;
validateCellInput(e.target, type);
}
updateStrikethrough();
// Check if row should be removed (use setTimeout to check after value is cleared)
const currentRows = tableBody.querySelectorAll('tr');
if (rowIndex < currentRows.length - 1) {
setTimeout(() => {
const rowsAtCallTime = tableBody.querySelectorAll('tr');
const currentRowIndex = Array.from(rowsAtCallTime).indexOf(row);
// Never remove if it's the last row
if (currentRowIndex < rowsAtCallTime.length - 1) {
checkRemoveRow(currentRowIndex);
}
}, 0);
}
}
});
tableBody.addEventListener('keydown', function(e) {
if (e.target.classList.contains('cell-input')) {
handleArrowKeyNavigation(e);
}
});
tableBody.addEventListener('blur', function(e) {
if (e.target.classList.contains('cell-input')) {
// Get the column index
const cell = e.target.closest('td');
const row = e.target.closest('tr');
const rows = tableBody.querySelectorAll('tr');
const rowIndex = Array.from(rows).indexOf(row);
const columnIndex = Array.from(row.querySelectorAll('td')).indexOf(cell);
// Get the type from the header
const headerCell = headerRow.querySelectorAll('th')[columnIndex];
if (headerCell) {
const type = headerCell.querySelector('.var-type').value;
validateCellInput(e.target, type);
}
updateStrikethrough();
// Never check for removal if this is the last row
const currentRows = tableBody.querySelectorAll('tr');
if (rowIndex < currentRows.length - 1) {
checkRemoveRow(rowIndex);
}
// Check if column should be removed
const currentHeaders = headerRow.querySelectorAll('th');
if (columnIndex < currentHeaders.length - 1) {
checkRemoveColumn(columnIndex);
}
}
}, true); // Use capture phase for blur events
// Function to remove empty input boxes (except the last one)
function removeEmptyInputs() {
const allInputs = inputsContainer.querySelectorAll('.user-input');
// Never remove if there's only one input
if (allInputs.length <= 1) return;
// Collect inputs to remove (empty ones that are not the last)
const inputsToRemove = [];
allInputs.forEach((input, index) => {
const isLast = index === allInputs.length - 1;
// Mark for removal if empty and not the last one
if (!isLast && input.value.trim() === '') {
inputsToRemove.push(input);
}
});
// Remove the collected inputs
inputsToRemove.forEach(input => input.remove());
}
// User input boxes functionality
function addInputBox() {
const input = document.createElement('input');
input.type = 'text';
input.className = 'user-input';
input.placeholder = 'Write user input value here...';
inputsContainer.appendChild(input);
// When this input gets focus and has content, add another
input.addEventListener('input', function() {
if (input.value.trim() !== '') {
// Check if there's already a next input
const nextInput = input.nextElementSibling;
if (!nextInput || !nextInput.classList.contains('user-input')) {
addInputBox();
}
} else {
// If input becomes empty, check if we should remove it (but not if it's the last one)
setTimeout(() => {
removeEmptyInputs();
}, 0);
}
});
input.addEventListener('blur', function() {
// Check if this is the last input and has content
const allInputs = inputsContainer.querySelectorAll('.user-input');
const isLast = input === allInputs[allInputs.length - 1];
if (isLast && input.value.trim() !== '') {
addInputBox();
}
// Remove empty inputs (but not the last one)
removeEmptyInputs();
});
}
// Initialize first input box
const firstInput = inputsContainer.querySelector('.user-input');
firstInput.addEventListener('input', function() {
if (firstInput.value.trim() !== '') {
const nextInput = firstInput.nextElementSibling;
if (!nextInput || !nextInput.classList.contains('user-input')) {
addInputBox();
}
} else {
// If input becomes empty, check if we should remove it (but not if it's the last one)
setTimeout(() => {
removeEmptyInputs();
}, 0);
}
});
firstInput.addEventListener('blur', function() {
const allInputs = inputsContainer.querySelectorAll('.user-input');
const isLast = firstInput === allInputs[allInputs.length - 1];
if (isLast && firstInput.value.trim() !== '') {
addInputBox();
}
// Remove empty inputs (but not the last one)
removeEmptyInputs();
});
// Export to markdown functionality
exportBtn.addEventListener('click', function() {
const headers = headerRow.querySelectorAll('th');
if (headers.length === 0) {
alert('No data to export');
return;
}
// Collect user inputs
const userInputs = [];
const inputElements = inputsContainer.querySelectorAll('.user-input');
inputElements.forEach(input => {
const value = input.value.trim();
if (value !== '') {
userInputs.push(value);
}
});
// Get program output
const programOutput = document.getElementById('programOutput').value.trim();
// Filter columns - only include columns with non-empty variable names
const validColumns = [];
headers.forEach((header, index) => {
const name = header.querySelector('.var-name').value.trim();
if (name !== '') {
const type = header.querySelector('.var-type').value;
validColumns.push({
index: index,
name: name,
type: type,
header: `${name} (${type})`
});
}
});
if (validColumns.length === 0) {
alert('No valid columns to export');
return;
}
// Get all rows and filter out completely empty rows
const rows = tableBody.querySelectorAll('tr');
const validRows = [];
rows.forEach(row => {
const cells = row.querySelectorAll('.cell-input');
const rowValues = [];
let hasAnyValue = false;
validColumns.forEach(col => {
const cell = cells[col.index];
const value = cell ? cell.value.trim() : '';
rowValues.push(value);
if (value !== '') {
hasAnyValue = true;
}
});
if (hasAnyValue) {
validRows.push(rowValues);
}
});
// Calculate column widths for proper spacing
const columnWidths = validColumns.map((col, colIndex) => {
let maxWidth = col.header.length;
validRows.forEach(row => {
const value = row[colIndex] || '';
if (value.length > maxWidth) {
maxWidth = value.length;
}
});
return maxWidth;
});
// Helper function to pad a string to a specific width
function padString(str, width) {
return str + ' '.repeat(Math.max(0, width - str.length));
}
// Build markdown with proper spacing
let markdown = '';
// Add user inputs section if there are any
if (userInputs.length > 0) {
markdown += '**User Inputs:**\n';
userInputs.forEach((input, index) => {
markdown += `${index + 1}. ${input}\n`;
});
markdown += '\n';
}
// Add trace table header
markdown += '**Trace Table:**\n\n';
// Build table header
const markdownHeaderRow = '| ' + validColumns.map((col, index) => {
return padString(col.header, columnWidths[index]);
}).join(' | ') + ' |\n';
markdown += markdownHeaderRow;
// Build separator row
const separatorRow = '| ' + validColumns.map((col, index) => {
return '-'.repeat(columnWidths[index]);
}).join(' | ') + ' |\n';
markdown += separatorRow;
// Build data rows
validRows.forEach(row => {
const rowText = '| ' + row.map((value, index) => {
return padString(value, columnWidths[index]);
}).join(' | ') + ' |\n';
markdown += rowText;
});
// Add program output section if there is any
if (programOutput !== '') {
markdown += '\n**Program Output:**\n';
markdown += '```\n';
markdown += programOutput;
markdown += '\n```\n';
}
// Copy to clipboard
navigator.clipboard.writeText(markdown).then(() => {
// Show temporary success message
const originalText = exportBtn.textContent;
exportBtn.textContent = 'Copied!';
exportBtn.style.background = 'linear-gradient(135deg, #51cf66 0%, #40c057 100%)';
setTimeout(() => {
exportBtn.textContent = originalText;
exportBtn.style.background = '';
}, 2000);
}).catch(err => {
// Fallback: show in alert
alert('Markdown:\n\n' + markdown);
});
});
// Reset button functionality
resetBtn.addEventListener('click', function() {
if (confirm('Are you sure you want to reset everything? This will clear all inputs, the table, and output.')) {
// Clear all user inputs
const userInputs = inputsContainer.querySelectorAll('.user-input');
userInputs.forEach((input, index) => {
if (index > 0) {
input.remove();
} else {
input.value = '';
}
});
// Clear program output
document.getElementById('programOutput').value = '';
// Reset table to initial state (1 column, 1 row)
// Remove all columns except the first
const headers = headerRow.querySelectorAll('th');
for (let i = headers.length - 1; i > 0; i--) {
headers[i].remove();
}
// Clear the first header
const firstHeader = headerRow.querySelector('th');
firstHeader.querySelector('.var-name').value = '';
firstHeader.querySelector('.var-type').value = 'str';
// Remove all rows except the first
const rows = tableBody.querySelectorAll('tr');
for (let i = rows.length - 1; i > 0; i--) {
rows[i].remove();
}
// Clear the first row's cells
const firstRow = tableBody.querySelector('tr');
const firstRowCells = firstRow.querySelectorAll('.cell-input');
firstRowCells.forEach(cell => {
cell.value = '';
cell.classList.remove('strikethrough', 'valid', 'invalid');
});
// Remove extra cells from first row (keep only one)
const firstRowTds = firstRow.querySelectorAll('td');
for (let i = firstRowTds.length - 1; i > 0; i--) {
firstRowTds[i].remove();
}
updateStrikethrough();
}
});
// Initialize event listeners for the initial column
attachHeaderListeners(headerRow.querySelector('th'));
// Initial strikethrough update
updateStrikethrough();
// Initial validation of variable names
validateAllVariableNames();
});