2021-01-01 14:27:58 +00:00
|
|
|
/* global debounce */// toolbox.js
|
|
|
|
/* global installed */// manage.js
|
|
|
|
/* global
|
2022-01-28 23:54:56 +00:00
|
|
|
$$
|
2021-01-01 14:27:58 +00:00
|
|
|
$
|
|
|
|
$create
|
|
|
|
$isTextInput
|
|
|
|
animateElement
|
|
|
|
scrollElementIntoView
|
|
|
|
*/// dom.js
|
2017-11-29 02:39:24 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-01-28 23:54:56 +00:00
|
|
|
(async () => {
|
|
|
|
await require(['/manage/incremental-search.css']);
|
2017-11-29 02:39:24 +00:00
|
|
|
let prevText, focusedLink, focusedEntry;
|
|
|
|
let prevTime = performance.now();
|
|
|
|
let focusedName = '';
|
2017-12-03 21:12:09 +00:00
|
|
|
const input = $create('textarea', {
|
2022-01-28 23:54:56 +00:00
|
|
|
id: 'incremental-search',
|
2017-11-29 02:39:24 +00:00
|
|
|
spellcheck: false,
|
2017-12-09 15:25:44 +00:00
|
|
|
attributes: {tabindex: -1},
|
2017-11-29 02:39:24 +00:00
|
|
|
oninput: incrementalSearch,
|
|
|
|
});
|
|
|
|
replaceInlineStyle({
|
2020-10-27 10:00:32 +00:00
|
|
|
opacity: '0',
|
2017-11-29 02:39:24 +00:00
|
|
|
});
|
|
|
|
document.body.appendChild(input);
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('keydown', maybeRefocus, true);
|
2017-11-29 02:39:24 +00:00
|
|
|
|
2022-01-28 23:54:56 +00:00
|
|
|
function incrementalSearch(event, immediately) {
|
|
|
|
const {key} = event;
|
2017-11-29 02:39:24 +00:00
|
|
|
if (!immediately) {
|
|
|
|
debounce(incrementalSearch, 100, {}, true);
|
|
|
|
return;
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
const direction = key === 'ArrowUp' ? -1 : key === 'ArrowDown' ? 1 : 0;
|
2017-11-29 02:39:24 +00:00
|
|
|
const text = input.value.toLocaleLowerCase();
|
2022-01-28 23:54:56 +00:00
|
|
|
if (direction) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2017-11-29 02:39:24 +00:00
|
|
|
if (!text.trim() || !direction && (text === prevText || focusedName.startsWith(text))) {
|
|
|
|
prevText = text;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let textAtPos = 1e6;
|
|
|
|
let rotated;
|
2022-01-28 23:54:56 +00:00
|
|
|
const entries = $('#message-box') ? $$('.injection-order-entry') : [...installed.children];
|
2017-11-29 02:39:24 +00:00
|
|
|
const focusedIndex = entries.indexOf(focusedEntry);
|
|
|
|
if (focusedIndex > 0) {
|
|
|
|
if (direction > 0) {
|
|
|
|
rotated = entries.slice(focusedIndex + 1).concat(entries.slice(0, focusedIndex + 1));
|
|
|
|
} else if (direction < 0) {
|
2021-01-01 14:27:58 +00:00
|
|
|
rotated = entries.slice(0, focusedIndex).reverse()
|
|
|
|
.concat(entries.slice(focusedIndex).reverse());
|
2017-11-29 02:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let found;
|
|
|
|
for (const entry of rotated || entries) {
|
2020-10-25 13:29:17 +00:00
|
|
|
if (entry.classList.contains('hidden')) continue;
|
2022-09-04 13:59:19 +00:00
|
|
|
const name = entry.styleNameLC;
|
2017-11-29 02:39:24 +00:00
|
|
|
const pos = name.indexOf(text);
|
|
|
|
if (pos === 0) {
|
|
|
|
found = entry;
|
|
|
|
break;
|
|
|
|
} else if (pos > 0 && (pos < textAtPos || direction)) {
|
|
|
|
found = entry;
|
|
|
|
textAtPos = pos;
|
|
|
|
if (direction) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found && found !== focusedEntry) {
|
|
|
|
focusedEntry = found;
|
2022-01-28 23:54:56 +00:00
|
|
|
focusedLink = $('a', found);
|
2022-09-04 13:59:19 +00:00
|
|
|
focusedName = found.styleNameLC;
|
2017-11-29 22:13:13 +00:00
|
|
|
scrollElementIntoView(found, {invalidMarginRatio: .25});
|
2020-11-06 18:04:10 +00:00
|
|
|
animateElement(found, 'highlight-quick');
|
2020-10-25 13:29:17 +00:00
|
|
|
replaceInlineStyle({
|
|
|
|
width: focusedLink.offsetWidth + 'px',
|
|
|
|
height: focusedLink.offsetHeight + 'px',
|
2020-10-27 10:00:32 +00:00
|
|
|
opacity: '1',
|
2020-10-25 13:29:17 +00:00
|
|
|
});
|
|
|
|
focusedLink.prepend(input);
|
2022-01-28 23:54:56 +00:00
|
|
|
input.focus();
|
2017-11-29 02:39:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeRefocus(event) {
|
2022-01-28 23:54:56 +00:00
|
|
|
if (event.altKey || event.metaKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const modal = $('#message-box');
|
|
|
|
if (modal && !modal.classList.contains('injection-order')) {
|
2017-11-29 02:39:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const inTextInput = $isTextInput(event.target);
|
2020-10-13 18:14:54 +00:00
|
|
|
const {key, code, ctrlKey: ctrl} = event;
|
|
|
|
// `code` is independent of the current keyboard language
|
|
|
|
if ((code === 'KeyF' && ctrl && !event.shiftKey) ||
|
|
|
|
(code === 'Slash' || key === '/') && !ctrl && !inTextInput) {
|
|
|
|
// focus search field on "/" or Ctrl-F key
|
2017-11-29 02:39:24 +00:00
|
|
|
event.preventDefault();
|
2022-01-28 23:54:56 +00:00
|
|
|
if (!modal) $('#search').focus();
|
2017-11-29 02:39:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
if (ctrl || inTextInput && event.target !== input) {
|
2020-05-23 03:12:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-29 02:39:24 +00:00
|
|
|
const time = performance.now();
|
2020-10-13 18:14:54 +00:00
|
|
|
if (key.length === 1) {
|
2017-11-29 02:39:24 +00:00
|
|
|
if (time - prevTime > 1000) {
|
|
|
|
input.value = '';
|
|
|
|
}
|
2020-11-08 08:12:42 +00:00
|
|
|
// Space or Shift-Space is for page down/up
|
|
|
|
if (key === ' ' && !input.value) {
|
|
|
|
input.blur();
|
|
|
|
} else {
|
|
|
|
input.focus();
|
|
|
|
prevTime = time;
|
|
|
|
}
|
2017-11-29 02:39:24 +00:00
|
|
|
} else
|
2020-10-13 18:14:54 +00:00
|
|
|
if (key === 'Enter' && focusedLink) {
|
2017-11-29 02:39:24 +00:00
|
|
|
focusedLink.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
|
|
} else
|
2020-10-13 18:14:54 +00:00
|
|
|
if ((key === 'ArrowUp' || key === 'ArrowDown') && !event.shiftKey &&
|
2017-11-29 02:39:24 +00:00
|
|
|
time - prevTime < 5000 && incrementalSearch(event, true)) {
|
|
|
|
prevTime = time;
|
|
|
|
} else
|
|
|
|
if (event.target === input) {
|
|
|
|
(focusedLink || document.body).focus();
|
|
|
|
input.value = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceInlineStyle(css) {
|
|
|
|
for (const prop in css) {
|
|
|
|
input.style.setProperty(prop, css[prop], 'important');
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
})();
|