-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
235 lines (190 loc) · 6.02 KB
/
script.js
File metadata and controls
235 lines (190 loc) · 6.02 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
// Helper to get the editable div
const textInput = document.getElementById("text-input");
function getText() {
return textInput.innerText;
}
function setText(newText) {
textInput.innerText = newText;
updatePreviewAndSummary();
}
// 1. UPPERCASE
function convertToUpper() {
setText(getText().toUpperCase());
}
// 2. lowercase
function convertToLower() {
setText(getText().toLowerCase());
}
// 3. Remove Spaces
function removeSpaces() {
setText(getText().replace(/\s+/g, ' ').trim());
}
// 4. Copy
function copyText() {
navigator.clipboard.writeText(getText()).then(() => alert("Copied!"));
}
// 5. Clear
function clearText() {
setText("");
}
// 6. Speak
function SpeakText() {
const msg = new SpeechSynthesisUtterance(getText());
speechSynthesis.speak(msg);
}
// 7-9. Formatting
function wrapSelectedText(tag) {
const selection = window.getSelection();
if (!selection.rangeCount) return;
const range = selection.getRangeAt(0);
const wrapper = document.createElement(tag);
range.surroundContents(wrapper);
updatePreviewAndSummary();
}
function makeBold() {
textInput.classList.toggle("bold-text");
}
function makeItalic() {
textInput.classList.toggle("italic-text");
}
function makeUnderline() {
textInput.classList.toggle("underline-text");
}
// 10. Aestheticify
function aestheticifyText() {
let newText = getText().split('').join(' ');
setText(newText);
}
// 11. Date Extractor
function extractDates() {
const regex = /\b\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4}\b/g;
const matches = getText().match(regex);
alert(matches ? matches.join("\n") : "No dates found");
}
// 12. Capitalize First Letters
function capitalizeFirstLetters() {
let newText = getText().replace(/\b\w/g, char => char.toUpperCase());
setText(newText);
}
// 13. Palindrome Checker
function checkPalindrome() {
const text = getText().toLowerCase().replace(/[^a-z0-9]/g, "");
const isPalindrome = text === text.split("").reverse().join("");
alert(isPalindrome ? "It's a palindrome!" : "Not a palindrome.");
}
// 14-15 Undo/Redo
let history = [], redoStack = [];
function saveState() {
history.push(getText());
if (history.length > 50) history.shift(); // limit
}
function undoAction() {
if (history.length) {
redoStack.push(getText());
setText(history.pop());
}
}
function redoAction() {
if (redoStack.length) {
saveState();
setText(redoStack.pop());
}
}
function searchAndHighlight() {
const inputText = document.getElementById("text-input");
const searchWord = document.getElementById("search-word").value.trim().toLowerCase();
const originalText = inputText.innerText;
if (!searchWord) {
inputText.innerHTML = originalText; // Reset
alert("Please enter a word to search.");
return;
}
const regex = new RegExp(`(${searchWord})`, "gi");
const matches = originalText.match(regex);
if (matches) {
const highlightedText = originalText.replace(regex, `<mark>$1</mark>`);
inputText.innerHTML = highlightedText;
} else {
alert("❌ Word not found!");
inputText.innerHTML = originalText; // Reset to original if not found
}
}
// Word, character count, reading time, preview
function updatePreviewAndSummary() {
const text = getText();
document.getElementById("word-count").innerText = `Words: ${text.trim().split(/\s+/).filter(Boolean).length}`;
document.getElementById("char-count").innerText = `Characters: ${text.length}`;
document.getElementById("reading-time").innerText = `Reading Time: ${Math.ceil(text.length / 200)} mins`;
document.getElementById("preview-text").innerText = text || "Nothing to preview";
}
// Save initial state
textInput.addEventListener("input", saveState);
textInput.addEventListener("input", updatePreviewAndSummary);
document.addEventListener("DOMContentLoaded", () => {
saveState();
updatePreviewAndSummary();
});
const themeToggle = document.getElementById("theme-toggle");
const body = document.body;
// 🔁 1. Check localStorage and apply mode on page load
window.addEventListener("DOMContentLoaded", () => {
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
enableDarkMode();
}
});
// 🔄 2. Toggle Theme and Save to localStorage
themeToggle.addEventListener("click", () => {
if (body.classList.contains("dark-mode")) {
disableDarkMode();
} else {
enableDarkMode();
}
});
// ✅ Enable Dark Mode
function enableDarkMode() {
body.classList.add("dark-mode");
document.querySelector(".navbar").classList.add("dark-mode");
document.getElementById("text-input").classList.add("dark-mode");
document.querySelectorAll("button").forEach((btn) => {
btn.classList.add("dark-mode");
});
document.querySelectorAll("input").forEach((input) => {
input.classList.add("dark-mode");
});
themeToggle.textContent = "☀️";
localStorage.setItem("theme", "dark");
}
// 🚫 Disable Dark Mode
function disableDarkMode() {
body.classList.remove("dark-mode");
document.querySelector(".navbar").classList.remove("dark-mode");
document.getElementById("text-input").classList.remove("dark-mode");
document.querySelectorAll("button").forEach((btn) => {
btn.classList.remove("dark-mode");
});
document.querySelectorAll("input").forEach((input) => {
input.classList.remove("dark-mode");
});
themeToggle.textContent = "🌙";
localStorage.setItem("theme", "light");
}
document.getElementById("feedback-form").addEventListener("submit", function (e) {
e.preventDefault();
const feedbackText = document.getElementById("feedback-text").value.trim();
if (feedbackText === "") {
alert("Please enter feedback before submitting.");
return;
}
// Load existing feedbacks or initialize an empty array
let feedbackList = JSON.parse(localStorage.getItem("feedbacks")) || [];
// Add new feedback
feedbackList.push({
text: feedbackText,
timestamp: new Date().toLocaleString(),
});
// Save back to localStorage
localStorage.setItem("feedbacks", JSON.stringify(feedbackList));
alert("🎉 Feedback submitted successfully!");
document.getElementById("feedback-form").reset();
});