Skip to content

Commit 03ee90b

Browse files
committed
refactor(defaultTheme): simplify script logic
1 parent 40b6a0c commit 03ee90b

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/tpl/defaultTheme/frontend/index.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
const Enter = 'Enter';
2121
const Escape = 'Escape';
22-
const Esc = 'Esc';
2322
const Space = ' ';
2423

2524
let hasStorage = false;
@@ -109,16 +108,12 @@
109108
};
110109

111110
input.addEventListener('keydown', function (e) {
112-
switch (e.key) {
113-
case Enter:
114-
onEnter();
115-
e.preventDefault();
116-
break;
117-
case Escape:
118-
case Esc:
119-
onEscape();
120-
e.preventDefault();
121-
break;
111+
if (e.key === Enter) {
112+
onEnter();
113+
e.preventDefault();
114+
} else if (e.key === Escape) {
115+
onEscape();
116+
e.preventDefault();
122117
}
123118
});
124119
clear.addEventListener('click', function () {
@@ -137,8 +132,9 @@
137132
}
138133

139134
window.addEventListener('pagehide', function () {
140-
if (input.value) {
141-
sessionStorage.setItem(location.pathname, input.value);
135+
const inputValue = input.value;
136+
if (inputValue) {
137+
sessionStorage.setItem(location.pathname, inputValue);
142138
}
143139
});
144140
}
@@ -150,8 +146,7 @@
150146
function keepFocusOnBackwardForward() {
151147
function onFocus(e) {
152148
const link = e.target.closest('a');
153-
if (!link || link === lastFocused) return;
154-
lastFocused = link;
149+
if (link && link !== lastFocused) lastFocused = link;
155150
}
156151

157152
const itemList = document.body.querySelector(selectorItemList);
@@ -212,6 +207,7 @@
212207
lastFocused = elLink;
213208
elLink.focus();
214209
elLink.scrollIntoView({block: 'center'});
210+
break;
215211
}
216212
}
217213

@@ -223,16 +219,14 @@
223219
}
224220

225221
function getFocusableSibling(container, isBackward, startA) {
222+
if (!container.childElementCount) return;
226223
if (!startA) {
227224
startA = container.querySelector(':focus');
228225
}
229226
let startLI = startA && startA.closest('li');
230227
if (!startLI) {
231228
startLI = isBackward ? container.firstElementChild : container.lastElementChild;
232229
}
233-
if (!startLI) {
234-
return;
235-
}
236230

237231
let siblingLI = startLI;
238232
do {

0 commit comments

Comments
 (0)