Skip to content

Commit 5064afc

Browse files
committed
- grammar check v0.8
1 parent e8d425a commit 5064afc

File tree

1 file changed

+69
-120
lines changed

1 file changed

+69
-120
lines changed

tools/grammar_check.html

Lines changed: 69 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@
8282
<div class="flex items-center justify-between mb-6">
8383
<div class="flex items-center gap-2"><h1
8484
class="text-3xl font-bold bg-gradient-to-r from-indigo-600 to-purple-600 text-transparent bg-clip-text dark:from-indigo-400 dark:to-purple-400">
85-
AI Grammar Checker</h1>
86-
</div>
85+
AI Grammar Checker</h1></div>
8786
<select id="mode-select"
8887
class="px-3 py-1 border rounded-md text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 border-gray-300 dark:border-gray-600 focus:ring-2 focus:ring-indigo-500 dark:focus:ring-indigo-400 transition-colors duration-200">
8988
<option value="standard">Standard Mode</option>
@@ -117,19 +116,28 @@
117116
</svg>
118117
Clear
119118
</button>
120-
<button id="copy-corrected"
121-
class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-md focus:outline-none focus:ring-2 focus:ring-green-500 transition-colors duration-200 flex items-center gap-2">
122-
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
123-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
124-
d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3"></path>
125-
</svg>
126-
Copy
127-
</button>
128119
</div>
129120
</div>
130121
<div id="result"
131122
class="p-4 border rounded-md min-h-[100px] bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 border-gray-300 dark:border-gray-600 transition-colors duration-200"></div>
132-
<button id="show-all-errors" class="mt-4 px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-md focus:outline-none focus:ring-2 focus:ring-gray-500 transition-colors duration-200 flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>Show All Errors</button>
123+
<div class="mt-4 flex flex-wrap gap-3">
124+
<button id="show-all-errors"
125+
class="px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-md focus:outline-none focus:ring-2 focus:ring-gray-500 transition-colors duration-200 flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed">
126+
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
127+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
128+
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
129+
</svg>
130+
Show All Errors
131+
</button>
132+
<button id="copy-corrected"
133+
class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-md focus:outline-none focus:ring-2 focus:ring-green-500 transition-colors duration-200 flex items-center gap-2">
134+
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
135+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
136+
d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3"></path>
137+
</svg>
138+
Copy
139+
</button>
140+
</div>
133141
<div id="history" class="mt-4 space-y-2">
134142
<h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100 flex items-center gap-2">
135143
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
@@ -142,29 +150,52 @@ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100 flex items-cen
142150
</div>
143151
<div id="error-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
144152
<div class="bg-white dark:bg-gray-800 rounded-lg max-w-2xl w-full mx-4 max-h-[90vh] flex flex-col">
145-
<div class="p-4 border-b border-gray-200 dark:border-gray-700 flex justify-between items-center">
146-
<h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">All Detected Errors</h3>
147-
<button id="close-modal" class="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200">
153+
<div class="p-4 border-b border-gray-200 dark:border-gray-700 flex justify-between items-center"><h3
154+
class="text-lg font-semibold text-gray-900 dark:text-gray-100">All Detected Errors</h3>
155+
<button id="close-modal"
156+
class="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200">
148157
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
149-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
158+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
159+
d="M6 18L18 6M6 6l12 12"></path>
150160
</svg>
151161
</button>
152162
</div>
153163
<div class="p-4 overflow-y-auto flex-1">
154-
<div id="error-list" class="space-y-4">
155-
<!-- Errors will be inserted here -->
156-
</div>
164+
<div id="error-list" class="space-y-4"></div>
157165
</div>
158166
</div>
159167
</div>
160168
</div>
161169
<script>let errorHistory = [];
162170
let currentText = '';
163171
let lastCheckTimestamp = 0;
172+
let window
173+
.errors = [];
164174
document.getElementById('check-grammar').addEventListener('click', checkGrammar);
165175
document.getElementById('clear-text').addEventListener('click', clearText);
166176
document.getElementById('copy-corrected').addEventListener('click', copyCorrectText);
167177
document.getElementById('input').addEventListener('input', updateWordCount);
178+
const showAllErrorsBtn = document.getElementById('show-all-errors');
179+
showAllErrorsBtn.disabled = true;
180+
const errorModal = document.getElementById('error-modal');
181+
const closeModal = document.getElementById('close-modal');
182+
showAllErrorsBtn.addEventListener('click', showErrorModal);
183+
closeModal.addEventListener('click', function () {
184+
errorModal.classList.add('hidden');
185+
errorModal.classList.remove('flex');
186+
});
187+
errorModal.addEventListener('click', function (e) {
188+
if (e.target === errorModal) {
189+
errorModal.classList.add('hidden');
190+
errorModal.classList.remove('flex');
191+
}
192+
});
193+
document.addEventListener('keydown', function (e) {
194+
if (e.key === 'Escape' && !errorModal.classList.contains('hidden')) {
195+
errorModal.classList.add('hidden');
196+
errorModal.classList.remove('flex');
197+
}
198+
});
168199

169200
function updateWordCount() {
170201
const text = document.getElementById('input').value;
@@ -176,6 +207,8 @@ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">All Detected
176207
function clearText() {
177208
document.getElementById('input').value = '';
178209
document.getElementById('result').innerHTML = '';
210+
window.errors = [];
211+
showAllErrorsBtn.disabled = true;
179212
updateWordCount();
180213
}
181214

@@ -240,14 +273,14 @@ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">All Detected
240273
const jsonMatch = responseContent.match(/\{[\s\S]*\}/);
241274
if (!jsonMatch) throw new Error('Invalid response format');
242275
const grammarData = JSON5.parse(jsonMatch[0]);
276+
window.errors = grammarData.errors;
277+
showAllErrorsBtn.disabled = window.errors.length === 0;
243278
displayResults(inputText, grammarData.errors);
244279
addToHistory(inputText, grammarData.errors);
245280
} catch (error) {
246281
console.error('Error:', error);
247282
resultContainer.textContent = 'An error occurred while checking the text. Please try again.';
248283
showNotification('Error checking grammar', 'error');
249-
window.errors = grammarData.errors;
250-
document.getElementById('show-all-errors').disabled = grammarData.errors.length === 0;
251284
} finally {
252285
button.disabled = false;
253286
button.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg>Check Grammar';
@@ -297,31 +330,26 @@ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">All Detected
297330
});
298331
}
299332

333+
function showErrorModal() {
334+
const errorList = document.getElementById('error-list');
335+
errorList.innerHTML = window.errors.map((error, index) => `<div class="p-4 bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600"><div class="flex justify-between items-start"><div class="font-medium text-gray-900 dark:text-gray-100">Error ${index + 1}</div><button onclick="applyCorrection('${error.text}','${error.suggestion}')" class="text-sm px-3 py-1 bg-green-600 hover:bg-green-700 text-white rounded-md transition-colors duration-200">Apply Fix</button></div><div class="mt-2 space-y-2"><div class="text-sm"><span class="text-gray-500 dark:text-gray-400">Original text: </span><span class="text-red-600 dark:text-red-400">${error.text}</span></div><div class="text-sm"><span class="text-gray-500 dark:text-gray-400">Suggestion: </span><span class="text-green-600 dark:text-green-400">${error.suggestion}</span></div><div class="text-sm"><span class="text-gray-500 dark:text-gray-400">Description: </span><span class="text-gray-900 dark:text-gray-100">${error.description}</span></div></div></div>`).join('');
336+
errorModal.classList.remove('hidden');
337+
errorModal.classList.add('flex');
338+
}
339+
340+
function applyCorrection(original, suggestion) {
341+
const input = document.getElementById('input');
342+
input.value = input.value.replace(original, suggestion);
343+
displayResults(input.value, window.errors.filter(e => e.text !== original));
344+
updateWordCount();
345+
showNotification(`Corrected: ${original}${suggestion}`, 'success');
346+
}
347+
300348
function escapeRegExp(string) {
301349
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
302350
}
303351

304352
updateWordCount();
305-
document.addEventListener('keydown', function (e) {
306-
if (e.ctrlKey || e.metaKey) {
307-
switch (e.key) {
308-
case'Enter':
309-
e.preventDefault();
310-
checkGrammar();
311-
break;
312-
case'l':
313-
e.preventDefault();
314-
clearText();
315-
break;
316-
case'c':
317-
if (document.activeElement !== document.getElementById('input')) {
318-
e.preventDefault();
319-
copyCorrectText();
320-
}
321-
break;
322-
}
323-
}
324-
});
325353
let autoSaveTimeout;
326354
document.getElementById('input').addEventListener('input', function () {
327355
clearTimeout(autoSaveTimeout);
@@ -335,85 +363,6 @@ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">All Detected
335363
document.getElementById('input').value = savedText;
336364
updateWordCount();
337365
}
338-
});
339-
340-
// Initialize button state
341-
const showAllErrorsBtn = document.getElementById('show-all-errors');
342-
showAllErrorsBtn.disabled = true;
343-
344-
// Add modal functionality
345-
const errorModal = document.getElementById('error-modal');
346-
const closeModal = document.getElementById('close-modal');
347-
348-
showAllErrorsBtn.addEventListener('click', function() {
349-
showErrorModal();
350-
});
351-
352-
closeModal.addEventListener('click', function() {
353-
errorModal.classList.add('hidden');
354-
errorModal.classList.remove('flex');
355-
});
356-
357-
// Close modal when clicking outside
358-
errorModal.addEventListener('click', function(e) {
359-
if (e.target === errorModal) {
360-
errorModal.classList.add('hidden');
361-
errorModal.classList.remove('flex');
362-
}
363-
});
364-
365-
// Close modal with Escape key
366-
document.addEventListener('keydown', function(e) {
367-
if (e.key === 'Escape' && !errorModal.classList.contains('hidden')) {
368-
errorModal.classList.add('hidden');
369-
errorModal.classList.remove('flex');
370-
}
371-
});
372-
373-
function showErrorModal() {
374-
const errorList = document.getElementById('error-list');
375-
errorList.innerHTML = errors.map((error, index) => `
376-
<div class="p-4 bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600">
377-
<div class="flex justify-between items-start">
378-
<div class="font-medium text-gray-900 dark:text-gray-100">Error ${index + 1}</div>
379-
<button onclick="applyCorrection('${error.text}', '${error.suggestion}')"
380-
class="text-sm px-3 py-1 bg-green-600 hover:bg-green-700 text-white rounded-md transition-colors duration-200">
381-
Apply Fix
382-
</button>
383-
</div>
384-
<div class="mt-2 space-y-2">
385-
<div class="text-sm">
386-
<span class="text-gray-500 dark:text-gray-400">Original text: </span>
387-
<span class="text-red-600 dark:text-red-400">${error.text}</span>
388-
</div>
389-
<div class="text-sm">
390-
<span class="text-gray-500 dark:text-gray-400">Suggestion: </span>
391-
<span class="text-green-600 dark:text-green-400">${error.suggestion}</span>
392-
</div>
393-
<div class="text-sm">
394-
<span class="text-gray-500 dark:text-gray-400">Description: </span>
395-
<span class="text-gray-900 dark:text-gray-100">${error.description}</span>
396-
</div>
397-
</div>
398-
</div>
399-
`).join('');
400-
401-
errorModal.classList.remove('hidden');
402-
errorModal.classList.add('flex');
403-
}
404-
405-
function applyCorrection(original, suggestion) {
406-
const input = document.getElementById('input');
407-
input.value = input.value.replace(original, suggestion);
408-
displayResults(input.value, errors.filter(e => e.text !== original));
409-
updateWordCount();
410-
showNotification(`Corrected: ${original}${suggestion}`, 'success');
411-
}
412-
413-
// Add this to your existing checkGrammar function, right after parsing the grammarData:
414-
window.errors = grammarData.errors; // Make errors globally available
415-
showAllErrorsBtn.disabled = grammarData.errors.length === 0;
416-
</script>
417-
<script src="../sidebar.js"></script>
366+
});</script>
418367
</body>
419368
</html>

0 commit comments

Comments
 (0)