-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.js
More file actions
206 lines (169 loc) · 7.4 KB
/
dictionary.js
File metadata and controls
206 lines (169 loc) · 7.4 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
// dictionary.js — Custom Dictionary management page
let allWords = []; // { word, addedAt }
let sortMode = 'recent'; // 'alpha' | 'recent'
let searchQuery = '';
// ── Load ──────────────────────────────────────────────────────
function load() {
chrome.storage.local.get(['customDictionary', 'customDictionaryMeta'], (data) => {
const words = data.customDictionary || [];
const meta = data.customDictionaryMeta || {};
allWords = words.map(w => ({
word: w,
addedAt: meta[w] || 0,
}));
render();
});
}
// ── Save ──────────────────────────────────────────────────────
function save() {
const words = allWords.map(w => w.word);
const meta = {};
allWords.forEach(w => { meta[w.word] = w.addedAt; });
chrome.storage.local.set({ customDictionary: words, customDictionaryMeta: meta });
}
// ── Add ───────────────────────────────────────────────────────
function addWord(raw) {
const word = raw.trim().toLowerCase();
if (!word) return;
if (allWords.some(w => w.word === word)) {
toast('Already in dictionary');
return;
}
allWords.push({ word, addedAt: Date.now() });
save();
render();
toast(`Added "${word}"`);
}
// ── Remove ────────────────────────────────────────────────────
function removeWord(word) {
allWords = allWords.filter(w => w.word !== word);
save();
render();
}
// ── Render ────────────────────────────────────────────────────
function render() {
const now = Date.now();
const oneWeekAgo = now - 7 * 24 * 60 * 60 * 1000;
const recentCount = allWords.filter(w => w.addedAt > oneWeekAgo).length;
document.getElementById('totalCount').textContent = allWords.length;
document.getElementById('recentCount').textContent = recentCount;
// Filter
let filtered = allWords;
if (searchQuery) {
filtered = filtered.filter(w => w.word.includes(searchQuery));
}
// Sort
if (sortMode === 'alpha') {
filtered = [...filtered].sort((a, b) => a.word.localeCompare(b.word));
} else {
filtered = [...filtered].sort((a, b) => b.addedAt - a.addedAt);
}
document.getElementById('shownCount').textContent = filtered.length;
document.getElementById('listCountLabel').textContent =
filtered.length === allWords.length
? `${allWords.length} word${allWords.length !== 1 ? 's' : ''}`
: `${filtered.length} of ${allWords.length} words`;
const grid = document.getElementById('wordGrid');
if (filtered.length === 0) {
grid.innerHTML = `
<div class="empty-state" style="grid-column:1/-1">
<div class="empty-state-icon">📖</div>
<div class="empty-state-title">${allWords.length === 0 ? 'No words yet' : 'No matches'}</div>
<div class="empty-state-sub">${allWords.length === 0
? 'Add technical terms, names, and acronyms that GramVault should ignore.'
: 'Try a different search term.'}</div>
</div>`;
return;
}
grid.innerHTML = filtered.map(({ word }) => `
<div class="word-chip">
<span title="${escHtml(word)}">${escHtml(word)}</span>
<button class="word-remove" data-word="${escHtml(word)}" title="Remove">✕</button>
</div>
`).join('');
grid.querySelectorAll('.word-remove').forEach(btn => {
btn.addEventListener('click', () => removeWord(btn.dataset.word));
});
}
// ── Toast ─────────────────────────────────────────────────────
let toastTimer = null;
function toast(msg) {
const el = document.getElementById('toast');
el.textContent = msg;
el.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => el.classList.remove('show'), 2200);
}
// ── Helpers ───────────────────────────────────────────────────
function escHtml(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
// ── Event wiring ──────────────────────────────────────────────
document.getElementById('addBtn').addEventListener('click', () => {
const input = document.getElementById('newWordInput');
addWord(input.value);
input.value = '';
input.focus();
});
document.getElementById('newWordInput').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
addWord(e.target.value);
e.target.value = '';
}
});
document.getElementById('searchInput').addEventListener('input', (e) => {
searchQuery = e.target.value.trim().toLowerCase();
render();
});
document.getElementById('sortAlpha').addEventListener('click', () => {
sortMode = 'alpha';
document.getElementById('sortAlpha').style.background = '#ede9fe';
document.getElementById('sortAlpha').style.borderColor = '#a5b4fc';
document.getElementById('sortRecent').style.background = '';
document.getElementById('sortRecent').style.borderColor = '';
render();
});
document.getElementById('sortRecent').addEventListener('click', () => {
sortMode = 'recent';
document.getElementById('sortRecent').style.background = '#ede9fe';
document.getElementById('sortRecent').style.borderColor = '#a5b4fc';
document.getElementById('sortAlpha').style.background = '';
document.getElementById('sortAlpha').style.borderColor = '';
render();
});
document.getElementById('clearAll').addEventListener('click', () => {
if (allWords.length === 0) return;
if (!confirm(`Remove all ${allWords.length} words from the dictionary?`)) return;
allWords = [];
save();
render();
toast('Dictionary cleared');
});
document.getElementById('importBtn').addEventListener('click', () => {
const text = document.getElementById('importArea').value;
const words = text.split('\n').map(w => w.trim().toLowerCase()).filter(Boolean);
if (words.length === 0) return;
let added = 0;
words.forEach(w => {
if (!allWords.some(x => x.word === w)) {
allWords.push({ word: w, addedAt: Date.now() });
added++;
}
});
save();
render();
document.getElementById('importArea').value = '';
toast(`Imported ${added} word${added !== 1 ? 's' : ''} (${words.length - added} already existed)`);
});
document.getElementById('exportBtn').addEventListener('click', () => {
if (allWords.length === 0) { toast('Nothing to export'); return; }
const text = allWords.map(w => w.word).sort().join('\n');
navigator.clipboard.writeText(text).then(() => {
toast(`Copied ${allWords.length} words to clipboard`);
});
});
// ── Init ──────────────────────────────────────────────────────
// Set Recent as active by default
document.getElementById('sortRecent').style.background = '#ede9fe';
document.getElementById('sortRecent').style.borderColor = '#a5b4fc';
load();