2025-10-30 10:03:34 +07:00
|
|
|
(() => {
|
2025-10-30 19:09:21 +08:00
|
|
|
const initCommand = (container) => {
|
|
|
|
|
const input = container.querySelector('header input');
|
|
|
|
|
const listbox = container.querySelector('[role="listbox"]');
|
2025-10-30 10:03:34 +07:00
|
|
|
|
|
|
|
|
if (!input || !listbox) {
|
|
|
|
|
const missing = [];
|
|
|
|
|
if (!input) missing.push('input');
|
|
|
|
|
if (!listbox) missing.push('listbox');
|
2025-10-30 19:09:21 +08:00
|
|
|
console.error(`Command component initialization failed. Missing element(s): ${missing.join(', ')}`, container);
|
2025-10-30 10:03:34 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const options = Array.from(listbox.querySelectorAll('[role="option"]'));
|
|
|
|
|
let visibleOptions = [...options];
|
|
|
|
|
let activeIndex = -1;
|
|
|
|
|
|
|
|
|
|
const setActiveOption = (index) => {
|
|
|
|
|
if (activeIndex > -1 && options[activeIndex]) {
|
|
|
|
|
options[activeIndex].classList.remove('active');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activeIndex = index;
|
|
|
|
|
|
|
|
|
|
if (activeIndex > -1) {
|
|
|
|
|
const activeOption = options[activeIndex];
|
|
|
|
|
activeOption.classList.add('active');
|
|
|
|
|
if (activeOption.id) {
|
|
|
|
|
input.setAttribute('aria-activedescendant', activeOption.id);
|
|
|
|
|
} else {
|
|
|
|
|
input.removeAttribute('aria-activedescendant');
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
input.removeAttribute('aria-activedescendant');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const filterOptions = () => {
|
|
|
|
|
const searchTerm = input.value.trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
setActiveOption(-1);
|
|
|
|
|
|
|
|
|
|
visibleOptions = [];
|
|
|
|
|
options.forEach(option => {
|
|
|
|
|
const optionText = (option.dataset.label || option.textContent).trim().toLowerCase();
|
|
|
|
|
const keywords = (option.dataset.keywords || '').toLowerCase();
|
|
|
|
|
const matches = optionText.includes(searchTerm) || keywords.includes(searchTerm);
|
|
|
|
|
option.setAttribute('aria-hidden', String(!matches));
|
|
|
|
|
if (matches) {
|
|
|
|
|
visibleOptions.push(option);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-30 19:09:21 +08:00
|
|
|
if (visibleOptions.length > 0) {
|
|
|
|
|
setActiveOption(options.indexOf(visibleOptions[0]));
|
|
|
|
|
visibleOptions[0].scrollIntoView({ block: 'nearest' });
|
|
|
|
|
}
|
2025-10-30 10:03:34 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const selectOption = (option) => {
|
|
|
|
|
if (!option) return;
|
|
|
|
|
|
|
|
|
|
const event = new CustomEvent('command:select', {
|
|
|
|
|
detail: { value: option.dataset.value },
|
|
|
|
|
bubbles: true
|
|
|
|
|
});
|
2025-10-30 19:09:21 +08:00
|
|
|
container.dispatchEvent(event);
|
2025-10-30 10:03:34 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
input.addEventListener('input', filterOptions);
|
|
|
|
|
|
|
|
|
|
const handleKeyNavigation = (event) => {
|
|
|
|
|
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End'].includes(event.key)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (activeIndex > -1) {
|
|
|
|
|
selectOption(options[activeIndex]);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (visibleOptions.length === 0) return;
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
const currentVisibleIndex = activeIndex > -1 ? visibleOptions.indexOf(options[activeIndex]) : -1;
|
|
|
|
|
let nextVisibleIndex = currentVisibleIndex;
|
|
|
|
|
|
|
|
|
|
switch (event.key) {
|
|
|
|
|
case 'ArrowDown':
|
|
|
|
|
if (currentVisibleIndex < visibleOptions.length - 1) {
|
|
|
|
|
nextVisibleIndex = currentVisibleIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'ArrowUp':
|
|
|
|
|
if (currentVisibleIndex > 0) {
|
|
|
|
|
nextVisibleIndex = currentVisibleIndex - 1;
|
2025-10-30 19:09:21 +08:00
|
|
|
} else if (currentVisibleIndex === -1) {
|
|
|
|
|
nextVisibleIndex = 0;
|
2025-10-30 10:03:34 +07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'Home':
|
|
|
|
|
nextVisibleIndex = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 'End':
|
|
|
|
|
nextVisibleIndex = visibleOptions.length - 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-30 19:09:21 +08:00
|
|
|
if (nextVisibleIndex !== currentVisibleIndex) {
|
2025-10-30 10:03:34 +07:00
|
|
|
const newActiveOption = visibleOptions[nextVisibleIndex];
|
|
|
|
|
setActiveOption(options.indexOf(newActiveOption));
|
|
|
|
|
newActiveOption.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
listbox.addEventListener('mousemove', (event) => {
|
|
|
|
|
const option = event.target.closest('[role="option"]');
|
|
|
|
|
if (option && visibleOptions.includes(option)) {
|
|
|
|
|
const index = options.indexOf(option);
|
|
|
|
|
if (index !== activeIndex) {
|
|
|
|
|
setActiveOption(index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
listbox.addEventListener('mouseleave', () => {
|
2025-10-30 19:09:21 +08:00
|
|
|
if (visibleOptions.length > 0) {
|
|
|
|
|
setActiveOption(options.indexOf(visibleOptions[0]));
|
|
|
|
|
} else {
|
|
|
|
|
setActiveOption(-1);
|
|
|
|
|
}
|
2025-10-30 10:03:34 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
listbox.addEventListener('click', (event) => {
|
|
|
|
|
const clickedOption = event.target.closest('[role="option"]');
|
|
|
|
|
if (clickedOption && visibleOptions.includes(clickedOption)) {
|
|
|
|
|
selectOption(clickedOption);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.addEventListener('keydown', handleKeyNavigation);
|
|
|
|
|
|
2025-10-30 19:09:21 +08:00
|
|
|
if (visibleOptions.length > 0) {
|
|
|
|
|
setActiveOption(options.indexOf(visibleOptions[0]));
|
|
|
|
|
visibleOptions[0].scrollIntoView({ block: 'nearest' });
|
2025-10-30 10:03:34 +07:00
|
|
|
}
|
2025-10-30 19:09:21 +08:00
|
|
|
|
|
|
|
|
container.dataset.commandInitialized = true;
|
|
|
|
|
container.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
|
|
|
};
|
2025-10-30 10:03:34 +07:00
|
|
|
|
|
|
|
|
if (window.basecoat) {
|
|
|
|
|
window.basecoat.register('command', '.command:not([data-command-initialized])', initCommand);
|
|
|
|
|
}
|
|
|
|
|
})();
|