Skip to content

Commit 88ad2f4

Browse files
committed
refactor(defaultTheme): refine enableKeyboardNavigate()
1 parent 9ed6723 commit 88ad2f4

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/tpl/defaultTheme/frontend/index.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,7 @@
236236
if (!startA) {
237237
startA = container.querySelector(':focus');
238238
}
239-
var startLI = startA;
240-
while (startLI && startLI.tagName !== 'LI') {
241-
startLI = startLI.parentElement;
242-
}
239+
var startLI = startA && startA.closest('li');
243240
if (!startLI) {
244241
if (isBackward) {
245242
startLI = container.firstElementChild;
@@ -254,15 +251,9 @@
254251
var siblingLI = startLI;
255252
do {
256253
if (isBackward) {
257-
siblingLI = siblingLI.previousElementSibling;
258-
if (!siblingLI) {
259-
siblingLI = container.lastElementChild;
260-
}
254+
siblingLI = siblingLI.previousElementSibling || container.lastElementChild;
261255
} else {
262-
siblingLI = siblingLI.nextElementSibling;
263-
if (!siblingLI) {
264-
siblingLI = container.firstElementChild;
265-
}
256+
siblingLI = siblingLI.nextElementSibling || container.firstElementChild;
266257
}
267258
} while (siblingLI !== startLI && (
268259
siblingLI.classList.contains(classNone) ||
@@ -287,8 +278,7 @@
287278
}
288279

289280
function getMatchedFocusableSibling(container, isBackward, startA, buf) {
290-
var skipRound = buf.length === 1; // find next prefix
291-
var matchKeyA;
281+
var skipRound = buf.length === 1; // find next single-char prefix
292282
var firstCheckA;
293283
var secondCheckA;
294284
var a = startA;
@@ -314,11 +304,10 @@
314304
}
315305

316306
var textContent = (a.querySelector('.name') || a).textContent.toLowerCase();
317-
if (buf.length <= textContent.length && textContent.substring(0, buf.length) === buf) {
307+
if (textContent.startsWith(buf)) {
318308
return a;
319309
}
320310
} while (a = getFocusableSibling(container, isBackward, a));
321-
return matchKeyA;
322311
}
323312

324313
var ARROW_UP = 'ArrowUp';
@@ -354,7 +343,7 @@
354343

355344
var currentLookupStartA;
356345
if (key === lookupKey) {
357-
// same as last key, lookup next for the same key as prefix
346+
// same as last key, lookup next single-char prefix
358347
currentLookupStartA = container.querySelector(':focus');
359348
} else {
360349
if (!lookupStartA) {
@@ -364,7 +353,7 @@
364353
if (lookupKey === undefined) {
365354
lookupKey = key;
366355
} else {
367-
// key changed, no more prefix match
356+
// key changed, no more single-char prefix match
368357
lookupKey = '';
369358
}
370359
}

0 commit comments

Comments
 (0)