-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (134 loc) · 5.95 KB
/
script.js
File metadata and controls
154 lines (134 loc) · 5.95 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
const DICT_API = "https://api.dictionaryapi.dev/api/v2/entries/en/";
const SUGGEST_API = "https://api.datamuse.com/sug?s=";
const input = document.getElementById('word-input');
const autoBox = document.getElementById('autocomplete-list');
const resultContainer = document.getElementById('result-container');
const starsList = document.getElementById('stars-list');
let favorites = JSON.parse(localStorage.getItem('lexi_stars')) || [];
let showPhonetics = JSON.parse(localStorage.getItem('lexi_phonetics')) ?? true;
// --- AUTOCOMPLETE LOGIC ---
input.addEventListener('input', async () => {
const query = input.value.trim();
if (query.length < 2) {
autoBox.classList.remove('active');
return;
}
try {
const res = await fetch(`${SUGGEST_API}${query}`);
const data = await res.json();
if (data.length > 0) {
autoBox.innerHTML = data.slice(0, 5).map(item => `<div onclick="selectWord('${item.word}')">${item.word}</div>`).join('');
autoBox.classList.add('active');
} else {
autoBox.classList.remove('active');
}
} catch (e) { console.error("Suggest error"); }
});
function selectWord(word) {
input.value = word;
autoBox.classList.remove('active');
fetchWord(word);
}
// Close autocomplete when clicking outside
document.addEventListener('click', (e) => {
if (!input.contains(e.target) && !autoBox.contains(e.target)) {
autoBox.classList.remove('active');
}
});
// --- CORE SEARCH LOGIC ---
async function fetchWord(word) {
const target = (word || input.value).trim().toLowerCase();
if (!target) return;
input.value = target;
autoBox.classList.remove('active');
resultContainer.innerHTML = "<p style='text-align:center;'>Searching...</p>";
try {
const res = await fetch(`${DICT_API}${target}`);
if (!res.ok) throw new Error();
const data = await res.json();
renderResult(data[0]);
} catch (err) {
resultContainer.innerHTML = `<div class="result-card"><h3>Word not found.</h3><p>Could not find "${target}". Please check spelling.</p></div>`;
}
}
function renderResult(data) {
const isStarred = favorites.includes(data.word.toLowerCase());
const phoneticStr = (showPhonetics && data.phonetic) ? data.phonetic : "";
let html = `
<div class="result-card">
<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px;">
<h1 style="margin:0; text-transform:capitalize;">${data.word} <span class="phonetic-text">${phoneticStr}</span></h1>
<button class="nav-link ${isStarred ? 'danger-btn' : ''}" style="width:auto; margin:0; ${isStarred ? 'background:#fee2e2;' : ''}" onclick="toggleStar('${data.word}')">
${isStarred ? 'Unstar' : 'Star Word'}
</button>
</div>
`;
data.meanings.forEach(m => {
html += `
<div class="meaning-section">
<span class="pos-label">${m.partOfSpeech}</span>
<ol class="definition-list">
${m.definitions.map(def => `<li>${def.definition}</li>`).join('')}
</ol>
${m.synonyms && m.synonyms.length ? `
<div class="synonyms">
<p style="margin:10px 0 5px; font-size:12px; font-weight:700; color:var(--text-muted);">SYNONYMS</p>
${m.synonyms.slice(0, 5).map(s => `<span class="synonym-tag" onclick="fetchWord('${s}')">${s}</span>`).join('')}
</div>
` : ''}
</div>
`;
});
resultContainer.innerHTML = html + `</div>`;
}
// --- APP HELPERS ---
function toggleStar(word) {
const w = word.toLowerCase();
favorites = favorites.includes(w) ? favorites.filter(item => item !== w) : [...favorites, w];
localStorage.setItem('lexi_stars', JSON.stringify(favorites));
fetchWord(w);
updateStarsUI();
}
function updateStarsUI() {
starsList.innerHTML = favorites.map(w => `
<li onclick="fetchWord('${w}'); closePanels();">
<span style="text-transform: capitalize; font-weight:600;">${w}</span>
<button class="remove-btn" onclick="event.stopPropagation(); removeStar('${w}')">Remove</button>
</li>
`).join('');
}
function removeStar(word) {
favorites = favorites.filter(w => w !== word.toLowerCase());
localStorage.setItem('lexi_stars', JSON.stringify(favorites));
updateStarsUI();
if (input.value.toLowerCase() === word.toLowerCase()) fetchWord(word);
}
function closePanels() {
document.getElementById('sidebar').classList.remove('open');
document.getElementById('settings-panel').classList.remove('open');
}
// --- HANDLERS ---
document.getElementById('theme-toggle').addEventListener('change', (e) => {
document.body.classList.toggle('dark-mode', e.target.checked);
localStorage.setItem('lexi_dark', e.target.checked);
});
document.getElementById('phonetic-toggle').addEventListener('change', (e) => {
showPhonetics = e.target.checked;
localStorage.setItem('lexi_phonetics', showPhonetics);
if(input.value) fetchWord(input.value);
});
document.getElementById('reset-all').onclick = () => {
if(confirm("Erase all data?")) { localStorage.clear(); location.reload(); }
};
document.getElementById('search-btn').onclick = () => fetchWord();
document.getElementById('open-sidebar').onclick = () => document.getElementById('sidebar').classList.add('open');
document.getElementById('open-settings').onclick = () => document.getElementById('settings-panel').classList.add('open');
document.getElementById('close-sidebar').onclick = closePanels;
document.getElementById('close-settings').onclick = closePanels;
input.onkeypress = (e) => { if(e.key === 'Enter') fetchWord(); };
// Init
if(localStorage.getItem('lexi_dark') === 'true') {
document.body.classList.add('dark-mode');
document.getElementById('theme-toggle').checked = true;
}
updateStarsUI();