2018-11-07 06:09:29 +00:00
|
|
|
/* global installed onDOMready $create debounce $ scrollElementIntoView
|
|
|
|
animateElement */
|
2017-11-29 02:39:24 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
onDOMready().then(() => {
|
|
|
|
let prevText, focusedLink, focusedEntry;
|
|
|
|
let prevTime = performance.now();
|
|
|
|
let focusedName = '';
|
2017-12-03 21:12:09 +00:00
|
|
|
const input = $create('textarea', {
|
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
|
|
|
position: 'absolute',
|
|
|
|
color: 'transparent',
|
|
|
|
border: '1px solid hsla(180, 100%, 100%, .5)',
|
2020-10-25 13:29:17 +00:00
|
|
|
margin: '-1px -2px',
|
2017-11-29 02:39:24 +00:00
|
|
|
overflow: 'hidden',
|
|
|
|
resize: 'none',
|
|
|
|
'background-color': 'hsla(180, 100%, 100%, .2)',
|
2020-10-25 13:29:17 +00:00
|
|
|
'box-sizing': 'content-box',
|
2017-11-29 02:39:24 +00:00
|
|
|
'pointer-events': 'none',
|
|
|
|
});
|
|
|
|
document.body.appendChild(input);
|
|
|
|
window.addEventListener('keydown', maybeRefocus, true);
|
|
|
|
|
2020-10-13 18:14:54 +00:00
|
|
|
function incrementalSearch({key}, immediately) {
|
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();
|
|
|
|
if (!text.trim() || !direction && (text === prevText || focusedName.startsWith(text))) {
|
|
|
|
prevText = text;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let textAtPos = 1e6;
|
|
|
|
let rotated;
|
|
|
|
const entries = [...installed.children];
|
|
|
|
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) {
|
|
|
|
rotated = entries.slice(0, focusedIndex).reverse().concat(entries.slice(focusedIndex).reverse());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let found;
|
|
|
|
for (const entry of rotated || entries) {
|
2020-10-25 13:29:17 +00:00
|
|
|
if (entry.classList.contains('hidden')) continue;
|
2017-11-29 02:39:24 +00:00
|
|
|
const name = entry.styleNameLowerCase;
|
|
|
|
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;
|
|
|
|
focusedLink = $('.style-name-link', found);
|
|
|
|
focusedName = found.styleNameLowerCase;
|
2017-11-29 22:13:13 +00:00
|
|
|
scrollElementIntoView(found, {invalidMarginRatio: .25});
|
2017-11-29 02:39:24 +00:00
|
|
|
animateElement(found, {className: '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);
|
2017-11-29 02:39:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeRefocus(event) {
|
2020-05-23 03:12:54 +00:00
|
|
|
if (event.altKey || event.metaKey || $('#message-box')) {
|
2017-11-29 02:39:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-13 18:14:54 +00:00
|
|
|
const inTextInput = $.isTextLikeInput(event.target);
|
|
|
|
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();
|
|
|
|
$('#search').focus();
|
|
|
|
return;
|
|
|
|
}
|
2020-10-22 05:51:48 +00:00
|
|
|
if (ctrl || inTextInput ||
|
|
|
|
key === ' ' && !input.value /* Space or Shift-Space is for page down/up */) {
|
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
|
|
|
input.focus();
|
|
|
|
if (time - prevTime > 1000) {
|
|
|
|
input.value = '';
|
|
|
|
}
|
|
|
|
prevTime = time;
|
|
|
|
} 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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|