-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
word-counter.html
219 lines (186 loc) · 4.66 KB
/
word-counter.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Word counter</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
margin: 0;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
}
h1 {
color: #333;
margin-bottom: 20px;
}
.writing-section {
margin-bottom: 30px;
background: white;
padding: 20px;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.writing-area {
width: 100%;
min-height: 200px;
padding: 20px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
background: white;
font-size: 16px;
line-height: 1.5;
resize: vertical;
}
.controls {
display: flex;
justify-content: space-between;
align-items: center;
}
.stats {
color: #666;
font-size: 14px;
}
.save-status {
color: #888;
font-style: italic;
margin-left: 15px;
}
.add-btn {
background-color: #4CAF50;
color: white;
padding: 8px 16px;
border: none;
border-radius: 4px;
font-size: 14px;
cursor: pointer;
transition: background-color 0.2s;
}
.add-btn:hover {
background-color: #45a049;
}
.remove-btn {
background-color: #ff4444;
color: white;
padding: 8px 16px;
border: none;
border-radius: 4px;
font-size: 14px;
cursor: pointer;
transition: background-color 0.2s;
}
.remove-btn:hover {
background-color: #ff6666;
}
</style>
</head>
<body>
<div class="container">
<h1>Word counter</h1>
<div id="writing-container"></div>
<button class="add-btn" id="addSection">Add new section</button>
</div>
<script type="module">
const STORAGE_KEY = 'writing-sections'
const container = document.getElementById('writing-container')
const addButton = document.getElementById('addSection')
let saveTimeouts = new Map()
function generateId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2)
}
function createSection(id, content = '') {
const section = document.createElement('div')
section.className = 'writing-section'
section.dataset.id = id
section.innerHTML = `
<textarea class="writing-area" placeholder="Start writing here...">${content}</textarea>
<div class="controls">
<div class="stats">
Words: <span class="word-count">0</span>
<span class="save-status"></span>
</div>
<button class="remove-btn">Remove</button>
</div>
`
const textarea = section.querySelector('textarea')
const wordCount = section.querySelector('.word-count')
const saveStatus = section.querySelector('.save-status')
const removeBtn = section.querySelector('.remove-btn')
updateWordCount(textarea, wordCount)
textarea.addEventListener('input', () => {
updateWordCount(textarea, wordCount)
debouncedSave()
})
removeBtn.addEventListener('click', () => {
section.remove()
debouncedSave()
})
return section
}
function countWords(text) {
return text.trim() ? text.trim().split(/\s+/).length : 0
}
function updateWordCount(textarea, wordCountElement) {
const count = countWords(textarea.value)
wordCountElement.textContent = count
}
function saveToLocalStorage() {
const sectionsData = Array.from(container.children).map(section => ({
id: section.dataset.id,
content: section.querySelector('textarea').value
}))
localStorage.setItem(STORAGE_KEY, JSON.stringify(sectionsData))
// Update save status for all sections
document.querySelectorAll('.save-status').forEach(status => {
status.textContent = 'Saved'
setTimeout(() => {
status.textContent = ''
}, 2000)
})
}
function debouncedSave() {
// Clear any existing save timeout
if (saveTimeouts.has('save')) {
clearTimeout(saveTimeouts.get('save'))
}
// Show 'Saving...' status
document.querySelectorAll('.save-status').forEach(status => {
status.textContent = 'Saving...'
})
// Set new save timeout
saveTimeouts.set('save', setTimeout(() => {
saveTimeouts.delete('save')
saveToLocalStorage()
}, 1000))
}
// Load saved content
const savedContent = localStorage.getItem(STORAGE_KEY)
if (savedContent) {
const savedSections = JSON.parse(savedContent)
savedSections.forEach(section => {
const newSection = createSection(section.id, section.content)
container.appendChild(newSection)
})
}
// Add new section button handler
addButton.addEventListener('click', () => {
const newSection = createSection(generateId())
container.appendChild(newSection)
newSection.querySelector('textarea').focus()
})
// Click the button once if there's nothing on the page
if (!document.querySelectorAll('textarea').length) {
addButton.click();
}
</script>
</body>
</html>