Skip to content

Commit

Permalink
Fix cmdbar init bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nomi-san committed Aug 10, 2023
1 parent 6e3d4fe commit 9fef404
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions plugins/src/views/components/CommandBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ export function CommandBar() {
.map(item => item.item);
});

createEffect(on(filteredItems, () => {
setActiveIndex(0);
scrollToActiveItem();
}));

function shouldShowCategory(item, index) {
if (index == 0) return true;
if (typeof filteredItems()[index - 1] === 'undefined') return false;
return item.group !== filteredItems()[index - 1].group;
}

function scrollToActiveItem() {
if (!visible() || !itemsList) return;
const activeElement: HTMLElement = itemsList.querySelector(`div[data-index="${activeIndex()}"]`)!;
if (!activeElement) return;
const newScrollPos = (activeElement.offsetTop + activeElement.offsetHeight) - itemsList.offsetHeight;
Expand All @@ -52,6 +48,7 @@ export function CommandBar() {
}

function execute(index: number) {
if (!visible()) return;
const item = filteredItems()[index];
setActiveIndex(index);
setVisible(false);
Expand All @@ -77,6 +74,11 @@ export function CommandBar() {
}

onMount(() => {
createEffect(on(filteredItems, () => {
setActiveIndex(0);
scrollToActiveItem();
}));

window.addEventListener('keydown', e => {
if (e.ctrlKey && e.code === 'KeyK') {
e.preventDefault();
Expand Down

0 comments on commit 9fef404

Please sign in to comment.